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