int ui_browser__show(struct ui_browser *self, const char *title, const char *helpline, ...) { va_list ap; if (self->form != NULL) { newtFormDestroy(self->form); newtPopWindow(); } ui_browser__refresh_dimensions(self); newtCenteredWindow(self->width, self->height, title); self->form = newt_form__new(); if (self->form == NULL) return -1; self->sb = newtVerticalScrollbar(self->width, 0, self->height, HE_COLORSET_NORMAL, HE_COLORSET_SELECTED); if (self->sb == NULL) return -1; newtFormAddHotKey(self->form, NEWT_KEY_UP); newtFormAddHotKey(self->form, NEWT_KEY_DOWN); newtFormAddHotKey(self->form, NEWT_KEY_PGUP); newtFormAddHotKey(self->form, NEWT_KEY_PGDN); newtFormAddHotKey(self->form, NEWT_KEY_HOME); newtFormAddHotKey(self->form, NEWT_KEY_END); newtFormAddHotKey(self->form, ' '); newtFormAddComponent(self->form, self->sb); va_start(ap, helpline); ui_helpline__vpush(helpline, ap); va_end(ap); return 0; }
newtComponent newtTextbox(int left, int top, int width, int height, int flags) { newtComponent co; struct textbox * tb; co = malloc(sizeof(*co)); tb = malloc(sizeof(*tb)); co->data = tb; co->ops = &textboxOps; co->height = height; co->top = top; co->left = left; co->takesFocus = 0; co->width = width; tb->doWrap = flags & NEWT_FLAG_WRAP; tb->numLines = 0; tb->linesAlloced = 0; tb->lines = NULL; tb->topLine = 0; tb->textWidth = width; tb->blankline = malloc(width+1); memset(tb->blankline,' ',width); tb->blankline[width] = '\0'; if (flags & NEWT_FLAG_SCROLL) { co->width += 2; tb->sb = newtVerticalScrollbar(co->left + co->width - 1, co->top, co->height, COLORSET_TEXTBOX, COLORSET_TEXTBOX); } else { tb->sb = NULL; } return co; }
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); }
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 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; }
newtComponent newtListbox(int left, int top, int height, int flags) { newtComponent co, sb; struct listbox * li; if (!(co = malloc(sizeof(*co)))) return NULL; if (!(li = malloc(sizeof(struct listbox)))) { free(co); return NULL; } li->boxItems = NULL; li->numItems = 0; li->currItem = 0; li->numSelected = 0; li->isActive = 0; li->userHasSetWidth = 0; li->startShowItem = 0; li->sbAdjust = 0; li->bdxAdjust = 0; li->bdyAdjust = 0; li->flags = flags & (NEWT_FLAG_RETURNEXIT | NEWT_FLAG_BORDER | NEWT_FLAG_MULTIPLE | NEWT_FLAG_SHOWCURSOR); if (li->flags & NEWT_FLAG_BORDER) { li->bdxAdjust = 2; li->bdyAdjust = 1; } co->height = height; li->curHeight = co->height - (2 * li->bdyAdjust); if (height) { li->grow = 0; if (flags & NEWT_FLAG_SCROLL) { sb = newtVerticalScrollbar(left, top + li->bdyAdjust, li->curHeight, COLORSET_LISTBOX, COLORSET_ACTLISTBOX); li->sbAdjust = 3; } else { sb = NULL; } } else { li->grow = 1; sb = NULL; } li->sb = sb; co->data = li; co->isMapped = 0; co->left = left; co->top = top; co->ops = &listboxOps; co->takesFocus = 1; co->callback = NULL; updateWidth(co, li, 5); return co; }