blob: 89fcf418a07a4262f7530f07ab46bae3fe957bd0 [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
Sam Ravnborg0e175d02005-11-20 22:41:21 +010061#define ITEM_IDENT 4 /* Indent of menu entries. Fixed for all menus */
62static int menu_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Print menu item
66 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +010067static void print_item(WINDOW * win, const char *item, int choice,
68 int selected, int hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010070 int j;
Sam Ravnborga06104a2005-11-19 22:17:55 +010071 char *menu_item = malloc(menu_width + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010073 strncpy(menu_item, item, menu_width);
74 menu_item[menu_width] = 0;
75 j = first_alpha(menu_item, "YyNnMmHh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010077 /* Clear 'residue' of last item */
78 wattrset(win, menubox_attr);
79 wmove(win, choice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#if OLD_NCURSES
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010081 {
82 int i;
83 for (i = 0; i < menu_width; i++)
84 waddch(win, ' ');
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#else
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010087 wclrtoeol(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010089 wattrset(win, selected ? item_selected_attr : item_attr);
Sam Ravnborg0e175d02005-11-20 22:41:21 +010090 mvwaddstr(win, choice, ITEM_IDENT, menu_item);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010091 if (hotkey) {
92 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
Sam Ravnborg0e175d02005-11-20 22:41:21 +010093 mvwaddch(win, choice, ITEM_IDENT + j, menu_item[j]);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010094 }
95 if (selected) {
Sam Ravnborg0e175d02005-11-20 22:41:21 +010096 wmove(win, choice, ITEM_IDENT + 1);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010097 wrefresh(win);
98 }
Sam Ravnborga06104a2005-11-19 22:17:55 +010099 free(menu_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102/*
103 * Print the scroll indicators.
104 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100105static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
106 int height)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100108 int cur_y, cur_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100110 getyx(win, cur_y, cur_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100112 wmove(win, y, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100114 if (scroll > 0) {
115 wattrset(win, uarrow_attr);
116 waddch(win, ACS_UARROW);
117 waddstr(win, "(-)");
118 } else {
119 wattrset(win, menubox_attr);
120 waddch(win, ACS_HLINE);
121 waddch(win, ACS_HLINE);
122 waddch(win, ACS_HLINE);
123 waddch(win, ACS_HLINE);
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100126 y = y + height + 1;
127 wmove(win, y, x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100129 if ((height < item_no) && (scroll + height < item_no)) {
130 wattrset(win, darrow_attr);
131 waddch(win, ACS_DARROW);
132 waddstr(win, "(+)");
133 } else {
134 wattrset(win, menubox_border_attr);
135 waddch(win, ACS_HLINE);
136 waddch(win, ACS_HLINE);
137 waddch(win, ACS_HLINE);
138 waddch(win, ACS_HLINE);
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100141 wmove(win, cur_y, cur_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144/*
145 * Display the termination buttons.
146 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100147static void print_buttons(WINDOW * win, int height, int width, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100149 int x = width / 2 - 16;
150 int y = height - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100152 print_button(win, "Select", y, x, selected == 0);
153 print_button(win, " Exit ", y, x + 12, selected == 1);
154 print_button(win, " Help ", y, x + 24, selected == 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100156 wmove(win, y, x + 1 + 12 * selected);
157 wrefresh(win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/*
161 * Display a menu for choosing among a number of options
162 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100163int dialog_menu(const char *title, const char *prompt, int height, int width,
164 int menu_height, const char *current, int item_no,
165 const char *const *items)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100167 int i, j, x, y, box_x, box_y;
168 int key = 0, button = 0, scroll = 0, choice = 0, first_item =
169 0, max_choice;
170 WINDOW *dialog, *menu;
171 FILE *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100173 max_choice = MIN(menu_height, item_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100175 /* center dialog box on screen */
176 x = (COLS - width) / 2;
177 y = (LINES - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100179 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100181 dialog = newwin(height, width, y, x);
182 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100184 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
185 wattrset(dialog, border_attr);
186 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
187 for (i = 0; i < width - 2; i++)
188 waddch(dialog, ACS_HLINE);
189 wattrset(dialog, dialog_attr);
190 wbkgdset(dialog, dialog_attr & A_COLOR);
191 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Sam Ravnborgfa7009d2005-11-19 23:38:06 +0100193 print_title(dialog, title, width);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100194
195 wattrset(dialog, dialog_attr);
196 print_autowrap(dialog, prompt, width - 2, 1, 3);
197
198 menu_width = width - 6;
199 box_y = height - menu_height - 5;
200 box_x = (width - menu_width) / 2 - 1;
201
202 /* create new window for the menu */
203 menu = subwin(dialog, menu_height, menu_width,
204 y + box_y + 1, x + box_x + 1);
205 keypad(menu, TRUE);
206
207 /* draw a box around the menu items */
208 draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
209 menubox_border_attr, menubox_attr);
210
Sam Ravnborg0e175d02005-11-20 22:41:21 +0100211 /* Set choice to default item */
212 for (i = 0; i < item_no; i++)
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100213 if (strcmp(current, items[i * 2]) == 0)
214 choice = i;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100215
216 /* get the scroll info from the temp file */
217 if ((f = fopen("lxdialog.scrltmp", "r")) != NULL) {
218 if ((fscanf(f, "%d\n", &scroll) == 1) && (scroll <= choice) &&
219 (scroll + max_choice > choice) && (scroll >= 0) &&
220 (scroll + max_choice <= item_no)) {
221 first_item = scroll;
222 choice = choice - scroll;
223 fclose(f);
224 } else {
225 scroll = 0;
226 remove("lxdialog.scrltmp");
227 fclose(f);
228 f = NULL;
229 }
230 }
231 if ((choice >= max_choice) || (f == NULL && choice >= max_choice / 2)) {
232 if (choice >= item_no - max_choice / 2)
233 scroll = first_item = item_no - max_choice;
234 else
235 scroll = first_item = choice - max_choice / 2;
236 choice = choice - scroll;
237 }
238
239 /* Print the menu */
240 for (i = 0; i < max_choice; i++) {
241 print_item(menu, items[(first_item + i) * 2 + 1], i,
242 i == choice,
243 (items[(first_item + i) * 2][0] != ':'));
244 }
245
246 wnoutrefresh(menu);
247
248 print_arrows(dialog, item_no, scroll,
Sam Ravnborg0e175d02005-11-20 22:41:21 +0100249 box_y, box_x + ITEM_IDENT + 1, menu_height);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100250
251 print_buttons(dialog, height, width, 0);
Sam Ravnborg0e175d02005-11-20 22:41:21 +0100252 wmove(menu, choice, ITEM_IDENT + 1);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100253 wrefresh(menu);
254
255 while (key != ESC) {
256 key = wgetch(menu);
257
258 if (key < 256 && isalpha(key))
259 key = tolower(key);
260
261 if (strchr("ynmh", key))
262 i = max_choice;
263 else {
264 for (i = choice + 1; i < max_choice; i++) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100265 j = first_alpha(items[(scroll + i) * 2 + 1], "YyNnMmHh");
266 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100267 break;
268 }
269 if (i == max_choice)
270 for (i = 0; i < max_choice; i++) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100271 j = first_alpha(items [(scroll + i) * 2 + 1], "YyNnMmHh");
272 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100273 break;
274 }
275 }
276
277 if (i < max_choice ||
278 key == KEY_UP || key == KEY_DOWN ||
279 key == '-' || key == '+' ||
280 key == KEY_PPAGE || key == KEY_NPAGE) {
Sam Ravnborg0e175d02005-11-20 22:41:21 +0100281 /* Remove highligt of current item */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100282 print_item(menu, items[(scroll + choice) * 2 + 1],
283 choice, FALSE,
284 (items[(scroll + choice) * 2][0] != ':'));
285
286 if (key == KEY_UP || key == '-') {
287 if (choice < 2 && scroll) {
288 /* Scroll menu down */
289 scrollok(menu, TRUE);
290 wscrl(menu, -1);
291 scrollok(menu, FALSE);
292
293 scroll--;
294
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100295 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
296 (items[scroll * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100297 } else
298 choice = MAX(choice - 1, 0);
299
300 } else if (key == KEY_DOWN || key == '+') {
301
302 print_item(menu,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100303 items[(scroll + choice) * 2 + 1], choice, FALSE,
304 (items[(scroll + choice) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100305
306 if ((choice > max_choice - 3) &&
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100307 (scroll + max_choice < item_no)) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100308 /* Scroll menu up */
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 + max_choice - 1) * 2 + 1],
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100316 max_choice - 1, FALSE,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100317 (items [(scroll + max_choice - 1) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100318 } else
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100319 choice = MIN(choice + 1, max_choice - 1);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100320
321 } else if (key == KEY_PPAGE) {
322 scrollok(menu, TRUE);
323 for (i = 0; (i < max_choice); i++) {
324 if (scroll > 0) {
325 wscrl(menu, -1);
326 scroll--;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100327 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
328 (items[scroll * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100329 } else {
330 if (choice > 0)
331 choice--;
332 }
333 }
334 scrollok(menu, FALSE);
335
336 } else if (key == KEY_NPAGE) {
337 for (i = 0; (i < max_choice); i++) {
338 if (scroll + max_choice < item_no) {
339 scrollok(menu, TRUE);
340 wscrl(menu, 1);
341 scrollok(menu, FALSE);
342 scroll++;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100343 print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
344 max_choice - 1, FALSE,
345 (items [(scroll + max_choice - 1) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100346 } else {
347 if (choice + 1 < max_choice)
348 choice++;
349 }
350 }
351
352 } else
353 choice = i;
354
355 print_item(menu, items[(scroll + choice) * 2 + 1],
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100356 choice, TRUE, (items[(scroll + choice) * 2][0] != ':'));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100357
358 print_arrows(dialog, item_no, scroll,
Sam Ravnborg0e175d02005-11-20 22:41:21 +0100359 box_y, box_x + ITEM_IDENT + 1, menu_height);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100360
361 wnoutrefresh(dialog);
362 wrefresh(menu);
363
364 continue; /* wait for another key press */
365 }
366
367 switch (key) {
368 case KEY_LEFT:
369 case TAB:
370 case KEY_RIGHT:
371 button = ((key == KEY_LEFT ? --button : ++button) < 0)
372 ? 2 : (button > 2 ? 0 : button);
373
374 print_buttons(dialog, height, width, button);
375 wrefresh(menu);
376 break;
377 case ' ':
378 case 's':
379 case 'y':
380 case 'n':
381 case 'm':
382 case '/':
383 /* save scroll info */
384 if ((f = fopen("lxdialog.scrltmp", "w")) != NULL) {
385 fprintf(f, "%d\n", scroll);
386 fclose(f);
387 }
388 delwin(dialog);
389 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
390 switch (key) {
391 case 's':
392 return 3;
393 case 'y':
394 return 3;
395 case 'n':
396 return 4;
397 case 'm':
398 return 5;
399 case ' ':
400 return 6;
401 case '/':
402 return 7;
403 }
404 return 0;
405 case 'h':
406 case '?':
407 button = 2;
408 case '\n':
409 delwin(dialog);
410 if (button == 2)
411 fprintf(stderr, "%s \"%s\"\n",
412 items[(scroll + choice) * 2],
413 items[(scroll + choice) * 2 + 1] +
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100414 first_alpha(items [(scroll + choice) * 2 + 1], ""));
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100415 else
416 fprintf(stderr, "%s\n",
417 items[(scroll + choice) * 2]);
418
419 remove("lxdialog.scrltmp");
420 return button;
421 case 'e':
422 case 'x':
423 key = ESC;
424 case ESC:
425 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
428
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100429 delwin(dialog);
430 remove("lxdialog.scrltmp");
431 return -1; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}