1
0

checklist.c 7.6 KB

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