5 kx /*
5 kx * $Id: menubox.c,v 1.171 2020/11/23 21:03:11 tom Exp $
5 kx *
5 kx * menubox.c -- implements the menu box
5 kx *
5 kx * Copyright 2000-2019,2020 Thomas E. Dickey
5 kx *
5 kx * This program is free software; you can redistribute it and/or modify
5 kx * it under the terms of the GNU Lesser General Public Licens, version 2.1e
5 kx * as published by the Free Software Foundation.
5 kx *
5 kx * This program is distributed in the hope that it will be useful, but
5 kx * WITHOUT ANY WARRANTY; without even the implied warranty of
5 kx * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5 kx * Lesser General Public License for more details.
5 kx *
5 kx * You should have received a copy of the GNU Lesser General Public
5 kx * License along with this program; if not, write to
5 kx * Free Software Foundation, Inc.
5 kx * 51 Franklin St., Fifth Floor
5 kx * Boston, MA 02110, USA.
5 kx *
5 kx * An earlier version of this program lists as authors
5 kx * Savio Lam (lam836@cs.cuhk.hk)
5 kx */
5 kx
5 kx #include <dlg_internals.h>
5 kx #include <dlg_keys.h>
5 kx
5 kx typedef enum {
5 kx Unselected = 0,
5 kx Selected,
5 kx Editing
5 kx } Mode;
5 kx
5 kx typedef struct {
5 kx /* the outer-window */
5 kx WINDOW *dialog;
5 kx int box_y;
5 kx int box_x;
5 kx int tag_x;
5 kx int item_x;
5 kx int menu_height;
5 kx int menu_width;
5 kx /* the inner-window */
5 kx WINDOW *menu;
5 kx DIALOG_LISTITEM *items;
5 kx int item_no;
5 kx } ALL_DATA;
5 kx
5 kx #define MIN_HIGH 4
5 kx
5 kx #define INPUT_ROWS 3 /* rows per inputmenu entry */
5 kx
5 kx #define RowHeight(i) (is_inputmenu ? ((i) * INPUT_ROWS) : ((i) * 1))
5 kx #define ItemToRow(i) (is_inputmenu ? ((i) * INPUT_ROWS + 1) : (i))
5 kx #define RowToItem(i) (is_inputmenu ? ((i) / INPUT_ROWS + 0) : (i))
5 kx
5 kx /*
5 kx * Print menu item
5 kx */
5 kx static void
5 kx print_item(ALL_DATA * data,
5 kx WINDOW *win,
5 kx DIALOG_LISTITEM * item,
5 kx int choice,
5 kx Mode selected,
5 kx bool is_inputmenu)
5 kx {
5 kx chtype save = dlg_get_attrs(win);
5 kx int climit = (data->item_x - data->tag_x - GUTTER);
5 kx int my_width = data->menu_width;
5 kx int my_x = data->item_x;
5 kx int my_y = ItemToRow(choice);
5 kx bool both = (!dialog_vars.no_tags && !dialog_vars.no_items);
5 kx bool first = TRUE;
5 kx chtype bordchar;
5 kx const char *show = (dialog_vars.no_items
5 kx ? item->name
5 kx : item->text);
5 kx
5 kx switch (selected) {
5 kx default:
5 kx case Unselected:
5 kx bordchar = item_attr;
5 kx break;
5 kx case Selected:
5 kx bordchar = item_selected_attr;
5 kx break;
5 kx case Editing:
5 kx bordchar = dialog_attr;
5 kx break;
5 kx }
5 kx
5 kx /* Clear 'residue' of last item and mark current current item */
5 kx if (is_inputmenu) {
5 kx int n;
5 kx
5 kx dlg_attrset(win, (selected != Unselected) ? item_selected_attr : item_attr);
5 kx for (n = my_y - 1; n < my_y + INPUT_ROWS - 1; n++) {
5 kx wmove(win, n, 0);
5 kx wprintw(win, "%*s", my_width, " ");
5 kx }
5 kx } else {
5 kx dlg_attrset(win, menubox_attr);
5 kx wmove(win, my_y, 0);
5 kx wprintw(win, "%*s", my_width, " ");
5 kx }
5 kx
5 kx /* highlight first char of the tag to be special */
5 kx if (both) {
5 kx (void) wmove(win, my_y, data->tag_x);
5 kx dlg_print_listitem(win, item->name, climit, first, selected);
5 kx first = FALSE;
5 kx }
5 kx
5 kx /* Draw the input field box (only for inputmenu) */
5 kx (void) wmove(win, my_y, my_x);
5 kx if (is_inputmenu) {
5 kx my_width -= 1;
5 kx dlg_draw_box(win, my_y - 1, my_x, INPUT_ROWS, my_width - my_x - data->tag_x,
5 kx bordchar,
5 kx bordchar);
5 kx my_width -= 1;
5 kx ++my_x;
5 kx }
5 kx
5 kx /* print actual item */
5 kx wmove(win, my_y, my_x);
5 kx dlg_print_listitem(win, show, my_width - my_x, first, selected);
5 kx
5 kx if (selected) {
5 kx dlg_item_help(item->help);
5 kx }
5 kx dlg_attrset(win, save);
5 kx }
5 kx
5 kx /*
5 kx * Allow the user to edit the text of a menu entry.
5 kx */
5 kx static int
5 kx input_menu_edit(ALL_DATA * data,
5 kx DIALOG_LISTITEM * items,
5 kx int choice,
5 kx char **resultp)
5 kx {
5 kx chtype save = dlg_get_attrs(data->menu);
5 kx char *result;
5 kx int offset = 0;
5 kx int key = 0, fkey = 0;
5 kx bool first = TRUE;
5 kx /* see above */
5 kx bool is_inputmenu = TRUE;
5 kx int y = ItemToRow(choice);
5 kx int code = TRUE;
5 kx int max_len = dlg_max_input(MAX((int) strlen(items->text) + 1, MAX_LEN));
5 kx
5 kx result = dlg_malloc(char, (size_t) max_len);
5 kx assert_ptr(result, "input_menu_edit");
5 kx
5 kx /* original item is used to initialize the input string. */
5 kx result[0] = '\0';
5 kx strcpy(result, items->text);
5 kx
5 kx print_item(data, data->menu, items, choice, Editing, TRUE);
5 kx
5 kx /* taken out of inputbox.c - but somewhat modified */
5 kx for (;;) {
5 kx if (!first) {
5 kx int check = DLG_EXIT_UNKNOWN;
5 kx key = dlg_mouse_wgetch(data->menu, &fkey);
5 kx if (dlg_result_key(key, fkey, &check)) {
5 kx if (check == DLG_EXIT_CANCEL) {
5 kx code = FALSE;
5 kx break;
5 kx } else {
5 kx flash();
5 kx }
5 kx }
5 kx }
5 kx if (dlg_edit_string(result, &offset, key, fkey, first)) {
5 kx dlg_show_string(data->menu, result, offset, inputbox_attr,
5 kx y,
5 kx data->item_x + 1,
5 kx data->menu_width - data->item_x - 3,
5 kx FALSE, first);
5 kx first = FALSE;
5 kx } else if (key == ESC || key == TAB) {
5 kx code = FALSE;
5 kx break;
5 kx } else {
5 kx break;
5 kx }
5 kx }
5 kx print_item(data, data->menu, items, choice, Selected, TRUE);
5 kx dlg_attrset(data->menu, save);
5 kx
5 kx *resultp = result;
5 kx return code;
5 kx }
5 kx
5 kx static int
5 kx handle_button(int code, DIALOG_LISTITEM * items, int choice)
5 kx {
5 kx char *help_result;
5 kx
5 kx switch (code) {
5 kx case DLG_EXIT_OK: /* FALLTHRU */
5 kx case DLG_EXIT_EXTRA:
5 kx dlg_add_string(items[choice].name);
5 kx break;
5 kx case DLG_EXIT_HELP:
5 kx dlg_add_help_listitem(&code, &help_result, &items[choice]);
5 kx dlg_add_string(help_result);
5 kx break;
5 kx }
5 kx AddLastKey();
5 kx return code;
5 kx }
5 kx
5 kx int
5 kx dlg_renamed_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
5 kx {
5 kx if (dialog_vars.input_result)
5 kx dialog_vars.input_result[0] = '\0';
5 kx dlg_add_result("RENAMED ");
5 kx dlg_add_string(items[current].name);
5 kx dlg_add_result(" ");
5 kx dlg_add_string(newtext);
5 kx AddLastKey();
5 kx return DLG_EXIT_EXTRA;
5 kx }
5 kx
5 kx int
5 kx dlg_dummy_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
5 kx {
5 kx (void) items;
5 kx (void) current;
5 kx (void) newtext;
5 kx return DLG_EXIT_ERROR;
5 kx }
5 kx
5 kx static void
5 kx print_menu(ALL_DATA * data, int choice, int scrollamt, int max_choice, bool is_inputmenu)
5 kx {
5 kx int i;
5 kx
5 kx for (i = 0; i < max_choice; i++) {
5 kx print_item(data,
5 kx data->menu,
5 kx &data->items[i + scrollamt],
5 kx i,
5 kx (i == choice) ? Selected : Unselected,
5 kx is_inputmenu);
5 kx }
5 kx
5 kx /* Clean bottom lines */
5 kx if (is_inputmenu) {
5 kx int spare_lines, x_count;
5 kx spare_lines = data->menu_height % INPUT_ROWS;
5 kx dlg_attrset(data->menu, menubox_attr);
5 kx for (; spare_lines; spare_lines--) {
5 kx wmove(data->menu, data->menu_height - spare_lines, 0);
5 kx for (x_count = 0; x_count < data->menu_width;
5 kx x_count++) {
5 kx waddch(data->menu, ' ');
5 kx }
5 kx }
5 kx }
5 kx
5 kx (void) wnoutrefresh(data->menu);
5 kx
5 kx dlg_draw_scrollbar(data->dialog,
5 kx scrollamt,
5 kx scrollamt,
5 kx scrollamt + max_choice,
5 kx data->item_no,
5 kx data->box_x,
5 kx data->box_x + data->menu_width,
5 kx data->box_y,
5 kx data->box_y + data->menu_height + 1,
5 kx menubox_border2_attr,
5 kx menubox_border_attr);
5 kx }
5 kx
5 kx static bool
5 kx check_hotkey(DIALOG_LISTITEM * items, int choice)
5 kx {
5 kx bool result = FALSE;
5 kx
5 kx if (dlg_match_char(dlg_last_getc(),
5 kx (dialog_vars.no_tags
5 kx ? items[choice].text
5 kx : items[choice].name))) {
5 kx result = TRUE;
5 kx }
5 kx return result;
5 kx }
5 kx
5 kx /*
5 kx * This is an alternate interface to 'menu' which allows the application
5 kx * to read the list item states back directly without putting them in the
5 kx * output buffer.
5 kx */
5 kx int
5 kx dlg_menu(const char *title,
5 kx const char *cprompt,
5 kx int height,
5 kx int width,
5 kx int menu_height,
5 kx int item_no,
5 kx DIALOG_LISTITEM * items,
5 kx int *current_item,
5 kx DIALOG_INPUTMENU rename_menutext)
5 kx {
5 kx /* *INDENT-OFF* */
5 kx static DLG_KEYS_BINDING binding[] = {
5 kx HELPKEY_BINDINGS,
5 kx ENTERKEY_BINDINGS,
5 kx TOGGLEKEY_BINDINGS,
5 kx DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
5 kx DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
5 kx DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
5 kx DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
5 kx DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+' ),
5 kx DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
5 kx DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ),
5 kx DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ),
5 kx DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
5 kx DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ),
5 kx DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ),
5 kx DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_END ),
5 kx DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_LL ),
5 kx DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
5 kx DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
5 kx END_KEYS_BINDING
5 kx };
5 kx static DLG_KEYS_BINDING binding2[] = {
5 kx INPUTSTR_BINDINGS,
5 kx HELPKEY_BINDINGS,
5 kx ENTERKEY_BINDINGS,
5 kx END_KEYS_BINDING
5 kx };
5 kx /* *INDENT-ON* */
5 kx
5 kx #ifdef KEY_RESIZE
5 kx int old_LINES = LINES;
5 kx int old_COLS = COLS;
5 kx int old_height = height;
5 kx int old_width = width;
5 kx #endif
5 kx ALL_DATA all;
5 kx int i, j, x, y, cur_x, cur_y;
5 kx int fkey;
5 kx int button = dialog_state.visit_items ? -1 : dlg_default_button();
5 kx int choice = dlg_default_listitem(items);
5 kx int result = DLG_EXIT_UNKNOWN;
5 kx int scrollamt = 0;
5 kx int max_choice;
5 kx int use_width, name_width, text_width, list_width;
5 kx WINDOW *dialog, *menu;
5 kx char *prompt = 0;
5 kx const char **buttons = dlg_ok_labels();
5 kx bool is_inputmenu = ((rename_menutext != 0)
5 kx && (rename_menutext != dlg_dummy_menutext));
5 kx
5 kx DLG_TRACE(("# menubox args:\n"));
5 kx DLG_TRACE2S("title", title);
5 kx DLG_TRACE2S("message", cprompt);
5 kx DLG_TRACE2N("height", height);
5 kx DLG_TRACE2N("width", width);
5 kx DLG_TRACE2N("lheight", menu_height);
5 kx DLG_TRACE2N("llength", item_no);
5 kx /* FIXME dump the items[][] too */
5 kx DLG_TRACE2N("rename", rename_menutext != 0);
5 kx
5 kx dialog_state.plain_buttons = TRUE;
5 kx
5 kx all.items = items;
5 kx all.item_no = item_no;
5 kx
5 kx dlg_does_output();
5 kx
5 kx #ifdef KEY_RESIZE
5 kx retry:
5 kx #endif
5 kx
5 kx prompt = dlg_strclone(cprompt);
5 kx dlg_tab_correct_str(prompt);
5 kx
5 kx all.menu_height = menu_height;
5 kx use_width = dlg_calc_list_width(item_no, items) + 10;
5 kx use_width = MAX(26, use_width);
5 kx if (all.menu_height == 0) {
5 kx /* calculate height without items (4) */
5 kx dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, use_width);
5 kx dlg_calc_listh(&height, &all.menu_height, item_no);
5 kx } else {
5 kx dlg_auto_size(title, prompt,
5 kx &height, &width,
5 kx MIN_HIGH + all.menu_height, use_width);
5 kx }
5 kx dlg_button_layout(buttons, &width);
5 kx dlg_print_size(height, width);
5 kx dlg_ctl_size(height, width);
5 kx
5 kx x = dlg_box_x_ordinate(width);
5 kx y = dlg_box_y_ordinate(height);
5 kx
5 kx dialog = dlg_new_window(height, width, y, x);
5 kx all.dialog = dialog;
5 kx
5 kx dlg_register_window(dialog, "menubox", binding);
5 kx dlg_register_buttons(dialog, "menubox", buttons);
5 kx
5 kx dlg_mouse_setbase(x, y);
5 kx
5 kx dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
5 kx dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
5 kx dlg_draw_title(dialog, title);
5 kx
5 kx dlg_attrset(dialog, dialog_attr);
5 kx dlg_print_autowrap(dialog, prompt, height, width);
5 kx
5 kx all.menu_width = width - 6;
5 kx getyx(dialog, cur_y, cur_x);
5 kx all.box_y = cur_y + 1;
5 kx all.box_x = (width - all.menu_width) / 2 - 1;
5 kx
5 kx /*
5 kx * After displaying the prompt, we know how much space we really have.
5 kx * Limit the list to avoid overwriting the ok-button.
5 kx */
5 kx if (all.menu_height + MIN_HIGH > height - cur_y)
5 kx all.menu_height = height - MIN_HIGH - cur_y;
5 kx if (all.menu_height <= 0)
5 kx all.menu_height = 1;
5 kx
5 kx /* Find out maximal number of displayable items at once. */
5 kx max_choice = MIN(all.menu_height,
5 kx RowHeight(item_no));
5 kx if (is_inputmenu)
5 kx max_choice /= INPUT_ROWS;
5 kx
5 kx /* create new window for the menu */
5 kx menu = dlg_sub_window(dialog, all.menu_height, all.menu_width,
5 kx y + all.box_y + 1,
5 kx x + all.box_x + 1);
5 kx all.menu = menu;
5 kx
5 kx dlg_register_window(menu, "menu", binding2);
5 kx dlg_register_buttons(menu, "menu", buttons);
5 kx
5 kx /* draw a box around the menu items */
5 kx dlg_draw_box(dialog,
5 kx all.box_y, all.box_x,
5 kx all.menu_height + 2, all.menu_width + 2,
5 kx menubox_border_attr, menubox_border2_attr);
5 kx
5 kx name_width = 0;
5 kx text_width = 0;
5 kx
5 kx /* Find length of longest item to center menu *
5 kx * only if --menu was given, using --inputmenu *
5 kx * won't be centered. */
5 kx for (i = 0; i < item_no; i++) {
5 kx name_width = MAX(name_width, dlg_count_columns(items[i].name));
5 kx text_width = MAX(text_width, dlg_count_columns(items[i].text));
5 kx }
5 kx
5 kx /* If the name+text is wider than the list is allowed, then truncate
5 kx * one or both of them. If the name is no wider than 30% of the list,
5 kx * leave it intact.
5 kx *
5 kx * FIXME: the gutter width and name/list ratio should be configurable.
5 kx */
5 kx use_width = (all.menu_width - GUTTER);
5 kx if (dialog_vars.no_tags) {
5 kx list_width = MIN(use_width, text_width);
5 kx } else if (dialog_vars.no_items) {
5 kx list_width = MIN(use_width, name_width);
5 kx } else {
5 kx if (text_width >= 0
5 kx && name_width >= 0
5 kx && use_width > 0
5 kx && text_width + name_width > use_width) {
5 kx int need = (int) (0.30 * use_width);
5 kx if (name_width > need) {
5 kx int want = (int) (use_width
5 kx * ((double) name_width)
5 kx / (text_width + name_width));
5 kx name_width = (want > need) ? want : need;
5 kx }
5 kx text_width = use_width - name_width;
5 kx }
5 kx list_width = (text_width + name_width);
5 kx }
5 kx
5 kx all.tag_x = (is_inputmenu
5 kx ? 0
5 kx : (use_width - list_width) / 2);
5 kx all.item_x = ((dialog_vars.no_tags
5 kx ? 0
5 kx : (dialog_vars.no_items
5 kx ? 0
5 kx : (GUTTER + name_width)))
5 kx + all.tag_x);
5 kx
5 kx if (choice - scrollamt >= max_choice) {
5 kx scrollamt = choice - (max_choice - 1);
5 kx choice = max_choice - 1;
5 kx }
5 kx
5 kx print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
5 kx
5 kx /* register the new window, along with its borders */
5 kx dlg_mouse_mkbigregion(all.box_y + 1, all.box_x,
5 kx all.menu_height + 2, all.menu_width + 2,
5 kx KEY_MAX, 1, 1, 1 /* by lines */ );
5 kx
5 kx dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
5 kx
5 kx dlg_trace_win(dialog);
5 kx
5 kx while (result == DLG_EXIT_UNKNOWN) {
5 kx int key, found;
5 kx
5 kx if (button < 0) /* --visit-items */
5 kx wmove(dialog,
5 kx all.box_y + ItemToRow(choice) + 1,
5 kx all.box_x + all.tag_x + 1);
5 kx
5 kx key = dlg_mouse_wgetch(dialog, &fkey);
5 kx if (dlg_result_key(key, fkey, &result)) {
5 kx if (!dlg_button_key(result, &button, &key, &fkey))
5 kx break;
5 kx }
5 kx
5 kx found = FALSE;
5 kx if (fkey) {
5 kx /*
5 kx * Allow a mouse-click on a box to switch selection to that box.
5 kx * Handling a button click is a little more complicated, since we
5 kx * push a KEY_ENTER back onto the input stream so we'll put the
5 kx * cursor at the right place before handling the "keypress".
5 kx */
5 kx if (key >= DLGK_MOUSE(KEY_MAX)) {
5 kx key -= DLGK_MOUSE(KEY_MAX);
5 kx i = RowToItem(key);
5 kx if (i < max_choice) {
5 kx found = TRUE;
5 kx } else {
5 kx beep();
5 kx continue;
5 kx }
5 kx } else if (is_DLGK_MOUSE(key)
5 kx && dlg_ok_buttoncode(key - M_EVENT) >= 0) {
5 kx button = (key - M_EVENT);
5 kx ungetch('\n');
5 kx continue;
5 kx }
5 kx } else {
5 kx /*
5 kx * Check if key pressed matches first character of any item tag in
5 kx * list. If there is more than one match, we will cycle through
5 kx * each one as the same key is pressed repeatedly.
5 kx */
5 kx if (button < 0 || !dialog_state.visit_items) {
5 kx for (j = scrollamt + choice + 1; j < item_no; j++) {
5 kx if (check_hotkey(items, j)) {
5 kx found = TRUE;
5 kx i = j - scrollamt;
5 kx break;
5 kx }
5 kx }
5 kx if (!found) {
5 kx for (j = 0; j <= scrollamt + choice; j++) {
5 kx if (check_hotkey(items, j)) {
5 kx found = TRUE;
5 kx i = j - scrollamt;
5 kx break;
5 kx }
5 kx }
5 kx }
5 kx if (found)
5 kx dlg_flush_getc();
5 kx } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
5 kx button = j;
5 kx ungetch('\n');
5 kx continue;
5 kx }
5 kx
5 kx /*
5 kx * A single digit (1-9) positions the selection to that line in the
5 kx * current screen.
5 kx */
5 kx if (!found
5 kx && (key <= '9')
5 kx && (key > '0')
5 kx && (key - '1' < max_choice)) {
5 kx found = TRUE;
5 kx i = key - '1';
5 kx }
5 kx }
5 kx
5 kx if (!found && fkey) {
5 kx found = TRUE;
5 kx switch (key) {
5 kx case DLGK_PAGE_FIRST:
5 kx i = -scrollamt;
5 kx break;
5 kx case DLGK_PAGE_LAST:
5 kx i = item_no - 1 - scrollamt;
5 kx break;
5 kx case DLGK_MOUSE(KEY_PPAGE):
5 kx case DLGK_PAGE_PREV:
5 kx if (choice)
5 kx i = 0;
5 kx else if (scrollamt != 0)
5 kx i = -MIN(scrollamt, max_choice);
5 kx else
5 kx continue;
5 kx break;
5 kx case DLGK_MOUSE(KEY_NPAGE):
5 kx case DLGK_PAGE_NEXT:
5 kx i = MIN(choice + max_choice, item_no - scrollamt - 1);
5 kx break;
5 kx case DLGK_ITEM_PREV:
5 kx i = choice - 1;
5 kx if (choice == 0 && scrollamt == 0)
5 kx continue;
5 kx break;
5 kx case DLGK_ITEM_NEXT:
5 kx i = choice + 1;
5 kx if (scrollamt + choice >= item_no - 1)
5 kx continue;
5 kx break;
5 kx default:
5 kx found = FALSE;
5 kx break;
5 kx }
5 kx }
5 kx
5 kx if (found) {
5 kx if (i != choice) {
5 kx getyx(dialog, cur_y, cur_x);
5 kx if (i < 0 || i >= max_choice) {
5 kx if (i < 0) {
5 kx scrollamt += i;
5 kx choice = 0;
5 kx } else {
5 kx choice = max_choice - 1;
5 kx scrollamt += (i - max_choice + 1);
5 kx }
5 kx print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
5 kx } else {
5 kx choice = i;
5 kx print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
5 kx (void) wmove(dialog, cur_y, cur_x);
5 kx wrefresh(dialog);
5 kx }
5 kx }
5 kx continue; /* wait for another key press */
5 kx }
5 kx
5 kx if (fkey) {
5 kx switch (key) {
5 kx case DLGK_FIELD_PREV:
5 kx button = dlg_prev_button(buttons, button);
5 kx dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
5 kx FALSE, width);
5 kx break;
5 kx
5 kx case DLGK_FIELD_NEXT:
5 kx button = dlg_next_button(buttons, button);
5 kx dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
5 kx FALSE, width);
5 kx break;
5 kx
5 kx case DLGK_TOGGLE:
5 kx case DLGK_ENTER:
5 kx case DLGK_LEAVE:
5 kx result = ((key == DLGK_LEAVE)
5 kx ? dlg_ok_buttoncode(button)
5 kx : dlg_enter_buttoncode(button));
5 kx
5 kx /*
5 kx * If dlg_menu() is called from dialog_menu(), we want to
5 kx * capture the results into dialog_vars.input_result.
5 kx */
5 kx if (result == DLG_EXIT_ERROR) {
5 kx result = DLG_EXIT_UNKNOWN;
5 kx } else if (is_inputmenu
5 kx || rename_menutext == dlg_dummy_menutext) {
5 kx result = handle_button(result,
5 kx items,
5 kx scrollamt + choice);
5 kx }
5 kx
5 kx /*
5 kx * If we have a rename_menutext function, interpret the Extra
5 kx * button as a request to rename the menu's text. If that
5 kx * function doesn't return "Unknown", we will exit from this
5 kx * function. Usually that is done for dialog_menu(), so the
5 kx * shell script can use the updated value. If it does return
5 kx * "Unknown", update the list item only. A direct caller of
5 kx * dlg_menu() can free the renamed value - we cannot.
5 kx */
5 kx if (is_inputmenu && result == DLG_EXIT_EXTRA) {
5 kx char *tmp;
5 kx
5 kx if (input_menu_edit(&all,
5 kx &items[scrollamt + choice],
5 kx choice,
5 kx &tmp)) {
5 kx result = rename_menutext(items, scrollamt + choice, tmp);
5 kx if (result == DLG_EXIT_UNKNOWN) {
5 kx items[scrollamt + choice].text = tmp;
5 kx } else {
5 kx free(tmp);
5 kx }
5 kx } else {
5 kx result = DLG_EXIT_UNKNOWN;
5 kx print_item(&all,
5 kx menu,
5 kx &items[scrollamt + choice],
5 kx choice,
5 kx Selected,
5 kx is_inputmenu);
5 kx (void) wnoutrefresh(menu);
5 kx free(tmp);
5 kx }
5 kx
5 kx if (result == DLG_EXIT_UNKNOWN) {
5 kx dlg_draw_buttons(dialog, height - 2, 0,
5 kx buttons, button, FALSE, width);
5 kx }
5 kx }
5 kx break;
5 kx #ifdef KEY_RESIZE
5 kx case KEY_RESIZE:
5 kx dlg_will_resize(dialog);
5 kx /* reset data */
5 kx resizeit(height, LINES);
5 kx resizeit(width, COLS);
5 kx free(prompt);
5 kx _dlg_resize_cleanup(dialog);
5 kx /* repaint */
5 kx goto retry;
5 kx #endif
5 kx default:
5 kx flash();
5 kx break;
5 kx }
5 kx }
5 kx }
5 kx
5 kx dlg_mouse_free_regions();
5 kx dlg_unregister_window(menu);
5 kx dlg_del_window(dialog);
5 kx free(prompt);
5 kx
5 kx *current_item = scrollamt + choice;
5 kx
5 kx DLG_TRACE2N("current", *current_item);
5 kx return result;
5 kx }
5 kx
5 kx /*
5 kx * Display a menu for choosing among a number of options
5 kx */
5 kx int
5 kx dialog_menu(const char *title,
5 kx const char *cprompt,
5 kx int height,
5 kx int width,
5 kx int menu_height,
5 kx int item_no,
5 kx char **items)
5 kx {
5 kx int result;
5 kx int choice;
5 kx int i, j;
5 kx DIALOG_LISTITEM *listitems;
5 kx
5 kx listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
5 kx assert_ptr(listitems, "dialog_menu");
5 kx
5 kx for (i = j = 0; i < item_no; ++i) {
5 kx listitems[i].name = items[j++];
5 kx listitems[i].text = (dialog_vars.no_items
5 kx ? dlg_strempty()
5 kx : items[j++]);
5 kx listitems[i].help = ((dialog_vars.item_help)
5 kx ? items[j++]
5 kx : dlg_strempty());
5 kx }
5 kx dlg_align_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
5 kx
5 kx result = dlg_menu(title,
5 kx cprompt,
5 kx height,
5 kx width,
5 kx menu_height,
5 kx item_no,
5 kx listitems,
5 kx &choice,
5 kx (dialog_vars.input_menu
5 kx ? dlg_renamed_menutext
5 kx : dlg_dummy_menutext));
5 kx
5 kx dlg_free_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
5 kx free(listitems);
5 kx return result;
5 kx }