menubox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * menubox.c -- implements the menu box
  4. *
  5. * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  6. * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcapw@cfw.com)
  7. */
  8. /*
  9. * Changes by Clifford Wolf (god@clifford.at)
  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 (mec@shout.net)
  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. #if OLD_NCURSES
  61. {
  62. int i;
  63. for (i = 0; i < menu_width; i++)
  64. waddch(win, ' ');
  65. }
  66. #else
  67. wclrtoeol(win);
  68. #endif
  69. wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
  70. mvwaddstr(win, line_y, item_x, menu_item);
  71. if (hotkey) {
  72. wattrset(win, selected ? dlg.tag_key_selected.atr
  73. : dlg.tag_key.atr);
  74. mvwaddch(win, line_y, item_x + j, menu_item[j]);
  75. }
  76. if (selected) {
  77. wmove(win, line_y, item_x + 1);
  78. }
  79. free(menu_item);
  80. wrefresh(win);
  81. }
  82. #define print_item(index, choice, selected) \
  83. do { \
  84. item_set(index); \
  85. do_print_item(menu, item_str(), choice, selected, !item_is_tag(':')); \
  86. } while (0)
  87. /*
  88. * Print the scroll indicators.
  89. */
  90. static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
  91. int height)
  92. {
  93. int cur_y, cur_x;
  94. getyx(win, cur_y, cur_x);
  95. wmove(win, y, x);
  96. if (scroll > 0) {
  97. wattrset(win, dlg.uarrow.atr);
  98. waddch(win, ACS_UARROW);
  99. waddstr(win, "(-)");
  100. } else {
  101. wattrset(win, dlg.menubox.atr);
  102. waddch(win, ACS_HLINE);
  103. waddch(win, ACS_HLINE);
  104. waddch(win, ACS_HLINE);
  105. waddch(win, ACS_HLINE);
  106. }
  107. y = y + height + 1;
  108. wmove(win, y, x);
  109. wrefresh(win);
  110. if ((height < item_no) && (scroll + height < item_no)) {
  111. wattrset(win, dlg.darrow.atr);
  112. waddch(win, ACS_DARROW);
  113. waddstr(win, "(+)");
  114. } else {
  115. wattrset(win, dlg.menubox_border.atr);
  116. waddch(win, ACS_HLINE);
  117. waddch(win, ACS_HLINE);
  118. waddch(win, ACS_HLINE);
  119. waddch(win, ACS_HLINE);
  120. }
  121. wmove(win, cur_y, cur_x);
  122. wrefresh(win);
  123. }
  124. /*
  125. * Display the termination buttons.
  126. */
  127. static void print_buttons(WINDOW * win, int height, int width, int selected)
  128. {
  129. int x = width / 2 - 28;
  130. int y = height - 2;
  131. print_button(win, gettext("Select"), y, x, selected == 0);
  132. print_button(win, gettext(" Exit "), y, x + 12, selected == 1);
  133. print_button(win, gettext(" Help "), y, x + 24, selected == 2);
  134. print_button(win, gettext(" Save "), y, x + 36, selected == 3);
  135. print_button(win, gettext(" Load "), y, x + 48, selected == 4);
  136. wmove(win, y, x + 1 + 12 * selected);
  137. wrefresh(win);
  138. }
  139. /* scroll up n lines (n may be negative) */
  140. static void do_scroll(WINDOW *win, int *scroll, int n)
  141. {
  142. /* Scroll menu up */
  143. scrollok(win, TRUE);
  144. wscrl(win, n);
  145. scrollok(win, FALSE);
  146. *scroll = *scroll + n;
  147. wrefresh(win);
  148. }
  149. /*
  150. * Display a menu for choosing among a number of options
  151. */
  152. int dialog_menu(const char *title, const char *prompt,
  153. const void *selected, int *s_scroll)
  154. {
  155. int i, j, x, y, box_x, box_y;
  156. int height, width, menu_height;
  157. int key = 0, button = 0, scroll = 0, choice = 0;
  158. int first_item = 0, max_choice;
  159. WINDOW *dialog, *menu;
  160. do_resize:
  161. height = getmaxy(stdscr);
  162. width = getmaxx(stdscr);
  163. if (height < MENUBOX_HEIGTH_MIN || width < MENUBOX_WIDTH_MIN)
  164. return -ERRDISPLAYTOOSMALL;
  165. height -= 4;
  166. width -= 5;
  167. menu_height = height - 10;
  168. max_choice = MIN(menu_height, item_count());
  169. /* center dialog box on screen */
  170. x = (getmaxx(stdscr) - width) / 2;
  171. y = (getmaxy(stdscr) - height) / 2;
  172. draw_shadow(stdscr, y, x, height, width);
  173. dialog = newwin(height, width, y, x);
  174. keypad(dialog, TRUE);
  175. draw_box(dialog, 0, 0, height, width,
  176. dlg.dialog.atr, dlg.border.atr);
  177. wattrset(dialog, dlg.border.atr);
  178. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  179. for (i = 0; i < width - 2; i++)
  180. waddch(dialog, ACS_HLINE);
  181. wattrset(dialog, dlg.dialog.atr);
  182. wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
  183. waddch(dialog, ACS_RTEE);
  184. print_title(dialog, title, width);
  185. wattrset(dialog, dlg.dialog.atr);
  186. print_autowrap(dialog, prompt, width - 2, 1, 3);
  187. menu_width = width - 6;
  188. box_y = height - menu_height - 5;
  189. box_x = (width - menu_width) / 2 - 1;
  190. /* create new window for the menu */
  191. menu = subwin(dialog, menu_height, menu_width,
  192. y + box_y + 1, x + box_x + 1);
  193. keypad(menu, TRUE);
  194. /* draw a box around the menu items */
  195. draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
  196. dlg.menubox_border.atr, dlg.menubox.atr);
  197. if (menu_width >= 80)
  198. item_x = (menu_width - 70) / 2;
  199. else
  200. item_x = 4;
  201. /* Set choice to default item */
  202. item_foreach()
  203. if (selected && (selected == item_data()))
  204. choice = item_n();
  205. /* get the saved scroll info */
  206. scroll = *s_scroll;
  207. if ((scroll <= choice) && (scroll + max_choice > choice) &&
  208. (scroll >= 0) && (scroll + max_choice <= item_count())) {
  209. first_item = scroll;
  210. choice = choice - scroll;
  211. } else {
  212. scroll = 0;
  213. }
  214. if ((choice >= max_choice)) {
  215. if (choice >= item_count() - max_choice / 2)
  216. scroll = first_item = item_count() - max_choice;
  217. else
  218. scroll = first_item = choice - max_choice / 2;
  219. choice = choice - scroll;
  220. }
  221. /* Print the menu */
  222. for (i = 0; i < max_choice; i++) {
  223. print_item(first_item + i, i, i == choice);
  224. }
  225. wnoutrefresh(menu);
  226. print_arrows(dialog, item_count(), scroll,
  227. box_y, box_x + item_x + 1, menu_height);
  228. print_buttons(dialog, height, width, 0);
  229. wmove(menu, choice, item_x + 1);
  230. wrefresh(menu);
  231. while (key != KEY_ESC) {
  232. key = wgetch(menu);
  233. if (key < 256 && isalpha(key))
  234. key = tolower(key);
  235. if (strchr("ynmh", key))
  236. i = max_choice;
  237. else {
  238. for (i = choice + 1; i < max_choice; i++) {
  239. item_set(scroll + i);
  240. j = first_alpha(item_str(), "YyNnMmHh");
  241. if (key == tolower(item_str()[j]))
  242. break;
  243. }
  244. if (i == max_choice)
  245. for (i = 0; i < max_choice; i++) {
  246. item_set(scroll + i);
  247. j = first_alpha(item_str(), "YyNnMmHh");
  248. if (key == tolower(item_str()[j]))
  249. break;
  250. }
  251. }
  252. if (item_count() != 0 &&
  253. (i < max_choice ||
  254. key == KEY_UP || key == KEY_DOWN ||
  255. key == '-' || key == '+' ||
  256. key == KEY_PPAGE || key == KEY_NPAGE)) {
  257. /* Remove highligt of current item */
  258. print_item(scroll + choice, choice, FALSE);
  259. if (key == KEY_UP || key == '-') {
  260. if (choice < 2 && scroll) {
  261. /* Scroll menu down */
  262. do_scroll(menu, &scroll, -1);
  263. print_item(scroll, 0, FALSE);
  264. } else
  265. choice = MAX(choice - 1, 0);
  266. } else if (key == KEY_DOWN || key == '+') {
  267. print_item(scroll+choice, choice, FALSE);
  268. if ((choice > max_choice - 3) &&
  269. (scroll + max_choice < item_count())) {
  270. /* Scroll menu up */
  271. do_scroll(menu, &scroll, 1);
  272. print_item(scroll+max_choice - 1,
  273. max_choice - 1, FALSE);
  274. } else
  275. choice = MIN(choice + 1, max_choice - 1);
  276. } else if (key == KEY_PPAGE) {
  277. scrollok(menu, TRUE);
  278. for (i = 0; (i < max_choice); i++) {
  279. if (scroll > 0) {
  280. do_scroll(menu, &scroll, -1);
  281. print_item(scroll, 0, FALSE);
  282. } else {
  283. if (choice > 0)
  284. choice--;
  285. }
  286. }
  287. } else if (key == KEY_NPAGE) {
  288. for (i = 0; (i < max_choice); i++) {
  289. if (scroll + max_choice < item_count()) {
  290. do_scroll(menu, &scroll, 1);
  291. print_item(scroll+max_choice-1,
  292. max_choice - 1, FALSE);
  293. } else {
  294. if (choice + 1 < max_choice)
  295. choice++;
  296. }
  297. }
  298. } else
  299. choice = i;
  300. print_item(scroll + choice, choice, TRUE);
  301. print_arrows(dialog, item_count(), scroll,
  302. box_y, box_x + item_x + 1, menu_height);
  303. wnoutrefresh(dialog);
  304. wrefresh(menu);
  305. continue; /* wait for another key press */
  306. }
  307. switch (key) {
  308. case KEY_LEFT:
  309. case TAB:
  310. case KEY_RIGHT:
  311. button = ((key == KEY_LEFT ? --button : ++button) < 0)
  312. ? 4 : (button > 4 ? 0 : button);
  313. print_buttons(dialog, height, width, button);
  314. wrefresh(menu);
  315. break;
  316. case ' ':
  317. case 's':
  318. case 'y':
  319. case 'n':
  320. case 'm':
  321. case '/':
  322. case 'h':
  323. case '?':
  324. case 'z':
  325. case '\n':
  326. /* save scroll info */
  327. *s_scroll = scroll;
  328. delwin(menu);
  329. delwin(dialog);
  330. item_set(scroll + choice);
  331. item_set_selected(1);
  332. switch (key) {
  333. case 'h':
  334. case '?':
  335. return 2;
  336. case 's':
  337. case 'y':
  338. return 5;
  339. case 'n':
  340. return 6;
  341. case 'm':
  342. return 7;
  343. case ' ':
  344. return 8;
  345. case '/':
  346. return 9;
  347. case 'z':
  348. return 10;
  349. case '\n':
  350. return button;
  351. }
  352. return 0;
  353. case 'e':
  354. case 'x':
  355. key = KEY_ESC;
  356. break;
  357. case KEY_ESC:
  358. key = on_key_esc(menu);
  359. break;
  360. case KEY_RESIZE:
  361. on_key_resize();
  362. delwin(menu);
  363. delwin(dialog);
  364. goto do_resize;
  365. }
  366. }
  367. delwin(menu);
  368. delwin(dialog);
  369. return key; /* ESC pressed */
  370. }