3
0

menubox.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 choice,
  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] = 0;
  69. j = first_alpha(menu_item, "YyNnMmHh");
  70. /* Clear 'residue' of last item */
  71. wattrset(win, menubox_attr);
  72. wmove(win, choice, 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 ? item_selected_attr : item_attr);
  83. mvwaddstr(win, choice, item_x, menu_item);
  84. if (hotkey) {
  85. wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
  86. mvwaddch(win, choice, item_x + j, menu_item[j]);
  87. }
  88. if (selected) {
  89. wmove(win, choice, item_x + 1);
  90. }
  91. free(menu_item);
  92. wrefresh(win);
  93. }
  94. #define print_item(index, choice, selected) \
  95. do {\
  96. int hotkey = (items[(index) * 2][0] != ':'); \
  97. do_print_item(menu, items[(index) * 2 + 1], choice, selected, hotkey); \
  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, uarrow_attr);
  110. waddch(win, ACS_UARROW);
  111. waddstr(win, "(-)");
  112. } else {
  113. wattrset(win, menubox_attr);
  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, darrow_attr);
  124. waddch(win, ACS_DARROW);
  125. waddstr(win, "(+)");
  126. } else {
  127. wattrset(win, menubox_border_attr);
  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, "Select", y, x, selected == 0);
  144. print_button(win, " Exit ", y, x + 12, selected == 1);
  145. print_button(win, " 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, int height, int width,
  163. int menu_height, const char *current, int item_no,
  164. const char *const *items)
  165. {
  166. int i, j, x, y, box_x, box_y;
  167. int key = 0, button = 0, scroll = 0, choice = 0;
  168. int first_item = 0, max_choice;
  169. WINDOW *dialog, *menu;
  170. FILE *f;
  171. max_choice = MIN(menu_height, item_no);
  172. /* center dialog box on screen */
  173. x = (COLS - width) / 2;
  174. y = (LINES - height) / 2;
  175. draw_shadow(stdscr, y, x, height, width);
  176. dialog = newwin(height, width, y, x);
  177. keypad(dialog, TRUE);
  178. draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
  179. wattrset(dialog, border_attr);
  180. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  181. for (i = 0; i < width - 2; i++)
  182. waddch(dialog, ACS_HLINE);
  183. wattrset(dialog, dialog_attr);
  184. wbkgdset(dialog, dialog_attr & A_COLOR);
  185. waddch(dialog, ACS_RTEE);
  186. print_title(dialog, title, width);
  187. wattrset(dialog, dialog_attr);
  188. print_autowrap(dialog, prompt, width - 2, 1, 3);
  189. menu_width = width - 6;
  190. box_y = height - menu_height - 5;
  191. box_x = (width - menu_width) / 2 - 1;
  192. /* create new window for the menu */
  193. menu = subwin(dialog, menu_height, menu_width,
  194. y + box_y + 1, x + box_x + 1);
  195. keypad(menu, TRUE);
  196. /* draw a box around the menu items */
  197. draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
  198. menubox_border_attr, menubox_attr);
  199. item_x = (menu_width - 70) / 2;
  200. /* Set choice to default item */
  201. for (i = 0; i < item_no; i++)
  202. if (strcmp(current, items[i * 2]) == 0)
  203. choice = i;
  204. /* get the scroll info from the temp file */
  205. if ((f = fopen("lxdialog.scrltmp", "r")) != NULL) {
  206. if ((fscanf(f, "%d\n", &scroll) == 1) && (scroll <= choice) &&
  207. (scroll + max_choice > choice) && (scroll >= 0) &&
  208. (scroll + max_choice <= item_no)) {
  209. first_item = scroll;
  210. choice = choice - scroll;
  211. fclose(f);
  212. } else {
  213. scroll = 0;
  214. remove("lxdialog.scrltmp");
  215. fclose(f);
  216. f = NULL;
  217. }
  218. }
  219. if ((choice >= max_choice) || (f == NULL && choice >= max_choice / 2)) {
  220. if (choice >= item_no - max_choice / 2)
  221. scroll = first_item = item_no - max_choice;
  222. else
  223. scroll = first_item = choice - max_choice / 2;
  224. choice = choice - scroll;
  225. }
  226. /* Print the menu */
  227. for (i = 0; i < max_choice; i++) {
  228. print_item(first_item + i, i, i == choice);
  229. }
  230. wnoutrefresh(menu);
  231. print_arrows(dialog, item_no, scroll,
  232. box_y, box_x + item_x + 1, menu_height);
  233. print_buttons(dialog, height, width, 0);
  234. wmove(menu, choice, item_x + 1);
  235. wrefresh(menu);
  236. while (key != ESC) {
  237. key = wgetch(menu);
  238. if (key < 256 && isalpha(key))
  239. key = tolower(key);
  240. if (strchr("ynmh", key))
  241. i = max_choice;
  242. else {
  243. for (i = choice + 1; i < max_choice; i++) {
  244. j = first_alpha(items[(scroll + i) * 2 + 1], "YyNnMmHh");
  245. if (key == tolower(items[(scroll + i) * 2 + 1][j]))
  246. break;
  247. }
  248. if (i == max_choice)
  249. for (i = 0; i < max_choice; i++) {
  250. j = first_alpha(items [(scroll + i) * 2 + 1], "YyNnMmHh");
  251. if (key == tolower(items[(scroll + i) * 2 + 1][j]))
  252. break;
  253. }
  254. }
  255. if (i < max_choice ||
  256. key == KEY_UP || key == KEY_DOWN ||
  257. key == '-' || key == '+' ||
  258. key == KEY_PPAGE || key == KEY_NPAGE) {
  259. /* Remove highligt of current item */
  260. print_item(scroll + choice, choice, FALSE);
  261. if (key == KEY_UP || key == '-') {
  262. if (choice < 2 && scroll) {
  263. /* Scroll menu down */
  264. do_scroll(menu, &scroll, -1);
  265. print_item(scroll, 0, FALSE);
  266. } else
  267. choice = MAX(choice - 1, 0);
  268. } else if (key == KEY_DOWN || key == '+') {
  269. print_item(scroll+choice, choice, FALSE);
  270. if ((choice > max_choice - 3) &&
  271. (scroll + max_choice < item_no)) {
  272. /* Scroll menu up */
  273. do_scroll(menu, &scroll, 1);
  274. print_item(scroll+max_choice - 1,
  275. max_choice - 1, FALSE);
  276. } else
  277. choice = MIN(choice + 1, max_choice - 1);
  278. } else if (key == KEY_PPAGE) {
  279. scrollok(menu, TRUE);
  280. for (i = 0; (i < max_choice); i++) {
  281. if (scroll > 0) {
  282. do_scroll(menu, &scroll, -1);
  283. print_item(scroll, 0, FALSE);
  284. } else {
  285. if (choice > 0)
  286. choice--;
  287. }
  288. }
  289. } else if (key == KEY_NPAGE) {
  290. for (i = 0; (i < max_choice); i++) {
  291. if (scroll + max_choice < item_no) {
  292. do_scroll(menu, &scroll, 1);
  293. print_item(scroll+max_choice-1,
  294. max_choice - 1, FALSE);
  295. } else {
  296. if (choice + 1 < max_choice)
  297. choice++;
  298. }
  299. }
  300. } else
  301. choice = i;
  302. print_item(scroll + choice, choice, TRUE);
  303. print_arrows(dialog, item_no, scroll,
  304. box_y, box_x + item_x + 1, menu_height);
  305. wnoutrefresh(dialog);
  306. wrefresh(menu);
  307. continue; /* wait for another key press */
  308. }
  309. switch (key) {
  310. case KEY_LEFT:
  311. case TAB:
  312. case KEY_RIGHT:
  313. button = ((key == KEY_LEFT ? --button : ++button) < 0)
  314. ? 2 : (button > 2 ? 0 : button);
  315. print_buttons(dialog, height, width, button);
  316. wrefresh(menu);
  317. break;
  318. case ' ':
  319. case 's':
  320. case 'y':
  321. case 'n':
  322. case 'm':
  323. case '/':
  324. /* save scroll info */
  325. if ((f = fopen("lxdialog.scrltmp", "w")) != NULL) {
  326. fprintf(f, "%d\n", scroll);
  327. fclose(f);
  328. }
  329. delwin(dialog);
  330. fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
  331. switch (key) {
  332. case 's':
  333. return 3;
  334. case 'y':
  335. return 3;
  336. case 'n':
  337. return 4;
  338. case 'm':
  339. return 5;
  340. case ' ':
  341. return 6;
  342. case '/':
  343. return 7;
  344. }
  345. return 0;
  346. case 'h':
  347. case '?':
  348. button = 2;
  349. case '\n':
  350. delwin(dialog);
  351. if (button == 2)
  352. fprintf(stderr, "%s \"%s\"\n",
  353. items[(scroll + choice) * 2],
  354. items[(scroll + choice) * 2 + 1] +
  355. first_alpha(items [(scroll + choice) * 2 + 1], ""));
  356. else
  357. fprintf(stderr, "%s\n",
  358. items[(scroll + choice) * 2]);
  359. remove("lxdialog.scrltmp");
  360. return button;
  361. case 'e':
  362. case 'x':
  363. key = ESC;
  364. case ESC:
  365. break;
  366. }
  367. }
  368. delwin(dialog);
  369. remove("lxdialog.scrltmp");
  370. return -1; /* ESC pressed */
  371. }