2 * menubox.c -- implements the menu box
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcapw@cfw.com)
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.
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.
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.
23 * Changes by Clifford Wolf (god@clifford.at)
27 * *) A bugfix for the Page-Down problem
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
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!
45 * *) Now lxdialog is crash-safe against broken "lxdialog.scrltmp" files
46 * and menus change their size on the fly.
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.
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.
61 #define ITEM_IDENT 4 /* Indent of menu entries. Fixed for all menus */
62 static int menu_width;
67 static void print_item(WINDOW * win, const char *item, int choice,
68 int selected, int hotkey)
71 char *menu_item = malloc(menu_width + 1);
73 strncpy(menu_item, item, menu_width);
74 menu_item[menu_width] = 0;
75 j = first_alpha(menu_item, "YyNnMmHh");
77 /* Clear 'residue' of last item */
78 wattrset(win, menubox_attr);
79 wmove(win, choice, 0);
83 for (i = 0; i < menu_width; i++)
89 wattrset(win, selected ? item_selected_attr : item_attr);
90 mvwaddstr(win, choice, ITEM_IDENT, menu_item);
92 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
93 mvwaddch(win, choice, ITEM_IDENT + j, menu_item[j]);
96 wmove(win, choice, ITEM_IDENT + 1);
103 * Print the scroll indicators.
105 static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
110 getyx(win, cur_y, cur_x);
115 wattrset(win, uarrow_attr);
116 waddch(win, ACS_UARROW);
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);
129 if ((height < item_no) && (scroll + height < item_no)) {
130 wattrset(win, darrow_attr);
131 waddch(win, ACS_DARROW);
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);
141 wmove(win, cur_y, cur_x);
145 * Display the termination buttons.
147 static void print_buttons(WINDOW * win, int height, int width, int selected)
149 int x = width / 2 - 16;
152 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);
156 wmove(win, y, x + 1 + 12 * selected);
161 * Display a menu for choosing among a number of options
163 int 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)
167 int i, j, x, y, box_x, box_y;
168 int key = 0, button = 0, scroll = 0, choice = 0, first_item =
170 WINDOW *dialog, *menu;
173 max_choice = MIN(menu_height, item_no);
175 /* center dialog box on screen */
176 x = (COLS - width) / 2;
177 y = (LINES - height) / 2;
179 draw_shadow(stdscr, y, x, height, width);
181 dialog = newwin(height, width, y, x);
182 keypad(dialog, TRUE);
184 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);
193 print_title(dialog, title, width);
195 wattrset(dialog, dialog_attr);
196 print_autowrap(dialog, prompt, width - 2, 1, 3);
198 menu_width = width - 6;
199 box_y = height - menu_height - 5;
200 box_x = (width - menu_width) / 2 - 1;
202 /* create new window for the menu */
203 menu = subwin(dialog, menu_height, menu_width,
204 y + box_y + 1, x + box_x + 1);
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);
211 /* Set choice to default item */
212 for (i = 0; i < item_no; i++)
213 if (strcmp(current, items[i * 2]) == 0)
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)) {
222 choice = choice - scroll;
226 remove("lxdialog.scrltmp");
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;
235 scroll = first_item = choice - max_choice / 2;
236 choice = choice - scroll;
240 for (i = 0; i < max_choice; i++) {
241 print_item(menu, items[(first_item + i) * 2 + 1], i,
243 (items[(first_item + i) * 2][0] != ':'));
248 print_arrows(dialog, item_no, scroll,
249 box_y, box_x + ITEM_IDENT + 1, menu_height);
251 print_buttons(dialog, height, width, 0);
252 wmove(menu, choice, ITEM_IDENT + 1);
258 if (key < 256 && isalpha(key))
261 if (strchr("ynmh", key))
264 for (i = choice + 1; i < max_choice; i++) {
265 j = first_alpha(items[(scroll + i) * 2 + 1], "YyNnMmHh");
266 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
270 for (i = 0; i < max_choice; i++) {
271 j = first_alpha(items [(scroll + i) * 2 + 1], "YyNnMmHh");
272 if (key == tolower(items[(scroll + i) * 2 + 1][j]))
277 if (i < max_choice ||
278 key == KEY_UP || key == KEY_DOWN ||
279 key == '-' || key == '+' ||
280 key == KEY_PPAGE || key == KEY_NPAGE) {
281 /* Remove highligt of current item */
282 print_item(menu, items[(scroll + choice) * 2 + 1],
284 (items[(scroll + choice) * 2][0] != ':'));
286 if (key == KEY_UP || key == '-') {
287 if (choice < 2 && scroll) {
288 /* Scroll menu down */
289 scrollok(menu, TRUE);
291 scrollok(menu, FALSE);
295 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
296 (items[scroll * 2][0] != ':'));
298 choice = MAX(choice - 1, 0);
300 } else if (key == KEY_DOWN || key == '+') {
303 items[(scroll + choice) * 2 + 1], choice, FALSE,
304 (items[(scroll + choice) * 2][0] != ':'));
306 if ((choice > max_choice - 3) &&
307 (scroll + max_choice < item_no)) {
309 scrollok(menu, TRUE);
311 scrollok(menu, FALSE);
315 print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
316 max_choice - 1, FALSE,
317 (items [(scroll + max_choice - 1) * 2][0] != ':'));
319 choice = MIN(choice + 1, max_choice - 1);
321 } else if (key == KEY_PPAGE) {
322 scrollok(menu, TRUE);
323 for (i = 0; (i < max_choice); i++) {
327 print_item(menu, items[scroll * 2 + 1], 0, FALSE,
328 (items[scroll * 2][0] != ':'));
334 scrollok(menu, FALSE);
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);
341 scrollok(menu, FALSE);
343 print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
344 max_choice - 1, FALSE,
345 (items [(scroll + max_choice - 1) * 2][0] != ':'));
347 if (choice + 1 < max_choice)
355 print_item(menu, items[(scroll + choice) * 2 + 1],
356 choice, TRUE, (items[(scroll + choice) * 2][0] != ':'));
358 print_arrows(dialog, item_no, scroll,
359 box_y, box_x + ITEM_IDENT + 1, menu_height);
361 wnoutrefresh(dialog);
364 continue; /* wait for another key press */
371 button = ((key == KEY_LEFT ? --button : ++button) < 0)
372 ? 2 : (button > 2 ? 0 : button);
374 print_buttons(dialog, height, width, button);
383 /* save scroll info */
384 if ((f = fopen("lxdialog.scrltmp", "w")) != NULL) {
385 fprintf(f, "%d\n", scroll);
389 fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
411 fprintf(stderr, "%s \"%s\"\n",
412 items[(scroll + choice) * 2],
413 items[(scroll + choice) * 2 + 1] +
414 first_alpha(items [(scroll + choice) * 2 + 1], ""));
416 fprintf(stderr, "%s\n",
417 items[(scroll + choice) * 2]);
419 remove("lxdialog.scrltmp");
430 remove("lxdialog.scrltmp");
431 return -1; /* ESC pressed */