2 * $Id: checklist.c,v 1.127 2011/06/29 23:04:09 tom Exp $
4 * checklist.c -- implements the checklist box
6 * Copyright 2000-2010,2011 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,
55 (long) (scrollamt + choice),
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 = dlg_get_attrs(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[] = {
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) ),
176 int old_height = height;
177 int old_width = width;
179 int i, j, key2, found, x, y, cur_x, cur_y, box_x, box_y;
181 int button = dialog_state.visit_items ? -1 : dlg_defaultno_button();
182 int choice = dlg_default_listitem(items);
187 int use_width, name_width, text_width;
188 int result = DLG_EXIT_UNKNOWN;
190 WINDOW *dialog, *list;
191 char *prompt = dlg_strclone(cprompt);
192 const char **buttons = dlg_ok_labels();
195 dlg_tab_correct_str(prompt);
198 * If this is a radiobutton list, ensure that no more than one item is
199 * selected initially. Allow none to be selected, since some users may
200 * wish to provide this flavor.
202 if (flag == FLAG_RADIO) {
205 for (i = 0; i < item_no; i++) {
206 if (items[i].state) {
219 use_height = list_height;
220 if (use_height == 0) {
221 use_width = dlg_calc_list_width(item_no, items) + 10;
222 /* calculate height without items (4) */
223 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MAX(26, use_width));
224 dlg_calc_listh(&height, &use_height, item_no);
226 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, 26);
228 dlg_button_layout(buttons, &width);
229 dlg_print_size(height, width);
230 dlg_ctl_size(height, width);
232 /* we need at least two states */
233 if (states == 0 || strlen(states) < 2)
235 num_states = (int) strlen(states);
239 x = dlg_box_x_ordinate(width);
240 y = dlg_box_y_ordinate(height);
242 dialog = dlg_new_window(height, width, y, x);
243 dlg_register_window(dialog, "checklist", binding);
244 dlg_register_buttons(dialog, "checklist", buttons);
246 dlg_mouse_setbase(x, y);
248 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
249 dlg_draw_bottom_box(dialog);
250 dlg_draw_title(dialog, title);
252 wattrset(dialog, dialog_attr);
253 dlg_print_autowrap(dialog, prompt, height, width);
255 list_width = width - 6;
256 getyx(dialog, cur_y, cur_x);
258 box_x = (width - list_width) / 2 - 1;
261 * After displaying the prompt, we know how much space we really have.
262 * Limit the list to avoid overwriting the ok-button.
264 if (use_height + MIN_HIGH > height - cur_y)
265 use_height = height - MIN_HIGH - cur_y;
269 max_choice = MIN(use_height, item_no);
271 /* create new window for the list */
272 list = dlg_sub_window(dialog, use_height, list_width,
273 y + box_y + 1, x + box_x + 1);
275 /* draw a box around the list items */
276 dlg_draw_box(dialog, box_y, box_x,
277 use_height + 2 * MARGIN,
278 list_width + 2 * MARGIN,
279 menubox_border_attr, menubox_attr);
283 /* Find length of longest item to center checklist */
284 for (i = 0; i < item_no; i++) {
285 text_width = MAX(text_width, dlg_count_columns(items[i].text));
286 name_width = MAX(name_width, dlg_count_columns(items[i].name));
289 /* If the name+text is wider than the list is allowed, then truncate
290 * one or both of them. If the name is no wider than 1/4 of the list,
293 use_width = (list_width - 6);
294 if (text_width + name_width > use_width) {
295 int need = (int) (0.25 * use_width);
296 if (name_width > need) {
297 int want = (int) (use_width * ((double) name_width) /
298 (text_width + name_width));
299 name_width = (want > need) ? want : need;
301 text_width = use_width - name_width;
304 check_x = (use_width - (text_width + name_width)) / 2;
305 item_x = name_width + check_x + 6;
307 /* ensure we are scrolled to show the current choice */
308 if (choice >= (max_choice + scrollamt)) {
309 scrollamt = choice - max_choice + 1;
310 choice = max_choice - 1;
313 for (i = 0; i < max_choice; i++) {
315 &items[i + scrollamt],
319 (void) wnoutrefresh(list);
321 /* register the new window, along with its borders */
322 dlg_mouse_mkbigregion(box_y + 1, box_x, use_height, list_width + 2,
323 KEY_MAX, 1, 1, 1 /* by lines */ );
327 scrollamt, max_choice, item_no, use_height);
329 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
331 while (result == DLG_EXIT_UNKNOWN) {
332 if (button < 0) /* --visit-items */
333 wmove(dialog, box_y + choice + 1, box_x + check_x + 2);
335 key = dlg_mouse_wgetch(dialog, &fkey);
336 if (dlg_result_key(key, fkey, &result))
339 was_mouse = (fkey && is_DLGK_MOUSE(key));
343 if (was_mouse && (key >= KEY_MAX)) {
344 getyx(dialog, cur_y, cur_x);
346 if (i < max_choice) {
347 /* De-highlight current item */
349 &items[scrollamt + choice],
352 /* Highlight new item */
353 choice = (key - KEY_MAX);
355 &items[scrollamt + choice],
358 (void) wnoutrefresh(list);
359 (void) wmove(dialog, cur_y, cur_x);
361 key = ' '; /* force the selected item to toggle */
367 } else if (was_mouse && key >= KEY_MIN) {
368 key = dlg_lookup_key(dialog, key, &fkey);
372 * A space toggles the item status. We handle either a checklist
373 * (any number of items can be selected) or radio list (zero or one
374 * items can be selected).
377 int current = scrollamt + choice;
378 int next = items[current].state + 1;
380 if (next >= num_states)
383 getyx(dialog, cur_y, cur_x);
384 if (flag == FLAG_CHECK) { /* checklist? */
385 items[current].state = next;
387 &items[scrollamt + choice],
390 } else { /* radiolist */
391 for (i = 0; i < item_no; i++) {
396 if (items[current].state) {
397 items[current].state = next ? next : 1;
403 items[current].state = 1;
404 for (i = 0; i < max_choice; i++)
406 &items[scrollamt + i],
411 (void) wnoutrefresh(list);
412 (void) wmove(dialog, cur_y, cur_x);
414 continue; /* wait for another key press */
418 * Check if key pressed matches first character of any item tag in
419 * list. If there is more than one match, we will cycle through
420 * each one as the same key is pressed repeatedly.
424 if (button < 0 || !dialog_state.visit_items) {
425 for (j = scrollamt + choice + 1; j < item_no; j++) {
426 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
433 for (j = 0; j <= scrollamt + choice; j++) {
434 if (dlg_match_char(dlg_last_getc(), items[j].name)) {
443 } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
451 * A single digit (1-9) positions the selection to that line in the
457 && (key - '1' < max_choice)) {
466 case DLGK_ITEM_FIRST:
470 i = item_no - 1 - scrollamt;
475 else if (scrollamt != 0)
476 i = -MIN(scrollamt, max_choice);
481 i = MIN(choice + max_choice, item_no - scrollamt - 1);
485 if (choice == 0 && scrollamt == 0)
490 if (scrollamt + choice >= item_no - 1)
502 getyx(dialog, cur_y, cur_x);
503 if (i < 0 || i >= max_choice) {
504 #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5
506 * Using wscrl to assist ncurses scrolling is not needed
510 if (use_height > 1) {
511 /* De-highlight current first item */
516 scrollok(list, TRUE);
518 scrollok(list, FALSE);
525 } else if (i == max_choice) {
526 if (use_height > 1) {
527 /* De-highlight current last item before scrolling up */
529 &items[scrollamt + max_choice - 1],
531 max_choice - 1, FALSE);
532 scrollok(list, TRUE);
534 scrollok(list, FALSE);
538 &items[scrollamt + max_choice - 1],
540 max_choice - 1, TRUE);
548 choice = max_choice - 1;
549 scrollamt += (i - max_choice + 1);
551 for (i = 0; i < max_choice; i++) {
553 &items[scrollamt + i],
558 (void) wnoutrefresh(list);
561 scrollamt, max_choice, item_no, use_height);
563 /* De-highlight current item */
565 &items[scrollamt + choice],
568 /* Highlight new item */
571 &items[scrollamt + choice],
574 (void) wnoutrefresh(list);
577 scrollamt, max_choice, item_no, use_height);
578 (void) wmove(dialog, cur_y, cur_x);
582 continue; /* wait for another key press */
588 result = dlg_enter_buttoncode(button);
590 case DLGK_FIELD_PREV:
591 button = dlg_prev_button(buttons, button);
592 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
595 case DLGK_FIELD_NEXT:
596 button = dlg_next_button(buttons, button);
597 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
607 dlg_del_window(dialog);
609 dlg_mouse_free_regions();
614 if ((key2 = dlg_ok_buttoncode(key)) >= 0) {
626 dlg_del_window(dialog);
627 dlg_mouse_free_regions();
629 *current_item = (scrollamt + choice);
634 * Display a dialog box with a list of options that can be turned on or off
635 * The `flag' parameter is used to select between radiolist and checklist.
638 dialog_checklist(const char *title,
649 DIALOG_LISTITEM *listitems;
650 bool separate_output = ((flag == FLAG_CHECK)
651 && (dialog_vars.separate_output));
652 bool show_status = FALSE;
655 listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
656 assert_ptr(listitems, "dialog_checklist");
658 for (i = 0; i < item_no; ++i) {
659 listitems[i].name = ItemName(i);
660 listitems[i].text = ItemText(i);
661 listitems[i].help = ((dialog_vars.item_help)
664 listitems[i].state = !dlg_strcmp(ItemStatus(i), "on");
666 dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
668 result = dlg_checklist(title,
680 case DLG_EXIT_OK: /* FALLTHRU */
685 dlg_add_result("HELP ");
686 show_status = dialog_vars.help_status;
687 if (USE_ITEM_HELP(listitems[current].help)) {
689 if (separate_output) {
690 dlg_add_string(listitems[current].help);
693 dlg_add_quoted(listitems[current].help);
696 dlg_add_string(listitems[current].help);
698 result = DLG_EXIT_ITEM_HELP;
701 if (separate_output) {
702 dlg_add_string(listitems[current].name);
705 dlg_add_quoted(listitems[current].name);
708 dlg_add_string(listitems[current].name);
715 for (i = 0; i < item_no; i++) {
716 if (listitems[i].state) {
717 if (separate_output) {
718 dlg_add_string(listitems[i].name);
721 if (dlg_need_separator())
723 dlg_add_string(listitems[i].name);
729 dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);