menubox.c 11 KB

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