xref: /freebsd/contrib/dialog/menubox.c (revision febdb468801f35e51c6c5c22221cfce9197c6f3b)
14c8945a0SNathan Whitehorn /*
2*febdb468SDevin Teske  *  $Id: menubox.c,v 1.148 2013/09/02 17:15:13 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  menubox.c -- implements the menu box
54c8945a0SNathan Whitehorn  *
6*febdb468SDevin Teske  *  Copyright 2000-2012,2013	Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public Licens, version 2.1e
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  *
234c8945a0SNathan Whitehorn  *  An earlier version of this program lists as authors
244c8945a0SNathan Whitehorn  *	Savio Lam (lam836@cs.cuhk.hk)
254c8945a0SNathan Whitehorn  */
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <dialog.h>
284c8945a0SNathan Whitehorn #include <dlg_keys.h>
294c8945a0SNathan Whitehorn 
304c8945a0SNathan Whitehorn typedef enum {
314c8945a0SNathan Whitehorn     Unselected = 0,
324c8945a0SNathan Whitehorn     Selected,
334c8945a0SNathan Whitehorn     Editing
344c8945a0SNathan Whitehorn } Mode;
354c8945a0SNathan Whitehorn 
362a3e3873SBaptiste Daroussin typedef struct {
372a3e3873SBaptiste Daroussin     /* the outer-window */
382a3e3873SBaptiste Daroussin     WINDOW *dialog;
392a3e3873SBaptiste Daroussin     int box_y;
402a3e3873SBaptiste Daroussin     int box_x;
412a3e3873SBaptiste Daroussin     int tag_x;
422a3e3873SBaptiste Daroussin     int item_x;
432a3e3873SBaptiste Daroussin     int menu_height;
442a3e3873SBaptiste Daroussin     int menu_width;
452a3e3873SBaptiste Daroussin     /* the inner-window */
462a3e3873SBaptiste Daroussin     WINDOW *menu;
472a3e3873SBaptiste Daroussin     DIALOG_LISTITEM *items;
482a3e3873SBaptiste Daroussin     int item_no;
492a3e3873SBaptiste Daroussin } ALL_DATA;
502a3e3873SBaptiste Daroussin 
514c8945a0SNathan Whitehorn #define MIN_HIGH  (1 + (5 * MARGIN))
524c8945a0SNathan Whitehorn 
534c8945a0SNathan Whitehorn #define INPUT_ROWS     3	/* rows per inputmenu entry */
544c8945a0SNathan Whitehorn 
554c8945a0SNathan Whitehorn #define RowHeight(i) (is_inputmenu ? ((i) * INPUT_ROWS) : ((i) * 1))
564c8945a0SNathan Whitehorn #define ItemToRow(i) (is_inputmenu ? ((i) * INPUT_ROWS + 1) : (i))
574c8945a0SNathan Whitehorn #define RowToItem(i) (is_inputmenu ? ((i) / INPUT_ROWS + 0) : (i))
584c8945a0SNathan Whitehorn 
594c8945a0SNathan Whitehorn /*
604c8945a0SNathan Whitehorn  * Print menu item
614c8945a0SNathan Whitehorn  */
624c8945a0SNathan Whitehorn static void
632a3e3873SBaptiste Daroussin print_item(ALL_DATA * data,
642a3e3873SBaptiste Daroussin 	   WINDOW *win,
652a3e3873SBaptiste Daroussin 	   DIALOG_LISTITEM * item,
664c8945a0SNathan Whitehorn 	   int choice,
674c8945a0SNathan Whitehorn 	   Mode selected,
684c8945a0SNathan Whitehorn 	   bool is_inputmenu)
694c8945a0SNathan Whitehorn {
707a1c0d96SNathan Whitehorn     chtype save = dlg_get_attrs(win);
714c8945a0SNathan Whitehorn     int n;
722a3e3873SBaptiste Daroussin     int climit = (data->item_x - data->tag_x - GUTTER);
732a3e3873SBaptiste Daroussin     int my_width = data->menu_width;
742a3e3873SBaptiste Daroussin     int my_x = data->item_x;
754c8945a0SNathan Whitehorn     int my_y = ItemToRow(choice);
762a3e3873SBaptiste Daroussin     bool both = (!dialog_vars.no_tags && !dialog_vars.no_items);
772a3e3873SBaptiste Daroussin     bool first = TRUE;
784c8945a0SNathan Whitehorn     chtype bordchar;
792a3e3873SBaptiste Daroussin     const char *show = (dialog_vars.no_items
802a3e3873SBaptiste Daroussin 			? item->name
812a3e3873SBaptiste Daroussin 			: item->text);
824c8945a0SNathan Whitehorn 
834c8945a0SNathan Whitehorn     switch (selected) {
844c8945a0SNathan Whitehorn     default:
854c8945a0SNathan Whitehorn     case Unselected:
864c8945a0SNathan Whitehorn 	bordchar = item_attr;
874c8945a0SNathan Whitehorn 	break;
884c8945a0SNathan Whitehorn     case Selected:
894c8945a0SNathan Whitehorn 	bordchar = item_selected_attr;
904c8945a0SNathan Whitehorn 	break;
914c8945a0SNathan Whitehorn     case Editing:
924c8945a0SNathan Whitehorn 	bordchar = dialog_attr;
934c8945a0SNathan Whitehorn 	break;
944c8945a0SNathan Whitehorn     }
954c8945a0SNathan Whitehorn 
964c8945a0SNathan Whitehorn     /* Clear 'residue' of last item and mark current current item */
974c8945a0SNathan Whitehorn     if (is_inputmenu) {
982a3e3873SBaptiste Daroussin 	(void) wattrset(win, (selected != Unselected) ? item_selected_attr : item_attr);
994c8945a0SNathan Whitehorn 	for (n = my_y - 1; n < my_y + INPUT_ROWS - 1; n++) {
1004c8945a0SNathan Whitehorn 	    wmove(win, n, 0);
1014c8945a0SNathan Whitehorn 	    wprintw(win, "%*s", my_width, " ");
1024c8945a0SNathan Whitehorn 	}
1034c8945a0SNathan Whitehorn     } else {
1042a3e3873SBaptiste Daroussin 	(void) wattrset(win, menubox_attr);
1054c8945a0SNathan Whitehorn 	wmove(win, my_y, 0);
1064c8945a0SNathan Whitehorn 	wprintw(win, "%*s", my_width, " ");
1074c8945a0SNathan Whitehorn     }
1084c8945a0SNathan Whitehorn 
1092a3e3873SBaptiste Daroussin     /* highlight first char of the tag to be special */
1102a3e3873SBaptiste Daroussin     if (both) {
1112a3e3873SBaptiste Daroussin 	(void) wmove(win, my_y, data->tag_x);
1122a3e3873SBaptiste Daroussin 	dlg_print_listitem(win, item->name, climit, first, selected);
1132a3e3873SBaptiste Daroussin 	first = FALSE;
1142a3e3873SBaptiste Daroussin     }
1154c8945a0SNathan Whitehorn 
1164c8945a0SNathan Whitehorn     /* Draw the input field box (only for inputmenu) */
1174c8945a0SNathan Whitehorn     (void) wmove(win, my_y, my_x);
1184c8945a0SNathan Whitehorn     if (is_inputmenu) {
1194c8945a0SNathan Whitehorn 	my_width -= 1;
1202a3e3873SBaptiste Daroussin 	dlg_draw_box(win, my_y - 1, my_x, INPUT_ROWS, my_width - my_x - data->tag_x,
1214c8945a0SNathan Whitehorn 		     bordchar,
1224c8945a0SNathan Whitehorn 		     bordchar);
1234c8945a0SNathan Whitehorn 	my_width -= 1;
1244c8945a0SNathan Whitehorn 	++my_x;
1254c8945a0SNathan Whitehorn     }
1264c8945a0SNathan Whitehorn 
1274c8945a0SNathan Whitehorn     /* print actual item */
1284c8945a0SNathan Whitehorn     wmove(win, my_y, my_x);
1292a3e3873SBaptiste Daroussin     dlg_print_listitem(win, show, my_width - my_x, first, selected);
1304c8945a0SNathan Whitehorn 
1314c8945a0SNathan Whitehorn     if (selected) {
1322a3e3873SBaptiste Daroussin 	dlg_item_help(item->help);
1334c8945a0SNathan Whitehorn     }
1342a3e3873SBaptiste Daroussin     (void) wattrset(win, save);
1354c8945a0SNathan Whitehorn }
1364c8945a0SNathan Whitehorn 
1374c8945a0SNathan Whitehorn /*
1384c8945a0SNathan Whitehorn  * Allow the user to edit the text of a menu entry.
1394c8945a0SNathan Whitehorn  */
1404c8945a0SNathan Whitehorn static int
1412a3e3873SBaptiste Daroussin input_menu_edit(ALL_DATA * data,
1424c8945a0SNathan Whitehorn 		DIALOG_LISTITEM * items,
1434c8945a0SNathan Whitehorn 		int choice,
1444c8945a0SNathan Whitehorn 		char **resultp)
1454c8945a0SNathan Whitehorn {
1462a3e3873SBaptiste Daroussin     chtype save = dlg_get_attrs(data->menu);
1474c8945a0SNathan Whitehorn     char *result;
1484c8945a0SNathan Whitehorn     int offset = 0;
1494c8945a0SNathan Whitehorn     int key = 0, fkey = 0;
1504c8945a0SNathan Whitehorn     int first = TRUE;
1514c8945a0SNathan Whitehorn     /* see above */
1524c8945a0SNathan Whitehorn     bool is_inputmenu = TRUE;
1534c8945a0SNathan Whitehorn     int y = ItemToRow(choice);
1544c8945a0SNathan Whitehorn     int code = TRUE;
1554c8945a0SNathan Whitehorn     int max_len = dlg_max_input(MAX((int) strlen(items->text) + 1, MAX_LEN));
1564c8945a0SNathan Whitehorn 
1574c8945a0SNathan Whitehorn     result = dlg_malloc(char, (size_t) max_len);
1584c8945a0SNathan Whitehorn     assert_ptr(result, "input_menu_edit");
1594c8945a0SNathan Whitehorn 
1604c8945a0SNathan Whitehorn     /* original item is used to initialize the input string. */
1614c8945a0SNathan Whitehorn     result[0] = '\0';
1624c8945a0SNathan Whitehorn     strcpy(result, items->text);
1634c8945a0SNathan Whitehorn 
1642a3e3873SBaptiste Daroussin     print_item(data, data->menu, items, choice, Editing, TRUE);
1654c8945a0SNathan Whitehorn 
1664c8945a0SNathan Whitehorn     /* taken out of inputbox.c - but somewhat modified */
1674c8945a0SNathan Whitehorn     for (;;) {
1684c8945a0SNathan Whitehorn 	if (!first)
1692a3e3873SBaptiste Daroussin 	    key = dlg_mouse_wgetch(data->menu, &fkey);
1704c8945a0SNathan Whitehorn 	if (dlg_edit_string(result, &offset, key, fkey, first)) {
1712a3e3873SBaptiste Daroussin 	    dlg_show_string(data->menu, result, offset, inputbox_attr,
1722a3e3873SBaptiste Daroussin 			    y,
1732a3e3873SBaptiste Daroussin 			    data->item_x + 1,
1742a3e3873SBaptiste Daroussin 			    data->menu_width - data->item_x - 3,
1754c8945a0SNathan Whitehorn 			    FALSE, first);
1764c8945a0SNathan Whitehorn 	    first = FALSE;
1774c8945a0SNathan Whitehorn 	} else if (key == ESC || key == TAB) {
1784c8945a0SNathan Whitehorn 	    code = FALSE;
1794c8945a0SNathan Whitehorn 	    break;
1804c8945a0SNathan Whitehorn 	} else {
1814c8945a0SNathan Whitehorn 	    break;
1824c8945a0SNathan Whitehorn 	}
1834c8945a0SNathan Whitehorn     }
1842a3e3873SBaptiste Daroussin     print_item(data, data->menu, items, choice, Selected, TRUE);
1852a3e3873SBaptiste Daroussin     (void) wattrset(data->menu, save);
1864c8945a0SNathan Whitehorn 
1874c8945a0SNathan Whitehorn     *resultp = result;
1884c8945a0SNathan Whitehorn     return code;
1894c8945a0SNathan Whitehorn }
1904c8945a0SNathan Whitehorn 
1914c8945a0SNathan Whitehorn static int
1924c8945a0SNathan Whitehorn handle_button(int code, DIALOG_LISTITEM * items, int choice)
1934c8945a0SNathan Whitehorn {
194*febdb468SDevin Teske     char *help_result;
195*febdb468SDevin Teske 
1964c8945a0SNathan Whitehorn     switch (code) {
1974c8945a0SNathan Whitehorn     case DLG_EXIT_OK:		/* FALLTHRU */
1984c8945a0SNathan Whitehorn     case DLG_EXIT_EXTRA:
1994c8945a0SNathan Whitehorn 	dlg_add_string(items[choice].name);
2004c8945a0SNathan Whitehorn 	break;
2014c8945a0SNathan Whitehorn     case DLG_EXIT_HELP:
202*febdb468SDevin Teske 	dlg_add_help_listitem(&code, &help_result, &items[choice]);
203*febdb468SDevin Teske 	dlg_add_string(help_result);
2044c8945a0SNathan Whitehorn 	break;
2054c8945a0SNathan Whitehorn     }
2064c8945a0SNathan Whitehorn     return code;
2074c8945a0SNathan Whitehorn }
2084c8945a0SNathan Whitehorn 
2092a3e3873SBaptiste Daroussin int
2104c8945a0SNathan Whitehorn dlg_renamed_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
2114c8945a0SNathan Whitehorn {
2124c8945a0SNathan Whitehorn     if (dialog_vars.input_result)
2134c8945a0SNathan Whitehorn 	dialog_vars.input_result[0] = '\0';
2144c8945a0SNathan Whitehorn     dlg_add_result("RENAMED ");
2154c8945a0SNathan Whitehorn     dlg_add_string(items[current].name);
2164c8945a0SNathan Whitehorn     dlg_add_result(" ");
2174c8945a0SNathan Whitehorn     dlg_add_string(newtext);
2184c8945a0SNathan Whitehorn     return DLG_EXIT_EXTRA;
2194c8945a0SNathan Whitehorn }
2204c8945a0SNathan Whitehorn 
2212a3e3873SBaptiste Daroussin int
2224c8945a0SNathan Whitehorn dlg_dummy_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
2234c8945a0SNathan Whitehorn {
2244c8945a0SNathan Whitehorn     (void) items;
2254c8945a0SNathan Whitehorn     (void) current;
2264c8945a0SNathan Whitehorn     (void) newtext;
2274c8945a0SNathan Whitehorn     return DLG_EXIT_ERROR;
2284c8945a0SNathan Whitehorn }
2294c8945a0SNathan Whitehorn 
2302a3e3873SBaptiste Daroussin static void
2312a3e3873SBaptiste Daroussin print_menu(ALL_DATA * data, int choice, int scrollamt, int max_choice, bool is_inputmenu)
2322a3e3873SBaptiste Daroussin {
2332a3e3873SBaptiste Daroussin     int i;
2342a3e3873SBaptiste Daroussin 
2352a3e3873SBaptiste Daroussin     for (i = 0; i < max_choice; i++) {
2362a3e3873SBaptiste Daroussin 	print_item(data,
2372a3e3873SBaptiste Daroussin 		   data->menu,
2382a3e3873SBaptiste Daroussin 		   &data->items[i + scrollamt],
2392a3e3873SBaptiste Daroussin 		   i,
2402a3e3873SBaptiste Daroussin 		   (i == choice) ? Selected : Unselected,
2412a3e3873SBaptiste Daroussin 		   is_inputmenu);
2422a3e3873SBaptiste Daroussin     }
2432a3e3873SBaptiste Daroussin 
2442a3e3873SBaptiste Daroussin     /* Clean bottom lines */
2452a3e3873SBaptiste Daroussin     if (is_inputmenu) {
2462a3e3873SBaptiste Daroussin 	int spare_lines, x_count;
2472a3e3873SBaptiste Daroussin 	spare_lines = data->menu_height % INPUT_ROWS;
2482a3e3873SBaptiste Daroussin 	(void) wattrset(data->menu, menubox_attr);
2492a3e3873SBaptiste Daroussin 	for (; spare_lines; spare_lines--) {
2502a3e3873SBaptiste Daroussin 	    wmove(data->menu, data->menu_height - spare_lines, 0);
2512a3e3873SBaptiste Daroussin 	    for (x_count = 0; x_count < data->menu_width;
2522a3e3873SBaptiste Daroussin 		 x_count++) {
2532a3e3873SBaptiste Daroussin 		waddch(data->menu, ' ');
2542a3e3873SBaptiste Daroussin 	    }
2552a3e3873SBaptiste Daroussin 	}
2562a3e3873SBaptiste Daroussin     }
2572a3e3873SBaptiste Daroussin 
2582a3e3873SBaptiste Daroussin     (void) wnoutrefresh(data->menu);
2592a3e3873SBaptiste Daroussin 
2602a3e3873SBaptiste Daroussin     dlg_draw_scrollbar(data->dialog,
2612a3e3873SBaptiste Daroussin 		       scrollamt,
2622a3e3873SBaptiste Daroussin 		       scrollamt,
2632a3e3873SBaptiste Daroussin 		       scrollamt + max_choice,
2642a3e3873SBaptiste Daroussin 		       data->item_no,
2652a3e3873SBaptiste Daroussin 		       data->box_x,
2662a3e3873SBaptiste Daroussin 		       data->box_x + data->menu_width,
2672a3e3873SBaptiste Daroussin 		       data->box_y,
2682a3e3873SBaptiste Daroussin 		       data->box_y + data->menu_height + 1,
2692a3e3873SBaptiste Daroussin 		       menubox_border2_attr,
2702a3e3873SBaptiste Daroussin 		       menubox_border_attr);
2712a3e3873SBaptiste Daroussin }
2722a3e3873SBaptiste Daroussin 
2732a3e3873SBaptiste Daroussin static bool
2742a3e3873SBaptiste Daroussin check_hotkey(DIALOG_LISTITEM * items, int choice)
2752a3e3873SBaptiste Daroussin {
2762a3e3873SBaptiste Daroussin     bool result = FALSE;
2772a3e3873SBaptiste Daroussin 
2782a3e3873SBaptiste Daroussin     if (dlg_match_char(dlg_last_getc(),
2792a3e3873SBaptiste Daroussin 		       (dialog_vars.no_tags
2802a3e3873SBaptiste Daroussin 			? items[choice].text
2812a3e3873SBaptiste Daroussin 			: items[choice].name))) {
2822a3e3873SBaptiste Daroussin 	result = TRUE;
2832a3e3873SBaptiste Daroussin     }
2842a3e3873SBaptiste Daroussin     return result;
2852a3e3873SBaptiste Daroussin }
2862a3e3873SBaptiste Daroussin 
2874c8945a0SNathan Whitehorn /*
2884c8945a0SNathan Whitehorn  * This is an alternate interface to 'menu' which allows the application
2894c8945a0SNathan Whitehorn  * to read the list item states back directly without putting them in the
2904c8945a0SNathan Whitehorn  * output buffer.
2914c8945a0SNathan Whitehorn  */
2924c8945a0SNathan Whitehorn int
2934c8945a0SNathan Whitehorn dlg_menu(const char *title,
2944c8945a0SNathan Whitehorn 	 const char *cprompt,
2954c8945a0SNathan Whitehorn 	 int height,
2964c8945a0SNathan Whitehorn 	 int width,
2974c8945a0SNathan Whitehorn 	 int menu_height,
2984c8945a0SNathan Whitehorn 	 int item_no,
2994c8945a0SNathan Whitehorn 	 DIALOG_LISTITEM * items,
3004c8945a0SNathan Whitehorn 	 int *current_item,
3014c8945a0SNathan Whitehorn 	 DIALOG_INPUTMENU rename_menutext)
3024c8945a0SNathan Whitehorn {
3034c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
3044c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
305682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
3064c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
3074c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	' ' ),
3084c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_RIGHT ),
3094c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	TAB ),
3104c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_BTAB ),
3114c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_LEFT ),
3124c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,	'+' ),
3134c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,	KEY_DOWN ),
3144c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  CHR_NEXT ),
3154c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,	'-' ),
3164c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,	KEY_UP ),
3174c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  CHR_PREVIOUS ),
3184c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_FIRST,	KEY_HOME ),
3194c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_END ),
3204c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_LL ),
3214c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	KEY_NPAGE ),
3224c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_PAGE_PREV,	KEY_PPAGE ),
3234c8945a0SNathan Whitehorn 	END_KEYS_BINDING
3244c8945a0SNathan Whitehorn     };
3254c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding2[] = {
3264c8945a0SNathan Whitehorn 	INPUTSTR_BINDINGS,
327682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
3284c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
3294c8945a0SNathan Whitehorn 	END_KEYS_BINDING
3304c8945a0SNathan Whitehorn     };
3314c8945a0SNathan Whitehorn     /* *INDENT-ON* */
3324c8945a0SNathan Whitehorn 
3334c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3344c8945a0SNathan Whitehorn     int old_height = height;
3354c8945a0SNathan Whitehorn     int old_width = width;
3364c8945a0SNathan Whitehorn #endif
3372a3e3873SBaptiste Daroussin     ALL_DATA all;
3382a3e3873SBaptiste Daroussin     int i, j, x, y, cur_x, cur_y;
3394c8945a0SNathan Whitehorn     int key = 0, fkey;
3402a3e3873SBaptiste Daroussin     int button = dialog_state.visit_items ? -1 : dlg_default_button();
3414c8945a0SNathan Whitehorn     int choice = dlg_default_listitem(items);
3424c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
3434c8945a0SNathan Whitehorn     int scrollamt = 0;
3442a3e3873SBaptiste Daroussin     int max_choice;
3454c8945a0SNathan Whitehorn     int found;
3462a3e3873SBaptiste Daroussin     int use_width, name_width, text_width, list_width;
3474c8945a0SNathan Whitehorn     WINDOW *dialog, *menu;
3484c8945a0SNathan Whitehorn     char *prompt = dlg_strclone(cprompt);
3494c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_labels();
3502a3e3873SBaptiste Daroussin     bool is_inputmenu = ((rename_menutext != 0)
3512a3e3873SBaptiste Daroussin 			 && (rename_menutext != dlg_dummy_menutext));
3522a3e3873SBaptiste Daroussin 
3532a3e3873SBaptiste Daroussin     all.items = items;
3542a3e3873SBaptiste Daroussin     all.item_no = item_no;
3554c8945a0SNathan Whitehorn 
3564c8945a0SNathan Whitehorn     dlg_does_output();
3574c8945a0SNathan Whitehorn     dlg_tab_correct_str(prompt);
3584c8945a0SNathan Whitehorn 
3594c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3604c8945a0SNathan Whitehorn   retry:
3614c8945a0SNathan Whitehorn #endif
3624c8945a0SNathan Whitehorn 
3632a3e3873SBaptiste Daroussin     all.menu_height = menu_height;
3642a3e3873SBaptiste Daroussin     use_width = dlg_calc_list_width(item_no, items) + 10;
3652a3e3873SBaptiste Daroussin     use_width = MAX(26, use_width);
3662a3e3873SBaptiste Daroussin     if (all.menu_height == 0) {
3674c8945a0SNathan Whitehorn 	/* calculate height without items (4) */
3682a3e3873SBaptiste Daroussin 	dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, use_width);
3692a3e3873SBaptiste Daroussin 	dlg_calc_listh(&height, &all.menu_height, item_no);
3704c8945a0SNathan Whitehorn     } else {
3712a3e3873SBaptiste Daroussin 	dlg_auto_size(title, prompt,
3722a3e3873SBaptiste Daroussin 		      &height, &width,
3732a3e3873SBaptiste Daroussin 		      MIN_HIGH + all.menu_height, use_width);
3744c8945a0SNathan Whitehorn     }
3754c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &width);
3764c8945a0SNathan Whitehorn     dlg_print_size(height, width);
3774c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
3784c8945a0SNathan Whitehorn 
3794c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
3804c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
3814c8945a0SNathan Whitehorn 
3824c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width, y, x);
3832a3e3873SBaptiste Daroussin     all.dialog = dialog;
3842a3e3873SBaptiste Daroussin 
3854c8945a0SNathan Whitehorn     dlg_register_window(dialog, "menubox", binding);
3864c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "menubox", buttons);
3874c8945a0SNathan Whitehorn 
3884c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
3894c8945a0SNathan Whitehorn 
3902a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
3912a3e3873SBaptiste Daroussin     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
3924c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
3934c8945a0SNathan Whitehorn 
3942a3e3873SBaptiste Daroussin     (void) wattrset(dialog, dialog_attr);
3954c8945a0SNathan Whitehorn     dlg_print_autowrap(dialog, prompt, height, width);
3964c8945a0SNathan Whitehorn 
3972a3e3873SBaptiste Daroussin     all.menu_width = width - 6;
3984c8945a0SNathan Whitehorn     getyx(dialog, cur_y, cur_x);
3992a3e3873SBaptiste Daroussin     all.box_y = cur_y + 1;
4002a3e3873SBaptiste Daroussin     all.box_x = (width - all.menu_width) / 2 - 1;
4014c8945a0SNathan Whitehorn 
4024c8945a0SNathan Whitehorn     /*
4034c8945a0SNathan Whitehorn      * After displaying the prompt, we know how much space we really have.
4044c8945a0SNathan Whitehorn      * Limit the list to avoid overwriting the ok-button.
4054c8945a0SNathan Whitehorn      */
4062a3e3873SBaptiste Daroussin     if (all.menu_height + MIN_HIGH > height - cur_y)
4072a3e3873SBaptiste Daroussin 	all.menu_height = height - MIN_HIGH - cur_y;
4082a3e3873SBaptiste Daroussin     if (all.menu_height <= 0)
4092a3e3873SBaptiste Daroussin 	all.menu_height = 1;
4104c8945a0SNathan Whitehorn 
4114c8945a0SNathan Whitehorn     /* Find out maximal number of displayable items at once. */
4122a3e3873SBaptiste Daroussin     max_choice = MIN(all.menu_height,
4134c8945a0SNathan Whitehorn 		     RowHeight(item_no));
4144c8945a0SNathan Whitehorn     if (is_inputmenu)
4154c8945a0SNathan Whitehorn 	max_choice /= INPUT_ROWS;
4164c8945a0SNathan Whitehorn 
4174c8945a0SNathan Whitehorn     /* create new window for the menu */
4182a3e3873SBaptiste Daroussin     menu = dlg_sub_window(dialog, all.menu_height, all.menu_width,
4192a3e3873SBaptiste Daroussin 			  y + all.box_y + 1,
4202a3e3873SBaptiste Daroussin 			  x + all.box_x + 1);
4212a3e3873SBaptiste Daroussin     all.menu = menu;
4222a3e3873SBaptiste Daroussin 
4234c8945a0SNathan Whitehorn     dlg_register_window(menu, "menu", binding2);
4244c8945a0SNathan Whitehorn     dlg_register_buttons(menu, "menu", buttons);
4254c8945a0SNathan Whitehorn 
4264c8945a0SNathan Whitehorn     /* draw a box around the menu items */
4272a3e3873SBaptiste Daroussin     dlg_draw_box(dialog,
4282a3e3873SBaptiste Daroussin 		 all.box_y, all.box_x,
4292a3e3873SBaptiste Daroussin 		 all.menu_height + 2, all.menu_width + 2,
4302a3e3873SBaptiste Daroussin 		 menubox_border_attr, menubox_border2_attr);
4314c8945a0SNathan Whitehorn 
4324c8945a0SNathan Whitehorn     name_width = 0;
4334c8945a0SNathan Whitehorn     text_width = 0;
4344c8945a0SNathan Whitehorn 
4354c8945a0SNathan Whitehorn     /* Find length of longest item to center menu  *
4364c8945a0SNathan Whitehorn      * only if --menu was given, using --inputmenu *
4374c8945a0SNathan Whitehorn      * won't be centered.                         */
4384c8945a0SNathan Whitehorn     for (i = 0; i < item_no; i++) {
4394c8945a0SNathan Whitehorn 	name_width = MAX(name_width, dlg_count_columns(items[i].name));
4404c8945a0SNathan Whitehorn 	text_width = MAX(text_width, dlg_count_columns(items[i].text));
4414c8945a0SNathan Whitehorn     }
4424c8945a0SNathan Whitehorn 
4434c8945a0SNathan Whitehorn     /* If the name+text is wider than the list is allowed, then truncate
4444c8945a0SNathan Whitehorn      * one or both of them.  If the name is no wider than 30% of the list,
4454c8945a0SNathan Whitehorn      * leave it intact.
4464c8945a0SNathan Whitehorn      *
4474c8945a0SNathan Whitehorn      * FIXME: the gutter width and name/list ratio should be configurable.
4484c8945a0SNathan Whitehorn      */
4492a3e3873SBaptiste Daroussin     use_width = (all.menu_width - GUTTER);
4502a3e3873SBaptiste Daroussin     if (dialog_vars.no_tags) {
4512a3e3873SBaptiste Daroussin 	list_width = MIN(use_width, text_width);
4522a3e3873SBaptiste Daroussin     } else if (dialog_vars.no_items) {
4532a3e3873SBaptiste Daroussin 	list_width = MIN(use_width, name_width);
4542a3e3873SBaptiste Daroussin     } else {
4552a3e3873SBaptiste Daroussin 	if (text_width >= 0
4562a3e3873SBaptiste Daroussin 	    && name_width >= 0
4572a3e3873SBaptiste Daroussin 	    && use_width > 0
4582a3e3873SBaptiste Daroussin 	    && text_width + name_width > use_width) {
4594c8945a0SNathan Whitehorn 	    int need = (int) (0.30 * use_width);
4604c8945a0SNathan Whitehorn 	    if (name_width > need) {
4614c8945a0SNathan Whitehorn 		int want = (int) (use_width
4624c8945a0SNathan Whitehorn 				  * ((double) name_width)
4634c8945a0SNathan Whitehorn 				  / (text_width + name_width));
4644c8945a0SNathan Whitehorn 		name_width = (want > need) ? want : need;
4654c8945a0SNathan Whitehorn 	    }
4664c8945a0SNathan Whitehorn 	    text_width = use_width - name_width;
4674c8945a0SNathan Whitehorn 	}
4682a3e3873SBaptiste Daroussin 	list_width = (text_width + name_width);
4692a3e3873SBaptiste Daroussin     }
4704c8945a0SNathan Whitehorn 
4712a3e3873SBaptiste Daroussin     all.tag_x = (is_inputmenu
4724c8945a0SNathan Whitehorn 		 ? 0
4732a3e3873SBaptiste Daroussin 		 : (use_width - list_width) / 2);
4742a3e3873SBaptiste Daroussin     all.item_x = ((dialog_vars.no_tags
4752a3e3873SBaptiste Daroussin 		   ? 0
4762a3e3873SBaptiste Daroussin 		   : (dialog_vars.no_items
4772a3e3873SBaptiste Daroussin 		      ? 0
4782a3e3873SBaptiste Daroussin 		      : (GUTTER + name_width)))
4792a3e3873SBaptiste Daroussin 		  + all.tag_x);
4804c8945a0SNathan Whitehorn 
4814c8945a0SNathan Whitehorn     if (choice - scrollamt >= max_choice) {
4824c8945a0SNathan Whitehorn 	scrollamt = choice - (max_choice - 1);
4834c8945a0SNathan Whitehorn 	choice = max_choice - 1;
4844c8945a0SNathan Whitehorn     }
4854c8945a0SNathan Whitehorn 
4862a3e3873SBaptiste Daroussin     print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
4874c8945a0SNathan Whitehorn 
4884c8945a0SNathan Whitehorn     /* register the new window, along with its borders */
4892a3e3873SBaptiste Daroussin     dlg_mouse_mkbigregion(all.box_y + 1, all.box_x,
4902a3e3873SBaptiste Daroussin 			  all.menu_height + 2, all.menu_width + 2,
4914c8945a0SNathan Whitehorn 			  KEY_MAX, 1, 1, 1 /* by lines */ );
4924c8945a0SNathan Whitehorn 
4934c8945a0SNathan Whitehorn     dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
4944c8945a0SNathan Whitehorn 
4952a3e3873SBaptiste Daroussin     dlg_trace_win(dialog);
4964c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
4974c8945a0SNathan Whitehorn 	if (button < 0)		/* --visit-items */
4982a3e3873SBaptiste Daroussin 	    wmove(dialog,
4992a3e3873SBaptiste Daroussin 		  all.box_y + ItemToRow(choice) + 1,
5002a3e3873SBaptiste Daroussin 		  all.box_x + all.tag_x + 1);
5014c8945a0SNathan Whitehorn 
5024c8945a0SNathan Whitehorn 	key = dlg_mouse_wgetch(dialog, &fkey);
5034c8945a0SNathan Whitehorn 	if (dlg_result_key(key, fkey, &result))
5044c8945a0SNathan Whitehorn 	    break;
5054c8945a0SNathan Whitehorn 
5064c8945a0SNathan Whitehorn 	found = FALSE;
5074c8945a0SNathan Whitehorn 	if (fkey) {
5084c8945a0SNathan Whitehorn 	    /*
5094c8945a0SNathan Whitehorn 	     * Allow a mouse-click on a box to switch selection to that box.
5104c8945a0SNathan Whitehorn 	     * Handling a button click is a little more complicated, since we
5114c8945a0SNathan Whitehorn 	     * push a KEY_ENTER back onto the input stream so we'll put the
5124c8945a0SNathan Whitehorn 	     * cursor at the right place before handling the "keypress".
5134c8945a0SNathan Whitehorn 	     */
5144c8945a0SNathan Whitehorn 	    if (key >= DLGK_MOUSE(KEY_MAX)) {
5154c8945a0SNathan Whitehorn 		key -= DLGK_MOUSE(KEY_MAX);
5164c8945a0SNathan Whitehorn 		i = RowToItem(key);
5174c8945a0SNathan Whitehorn 		if (i < max_choice) {
5184c8945a0SNathan Whitehorn 		    found = TRUE;
5194c8945a0SNathan Whitehorn 		} else {
5204c8945a0SNathan Whitehorn 		    beep();
5214c8945a0SNathan Whitehorn 		    continue;
5224c8945a0SNathan Whitehorn 		}
5234c8945a0SNathan Whitehorn 	    } else if (is_DLGK_MOUSE(key)
5244c8945a0SNathan Whitehorn 		       && dlg_ok_buttoncode(key - M_EVENT) >= 0) {
5254c8945a0SNathan Whitehorn 		button = (key - M_EVENT);
5264c8945a0SNathan Whitehorn 		ungetch('\n');
5274c8945a0SNathan Whitehorn 		continue;
5284c8945a0SNathan Whitehorn 	    }
5294c8945a0SNathan Whitehorn 	} else {
5304c8945a0SNathan Whitehorn 	    /*
5314c8945a0SNathan Whitehorn 	     * Check if key pressed matches first character of any item tag in
5324c8945a0SNathan Whitehorn 	     * list.  If there is more than one match, we will cycle through
5334c8945a0SNathan Whitehorn 	     * each one as the same key is pressed repeatedly.
5344c8945a0SNathan Whitehorn 	     */
5354c8945a0SNathan Whitehorn 	    if (button < 0 || !dialog_state.visit_items) {
5364c8945a0SNathan Whitehorn 		for (j = scrollamt + choice + 1; j < item_no; j++) {
5372a3e3873SBaptiste Daroussin 		    if (check_hotkey(items, j)) {
5384c8945a0SNathan Whitehorn 			found = TRUE;
5394c8945a0SNathan Whitehorn 			i = j - scrollamt;
5404c8945a0SNathan Whitehorn 			break;
5414c8945a0SNathan Whitehorn 		    }
5424c8945a0SNathan Whitehorn 		}
5434c8945a0SNathan Whitehorn 		if (!found) {
5444c8945a0SNathan Whitehorn 		    for (j = 0; j <= scrollamt + choice; j++) {
5452a3e3873SBaptiste Daroussin 			if (check_hotkey(items, j)) {
5464c8945a0SNathan Whitehorn 			    found = TRUE;
5474c8945a0SNathan Whitehorn 			    i = j - scrollamt;
5484c8945a0SNathan Whitehorn 			    break;
5494c8945a0SNathan Whitehorn 			}
5504c8945a0SNathan Whitehorn 		    }
5514c8945a0SNathan Whitehorn 		}
5524c8945a0SNathan Whitehorn 		if (found)
5534c8945a0SNathan Whitehorn 		    dlg_flush_getc();
5544c8945a0SNathan Whitehorn 	    } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
5554c8945a0SNathan Whitehorn 		button = j;
5564c8945a0SNathan Whitehorn 		ungetch('\n');
5574c8945a0SNathan Whitehorn 		continue;
5584c8945a0SNathan Whitehorn 	    }
5594c8945a0SNathan Whitehorn 
5604c8945a0SNathan Whitehorn 	    /*
5614c8945a0SNathan Whitehorn 	     * A single digit (1-9) positions the selection to that line in the
5624c8945a0SNathan Whitehorn 	     * current screen.
5634c8945a0SNathan Whitehorn 	     */
5644c8945a0SNathan Whitehorn 	    if (!found
5654c8945a0SNathan Whitehorn 		&& (key <= '9')
5664c8945a0SNathan Whitehorn 		&& (key > '0')
5674c8945a0SNathan Whitehorn 		&& (key - '1' < max_choice)) {
5684c8945a0SNathan Whitehorn 		found = TRUE;
5694c8945a0SNathan Whitehorn 		i = key - '1';
5704c8945a0SNathan Whitehorn 	    }
5714c8945a0SNathan Whitehorn 	}
5724c8945a0SNathan Whitehorn 
5734c8945a0SNathan Whitehorn 	if (!found && fkey) {
5744c8945a0SNathan Whitehorn 	    found = TRUE;
5754c8945a0SNathan Whitehorn 	    switch (key) {
5764c8945a0SNathan Whitehorn 	    case DLGK_PAGE_FIRST:
5774c8945a0SNathan Whitehorn 		i = -scrollamt;
5784c8945a0SNathan Whitehorn 		break;
5794c8945a0SNathan Whitehorn 	    case DLGK_PAGE_LAST:
5804c8945a0SNathan Whitehorn 		i = item_no - 1 - scrollamt;
5814c8945a0SNathan Whitehorn 		break;
5824c8945a0SNathan Whitehorn 	    case DLGK_MOUSE(KEY_PPAGE):
5834c8945a0SNathan Whitehorn 	    case DLGK_PAGE_PREV:
5844c8945a0SNathan Whitehorn 		if (choice)
5854c8945a0SNathan Whitehorn 		    i = 0;
5864c8945a0SNathan Whitehorn 		else if (scrollamt != 0)
5874c8945a0SNathan Whitehorn 		    i = -MIN(scrollamt, max_choice);
5884c8945a0SNathan Whitehorn 		else
5894c8945a0SNathan Whitehorn 		    continue;
5904c8945a0SNathan Whitehorn 		break;
5914c8945a0SNathan Whitehorn 	    case DLGK_MOUSE(KEY_NPAGE):
5924c8945a0SNathan Whitehorn 	    case DLGK_PAGE_NEXT:
5934c8945a0SNathan Whitehorn 		i = MIN(choice + max_choice, item_no - scrollamt - 1);
5944c8945a0SNathan Whitehorn 		break;
5954c8945a0SNathan Whitehorn 	    case DLGK_ITEM_PREV:
5964c8945a0SNathan Whitehorn 		i = choice - 1;
5974c8945a0SNathan Whitehorn 		if (choice == 0 && scrollamt == 0)
5984c8945a0SNathan Whitehorn 		    continue;
5994c8945a0SNathan Whitehorn 		break;
6004c8945a0SNathan Whitehorn 	    case DLGK_ITEM_NEXT:
6014c8945a0SNathan Whitehorn 		i = choice + 1;
6024c8945a0SNathan Whitehorn 		if (scrollamt + choice >= item_no - 1)
6034c8945a0SNathan Whitehorn 		    continue;
6044c8945a0SNathan Whitehorn 		break;
6054c8945a0SNathan Whitehorn 	    default:
6064c8945a0SNathan Whitehorn 		found = FALSE;
6074c8945a0SNathan Whitehorn 		break;
6084c8945a0SNathan Whitehorn 	    }
6094c8945a0SNathan Whitehorn 	}
6104c8945a0SNathan Whitehorn 
6114c8945a0SNathan Whitehorn 	if (found) {
6124c8945a0SNathan Whitehorn 	    if (i != choice) {
6134c8945a0SNathan Whitehorn 		getyx(dialog, cur_y, cur_x);
6144c8945a0SNathan Whitehorn 		if (i < 0 || i >= max_choice) {
6154c8945a0SNathan Whitehorn 		    if (i < 0) {
6164c8945a0SNathan Whitehorn 			scrollamt += i;
6174c8945a0SNathan Whitehorn 			choice = 0;
6184c8945a0SNathan Whitehorn 		    } else {
6194c8945a0SNathan Whitehorn 			choice = max_choice - 1;
6204c8945a0SNathan Whitehorn 			scrollamt += (i - max_choice + 1);
6214c8945a0SNathan Whitehorn 		    }
6222a3e3873SBaptiste Daroussin 		    print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
6234c8945a0SNathan Whitehorn 		} else {
6244c8945a0SNathan Whitehorn 		    choice = i;
6252a3e3873SBaptiste Daroussin 		    print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
6264c8945a0SNathan Whitehorn 		    (void) wmove(dialog, cur_y, cur_x);
6274c8945a0SNathan Whitehorn 		    wrefresh(dialog);
6284c8945a0SNathan Whitehorn 		}
6294c8945a0SNathan Whitehorn 	    }
6304c8945a0SNathan Whitehorn 	    continue;		/* wait for another key press */
6314c8945a0SNathan Whitehorn 	}
6324c8945a0SNathan Whitehorn 
6334c8945a0SNathan Whitehorn 	if (fkey) {
6344c8945a0SNathan Whitehorn 	    switch (key) {
6354c8945a0SNathan Whitehorn 	    case DLGK_FIELD_PREV:
6364c8945a0SNathan Whitehorn 		button = dlg_prev_button(buttons, button);
6374c8945a0SNathan Whitehorn 		dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
6384c8945a0SNathan Whitehorn 				 FALSE, width);
6394c8945a0SNathan Whitehorn 		break;
6404c8945a0SNathan Whitehorn 	    case DLGK_FIELD_NEXT:
6414c8945a0SNathan Whitehorn 		button = dlg_next_button(buttons, button);
6424c8945a0SNathan Whitehorn 		dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
6434c8945a0SNathan Whitehorn 				 FALSE, width);
6444c8945a0SNathan Whitehorn 		break;
6454c8945a0SNathan Whitehorn 	    case DLGK_ENTER:
6462a3e3873SBaptiste Daroussin 		if (is_inputmenu)
6472a3e3873SBaptiste Daroussin 		    result = dlg_ok_buttoncode(button);
6482a3e3873SBaptiste Daroussin 		else
649682c9e0fSNathan Whitehorn 		    result = dlg_enter_buttoncode(button);
6504c8945a0SNathan Whitehorn 
6514c8945a0SNathan Whitehorn 		/*
6524c8945a0SNathan Whitehorn 		 * If dlg_menu() is called from dialog_menu(), we want to
6532a3e3873SBaptiste Daroussin 		 * capture the results into dialog_vars.input_result.
6544c8945a0SNathan Whitehorn 		 */
6554c8945a0SNathan Whitehorn 		if (result == DLG_EXIT_ERROR) {
6564c8945a0SNathan Whitehorn 		    result = DLG_EXIT_UNKNOWN;
6574c8945a0SNathan Whitehorn 		} else if (is_inputmenu
6584c8945a0SNathan Whitehorn 			   || rename_menutext == dlg_dummy_menutext) {
6594c8945a0SNathan Whitehorn 		    result = handle_button(result,
6604c8945a0SNathan Whitehorn 					   items,
6614c8945a0SNathan Whitehorn 					   scrollamt + choice);
6624c8945a0SNathan Whitehorn 		}
6634c8945a0SNathan Whitehorn 
6644c8945a0SNathan Whitehorn 		/*
6654c8945a0SNathan Whitehorn 		 * If we have a rename_menutext function, interpret the Extra
6664c8945a0SNathan Whitehorn 		 * button as a request to rename the menu's text.  If that
6674c8945a0SNathan Whitehorn 		 * function doesn't return "Unknown", we will exit from this
6684c8945a0SNathan Whitehorn 		 * function.  Usually that is done for dialog_menu(), so the
6694c8945a0SNathan Whitehorn 		 * shell script can use the updated value.  If it does return
6704c8945a0SNathan Whitehorn 		 * "Unknown", update the list item only.  A direct caller of
6714c8945a0SNathan Whitehorn 		 * dlg_menu() can free the renamed value - we cannot.
6724c8945a0SNathan Whitehorn 		 */
6734c8945a0SNathan Whitehorn 		if (is_inputmenu && result == DLG_EXIT_EXTRA) {
6744c8945a0SNathan Whitehorn 		    char *tmp;
6754c8945a0SNathan Whitehorn 
6762a3e3873SBaptiste Daroussin 		    if (input_menu_edit(&all,
6774c8945a0SNathan Whitehorn 					&items[scrollamt + choice],
6784c8945a0SNathan Whitehorn 					choice,
6794c8945a0SNathan Whitehorn 					&tmp)) {
6804c8945a0SNathan Whitehorn 			result = rename_menutext(items, scrollamt + choice, tmp);
6814c8945a0SNathan Whitehorn 			if (result == DLG_EXIT_UNKNOWN) {
6824c8945a0SNathan Whitehorn 			    items[scrollamt + choice].text = tmp;
6834c8945a0SNathan Whitehorn 			} else {
6844c8945a0SNathan Whitehorn 			    free(tmp);
6854c8945a0SNathan Whitehorn 			}
6864c8945a0SNathan Whitehorn 		    } else {
6874c8945a0SNathan Whitehorn 			result = DLG_EXIT_UNKNOWN;
6882a3e3873SBaptiste Daroussin 			print_item(&all,
6892a3e3873SBaptiste Daroussin 				   menu,
6904c8945a0SNathan Whitehorn 				   &items[scrollamt + choice],
6914c8945a0SNathan Whitehorn 				   choice,
6924c8945a0SNathan Whitehorn 				   Selected,
6934c8945a0SNathan Whitehorn 				   is_inputmenu);
6944c8945a0SNathan Whitehorn 			(void) wnoutrefresh(menu);
6954c8945a0SNathan Whitehorn 			free(tmp);
6964c8945a0SNathan Whitehorn 		    }
6974c8945a0SNathan Whitehorn 
6984c8945a0SNathan Whitehorn 		    if (result == DLG_EXIT_UNKNOWN) {
6994c8945a0SNathan Whitehorn 			dlg_draw_buttons(dialog, height - 2, 0,
7004c8945a0SNathan Whitehorn 					 buttons, button, FALSE, width);
7014c8945a0SNathan Whitehorn 		    }
7024c8945a0SNathan Whitehorn 		}
7034c8945a0SNathan Whitehorn 		break;
7044c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
7054c8945a0SNathan Whitehorn 	    case KEY_RESIZE:
7064c8945a0SNathan Whitehorn 		/* reset data */
7074c8945a0SNathan Whitehorn 		height = old_height;
7084c8945a0SNathan Whitehorn 		width = old_width;
7094c8945a0SNathan Whitehorn 		/* repaint */
7104c8945a0SNathan Whitehorn 		dlg_clear();
7114c8945a0SNathan Whitehorn 		dlg_del_window(dialog);
7124c8945a0SNathan Whitehorn 		refresh();
7134c8945a0SNathan Whitehorn 		dlg_mouse_free_regions();
7144c8945a0SNathan Whitehorn 		goto retry;
7154c8945a0SNathan Whitehorn #endif
7164c8945a0SNathan Whitehorn 	    default:
7174c8945a0SNathan Whitehorn 		flash();
7184c8945a0SNathan Whitehorn 		break;
7194c8945a0SNathan Whitehorn 	    }
7204c8945a0SNathan Whitehorn 	}
7214c8945a0SNathan Whitehorn     }
7224c8945a0SNathan Whitehorn 
7234c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
7244c8945a0SNathan Whitehorn     dlg_unregister_window(menu);
7254c8945a0SNathan Whitehorn     dlg_del_window(dialog);
7264c8945a0SNathan Whitehorn     free(prompt);
7274c8945a0SNathan Whitehorn 
7284c8945a0SNathan Whitehorn     *current_item = scrollamt + choice;
7294c8945a0SNathan Whitehorn     return result;
7304c8945a0SNathan Whitehorn }
7314c8945a0SNathan Whitehorn 
7324c8945a0SNathan Whitehorn /*
7334c8945a0SNathan Whitehorn  * Display a menu for choosing among a number of options
7344c8945a0SNathan Whitehorn  */
7354c8945a0SNathan Whitehorn int
7364c8945a0SNathan Whitehorn dialog_menu(const char *title,
7374c8945a0SNathan Whitehorn 	    const char *cprompt,
7384c8945a0SNathan Whitehorn 	    int height,
7394c8945a0SNathan Whitehorn 	    int width,
7404c8945a0SNathan Whitehorn 	    int menu_height,
7414c8945a0SNathan Whitehorn 	    int item_no,
7424c8945a0SNathan Whitehorn 	    char **items)
7434c8945a0SNathan Whitehorn {
7444c8945a0SNathan Whitehorn     int result;
7454c8945a0SNathan Whitehorn     int choice;
7462a3e3873SBaptiste Daroussin     int i, j;
7474c8945a0SNathan Whitehorn     DIALOG_LISTITEM *listitems;
7484c8945a0SNathan Whitehorn 
7494c8945a0SNathan Whitehorn     listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
7504c8945a0SNathan Whitehorn     assert_ptr(listitems, "dialog_menu");
7514c8945a0SNathan Whitehorn 
7522a3e3873SBaptiste Daroussin     for (i = j = 0; i < item_no; ++i) {
7532a3e3873SBaptiste Daroussin 	listitems[i].name = items[j++];
7542a3e3873SBaptiste Daroussin 	listitems[i].text = (dialog_vars.no_items
7552a3e3873SBaptiste Daroussin 			     ? dlg_strempty()
7562a3e3873SBaptiste Daroussin 			     : items[j++]);
7574c8945a0SNathan Whitehorn 	listitems[i].help = ((dialog_vars.item_help)
7582a3e3873SBaptiste Daroussin 			     ? items[j++]
7594c8945a0SNathan Whitehorn 			     : dlg_strempty());
7604c8945a0SNathan Whitehorn     }
7614c8945a0SNathan Whitehorn     dlg_align_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
7624c8945a0SNathan Whitehorn 
7634c8945a0SNathan Whitehorn     result = dlg_menu(title,
7644c8945a0SNathan Whitehorn 		      cprompt,
7654c8945a0SNathan Whitehorn 		      height,
7664c8945a0SNathan Whitehorn 		      width,
7674c8945a0SNathan Whitehorn 		      menu_height,
7684c8945a0SNathan Whitehorn 		      item_no,
7694c8945a0SNathan Whitehorn 		      listitems,
7704c8945a0SNathan Whitehorn 		      &choice,
7712a3e3873SBaptiste Daroussin 		      (dialog_vars.input_menu
7722a3e3873SBaptiste Daroussin 		       ? dlg_renamed_menutext
7732a3e3873SBaptiste Daroussin 		       : dlg_dummy_menutext));
7744c8945a0SNathan Whitehorn 
7754c8945a0SNathan Whitehorn     dlg_free_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
7764c8945a0SNathan Whitehorn     free(listitems);
7774c8945a0SNathan Whitehorn     return result;
7784c8945a0SNathan Whitehorn }
779