checklist.c 9.0 KB

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