1
0

checklist.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, int choice, int selected)
  29. {
  30. int i;
  31. /* Clear 'residue' of last item */
  32. wattrset(win, dlg.menubox.atr);
  33. wmove(win, choice, 0);
  34. for (i = 0; i < list_width; i++)
  35. waddch(win, ' ');
  36. wmove(win, choice, check_x);
  37. wattrset(win, selected ? dlg.check_selected.atr
  38. : dlg.check.atr);
  39. if (!item_is_tag(':'))
  40. wprintw(win, "(%c)", item_is_tag('X') ? 'X' : ' ');
  41. wattrset(win, selected ? dlg.tag_selected.atr : dlg.tag.atr);
  42. mvwaddch(win, choice, item_x, item_str()[0]);
  43. wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
  44. waddstr(win, (char *)item_str() + 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, gettext("Select"), y, x, selected == 0);
  90. print_button(dialog, gettext(" 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)
  100. {
  101. int i, x, y, box_x, box_y;
  102. int key = 0, button = 0, choice = 0, scroll = 0, max_choice;
  103. WINDOW *dialog, *list;
  104. /* which item to highlight */
  105. item_foreach() {
  106. if (item_is_tag('X'))
  107. choice = item_n();
  108. if (item_is_selected()) {
  109. choice = item_n();
  110. break;
  111. }
  112. }
  113. do_resize:
  114. if (getmaxy(stdscr) < (height + 6))
  115. return -ERRDISPLAYTOOSMALL;
  116. if (getmaxx(stdscr) < (width + 6))
  117. return -ERRDISPLAYTOOSMALL;
  118. max_choice = MIN(list_height, item_count());
  119. /* center dialog box on screen */
  120. x = (COLS - width) / 2;
  121. y = (LINES - height) / 2;
  122. draw_shadow(stdscr, y, x, height, width);
  123. dialog = newwin(height, width, y, x);
  124. keypad(dialog, TRUE);
  125. draw_box(dialog, 0, 0, height, width,
  126. dlg.dialog.atr, dlg.border.atr);
  127. wattrset(dialog, dlg.border.atr);
  128. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  129. for (i = 0; i < width - 2; i++)
  130. waddch(dialog, ACS_HLINE);
  131. wattrset(dialog, dlg.dialog.atr);
  132. waddch(dialog, ACS_RTEE);
  133. print_title(dialog, title, width);
  134. wattrset(dialog, dlg.dialog.atr);
  135. print_autowrap(dialog, prompt, width - 2, 1, 3);
  136. list_width = width - 6;
  137. box_y = height - list_height - 5;
  138. box_x = (width - list_width) / 2 - 1;
  139. /* create new window for the list */
  140. list = subwin(dialog, list_height, list_width, y + box_y + 1,
  141. x + box_x + 1);
  142. keypad(list, TRUE);
  143. /* draw a box around the list items */
  144. draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2,
  145. dlg.menubox_border.atr, dlg.menubox.atr);
  146. /* Find length of longest item in order to center checklist */
  147. check_x = 0;
  148. item_foreach()
  149. check_x = MAX(check_x, strlen(item_str()) + 4);
  150. check_x = (list_width - check_x) / 2;
  151. item_x = check_x + 4;
  152. if (choice >= list_height) {
  153. scroll = choice - list_height + 1;
  154. choice -= scroll;
  155. }
  156. /* Print the list */
  157. for (i = 0; i < max_choice; i++) {
  158. item_set(scroll + i);
  159. print_item(list, i, i == choice);
  160. }
  161. print_arrows(dialog, choice, item_count(), scroll,
  162. box_y, box_x + check_x + 5, list_height);
  163. print_buttons(dialog, height, width, 0);
  164. wnoutrefresh(dialog);
  165. wnoutrefresh(list);
  166. doupdate();
  167. while (key != KEY_ESC) {
  168. key = wgetch(dialog);
  169. for (i = 0; i < max_choice; i++) {
  170. item_set(i + scroll);
  171. if (toupper(key) == toupper(item_str()[0]))
  172. break;
  173. }
  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. item_set(scroll);
  184. print_item(list, 0, FALSE);
  185. scrollok(list, TRUE);
  186. wscrl(list, -1);
  187. scrollok(list, FALSE);
  188. }
  189. scroll--;
  190. item_set(scroll);
  191. print_item(list, 0, TRUE);
  192. print_arrows(dialog, choice, item_count(),
  193. scroll, box_y, box_x + check_x + 5, list_height);
  194. wnoutrefresh(dialog);
  195. wrefresh(list);
  196. continue; /* wait for another key press */
  197. } else
  198. i = choice - 1;
  199. } else if (key == KEY_DOWN || key == '+') {
  200. if (choice == max_choice - 1) {
  201. if (scroll + choice >= item_count() - 1)
  202. continue;
  203. /* Scroll list up */
  204. if (list_height > 1) {
  205. /* De-highlight current last item before scrolling up */
  206. item_set(scroll + max_choice - 1);
  207. print_item(list,
  208. max_choice - 1,
  209. FALSE);
  210. scrollok(list, TRUE);
  211. wscrl(list, 1);
  212. scrollok(list, FALSE);
  213. }
  214. scroll++;
  215. item_set(scroll + max_choice - 1);
  216. print_item(list, max_choice - 1, TRUE);
  217. print_arrows(dialog, choice, item_count(),
  218. scroll, box_y, box_x + check_x + 5, list_height);
  219. wnoutrefresh(dialog);
  220. wrefresh(list);
  221. continue; /* wait for another key press */
  222. } else
  223. i = choice + 1;
  224. }
  225. if (i != choice) {
  226. /* De-highlight current item */
  227. item_set(scroll + choice);
  228. print_item(list, choice, FALSE);
  229. /* Highlight new item */
  230. choice = i;
  231. item_set(scroll + choice);
  232. print_item(list, choice, TRUE);
  233. wnoutrefresh(dialog);
  234. wrefresh(list);
  235. }
  236. continue; /* wait for another key press */
  237. }
  238. switch (key) {
  239. case 'H':
  240. case 'h':
  241. case '?':
  242. button = 1;
  243. /* fall-through */
  244. case 'S':
  245. case 's':
  246. case ' ':
  247. case '\n':
  248. item_foreach()
  249. item_set_selected(0);
  250. item_set(scroll + choice);
  251. item_set_selected(1);
  252. delwin(list);
  253. delwin(dialog);
  254. return button;
  255. case TAB:
  256. case KEY_LEFT:
  257. case KEY_RIGHT:
  258. button = ((key == KEY_LEFT ? --button : ++button) < 0)
  259. ? 1 : (button > 1 ? 0 : button);
  260. print_buttons(dialog, height, width, button);
  261. wrefresh(dialog);
  262. break;
  263. case 'X':
  264. case 'x':
  265. key = KEY_ESC;
  266. break;
  267. case KEY_ESC:
  268. key = on_key_esc(dialog);
  269. break;
  270. case KEY_RESIZE:
  271. delwin(list);
  272. delwin(dialog);
  273. on_key_resize();
  274. goto do_resize;
  275. }
  276. /* Now, update everything... */
  277. doupdate();
  278. }
  279. delwin(list);
  280. delwin(dialog);
  281. return key; /* ESC pressed */
  282. }