blob: ebfe6a3c8cccc3d9155f5c1a7a3acb6f87a69ad7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * menubox.c -- implements the menu box
3 *
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcapw@cfw.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/*
23 * Changes by Clifford Wolf (god@clifford.at)
24 *
25 * [ 1998-06-13 ]
26 *
27 * *) A bugfix for the Page-Down problem
28 *
29 * *) Formerly when I used Page Down and Page Up, the cursor would be set
30 * to the first position in the menu box. Now lxdialog is a bit
31 * smarter and works more like other menu systems (just have a look at
32 * it).
33 *
34 * *) Formerly if I selected something my scrolling would be broken because
35 * lxdialog is re-invoked by the Menuconfig shell script, can't
36 * remember the last scrolling position, and just sets it so that the
37 * cursor is at the bottom of the box. Now it writes the temporary file
38 * lxdialog.scrltmp which contains this information. The file is
39 * deleted by lxdialog if the user leaves a submenu or enters a new
40 * one, but it would be nice if Menuconfig could make another "rm -f"
41 * just to be sure. Just try it out - you will recognise a difference!
42 *
43 * [ 1998-06-14 ]
44 *
45 * *) Now lxdialog is crash-safe against broken "lxdialog.scrltmp" files
46 * and menus change their size on the fly.
47 *
48 * *) If for some reason the last scrolling position is not saved by
49 * lxdialog, it sets the scrolling so that the selected item is in the
50 * middle of the menu box, not at the bottom.
51 *
52 * 02 January 1999, Michael Elizabeth Chastain (mec@shout.net)
53 * Reset 'scroll' to 0 if the value from lxdialog.scrltmp is bogus.
54 * This fixes a bug in Menuconfig where using ' ' to descend into menus
55 * would leave mis-synchronized lxdialog.scrltmp files lying around,
56 * fscanf would read in 'scroll', and eventually that value would get used.
57 */
58
59#include "dialog.h"
60
61static int menu_width, item_x;
62
63/*
64 * Print menu item
65 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +010066static void print_item(WINDOW * win, const char *item, int choice,
67 int selected, int hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010069 int j;
Sam Ravnborga06104a2005-11-19 22:17:55 +010070 char *menu_item = malloc(menu_width + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010072 strncpy(menu_item, item, menu_width);
73 menu_item[menu_width] = 0;
74 j = first_alpha(menu_item, "YyNnMmHh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010076 /* Clear 'residue' of last item */
77 wattrset(win, menubox_attr);
78 wmove(win, choice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#if OLD_NCURSES
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010080 {
81 int i;
82 for (i = 0; i < menu_width; i++)
83 waddch(win, ' ');
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#else
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010086 wclrtoeol(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#endif
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010088 wattrset(win, selected ? item_selected_attr : item_attr);
89 mvwaddstr(win, choice, item_x, menu_item);
90 if (hotkey) {
91 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
92 mvwaddch(win, choice, item_x + j, menu_item[j]);
93 }
94 if (selected) {
95 wmove(win, choice, item_x + 1);
96 wrefresh(win);
97 }
Sam Ravnborga06104a2005-11-19 22:17:55 +010098 free(menu_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101/*
102 * Print the scroll indicators.
103 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100104static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
105 int height)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100107 int cur_y, cur_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100109 getyx(win, cur_y, cur_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100111 wmove(win, y, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100113 if (scroll > 0) {
114 wattrset(win, uarrow_attr);
115 waddch(win, ACS_UARROW);
116 waddstr(win, "(-)");
117 } else {
118 wattrset(win, menubox_attr);
119 waddch(win, ACS_HLINE);
120 waddch(win, ACS_HLINE);
121 waddch(win, ACS_HLINE);
122 waddch(win, ACS_HLINE);
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100125 y = y + height + 1;
126 wmove(win, y, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100128 if ((height < item_no) && (scroll + height < item_no)) {
129 wattrset(win, darrow_attr);
130 waddch(win, ACS_DARROW);
131 waddstr(win, "(+)");
132 } else {
133 wattrset(win, menubox_border_attr);
134 waddch(win, ACS_HLINE);
135 waddch(win, ACS_HLINE);
136 waddch(win, ACS_HLINE);
137 waddch(win, ACS_HLINE);
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100140 wmove(win, cur_y, cur_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/*
144 * Display the termination buttons.
145 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100146static void print_buttons(WINDOW * win, int height, int width, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100148 int x = width / 2 - 16;
149 int y = height - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100151 print_button(win, "Select", y, x, selected == 0);
152 print_button(win, " Exit ", y, x + 12, selected == 1);
153 print_button(win, " Help ", y, x + 24, selected == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100155 wmove(win, y, x + 1 + 12 * selected);
156 wrefresh(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/*
160 * Display a menu for choosing among a number of options
161 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100162int dialog_menu(const char *title, const char *prompt, int height, int width,
163 int menu_height, const char *current, int item_no,
164 const char *const *items)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100166 int i, j, x, y, box_x, box_y;
167 int key = 0, button = 0, scroll = 0, choice = 0, first_item =
168 0, max_choice;
169 WINDOW *dialog, *menu;
170 FILE *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100172 max_choice = MIN(menu_height, item_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100174 /* center dialog box on screen */
175 x = (COLS - width) / 2;
176 y = (LINES - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100178 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100180 dialog = newwin(height, width, y, x);
181 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100183 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
184 wattrset(dialog, border_attr);
185 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
186 for (i = 0; i < width - 2; i++)
187 waddch(dialog, ACS_HLINE);
188 wattrset(dialog, dialog_attr);
189 wbkgdset(dialog, dialog_attr & A_COLOR);
190 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Sam Ravnborgfa7009d2005-11-19 23:38:06 +0100192 print_title(dialog, title, width);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100193
194 wattrset(dialog, dialog_attr);
195 print_autowrap(dialog, prompt, width - 2, 1, 3);
196
197 menu_width = width - 6;
198 box_y = height - menu_height - 5;
199 box_x = (width - menu_width) / 2 - 1;
200
201 /* create new window for the menu */
202 menu = subwin(dialog, menu_height, menu_width,
203 y + box_y + 1, x + box_x + 1);
204 keypad(menu, TRUE);
205
206 /* draw a box around the menu items */
207 draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
208 menubox_border_attr, menubox_attr);
209
210 /*
211 * Find length of longest item in order to center menu.
Sam Ravnborga06104a2005-11-19 22:17:55 +0100212 * Set 'choice' to default item.
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100213 */
214 item_x = 0;
215 for (i = 0; i < item_no; i++) {
216 item_x =
217 MAX(item_x, MIN(menu_width, strlen(items[i * 2 + 1]) + 2));
218 if (strcmp(current, items[i * 2]) == 0)
219 choice = i;
220 }
221
222 item_x = (menu_width - item_x) / 2;
223
224 /* get the scroll info from the temp file */
225 if ((f = fopen("lxdialog.scrltmp", "r")) != NULL) {
226 if ((fscanf(f, "%d\n", &scroll) == 1) && (scroll <= choice) &&
227 (scroll + max_choice > choice) && (scroll >= 0) &&
228 (scroll + max_choice <= item_no)) {
229 first_item = scroll;
230 choice = choice - scroll;
231 fclose(f);
232 } else {
233 scroll = 0;
234 remove("lxdialog.scrltmp");
235 fclose(f);
236 f = NULL;
237 }
238 }
239 if ((choice >= max_choice) || (f == NULL && choice >= max_choice / 2)) {
240 if (choice >= item_no - max_choice / 2)
241 scroll = first_item = item_no - max_choice;
242 else
243 scroll = first_item = choice - max_choice / 2;
244 choice = choice - scroll;
245 }
246
247 /* Print the menu */
248 for (i = 0; i < max_choice; i++) {
249 print_item(menu, items[(first_item + i) * 2 + 1], i,
250 i == choice,
251 (items[(first_item + i) * 2][0] != ':'));
252 }
253
254 wnoutrefresh(menu);
255
256 print_arrows(dialog, item_no, scroll,
257 box_y, box_x + item_x + 1, menu_height);
258
259 print_buttons(dialog, height, width, 0);
260 wmove(menu, choice, item_x + 1);
261 wrefresh(menu);
262
263 while (key != ESC) {
264 key = wgetch(menu);
265
266 if (key < 256 && isalpha(key))
267 key = tolower(key);
268
269 if (strchr("ynmh", key))
270 i = max_choice;
271 else {
272 for (i = choice + 1; i < max_choice; i++) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100273 j = first_alpha(items[(scroll + i) * 2 + 1], "YyNnMmHh");
274 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100275 break;
276 }
277 if (i == max_choice)
278 for (i = 0; i < max_choice; i++) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100279 j = first_alpha(items [(scroll + i) * 2 + 1], "YyNnMmHh");
280 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100281 break;
282 }
283 }
284
285 if (i < max_choice ||
286 key == KEY_UP || key == KEY_DOWN ||
287 key == '-' || key == '+' ||
288 key == KEY_PPAGE || key == KEY_NPAGE) {
289
290 print_item(menu, items[(scroll + choice) * 2 + 1],
291 choice, FALSE,
292 (items[(scroll + choice) * 2][0] != ':'));
293
294 if (key == KEY_UP || key == '-') {
295 if (choice < 2 && scroll) {
296 /* Scroll menu down */
297 scrollok(menu, TRUE);
298 wscrl(menu, -1);
299 scrollok(menu, FALSE);
300
301 scroll--;
302
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100303 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
304 (items[scroll * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100305 } else
306 choice = MAX(choice - 1, 0);
307
308 } else if (key == KEY_DOWN || key == '+') {
309
310 print_item(menu,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100311 items[(scroll + choice) * 2 + 1], choice, FALSE,
312 (items[(scroll + choice) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100313
314 if ((choice > max_choice - 3) &&
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100315 (scroll + max_choice < item_no)) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100316 /* Scroll menu up */
317 scrollok(menu, TRUE);
318 wscrl(menu, 1);
319 scrollok(menu, FALSE);
320
321 scroll++;
322
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100323 print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100324 max_choice - 1, FALSE,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100325 (items [(scroll + max_choice - 1) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100326 } else
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100327 choice = MIN(choice + 1, max_choice - 1);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100328
329 } else if (key == KEY_PPAGE) {
330 scrollok(menu, TRUE);
331 for (i = 0; (i < max_choice); i++) {
332 if (scroll > 0) {
333 wscrl(menu, -1);
334 scroll--;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100335 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
336 (items[scroll * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100337 } else {
338 if (choice > 0)
339 choice--;
340 }
341 }
342 scrollok(menu, FALSE);
343
344 } else if (key == KEY_NPAGE) {
345 for (i = 0; (i < max_choice); i++) {
346 if (scroll + max_choice < item_no) {
347 scrollok(menu, TRUE);
348 wscrl(menu, 1);
349 scrollok(menu, FALSE);
350 scroll++;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100351 print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
352 max_choice - 1, FALSE,
353 (items [(scroll + max_choice - 1) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100354 } else {
355 if (choice + 1 < max_choice)
356 choice++;
357 }
358 }
359
360 } else
361 choice = i;
362
363 print_item(menu, items[(scroll + choice) * 2 + 1],
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100364 choice, TRUE, (items[(scroll + choice) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100365
366 print_arrows(dialog, item_no, scroll,
367 box_y, box_x + item_x + 1, menu_height);
368
369 wnoutrefresh(dialog);
370 wrefresh(menu);
371
372 continue; /* wait for another key press */
373 }
374
375 switch (key) {
376 case KEY_LEFT:
377 case TAB:
378 case KEY_RIGHT:
379 button = ((key == KEY_LEFT ? --button : ++button) < 0)
380 ? 2 : (button > 2 ? 0 : button);
381
382 print_buttons(dialog, height, width, button);
383 wrefresh(menu);
384 break;
385 case ' ':
386 case 's':
387 case 'y':
388 case 'n':
389 case 'm':
390 case '/':
391 /* save scroll info */
392 if ((f = fopen("lxdialog.scrltmp", "w")) != NULL) {
393 fprintf(f, "%d\n", scroll);
394 fclose(f);
395 }
396 delwin(dialog);
397 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
398 switch (key) {
399 case 's':
400 return 3;
401 case 'y':
402 return 3;
403 case 'n':
404 return 4;
405 case 'm':
406 return 5;
407 case ' ':
408 return 6;
409 case '/':
410 return 7;
411 }
412 return 0;
413 case 'h':
414 case '?':
415 button = 2;
416 case '\n':
417 delwin(dialog);
418 if (button == 2)
419 fprintf(stderr, "%s \"%s\"\n",
420 items[(scroll + choice) * 2],
421 items[(scroll + choice) * 2 + 1] +
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100422 first_alpha(items [(scroll + choice) * 2 + 1], ""));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100423 else
424 fprintf(stderr, "%s\n",
425 items[(scroll + choice) * 2]);
426
427 remove("lxdialog.scrltmp");
428 return button;
429 case 'e':
430 case 'x':
431 key = ESC;
432 case ESC:
433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 }
436
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100437 delwin(dialog);
438 remove("lxdialog.scrltmp");
439 return -1; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}