checklist.c 9.8 KB

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