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, see <http://www.gnu.org/licenses/>.
22 * Changes by Clifford Wolf (god@clifford.at)
26 * *) A bugfix for the Page-Down problem
28 * *) Formerly when I used Page Down and Page Up, the cursor would be set
29 * to the first position in the menu box. Now lxdialog is a bit
30 * smarter and works more like other menu systems (just have a look at
33 * *) Formerly if I selected something my scrolling would be broken because
34 * lxdialog is re-invoked by the Menuconfig shell script, can't
35 * remember the last scrolling position, and just sets it so that the
36 * cursor is at the bottom of the box. Now it writes the temporary file
37 * lxdialog.scrltmp which contains this information. The file is
38 * deleted by lxdialog if the user leaves a submenu or enters a new
39 * one, but it would be nice if Menuconfig could make another "rm -f"
40 * just to be sure. Just try it out - you will recognise a difference!
44 * *) Now lxdialog is crash-safe against broken "lxdialog.scrltmp" files
45 * and menus change their size on the fly.
47 * *) If for some reason the last scrolling position is not saved by
48 * lxdialog, it sets the scrolling so that the selected item is in the
49 * middle of the menu box, not at the bottom.
51 * 02 January 1999, Michael Elizabeth Chastain (mec@shout.net)
52 * Reset 'scroll' to 0 if the value from lxdialog.scrltmp is bogus.
53 * This fixes a bug in Menuconfig where using ' ' to descend into menus
54 * would leave mis-synchronized lxdialog.scrltmp files lying around,
55 * fscanf would read in 'scroll', and eventually that value would get used.
60 static int menu_width, item_x;
65 static void do_print_item(WINDOW * win, const char *item, int line_y,
66 int selected, int hotkey)
69 char *menu_item = malloc(menu_width + 1);
71 strncpy(menu_item, item, menu_width - item_x);
72 menu_item[menu_width - item_x] = '\0';
73 j = first_alpha(menu_item, "YyNnMmHh");
75 /* Clear 'residue' of last item */
76 wattrset(win, dlg.menubox.atr);
77 wmove(win, line_y, 0);
81 for (i = 0; i < menu_width; i++)
87 wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
88 mvwaddstr(win, line_y, item_x, menu_item);
90 wattrset(win, selected ? dlg.tag_key_selected.atr
92 mvwaddch(win, line_y, item_x + j, menu_item[j]);
95 wmove(win, line_y, item_x + 1);
101 #define print_item(index, choice, selected) \
104 do_print_item(menu, item_str(), choice, selected, !item_is_tag(':')); \
108 * Print the scroll indicators.
110 static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
115 getyx(win, cur_y, cur_x);
120 wattrset(win, dlg.uarrow.atr);
121 waddch(win, ACS_UARROW);
124 wattrset(win, dlg.menubox.atr);
125 waddch(win, ACS_HLINE);
126 waddch(win, ACS_HLINE);
127 waddch(win, ACS_HLINE);
128 waddch(win, ACS_HLINE);
135 if ((height < item_no) && (scroll + height < item_no)) {
136 wattrset(win, dlg.darrow.atr);
137 waddch(win, ACS_DARROW);
140 wattrset(win, dlg.menubox_border.atr);
141 waddch(win, ACS_HLINE);
142 waddch(win, ACS_HLINE);
143 waddch(win, ACS_HLINE);
144 waddch(win, ACS_HLINE);
147 wmove(win, cur_y, cur_x);
152 * Display the termination buttons.
154 static void print_buttons(WINDOW * win, int height, int width, int selected)
156 int x = width / 2 - 16;
159 print_button(win, gettext("Select"), y, x, selected == 0);
160 print_button(win, gettext(" Exit "), y, x + 12, selected == 1);
161 print_button(win, gettext(" Help "), y, x + 24, selected == 2);
163 wmove(win, y, x + 1 + 12 * selected);
167 /* scroll up n lines (n may be negative) */
168 static void do_scroll(WINDOW *win, int *scroll, int n)
173 scrollok(win, FALSE);
174 *scroll = *scroll + n;
179 * Display a menu for choosing among a number of options
181 int dialog_menu(const char *title, const char *prompt,
182 const void *selected, int *s_scroll)
184 int i, j, x, y, box_x, box_y;
185 int height, width, menu_height;
186 int key = 0, button = 0, scroll = 0, choice = 0;
187 int first_item = 0, max_choice;
188 WINDOW *dialog, *menu;
191 height = getmaxy(stdscr);
192 width = getmaxx(stdscr);
193 if (height < 15 || width < 65)
194 return -ERRDISPLAYTOOSMALL;
198 menu_height = height - 10;
200 max_choice = MIN(menu_height, item_count());
202 /* center dialog box on screen */
203 x = (COLS - width) / 2;
204 y = (LINES - height) / 2;
206 draw_shadow(stdscr, y, x, height, width);
208 dialog = newwin(height, width, y, x);
209 keypad(dialog, TRUE);
211 draw_box(dialog, 0, 0, height, width,
212 dlg.dialog.atr, dlg.border.atr);
213 wattrset(dialog, dlg.border.atr);
214 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
215 for (i = 0; i < width - 2; i++)
216 waddch(dialog, ACS_HLINE);
217 wattrset(dialog, dlg.dialog.atr);
218 wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
219 waddch(dialog, ACS_RTEE);
221 print_title(dialog, title, width);
223 wattrset(dialog, dlg.dialog.atr);
224 print_autowrap(dialog, prompt, width - 2, 1, 3);
226 menu_width = width - 6;
227 box_y = height - menu_height - 5;
228 box_x = (width - menu_width) / 2 - 1;
230 /* create new window for the menu */
231 menu = subwin(dialog, menu_height, menu_width,
232 y + box_y + 1, x + box_x + 1);
235 /* draw a box around the menu items */
236 draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
237 dlg.menubox_border.atr, dlg.menubox.atr);
239 if (menu_width >= 80)
240 item_x = (menu_width - 70) / 2;
244 /* Set choice to default item */
246 if (selected && (selected == item_data()))
248 /* get the saved scroll info */
250 if ((scroll <= choice) && (scroll + max_choice > choice) &&
251 (scroll >= 0) && (scroll + max_choice <= item_count())) {
253 choice = choice - scroll;
257 if ((choice >= max_choice)) {
258 if (choice >= item_count() - max_choice / 2)
259 scroll = first_item = item_count() - max_choice;
261 scroll = first_item = choice - max_choice / 2;
262 choice = choice - scroll;
266 for (i = 0; i < max_choice; i++) {
267 print_item(first_item + i, i, i == choice);
272 print_arrows(dialog, item_count(), scroll,
273 box_y, box_x + item_x + 1, menu_height);
275 print_buttons(dialog, height, width, 0);
276 wmove(menu, choice, item_x + 1);
279 while (key != KEY_ESC) {
282 if (key < 256 && isalpha(key))
285 if (strchr("ynmh", key))
288 for (i = choice + 1; i < max_choice; i++) {
289 item_set(scroll + i);
290 j = first_alpha(item_str(), "YyNnMmHh");
291 if (key == tolower(item_str()[j]))
295 for (i = 0; i < max_choice; i++) {
296 item_set(scroll + i);
297 j = first_alpha(item_str(), "YyNnMmHh");
298 if (key == tolower(item_str()[j]))
303 if (i < max_choice ||
304 key == KEY_UP || key == KEY_DOWN ||
305 key == '-' || key == '+' ||
306 key == KEY_PPAGE || key == KEY_NPAGE) {
307 /* Remove highligt of current item */
308 print_item(scroll + choice, choice, FALSE);
310 if (key == KEY_UP || key == '-') {
311 if (choice < 2 && scroll) {
312 /* Scroll menu down */
313 do_scroll(menu, &scroll, -1);
315 print_item(scroll, 0, FALSE);
317 choice = MAX(choice - 1, 0);
319 } else if (key == KEY_DOWN || key == '+') {
320 print_item(scroll+choice, choice, FALSE);
322 if ((choice > max_choice - 3) &&
323 (scroll + max_choice < item_count())) {
325 do_scroll(menu, &scroll, 1);
327 print_item(scroll+max_choice - 1,
328 max_choice - 1, FALSE);
330 choice = MIN(choice + 1, max_choice - 1);
332 } else if (key == KEY_PPAGE) {
333 scrollok(menu, TRUE);
334 for (i = 0; (i < max_choice); i++) {
336 do_scroll(menu, &scroll, -1);
337 print_item(scroll, 0, FALSE);
344 } else if (key == KEY_NPAGE) {
345 for (i = 0; (i < max_choice); i++) {
346 if (scroll + max_choice < item_count()) {
347 do_scroll(menu, &scroll, 1);
348 print_item(scroll+max_choice-1,
349 max_choice - 1, FALSE);
351 if (choice + 1 < max_choice)
358 print_item(scroll + choice, choice, TRUE);
360 print_arrows(dialog, item_count(), scroll,
361 box_y, box_x + item_x + 1, menu_height);
363 wnoutrefresh(dialog);
366 continue; /* wait for another key press */
373 button = ((key == KEY_LEFT ? --button : ++button) < 0)
374 ? 2 : (button > 2 ? 0 : button);
376 print_buttons(dialog, height, width, button);
385 /* save scroll info */
389 item_set(scroll + choice);
390 item_set_selected(1);
413 item_set(scroll + choice);
414 item_set_selected(1);
421 key = on_key_esc(menu);
432 return key; /* ESC pressed */