menubox.c 10 KB

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