checklist.c 8.2 KB

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