]> git.ipfire.org Git - thirdparty/newt.git/blobdiff - windows.c
install python modules to purelib and platlib
[thirdparty/newt.git] / windows.c
index 907d665aa7b5957f7df87bcc5e97bdd524a24daf..29566a6bf553dda850cffb8b93b62b5e1768fe12 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -13,16 +13,21 @@ static void * newtvwindow(char * title, char * button1, char * button2,
     char * buf = NULL;
     int size = 0;
     int i = 0;
+    int scroll = 0;
     int width, height;
     char * flowedText;
     newtGrid grid, buttonGrid;
 
     do {
+       va_list argscopy;
+
+       va_copy(argscopy, args);
        size += 1000;
        if (buf) free(buf);
        buf = malloc(size);
-       i = vsnprintf(buf, size, message, args);
-    } while (i == size);
+       i = vsnprintf(buf, size, message, argscopy);
+       va_end(argscopy);
+    } while (i >= size || i == -1);
 
     flowedText = newtReflowText(buf, 35, 5, 5, &width, &height);
     if (height > 6) {
@@ -31,7 +36,11 @@ static void * newtvwindow(char * title, char * button1, char * button2,
     }
     free(buf);
 
-    t = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP);
+    if (height > 12) {
+       height = 12;
+       scroll = NEWT_FLAG_SCROLL;
+    }
+    t = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP | scroll);
     newtTextboxSetText(t, flowedText);
     free(flowedText);
 
@@ -47,9 +56,10 @@ static void * newtvwindow(char * title, char * button1, char * button2,
     newtGridSetField(buttonGrid, 0, 0, NEWT_GRID_COMPONENT, b1, 
                     0, 0, button2 ? 1 : 0, 0, 0, 0);
 
-    grid = newtGridVStacked(NEWT_GRID_COMPONENT, t,
-                           NEWT_GRID_SUBGRID, buttonGrid,
-                           NEWT_GRID_EMPTY);
+    grid = newtCreateGrid(1, 2);
+    newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, t, 0, 0, 0, 0, 0, 0);
+    newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, buttonGrid, 
+                    0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX);
     newtGridWrappedWindow(grid, title);
 
     f = newtForm(NULL, NULL, 0);
@@ -125,15 +135,14 @@ int newtWinTernary(char * title, char * button1, char * button2,
     return 0;
 }
 
-/* only supports up to 50 buttons -- shucks! */
 int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown, 
                int flexUp, int maxListHeight, char ** items, int * listItem,
                char * button1, ...) {
     newtComponent textbox, listbox, result, form;
     va_list args;
-    newtComponent buttons[50];
+    newtComponent *buttons = NULL;
     newtGrid grid, buttonBar;
-    int numButtons;
+    size_t totalButtons = 0, numButtons = 0;
     int i, rc;
     int needScroll;
     char * buttonName;
@@ -146,35 +155,32 @@ int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown,
     needScroll = i > maxListHeight;
 
     listbox = newtListbox(-1, -1, maxListHeight, 
-                 (needScroll ? 0 : NEWT_FLAG_NOSCROLL) | NEWT_FLAG_RETURNEXIT);
+                 (needScroll ? NEWT_FLAG_SCROLL : 0) | NEWT_FLAG_RETURNEXIT);
     for (i = 0; items[i]; i++) {
-       newtListboxAddEntry(listbox, items[i], (void *) i);
+       newtListboxAddEntry(listbox, items[i], (void *)(long) i);
     }
 
     newtListboxSetCurrent(listbox, *listItem);
 
-    buttonName = button1, numButtons = 0;
     va_start(args, button1);
-    while (buttonName) {
-       buttons[numButtons] = newtButton(-1, -1, buttonName);
-       numButtons++;
-       buttonName = va_arg(args, char *);
-    }
+    for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) 
+       ++totalButtons;                                                        
+    va_end(args);                                                             
 
-    va_end(button1);
+    buttons = (newtComponent *)alloca(sizeof(newtComponent)*(totalButtons)); 
+    va_start(args, button1);                                                  
+    for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) 
+       buttons[numButtons++] = newtButton(-1, -1, buttonName);                
+    va_end(args);                                                             
 
-    buttonBar = newtCreateGrid(numButtons, 1);
+    buttonBar = newtCreateGrid(numButtons ? numButtons : 1, 1);
     for (i = 0; i < numButtons; i++) {
        newtGridSetField(buttonBar, i, 0, NEWT_GRID_COMPONENT, 
                         buttons[i],
                         i ? 1 : 0, 0, 0, 0, 0, 0);
     }
 
-    grid = newtGridVStacked(NEWT_GRID_COMPONENT, textbox,
-                           NEWT_GRID_COMPONENT, listbox,
-                           NEWT_GRID_SUBGRID, buttonBar,
-                           NEWT_GRID_EMPTY);
-
+    grid = newtGridSimpleWindow(textbox, listbox, buttonBar);
     newtGridWrappedWindow(grid, title);
 
     form = newtForm(NULL, 0, 0);
@@ -185,7 +191,8 @@ int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown,
 
     *listItem = ((long) newtListboxGetCurrent(listbox));
 
-    for (rc = 0; result != buttons[rc] && rc < numButtons; rc++);
+    for (rc = 0; rc < numButtons && result != buttons[rc]; rc++)
+       ;
     if (rc == numButtons) 
        rc = 0; /* F12 or return-on-exit (which are the same for us) */
     else 
@@ -197,15 +204,14 @@ int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown,
     return rc;
 }
 
-/* only supports up to 50 buttons and entries -- shucks! */
 int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown, 
                   int flexUp, int dataWidth, 
                   struct newtWinEntry * items, char * button1, ...) {
-    newtComponent buttons[50], result, form, textw;
+    newtComponent *buttons, result, form, textw;
     newtGrid grid, buttonBar, subgrid;
     int numItems;
     int rc, i;
-    int numButtons;
+    size_t numButtons = 0, totalButtons = 0;
     char * buttonName;
     va_list args;
 
@@ -214,24 +220,25 @@ int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown,
 
     for (numItems = 0; items[numItems].text; numItems++); 
 
-    buttonName = button1, numButtons = 0;
     va_start(args, button1);
-    while (buttonName) {
-       buttons[numButtons] = newtButton(-1, -1, buttonName);
-       numButtons++;
-       buttonName = va_arg(args, char *);
-    }
-
-    va_end(button1);
+    for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) 
+       ++totalButtons;                                                        
+    va_end(args);                                                             
+    buttons = (newtComponent *)alloca(sizeof(newtComponent)*(totalButtons)); 
+    va_start(args, button1);                                                  
+    for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) 
+       buttons[numButtons++] = newtButton(-1, -1, buttonName);                
+    va_end(args);                                                             
 
-    buttonBar = newtCreateGrid(numButtons, 1);
+    buttonBar = newtCreateGrid(numButtons ? numButtons : 1, 1);
     for (i = 0; i < numButtons; i++) {
        newtGridSetField(buttonBar, i, 0, NEWT_GRID_COMPONENT, 
                         buttons[i],
                         i ? 1 : 0, 0, 0, 0, 0, 0);
     }
 
-    subgrid = newtCreateGrid(2, numItems);
+    subgrid = newtCreateGrid(2, numItems ? numItems : 1);
     for (i = 0; i < numItems; i++) {
        newtGridSetField(subgrid, 0, i, NEWT_GRID_COMPONENT,
                         newtLabel(-1, -1, items[i].text),
@@ -239,14 +246,14 @@ int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown,
        newtGridSetField(subgrid, 1, i, NEWT_GRID_COMPONENT,
                         newtEntry(-1, -1, items[i].value ? 
                                    *items[i].value : NULL, dataWidth,
-                                   items[i].value, items[i].flags),
+                                   (const char **)items[i].value, items[i].flags),
                         1, 0, 0, 0, 0, 0);
     }
 
     grid = newtCreateGrid(1, 3);
     form = newtForm(NULL, 0, 0);
     newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, textw, 
-                    0, 0, 0, 0, 0, 0);
+                    0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
     newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid, 
                     0, 1, 0, 0, 0, 0);
     newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttonBar, 
@@ -260,7 +267,8 @@ int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown,
     for (rc = 0; rc < numItems; rc++)
        *items[rc].value = strdup(*items[rc].value);
 
-    for (rc = 0; result != buttons[rc] && rc < numButtons; rc++);
+    for (rc = 0; rc < numButtons && result != buttons[rc]; rc++)
+       ;
     if (rc == numButtons) 
        rc = 0; /* F12 */
     else