]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
added newtWinEntries
authorewt <ewt>
Wed, 18 Feb 1998 19:29:10 +0000 (19:29 +0000)
committerewt <ewt>
Wed, 18 Feb 1998 19:29:10 +0000 (19:29 +0000)
newt.h
testgrid.c
windows.c

diff --git a/newt.h b/newt.h
index 8b300905fbc85383c488dc0e86c45bb53bc3fb3b..30663ce89d6558dce6bceaaf52f093ddb36a61a5 100644 (file)
--- a/newt.h
+++ b/newt.h
@@ -153,6 +153,8 @@ void newtListboxSelectItem(newtComponent co, int item,
        enum newtFlagsSense sense);
 
     
+newtComponent newtTextboxReflowed(int left, int top, char * text, int width,
+                                 int flexDown, int flexUp, int flags);
 newtComponent newtTextbox(int left, int top, int with, int height, int flags);
 void newtTextboxSetText(newtComponent co, const char * text);
 void newtTextboxSetHeight(newtComponent co, int height);
@@ -286,6 +288,17 @@ int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown,
                int flexUp, int maxListHeight, char ** items, int * listItem,
                char * button1, ...);
 
+struct newtWinEntry {
+    char * text;
+    char ** value;             /* may be initialized to set default */
+    int flags;
+};
+
+/* Returns the button number pressed, 0 on F12. The final values are
+   dynamically allocated, and need to be freed. */
+int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown, 
+                  int flexUp, int dataWidth, 
+                  struct newtWinEntry * items, char * button1, ...);
 
 #ifdef __cplusplus
 } /* End of extern "C" { */
index 198308fd5c7d7aeb40732af92f0cebf5cf4e7260..c7497faf4e34db17de4752ef84ec8c0deb9b472a 100644 (file)
@@ -12,6 +12,15 @@ int main(void) {
     char * flowedText;
     int textWidth, textHeight, rc;
     char * menuContents[] = { "One", "Two", "Three", "Four", "Five", NULL };
+    char * entries[10];
+    struct newtWinEntry autoEntries[] = {
+       { "An entry", entries + 0, 0 },
+       { "Another entry", entries + 1, 0 },
+       { "Third entry", entries + 2, 0 },
+       { "Fourth entry", entries + 3, 0 },
+       { NULL, NULL, 0 } };
+
+    memset(entries, 0, sizeof(entries));
 
     newtInit();
     newtCls();
@@ -84,7 +93,13 @@ int main(void) {
                     "depending on the need for one.", 50, 5, 5, 3, 
                     menuContents, &textWidth, "Ok", "Cancel", NULL);
 
+    rc = newtWinEntries("Text newtWinEntries()", "This is a sample invovation of "
+                    "newtWinEntries() call. It lets you get a lot of input "
+                    "quite easily.", 50, 5, 5, 20, autoEntries, "Ok", 
+                    "Cancel", NULL);
+
     newtFinished();
+
     printf("rc = 0x%x item = %d\n", rc, textWidth);
 
     return 0;
index 0360fd0a1be8e29d5196504f945d5f3366c83bc4..907d665aa7b5957f7df87bcc5e97bdd524a24daf 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -129,8 +129,6 @@ int newtWinTernary(char * title, char * button1, char * button2,
 int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown, 
                int flexUp, int maxListHeight, char ** items, int * listItem,
                char * button1, ...) {
-    char * reflowedText;
-    int width, height;
     newtComponent textbox, listbox, result, form;
     va_list args;
     newtComponent buttons[50];
@@ -140,11 +138,8 @@ int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown,
     int needScroll;
     char * buttonName;
 
-    reflowedText = newtReflowText(text, suggestedWidth, flexDown, flexUp,
-                                 &width, &height);
-    
-    textbox = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP);
-    newtTextboxSetText(textbox, reflowedText);
+    textbox = newtTextboxReflowed(-1, -1, text, suggestedWidth, flexDown,
+                                 flexUp, 0);
 
     for (i = 0; items[i]; i++) ;
     if (i < maxListHeight) maxListHeight = i;
@@ -201,3 +196,78 @@ 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;
+    newtGrid grid, buttonBar, subgrid;
+    int numItems;
+    int rc, i;
+    int numButtons;
+    char * buttonName;
+    va_list args;
+
+    textw = newtTextboxReflowed(-1, -1, text, suggestedWidth, flexDown,
+                               flexUp, 0);
+
+    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);
+
+    buttonBar = newtCreateGrid(numButtons, 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);
+    for (i = 0; i < numItems; i++) {
+       newtGridSetField(subgrid, 0, i, NEWT_GRID_COMPONENT,
+                        newtLabel(-1, -1, items[i].text),
+                        0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
+       newtGridSetField(subgrid, 1, i, NEWT_GRID_COMPONENT,
+                        newtEntry(-1, -1, items[i].value ? 
+                                   *items[i].value : NULL, dataWidth,
+                                   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);
+    newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid, 
+                    0, 1, 0, 0, 0, 0);
+    newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttonBar, 
+                    0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX);
+    newtGridAddComponentsToForm(grid, form, 1);
+    newtGridWrappedWindow(grid, title);
+    newtGridFree(grid, 1);
+
+    result = newtRunForm(form);
+
+    for (rc = 0; rc < numItems; rc++)
+       *items[rc].value = strdup(*items[rc].value);
+
+    for (rc = 0; result != buttons[rc] && rc < numButtons; rc++);
+    if (rc == numButtons) 
+       rc = 0; /* F12 */
+    else 
+       rc++;
+
+    newtFormDestroy(form);
+    newtPopWindow();
+
+    return rc;
+}