2 * $Id: checklist.c,v 1.121 2010/01/17 19:32:17 tom Exp $
4 * checklist.c -- implements the checklist box
6 * Copyright 2000-2009,2010 Thomas E. Dickey
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.
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.
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.
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
32 static int list_width, check_x, item_x, checkflag;
34 #define MIN_HIGH (1 + (5 * MARGIN))
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]
44 print_arrows(WINDOW *win,
52 dlg_draw_scrollbar(win,
60 box_y + list_height + 1,
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.
70 print_item(WINDOW *win,
71 DIALOG_LISTITEM * item,
76 chtype save = getattrs(win);
78 chtype attr = A_NORMAL;
83 /* Clear 'residue' of last item */
84 wattrset(win, menubox_attr);
85 (void) wmove(win, choice, 0);
86 for (i = 0; i < list_width; i++)
87 (void) waddch(win, ' ');
89 (void) wmove(win, choice, check_x);
90 wattrset(win, selected ? check_selected_attr : check_attr);
92 (checkflag == FLAG_CHECK) ? "[%c]" : "(%c)",
94 wattrset(win, menubox_attr);
95 (void) waddch(win, ' ');
97 if (strlen(item->name) != 0) {
99 indx = dlg_index_wchars(item->name);
101 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
102 (void) waddnstr(win, item->name, indx[1]);
104 if ((int) strlen(item->name) > indx[1]) {
105 limit = dlg_limit_columns(item->name, (item_x - check_x - 6), 1);
107 wattrset(win, selected ? tag_selected_attr : tag_attr);
109 item->name + indx[1],
110 indx[limit] - indx[1]);
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);
120 (void) wmove(win, choice, item_x);
121 wattrset(win, selected ? item_selected_attr : item_attr);
122 dlg_print_text(win, item->text, cols[limit], &attr);
127 dlg_item_help(item->help);
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.
139 dlg_checklist(const char *title,
145 DIALOG_LISTITEM * items,
151 static DLG_KEYS_BINDING binding[] = {
153 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
154 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
155 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
156 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
157 DLG_KEYS_DATA( DLGK_ITEM_FIRST, KEY_HOME ),
158 DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_END ),
159 DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_LL ),
160 DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+' ),
161 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
162 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ),
163 DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ),
164 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
165 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ),
166 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
167 DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE) ),
168 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
169 DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE) ),
175 int old_height = height;
176 int old_width = width;
178 int i, j, key2, found, x, y, cur_x, cur_y, box_x, box_y;
180 int button = dialog_state.visit_items ? -1 : dlg_defaultno_button();
181 int choice = dlg_default_listitem(items);
186 int use_width, name_width, text_width;
187 int result = DLG_EXIT_UNKNOWN;
189 WINDOW *dialog, *list;
190 char *prompt = dlg_strclone(cprompt);
191 const char **buttons = dlg_ok_labels();
194 dlg_tab_correct_str(prompt);
200 use_height = list_height;
201 if (use_height == 0) {
202 use_width = dlg_calc_list_width(item_no, items) + 10;
203 /* calculate height without items (4) */
204 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MAX(26, use_width));
205 dlg_calc_listh(&height, &use_height, item_no);
207 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, 26);
209 dlg_button_layout(buttons, &width);
210 dlg_print_size(height, width);
211 dlg_ctl_size(height, width);
213 /* we need at least two states */
214 if (states == 0 || strlen(states) < 2)
216 num_states = (int) strlen(states);
220 x = dlg_box_x_ordinate(width);
221 y = dlg_box_y_ordinate(height);
223 dialog = dlg_new_window(height, width, y, x);
224 dlg_register_window(dialog, "checklist", binding);
225 dlg_register_buttons(dialog, "checklist", buttons);
227 dlg_mouse_setbase(x, y);
229 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
230 dlg_draw_bottom_box(dialog);
231 dlg_draw_title(dialog, title);
233 wattrset(dialog, dialog_attr);
234 dlg_print_autowrap(dialog, prompt, height, width);
236 list_width = width - 6;
237 getyx(dialog, cur_y, cur_x);
239 box_x = (width - list_width) / 2 - 1;
242 * After displaying the prompt, we know how much space we really have.
243 * Limit the list to avoid overwriting the ok-button.
245 if (use_height + MIN_HIGH > height - cur_y)
246 use_height = height - MIN_HIGH - cur_y;
250 max_choice = MIN(use_height, item_no);
252 /* create new window for the list */
253 list = dlg_sub_window(dialog, use_height, list_width,
254 y + box_y + 1, x + box_x + 1);
256 /* draw a box around the list items */
257 dlg_draw_box(dialog, box_y, box_x,
258 use_height + 2 * MARGIN,
259 list_width + 2 * MARGIN,
260 menubox_border_attr, menubox_attr);
264 /* Find length of longest item to center checklist */
265 for (i = 0; i < item_no; i++) {
266 text_width = MAX(text_width, dlg_count_columns(items[i].text));
267 name_width = MAX(name_width, dlg_count_columns(items[i].name));
270 /* If the name+text is wider than the list is allowed, then truncate
271 * one or both of them. If the name is no wider than 1/4 of the list,
274 use_width = (list_width - 6);
275 if (text_width + name_width > use_width) {
276 int need = (int) (0.25 * use_width);
277 if (name_width > need) {
278 int want = (int) (use_width * ((double) name_width) /
279 (text_width + name_width));
280 name_width = (want > need) ? want : need;
282 text_width = use_width - name_width;
285 check_x = (use_width - (text_width + name_width)) / 2;
286 item_x = name_width + check_x + 6;
288 /* ensure we are scrolled to show the current choice */
289 if (choice >= (max_choice + scrollamt)) {
290 scrollamt = choice - max_choice + 1;
291 choice = max_choice - 1;
294 for (i = 0; i < max_choice; i++)
296 &items[i + scrollamt],
299 (void) wnoutrefresh(list);
301 /* register the new window, along with its borders */
302 dlg_mouse_mkbigregion(box_y + 1, box_x, use_height, list_width + 2,
303 KEY_MAX, 1, 1, 1 /* by lines */ );
307 scrollamt, max_choice, item_no, use_height);
309 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
311 while (result == DLG_EXIT_UNKNOWN) {
312 if (button < 0) /* --visit-items */
313 wmove(dialog, box_y + choice + 1, box_x + check_x + 2);
315 key = dlg_mouse_wgetch(dialog, &fkey);
316 if (dlg_result_key(key, fkey, &result))
319 was_mouse = (fkey && is_DLGK_MOUSE(key));
323 if (was_mouse && (key >= KEY_MAX)) {
324 getyx(dialog, cur_y, cur_x);
326 if (i < max_choice) {
327 /* De-highlight current item */
329 &items[scrollamt + choice],
332 /* Highlight new item */
333 choice = (key - KEY_MAX);
335 &items[scrollamt + choice],
338 (void) wnoutrefresh(list);
339 (void) wmove(dialog, cur_y, cur_x);
341 key = ' '; /* force the selected item to toggle */
347 } else if (was_mouse && key >= KEY_MIN) {
348 key = dlg_lookup_key(dialog, key, &fkey);
352 * A space toggles the item status. We handle either a checklist
353 * (any number of items can be selected) or radio list (zero or one
354 * items can be selected).
357 int current = scrollamt + choice;
358 int next = items[current].state + 1;
360 if (next >= num_states)
363 getyx(dialog, cur_y, cur_x);
364 if (flag == FLAG_CHECK) { /* checklist? */
365 items[current].state = next;
367 &items[scrollamt + choice],
370 } else { /* radiolist */
371 for (i = 0; i < item_no; i++) {
376 if (items[current].state) {
377 items[current].state = next ? next : 1;
383 items[current].state = 1;
384 for (i = 0; i < max_choice; i++)
386 &items[scrollamt + i],
391 (void) wnoutrefresh(list);
392 (void) wmove(dialog, cur_y, cur_x);
394 continue; /* wait for another key press */
398 * Check if key pressed matches first character of any item tag in
399 * list. If there is more than one match, we will cycle through
400 * each one as the same key is pressed repeatedly.
404 if (button < 0 || !dialog_state.visit_items) {
405 for (j = scrollamt + choice + 1; j < item_no; j++) {
406 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
413 for (j = 0; j <= scrollamt + choice; j++) {
414 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
423 } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
431 * A single digit (1-9) positions the selection to that line in the
437 && (key - '1' < max_choice)) {
446 case DLGK_ITEM_FIRST:
450 i = item_no - 1 - scrollamt;
455 else if (scrollamt != 0)
456 i = -MIN(scrollamt, max_choice);
461 i = MIN(choice + max_choice, item_no - scrollamt - 1);
465 if (choice == 0 && scrollamt == 0)
470 if (scrollamt + choice >= item_no - 1)
482 getyx(dialog, cur_y, cur_x);
483 if (i < 0 || i >= max_choice) {
484 #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5
486 * Using wscrl to assist ncurses scrolling is not needed
490 if (use_height > 1) {
491 /* De-highlight current first item */
496 scrollok(list, TRUE);
498 scrollok(list, FALSE);
505 } else if (i == max_choice) {
506 if (use_height > 1) {
507 /* De-highlight current last item before scrolling up */
509 &items[scrollamt + max_choice - 1],
511 max_choice - 1, FALSE);
512 scrollok(list, TRUE);
514 scrollok(list, FALSE);
518 &items[scrollamt + max_choice - 1],
520 max_choice - 1, TRUE);
528 choice = max_choice - 1;
529 scrollamt += (i - max_choice + 1);
531 for (i = 0; i < max_choice; i++) {
533 &items[scrollamt + i],
538 (void) wnoutrefresh(list);
541 scrollamt, max_choice, item_no, use_height);
543 /* De-highlight current item */
545 &items[scrollamt + choice],
548 /* Highlight new item */
551 &items[scrollamt + choice],
554 (void) wnoutrefresh(list);
557 scrollamt, max_choice, item_no, use_height);
558 (void) wmove(dialog, cur_y, cur_x);
562 continue; /* wait for another key press */
568 result = dlg_ok_buttoncode(button);
570 case DLGK_FIELD_PREV:
571 button = dlg_prev_button(buttons, button);
572 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
575 case DLGK_FIELD_NEXT:
576 button = dlg_next_button(buttons, button);
577 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
587 dlg_del_window(dialog);
589 dlg_mouse_free_regions();
594 if ((key2 = dlg_ok_buttoncode(key)) >= 0) {
606 dlg_del_window(dialog);
607 dlg_mouse_free_regions();
609 *current_item = (scrollamt + choice);
614 * Display a dialog box with a list of options that can be turned on or off
615 * The `flag' parameter is used to select between radiolist and checklist.
618 dialog_checklist(const char *title,
629 DIALOG_LISTITEM *listitems;
630 bool separate_output = ((flag == FLAG_CHECK)
631 && (dialog_vars.separate_output));
632 bool show_status = FALSE;
635 listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
636 assert_ptr(listitems, "dialog_checklist");
638 for (i = 0; i < item_no; ++i) {
639 listitems[i].name = ItemName(i);
640 listitems[i].text = ItemText(i);
641 listitems[i].help = ((dialog_vars.item_help)
644 listitems[i].state = !dlg_strcmp(ItemStatus(i), "on");
646 dlg_align_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);
648 result = dlg_checklist(title,
660 case DLG_EXIT_OK: /* FALLTHRU */
665 dlg_add_result("HELP ");
666 show_status = dialog_vars.help_status;
667 if (USE_ITEM_HELP(listitems[current].help)) {
669 if (separate_output) {
670 dlg_add_string(listitems[current].help);
673 dlg_add_quoted(listitems[current].help);
676 dlg_add_string(listitems[current].help);
678 result = DLG_EXIT_ITEM_HELP;
681 if (separate_output) {
682 dlg_add_string(listitems[current].name);
685 dlg_add_quoted(listitems[current].name);
688 dlg_add_string(listitems[current].name);
695 for (i = 0; i < item_no; i++) {
696 if (listitems[i].state) {
697 if (separate_output) {
698 dlg_add_string(listitems[i].name);
701 if (dlg_need_separator())
703 dlg_add_string(listitems[i].name);
709 dlg_free_columns(&listitems[0].text, sizeof(DIALOG_LISTITEM), item_no);