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.
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
27 #include "dialog.priv.h"
31 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);
33 #define DREF(di, item) ((di) ? &((di)[(item)]) : NULL)
36 * Display a menu for choosing among a number of options
39 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)
41 int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button, choice,
42 l, k, scroll, max_choice, item_no, redraw_menu = FALSE;
43 char okButton, cancelButton;
44 int rval = 0, ok_space, cancel_space;
45 WINDOW *dialog, *menu;
46 unsigned char **items = NULL;
47 dialogMenuItem *ditems;
48 int menu_width, tag_x, item_x;
51 choice = ch ? *ch : 0;
52 scroll = sc ? *sc : 0;
55 /* If item_no is a positive integer, use old item specification format */
61 /* It's the new specification format - fake the rest of the code out */
66 items = (unsigned char **)alloca((item_no * 2) * sizeof(unsigned char *));
68 /* Initializes status */
69 for (i = 0; i < item_no; i++) {
70 items[i*2] = ditems[i].prompt;
71 items[i*2 + 1] = ditems[i].title;
74 max_choice = MIN(menu_height, item_no);
78 /* Find length of longest item in order to center menu */
79 for (i = 0; i < item_no; i++) {
80 l = strlen(items[i * 2]);
81 for (j = 0; j < item_no; j++) {
82 k = strlen(items[j * 2 + 1]);
83 tag_x = MAX(tag_x, l + k + 2);
85 item_x = MAX(item_x, l);
88 height = strheight(prompt) + menu_height + 4 + 2;
91 j = ((title != NULL) ? strwidth(title) : 0);
93 width = MAX(width, tag_x + 4) + 4;
95 width = MAX(width, 24);
101 /* center dialog box on screen */
102 x = DialogX ? DialogX : (COLS - width) / 2;
103 y = DialogY ? DialogY : (LINES - height) / 2;
107 draw_shadow(stdscr, y, x, height, width);
109 dialog = newwin(height, width, y, x);
110 if (dialog == NULL) {
112 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height, width, y, x);
115 keypad(dialog, TRUE);
117 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
118 wattrset(dialog, border_attr);
119 wmove(dialog, height - 3, 0);
120 waddch(dialog, ACS_LTEE);
121 for (i = 0; i < width - 2; i++)
122 waddch(dialog, ACS_HLINE);
123 wattrset(dialog, dialog_attr);
124 waddch(dialog, ACS_RTEE);
125 wmove(dialog, height - 2, 1);
126 for (i = 0; i < width - 2; i++)
130 wattrset(dialog, title_attr);
131 wmove(dialog, 0, (width - strlen(title)) / 2 - 1);
133 waddstr(dialog, title);
136 wattrset(dialog, dialog_attr);
138 print_autowrap(dialog, prompt, height - 1, width - 2, width, 1, 2, TRUE, FALSE);
140 menu_width = width - 6;
141 getyx(dialog, cur_y, cur_x);
143 box_x = (width - menu_width) / 2 - 1;
145 /* create new window for the menu */
146 menu = subwin(dialog, menu_height, menu_width, y + box_y + 1, x + box_x + 1);
150 fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", menu_height, menu_width,
151 y + box_y + 1, x + box_x + 1);
156 /* draw a box around the menu items */
157 draw_box(dialog, box_y, box_x, menu_height+2, menu_width+2, menubox_border_attr, menubox_attr);
159 tag_x = menu_width > tag_x + 1 ? (menu_width - tag_x) / 2 : 1;
160 item_x = menu_width > item_x + 4 ? tag_x + item_x + 2 : menu_width - 3;
163 for (i = 0; i < max_choice; i++)
164 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);
166 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
168 display_helpline(dialog, height - 1, width);
173 if (ditems && result) {
174 cancelButton = toupper(ditems[CANCEL_BUTTON].prompt[0]);
175 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);
176 okButton = toupper(ditems[OK_BUTTON].prompt[0]);
177 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : TRUE);
181 print_button(dialog, "Cancel", y, x + 14, FALSE);
183 print_button(dialog, " OK ", y, x, TRUE);
188 key = wgetch(dialog);
190 /* Shortcut to OK? */
191 if (toupper(key) == okButton) {
193 if (result && ditems[OK_BUTTON].fire) {
197 save = dupwin(newscr);
198 status = ditems[OK_BUTTON].fire(&ditems[OK_BUTTON]);
199 if (status & DITEM_RESTORE) {
207 strcpy(result, items[(scroll + choice) * 2]);
209 key = ESC; /* Punt! */
213 /* Shortcut to cancel? */
214 if (toupper(key) == cancelButton) {
215 if (ditems && result && ditems[CANCEL_BUTTON].fire) {
219 save = dupwin(newscr);
220 status = ditems[CANCEL_BUTTON].fire(&ditems[CANCEL_BUTTON]);
221 if (status & DITEM_RESTORE) {
228 key = ESC; /* Run away! */
232 /* Check if key pressed matches first character of any item tag in menu */
233 for (i = 0; i < max_choice; i++)
234 if (key < 0x100 && key != ' ' && toupper(key) == toupper(items[(scroll + i) * 2][0]))
237 if (i < max_choice || (key >= '1' && key <= MIN('9', '0'+max_choice)) || KEY_IS_UP(key) || KEY_IS_DOWN(key)) {
238 if (key >= '1' && key <= MIN('9', '0'+max_choice))
240 else if (KEY_IS_UP(key)) {
243 /* Scroll menu down */
244 getyx(dialog, cur_y, cur_x); /* Save cursor position */
245 if (menu_height > 1) {
246 /* De-highlight current first item before scrolling down */
247 print_item(menu, items[scroll * 2], items[scroll * 2 + 1], 0, FALSE, DREF(ditems, scroll), menu_width, tag_x, item_x);
248 scrollok(menu, TRUE);
250 scrollok(menu, FALSE);
253 print_item(menu, items[scroll * 2], items[scroll * 2 + 1], 0, TRUE, DREF(ditems, scroll), menu_width, tag_x, item_x);
255 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
258 continue; /* wait for another key press */
263 else if (KEY_IS_DOWN(key)) {
264 if (choice == max_choice - 1) {
265 if (scroll + choice < item_no - 1) {
267 getyx(dialog, cur_y, cur_x); /* Save cursor position */
268 if (menu_height > 1) {
269 /* De-highlight current last item before scrolling up */
270 print_item(menu, items[(scroll + max_choice - 1) * 2],
271 items[(scroll + max_choice - 1) * 2 + 1],
272 max_choice-1, FALSE, DREF(ditems, scroll + max_choice - 1), menu_width, tag_x, item_x);
273 scrollok(menu, TRUE);
275 scrollok(menu, FALSE);
278 print_item(menu, items[(scroll + max_choice - 1) * 2],
279 items[(scroll + max_choice - 1) * 2 + 1],
280 max_choice - 1, TRUE, DREF(ditems, scroll + max_choice - 1), menu_width, tag_x, item_x);
282 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
285 continue; /* wait for another key press */
292 /* De-highlight current item */
293 getyx(dialog, cur_y, cur_x); /* Save cursor position */
294 print_item(menu, items[(scroll + choice) * 2], items[(scroll + choice) * 2 + 1], choice, FALSE, DREF(ditems, scroll + choice), menu_width, tag_x, item_x);
296 /* Highlight new item */
298 print_item(menu, items[(scroll + choice) * 2], items[(scroll + choice) * 2 + 1], choice, TRUE, DREF(ditems, scroll + choice), menu_width, tag_x, item_x);
300 wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */
303 continue; /* wait for another key press */
308 if (scroll > height - 4) { /* can we go up? */
309 scroll -= (height - 4);
317 if (scroll + menu_height >= item_no-1 - menu_height) { /* can we go down a full page? */
318 scroll = item_no - menu_height;
322 scroll += menu_height;
334 scroll = item_no - menu_height;
337 choice = max_choice - 1;
346 if (ditems && result) {
347 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);
348 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
350 cancel_space = strlen(ditems[OK_BUTTON].prompt) + 6;
353 print_button(dialog, "Cancel", y, x + 14, button);
354 print_button(dialog, " OK ", y, x, !button);
359 wmove(dialog, y, x+cancel_space);
361 wmove(dialog, y, x+ok_space);
369 /* A fire routine can do just about anything to the screen, so be prepared
370 to accept some hints as to what to do in the aftermath. */
372 if (ditems[scroll + choice].fire) {
376 save = dupwin(newscr);
377 status = ditems[scroll + choice].fire(&ditems[scroll + choice]);
378 if (status & DITEM_RESTORE) {
383 if (status & DITEM_CONTINUE)
385 else if (status & DITEM_LEAVE_MENU) {
386 /* Allow a fire action to take us out of the menu */
390 else if (status & DITEM_RECREATE) {
399 strcpy(result, items[(scroll+choice)*2]);
415 /* save info about menu item position */
422 for (i = 0; i < max_choice; i++) {
423 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);
426 getyx(dialog, cur_y, cur_x); /* Save cursor position */
427 print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, tag_x, cur_x, cur_y);
428 wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */
443 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 errx(1, "bad parameter to print_item()\n");
450 /* Clear 'residue' of last item */
451 wattrset(win, menubox_attr);
452 wmove(win, choice, 0);
453 for (i = 0; i < menu_width; i++)
455 wmove(win, choice, tag_x);
456 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
458 wattrset(win, selected ? tag_selected_attr : tag_attr);
459 waddnstr(win, tag + 1, item_x - tag_x - 3);
460 wmove(win, choice, item_x);
461 wattrset(win, selected ? item_selected_attr : item_attr);
462 waddnstr(win, item, menu_width - item_x - 1);
463 /* If have a selection handler for this, call it */
464 if (me && me->selected) {
466 me->selected(me, selected);
469 /* End of print_item() */