1 /*
2 * $Id: checklist.c,v 1.135 2012/07/01 16:30:04 Zoltan.Kelemen Exp $
3 *
4 * checklist.c -- implements the checklist box
5 *
6 * Copyright 2000-2011,2012 Thomas E. Dickey
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
22 *
23 * An earlier version of this program lists as authors:
24 * Savio Lam (lam836@cs.cuhk.hk)
25 * Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
26 * Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
27 */
28
29 #include <dialog.h>
30 #include <dlg_keys.h>
31
32 static int list_width, check_x, item_x, checkflag;
33
34 #define MIN_HIGH (1 + (5 * MARGIN))
35
36 #define LLEN(n) ((n) * CHECKBOX_TAGS)
37 #define ItemData(i) &items[LLEN(i)]
38 #define ItemName(i) items[LLEN(i)]
39 #define ItemText(i) items[LLEN(i) + 1]
40 #define ItemStatus(i) items[LLEN(i) + 2]
41 #define ItemHelp(i) items[LLEN(i) + 3]
42
43 static void
print_arrows(WINDOW * win,int box_x,int box_y,int scrollamt,int choice,int item_no,int list_height)44 print_arrows(WINDOW *win,
45 int box_x,
46 int box_y,
47 int scrollamt,
48 int choice,
49 int item_no,
50 int list_height)
51 {
52 dlg_draw_scrollbar(win,
53 (long) (scrollamt),
54 (long) (scrollamt),
55 (long) (scrollamt + choice),
56 (long) (item_no),
57 box_x + check_x,
58 box_x + list_width,
59 box_y,
60 box_y + list_height + 1,
61 menubox_border2_attr,
62 menubox_border_attr);
63 }
64
65 /*
66 * Print list item. The 'selected' parameter is true if 'choice' is the
67 * current item. That one is colored differently from the other items.
68 */
69 static void
print_item(WINDOW * win,DIALOG_LISTITEM * item,const char * states,int choice,int selected)70 print_item(WINDOW *win,
71 DIALOG_LISTITEM * item,
72 const char *states,
73 int choice,
74 int selected)
75 {
76 chtype save = dlg_get_attrs(win);
77 int i;
78 chtype attr = A_NORMAL;
79 const int *cols;
80 const int *indx;
81 int limit;
82
83 /* Clear 'residue' of last item */
84 (void) wattrset(win, menubox_attr);
85 (void) wmove(win, choice, 0);
86 for (i = 0; i < list_width; i++)
87 (void) waddch(win, ' ');
88
89 (void) wmove(win, choice, check_x);
90 (void) wattrset(win, selected ? check_selected_attr : check_attr);
91 (void) wprintw(win,
92 (checkflag == FLAG_CHECK) ? "[%c]" : "(%c)",
93 states[item->state]);
94 (void) wattrset(win, menubox_attr);
95 (void) waddch(win, ' ');
96
97 if (strlen(item->name) != 0) {
98
99 indx = dlg_index_wchars(item->name);
100
101 (void) wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
102 (void) waddnstr(win, item->name, indx[1]);
103
104 if ((int) strlen(item->name) > indx[1]) {
105 limit = dlg_limit_columns(item->name, (item_x - check_x - 6), 1);
106 if (limit > 1) {
107 (void) wattrset(win, selected ? tag_selected_attr : tag_attr);
108 (void) waddnstr(win,
109 item->name + indx[1],
110 indx[limit] - indx[1]);
111 }
112 }
113 }
114
115 if (strlen(item->text) != 0) {
116 cols = dlg_index_columns(item->text);
117 limit = dlg_limit_columns(item->text, (getmaxx(win) - item_x + 1), 0);
118
119 if (limit > 0) {
120 (void) wmove(win, choice, item_x);
121 (void) wattrset(win, selected ? item_selected_attr : item_attr);
122 dlg_print_text(win, item->text, cols[limit], &attr);
123 }
124 }
125
126 if (selected) {
127 dlg_item_help(item->help);
128 }
129 (void) wattrset(win, save);
130 }
131
132 /*
133 * This is an alternate interface to 'checklist' which allows the application
134 * to read the list item states back directly without putting them in the
135 * output buffer. It also provides for more than two states over which the
136 * check/radio box can display.
137 */
138 int
dlg_checklist(const char * title,const char * cprompt,int height,int width,int list_height,int item_no,DIALOG_LISTITEM * items,const char * states,int flag,int * current_item)139 dlg_checklist(const char *title,
140 const char *cprompt,
141 int height,
142 int width,
143 int list_height,
144 int item_no,
145 DIALOG_LISTITEM * items,
146 const char *states,
147 int flag,
148 int *current_item)
149 {
150 /* *INDENT-OFF* */
151 static DLG_KEYS_BINDING binding[] = {
152 HELPKEY_BINDINGS,
153 ENTERKEY_BINDINGS,
154 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
155 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
156 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
157 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
158 DLG_KEYS_DATA( DLGK_ITEM_FIRST, KEY_HOME ),
159 DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_END ),
160 DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_LL ),
161 DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+' ),
162 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
163 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ),
164 DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ),
165 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
166 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ),
167 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
168 DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE) ),
169 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
170 DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE) ),
171 END_KEYS_BINDING
172 };
173 /* *INDENT-ON* */
174
175 #ifdef KEY_RESIZE
176 int old_height = height;
177 int old_width = width;
178 #endif
179 int i, j, key2, found, x, y, cur_x, cur_y, box_x, box_y;
180 int key = 0, fkey;
181 int button = dialog_state.visit_items ? -1 : dlg_default_button();
182 int choice = dlg_default_listitem(items);
183 int scrollamt = 0;
184 int max_choice;
185 int was_mouse;
186 int use_height;
187 int use_width, name_width, text_width;
188 int result = DLG_EXIT_UNKNOWN;
189 int num_states;
190 WINDOW *dialog, *list;
191 char *prompt = dlg_strclone(cprompt);
192 const char **buttons = dlg_ok_labels();
193 const char *widget_name;
194
195 dlg_does_output();
196 dlg_tab_correct_str(prompt);
197
198 /*
199 * If this is a radiobutton list, ensure that no more than one item is
200 * selected initially. Allow none to be selected, since some users may
201 * wish to provide this flavor.
202 */
203 if (flag == FLAG_RADIO) {
204 bool first = TRUE;
205
206 for (i = 0; i < item_no; i++) {
207 if (items[i].state) {
208 if (first) {
209 first = FALSE;
210 } else {
211 items[i].state = 0;
212 }
213 }
214 }
215 widget_name = "radiolist";
216 } else {
217 widget_name = "checklist";
218 }
219 #ifdef KEY_RESIZE
220 retry:
221 #endif
222
223 use_height = list_height;
224 use_width = dlg_calc_list_width(item_no, items) + 10;
225 use_width = MAX(26, use_width);
226 if (use_height == 0) {
227 /* calculate height without items (4) */
228 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, use_width);
229 dlg_calc_listh(&height, &use_height, item_no);
230 } else {
231 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, use_width);
232 }
233 dlg_button_layout(buttons, &width);
234 dlg_print_size(height, width);
235 dlg_ctl_size(height, width);
236
237 /* we need at least two states */
238 if (states == 0 || strlen(states) < 2)
239 states = " *";
240 num_states = (int) strlen(states);
241
242 checkflag = flag;
243
244 x = dlg_box_x_ordinate(width);
245 y = dlg_box_y_ordinate(height);
246
247 dialog = dlg_new_window(height, width, y, x);
248 dlg_register_window(dialog, widget_name, binding);
249 dlg_register_buttons(dialog, widget_name, buttons);
250
251 dlg_mouse_setbase(x, y);
252
253 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
254 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
255 dlg_draw_title(dialog, title);
256
257 (void) wattrset(dialog, dialog_attr);
258 dlg_print_autowrap(dialog, prompt, height, width);
259
260 list_width = width - 6;
261 getyx(dialog, cur_y, cur_x);
262 box_y = cur_y + 1;
263 box_x = (width - list_width) / 2 - 1;
264
265 /*
266 * After displaying the prompt, we know how much space we really have.
267 * Limit the list to avoid overwriting the ok-button.
268 */
269 if (use_height + MIN_HIGH > height - cur_y)
270 use_height = height - MIN_HIGH - cur_y;
271 if (use_height <= 0)
272 use_height = 1;
273
274 max_choice = MIN(use_height, item_no);
275
276 /* create new window for the list */
277 list = dlg_sub_window(dialog, use_height, list_width,
278 y + box_y + 1, x + box_x + 1);
279
280 /* draw a box around the list items */
281 dlg_draw_box(dialog, box_y, box_x,
282 use_height + 2 * MARGIN,
283 list_width + 2 * MARGIN,
284 menubox_border_attr, menubox_border2_attr);
285
286 text_width = 0;
287 name_width = 0;
288 /* Find length of longest item to center checklist */
289 for (i = 0; i < item_no; i++) {
290 text_width = MAX(text_width, dlg_count_columns(items[i].text));
291 name_width = MAX(name_width, dlg_count_columns(items[i].name));
292 }
293
294 /* If the name+text is wider than the list is allowed, then truncate
295 * one or both of them. If the name is no wider than 1/4 of the list,
296 * leave it intact.
297 */
298 use_width = (list_width - 6);
299 if (text_width + name_width > use_width) {
300 int need = (int) (0.25 * use_width);
301 if (name_width > need) {
302 int want = (int) (use_width * ((double) name_width) /
303 (text_width + name_width));
304 name_width = (want > need) ? want : need;
305 }
306 text_width = use_width - name_width;
307 }
308
309 check_x = (use_width - (text_width + name_width)) / 2;
310 item_x = name_width + check_x + 6;
311
312 /* ensure we are scrolled to show the current choice */
313 if (choice >= (max_choice + scrollamt)) {
314 scrollamt = choice - max_choice + 1;
315 choice = max_choice - 1;
316 }
317 /* Print the list */
318 for (i = 0; i < max_choice; i++) {
319 print_item(list,
320 &items[i + scrollamt],
321 states,
322 i, i == choice);
323 }
324 (void) wnoutrefresh(list);
325
326 /* register the new window, along with its borders */
327 dlg_mouse_mkbigregion(box_y + 1, box_x, use_height, list_width + 2,
328 KEY_MAX, 1, 1, 1 /* by lines */ );
329
330 print_arrows(dialog,
331 box_x, box_y,
332 scrollamt, max_choice, item_no, use_height);
333
334 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
335
336 dlg_trace_win(dialog);
337 while (result == DLG_EXIT_UNKNOWN) {
338 if (button < 0) /* --visit-items */
339 wmove(dialog, box_y + choice + 1, box_x + check_x + 2);
340
341 key = dlg_mouse_wgetch(dialog, &fkey);
342 if (dlg_result_key(key, fkey, &result))
343 break;
344
345 was_mouse = (fkey && is_DLGK_MOUSE(key));
346 if (was_mouse)
347 key -= M_EVENT;
348
349 if (was_mouse && (key >= KEY_MAX)) {
350 getyx(dialog, cur_y, cur_x);
351 i = (key - KEY_MAX);
352 if (i < max_choice) {
353 /* De-highlight current item */
354 print_item(list,
355 &items[scrollamt + choice],
356 states,
357 choice, FALSE);
358 /* Highlight new item */
359 choice = (key - KEY_MAX);
360 print_item(list,
361 &items[scrollamt + choice],
362 states,
363 choice, TRUE);
364 (void) wnoutrefresh(list);
365 (void) wmove(dialog, cur_y, cur_x);
366
367 key = ' '; /* force the selected item to toggle */
368 } else {
369 beep();
370 continue;
371 }
372 fkey = FALSE;
373 } else if (was_mouse && key >= KEY_MIN) {
374 key = dlg_lookup_key(dialog, key, &fkey);
375 }
376
377 /*
378 * A space toggles the item status. We handle either a checklist
379 * (any number of items can be selected) or radio list (zero or one
380 * items can be selected).
381 */
382 if (key == ' ') {
383 int current = scrollamt + choice;
384 int next = items[current].state + 1;
385
386 if (next >= num_states)
387 next = 0;
388
389 getyx(dialog, cur_y, cur_x);
390 if (flag == FLAG_CHECK) { /* checklist? */
391 items[current].state = next;
392 print_item(list,
393 &items[scrollamt + choice],
394 states,
395 choice, TRUE);
396 } else { /* radiolist */
397 for (i = 0; i < item_no; i++) {
398 if (i != current) {
399 items[i].state = 0;
400 }
401 }
402 if (items[current].state) {
403 items[current].state = next ? next : 1;
404 print_item(list,
405 &items[current],
406 states,
407 choice, TRUE);
408 } else {
409 items[current].state = 1;
410 for (i = 0; i < max_choice; i++)
411 print_item(list,
412 &items[scrollamt + i],
413 states,
414 i, i == choice);
415 }
416 }
417 (void) wnoutrefresh(list);
418 (void) wmove(dialog, cur_y, cur_x);
419 wrefresh(dialog);
420 continue; /* wait for another key press */
421 }
422
423 /*
424 * Check if key pressed matches first character of any item tag in
425 * list. If there is more than one match, we will cycle through
426 * each one as the same key is pressed repeatedly.
427 */
428 found = FALSE;
429 if (!fkey) {
430 if (button < 0 || !dialog_state.visit_items) {
431 for (j = scrollamt + choice + 1; j < item_no; j++) {
432 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
433 found = TRUE;
434 i = j - scrollamt;
435 break;
436 }
437 }
438 if (!found) {
439 for (j = 0; j <= scrollamt + choice; j++) {
440 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
441 found = TRUE;
442 i = j - scrollamt;
443 break;
444 }
445 }
446 }
447 if (found)
448 dlg_flush_getc();
449 } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
450 button = j;
451 ungetch('\n');
452 continue;
453 }
454 }
455
456 /*
457 * A single digit (1-9) positions the selection to that line in the
458 * current screen.
459 */
460 if (!found
461 && (key <= '9')
462 && (key > '0')
463 && (key - '1' < max_choice)) {
464 found = TRUE;
465 i = key - '1';
466 }
467
468 if (!found) {
469 if (fkey) {
470 found = TRUE;
471 switch (key) {
472 case DLGK_ITEM_FIRST:
473 i = -scrollamt;
474 break;
475 case DLGK_ITEM_LAST:
476 i = item_no - 1 - scrollamt;
477 break;
478 case DLGK_PAGE_PREV:
479 if (choice)
480 i = 0;
481 else if (scrollamt != 0)
482 i = -MIN(scrollamt, max_choice);
483 else
484 continue;
485 break;
486 case DLGK_PAGE_NEXT:
487 i = MIN(choice + max_choice, item_no - scrollamt - 1);
488 break;
489 case DLGK_ITEM_PREV:
490 i = choice - 1;
491 if (choice == 0 && scrollamt == 0)
492 continue;
493 break;
494 case DLGK_ITEM_NEXT:
495 i = choice + 1;
496 if (scrollamt + choice >= item_no - 1)
497 continue;
498 break;
499 default:
500 found = FALSE;
501 break;
502 }
503 }
504 }
505
506 if (found) {
507 if (i != choice) {
508 getyx(dialog, cur_y, cur_x);
509 if (i < 0 || i >= max_choice) {
510 #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5
511 /*
512 * Using wscrl to assist ncurses scrolling is not needed
513 * in version 5.x
514 */
515 if (i == -1) {
516 if (use_height > 1) {
517 /* De-highlight current first item */
518 print_item(list,
519 &items[scrollamt],
520 states,
521 0, FALSE);
522 scrollok(list, TRUE);
523 wscrl(list, -1);
524 scrollok(list, FALSE);
525 }
526 scrollamt--;
527 print_item(list,
528 &items[scrollamt],
529 states,
530 0, TRUE);
531 } else if (i == max_choice) {
532 if (use_height > 1) {
533 /* De-highlight current last item before scrolling up */
534 print_item(list,
535 &items[scrollamt + max_choice - 1],
536 states,
537 max_choice - 1, FALSE);
538 scrollok(list, TRUE);
539 wscrl(list, 1);
540 scrollok(list, FALSE);
541 }
542 scrollamt++;
543 print_item(list,
544 &items[scrollamt + max_choice - 1],
545 states,
546 max_choice - 1, TRUE);
547 } else
548 #endif
549 {
550 if (i < 0) {
551 scrollamt += i;
552 choice = 0;
553 } else {
554 choice = max_choice - 1;
555 scrollamt += (i - max_choice + 1);
556 }
557 for (i = 0; i < max_choice; i++) {
558 print_item(list,
559 &items[scrollamt + i],
560 states,
561 i, i == choice);
562 }
563 }
564 (void) wnoutrefresh(list);
565 print_arrows(dialog,
566 box_x, box_y,
567 scrollamt, max_choice, item_no, use_height);
568 } else {
569 /* De-highlight current item */
570 print_item(list,
571 &items[scrollamt + choice],
572 states,
573 choice, FALSE);
574 /* Highlight new item */
575 choice = i;
576 print_item(list,
577 &items[scrollamt + choice],
578 states,
579 choice, TRUE);
580 (void) wnoutrefresh(list);
581 print_arrows(dialog,
582 box_x, box_y,
583 scrollamt, max_choice, item_no, use_height);
584 (void) wmove(dialog, cur_y, cur_x);
585 wrefresh(dialog);
586 }
587 }
588 continue; /* wait for another key press */
589 }
590
591 if (fkey) {
592 switch (key) {
593 case DLGK_ENTER:
594 result = dlg_enter_buttoncode(button);
595 break;
596 case DLGK_FIELD_PREV:
597 button = dlg_prev_button(buttons, button);
598 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
599 FALSE, width);
600 break;
601 case DLGK_FIELD_NEXT:
602 button = dlg_next_button(buttons, button);
603 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
604 FALSE, width);
605 break;
606 #ifdef KEY_RESIZE
607 case KEY_RESIZE:
608 /* reset data */
609 height = old_height;
610 width = old_width;
611 /* repaint */
612 dlg_clear();
613 dlg_del_window(dialog);
614 refresh();
615 dlg_mouse_free_regions();
616 goto retry;
617 #endif
618 default:
619 if (was_mouse) {
620 if ((key2 = dlg_ok_buttoncode(key)) >= 0) {
621 result = key2;
622 break;
623 }
624 beep();
625 }
626 }
627 } else {
628 beep();
629 }
630 }
631
632 dlg_del_window(dialog);
633 dlg_mouse_free_regions();
634 free(prompt);
635 *current_item = (scrollamt + choice);
636 return result;
637 }
638
639 /*
640 * Display a dialog box with a list of options that can be turned on or off
641 * The `flag' parameter is used to select between radiolist and checklist.
642 */
643 int
dialog_checklist(const char * title,const char * cprompt,int height,int width,int list_height,int item_no,char ** items,int flag)644 dialog_checklist(const char *title,
645 const char *cprompt,
646 int height,
647 int width,
648 int list_height,
649 int item_no,
650 char **items,
651 int flag)
652 {
653 int result;
654 int i;
655 DIALOG_LISTITEM *listitems;
656 bool separate_output = ((flag == FLAG_CHECK)
657 && (dialog_vars.separate_output));
658 bool show_status = FALSE;
659 int current = 0;
660
661 listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
662 assert_ptr(listitems, "dialog_checklist");
663
664 for (i = 0; i < item_no; ++i) {
665 listitems[i].name = ItemName(i);
666 listitems[i].text = ItemText(i);
667 listitems[i].help = ((dialog_vars.item_help)
668 ? ItemHelp(i)
669 : dlg_strempty());
670 listitems[i].state = !dlg_strcmp(ItemStatus(i), "on");
671 }
672 dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
673
674 result = dlg_checklist(title,
675 cprompt,
676 height,
677 width,
678 list_height,
679 item_no,
680 listitems,
681 NULL,
682 flag,
683 ¤t);
684
685 switch (result) {
686 case DLG_EXIT_OK: /* FALLTHRU */
687 case DLG_EXIT_EXTRA:
688 show_status = TRUE;
689 break;
690 case DLG_EXIT_HELP:
691 dlg_add_result("HELP ");
692 show_status = dialog_vars.help_status;
693 if (USE_ITEM_HELP(listitems[current].help)) {
694 if (show_status) {
695 if (separate_output) {
696 dlg_add_string(listitems[current].help);
697 dlg_add_separator();
698 } else {
699 dlg_add_quoted(listitems[current].help);
700 }
701 } else {
702 dlg_add_string(listitems[current].help);
703 }
704 result = DLG_EXIT_ITEM_HELP;
705 } else {
706 if (show_status) {
707 if (separate_output) {
708 dlg_add_string(listitems[current].name);
709 dlg_add_separator();
710 } else {
711 dlg_add_quoted(listitems[current].name);
712 }
713 } else {
714 dlg_add_string(listitems[current].name);
715 }
716 }
717 break;
718 }
719
720 if (show_status) {
721 for (i = 0; i < item_no; i++) {
722 if (listitems[i].state) {
723 if (separate_output) {
724 dlg_add_string(listitems[i].name);
725 dlg_add_separator();
726 } else {
727 if (dlg_need_separator())
728 dlg_add_separator();
729 if (flag == FLAG_CHECK)
730 dlg_add_quoted(listitems[i].name);
731 else
732 dlg_add_string(listitems[i].name);
733 }
734 }
735 }
736 }
737
738 dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
739 free(listitems);
740 return result;
741 }
742