checklist.c 8.8 KB

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