]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
added newtReflowText()
authorewt <ewt>
Fri, 12 Sep 1997 15:49:47 +0000 (15:49 +0000)
committerewt <ewt>
Fri, 12 Sep 1997 15:49:47 +0000 (15:49 +0000)
textbox.c

index c90782dfa565323813cc53d63e8c719615a00b65..e3f0aad9a8e5ba6713df00c3ce28c4d99a888d77 100644 (file)
--- a/textbox.c
+++ b/textbox.c
@@ -72,6 +72,89 @@ newtComponent newtTextbox(int left, int top, int width, int height, int flags) {
     return co;
 }
 
+static void doReflow(char * text, char ** resultPtr, int width, int * badness,
+                    int * heightPtr) {
+    char * result = NULL;
+    char * chptr, * end;
+    int howbad = 0;
+    int height = 0;
+
+    if (resultPtr) {
+       /* XXX I think this will work */
+       result = malloc(strlen(text) + (strlen(text) / width) + 2);
+       *result = '\0';
+    }
+    
+    while (*text) {
+       end = strchr(text, '\n');
+       if (!end)
+           end = text + strlen(text);
+
+       while (*text && text <= end) {
+           if (end - text < width) {
+               if (result) {
+                   strncat(result, text, end - text);
+                   strcat(result, "\n");
+                   height++;
+               }
+               text = end;
+               if (*text) text++;
+           } else {
+               chptr = text + width - 1;
+               while (chptr > text && !isspace(*chptr)) chptr--;
+               while (isspace(*chptr)) chptr--;
+               chptr++;
+
+               if (chptr > text)
+                   howbad += width - (chptr - text) + 1;
+               if (result) {
+                   strncat(result, text, chptr - text);
+                   strcat(result, "\n");
+                   height++;
+               }
+
+               text = chptr + 1;
+               while (isspace(*text)) text++;
+           }
+       }
+    }
+
+    if (badness) *badness = howbad;
+    if (resultPtr) *resultPtr = result;
+    if (heightPtr) *heightPtr = height;
+}
+
+char * newtReflowText(char * text, int width, int flexDown, int flexUp,
+                     int * actualWidth, int * actualHeight) {
+    int min, max;
+    int i;
+    char * result;
+    int minbad, minbadwidth, howbad;
+
+    if (flexDown || flexUp) {
+       min = width - flexDown;
+       max = width + flexUp;
+
+       minbad = -1;
+       minbadwidth = width;
+
+       for (i = min; i <= max; i++) {
+           doReflow(text, NULL, i, &howbad, NULL);
+
+           if (minbad == -1 || howbad < minbad) {
+               minbad = howbad;
+               minbadwidth = i;
+           }
+       }
+
+       width = minbadwidth;
+    }
+
+    doReflow(text, &result, width, NULL, actualHeight);
+    if (actualWidth) *actualWidth = width;
+    return result;
+}
+
 void newtTextboxSetText(newtComponent co, const char * text) {
     const char * start, * end;
     struct textbox * tb = co->data;