mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
12 KiB

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