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.

360 lines
9.3 KiB

  1. /*
  2. * checklist.c -- implements the checklist box
  3. *
  4. * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5. * Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
  6. * Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
  7. * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
  8. * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include "dialog.h"
  25. static int list_width, check_x, item_x, checkflag;
  26. /*
  27. * Print list item
  28. */
  29. static void
  30. print_item (WINDOW * win, const char *item, int status,
  31. int choice, int selected)
  32. {
  33. int i;
  34. /* Clear 'residue' of last item */
  35. wattrset (win, menubox_attr);
  36. wmove (win, choice, 0);
  37. for (i = 0; i < list_width; i++)
  38. waddch (win, ' ');
  39. wmove (win, choice, check_x);
  40. wattrset (win, selected ? check_selected_attr : check_attr);
  41. if (checkflag == FLAG_CHECK)
  42. wprintw (win, "[%c]", status ? 'X' : ' ');
  43. else
  44. wprintw (win, "(%c)", status ? 'X' : ' ');
  45. wattrset (win, selected ? tag_selected_attr : tag_attr);
  46. mvwaddch(win, choice, item_x, item[0]);
  47. wattrset (win, selected ? item_selected_attr : item_attr);
  48. waddstr (win, (char *)item+1);
  49. if (selected) {
  50. wmove (win, choice, check_x+1);
  51. wrefresh (win);
  52. }
  53. }
  54. /*
  55. * Print the scroll indicators.
  56. */
  57. static void
  58. print_arrows (WINDOW * win, int choice, int item_no, int scroll,
  59. int y, int x, int height)
  60. {
  61. wmove(win, y, x);
  62. if (scroll > 0) {
  63. wattrset (win, uarrow_attr);
  64. waddch (win, ACS_UARROW);
  65. waddstr (win, "(-)");
  66. }
  67. else {
  68. wattrset (win, menubox_attr);
  69. waddch (win, ACS_HLINE);
  70. waddch (win, ACS_HLINE);
  71. waddch (win, ACS_HLINE);
  72. waddch (win, ACS_HLINE);
  73. }
  74. y = y + height + 1;
  75. wmove(win, y, x);
  76. if ((height < item_no) && (scroll + choice < item_no - 1)) {
  77. wattrset (win, darrow_attr);
  78. waddch (win, ACS_DARROW);
  79. waddstr (win, "(+)");
  80. }
  81. else {
  82. wattrset (win, menubox_border_attr);
  83. waddch (win, ACS_HLINE);
  84. waddch (win, ACS_HLINE);
  85. waddch (win, ACS_HLINE);
  86. waddch (win, ACS_HLINE);
  87. }
  88. }
  89. /*
  90. * Display the termination buttons
  91. */
  92. static void
  93. print_buttons( WINDOW *dialog, int height, int width, int selected)
  94. {
  95. int x = width / 2 - 5;
  96. int y = height - 2;
  97. print_button (dialog, "Select", y, x, 1);
  98. wmove(dialog, y, x+1);
  99. wrefresh (dialog);
  100. }
  101. /*
  102. * Display a dialog box with a list of options that can be turned on or off
  103. * The `flag' parameter is used to select between radiolist and checklist.
  104. */
  105. int
  106. dialog_checklist (const char *title, const char *prompt, int height, int width,
  107. int list_height, int item_no, const char * const * items, int flag)
  108. {
  109. int i, x, y, box_x, box_y;
  110. int key = 0, button = 0, choice = 0, scroll = 0, max_choice, *status;
  111. WINDOW *dialog, *list;
  112. checkflag = flag;
  113. /* Allocate space for storing item on/off status */
  114. if ((status = malloc (sizeof (int) * item_no)) == NULL) {
  115. endwin ();
  116. fprintf (stderr,
  117. "\nCan't allocate memory in dialog_checklist().\n");
  118. exit (-1);
  119. }
  120. /* Initializes status */
  121. for (i = 0; i < item_no; i++) {
  122. status[i] = !strcasecmp (items[i * 3 + 2], "on");
  123. if (!choice && status[i])
  124. choice = i;
  125. }
  126. max_choice = MIN (list_height, item_no);
  127. /* center dialog box on screen */
  128. x = (COLS - width) / 2;
  129. y = (LINES - height) / 2;
  130. draw_shadow (stdscr, y, x, height, width);
  131. dialog = newwin (height, width, y, x);
  132. keypad (dialog, TRUE);
  133. draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  134. wattrset (dialog, border_attr);
  135. mvwaddch (dialog, height-3, 0, ACS_LTEE);
  136. for (i = 0; i < width - 2; i++)
  137. waddch (dialog, ACS_HLINE);
  138. wattrset (dialog, dialog_attr);
  139. waddch (dialog, ACS_RTEE);
  140. if (title != NULL && strlen(title) >= width-2 ) {
  141. /* truncate long title -- mec */
  142. char * title2 = malloc(width-2+1);
  143. memcpy( title2, title, width-2 );
  144. title2[width-2] = '\0';
  145. title = title2;
  146. }
  147. if (title != NULL) {
  148. wattrset (dialog, title_attr);
  149. mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
  150. waddstr (dialog, (char *)title);
  151. waddch (dialog, ' ');
  152. }
  153. wattrset (dialog, dialog_attr);
  154. print_autowrap (dialog, prompt, width - 2, 1, 3);
  155. list_width = width - 6;
  156. box_y = height - list_height - 5;
  157. box_x = (width - list_width) / 2 - 1;
  158. /* create new window for the list */
  159. list = subwin (dialog, list_height, list_width, y+box_y+1, x+box_x+1);
  160. keypad (list, TRUE);
  161. /* draw a box around the list items */
  162. draw_box (dialog, box_y, box_x, list_height + 2, list_width + 2,
  163. menubox_border_attr, menubox_attr);
  164. /* Find length of longest item in order to center checklist */
  165. check_x = 0;
  166. for (i = 0; i < item_no; i++)
  167. check_x = MAX (check_x, + strlen (items[i * 3 + 1]) + 4);
  168. check_x = (list_width - check_x) / 2;
  169. item_x = check_x + 4;
  170. if (choice >= list_height) {
  171. scroll = choice - list_height + 1;
  172. choice -= scroll;
  173. }
  174. /* Print the list */
  175. for (i = 0; i < max_choice; i++) {
  176. print_item (list, items[(scroll+i) * 3 + 1],
  177. status[i+scroll], i, i == choice);
  178. }
  179. print_arrows(dialog, choice, item_no, scroll,
  180. box_y, box_x + check_x + 5, list_height);
  181. print_buttons(dialog, height, width, 0);
  182. wnoutrefresh (list);
  183. wnoutrefresh (dialog);
  184. doupdate ();
  185. while (key != ESC) {
  186. key = wgetch (dialog);
  187. for (i = 0; i < max_choice; i++)
  188. if (toupper(key) == toupper(items[(scroll+i)*3+1][0]))
  189. break;
  190. if ( i < max_choice || key == KEY_UP || key == KEY_DOWN ||
  191. key == '+' || key == '-' ) {
  192. if (key == KEY_UP || key == '-') {
  193. if (!choice) {
  194. if (!scroll)
  195. continue;
  196. /* Scroll list down */
  197. if (list_height > 1) {
  198. /* De-highlight current first item */
  199. print_item (list, items[scroll * 3 + 1],
  200. status[scroll], 0, FALSE);
  201. scrollok (list, TRUE);
  202. wscrl (list, -1);
  203. scrollok (list, FALSE);
  204. }
  205. scroll--;
  206. print_item (list, items[scroll * 3 + 1],
  207. status[scroll], 0, TRUE);
  208. wnoutrefresh (list);
  209. print_arrows(dialog, choice, item_no, scroll,
  210. box_y, box_x + check_x + 5, list_height);
  211. wrefresh (dialog);
  212. continue; /* wait for another key press */
  213. } else
  214. i = choice - 1;
  215. } else if (key == KEY_DOWN || key == '+') {
  216. if (choice == max_choice - 1) {
  217. if (scroll + choice >= item_no - 1)
  218. continue;
  219. /* Scroll list up */
  220. if (list_height > 1) {
  221. /* De-highlight current last item before scrolling up */
  222. print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
  223. status[scroll + max_choice - 1],
  224. max_choice - 1, FALSE);
  225. scrollok (list, TRUE);
  226. scroll (list);
  227. scrollok (list, FALSE);
  228. }
  229. scroll++;
  230. print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
  231. status[scroll + max_choice - 1],
  232. max_choice - 1, TRUE);
  233. wnoutrefresh (list);
  234. print_arrows(dialog, choice, item_no, scroll,
  235. box_y, box_x + check_x + 5, list_height);
  236. wrefresh (dialog);
  237. continue; /* wait for another key press */
  238. } else
  239. i = choice + 1;
  240. }
  241. if (i != choice) {
  242. /* De-highlight current item */
  243. print_item (list, items[(scroll + choice) * 3 + 1],
  244. status[scroll + choice], choice, FALSE);
  245. /* Highlight new item */
  246. choice = i;
  247. print_item (list, items[(scroll + choice) * 3 + 1],
  248. status[scroll + choice], choice, TRUE);
  249. wnoutrefresh (list);
  250. wrefresh (dialog);
  251. }
  252. continue; /* wait for another key press */
  253. }
  254. switch (key) {
  255. case 'H':
  256. case 'h':
  257. case '?':
  258. delwin (dialog);
  259. free (status);
  260. return 1;
  261. case 'S':
  262. case 's':
  263. case ' ':
  264. case '\n':
  265. if (!button) {
  266. if (flag == FLAG_CHECK) {
  267. status[scroll + choice] = !status[scroll + choice];
  268. wmove (list, choice, check_x);
  269. wattrset (list, check_selected_attr);
  270. wprintw (list, "[%c]", status[scroll + choice] ? 'X' : ' ');
  271. } else {
  272. if (!status[scroll + choice]) {
  273. for (i = 0; i < item_no; i++)
  274. status[i] = 0;
  275. status[scroll + choice] = 1;
  276. for (i = 0; i < max_choice; i++)
  277. print_item (list, items[(scroll + i) * 3 + 1],
  278. status[scroll + i], i, i == choice);
  279. }
  280. }
  281. wnoutrefresh (list);
  282. wrefresh (dialog);
  283. for (i = 0; i < item_no; i++) {
  284. if (status[i]) {
  285. if (flag == FLAG_CHECK) {
  286. fprintf (stderr, "\"%s\" ", items[i * 3]);
  287. } else {
  288. fprintf (stderr, "%s", items[i * 3]);
  289. }
  290. }
  291. }
  292. }
  293. delwin (dialog);
  294. free (status);
  295. return button;
  296. case 'X':
  297. case 'x':
  298. key = ESC;
  299. case ESC:
  300. break;
  301. }
  302. /* Now, update everything... */
  303. doupdate ();
  304. }
  305. delwin (dialog);
  306. free (status);
  307. return -1; /* ESC pressed */
  308. }