2 * $Id: menubox.c,v 1.122 2011/06/29 09:48:46 tom Exp $
4 * menubox.c -- implements the menu box
6 * Copyright 2000-2010,2011 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)
30 static int menu_width, tag_x, item_x;
38 #define MIN_HIGH (1 + (5 * MARGIN))
40 #define INPUT_ROWS 3 /* rows per inputmenu entry */
42 #define LLEN(n) ((n) * MENUBOX_TAGS)
43 #define ItemName(i) items[LLEN(i)]
44 #define ItemText(i) items[LLEN(i) + 1]
45 #define ItemHelp(i) items[LLEN(i) + 2]
47 #define RowHeight(i) (is_inputmenu ? ((i) * INPUT_ROWS) : ((i) * 1))
48 #define ItemToRow(i) (is_inputmenu ? ((i) * INPUT_ROWS + 1) : (i))
49 #define RowToItem(i) (is_inputmenu ? ((i) / INPUT_ROWS + 0) : (i))
52 print_arrows(WINDOW *win,
60 dlg_draw_scrollbar(win,
63 scrollamt + max_choice,
68 box_y + menu_height + 1,
74 * Print the tag of a menu-item
77 print_tag(WINDOW *win,
78 DIALOG_LISTITEM * item,
84 int my_y = ItemToRow(choice);
85 int tag_width = (my_x - tag_x - GUTTER);
91 cols = dlg_index_columns(item->name);
92 indx = dlg_index_wchars(item->name);
93 limit = dlg_count_wchars(item->name);
94 prefix = (indx[1] - indx[0]);
96 /* highlight first char of the tag to be special */
97 (void) wmove(win, my_y, tag_x);
98 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
99 if (strlen(item->name) != 0)
100 (void) waddnstr(win, item->name, prefix);
101 /* print rest of the string */
102 wattrset(win, selected ? tag_selected_attr : tag_attr);
103 if ((int) strlen(item->name) > prefix) {
104 limit = dlg_limit_columns(item->name, tag_width, 1);
106 (void) waddnstr(win, item->name + indx[1], indx[limit] - indx[1]);
114 print_item(WINDOW *win,
115 DIALOG_LISTITEM * items,
120 chtype save = dlg_get_attrs(win);
122 int my_width = menu_width;
124 int my_y = ItemToRow(choice);
125 chtype attr = A_NORMAL;
132 textchar = item_attr;
133 bordchar = item_attr;
136 textchar = item_selected_attr;
137 bordchar = item_selected_attr;
140 textchar = inputbox_attr;
141 bordchar = dialog_attr;
145 /* Clear 'residue' of last item and mark current current item */
147 wattrset(win, (selected != Unselected) ? item_selected_attr : item_attr);
148 for (n = my_y - 1; n < my_y + INPUT_ROWS - 1; n++) {
150 wprintw(win, "%*s", my_width, " ");
153 wattrset(win, menubox_attr);
155 wprintw(win, "%*s", my_width, " ");
158 print_tag(win, items, choice, selected, is_inputmenu);
160 /* Draw the input field box (only for inputmenu) */
161 (void) wmove(win, my_y, my_x);
164 dlg_draw_box(win, my_y - 1, my_x, INPUT_ROWS, my_width - my_x - tag_x,
171 /* print actual item */
172 wmove(win, my_y, my_x);
173 wattrset(win, textchar);
174 dlg_print_text(win, items->text, my_width - my_x, &attr);
177 dlg_item_help(items->help);
183 * Allow the user to edit the text of a menu entry.
186 input_menu_edit(WINDOW *win,
187 DIALOG_LISTITEM * items,
191 chtype save = dlg_get_attrs(win);
194 int key = 0, fkey = 0;
197 bool is_inputmenu = TRUE;
198 int y = ItemToRow(choice);
200 int max_len = dlg_max_input(MAX((int) strlen(items->text) + 1, MAX_LEN));
202 result = dlg_malloc(char, (size_t) max_len);
203 assert_ptr(result, "input_menu_edit");
205 /* original item is used to initialize the input string. */
207 strcpy(result, items->text);
209 print_item(win, items, choice, Editing, TRUE);
211 /* taken out of inputbox.c - but somewhat modified */
214 key = dlg_mouse_wgetch(win, &fkey);
215 if (dlg_edit_string(result, &offset, key, fkey, first)) {
216 dlg_show_string(win, result, offset, inputbox_attr,
217 y, item_x + 1, menu_width - item_x - 3,
220 } else if (key == ESC || key == TAB) {
227 print_item(win, items, choice, Selected, TRUE);
235 handle_button(int code, DIALOG_LISTITEM * items, int choice)
238 case DLG_EXIT_OK: /* FALLTHRU */
240 dlg_add_string(items[choice].name);
243 dlg_add_result("HELP ");
244 if (USE_ITEM_HELP(items[choice].help)) {
245 dlg_add_string(items[choice].help);
246 code = DLG_EXIT_ITEM_HELP;
248 dlg_add_string(items[choice].name);
256 dlg_renamed_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
258 if (dialog_vars.input_result)
259 dialog_vars.input_result[0] = '\0';
260 dlg_add_result("RENAMED ");
261 dlg_add_string(items[current].name);
263 dlg_add_string(newtext);
264 return DLG_EXIT_EXTRA;
268 dlg_dummy_menutext(DIALOG_LISTITEM * items, int current, char *newtext)
273 return DLG_EXIT_ERROR;
277 * This is an alternate interface to 'menu' which allows the application
278 * to read the list item states back directly without putting them in the
282 dlg_menu(const char *title,
288 DIALOG_LISTITEM * items,
290 DIALOG_INPUTMENU rename_menutext)
293 static DLG_KEYS_BINDING binding[] = {
296 DLG_KEYS_DATA( DLGK_FIELD_NEXT, ' ' ),
297 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
298 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
299 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
300 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
301 DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+' ),
302 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
303 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ),
304 DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ),
305 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
306 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ),
307 DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ),
308 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_END ),
309 DLG_KEYS_DATA( DLGK_PAGE_LAST, KEY_LL ),
310 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
311 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
314 static DLG_KEYS_BINDING binding2[] = {
323 int old_height = height;
324 int old_width = width;
326 int i, j, x, y, cur_x, cur_y, box_x, box_y;
328 int button = dialog_state.visit_items ? -1 : dlg_defaultno_button();
329 int choice = dlg_default_listitem(items);
330 int result = DLG_EXIT_UNKNOWN;
332 int max_choice, min_width;
334 int use_height, use_width, name_width, text_width;
335 WINDOW *dialog, *menu;
336 char *prompt = dlg_strclone(cprompt);
337 const char **buttons = dlg_ok_labels();
338 bool is_inputmenu = (rename_menutext == dlg_renamed_menutext);
341 dlg_tab_correct_str(prompt);
347 use_height = menu_height;
348 if (use_height == 0) {
349 min_width = dlg_calc_list_width(item_no, items) + 10;
350 /* calculate height without items (4) */
351 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MAX(26, min_width));
352 dlg_calc_listh(&height, &use_height, item_no);
354 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, 26);
356 dlg_button_layout(buttons, &width);
357 dlg_print_size(height, width);
358 dlg_ctl_size(height, width);
360 x = dlg_box_x_ordinate(width);
361 y = dlg_box_y_ordinate(height);
363 dialog = dlg_new_window(height, width, y, x);
364 dlg_register_window(dialog, "menubox", binding);
365 dlg_register_buttons(dialog, "menubox", buttons);
367 dlg_mouse_setbase(x, y);
369 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
370 dlg_draw_bottom_box(dialog);
371 dlg_draw_title(dialog, title);
373 wattrset(dialog, dialog_attr);
374 dlg_print_autowrap(dialog, prompt, height, width);
376 menu_width = width - 6;
377 getyx(dialog, cur_y, cur_x);
379 box_x = (width - menu_width) / 2 - 1;
382 * After displaying the prompt, we know how much space we really have.
383 * Limit the list to avoid overwriting the ok-button.
385 if (use_height + MIN_HIGH > height - cur_y)
386 use_height = height - MIN_HIGH - cur_y;
390 /* Find out maximal number of displayable items at once. */
391 max_choice = MIN(use_height,
394 max_choice /= INPUT_ROWS;
396 /* create new window for the menu */
397 menu = dlg_sub_window(dialog, use_height, menu_width,
400 dlg_register_window(menu, "menu", binding2);
401 dlg_register_buttons(menu, "menu", buttons);
403 /* draw a box around the menu items */
404 dlg_draw_box(dialog, box_y, box_x, use_height + 2, menu_width + 2,
405 menubox_border_attr, menubox_attr);
410 /* Find length of longest item to center menu *
411 * only if --menu was given, using --inputmenu *
412 * won't be centered. */
413 for (i = 0; i < item_no; i++) {
414 name_width = MAX(name_width, dlg_count_columns(items[i].name));
415 text_width = MAX(text_width, dlg_count_columns(items[i].text));
418 /* If the name+text is wider than the list is allowed, then truncate
419 * one or both of them. If the name is no wider than 30% of the list,
422 * FIXME: the gutter width and name/list ratio should be configurable.
424 use_width = (menu_width - GUTTER);
425 if (text_width + name_width > use_width) {
426 int need = (int) (0.30 * use_width);
427 if (name_width > need) {
428 int want = (int) (use_width
429 * ((double) name_width)
430 / (text_width + name_width));
431 name_width = (want > need) ? want : need;
433 text_width = use_width - name_width;
436 tag_x = (is_inputmenu
438 : (use_width - text_width - name_width) / 2);
439 item_x = name_width + tag_x + GUTTER;
441 if (choice - scrollamt >= max_choice) {
442 scrollamt = choice - (max_choice - 1);
443 choice = max_choice - 1;
447 for (i = 0; i < max_choice; i++) {
449 &items[i + scrollamt],
451 (i == choice) ? Selected : Unselected,
454 (void) wnoutrefresh(menu);
456 /* register the new window, along with its borders */
457 dlg_mouse_mkbigregion(box_y + 1, box_x, use_height + 2, menu_width + 2,
458 KEY_MAX, 1, 1, 1 /* by lines */ );
460 print_arrows(dialog, box_x, box_y, scrollamt, max_choice, item_no, use_height);
462 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
464 while (result == DLG_EXIT_UNKNOWN) {
465 if (button < 0) /* --visit-items */
466 wmove(dialog, box_y + ItemToRow(choice) + 1, box_x + tag_x + 1);
468 key = dlg_mouse_wgetch(dialog, &fkey);
469 if (dlg_result_key(key, fkey, &result))
475 * Allow a mouse-click on a box to switch selection to that box.
476 * Handling a button click is a little more complicated, since we
477 * push a KEY_ENTER back onto the input stream so we'll put the
478 * cursor at the right place before handling the "keypress".
480 if (key >= DLGK_MOUSE(KEY_MAX)) {
481 key -= DLGK_MOUSE(KEY_MAX);
483 if (i < max_choice) {
489 } else if (is_DLGK_MOUSE(key)
490 && dlg_ok_buttoncode(key - M_EVENT) >= 0) {
491 button = (key - M_EVENT);
497 * Check if key pressed matches first character of any item tag in
498 * list. If there is more than one match, we will cycle through
499 * each one as the same key is pressed repeatedly.
501 if (button < 0 || !dialog_state.visit_items) {
502 for (j = scrollamt + choice + 1; j < item_no; j++) {
503 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
510 for (j = 0; j <= scrollamt + choice; j++) {
511 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
520 } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
527 * A single digit (1-9) positions the selection to that line in the
533 && (key - '1' < max_choice)) {
539 if (!found && fkey) {
542 case DLGK_PAGE_FIRST:
546 i = item_no - 1 - scrollamt;
548 case DLGK_MOUSE(KEY_PPAGE):
552 else if (scrollamt != 0)
553 i = -MIN(scrollamt, max_choice);
557 case DLGK_MOUSE(KEY_NPAGE):
559 i = MIN(choice + max_choice, item_no - scrollamt - 1);
563 if (choice == 0 && scrollamt == 0)
568 if (scrollamt + choice >= item_no - 1)
579 getyx(dialog, cur_y, cur_x);
580 if (i < 0 || i >= max_choice) {
581 #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5
583 * Using wscrl to assist ncurses scrolling is not needed
587 if (use_height > 1) {
588 /* De-highlight current first item */
591 0, Unselected, is_inputmenu);
592 scrollok(menu, TRUE);
593 wscrl(menu, -RowHeight(1));
594 scrollok(menu, FALSE);
599 0, Selected, is_inputmenu);
600 } else if (i == max_choice) {
601 if (use_height > 1) {
602 /* De-highlight current last item before scrolling up */
604 &items[scrollamt + max_choice - 1],
608 scrollok(menu, TRUE);
609 wscrl(menu, RowHeight(1));
610 scrollok(menu, FALSE);
614 &items[scrollamt + max_choice - 1],
615 max_choice - 1, TRUE,
624 choice = max_choice - 1;
625 scrollamt += (i - max_choice + 1);
627 for (i = 0; i < max_choice; i++) {
629 &items[scrollamt + i],
631 (i == choice) ? Selected : Unselected,
635 /* Clean bottom lines */
637 int spare_lines, x_count;
638 spare_lines = use_height % INPUT_ROWS;
639 wattrset(menu, menubox_attr);
640 for (; spare_lines; spare_lines--) {
641 wmove(menu, use_height - spare_lines, 0);
642 for (x_count = 0; x_count < menu_width;
648 (void) wnoutrefresh(menu);
651 scrollamt, max_choice, item_no, use_height);
653 /* De-highlight current item */
655 &items[scrollamt + choice],
659 /* Highlight new item */
662 &items[scrollamt + choice],
666 (void) wnoutrefresh(menu);
669 scrollamt, max_choice, item_no, use_height);
670 (void) wmove(dialog, cur_y, cur_x);
674 continue; /* wait for another key press */
679 case DLGK_FIELD_PREV:
680 button = dlg_prev_button(buttons, button);
681 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
684 case DLGK_FIELD_NEXT:
685 button = dlg_next_button(buttons, button);
686 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
690 result = dlg_enter_buttoncode(button);
693 * If dlg_menu() is called from dialog_menu(), we want to
694 * capture the results into dialog_vars.input_result, but not
695 * if dlg_menu() is called directly from an application. We
696 * can check this by testing if rename_menutext is the function
697 * pointer owned by dialog_menu(). It would be nicer to have
698 * this logic inside dialog_menu(), but that cannot be done
699 * since we would lose compatibility for the results reported
700 * after input_menu_edit().
702 if (result == DLG_EXIT_ERROR) {
703 result = DLG_EXIT_UNKNOWN;
704 } else if (is_inputmenu
705 || rename_menutext == dlg_dummy_menutext) {
706 result = handle_button(result,
712 * If we have a rename_menutext function, interpret the Extra
713 * button as a request to rename the menu's text. If that
714 * function doesn't return "Unknown", we will exit from this
715 * function. Usually that is done for dialog_menu(), so the
716 * shell script can use the updated value. If it does return
717 * "Unknown", update the list item only. A direct caller of
718 * dlg_menu() can free the renamed value - we cannot.
720 if (is_inputmenu && result == DLG_EXIT_EXTRA) {
723 if (input_menu_edit(menu,
724 &items[scrollamt + choice],
727 result = rename_menutext(items, scrollamt + choice, tmp);
728 if (result == DLG_EXIT_UNKNOWN) {
729 items[scrollamt + choice].text = tmp;
734 result = DLG_EXIT_UNKNOWN;
736 &items[scrollamt + choice],
740 (void) wnoutrefresh(menu);
744 if (result == DLG_EXIT_UNKNOWN) {
745 dlg_draw_buttons(dialog, height - 2, 0,
746 buttons, button, FALSE, width);
757 dlg_del_window(dialog);
759 dlg_mouse_free_regions();
769 dlg_mouse_free_regions();
770 dlg_unregister_window(menu);
771 dlg_del_window(dialog);
774 *current_item = scrollamt + choice;
779 * Display a menu for choosing among a number of options
782 dialog_menu(const char *title,
793 DIALOG_LISTITEM *listitems;
795 listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
796 assert_ptr(listitems, "dialog_menu");
798 for (i = 0; i < item_no; ++i) {
799 listitems[i].name = ItemName(i);
800 listitems[i].text = ItemText(i);
801 listitems[i].help = ((dialog_vars.item_help)
805 dlg_align_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
807 result = dlg_menu(title,
815 dialog_vars.input_menu ? dlg_renamed_menutext : dlg_dummy_menutext);
817 dlg_free_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);