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