2 * menubox.c -- implements the menu box
4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * Substantial rennovation: 12/18/95, Jordan K. Hubbard
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static const char rcsid[] =
29 #include "dialog.priv.h"
32 static void print_item(WINDOW *win, unsigned char *tag, unsigned char *item, int choice, int selected, dialogMenuItem *me, int menu_width, int tag_x, int item_x);
34 #define DREF(di, item) ((di) ? &((di)[(item)]) : NULL)
37 * Display a menu for choosing among a number of options
40 dialog_menu(unsigned char *title, unsigned char *prompt, int height, int width, int menu_height, int cnt, void *it, unsigned char *result, int *ch, int *sc)
42 int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button, choice,
43 l, k, scroll, max_choice, item_no, redraw_menu = FALSE;
44 char okButton, cancelButton;
45 int rval = 0, ok_space, cancel_space;
46 WINDOW *dialog, *menu;
47 unsigned char **items = NULL;
48 dialogMenuItem *ditems;
49 int menu_width, tag_x, item_x;
52 choice = ch ? *ch : 0;
53 scroll = sc ? *sc : 0;
56 /* If item_no is a positive integer, use old item specification format */
62 /* It's the new specification format - fake the rest of the code out */
67 items = (unsigned char **)alloca((item_no * 2) * sizeof(unsigned char *));
69 /* Initializes status */
70 for (i = 0; i < item_no; i++) {
71 items[i*2] = ditems[i].prompt;
72 items[i*2 + 1] = ditems[i].title;
75 max_choice = MIN(menu_height, item_no);
79 /* Find length of longest item in order to center menu */
80 for (i = 0; i < item_no; i++) {
81 l = strlen(items[i * 2]);
82 for (j = 0; j < item_no; j++) {
83 k = strlen(items[j * 2 + 1]);
84 tag_x = MAX(tag_x, l + k + 2);
86 item_x = MAX(item_x, l);
89 height = strheight(prompt) + menu_height + 4 + 2;
92 j = ((title != NULL) ? strwidth(title) : 0);
94 width = MAX(width, tag_x + 4) + 4;
96 width = MAX(width, 24);
102 /* center dialog box on screen */
103 x = DialogX ? DialogX : (COLS - width) / 2;
104 y = DialogY ? DialogY : (LINES - height) / 2;
108 draw_shadow(stdscr, y, x, height, width);
110 dialog = newwin(height, width, y, x);
111 if (dialog == NULL) {
113 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height, width, y, x);
116 keypad(dialog, TRUE);
118 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
119 wattrset(dialog, border_attr);
120 wmove(dialog, height - 3, 0);
121 waddch(dialog, ACS_LTEE);
122 for (i = 0; i < width - 2; i++)
123 waddch(dialog, ACS_HLINE);
124 wattrset(dialog, dialog_attr);
125 waddch(dialog, ACS_RTEE);
126 wmove(dialog, height - 2, 1);
127 for (i = 0; i < width - 2; i++)
131 wattrset(dialog, title_attr);
132 wmove(dialog, 0, (width - strlen(title)) / 2 - 1);
134 waddstr(dialog, title);
137 wattrset(dialog, dialog_attr);
139 print_autowrap(dialog, prompt, height - 1, width - 2, width, 1, 2, TRUE, FALSE);
141 menu_width = width - 6;
142 getyx(dialog, cur_y, cur_x);
144 box_x = (width - menu_width) / 2 - 1;
146 /* create new window for the menu */
147 menu = subwin(dialog, menu_height, menu_width, y + box_y + 1, x + box_x + 1);
151 fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", menu_height, menu_width,
152 y + box_y + 1, x + box_x + 1);
157 /* draw a box around the menu items */
158 draw_box(dialog, box_y, box_x, menu_height+2, menu_width+2, menubox_border_attr, menubox_attr);
160 tag_x = menu_width > tag_x + 1 ? (menu_width - tag_x) / 2 : 1;
161 item_x = menu_width > item_x + 4 ? tag_x + item_x + 2 : menu_width - 3;
164 for (i = 0; i < max_choice; i++)
165 print_item(menu, items[(scroll + i) * 2], items[(scroll + i) * 2 + 1], i, i == choice, DREF(ditems, scroll + i), menu_width, tag_x, item_x);
167 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
169 display_helpline(dialog, height - 1, width);
174 if (ditems && result) {
175 cancelButton = toupper(ditems[CANCEL_BUTTON].prompt[0]);
176 print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : FALSE);
177 okButton = toupper(ditems[OK_BUTTON].prompt[0]);
178 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : TRUE);
182 print_button(dialog, "Cancel", y, x + 14, FALSE);
184 print_button(dialog, " OK ", y, x, TRUE);
189 key = wgetch(dialog);
191 /* Shortcut to OK? */
192 if (toupper(key) == okButton) {
194 if (result && ditems[OK_BUTTON].fire) {
198 save = dupwin(newscr);
199 status = ditems[OK_BUTTON].fire(&ditems[OK_BUTTON]);
200 if (status & DITEM_RESTORE) {
208 strcpy(result, items[(scroll + choice) * 2]);
210 key = ESC; /* Punt! */
214 /* Shortcut to cancel? */
215 if (toupper(key) == cancelButton) {
216 if (ditems && result && ditems[CANCEL_BUTTON].fire) {
220 save = dupwin(newscr);
221 status = ditems[CANCEL_BUTTON].fire(&ditems[CANCEL_BUTTON]);
222 if (status & DITEM_RESTORE) {
229 key = ESC; /* Run away! */
233 /* Check if key pressed matches first character of any item tag in menu */
234 for (i = 0; i < max_choice; i++)
235 if (key < 0x100 && key != ' ' && toupper(key) == toupper(items[(scroll + i) * 2][0]))
238 if (i < max_choice || (key >= '1' && key <= MIN('9', '0'+max_choice)) || KEY_IS_UP(key) || KEY_IS_DOWN(key)) {
239 if (key >= '1' && key <= MIN('9', '0'+max_choice))
241 else if (KEY_IS_UP(key)) {
244 /* Scroll menu down */
245 getyx(dialog, cur_y, cur_x); /* Save cursor position */
246 if (menu_height > 1) {
247 /* De-highlight current first item before scrolling down */
248 print_item(menu, items[scroll * 2], items[scroll * 2 + 1], 0, FALSE, DREF(ditems, scroll), menu_width, tag_x, item_x);
249 scrollok(menu, TRUE);
251 scrollok(menu, FALSE);
254 print_item(menu, items[scroll * 2], items[scroll * 2 + 1], 0, TRUE, DREF(ditems, scroll), menu_width, tag_x, item_x);
256 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
259 continue; /* wait for another key press */
264 else if (KEY_IS_DOWN(key)) {
265 if (choice == max_choice - 1) {
266 if (scroll + choice < item_no - 1) {
268 getyx(dialog, cur_y, cur_x); /* Save cursor position */
269 if (menu_height > 1) {
270 /* De-highlight current last item before scrolling up */
271 print_item(menu, items[(scroll + max_choice - 1) * 2],
272 items[(scroll + max_choice - 1) * 2 + 1],
273 max_choice-1, FALSE, DREF(ditems, scroll + max_choice - 1), menu_width, tag_x, item_x);
274 scrollok(menu, TRUE);
276 scrollok(menu, FALSE);
279 print_item(menu, items[(scroll + max_choice - 1) * 2],
280 items[(scroll + max_choice - 1) * 2 + 1],
281 max_choice - 1, TRUE, DREF(ditems, scroll + max_choice - 1), menu_width, tag_x, item_x);
283 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
286 continue; /* wait for another key press */
293 /* De-highlight current item */
294 getyx(dialog, cur_y, cur_x); /* Save cursor position */
295 print_item(menu, items[(scroll + choice) * 2], items[(scroll + choice) * 2 + 1], choice, FALSE, DREF(ditems, scroll + choice), menu_width, tag_x, item_x);
297 /* Highlight new item */
299 print_item(menu, items[(scroll + choice) * 2], items[(scroll + choice) * 2 + 1], choice, TRUE, DREF(ditems, scroll + choice), menu_width, tag_x, item_x);
301 wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */
304 continue; /* wait for another key press */
309 if (scroll > height - 4) { /* can we go up? */
310 scroll -= (height - 4);
318 if (scroll + menu_height >= item_no-1 - menu_height) { /* can we go down a full page? */
319 scroll = item_no - menu_height;
323 scroll += menu_height;
335 scroll = item_no - menu_height;
338 choice = max_choice - 1;
347 if (ditems && result) {
348 print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button);
349 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
351 cancel_space = strlen(ditems[OK_BUTTON].prompt) + 6;
354 print_button(dialog, "Cancel", y, x + 14, button);
355 print_button(dialog, " OK ", y, x, !button);
360 wmove(dialog, y, x+cancel_space);
362 wmove(dialog, y, x+ok_space);
370 /* A fire routine can do just about anything to the screen, so be prepared
371 to accept some hints as to what to do in the aftermath. */
373 if (ditems[scroll + choice].fire) {
377 save = dupwin(newscr);
378 status = ditems[scroll + choice].fire(&ditems[scroll + choice]);
379 if (status & DITEM_RESTORE) {
384 if (status & DITEM_CONTINUE)
386 else if (status & DITEM_LEAVE_MENU) {
387 /* Allow a fire action to take us out of the menu */
391 else if (status & DITEM_RECREATE) {
400 strcpy(result, items[(scroll+choice)*2]);
416 /* save info about menu item position */
423 for (i = 0; i < max_choice; i++) {
424 print_item(menu, items[(scroll + i) * 2], items[(scroll + i) * 2 + 1], i, i == choice, DREF(ditems, scroll + i), menu_width, tag_x, item_x);
427 getyx(dialog, cur_y, cur_x); /* Save cursor position */
428 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
429 wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */
444 print_item(WINDOW *win, unsigned char *tag, unsigned char *item, int choice, int selected, dialogMenuItem *me, int menu_width, int tag_x, int item_x)
448 /* Clear 'residue' of last item */
449 wattrset(win, menubox_attr);
450 wmove(win, choice, 0);
451 for (i = 0; i < menu_width; i++)
453 wmove(win, choice, tag_x);
454 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
456 wattrset(win, selected ? tag_selected_attr : tag_attr);
457 waddnstr(win, tag + 1, item_x - tag_x - 3);
458 wmove(win, choice, item_x);
459 wattrset(win, selected ? item_selected_attr : item_attr);
460 waddnstr(win, item, menu_width - item_x - 1);
461 /* If have a selection handler for this, call it */
462 if (me && me->selected) {
464 me->selected(me, selected);
467 /* End of print_item() */