menubox.c 11 KB

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