Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 1 | /* |
| 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 | * |
Tom Rini | 5b8031c | 2016-01-14 22:05:13 -0500 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Changes by Clifford Wolf (god@clifford.at) |
| 12 | * |
| 13 | * [ 1998-06-13 ] |
| 14 | * |
| 15 | * *) A bugfix for the Page-Down problem |
| 16 | * |
| 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 |
| 20 | * it). |
| 21 | * |
| 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! |
| 30 | * |
| 31 | * [ 1998-06-14 ] |
| 32 | * |
| 33 | * *) Now lxdialog is crash-safe against broken "lxdialog.scrltmp" files |
| 34 | * and menus change their size on the fly. |
| 35 | * |
| 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. |
| 39 | * |
| 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. |
| 45 | */ |
| 46 | |
| 47 | #include "dialog.h" |
| 48 | |
| 49 | static int menu_width, item_x; |
| 50 | |
| 51 | /* |
| 52 | * Print menu item |
| 53 | */ |
| 54 | static void do_print_item(WINDOW * win, const char *item, int line_y, |
| 55 | int selected, int hotkey) |
| 56 | { |
| 57 | int j; |
| 58 | char *menu_item = malloc(menu_width + 1); |
| 59 | |
| 60 | strncpy(menu_item, item, menu_width - item_x); |
| 61 | menu_item[menu_width - item_x] = '\0'; |
| 62 | j = first_alpha(menu_item, "YyNnMmHh"); |
| 63 | |
| 64 | /* Clear 'residue' of last item */ |
| 65 | wattrset(win, dlg.menubox.atr); |
| 66 | wmove(win, line_y, 0); |
| 67 | #if OLD_NCURSES |
| 68 | { |
| 69 | int i; |
| 70 | for (i = 0; i < menu_width; i++) |
| 71 | waddch(win, ' '); |
| 72 | } |
| 73 | #else |
| 74 | wclrtoeol(win); |
| 75 | #endif |
| 76 | wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr); |
| 77 | mvwaddstr(win, line_y, item_x, menu_item); |
| 78 | if (hotkey) { |
| 79 | wattrset(win, selected ? dlg.tag_key_selected.atr |
| 80 | : dlg.tag_key.atr); |
| 81 | mvwaddch(win, line_y, item_x + j, menu_item[j]); |
| 82 | } |
| 83 | if (selected) { |
| 84 | wmove(win, line_y, item_x + 1); |
| 85 | } |
| 86 | free(menu_item); |
| 87 | wrefresh(win); |
| 88 | } |
| 89 | |
| 90 | #define print_item(index, choice, selected) \ |
| 91 | do { \ |
| 92 | item_set(index); \ |
| 93 | do_print_item(menu, item_str(), choice, selected, !item_is_tag(':')); \ |
| 94 | } while (0) |
| 95 | |
| 96 | /* |
| 97 | * Print the scroll indicators. |
| 98 | */ |
| 99 | static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x, |
| 100 | int height) |
| 101 | { |
| 102 | int cur_y, cur_x; |
| 103 | |
| 104 | getyx(win, cur_y, cur_x); |
| 105 | |
| 106 | wmove(win, y, x); |
| 107 | |
| 108 | if (scroll > 0) { |
| 109 | wattrset(win, dlg.uarrow.atr); |
| 110 | waddch(win, ACS_UARROW); |
| 111 | waddstr(win, "(-)"); |
| 112 | } else { |
| 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); |
| 118 | } |
| 119 | |
| 120 | y = y + height + 1; |
| 121 | wmove(win, y, x); |
| 122 | wrefresh(win); |
| 123 | |
| 124 | if ((height < item_no) && (scroll + height < item_no)) { |
| 125 | wattrset(win, dlg.darrow.atr); |
| 126 | waddch(win, ACS_DARROW); |
| 127 | waddstr(win, "(+)"); |
| 128 | } else { |
| 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); |
| 134 | } |
| 135 | |
| 136 | wmove(win, cur_y, cur_x); |
| 137 | wrefresh(win); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Display the termination buttons. |
| 142 | */ |
| 143 | static void print_buttons(WINDOW * win, int height, int width, int selected) |
| 144 | { |
| 145 | int x = width / 2 - 28; |
| 146 | int y = height - 2; |
| 147 | |
| 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); |
| 153 | |
| 154 | wmove(win, y, x + 1 + 12 * selected); |
| 155 | wrefresh(win); |
| 156 | } |
| 157 | |
| 158 | /* scroll up n lines (n may be negative) */ |
| 159 | static void do_scroll(WINDOW *win, int *scroll, int n) |
| 160 | { |
| 161 | /* Scroll menu up */ |
| 162 | scrollok(win, TRUE); |
| 163 | wscrl(win, n); |
| 164 | scrollok(win, FALSE); |
| 165 | *scroll = *scroll + n; |
| 166 | wrefresh(win); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Display a menu for choosing among a number of options |
| 171 | */ |
| 172 | int dialog_menu(const char *title, const char *prompt, |
| 173 | const void *selected, int *s_scroll) |
| 174 | { |
| 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; |
| 180 | |
| 181 | do_resize: |
| 182 | height = getmaxy(stdscr); |
| 183 | width = getmaxx(stdscr); |
| 184 | if (height < MENUBOX_HEIGTH_MIN || width < MENUBOX_WIDTH_MIN) |
| 185 | return -ERRDISPLAYTOOSMALL; |
| 186 | |
| 187 | height -= 4; |
| 188 | width -= 5; |
| 189 | menu_height = height - 10; |
| 190 | |
| 191 | max_choice = MIN(menu_height, item_count()); |
| 192 | |
| 193 | /* center dialog box on screen */ |
| 194 | x = (getmaxx(stdscr) - width) / 2; |
| 195 | y = (getmaxy(stdscr) - height) / 2; |
| 196 | |
| 197 | draw_shadow(stdscr, y, x, height, width); |
| 198 | |
| 199 | dialog = newwin(height, width, y, x); |
| 200 | keypad(dialog, TRUE); |
| 201 | |
| 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); |
| 211 | |
| 212 | print_title(dialog, title, width); |
| 213 | |
| 214 | wattrset(dialog, dlg.dialog.atr); |
| 215 | print_autowrap(dialog, prompt, width - 2, 1, 3); |
| 216 | |
| 217 | menu_width = width - 6; |
| 218 | box_y = height - menu_height - 5; |
| 219 | box_x = (width - menu_width) / 2 - 1; |
| 220 | |
| 221 | /* create new window for the menu */ |
| 222 | menu = subwin(dialog, menu_height, menu_width, |
| 223 | y + box_y + 1, x + box_x + 1); |
| 224 | keypad(menu, TRUE); |
| 225 | |
| 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); |
| 229 | |
| 230 | if (menu_width >= 80) |
| 231 | item_x = (menu_width - 70) / 2; |
| 232 | else |
| 233 | item_x = 4; |
| 234 | |
| 235 | /* Set choice to default item */ |
| 236 | item_foreach() |
| 237 | if (selected && (selected == item_data())) |
| 238 | choice = item_n(); |
| 239 | /* get the saved scroll info */ |
| 240 | scroll = *s_scroll; |
| 241 | if ((scroll <= choice) && (scroll + max_choice > choice) && |
| 242 | (scroll >= 0) && (scroll + max_choice <= item_count())) { |
| 243 | first_item = scroll; |
| 244 | choice = choice - scroll; |
| 245 | } else { |
| 246 | scroll = 0; |
| 247 | } |
| 248 | if ((choice >= max_choice)) { |
| 249 | if (choice >= item_count() - max_choice / 2) |
| 250 | scroll = first_item = item_count() - max_choice; |
| 251 | else |
| 252 | scroll = first_item = choice - max_choice / 2; |
| 253 | choice = choice - scroll; |
| 254 | } |
| 255 | |
| 256 | /* Print the menu */ |
| 257 | for (i = 0; i < max_choice; i++) { |
| 258 | print_item(first_item + i, i, i == choice); |
| 259 | } |
| 260 | |
| 261 | wnoutrefresh(menu); |
| 262 | |
| 263 | print_arrows(dialog, item_count(), scroll, |
| 264 | box_y, box_x + item_x + 1, menu_height); |
| 265 | |
| 266 | print_buttons(dialog, height, width, 0); |
| 267 | wmove(menu, choice, item_x + 1); |
| 268 | wrefresh(menu); |
| 269 | |
| 270 | while (key != KEY_ESC) { |
| 271 | key = wgetch(menu); |
| 272 | |
| 273 | if (key < 256 && isalpha(key)) |
| 274 | key = tolower(key); |
| 275 | |
| 276 | if (strchr("ynmh", key)) |
| 277 | i = max_choice; |
| 278 | else { |
| 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])) |
| 283 | break; |
| 284 | } |
| 285 | if (i == max_choice) |
| 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])) |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (item_count() != 0 && |
| 295 | (i < max_choice || |
| 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); |
| 301 | |
| 302 | if (key == KEY_UP || key == '-') { |
| 303 | if (choice < 2 && scroll) { |
| 304 | /* Scroll menu down */ |
| 305 | do_scroll(menu, &scroll, -1); |
| 306 | |
| 307 | print_item(scroll, 0, FALSE); |
| 308 | } else |
| 309 | choice = MAX(choice - 1, 0); |
| 310 | |
| 311 | } else if (key == KEY_DOWN || key == '+') { |
| 312 | print_item(scroll+choice, choice, FALSE); |
| 313 | |
| 314 | if ((choice > max_choice - 3) && |
| 315 | (scroll + max_choice < item_count())) { |
| 316 | /* Scroll menu up */ |
| 317 | do_scroll(menu, &scroll, 1); |
| 318 | |
| 319 | print_item(scroll+max_choice - 1, |
| 320 | max_choice - 1, FALSE); |
| 321 | } else |
| 322 | choice = MIN(choice + 1, max_choice - 1); |
| 323 | |
| 324 | } else if (key == KEY_PPAGE) { |
| 325 | scrollok(menu, TRUE); |
| 326 | for (i = 0; (i < max_choice); i++) { |
| 327 | if (scroll > 0) { |
| 328 | do_scroll(menu, &scroll, -1); |
| 329 | print_item(scroll, 0, FALSE); |
| 330 | } else { |
| 331 | if (choice > 0) |
| 332 | choice--; |
| 333 | } |
| 334 | } |
| 335 | |
| 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); |
| 342 | } else { |
| 343 | if (choice + 1 < max_choice) |
| 344 | choice++; |
| 345 | } |
| 346 | } |
| 347 | } else |
| 348 | choice = i; |
| 349 | |
| 350 | print_item(scroll + choice, choice, TRUE); |
| 351 | |
| 352 | print_arrows(dialog, item_count(), scroll, |
| 353 | box_y, box_x + item_x + 1, menu_height); |
| 354 | |
| 355 | wnoutrefresh(dialog); |
| 356 | wrefresh(menu); |
| 357 | |
| 358 | continue; /* wait for another key press */ |
| 359 | } |
| 360 | |
| 361 | switch (key) { |
| 362 | case KEY_LEFT: |
| 363 | case TAB: |
| 364 | case KEY_RIGHT: |
| 365 | button = ((key == KEY_LEFT ? --button : ++button) < 0) |
| 366 | ? 4 : (button > 4 ? 0 : button); |
| 367 | |
| 368 | print_buttons(dialog, height, width, button); |
| 369 | wrefresh(menu); |
| 370 | break; |
| 371 | case ' ': |
| 372 | case 's': |
| 373 | case 'y': |
| 374 | case 'n': |
| 375 | case 'm': |
| 376 | case '/': |
| 377 | case 'h': |
| 378 | case '?': |
| 379 | case 'z': |
| 380 | case '\n': |
| 381 | /* save scroll info */ |
| 382 | *s_scroll = scroll; |
| 383 | delwin(menu); |
| 384 | delwin(dialog); |
| 385 | item_set(scroll + choice); |
| 386 | item_set_selected(1); |
| 387 | switch (key) { |
| 388 | case 'h': |
| 389 | case '?': |
| 390 | return 2; |
| 391 | case 's': |
| 392 | case 'y': |
| 393 | return 5; |
| 394 | case 'n': |
| 395 | return 6; |
| 396 | case 'm': |
| 397 | return 7; |
| 398 | case ' ': |
| 399 | return 8; |
| 400 | case '/': |
| 401 | return 9; |
| 402 | case 'z': |
| 403 | return 10; |
| 404 | case '\n': |
| 405 | return button; |
| 406 | } |
| 407 | return 0; |
| 408 | case 'e': |
| 409 | case 'x': |
| 410 | key = KEY_ESC; |
| 411 | break; |
| 412 | case KEY_ESC: |
| 413 | key = on_key_esc(menu); |
| 414 | break; |
| 415 | case KEY_RESIZE: |
| 416 | on_key_resize(); |
| 417 | delwin(menu); |
| 418 | delwin(dialog); |
| 419 | goto do_resize; |
| 420 | } |
| 421 | } |
| 422 | delwin(menu); |
| 423 | delwin(dialog); |
| 424 | return key; /* ESC pressed */ |
| 425 | } |