menubox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * menubox.c -- implements the menu box
  4. *
  5. * ORIGINAL AUTHOR: Savio Lam ([email protected])
  6. * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap ([email protected])
  7. */
  8. /*
  9. * Changes by Clifford Wolf ([email protected])
  10. *
  11. * [ 1998-06-13 ]
  12. *
  13. * *) A bugfix for the Page-Down problem
  14. *
  15. * *) Formerly when I used Page Down and Page Up, the cursor would be set
  16. * to the first position in the menu box. Now lxdialog is a bit
  17. * smarter and works more like other menu systems (just have a look at
  18. * it).
  19. *
  20. * *) Formerly if I selected something my scrolling would be broken because
  21. * lxdialog is re-invoked by the Menuconfig shell script, can't
  22. * remember the last scrolling position, and just sets it so that the
  23. * cursor is at the bottom of the box. Now it writes the temporary file
  24. * lxdialog.scrltmp which contains this information. The file is
  25. * deleted by lxdialog if the user leaves a submenu or enters a new
  26. * one, but it would be nice if Menuconfig could make another "rm -f"
  27. * just to be sure. Just try it out - you will recognise a difference!
  28. *
  29. * [ 1998-06-14 ]
  30. *
  31. * *) Now lxdialog is crash-safe against broken "lxdialog.scrltmp" files
  32. * and menus change their size on the fly.
  33. *
  34. * *) If for some reason the last scrolling position is not saved by
  35. * lxdialog, it sets the scrolling so that the selected item is in the
  36. * middle of the menu box, not at the bottom.
  37. *
  38. * 02 January 1999, Michael Elizabeth Chastain ([email protected])
  39. * Reset 'scroll' to 0 if the value from lxdialog.scrltmp is bogus.
  40. * This fixes a bug in Menuconfig where using ' ' to descend into menus
  41. * would leave mis-synchronized lxdialog.scrltmp files lying around,
  42. * fscanf would read in 'scroll', and eventually that value would get used.
  43. */
  44. #include "dialog.h"
  45. static int menu_width, item_x;
  46. /*
  47. * Print menu item
  48. */
  49. static void do_print_item(WINDOW * win, const char *item, int line_y,
  50. int selected, int hotkey)
  51. {
  52. int j;
  53. char *menu_item = malloc(menu_width + 1);
  54. strncpy(menu_item, item, menu_width - item_x);
  55. menu_item[menu_width - item_x] = '\0';
  56. j = first_alpha(menu_item, "YyNnMmHh");
  57. /* Clear 'residue' of last item */
  58. wattrset(win, dlg.menubox.atr);
  59. wmove(win, line_y, 0);
  60. wclrtoeol(win);
  61. wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
  62. mvwaddstr(win, line_y, item_x, menu_item);
  63. if (hotkey) {
  64. wattrset(win, selected ? dlg.tag_key_selected.atr
  65. : dlg.tag_key.atr);
  66. mvwaddch(win, line_y, item_x + j, menu_item[j]);
  67. }
  68. if (selected) {
  69. wmove(win, line_y, item_x + 1);
  70. }
  71. free(menu_item);
  72. wrefresh(win);
  73. }
  74. #define print_item(index, choice, selected) \
  75. do { \
  76. item_set(index); \
  77. do_print_item(menu, item_str(), choice, selected, !item_is_tag(':')); \
  78. } while (0)
  79. /*
  80. * Print the scroll indicators.
  81. */
  82. static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
  83. int height)
  84. {
  85. int cur_y, cur_x;
  86. getyx(win, cur_y, cur_x);
  87. wmove(win, y, x);
  88. if (scroll > 0) {
  89. wattrset(win, dlg.uarrow.atr);
  90. waddch(win, ACS_UARROW);
  91. waddstr(win, "(-)");
  92. } else {
  93. wattrset(win, dlg.menubox.atr);
  94. waddch(win, ACS_HLINE);
  95. waddch(win, ACS_HLINE);
  96. waddch(win, ACS_HLINE);
  97. waddch(win, ACS_HLINE);
  98. }
  99. y = y + height + 1;
  100. wmove(win, y, x);
  101. wrefresh(win);
  102. if ((height < item_no) && (scroll + height < item_no)) {
  103. wattrset(win, dlg.darrow.atr);
  104. waddch(win, ACS_DARROW);
  105. waddstr(win, "(+)");
  106. } else {
  107. wattrset(win, dlg.menubox_border.atr);
  108. waddch(win, ACS_HLINE);
  109. waddch(win, ACS_HLINE);
  110. waddch(win, ACS_HLINE);
  111. waddch(win, ACS_HLINE);
  112. }
  113. wmove(win, cur_y, cur_x);
  114. wrefresh(win);
  115. }
  116. /*
  117. * Display the termination buttons.
  118. */
  119. static void print_buttons(WINDOW * win, int height, int width, int selected)
  120. {
  121. int x = width / 2 - 28;
  122. int y = height - 2;
  123. print_button(win, "Select", y, x, selected == 0);
  124. print_button(win, " Exit ", y, x + 12, selected == 1);
  125. print_button(win, " Help ", y, x + 24, selected == 2);
  126. print_button(win, " Save ", y, x + 36, selected == 3);
  127. print_button(win, " Load ", y, x + 48, selected == 4);
  128. wmove(win, y, x + 1 + 12 * selected);
  129. wrefresh(win);
  130. }
  131. /* scroll up n lines (n may be negative) */
  132. static void do_scroll(WINDOW *win, int *scroll, int n)
  133. {
  134. /* Scroll menu up */
  135. scrollok(win, TRUE);
  136. wscrl(win, n);
  137. scrollok(win, FALSE);
  138. *scroll = *scroll + n;
  139. wrefresh(win);
  140. }
  141. /*
  142. * Display a menu for choosing among a number of options
  143. */
  144. int dialog_menu(const char *title, const char *prompt,
  145. const void *selected, int *s_scroll)
  146. {
  147. int i, j, x, y, box_x, box_y;
  148. int height, width, menu_height;
  149. int key = 0, button = 0, scroll = 0, choice = 0;
  150. int first_item = 0, max_choice;
  151. WINDOW *dialog, *menu;
  152. do_resize:
  153. height = getmaxy(stdscr);
  154. width = getmaxx(stdscr);
  155. if (height < MENUBOX_HEIGTH_MIN || width < MENUBOX_WIDTH_MIN)
  156. return -ERRDISPLAYTOOSMALL;
  157. height -= 4;
  158. width -= 5;
  159. menu_height = height - 10;
  160. max_choice = MIN(menu_height, item_count());
  161. /* center dialog box on screen */
  162. x = (getmaxx(stdscr) - width) / 2;
  163. y = (getmaxy(stdscr) - height) / 2;
  164. draw_shadow(stdscr, y, x, height, width);
  165. dialog = newwin(height, width, y, x);
  166. keypad(dialog, TRUE);
  167. draw_box(dialog, 0, 0, height, width,
  168. dlg.dialog.atr, dlg.border.atr);
  169. wattrset(dialog, dlg.border.atr);
  170. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  171. for (i = 0; i < width - 2; i++)
  172. waddch(dialog, ACS_HLINE);
  173. wattrset(dialog, dlg.dialog.atr);
  174. wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
  175. waddch(dialog, ACS_RTEE);
  176. print_title(dialog, title, width);
  177. wattrset(dialog, dlg.dialog.atr);
  178. print_autowrap(dialog, prompt, width - 2, 1, 3);
  179. menu_width = width - 6;
  180. box_y = height - menu_height - 5;
  181. box_x = (width - menu_width) / 2 - 1;
  182. /* create new window for the menu */
  183. menu = subwin(dialog, menu_height, menu_width,
  184. y + box_y + 1, x + box_x + 1);
  185. keypad(menu, TRUE);
  186. /* draw a box around the menu items */
  187. draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
  188. dlg.menubox_border.atr, dlg.menubox.atr);
  189. if (menu_width >= 80)
  190. item_x = (menu_width - 70) / 2;
  191. else
  192. item_x = 4;
  193. /* Set choice to default item */
  194. item_foreach()
  195. if (selected && (selected == item_data()))
  196. choice = item_n();
  197. /* get the saved scroll info */
  198. scroll = *s_scroll;
  199. if ((scroll <= choice) && (scroll + max_choice > choice) &&
  200. (scroll >= 0) && (scroll + max_choice <= item_count())) {
  201. first_item = scroll;
  202. choice = choice - scroll;
  203. } else {
  204. scroll = 0;
  205. }
  206. if ((choice >= max_choice)) {
  207. if (choice >= item_count() - max_choice / 2)
  208. scroll = first_item = item_count() - max_choice;
  209. else
  210. scroll = first_item = choice - max_choice / 2;
  211. choice = choice - scroll;
  212. }
  213. /* Print the menu */
  214. for (i = 0; i < max_choice; i++) {
  215. print_item(first_item + i, i, i == choice);
  216. }
  217. wnoutrefresh(menu);
  218. print_arrows(dialog, item_count(), scroll,
  219. box_y, box_x + item_x + 1, menu_height);
  220. print_buttons(dialog, height, width, 0);
  221. wmove(menu, choice, item_x + 1);
  222. wrefresh(menu);
  223. while (key != KEY_ESC) {
  224. key = wgetch(menu);
  225. if (key < 256 && isalpha(key))
  226. key = tolower(key);
  227. if (strchr("ynmh", key))
  228. i = max_choice;
  229. else {
  230. for (i = choice + 1; i < max_choice; i++) {
  231. item_set(scroll + i);
  232. j = first_alpha(item_str(), "YyNnMmHh");
  233. if (key == tolower(item_str()[j]))
  234. break;
  235. }
  236. if (i == max_choice)
  237. for (i = 0; i < max_choice; i++) {
  238. item_set(scroll + i);
  239. j = first_alpha(item_str(), "YyNnMmHh");
  240. if (key == tolower(item_str()[j]))
  241. break;
  242. }
  243. }
  244. if (item_count() != 0 &&
  245. (i < max_choice ||
  246. key == KEY_UP || key == KEY_DOWN ||
  247. key == '-' || key == '+' ||
  248. key == KEY_PPAGE || key == KEY_NPAGE)) {
  249. /* Remove highligt of current item */
  250. print_item(scroll + choice, choice, FALSE);
  251. if (key == KEY_UP || key == '-') {
  252. if (choice < 2 && scroll) {
  253. /* Scroll menu down */
  254. do_scroll(menu, &scroll, -1);
  255. print_item(scroll, 0, FALSE);
  256. } else
  257. choice = MAX(choice - 1, 0);
  258. } else if (key == KEY_DOWN || key == '+') {
  259. print_item(scroll+choice, choice, FALSE);
  260. if ((choice > max_choice - 3) &&
  261. (scroll + max_choice < item_count())) {
  262. /* Scroll menu up */
  263. do_scroll(menu, &scroll, 1);
  264. print_item(scroll+max_choice - 1,
  265. max_choice - 1, FALSE);
  266. } else
  267. choice = MIN(choice + 1, max_choice - 1);
  268. } else if (key == KEY_PPAGE) {
  269. scrollok(menu, TRUE);
  270. for (i = 0; (i < max_choice); i++) {
  271. if (scroll > 0) {
  272. do_scroll(menu, &scroll, -1);
  273. print_item(scroll, 0, FALSE);
  274. } else {
  275. if (choice > 0)
  276. choice--;
  277. }
  278. }
  279. } else if (key == KEY_NPAGE) {
  280. for (i = 0; (i < max_choice); i++) {
  281. if (scroll + max_choice < item_count()) {
  282. do_scroll(menu, &scroll, 1);
  283. print_item(scroll+max_choice-1,
  284. max_choice - 1, FALSE);
  285. } else {
  286. if (choice + 1 < max_choice)
  287. choice++;
  288. }
  289. }
  290. } else
  291. choice = i;
  292. print_item(scroll + choice, choice, TRUE);
  293. print_arrows(dialog, item_count(), scroll,
  294. box_y, box_x + item_x + 1, menu_height);
  295. wnoutrefresh(dialog);
  296. wrefresh(menu);
  297. continue; /* wait for another key press */
  298. }
  299. switch (key) {
  300. case KEY_LEFT:
  301. case TAB:
  302. case KEY_RIGHT:
  303. button = ((key == KEY_LEFT ? --button : ++button) < 0)
  304. ? 4 : (button > 4 ? 0 : button);
  305. print_buttons(dialog, height, width, button);
  306. wrefresh(menu);
  307. break;
  308. case ' ':
  309. case 's':
  310. case 'y':
  311. case 'n':
  312. case 'm':
  313. case '/':
  314. case 'h':
  315. case '?':
  316. case 'z':
  317. case '\n':
  318. /* save scroll info */
  319. *s_scroll = scroll;
  320. delwin(menu);
  321. delwin(dialog);
  322. item_set(scroll + choice);
  323. item_set_selected(1);
  324. switch (key) {
  325. case 'h':
  326. case '?':
  327. return 2;
  328. case 's':
  329. case 'y':
  330. return 5;
  331. case 'n':
  332. return 6;
  333. case 'm':
  334. return 7;
  335. case ' ':
  336. return 8;
  337. case '/':
  338. return 9;
  339. case 'z':
  340. return 10;
  341. case '\n':
  342. return button;
  343. }
  344. return 0;
  345. case 'e':
  346. case 'x':
  347. key = KEY_ESC;
  348. break;
  349. case KEY_ESC:
  350. key = on_key_esc(menu);
  351. break;
  352. case KEY_RESIZE:
  353. on_key_resize();
  354. delwin(menu);
  355. delwin(dialog);
  356. goto do_resize;
  357. }
  358. }
  359. delwin(menu);
  360. delwin(dialog);
  361. return key; /* ESC pressed */
  362. }