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 * SPDX-License-Identifier: GPL-2.0+
11 * Changes by Clifford Wolf (god@clifford.at)
15 * *) A bugfix for the Page-Down problem
17 * *) Formerly when I used Page Down and Page Up, the cursor would be set
18 * to the first position in the menu box. Now lxdialog is a bit
19 * smarter and works more like other menu systems (just have a look at
22 * *) Formerly if I selected something my scrolling would be broken because
23 * lxdialog is re-invoked by the Menuconfig shell script, can't
24 * remember the last scrolling position, and just sets it so that the
25 * cursor is at the bottom of the box. Now it writes the temporary file
26 * lxdialog.scrltmp which contains this information. The file is
27 * deleted by lxdialog if the user leaves a submenu or enters a new
28 * one, but it would be nice if Menuconfig could make another "rm -f"
29 * just to be sure. Just try it out - you will recognise a difference!
33 * *) Now lxdialog is crash-safe against broken "lxdialog.scrltmp" files
34 * and menus change their size on the fly.
36 * *) If for some reason the last scrolling position is not saved by
37 * lxdialog, it sets the scrolling so that the selected item is in the
38 * middle of the menu box, not at the bottom.
40 * 02 January 1999, Michael Elizabeth Chastain (mec@shout.net)
41 * Reset 'scroll' to 0 if the value from lxdialog.scrltmp is bogus.
42 * This fixes a bug in Menuconfig where using ' ' to descend into menus
43 * would leave mis-synchronized lxdialog.scrltmp files lying around,
44 * fscanf would read in 'scroll', and eventually that value would get used.
49 static int menu_width, item_x;
54 static void do_print_item(WINDOW * win, const char *item, int line_y,
55 int selected, int hotkey)
58 char *menu_item = malloc(menu_width + 1);
60 strncpy(menu_item, item, menu_width - item_x);
61 menu_item[menu_width - item_x] = '\0';
62 j = first_alpha(menu_item, "YyNnMmHh");
64 /* Clear 'residue' of last item */
65 wattrset(win, dlg.menubox.atr);
66 wmove(win, line_y, 0);
70 for (i = 0; i < menu_width; i++)
76 wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
77 mvwaddstr(win, line_y, item_x, menu_item);
79 wattrset(win, selected ? dlg.tag_key_selected.atr
81 mvwaddch(win, line_y, item_x + j, menu_item[j]);
84 wmove(win, line_y, item_x + 1);
90 #define print_item(index, choice, selected) \
93 do_print_item(menu, item_str(), choice, selected, !item_is_tag(':')); \
97 * Print the scroll indicators.
99 static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
104 getyx(win, cur_y, cur_x);
109 wattrset(win, dlg.uarrow.atr);
110 waddch(win, ACS_UARROW);
113 wattrset(win, dlg.menubox.atr);
114 waddch(win, ACS_HLINE);
115 waddch(win, ACS_HLINE);
116 waddch(win, ACS_HLINE);
117 waddch(win, ACS_HLINE);
124 if ((height < item_no) && (scroll + height < item_no)) {
125 wattrset(win, dlg.darrow.atr);
126 waddch(win, ACS_DARROW);
129 wattrset(win, dlg.menubox_border.atr);
130 waddch(win, ACS_HLINE);
131 waddch(win, ACS_HLINE);
132 waddch(win, ACS_HLINE);
133 waddch(win, ACS_HLINE);
136 wmove(win, cur_y, cur_x);
141 * Display the termination buttons.
143 static void print_buttons(WINDOW * win, int height, int width, int selected)
145 int x = width / 2 - 28;
148 print_button(win, gettext("Select"), y, x, selected == 0);
149 print_button(win, gettext(" Exit "), y, x + 12, selected == 1);
150 print_button(win, gettext(" Help "), y, x + 24, selected == 2);
151 print_button(win, gettext(" Save "), y, x + 36, selected == 3);
152 print_button(win, gettext(" Load "), y, x + 48, selected == 4);
154 wmove(win, y, x + 1 + 12 * selected);
158 /* scroll up n lines (n may be negative) */
159 static void do_scroll(WINDOW *win, int *scroll, int n)
164 scrollok(win, FALSE);
165 *scroll = *scroll + n;
170 * Display a menu for choosing among a number of options
172 int dialog_menu(const char *title, const char *prompt,
173 const void *selected, int *s_scroll)
175 int i, j, x, y, box_x, box_y;
176 int height, width, menu_height;
177 int key = 0, button = 0, scroll = 0, choice = 0;
178 int first_item = 0, max_choice;
179 WINDOW *dialog, *menu;
182 height = getmaxy(stdscr);
183 width = getmaxx(stdscr);
184 if (height < MENUBOX_HEIGTH_MIN || width < MENUBOX_WIDTH_MIN)
185 return -ERRDISPLAYTOOSMALL;
189 menu_height = height - 10;
191 max_choice = MIN(menu_height, item_count());
193 /* center dialog box on screen */
194 x = (getmaxx(stdscr) - width) / 2;
195 y = (getmaxy(stdscr) - height) / 2;
197 draw_shadow(stdscr, y, x, height, width);
199 dialog = newwin(height, width, y, x);
200 keypad(dialog, TRUE);
202 draw_box(dialog, 0, 0, height, width,
203 dlg.dialog.atr, dlg.border.atr);
204 wattrset(dialog, dlg.border.atr);
205 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
206 for (i = 0; i < width - 2; i++)
207 waddch(dialog, ACS_HLINE);
208 wattrset(dialog, dlg.dialog.atr);
209 wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
210 waddch(dialog, ACS_RTEE);
212 print_title(dialog, title, width);
214 wattrset(dialog, dlg.dialog.atr);
215 print_autowrap(dialog, prompt, width - 2, 1, 3);
217 menu_width = width - 6;
218 box_y = height - menu_height - 5;
219 box_x = (width - menu_width) / 2 - 1;
221 /* create new window for the menu */
222 menu = subwin(dialog, menu_height, menu_width,
223 y + box_y + 1, x + box_x + 1);
226 /* draw a box around the menu items */
227 draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
228 dlg.menubox_border.atr, dlg.menubox.atr);
230 if (menu_width >= 80)
231 item_x = (menu_width - 70) / 2;
235 /* Set choice to default item */
237 if (selected && (selected == item_data()))
239 /* get the saved scroll info */
241 if ((scroll <= choice) && (scroll + max_choice > choice) &&
242 (scroll >= 0) && (scroll + max_choice <= item_count())) {
244 choice = choice - scroll;
248 if ((choice >= max_choice)) {
249 if (choice >= item_count() - max_choice / 2)
250 scroll = first_item = item_count() - max_choice;
252 scroll = first_item = choice - max_choice / 2;
253 choice = choice - scroll;
257 for (i = 0; i < max_choice; i++) {
258 print_item(first_item + i, i, i == choice);
263 print_arrows(dialog, item_count(), scroll,
264 box_y, box_x + item_x + 1, menu_height);
266 print_buttons(dialog, height, width, 0);
267 wmove(menu, choice, item_x + 1);
270 while (key != KEY_ESC) {
273 if (key < 256 && isalpha(key))
276 if (strchr("ynmh", key))
279 for (i = choice + 1; i < max_choice; i++) {
280 item_set(scroll + i);
281 j = first_alpha(item_str(), "YyNnMmHh");
282 if (key == tolower(item_str()[j]))
286 for (i = 0; i < max_choice; i++) {
287 item_set(scroll + i);
288 j = first_alpha(item_str(), "YyNnMmHh");
289 if (key == tolower(item_str()[j]))
294 if (item_count() != 0 &&
296 key == KEY_UP || key == KEY_DOWN ||
297 key == '-' || key == '+' ||
298 key == KEY_PPAGE || key == KEY_NPAGE)) {
299 /* Remove highligt of current item */
300 print_item(scroll + choice, choice, FALSE);
302 if (key == KEY_UP || key == '-') {
303 if (choice < 2 && scroll) {
304 /* Scroll menu down */
305 do_scroll(menu, &scroll, -1);
307 print_item(scroll, 0, FALSE);
309 choice = MAX(choice - 1, 0);
311 } else if (key == KEY_DOWN || key == '+') {
312 print_item(scroll+choice, choice, FALSE);
314 if ((choice > max_choice - 3) &&
315 (scroll + max_choice < item_count())) {
317 do_scroll(menu, &scroll, 1);
319 print_item(scroll+max_choice - 1,
320 max_choice - 1, FALSE);
322 choice = MIN(choice + 1, max_choice - 1);
324 } else if (key == KEY_PPAGE) {
325 scrollok(menu, TRUE);
326 for (i = 0; (i < max_choice); i++) {
328 do_scroll(menu, &scroll, -1);
329 print_item(scroll, 0, FALSE);
336 } else if (key == KEY_NPAGE) {
337 for (i = 0; (i < max_choice); i++) {
338 if (scroll + max_choice < item_count()) {
339 do_scroll(menu, &scroll, 1);
340 print_item(scroll+max_choice-1,
341 max_choice - 1, FALSE);
343 if (choice + 1 < max_choice)
350 print_item(scroll + choice, choice, TRUE);
352 print_arrows(dialog, item_count(), scroll,
353 box_y, box_x + item_x + 1, menu_height);
355 wnoutrefresh(dialog);
358 continue; /* wait for another key press */
365 button = ((key == KEY_LEFT ? --button : ++button) < 0)
366 ? 4 : (button > 4 ? 0 : button);
368 print_buttons(dialog, height, width, button);
381 /* save scroll info */
385 item_set(scroll + choice);
386 item_set_selected(1);
413 key = on_key_esc(menu);
424 return key; /* ESC pressed */