menubox.c 11 KB

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