checklist.c 7.9 KB

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