2 * $Id: menubox.c,v 1.171 2020/11/23 21:03:11 tom Exp $
4 * menubox.c -- implements the menu box
6 * Copyright 2000-2019,2020 Thomas E. Dickey
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.
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.
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.
23 * An earlier version of this program lists as authors
24 * Savio Lam (lam836@cs.cuhk.hk)
27 #include <dlg_internals.h>
37 /* the outer-window */
45 /* the inner-window */
47 DIALOG_LISTITEM *items;
51 #define MIN_HIGH (1 + (5 * MARGIN))
53 #define INPUT_ROWS 3 /* rows per inputmenu entry */
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))
63 print_item(ALL_DATA * data,
65 DIALOG_LISTITEM * item,
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);
78 const char *show = (dialog_vars.no_items
88 bordchar = item_selected_attr;
91 bordchar = dialog_attr;
95 /* Clear 'residue' of last item and mark current current item */
99 dlg_attrset(win, (selected != Unselected) ? item_selected_attr : item_attr);
100 for (n = my_y - 1; n < my_y + INPUT_ROWS - 1; n++) {
102 wprintw(win, "%*s", my_width, " ");
105 dlg_attrset(win, menubox_attr);
107 wprintw(win, "%*s", my_width, " ");
110 /* highlight first char of the tag to be special */
112 (void) wmove(win, my_y, data->tag_x);
113 dlg_print_listitem(win, item->name, climit, first, selected);
117 /* Draw the input field box (only for inputmenu) */
118 (void) wmove(win, my_y, my_x);
121 dlg_draw_box(win, my_y - 1, my_x, INPUT_ROWS, my_width - my_x - data->tag_x,
128 /* print actual item */
129 wmove(win, my_y, my_x);
130 dlg_print_listitem(win, show, my_width - my_x, first, selected);
133 dlg_item_help(item->help);
135 dlg_attrset(win, save);
139 * Allow the user to edit the text of a menu entry.
142 input_menu_edit(ALL_DATA * data,
143 DIALOG_LISTITEM * items,
147 chtype save = dlg_get_attrs(data->menu);
150 int key = 0, fkey = 0;
153 bool is_inputmenu = TRUE;
154 int y = ItemToRow(choice);
156 int max_len = dlg_max_input(MAX((int) strlen(items->text) + 1, MAX_LEN));
158 result = dlg_malloc(char, (size_t) max_len);
159 assert_ptr(result, "input_menu_edit");
161 /* original item is used to initialize the input string. */
163 strcpy(result, items->text);
165 print_item(data, data->menu, items, choice, Editing, TRUE);
167 /* taken out of inputbox.c - but somewhat modified */
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) {
181 if (dlg_edit_string(result, &offset, key, fkey, first)) {
182 dlg_show_string(data->menu, result, offset, inputbox_attr,
185 data->menu_width - data->item_x - 3,
188 } else if (key == ESC || key == TAB) {
195 print_item(data, data->menu, items, choice, Selected, TRUE);
196 dlg_attrset(data->menu, save);
203 handle_button(int code, DIALOG_LISTITEM * items, int choice)
208 case DLG_EXIT_OK: /* FALLTHRU */
210 dlg_add_string(items[choice].name);
213 dlg_add_help_listitem(&code, &help_result, &items[choice]);
214 dlg_add_string(help_result);
222 dlg_renamed_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
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);
229 dlg_add_string(newtext);
231 return DLG_EXIT_EXTRA;
235 dlg_dummy_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
240 return DLG_EXIT_ERROR;
244 print_menu(ALL_DATA * data, int choice, int scrollamt, int max_choice, bool is_inputmenu)
248 for (i = 0; i < max_choice; i++) {
251 &data->items[i + scrollamt],
253 (i == choice) ? Selected : Unselected,
257 /* Clean bottom lines */
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;
266 waddch(data->menu, ' ');
271 (void) wnoutrefresh(data->menu);
273 dlg_draw_scrollbar(data->dialog,
276 scrollamt + max_choice,
279 data->box_x + data->menu_width,
281 data->box_y + data->menu_height + 1,
282 menubox_border2_attr,
283 menubox_border_attr);
287 check_hotkey(DIALOG_LISTITEM * items, int choice)
291 if (dlg_match_char(dlg_last_getc(),
294 : items[choice].name))) {
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
306 dlg_menu(const char *title,
312 DIALOG_LISTITEM * items,
314 DIALOG_INPUTMENU rename_menutext)
317 static DLG_KEYS_BINDING binding[] = {
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 ),
338 static DLG_KEYS_BINDING binding2[] = {
347 int old_LINES = LINES;
349 int old_height = height;
350 int old_width = width;
353 int i, j, x, y, cur_x, cur_y;
355 int button = dialog_state.visit_items ? -1 : dlg_default_button();
356 int choice = dlg_default_listitem(items);
357 int result = DLG_EXIT_UNKNOWN;
360 int use_width, name_width, text_width, list_width;
361 WINDOW *dialog, *menu;
363 const char **buttons = dlg_ok_labels();
364 bool is_inputmenu = ((rename_menutext != 0)
365 && (rename_menutext != dlg_dummy_menutext));
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);
377 dialog_state.plain_buttons = TRUE;
380 all.item_no = item_no;
388 prompt = dlg_strclone(cprompt);
389 dlg_tab_correct_str(prompt);
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);
399 dlg_auto_size(title, prompt,
401 MIN_HIGH + all.menu_height, use_width);
403 dlg_button_layout(buttons, &width);
404 dlg_print_size(height, width);
405 dlg_ctl_size(height, width);
407 x = dlg_box_x_ordinate(width);
408 y = dlg_box_y_ordinate(height);
410 dialog = dlg_new_window(height, width, y, x);
413 dlg_register_window(dialog, "menubox", binding);
414 dlg_register_buttons(dialog, "menubox", buttons);
416 dlg_mouse_setbase(x, y);
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);
422 dlg_attrset(dialog, dialog_attr);
423 dlg_print_autowrap(dialog, prompt, height, width);
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;
431 * After displaying the prompt, we know how much space we really have.
432 * Limit the list to avoid overwriting the ok-button.
434 all.menu_height = height - MIN_HIGH - cur_y;
435 if (all.menu_height <= 0)
438 /* Find out maximal number of displayable items at once. */
439 max_choice = MIN(all.menu_height,
442 max_choice /= INPUT_ROWS;
444 /* create new window for the menu */
445 menu = dlg_sub_window(dialog, all.menu_height, all.menu_width,
450 dlg_register_window(menu, "menu", binding2);
451 dlg_register_buttons(menu, "menu", buttons);
453 /* draw a box around the menu items */
455 all.box_y, all.box_x,
456 all.menu_height + 2, all.menu_width + 2,
457 menubox_border_attr, menubox_border2_attr);
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));
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,
474 * FIXME: the gutter width and name/list ratio should be configurable.
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);
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;
493 text_width = use_width - name_width;
495 list_width = (text_width + name_width);
498 all.tag_x = (is_inputmenu
500 : (use_width - list_width) / 2);
501 all.item_x = ((dialog_vars.no_tags
503 : (dialog_vars.no_items
505 : (GUTTER + name_width)))
508 if (choice - scrollamt >= max_choice) {
509 scrollamt = choice - (max_choice - 1);
510 choice = max_choice - 1;
513 print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
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 */ );
520 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
522 dlg_trace_win(dialog);
524 while (result == DLG_EXIT_UNKNOWN) {
527 if (button < 0) /* --visit-items */
529 all.box_y + ItemToRow(choice) + 1,
530 all.box_x + all.tag_x + 1);
532 key = dlg_mouse_wgetch(dialog, &fkey);
533 if (dlg_result_key(key, fkey, &result)) {
534 if (!dlg_button_key(result, &button, &key, &fkey))
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".
546 if (key >= DLGK_MOUSE(KEY_MAX)) {
547 key -= DLGK_MOUSE(KEY_MAX);
549 if (i < max_choice) {
555 } else if (is_DLGK_MOUSE(key)
556 && dlg_ok_buttoncode(key - M_EVENT) >= 0) {
557 button = (key - M_EVENT);
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.
567 if (button < 0 || !dialog_state.visit_items) {
568 for (j = scrollamt + choice + 1; j < item_no; j++) {
569 if (check_hotkey(items, j)) {
576 for (j = 0; j <= scrollamt + choice; j++) {
577 if (check_hotkey(items, j)) {
586 } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
593 * A single digit (1-9) positions the selection to that line in the
599 && (key - '1' < max_choice)) {
605 if (!found && fkey) {
608 case DLGK_PAGE_FIRST:
612 i = item_no - 1 - scrollamt;
614 case DLGK_MOUSE(KEY_PPAGE):
618 else if (scrollamt != 0)
619 i = -MIN(scrollamt, max_choice);
623 case DLGK_MOUSE(KEY_NPAGE):
625 i = MIN(choice + max_choice, item_no - scrollamt - 1);
629 if (choice == 0 && scrollamt == 0)
634 if (scrollamt + choice >= item_no - 1)
645 getyx(dialog, cur_y, cur_x);
646 if (i < 0 || i >= max_choice) {
651 choice = max_choice - 1;
652 scrollamt += (i - max_choice + 1);
654 print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
657 print_menu(&all, choice, scrollamt, max_choice, is_inputmenu);
658 (void) wmove(dialog, cur_y, cur_x);
662 continue; /* wait for another key press */
667 case DLGK_FIELD_PREV:
668 button = dlg_prev_button(buttons, button);
669 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
673 case DLGK_FIELD_NEXT:
674 button = dlg_next_button(buttons, button);
675 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
682 result = ((key == DLGK_LEAVE)
683 ? dlg_ok_buttoncode(button)
684 : dlg_enter_buttoncode(button));
687 * If dlg_menu() is called from dialog_menu(), we want to
688 * capture the results into dialog_vars.input_result.
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,
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.
708 if (is_inputmenu && result == DLG_EXIT_EXTRA) {
711 if (input_menu_edit(&all,
712 &items[scrollamt + choice],
715 result = rename_menutext(items, scrollamt + choice, tmp);
716 if (result == DLG_EXIT_UNKNOWN) {
717 items[scrollamt + choice].text = tmp;
722 result = DLG_EXIT_UNKNOWN;
725 &items[scrollamt + choice],
729 (void) wnoutrefresh(menu);
733 if (result == DLG_EXIT_UNKNOWN) {
734 dlg_draw_buttons(dialog, height - 2, 0,
735 buttons, button, FALSE, width);
741 dlg_will_resize(dialog);
743 resizeit(height, LINES);
744 resizeit(width, COLS);
746 _dlg_resize_cleanup(dialog);
757 dlg_mouse_free_regions();
758 dlg_unregister_window(menu);
759 dlg_del_window(dialog);
762 *current_item = scrollamt + choice;
764 DLG_TRACE2N("current", *current_item);
769 * Display a menu for choosing among a number of options
772 dialog_menu(const char *title,
783 DIALOG_LISTITEM *listitems;
785 listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
786 assert_ptr(listitems, "dialog_menu");
788 for (i = j = 0; i < item_no; ++i) {
789 listitems[i].name = items[j++];
790 listitems[i].text = (dialog_vars.no_items
793 listitems[i].help = ((dialog_vars.item_help)
797 dlg_align_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
799 result = dlg_menu(title,
807 (dialog_vars.input_menu
808 ? dlg_renamed_menutext
809 : dlg_dummy_menutext));
811 dlg_free_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);