xref: /freebsd/contrib/dialog/menubox.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
1 /*
2  *  $Id: menubox.c,v 1.171 2020/11/23 21:03:11 tom Exp $
3  *
4  *  menubox.c -- implements the menu box
5  *
6  *  Copyright 2000-2019,2020	Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public Licens, version 2.1e
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *	Free Software Foundation, Inc.
20  *	51 Franklin St., Fifth Floor
21  *	Boston, MA 02110, USA.
22  *
23  *  An earlier version of this program lists as authors
24  *	Savio Lam (lam836@cs.cuhk.hk)
25  */
26 
27 #include <dlg_internals.h>
28 #include <dlg_keys.h>
29 
30 typedef enum {
31     Unselected = 0,
32     Selected,
33     Editing
34 } Mode;
35 
36 typedef struct {
37     /* the outer-window */
38     WINDOW *dialog;
39     int box_y;
40     int box_x;
41     int tag_x;
42     int item_x;
43     int menu_height;
44     int menu_width;
45     /* the inner-window */
46     WINDOW *menu;
47     DIALOG_LISTITEM *items;
48     int item_no;
49 } ALL_DATA;
50 
51 #define MIN_HIGH  (1 + (5 * MARGIN))
52 
53 #define INPUT_ROWS     3	/* rows per inputmenu entry */
54 
55 #define RowHeight(i) (is_inputmenu ? ((i) * INPUT_ROWS) : ((i) * 1))
56 #define ItemToRow(i) (is_inputmenu ? ((i) * INPUT_ROWS + 1) : (i))
57 #define RowToItem(i) (is_inputmenu ? ((i) / INPUT_ROWS + 0) : (i))
58 
59 /*
60  * Print menu item
61  */
62 static void
print_item(ALL_DATA * data,WINDOW * win,DIALOG_LISTITEM * item,int choice,Mode selected,bool is_inputmenu)63 print_item(ALL_DATA * data,
64 	   WINDOW *win,
65 	   DIALOG_LISTITEM * item,
66 	   int choice,
67 	   Mode selected,
68 	   bool is_inputmenu)
69 {
70     chtype save = dlg_get_attrs(win);
71     int climit = (data->item_x - data->tag_x - GUTTER);
72     int my_width = data->menu_width;
73     int my_x = data->item_x;
74     int my_y = ItemToRow(choice);
75     bool both = (!dialog_vars.no_tags && !dialog_vars.no_items);
76     bool first = TRUE;
77     chtype bordchar;
78     const char *show = (dialog_vars.no_items
79 			? item->name
80 			: item->text);
81 
82     switch (selected) {
83     default:
84     case Unselected:
85 	bordchar = item_attr;
86 	break;
87     case Selected:
88 	bordchar = item_selected_attr;
89 	break;
90     case Editing:
91 	bordchar = dialog_attr;
92 	break;
93     }
94 
95     /* Clear 'residue' of last item and mark current current item */
96     if (is_inputmenu) {
97 	int n;
98 
99 	dlg_attrset(win, (selected != Unselected) ? item_selected_attr : item_attr);
100 	for (n = my_y - 1; n < my_y + INPUT_ROWS - 1; n++) {
101 	    wmove(win, n, 0);
102 	    wprintw(win, "%*s", my_width, " ");
103 	}
104     } else {
105 	dlg_attrset(win, menubox_attr);
106 	wmove(win, my_y, 0);
107 	wprintw(win, "%*s", my_width, " ");
108     }
109 
110     /* highlight first char of the tag to be special */
111     if (both) {
112 	(void) wmove(win, my_y, data->tag_x);
113 	dlg_print_listitem(win, item->name, climit, first, selected);
114 	first = FALSE;
115     }
116 
117     /* Draw the input field box (only for inputmenu) */
118     (void) wmove(win, my_y, my_x);
119     if (is_inputmenu) {
120 	my_width -= 1;
121 	dlg_draw_box(win, my_y - 1, my_x, INPUT_ROWS, my_width - my_x - data->tag_x,
122 		     bordchar,
123 		     bordchar);
124 	my_width -= 1;
125 	++my_x;
126     }
127 
128     /* print actual item */
129     wmove(win, my_y, my_x);
130     dlg_print_listitem(win, show, my_width - my_x, first, selected);
131 
132     if (selected) {
133 	dlg_item_help(item->help);
134     }
135     dlg_attrset(win, save);
136 }
137 
138 /*
139  * Allow the user to edit the text of a menu entry.
140  */
141 static int
input_menu_edit(ALL_DATA * data,DIALOG_LISTITEM * items,int choice,char ** resultp)142 input_menu_edit(ALL_DATA * data,
143 		DIALOG_LISTITEM * items,
144 		int choice,
145 		char **resultp)
146 {
147     chtype save = dlg_get_attrs(data->menu);
148     char *result;
149     int offset = 0;
150     int key = 0, fkey = 0;
151     bool first = TRUE;
152     /* see above */
153     bool is_inputmenu = TRUE;
154     int y = ItemToRow(choice);
155     int code = TRUE;
156     int max_len = dlg_max_input(MAX((int) strlen(items->text) + 1, MAX_LEN));
157 
158     result = dlg_malloc(char, (size_t) max_len);
159     assert_ptr(result, "input_menu_edit");
160 
161     /* original item is used to initialize the input string. */
162     result[0] = '\0';
163     strcpy(result, items->text);
164 
165     print_item(data, data->menu, items, choice, Editing, TRUE);
166 
167     /* taken out of inputbox.c - but somewhat modified */
168     for (;;) {
169 	if (!first) {
170 	    int check = DLG_EXIT_UNKNOWN;
171 	    key = dlg_mouse_wgetch(data->menu, &fkey);
172 	    if (dlg_result_key(key, fkey, &check)) {
173 		if (check == DLG_EXIT_CANCEL) {
174 		    code = FALSE;
175 		    break;
176 		} else {
177 		    flash();
178 		}
179 	    }
180 	}
181 	if (dlg_edit_string(result, &offset, key, fkey, first)) {
182 	    dlg_show_string(data->menu, result, offset, inputbox_attr,
183 			    y,
184 			    data->item_x + 1,
185 			    data->menu_width - data->item_x - 3,
186 			    FALSE, first);
187 	    first = FALSE;
188 	} else if (key == ESC || key == TAB) {
189 	    code = FALSE;
190 	    break;
191 	} else {
192 	    break;
193 	}
194     }
195     print_item(data, data->menu, items, choice, Selected, TRUE);
196     dlg_attrset(data->menu, save);
197 
198     *resultp = result;
199     return code;
200 }
201 
202 static int
handle_button(int code,DIALOG_LISTITEM * items,int choice)203 handle_button(int code, DIALOG_LISTITEM * items, int choice)
204 {
205     char *help_result;
206 
207     switch (code) {
208     case DLG_EXIT_OK:		/* FALLTHRU */
209     case DLG_EXIT_EXTRA:
210 	dlg_add_string(items[choice].name);
211 	break;
212     case DLG_EXIT_HELP:
213 	dlg_add_help_listitem(&code, &help_result, &items[choice]);
214 	dlg_add_string(help_result);
215 	break;
216     }
217     AddLastKey();
218     return code;
219 }
220 
221 int
dlg_renamed_menutext(DIALOG_LISTITEM * items,int current,char * newtext)222 dlg_renamed_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
223 {
224     if (dialog_vars.input_result)
225 	dialog_vars.input_result[0] = '\0';
226     dlg_add_result("RENAMED ");
227     dlg_add_string(items[current].name);
228     dlg_add_result(" ");
229     dlg_add_string(newtext);
230     AddLastKey();
231     return DLG_EXIT_EXTRA;
232 }
233 
234 int
dlg_dummy_menutext(DIALOG_LISTITEM * items,int current,char * newtext)235 dlg_dummy_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
236 {
237     (void) items;
238     (void) current;
239     (void) newtext;
240     return DLG_EXIT_ERROR;
241 }
242 
243 static void
print_menu(ALL_DATA * data,int choice,int scrollamt,int max_choice,bool is_inputmenu)244 print_menu(ALL_DATA * data, int choice, int scrollamt, int max_choice, bool is_inputmenu)
245 {
246     int i;
247 
248     for (i = 0; i < max_choice; i++) {
249 	print_item(data,
250 		   data->menu,
251 		   &data->items[i + scrollamt],
252 		   i,
253 		   (i == choice) ? Selected : Unselected,
254 		   is_inputmenu);
255     }
256 
257     /* Clean bottom lines */
258     if (is_inputmenu) {
259 	int spare_lines, x_count;
260 	spare_lines = data->menu_height % INPUT_ROWS;
261 	dlg_attrset(data->menu, menubox_attr);
262 	for (; spare_lines; spare_lines--) {
263 	    wmove(data->menu, data->menu_height - spare_lines, 0);
264 	    for (x_count = 0; x_count < data->menu_width;
265 		 x_count++) {
266 		waddch(data->menu, ' ');
267 	    }
268 	}
269     }
270 
271     (void) wnoutrefresh(data->menu);
272 
273     dlg_draw_scrollbar(data->dialog,
274 		       scrollamt,
275 		       scrollamt,
276 		       scrollamt + max_choice,
277 		       data->item_no,
278 		       data->box_x,
279 		       data->box_x + data->menu_width,
280 		       data->box_y,
281 		       data->box_y + data->menu_height + 1,
282 		       menubox_border2_attr,
283 		       menubox_border_attr);
284 }
285 
286 static bool
check_hotkey(DIALOG_LISTITEM * items,int choice)287 check_hotkey(DIALOG_LISTITEM * items, int choice)
288 {
289     bool result = FALSE;
290 
291     if (dlg_match_char(dlg_last_getc(),
292 		       (dialog_vars.no_tags
293 			? items[choice].text
294 			: items[choice].name))) {
295 	result = TRUE;
296     }
297     return result;
298 }
299 
300 /*
301  * This is an alternate interface to 'menu' which allows the application
302  * to read the list item states back directly without putting them in the
303  * output buffer.
304  */
305 int
dlg_menu(const char * title,const char * cprompt,int height,int width,int menu_height,int item_no,DIALOG_LISTITEM * items,int * current_item,DIALOG_INPUTMENU rename_menutext)306 dlg_menu(const char *title,
307 	 const char *cprompt,
308 	 int height,
309 	 int width,
310 	 int menu_height,
311 	 int item_no,
312 	 DIALOG_LISTITEM * items,
313 	 int *current_item,
314 	 DIALOG_INPUTMENU rename_menutext)
315 {
316     /* *INDENT-OFF* */
317     static DLG_KEYS_BINDING binding[] = {
318 	HELPKEY_BINDINGS,
319 	ENTERKEY_BINDINGS,
320 	TOGGLEKEY_BINDINGS,
321 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_RIGHT ),
322 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	TAB ),
323 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_BTAB ),
324 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_LEFT ),
325 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,	'+' ),
326 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,	KEY_DOWN ),
327 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  CHR_NEXT ),
328 	DLG_KEYS_DATA( DLGK_ITEM_PREV,	'-' ),
329 	DLG_KEYS_DATA( DLGK_ITEM_PREV,	KEY_UP ),
330 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  CHR_PREVIOUS ),
331 	DLG_KEYS_DATA( DLGK_PAGE_FIRST,	KEY_HOME ),
332 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_END ),
333 	DLG_KEYS_DATA( DLGK_PAGE_LAST,	KEY_LL ),
334 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	KEY_NPAGE ),
335 	DLG_KEYS_DATA( DLGK_PAGE_PREV,	KEY_PPAGE ),
336 	END_KEYS_BINDING
337     };
338     static DLG_KEYS_BINDING binding2[] = {
339 	INPUTSTR_BINDINGS,
340 	HELPKEY_BINDINGS,
341 	ENTERKEY_BINDINGS,
342 	END_KEYS_BINDING
343     };
344     /* *INDENT-ON* */
345 
346 #ifdef KEY_RESIZE
347     int old_LINES = LINES;
348     int old_COLS = COLS;
349     int old_height = height;
350     int old_width = width;
351 #endif
352     ALL_DATA all;
353     int i, j, x, y, cur_x, cur_y;
354     int fkey;
355     int button = dialog_state.visit_items ? -1 : dlg_default_button();
356     int choice = dlg_default_listitem(items);
357     int result = DLG_EXIT_UNKNOWN;
358     int scrollamt = 0;
359     int max_choice;
360     int use_width, name_width, text_width, list_width;
361     WINDOW *dialog, *menu;
362     char *prompt = 0;
363     const char **buttons = dlg_ok_labels();
364     bool is_inputmenu = ((rename_menutext != 0)
365 			 && (rename_menutext != dlg_dummy_menutext));
366 
367     DLG_TRACE(("# menubox args:\n"));
368     DLG_TRACE2S("title", title);
369     DLG_TRACE2S("message", cprompt);
370     DLG_TRACE2N("height", height);
371     DLG_TRACE2N("width", width);
372     DLG_TRACE2N("lheight", menu_height);
373     DLG_TRACE2N("llength", item_no);
374     /* FIXME dump the items[][] too */
375     DLG_TRACE2N("rename", rename_menutext != 0);
376 
377     dialog_state.plain_buttons = TRUE;
378 
379     all.items = items;
380     all.item_no = item_no;
381 
382     dlg_does_output();
383 
384 #ifdef KEY_RESIZE
385   retry:
386 #endif
387 
388     prompt = dlg_strclone(cprompt);
389     dlg_tab_correct_str(prompt);
390 
391     all.menu_height = menu_height;
392     use_width = dlg_calc_list_width(item_no, items) + 10;
393     use_width = MAX(26, use_width);
394     if (all.menu_height == 0) {
395 	/* calculate height without items (4) */
396 	dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, use_width);
397 	dlg_calc_listh(&height, &all.menu_height, item_no);
398     } else {
399 	dlg_auto_size(title, prompt,
400 		      &height, &width,
401 		      MIN_HIGH + all.menu_height, use_width);
402     }
403     dlg_button_layout(buttons, &width);
404     dlg_print_size(height, width);
405     dlg_ctl_size(height, width);
406 
407     x = dlg_box_x_ordinate(width);
408     y = dlg_box_y_ordinate(height);
409 
410     dialog = dlg_new_window(height, width, y, x);
411     all.dialog = dialog;
412 
413     dlg_register_window(dialog, "menubox", binding);
414     dlg_register_buttons(dialog, "menubox", buttons);
415 
416     dlg_mouse_setbase(x, y);
417 
418     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
419     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
420     dlg_draw_title(dialog, title);
421 
422     dlg_attrset(dialog, dialog_attr);
423     dlg_print_autowrap(dialog, prompt, height, width);
424 
425     all.menu_width = width - 6;
426     getyx(dialog, cur_y, cur_x);
427     all.box_y = cur_y + 1;
428     all.box_x = (width - all.menu_width) / 2 - 1;
429 
430     /*
431      * After displaying the prompt, we know how much space we really have.
432      * Limit the list to avoid overwriting the ok-button.
433      */
434     all.menu_height = height - MIN_HIGH - cur_y;
435     if (all.menu_height <= 0)
436 	all.menu_height = 1;
437 
438     /* Find out maximal number of displayable items at once. */
439     max_choice = MIN(all.menu_height,
440 		     RowHeight(item_no));
441     if (is_inputmenu)
442 	max_choice /= INPUT_ROWS;
443 
444     /* create new window for the menu */
445     menu = dlg_sub_window(dialog, all.menu_height, all.menu_width,
446 			  y + all.box_y + 1,
447 			  x + all.box_x + 1);
448     all.menu = menu;
449 
450     dlg_register_window(menu, "menu", binding2);
451     dlg_register_buttons(menu, "menu", buttons);
452 
453     /* draw a box around the menu items */
454     dlg_draw_box(dialog,
455 		 all.box_y, all.box_x,
456 		 all.menu_height + 2, all.menu_width + 2,
457 		 menubox_border_attr, menubox_border2_attr);
458 
459     name_width = 0;
460     text_width = 0;
461 
462     /* Find length of longest item to center menu  *
463      * only if --menu was given, using --inputmenu *
464      * won't be centered.                         */
465     for (i = 0; i < item_no; i++) {
466 	name_width = MAX(name_width, dlg_count_columns(items[i].name));
467 	text_width = MAX(text_width, dlg_count_columns(items[i].text));
468     }
469 
470     /* If the name+text is wider than the list is allowed, then truncate
471      * one or both of them.  If the name is no wider than 30% of the list,
472      * leave it intact.
473      *
474      * FIXME: the gutter width and name/list ratio should be configurable.
475      */
476     use_width = (all.menu_width - GUTTER);
477     if (dialog_vars.no_tags) {
478 	list_width = MIN(use_width, text_width);
479     } else if (dialog_vars.no_items) {
480 	list_width = MIN(use_width, name_width);
481     } else {
482 	if (text_width >= 0
483 	    && name_width >= 0
484 	    && use_width > 0
485 	    && text_width + name_width > use_width) {
486 	    int need = (int) (0.30 * use_width);
487 	    if (name_width > need) {
488 		int want = (int) (use_width
489 				  * ((double) name_width)
490 				  / (text_width + name_width));
491 		name_width = (want > need) ? want : need;
492 	    }
493 	    text_width = use_width - name_width;
494 	}
495 	list_width = (text_width + name_width);
496     }
497 
498     all.tag_x = (is_inputmenu
499 		 ? 0
500 		 : (use_width - list_width) / 2);
501     all.item_x = ((dialog_vars.no_tags
502 		   ? 0
503 		   : (dialog_vars.no_items
504 		      ? 0
505 		      : (GUTTER + name_width)))
506 		  + all.tag_x);
507 
508     if (choice - scrollamt >= max_choice) {
509 	scrollamt = choice - (max_choice - 1);
510 	choice = max_choice - 1;
511     }
512 
513     print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
514 
515     /* register the new window, along with its borders */
516     dlg_mouse_mkbigregion(all.box_y + 1, all.box_x,
517 			  all.menu_height + 2, all.menu_width + 2,
518 			  KEY_MAX, 1, 1, 1 /* by lines */ );
519 
520     dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
521 
522     dlg_trace_win(dialog);
523 
524     while (result == DLG_EXIT_UNKNOWN) {
525 	int key, found;
526 
527 	if (button < 0)		/* --visit-items */
528 	    wmove(dialog,
529 		  all.box_y + ItemToRow(choice) + 1,
530 		  all.box_x + all.tag_x + 1);
531 
532 	key = dlg_mouse_wgetch(dialog, &fkey);
533 	if (dlg_result_key(key, fkey, &result)) {
534 	    if (!dlg_button_key(result, &button, &key, &fkey))
535 		break;
536 	}
537 
538 	found = FALSE;
539 	if (fkey) {
540 	    /*
541 	     * Allow a mouse-click on a box to switch selection to that box.
542 	     * Handling a button click is a little more complicated, since we
543 	     * push a KEY_ENTER back onto the input stream so we'll put the
544 	     * cursor at the right place before handling the "keypress".
545 	     */
546 	    if (key >= DLGK_MOUSE(KEY_MAX)) {
547 		key -= DLGK_MOUSE(KEY_MAX);
548 		i = RowToItem(key);
549 		if (i < max_choice) {
550 		    found = TRUE;
551 		} else {
552 		    beep();
553 		    continue;
554 		}
555 	    } else if (is_DLGK_MOUSE(key)
556 		       && dlg_ok_buttoncode(key - M_EVENT) >= 0) {
557 		button = (key - M_EVENT);
558 		ungetch('\n');
559 		continue;
560 	    }
561 	} else {
562 	    /*
563 	     * Check if key pressed matches first character of any item tag in
564 	     * list.  If there is more than one match, we will cycle through
565 	     * each one as the same key is pressed repeatedly.
566 	     */
567 	    if (button < 0 || !dialog_state.visit_items) {
568 		for (j = scrollamt + choice + 1; j < item_no; j++) {
569 		    if (check_hotkey(items, j)) {
570 			found = TRUE;
571 			i = j - scrollamt;
572 			break;
573 		    }
574 		}
575 		if (!found) {
576 		    for (j = 0; j <= scrollamt + choice; j++) {
577 			if (check_hotkey(items, j)) {
578 			    found = TRUE;
579 			    i = j - scrollamt;
580 			    break;
581 			}
582 		    }
583 		}
584 		if (found)
585 		    dlg_flush_getc();
586 	    } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
587 		button = j;
588 		ungetch('\n');
589 		continue;
590 	    }
591 
592 	    /*
593 	     * A single digit (1-9) positions the selection to that line in the
594 	     * current screen.
595 	     */
596 	    if (!found
597 		&& (key <= '9')
598 		&& (key > '0')
599 		&& (key - '1' < max_choice)) {
600 		found = TRUE;
601 		i = key - '1';
602 	    }
603 	}
604 
605 	if (!found && fkey) {
606 	    found = TRUE;
607 	    switch (key) {
608 	    case DLGK_PAGE_FIRST:
609 		i = -scrollamt;
610 		break;
611 	    case DLGK_PAGE_LAST:
612 		i = item_no - 1 - scrollamt;
613 		break;
614 	    case DLGK_MOUSE(KEY_PPAGE):
615 	    case DLGK_PAGE_PREV:
616 		if (choice)
617 		    i = 0;
618 		else if (scrollamt != 0)
619 		    i = -MIN(scrollamt, max_choice);
620 		else
621 		    continue;
622 		break;
623 	    case DLGK_MOUSE(KEY_NPAGE):
624 	    case DLGK_PAGE_NEXT:
625 		i = MIN(choice + max_choice, item_no - scrollamt - 1);
626 		break;
627 	    case DLGK_ITEM_PREV:
628 		i = choice - 1;
629 		if (choice == 0 && scrollamt == 0)
630 		    continue;
631 		break;
632 	    case DLGK_ITEM_NEXT:
633 		i = choice + 1;
634 		if (scrollamt + choice >= item_no - 1)
635 		    continue;
636 		break;
637 	    default:
638 		found = FALSE;
639 		break;
640 	    }
641 	}
642 
643 	if (found) {
644 	    if (i != choice) {
645 		getyx(dialog, cur_y, cur_x);
646 		if (i < 0 || i >= max_choice) {
647 		    if (i < 0) {
648 			scrollamt += i;
649 			choice = 0;
650 		    } else {
651 			choice = max_choice - 1;
652 			scrollamt += (i - max_choice + 1);
653 		    }
654 		    print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
655 		} else {
656 		    choice = i;
657 		    print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
658 		    (void) wmove(dialog, cur_y, cur_x);
659 		    wrefresh(dialog);
660 		}
661 	    }
662 	    continue;		/* wait for another key press */
663 	}
664 
665 	if (fkey) {
666 	    switch (key) {
667 	    case DLGK_FIELD_PREV:
668 		button = dlg_prev_button(buttons, button);
669 		dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
670 				 FALSE, width);
671 		break;
672 
673 	    case DLGK_FIELD_NEXT:
674 		button = dlg_next_button(buttons, button);
675 		dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
676 				 FALSE, width);
677 		break;
678 
679 	    case DLGK_TOGGLE:
680 	    case DLGK_ENTER:
681 	    case DLGK_LEAVE:
682 		result = ((key == DLGK_LEAVE)
683 			  ? dlg_ok_buttoncode(button)
684 			  : dlg_enter_buttoncode(button));
685 
686 		/*
687 		 * If dlg_menu() is called from dialog_menu(), we want to
688 		 * capture the results into dialog_vars.input_result.
689 		 */
690 		if (result == DLG_EXIT_ERROR) {
691 		    result = DLG_EXIT_UNKNOWN;
692 		} else if (is_inputmenu
693 			   || rename_menutext == dlg_dummy_menutext) {
694 		    result = handle_button(result,
695 					   items,
696 					   scrollamt + choice);
697 		}
698 
699 		/*
700 		 * If we have a rename_menutext function, interpret the Extra
701 		 * button as a request to rename the menu's text.  If that
702 		 * function doesn't return "Unknown", we will exit from this
703 		 * function.  Usually that is done for dialog_menu(), so the
704 		 * shell script can use the updated value.  If it does return
705 		 * "Unknown", update the list item only.  A direct caller of
706 		 * dlg_menu() can free the renamed value - we cannot.
707 		 */
708 		if (is_inputmenu && result == DLG_EXIT_EXTRA) {
709 		    char *tmp;
710 
711 		    if (input_menu_edit(&all,
712 					&items[scrollamt + choice],
713 					choice,
714 					&tmp)) {
715 			result = rename_menutext(items, scrollamt + choice, tmp);
716 			if (result == DLG_EXIT_UNKNOWN) {
717 			    items[scrollamt + choice].text = tmp;
718 			} else {
719 			    free(tmp);
720 			}
721 		    } else {
722 			result = DLG_EXIT_UNKNOWN;
723 			print_item(&all,
724 				   menu,
725 				   &items[scrollamt + choice],
726 				   choice,
727 				   Selected,
728 				   is_inputmenu);
729 			(void) wnoutrefresh(menu);
730 			free(tmp);
731 		    }
732 
733 		    if (result == DLG_EXIT_UNKNOWN) {
734 			dlg_draw_buttons(dialog, height - 2, 0,
735 					 buttons, button, FALSE, width);
736 		    }
737 		}
738 		break;
739 #ifdef KEY_RESIZE
740 	    case KEY_RESIZE:
741 		dlg_will_resize(dialog);
742 		/* reset data */
743 		resizeit(height, LINES);
744 		resizeit(width, COLS);
745 		free(prompt);
746 		_dlg_resize_cleanup(dialog);
747 		/* repaint */
748 		goto retry;
749 #endif
750 	    default:
751 		flash();
752 		break;
753 	    }
754 	}
755     }
756 
757     dlg_mouse_free_regions();
758     dlg_unregister_window(menu);
759     dlg_del_window(dialog);
760     free(prompt);
761 
762     *current_item = scrollamt + choice;
763 
764     DLG_TRACE2N("current", *current_item);
765     return result;
766 }
767 
768 /*
769  * Display a menu for choosing among a number of options
770  */
771 int
dialog_menu(const char * title,const char * cprompt,int height,int width,int menu_height,int item_no,char ** items)772 dialog_menu(const char *title,
773 	    const char *cprompt,
774 	    int height,
775 	    int width,
776 	    int menu_height,
777 	    int item_no,
778 	    char **items)
779 {
780     int result;
781     int choice;
782     int i, j;
783     DIALOG_LISTITEM *listitems;
784 
785     listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
786     assert_ptr(listitems, "dialog_menu");
787 
788     for (i = j = 0; i < item_no; ++i) {
789 	listitems[i].name = items[j++];
790 	listitems[i].text = (dialog_vars.no_items
791 			     ? dlg_strempty()
792 			     : items[j++]);
793 	listitems[i].help = ((dialog_vars.item_help)
794 			     ? items[j++]
795 			     : dlg_strempty());
796     }
797     dlg_align_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
798 
799     result = dlg_menu(title,
800 		      cprompt,
801 		      height,
802 		      width,
803 		      menu_height,
804 		      item_no,
805 		      listitems,
806 		      &choice,
807 		      (dialog_vars.input_menu
808 		       ? dlg_renamed_menutext
809 		       : dlg_dummy_menutext));
810 
811     dlg_free_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
812     free(listitems);
813     return result;
814 }
815