static void nmt_newt_form_build (NmtNewtForm *form) { NmtNewtFormPrivate *priv = NMT_NEWT_FORM_GET_PRIVATE (form); int screen_height, screen_width, form_height, form_width; newtComponent *cos; int i; priv->dirty = FALSE; nmt_newt_widget_realize (NMT_NEWT_WIDGET (form)); nmt_newt_widget_size_request (priv->content, &form_width, &form_height); newtGetScreenSize (&screen_width, &screen_height); if (!priv->fixed_width) priv->width = MIN (form_width + 2 * priv->padding, screen_width - 2); if (!priv->fixed_height) priv->height = MIN (form_height + 2 * priv->padding, screen_height - 2); if (!priv->fixed_x) priv->x = (screen_width - form_width) / 2; if (!priv->fixed_y) priv->y = (screen_height - form_height) / 2; nmt_newt_widget_size_allocate (priv->content, priv->padding, priv->padding, priv->width - 2 * priv->padding, priv->height - 2 * priv->padding); if (priv->height - 2 * priv->padding < form_height) { newtComponent scroll_bar = newtVerticalScrollbar (priv->width - 1, 0, priv->height, NEWT_COLORSET_WINDOW, NEWT_COLORSET_ACTCHECKBOX); priv->form = newtForm (scroll_bar, NULL, NEWT_FLAG_NOF12); newtFormAddComponent (priv->form, scroll_bar); newtFormSetHeight (priv->form, priv->height - 2); } else priv->form = newtForm (NULL, NULL, NEWT_FLAG_NOF12); if (priv->escape_exits) newtFormAddHotKey (priv->form, NEWT_KEY_ESCAPE); cos = nmt_newt_widget_get_components (priv->content); for (i = 0; cos[i]; i++) newtFormAddComponent (priv->form, cos[i]); g_free (cos); if (priv->focus) { newtComponent fco; fco = nmt_newt_widget_get_focus_component (priv->focus); if (fco) newtFormSetCurrent (priv->form, fco); } #ifdef HAVE_NEWTFORMGETSCROLLPOSITION if (priv->scroll_position) newtFormSetScrollPosition (priv->form, priv->scroll_position); #endif newtOpenWindow (priv->x, priv->y, priv->width, priv->height, priv->title_lc); }
int checkList(const char * text, int height, int width, poptContext optCon, int useRadio, int flags, char *** selections) { newtComponent form, okay, tb, subform, answer; newtComponent sb = NULL, cancel = NULL; const char * arg; char * end; int listHeight; int numBoxes = 0; int allocedBoxes = 5; int i; int numSelected; int rc = DLG_OKAY; char buf[80], format[20]; int maxWidth = 0; int top; struct { const char * text; const char * tag; newtComponent comp; } * cbInfo = malloc(allocedBoxes * sizeof(*cbInfo)); char * cbStates = malloc(allocedBoxes * sizeof(cbStates)); if (!(arg = poptGetArg(optCon))) return DLG_ERROR; listHeight = strtoul(arg, &end, 10); if (*end) return DLG_ERROR; while ((arg = poptGetArg(optCon))) { if (allocedBoxes == numBoxes) { allocedBoxes += 5; cbInfo = realloc(cbInfo, sizeof(*cbInfo) * allocedBoxes); cbStates = realloc(cbStates, sizeof(*cbStates) * allocedBoxes); } cbInfo[numBoxes].tag = arg; if (!(arg = poptGetArg(optCon))) return DLG_ERROR; if (!(flags & FLAG_NOITEM)) { cbInfo[numBoxes].text = arg; if (!(arg = poptGetArg(optCon))) return DLG_ERROR; } else cbInfo[numBoxes].text = ""; if (!strcmp(arg, "1") || !strcasecmp(arg, "on") || !strcasecmp(arg, "yes")) cbStates[numBoxes] = '*'; else cbStates[numBoxes] = ' '; if (wstrlen(cbInfo[numBoxes].tag,-1) > (unsigned int)maxWidth) maxWidth = wstrlen(cbInfo[numBoxes].tag,-1); numBoxes++; } form = newtForm(NULL, NULL, 0); tb = textbox(height - 3 - buttonHeight - listHeight, width - 2, text, flags, &top); if (listHeight < numBoxes) { sb = newtVerticalScrollbar(width - 4, top + 1, listHeight, NEWT_COLORSET_CHECKBOX, NEWT_COLORSET_ACTCHECKBOX); newtFormAddComponent(form, sb); } subform = newtForm(sb, NULL, 0); newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX); sprintf(format, "%%-%ds %%s", maxWidth); for (i = 0; i < numBoxes; i++) { sprintf(buf, format, cbInfo[i].tag, cbInfo[i].text); if (useRadio) cbInfo[i].comp = newtRadiobutton(4, top + 1 + i, buf, cbStates[i] != ' ', i ? cbInfo[i - 1].comp : NULL); else cbInfo[i].comp = newtCheckbox(4, top + 1 + i, buf, cbStates[i], NULL, cbStates + i); newtFormAddComponent(subform, cbInfo[i].comp); } newtFormSetHeight(subform, listHeight); newtFormSetWidth(subform, width - 10); newtFormAddComponents(form, tb, subform, NULL); addButtons(height, width, form, &okay, &cancel, flags); answer = newtRunForm(form); if (answer == cancel) rc = DLG_CANCEL; if (useRadio) { answer = newtRadioGetCurrent(cbInfo[0].comp); for (i = 0; i < numBoxes; i++) if (cbInfo[i].comp == answer) { *selections = malloc(sizeof(char *) * 2); (*selections)[0] = cbInfo[i].tag; (*selections)[1] = NULL; break; } } else { numSelected = 0; for (i = 0; i < numBoxes; i++) { if (cbStates[i] != ' ') numSelected++; } *selections = malloc(sizeof(char *) * (numSelected + 1)); numSelected = 0; for (i = 0; i < numBoxes; i++) { if (cbStates[i] != ' ') (*selections)[numSelected++] = cbInfo[i].tag; } (*selections)[numSelected] = NULL; } return rc; }
static int newtChecklist(const char* title, const char* message, unsigned int width, unsigned int height, unsigned int num_entries, const char** entries, int* states) { int ret; const int list_height = 4; char cbstates[num_entries]; for (unsigned int i = 0; i < num_entries; i++) { cbstates[i] = states[i] ? '*' : ' '; } newtCenteredWindow(width, height, title); newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6 - list_height, NEWT_FLAG_WRAP); newtTextboxSetText(textbox, message); int top = newtTextboxGetNumLines(textbox) + 2; newtComponent form = newtForm(NULL, NULL, 0); newtComponent sb = NULL; if (list_height < num_entries) { sb = newtVerticalScrollbar( width - 4, top + 1, list_height, NEWT_COLORSET_CHECKBOX, NEWT_COLORSET_ACTCHECKBOX); newtFormAddComponent(form, sb); } newtComponent subform = newtForm(sb, NULL, 0); newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX); newtFormSetHeight(subform, list_height); newtFormSetWidth(subform, width - 10); for (unsigned int i = 0; i < num_entries; i++) { newtComponent cb = newtCheckbox(4, top + i, entries[i], cbstates[i], NULL, &cbstates[i]); newtFormAddComponent(subform, cb); } newtFormAddComponents(form, textbox, subform, NULL); newtComponent btn_okay = newtButton((width - 18) / 3, height - 4, _("OK")); newtComponent btn_cancel = newtButton((width - 18) / 3 * 2 + 9, height - 4, _("Cancel")); newtFormAddComponents(form, btn_okay, btn_cancel, NULL); newtComponent answer = newtRunForm(form); if ((answer == NULL) || (answer == btn_cancel)) { ret = -1; } else { ret = 0; for (unsigned int i = 0; i < num_entries; i++) { states[i] = (cbstates[i] != ' '); if (states[i]) ret++; } } newtFormDestroy(form); newtPopWindow(); return ret; }
int main(void) { newtComponent b1, b2, r1, r2, r3, e2, e3, l1, l2, l3, scale; newtComponent lb, t, rsf, answer, timeLabel; newtComponent cs[10]; newtComponent f, chklist, e1; struct callbackInfo cbis[3]; char results[10]; char * enr2, * enr3, * scaleVal; void ** selectedList; int i, numsel; char buf[20]; const char * spinner = "-\\|/\\|/"; const char * spinState; struct newtExitStruct es; newtInit(); newtCls(); newtSetSuspendCallback(suspend, NULL); newtSetHelpCallback(helpCallback); newtDrawRootText(0, 0, "Newt test program"); newtPushHelpLine(NULL); newtDrawRootText(-50, 0, "More root text"); newtOpenWindow(2, 2, 30, 10, "first window"); newtOpenWindow(10, 5, 65, 16, "window 2"); f = newtForm(NULL, "This is some help text", 0); chklist = newtForm(NULL, NULL, 0); b1 = newtButton(3, 1, "Exit"); b2 = newtButton(18, 1, "Update"); r1 = newtRadiobutton(20, 10, "Choice 1", 0, NULL); r2 = newtRadiobutton(20, 11, "Chc 2", 1, r1); r3 = newtRadiobutton(20, 12, "Choice 3", 0, r2); rsf = newtForm(NULL, NULL, 0); newtFormAddComponents(rsf, r1, r2, r3, NULL); newtFormSetBackground(rsf, NEWT_COLORSET_CHECKBOX); for (i = 0; i < 10; i++) { sprintf(buf, "Check %d", i); cs[i] = newtCheckbox(3, 10 + i, buf, ' ', NULL, &results[i]); newtFormAddComponent(chklist, cs[i]); } l1 = newtLabel(3, 6, "Scale:"); l2 = newtLabel(3, 7, "Scrolls:"); l3 = newtLabel(3, 8, "Hidden:"); e1 = newtEntry(12, 6, "", 20, &scaleVal, 0); e2 = newtEntry(12, 7, "Default", 20, &enr2, NEWT_FLAG_SCROLL); /* e3 = newtEntry(12, 8, NULL, 20, &enr3, NEWT_FLAG_HIDDEN); */ e3 = newtEntry(12, 8, NULL, 20, &enr3, NEWT_FLAG_PASSWORD); cbis[0].state = &results[0]; cbis[0].en = e1; newtComponentAddCallback(cs[0], disableCallback, &cbis[0]); scale = newtScale(3, 14, 32, 100); newtFormSetHeight(chklist, 3); newtFormAddComponents(f, b1, b2, l1, l2, l3, e1, e2, e3, chklist, NULL); newtFormAddComponents(f, rsf, scale, NULL); lb = newtListbox(45, 1, 6, NEWT_FLAG_MULTIPLE | NEWT_FLAG_BORDER | NEWT_FLAG_SCROLL | NEWT_FLAG_SHOWCURSOR); newtListboxAppendEntry(lb, "First", (void *) 1); newtListboxAppendEntry(lb, "Second", (void *) 2); newtListboxAppendEntry(lb, "Third", (void *) 3); newtListboxAppendEntry(lb, "Fourth", (void *) 4); newtListboxAppendEntry(lb, "Sixth", (void *) 6); newtListboxAppendEntry(lb, "Seventh", (void *) 7); newtListboxAppendEntry(lb, "Eighth", (void *) 8); newtListboxAppendEntry(lb, "Ninth", (void *) 9); newtListboxAppendEntry(lb, "Tenth", (void *) 10); newtListboxInsertEntry(lb, "Fifth", (void *) 5, (void *) 4); newtListboxInsertEntry(lb, "Eleventh", (void *) 11, (void *) 10); newtListboxDeleteEntry(lb, (void *) 11); spinState = spinner; timeLabel = newtLabel(45, 8, "Spinner: -"); t = newtTextbox(45, 10, 17, 5, NEWT_FLAG_WRAP); newtTextboxSetText(t, "This is some text does it look okay?\nThis should be alone.\nThis shouldn't be printed"); newtFormAddComponents(f, lb, timeLabel, t, NULL); newtRefresh(); newtFormSetTimer(f, 200); do { newtFormRun(f, &es); if (es.reason == NEWT_EXIT_COMPONENT && es.u.co == b2) { newtScaleSet(scale, atoi(scaleVal)); newtRefresh(); answer = NULL; } else if (es.reason == NEWT_EXIT_TIMER) { spinState++; if (!*spinState) spinState = spinner; sprintf(buf, "Spinner: %c", *spinState); newtLabelSetText(timeLabel, buf); } } while (es.reason != NEWT_EXIT_COMPONENT || es.u.co == b2); scaleVal = strdup(scaleVal); enr2 = strdup(enr2); enr3 = strdup(enr3); selectedList = newtListboxGetSelection(lb, &numsel); newtFormDestroy(f); newtPopWindow(); newtPopWindow(); newtFinished(); printf("got string 1: %s\n", scaleVal); printf("got string 2: %s\n", enr2); printf("got string 3: %s\n", enr3); if(selectedList) { printf("\nSelected listbox items:\n"); for(i = 0; i < numsel; i++) puts(selectedList[i]); } return 0; }