menubox.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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
  63. print_item (WINDOW * win, const char *item, int choice, int selected, int hotkey)
  64. {
  65. int j;
  66. char menu_item[menu_width+1];
  67. strncpy(menu_item, item, menu_width);
  68. menu_item[menu_width] = 0;
  69. j = first_alpha(menu_item, "YyNnMm");
  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 0
  85. if (hotkey) {
  86. wattrset (win, selected ? tag_key_selected_attr : tag_key_attr);
  87. mvwaddch(win, choice, item_x+j, menu_item[j]);
  88. }
  89. #endif
  90. if (selected) {
  91. wmove (win, choice, item_x+1);
  92. wrefresh (win);
  93. }
  94. }
  95. /*
  96. * Print the scroll indicators.
  97. */
  98. static void
  99. print_arrows (WINDOW * win, int item_no, int scroll,
  100. int y, int x, int height)
  101. {
  102. int cur_y, cur_x;
  103. getyx(win, cur_y, cur_x);
  104. wmove(win, y, x);
  105. if (scroll > 0) {
  106. wattrset (win, uarrow_attr);
  107. waddch (win, ACS_UARROW);
  108. waddstr (win, "(-)");
  109. }
  110. else {
  111. wattrset (win, menubox_attr);
  112. waddch (win, ACS_HLINE);
  113. waddch (win, ACS_HLINE);
  114. waddch (win, ACS_HLINE);
  115. waddch (win, ACS_HLINE);
  116. }
  117. y = y + height + 1;
  118. wmove(win, y, x);
  119. if ((height < item_no) && (scroll + height < item_no)) {
  120. wattrset (win, darrow_attr);
  121. waddch (win, ACS_DARROW);
  122. waddstr (win, "(+)");
  123. }
  124. else {
  125. wattrset (win, menubox_border_attr);
  126. waddch (win, ACS_HLINE);
  127. waddch (win, ACS_HLINE);
  128. waddch (win, ACS_HLINE);
  129. waddch (win, ACS_HLINE);
  130. }
  131. wmove(win, cur_y, cur_x);
  132. }
  133. /*
  134. * Display the termination buttons.
  135. */
  136. static void
  137. print_buttons (WINDOW *win, int height, int width, int selected)
  138. {
  139. int x = width / 2 - 16;
  140. int y = height - 2;
  141. print_button (win, "Select", y, x, selected == 0);
  142. print_button (win, " Exit ", y, x + 12, selected == 1);
  143. print_button (win, " Help ", y, x + 24, selected == 2);
  144. wmove(win, y, x+1+12*selected);
  145. wrefresh (win);
  146. }
  147. /*
  148. * Display a menu for choosing among a number of options
  149. */
  150. int
  151. dialog_menu (const char *title, const char *prompt, int height, int width,
  152. int menu_height, const char *current, int item_no,
  153. struct dialog_list_item ** items)
  154. {
  155. int i, j, x, y, box_x, box_y;
  156. int key = 0, button = 0, scroll = 0, choice = 0, first_item = 0, max_choice;
  157. WINDOW *dialog, *menu;
  158. FILE *f;
  159. max_choice = MIN (menu_height, item_no);
  160. /* center dialog box on screen */
  161. x = (COLS - width) / 2;
  162. y = (LINES - height) / 2;
  163. draw_shadow (stdscr, y, x, height, width);
  164. dialog = newwin (height, width, y, x);
  165. keypad (dialog, TRUE);
  166. draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  167. wattrset (dialog, border_attr);
  168. mvwaddch (dialog, height - 3, 0, ACS_LTEE);
  169. for (i = 0; i < width - 2; i++)
  170. waddch (dialog, ACS_HLINE);
  171. wattrset (dialog, dialog_attr);
  172. wbkgdset (dialog, dialog_attr & A_COLOR);
  173. waddch (dialog, ACS_RTEE);
  174. if (title != NULL && strlen(title) >= width-2 ) {
  175. /* truncate long title -- mec */
  176. char * title2 = malloc(width-2+1);
  177. memcpy( title2, title, width-2 );
  178. title2[width-2] = '\0';
  179. title = title2;
  180. }
  181. if (title != NULL) {
  182. wattrset (dialog, title_attr);
  183. mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
  184. waddstr (dialog, (char *)title);
  185. waddch (dialog, ' ');
  186. }
  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. /*
  200. * Find length of longest item in order to center menu.
  201. * Set 'choice' to default item.
  202. */
  203. item_x = 0;
  204. for (i = 0; i < item_no; i++) {
  205. item_x = MAX (item_x, MIN(menu_width, strlen (items[i]->name) + 2));
  206. if (strcmp(current, items[i]->tag) == 0) choice = i;
  207. }
  208. item_x = (menu_width - item_x) / 2;
  209. /* get the scroll info from the temp file */
  210. if ( (f=fopen("lxdialog.scrltmp","r")) != NULL ) {
  211. if ( (fscanf(f,"%d\n",&scroll) == 1) && (scroll <= choice) &&
  212. (scroll+max_choice > choice) && (scroll >= 0) &&
  213. (scroll+max_choice <= item_no) ) {
  214. first_item = scroll;
  215. choice = choice - scroll;
  216. fclose(f);
  217. } else {
  218. scroll=0;
  219. remove("lxdialog.scrltmp");
  220. fclose(f);
  221. f=NULL;
  222. }
  223. }
  224. if ( (choice >= max_choice) || (f==NULL && choice >= max_choice/2) ) {
  225. if (choice >= item_no-max_choice/2)
  226. scroll = first_item = item_no-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 (menu, items[first_item + i]->name, i, i == choice,
  234. (items[first_item + i]->tag[0] != ':'));
  235. }
  236. wnoutrefresh (menu);
  237. print_arrows(dialog, item_no, 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 != ESC) {
  243. key = wgetch(menu);
  244. if (key < 256 && isalpha(key)) key = tolower(key);
  245. if (strchr("ynm", key))
  246. i = max_choice;
  247. else {
  248. for (i = choice+1; i < max_choice; i++) {
  249. j = first_alpha(items[scroll + i]->name, "YyNnMm>");
  250. if (key == tolower(items[scroll + i]->name[j]))
  251. break;
  252. }
  253. if (i == max_choice)
  254. for (i = 0; i < max_choice; i++) {
  255. j = first_alpha(items[scroll + i]->name, "YyNnMm>");
  256. if (key == tolower(items[scroll + i]->name[j]))
  257. break;
  258. }
  259. }
  260. if (i < max_choice ||
  261. key == KEY_UP || key == KEY_DOWN ||
  262. key == '-' || key == '+' ||
  263. key == KEY_PPAGE || key == KEY_NPAGE) {
  264. print_item (menu, items[scroll + choice]->name, choice, FALSE,
  265. (items[scroll + choice]->tag[0] != ':'));
  266. if (key == KEY_UP || key == '-') {
  267. if (choice < 2 && scroll) {
  268. /* Scroll menu down */
  269. scrollok (menu, TRUE);
  270. wscrl (menu, -1);
  271. scrollok (menu, FALSE);
  272. scroll--;
  273. print_item (menu, items[scroll]->name, 0, FALSE,
  274. (items[scroll]->tag[0] != ':'));
  275. } else
  276. choice = MAX(choice - 1, 0);
  277. } else if (key == KEY_DOWN || key == '+') {
  278. print_item (menu, items[scroll + choice]->name, choice, FALSE,
  279. (items[scroll + choice]->tag[0] != ':'));
  280. if ((choice > max_choice-3) &&
  281. (scroll + max_choice < item_no)
  282. ) {
  283. /* Scroll menu up */
  284. scrollok (menu, TRUE);
  285. scroll (menu);
  286. scrollok (menu, FALSE);
  287. scroll++;
  288. print_item (menu, items[scroll + max_choice - 1]->name,
  289. max_choice-1, FALSE,
  290. (items[scroll + max_choice - 1]->tag[0] != ':'));
  291. } else
  292. choice = MIN(choice+1, max_choice-1);
  293. } else if (key == KEY_PPAGE) {
  294. scrollok (menu, TRUE);
  295. for (i=0; (i < max_choice); i++) {
  296. if (scroll > 0) {
  297. wscrl (menu, -1);
  298. scroll--;
  299. print_item (menu, items[scroll]->name, 0, FALSE,
  300. (items[scroll]->tag[0] != ':'));
  301. } else {
  302. if (choice > 0)
  303. choice--;
  304. }
  305. }
  306. scrollok (menu, FALSE);
  307. } else if (key == KEY_NPAGE) {
  308. for (i=0; (i < max_choice); i++) {
  309. if (scroll+max_choice < item_no) {
  310. scrollok (menu, TRUE);
  311. scroll(menu);
  312. scrollok (menu, FALSE);
  313. scroll++;
  314. print_item (menu, items[scroll + max_choice - 1]->name,
  315. max_choice-1, FALSE,
  316. (items[scroll + max_choice - 1]->tag[0] != ':'));
  317. } else {
  318. if (choice+1 < max_choice)
  319. choice++;
  320. }
  321. }
  322. } else
  323. choice = i;
  324. print_item (menu, items[scroll + choice]->name, choice, TRUE,
  325. (items[scroll + choice]->tag[0] != ':'));
  326. print_arrows(dialog, item_no, scroll,
  327. box_y, box_x+item_x+1, menu_height);
  328. wnoutrefresh (dialog);
  329. wrefresh (menu);
  330. continue; /* wait for another key press */
  331. }
  332. switch (key) {
  333. case KEY_LEFT:
  334. case TAB:
  335. case KEY_RIGHT:
  336. button = ((key == KEY_LEFT ? --button : ++button) < 0)
  337. ? 2 : (button > 2 ? 0 : button);
  338. print_buttons(dialog, height, width, button);
  339. wrefresh (menu);
  340. break;
  341. case ' ':
  342. case 's':
  343. case 'y':
  344. case 'n':
  345. case 'm':
  346. /* save scroll info */
  347. if ( (f=fopen("lxdialog.scrltmp","w")) != NULL ) {
  348. fprintf(f,"%d\n",scroll);
  349. fclose(f);
  350. }
  351. delwin (dialog);
  352. items[scroll + choice]->selected = 1;
  353. switch (key) {
  354. case 's': return 3;
  355. case 'y': return 3;
  356. case 'n': return 4;
  357. case 'm': return 5;
  358. case ' ': return 6;
  359. }
  360. return 0;
  361. case 'h':
  362. case '?':
  363. button = 2;
  364. case '\n':
  365. delwin (dialog);
  366. items[scroll + choice]->selected = 1;
  367. remove("lxdialog.scrltmp");
  368. return button;
  369. case 'e':
  370. case 'x':
  371. key = ESC;
  372. case ESC:
  373. break;
  374. }
  375. }
  376. delwin (dialog);
  377. remove("lxdialog.scrltmp");
  378. return -1; /* ESC pressed */
  379. }