checklist.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. wattrset (win, selected ? tag_selected_attr : tag_attr);
  45. mvwaddch(win, choice, item_x, item[0]);
  46. wattrset (win, selected ? item_selected_attr : item_attr);
  47. waddstr (win, (char *)item+1);
  48. if (selected) {
  49. wmove (win, choice, check_x+1);
  50. wrefresh (win);
  51. }
  52. }
  53. /*
  54. * Print the scroll indicators.
  55. */
  56. static void
  57. print_arrows (WINDOW * win, int choice, int item_no, int scroll,
  58. int y, int x, int height)
  59. {
  60. wmove(win, y, x);
  61. if (scroll > 0) {
  62. wattrset (win, uarrow_attr);
  63. waddch (win, ACS_UARROW);
  64. waddstr (win, "(-)");
  65. }
  66. else {
  67. wattrset (win, menubox_attr);
  68. waddch (win, ACS_HLINE);
  69. waddch (win, ACS_HLINE);
  70. waddch (win, ACS_HLINE);
  71. waddch (win, ACS_HLINE);
  72. }
  73. y = y + height + 1;
  74. wmove(win, y, x);
  75. if ((height < item_no) && (scroll + choice < item_no - 1)) {
  76. wattrset (win, darrow_attr);
  77. waddch (win, ACS_DARROW);
  78. waddstr (win, "(+)");
  79. }
  80. else {
  81. wattrset (win, menubox_border_attr);
  82. waddch (win, ACS_HLINE);
  83. waddch (win, ACS_HLINE);
  84. waddch (win, ACS_HLINE);
  85. waddch (win, ACS_HLINE);
  86. }
  87. }
  88. /*
  89. * Display the termination buttons
  90. */
  91. static void
  92. print_buttons( WINDOW *dialog, int height, int width, int selected)
  93. {
  94. int x = width / 2 - 11;
  95. int y = height - 2;
  96. print_button (dialog, "Select", y, x, selected == 0);
  97. print_button (dialog, " Help ", y, x + 14, selected == 1);
  98. wmove(dialog, y, x+1 + 14*selected);
  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, struct dialog_list_item ** items,
  108. int flag)
  109. {
  110. int i, x, y, box_x, box_y;
  111. int key = 0, button = 0, choice = 0, scroll = 0, max_choice, *status;
  112. WINDOW *dialog, *list;
  113. checkflag = flag;
  114. /* Allocate space for storing item on/off status */
  115. if ((status = malloc (sizeof (int) * item_no)) == NULL) {
  116. endwin ();
  117. fprintf (stderr,
  118. "\nCan't allocate memory in dialog_checklist().\n");
  119. exit (-1);
  120. }
  121. /* Initializes status */
  122. for (i = 0; i < item_no; i++) {
  123. status[i] = items[i]->selected;
  124. if (!choice && status[i])
  125. choice = i;
  126. }
  127. max_choice = MIN (list_height, item_no);
  128. /* center dialog box on screen */
  129. x = (COLS - width) / 2;
  130. y = (LINES - height) / 2;
  131. draw_shadow (stdscr, y, x, height, width);
  132. dialog = newwin (height, width, y, x);
  133. keypad (dialog, TRUE);
  134. draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  135. wattrset (dialog, border_attr);
  136. mvwaddch (dialog, height-3, 0, ACS_LTEE);
  137. for (i = 0; i < width - 2; i++)
  138. waddch (dialog, ACS_HLINE);
  139. wattrset (dialog, dialog_attr);
  140. waddch (dialog, ACS_RTEE);
  141. if (title != NULL && strlen(title) >= width-2 ) {
  142. /* truncate long title -- mec */
  143. char * title2 = malloc(width-2+1);
  144. memcpy( title2, title, width-2 );
  145. title2[width-2] = '\0';
  146. title = title2;
  147. }
  148. if (title != NULL) {
  149. wattrset (dialog, title_attr);
  150. mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
  151. waddstr (dialog, (char *)title);
  152. waddch (dialog, ' ');
  153. }
  154. wattrset (dialog, dialog_attr);
  155. print_autowrap (dialog, prompt, width - 2, 1, 3);
  156. list_width = width - 6;
  157. box_y = height - list_height - 5;
  158. box_x = (width - list_width) / 2 - 1;
  159. /* create new window for the list */
  160. list = subwin (dialog, list_height, list_width, y+box_y+1, x+box_x+1);
  161. keypad (list, TRUE);
  162. /* draw a box around the list items */
  163. draw_box (dialog, box_y, box_x, list_height + 2, list_width + 2,
  164. menubox_border_attr, menubox_attr);
  165. /* Find length of longest item in order to center checklist */
  166. check_x = 0;
  167. for (i = 0; i < item_no; i++)
  168. check_x = MAX (check_x, + strlen (items[i]->name) + 4);
  169. check_x = (list_width - check_x) / 2;
  170. item_x = check_x + 4;
  171. if (choice >= list_height) {
  172. scroll = choice - list_height + 1;
  173. choice -= scroll;
  174. }
  175. /* Print the list */
  176. for (i = 0; i < max_choice; i++) {
  177. print_item (list, items[scroll + i]->name,
  178. status[i+scroll], i, i == choice);
  179. }
  180. print_arrows(dialog, choice, item_no, scroll,
  181. box_y, box_x + check_x + 5, list_height);
  182. print_buttons(dialog, height, width, 0);
  183. wnoutrefresh (list);
  184. wnoutrefresh (dialog);
  185. doupdate ();
  186. while (key != ESC) {
  187. key = wgetch (dialog);
  188. for (i = 0; i < max_choice; i++)
  189. if (toupper(key) == toupper(items[scroll + i]->name[0]))
  190. break;
  191. if ( i < max_choice || key == KEY_UP || key == KEY_DOWN ||
  192. key == '+' || key == '-' ) {
  193. if (key == KEY_UP || key == '-') {
  194. if (!choice) {
  195. if (!scroll)
  196. continue;
  197. /* Scroll list down */
  198. if (list_height > 1) {
  199. /* De-highlight current first item */
  200. print_item (list, items[scroll]->name,
  201. status[scroll], 0, FALSE);
  202. scrollok (list, TRUE);
  203. wscrl (list, -1);
  204. scrollok (list, FALSE);
  205. }
  206. scroll--;
  207. print_item (list, items[scroll]->name,
  208. status[scroll], 0, TRUE);
  209. wnoutrefresh (list);
  210. print_arrows(dialog, choice, item_no, scroll,
  211. box_y, box_x + check_x + 5, list_height);
  212. wrefresh (dialog);
  213. continue; /* wait for another key press */
  214. } else
  215. i = choice - 1;
  216. } else if (key == KEY_DOWN || key == '+') {
  217. if (choice == max_choice - 1) {
  218. if (scroll + choice >= item_no - 1)
  219. continue;
  220. /* Scroll list up */
  221. if (list_height > 1) {
  222. /* De-highlight current last item before scrolling up */
  223. print_item (list, items[scroll + max_choice - 1]->name,
  224. status[scroll + max_choice - 1],
  225. max_choice - 1, FALSE);
  226. scrollok (list, TRUE);
  227. scroll (list);
  228. scrollok (list, FALSE);
  229. }
  230. scroll++;
  231. print_item (list, items[scroll + max_choice - 1]->name,
  232. status[scroll + max_choice - 1],
  233. max_choice - 1, TRUE);
  234. wnoutrefresh (list);
  235. print_arrows(dialog, choice, item_no, scroll,
  236. box_y, box_x + check_x + 5, list_height);
  237. wrefresh (dialog);
  238. continue; /* wait for another key press */
  239. } else
  240. i = choice + 1;
  241. }
  242. if (i != choice) {
  243. /* De-highlight current item */
  244. print_item (list, items[scroll + choice]->name,
  245. status[scroll + choice], choice, FALSE);
  246. /* Highlight new item */
  247. choice = i;
  248. print_item (list, items[scroll + choice]->name,
  249. status[scroll + choice], choice, TRUE);
  250. wnoutrefresh (list);
  251. wrefresh (dialog);
  252. }
  253. continue; /* wait for another key press */
  254. }
  255. switch (key) {
  256. case 'H':
  257. case 'h':
  258. case '?':
  259. delwin (dialog);
  260. free (status);
  261. return 1;
  262. case TAB:
  263. case KEY_LEFT:
  264. case KEY_RIGHT:
  265. button = ((key == KEY_LEFT ? --button : ++button) < 0)
  266. ? 1 : (button > 1 ? 0 : button);
  267. print_buttons(dialog, height, width, button);
  268. wrefresh (dialog);
  269. break;
  270. case 'S':
  271. case 's':
  272. case ' ':
  273. case '\n':
  274. if (!button) {
  275. if (flag == FLAG_CHECK) {
  276. status[scroll + choice] = !status[scroll + choice];
  277. wmove (list, choice, check_x);
  278. wattrset (list, check_selected_attr);
  279. wprintw (list, "[%c]", status[scroll + choice] ? 'X' : ' ');
  280. } else {
  281. if (!status[scroll + choice]) {
  282. for (i = 0; i < item_no; i++)
  283. status[i] = 0;
  284. status[scroll + choice] = 1;
  285. for (i = 0; i < max_choice; i++)
  286. print_item (list, items[scroll + i]->name,
  287. status[scroll + i], i, i == choice);
  288. }
  289. }
  290. wnoutrefresh (list);
  291. wrefresh (dialog);
  292. for (i = 0; i < item_no; i++) {
  293. items[i]->selected = status[i];
  294. }
  295. }
  296. delwin (dialog);
  297. free (status);
  298. return button;
  299. case 'X':
  300. case 'x':
  301. key = ESC;
  302. case ESC:
  303. break;
  304. }
  305. /* Now, update everything... */
  306. doupdate ();
  307. }
  308. delwin (dialog);
  309. free (status);
  310. return -1; /* ESC pressed */
  311. }