blob: 260cc4dd5dabd555a42746a1b9766ba4bf2d9248 [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;
70 char menu_item[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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100/*
101 * Print the scroll indicators.
102 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100103static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
104 int height)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100106 int cur_y, cur_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100108 getyx(win, cur_y, cur_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100110 wmove(win, y, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100112 if (scroll > 0) {
113 wattrset(win, uarrow_attr);
114 waddch(win, ACS_UARROW);
115 waddstr(win, "(-)");
116 } else {
117 wattrset(win, menubox_attr);
118 waddch(win, ACS_HLINE);
119 waddch(win, ACS_HLINE);
120 waddch(win, ACS_HLINE);
121 waddch(win, ACS_HLINE);
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100124 y = y + height + 1;
125 wmove(win, y, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100127 if ((height < item_no) && (scroll + height < item_no)) {
128 wattrset(win, darrow_attr);
129 waddch(win, ACS_DARROW);
130 waddstr(win, "(+)");
131 } else {
132 wattrset(win, menubox_border_attr);
133 waddch(win, ACS_HLINE);
134 waddch(win, ACS_HLINE);
135 waddch(win, ACS_HLINE);
136 waddch(win, ACS_HLINE);
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100139 wmove(win, cur_y, cur_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/*
143 * Display the termination buttons.
144 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100145static void print_buttons(WINDOW * win, int height, int width, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100147 int x = width / 2 - 16;
148 int y = height - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100150 print_button(win, "Select", y, x, selected == 0);
151 print_button(win, " Exit ", y, x + 12, selected == 1);
152 print_button(win, " Help ", y, x + 24, selected == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100154 wmove(win, y, x + 1 + 12 * selected);
155 wrefresh(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*
159 * Display a menu for choosing among a number of options
160 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100161int dialog_menu(const char *title, const char *prompt, int height, int width,
162 int menu_height, const char *current, int item_no,
163 const char *const *items)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100165 int i, j, x, y, box_x, box_y;
166 int key = 0, button = 0, scroll = 0, choice = 0, first_item =
167 0, max_choice;
168 WINDOW *dialog, *menu;
169 FILE *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100171 max_choice = MIN(menu_height, item_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100173 /* center dialog box on screen */
174 x = (COLS - width) / 2;
175 y = (LINES - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100177 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100179 dialog = newwin(height, width, y, x);
180 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100182 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
183 wattrset(dialog, border_attr);
184 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
185 for (i = 0; i < width - 2; i++)
186 waddch(dialog, ACS_HLINE);
187 wattrset(dialog, dialog_attr);
188 wbkgdset(dialog, dialog_attr & A_COLOR);
189 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100191 if (title != NULL && strlen(title) >= width - 2) {
192 /* truncate long title -- mec */
193 char *title2 = malloc(width - 2 + 1);
194 memcpy(title2, title, width - 2);
195 title2[width - 2] = '\0';
196 title = title2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100199 if (title != NULL) {
200 wattrset(dialog, title_attr);
201 mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' ');
202 waddstr(dialog, (char *)title);
203 waddch(dialog, ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100205
206 wattrset(dialog, dialog_attr);
207 print_autowrap(dialog, prompt, width - 2, 1, 3);
208
209 menu_width = width - 6;
210 box_y = height - menu_height - 5;
211 box_x = (width - menu_width) / 2 - 1;
212
213 /* create new window for the menu */
214 menu = subwin(dialog, menu_height, menu_width,
215 y + box_y + 1, x + box_x + 1);
216 keypad(menu, TRUE);
217
218 /* draw a box around the menu items */
219 draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
220 menubox_border_attr, menubox_attr);
221
222 /*
223 * Find length of longest item in order to center menu.
224 * Set 'choice' to default item.
225 */
226 item_x = 0;
227 for (i = 0; i < item_no; i++) {
228 item_x =
229 MAX(item_x, MIN(menu_width, strlen(items[i * 2 + 1]) + 2));
230 if (strcmp(current, items[i * 2]) == 0)
231 choice = i;
232 }
233
234 item_x = (menu_width - item_x) / 2;
235
236 /* get the scroll info from the temp file */
237 if ((f = fopen("lxdialog.scrltmp", "r")) != NULL) {
238 if ((fscanf(f, "%d\n", &scroll) == 1) && (scroll <= choice) &&
239 (scroll + max_choice > choice) && (scroll >= 0) &&
240 (scroll + max_choice <= item_no)) {
241 first_item = scroll;
242 choice = choice - scroll;
243 fclose(f);
244 } else {
245 scroll = 0;
246 remove("lxdialog.scrltmp");
247 fclose(f);
248 f = NULL;
249 }
250 }
251 if ((choice >= max_choice) || (f == NULL && choice >= max_choice / 2)) {
252 if (choice >= item_no - max_choice / 2)
253 scroll = first_item = item_no - max_choice;
254 else
255 scroll = first_item = choice - max_choice / 2;
256 choice = choice - scroll;
257 }
258
259 /* Print the menu */
260 for (i = 0; i < max_choice; i++) {
261 print_item(menu, items[(first_item + i) * 2 + 1], i,
262 i == choice,
263 (items[(first_item + i) * 2][0] != ':'));
264 }
265
266 wnoutrefresh(menu);
267
268 print_arrows(dialog, item_no, scroll,
269 box_y, box_x + item_x + 1, menu_height);
270
271 print_buttons(dialog, height, width, 0);
272 wmove(menu, choice, item_x + 1);
273 wrefresh(menu);
274
275 while (key != ESC) {
276 key = wgetch(menu);
277
278 if (key < 256 && isalpha(key))
279 key = tolower(key);
280
281 if (strchr("ynmh", key))
282 i = max_choice;
283 else {
284 for (i = choice + 1; i < max_choice; i++) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100285 j = first_alpha(items[(scroll + i) * 2 + 1], "YyNnMmHh");
286 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100287 break;
288 }
289 if (i == max_choice)
290 for (i = 0; i < max_choice; i++) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100291 j = first_alpha(items [(scroll + i) * 2 + 1], "YyNnMmHh");
292 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100293 break;
294 }
295 }
296
297 if (i < max_choice ||
298 key == KEY_UP || key == KEY_DOWN ||
299 key == '-' || key == '+' ||
300 key == KEY_PPAGE || key == KEY_NPAGE) {
301
302 print_item(menu, items[(scroll + choice) * 2 + 1],
303 choice, FALSE,
304 (items[(scroll + choice) * 2][0] != ':'));
305
306 if (key == KEY_UP || key == '-') {
307 if (choice < 2 && scroll) {
308 /* Scroll menu down */
309 scrollok(menu, TRUE);
310 wscrl(menu, -1);
311 scrollok(menu, FALSE);
312
313 scroll--;
314
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100315 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
316 (items[scroll * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100317 } else
318 choice = MAX(choice - 1, 0);
319
320 } else if (key == KEY_DOWN || key == '+') {
321
322 print_item(menu,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100323 items[(scroll + choice) * 2 + 1], choice, FALSE,
324 (items[(scroll + choice) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100325
326 if ((choice > max_choice - 3) &&
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100327 (scroll + max_choice < item_no)) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100328 /* Scroll menu up */
329 scrollok(menu, TRUE);
330 wscrl(menu, 1);
331 scrollok(menu, FALSE);
332
333 scroll++;
334
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100335 print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100336 max_choice - 1, FALSE,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100337 (items [(scroll + max_choice - 1) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100338 } else
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100339 choice = MIN(choice + 1, max_choice - 1);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100340
341 } else if (key == KEY_PPAGE) {
342 scrollok(menu, TRUE);
343 for (i = 0; (i < max_choice); i++) {
344 if (scroll > 0) {
345 wscrl(menu, -1);
346 scroll--;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100347 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
348 (items[scroll * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100349 } else {
350 if (choice > 0)
351 choice--;
352 }
353 }
354 scrollok(menu, FALSE);
355
356 } else if (key == KEY_NPAGE) {
357 for (i = 0; (i < max_choice); i++) {
358 if (scroll + max_choice < item_no) {
359 scrollok(menu, TRUE);
360 wscrl(menu, 1);
361 scrollok(menu, FALSE);
362 scroll++;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100363 print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
364 max_choice - 1, FALSE,
365 (items [(scroll + max_choice - 1) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100366 } else {
367 if (choice + 1 < max_choice)
368 choice++;
369 }
370 }
371
372 } else
373 choice = i;
374
375 print_item(menu, items[(scroll + choice) * 2 + 1],
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100376 choice, TRUE, (items[(scroll + choice) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100377
378 print_arrows(dialog, item_no, scroll,
379 box_y, box_x + item_x + 1, menu_height);
380
381 wnoutrefresh(dialog);
382 wrefresh(menu);
383
384 continue; /* wait for another key press */
385 }
386
387 switch (key) {
388 case KEY_LEFT:
389 case TAB:
390 case KEY_RIGHT:
391 button = ((key == KEY_LEFT ? --button : ++button) < 0)
392 ? 2 : (button > 2 ? 0 : button);
393
394 print_buttons(dialog, height, width, button);
395 wrefresh(menu);
396 break;
397 case ' ':
398 case 's':
399 case 'y':
400 case 'n':
401 case 'm':
402 case '/':
403 /* save scroll info */
404 if ((f = fopen("lxdialog.scrltmp", "w")) != NULL) {
405 fprintf(f, "%d\n", scroll);
406 fclose(f);
407 }
408 delwin(dialog);
409 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
410 switch (key) {
411 case 's':
412 return 3;
413 case 'y':
414 return 3;
415 case 'n':
416 return 4;
417 case 'm':
418 return 5;
419 case ' ':
420 return 6;
421 case '/':
422 return 7;
423 }
424 return 0;
425 case 'h':
426 case '?':
427 button = 2;
428 case '\n':
429 delwin(dialog);
430 if (button == 2)
431 fprintf(stderr, "%s \"%s\"\n",
432 items[(scroll + choice) * 2],
433 items[(scroll + choice) * 2 + 1] +
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100434 first_alpha(items [(scroll + choice) * 2 + 1], ""));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100435 else
436 fprintf(stderr, "%s\n",
437 items[(scroll + choice) * 2]);
438
439 remove("lxdialog.scrltmp");
440 return button;
441 case 'e':
442 case 'x':
443 key = ESC;
444 case ESC:
445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 }
448
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100449 delwin(dialog);
450 remove("lxdialog.scrltmp");
451 return -1; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}