checklist.c 8.6 KB

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