]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/dialog/checklist.c
Merge llvm-project 13.0.0 release
[FreeBSD/FreeBSD.git] / contrib / dialog / checklist.c
1 /*
2  *  $Id: checklist.c,v 1.167 2020/11/23 00:37:47 tom Exp $
3  *
4  *  checklist.c -- implements the checklist box
5  *
6  *  Copyright 2000-2019,2020    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     dlg_attrset(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     dlg_attrset(win, selected ? check_selected_attr : check_attr);
80     (void) wprintw(win,
81                    (data->checkflag == FLAG_CHECK) ? "[%c]" : "(%c)",
82                    states[item->state]);
83     dlg_attrset(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     dlg_attrset(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         TOGGLEKEY_BINDINGS,
185         END_KEYS_BINDING
186     };
187     /* *INDENT-ON* */
188
189 #ifdef KEY_RESIZE
190     int old_height = height;
191     int old_width = width;
192 #endif
193     ALL_DATA all;
194     int i, j, key2, found, x, y, cur_x, cur_y;
195     int key, fkey;
196     int button = dialog_state.visit_items ? -1 : dlg_default_button();
197     int choice = dlg_default_listitem(items);
198     int scrollamt = 0;
199     int max_choice;
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;
205     const char **buttons = dlg_ok_labels();
206     const char *widget_name;
207
208     DLG_TRACE(("# %s args:\n", flag ? "checklist" : "radiolist"));
209     DLG_TRACE2S("title", title);
210     DLG_TRACE2S("message", cprompt);
211     DLG_TRACE2N("height", height);
212     DLG_TRACE2N("width", width);
213     DLG_TRACE2N("lheight", list_height);
214     DLG_TRACE2N("llength", item_no);
215     /* FIXME dump the items[][] too */
216     DLG_TRACE2S("states", states);
217     DLG_TRACE2N("flag", flag);
218     DLG_TRACE2N("current", *current_item);
219
220     dialog_state.plain_buttons = TRUE;
221
222     memset(&all, 0, sizeof(all));
223     all.items = items;
224     all.item_no = item_no;
225
226     dlg_does_output();
227
228     /*
229      * If this is a radiobutton list, ensure that no more than one item is
230      * selected initially.  Allow none to be selected, since some users may
231      * wish to provide this flavor.
232      */
233     if (flag == FLAG_RADIO) {
234         bool first = TRUE;
235
236         for (i = 0; i < item_no; i++) {
237             if (items[i].state) {
238                 if (first) {
239                     first = FALSE;
240                 } else {
241                     items[i].state = 0;
242                 }
243             }
244         }
245         widget_name = "radiolist";
246     } else {
247         widget_name = "checklist";
248     }
249 #ifdef KEY_RESIZE
250   retry:
251 #endif
252
253     prompt = dlg_strclone(cprompt);
254     dlg_tab_correct_str(prompt);
255
256     all.use_height = list_height;
257     use_width = dlg_calc_list_width(item_no, items) + 10;
258     use_width = MAX(26, use_width);
259     if (all.use_height == 0) {
260         /* calculate height without items (4) */
261         dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, use_width);
262         dlg_calc_listh(&height, &all.use_height, item_no);
263     } else {
264         dlg_auto_size(title, prompt,
265                       &height, &width,
266                       MIN_HIGH + all.use_height, use_width);
267     }
268     dlg_button_layout(buttons, &width);
269     dlg_print_size(height, width);
270     dlg_ctl_size(height, width);
271
272     /* we need at least two states */
273     if (states == 0 || strlen(states) < 2)
274         states = " *";
275     num_states = (int) strlen(states);
276     all.states = states;
277
278     all.checkflag = flag;
279
280     x = dlg_box_x_ordinate(width);
281     y = dlg_box_y_ordinate(height);
282
283     dialog = dlg_new_window(height, width, y, x);
284     all.dialog = dialog;
285     dlg_register_window(dialog, widget_name, binding);
286     dlg_register_buttons(dialog, widget_name, buttons);
287
288     dlg_mouse_setbase(x, y);
289
290     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
291     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
292     dlg_draw_title(dialog, title);
293
294     dlg_attrset(dialog, dialog_attr);
295     dlg_print_autowrap(dialog, prompt, height, width);
296
297     all.use_width = width - 6;
298     getyx(dialog, cur_y, cur_x);
299     all.box_y = cur_y + 1;
300     all.box_x = (width - all.use_width) / 2 - 1;
301
302     /*
303      * After displaying the prompt, we know how much space we really have.
304      * Limit the list to avoid overwriting the ok-button.
305      */
306     all.use_height = height - MIN_HIGH - cur_y;
307     if (all.use_height <= 0)
308         all.use_height = 1;
309
310     max_choice = MIN(all.use_height, item_no);
311     max_choice = MAX(max_choice, 1);
312
313     /* create new window for the list */
314     all.list = dlg_sub_window(dialog, all.use_height, all.use_width,
315                               y + all.box_y + 1, x + all.box_x + 1);
316
317     /* draw a box around the list items */
318     dlg_draw_box(dialog, all.box_y, all.box_x,
319                  all.use_height + 2 * MARGIN,
320                  all.use_width + 2 * MARGIN,
321                  menubox_border_attr, menubox_border2_attr);
322
323     text_width = 0;
324     name_width = 0;
325     /* Find length of longest item to center checklist */
326     for (i = 0; i < item_no; i++) {
327         text_width = MAX(text_width, dlg_count_columns(items[i].text));
328         name_width = MAX(name_width, dlg_count_columns(items[i].name));
329     }
330
331     /* If the name+text is wider than the list is allowed, then truncate
332      * one or both of them.  If the name is no wider than 1/4 of the list,
333      * leave it intact.
334      */
335     use_width = (all.use_width - 6);
336     if (dialog_vars.no_tags) {
337         list_width = MIN(all.use_width, text_width);
338     } else if (dialog_vars.no_items) {
339         list_width = MIN(all.use_width, name_width);
340     } else {
341         if (text_width >= 0
342             && name_width >= 0
343             && use_width > 0
344             && text_width + name_width > use_width) {
345             int need = (int) (0.25 * use_width);
346             if (name_width > need) {
347                 int want = (int) (use_width * ((double) name_width) /
348                                   (text_width + name_width));
349                 name_width = (want > need) ? want : need;
350             }
351             text_width = use_width - name_width;
352         }
353         list_width = (text_width + name_width);
354     }
355
356     all.check_x = (use_width - list_width) / 2;
357     all.item_x = ((dialog_vars.no_tags
358                    ? 0
359                    : (dialog_vars.no_items
360                       ? 0
361                       : (2 + name_width)))
362                   + all.check_x + 4);
363
364     /* ensure we are scrolled to show the current choice */
365     scrollamt = MIN(scrollamt, max_choice + item_no - 1);
366     if (choice >= (max_choice + scrollamt - 1)) {
367         scrollamt = MAX(0, choice - max_choice + 1);
368         choice = max_choice - 1;
369     }
370     print_list(&all, choice, scrollamt, max_choice);
371
372     /* register the new window, along with its borders */
373     dlg_mouse_mkbigregion(all.box_y + 1, all.box_x,
374                           all.use_height, all.use_width + 2,
375                           KEY_MAX, 1, 1, 1 /* by lines */ );
376
377     dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
378
379     dlg_trace_win(dialog);
380
381     while (result == DLG_EXIT_UNKNOWN) {
382         int was_mouse;
383
384         if (button < 0)         /* --visit-items */
385             wmove(dialog, all.box_y + choice + 1, all.box_x + all.check_x + 2);
386
387         key = dlg_mouse_wgetch(dialog, &fkey);
388         if (dlg_result_key(key, fkey, &result)) {
389             if (!dlg_button_key(result, &button, &key, &fkey))
390                 break;
391         }
392
393         was_mouse = (fkey && is_DLGK_MOUSE(key));
394         if (was_mouse)
395             key -= M_EVENT;
396
397         if (was_mouse && (key >= KEY_MAX)) {
398             getyx(dialog, cur_y, cur_x);
399             i = (key - KEY_MAX);
400             if (i < max_choice) {
401                 choice = (key - KEY_MAX);
402                 print_list(&all, choice, scrollamt, max_choice);
403
404                 key = DLGK_TOGGLE;      /* force the selected item to toggle */
405             } else {
406                 beep();
407                 continue;
408             }
409             fkey = FALSE;
410         } else if (was_mouse && key >= KEY_MIN) {
411             key = dlg_lookup_key(dialog, key, &fkey);
412         }
413
414         /*
415          * A space toggles the item status.  We handle either a checklist
416          * (any number of items can be selected) or radio list (zero or one
417          * items can be selected).
418          */
419         if (key == DLGK_TOGGLE) {
420             int current = scrollamt + choice;
421             int next = items[current].state + 1;
422
423             if (next >= num_states)
424                 next = 0;
425
426             if (flag == FLAG_CHECK) {   /* checklist? */
427                 getyx(dialog, cur_y, cur_x);
428                 items[current].state = next;
429                 print_item(&all, all.list,
430                            &items[scrollamt + choice],
431                            states,
432                            choice, TRUE);
433                 (void) wnoutrefresh(all.list);
434                 (void) wmove(dialog, cur_y, cur_x);
435             } else {            /* radiolist */
436                 for (i = 0; i < item_no; i++) {
437                     if (i != current) {
438                         items[i].state = 0;
439                     }
440                 }
441                 if (items[current].state) {
442                     getyx(dialog, cur_y, cur_x);
443                     items[current].state = next ? next : 1;
444                     print_item(&all, all.list,
445                                &items[current],
446                                states,
447                                choice, TRUE);
448                     (void) wnoutrefresh(all.list);
449                     (void) wmove(dialog, cur_y, cur_x);
450                 } else {
451                     items[current].state = 1;
452                     print_list(&all, choice, scrollamt, max_choice);
453                 }
454             }
455             continue;           /* wait for another key press */
456         }
457
458         /*
459          * Check if key pressed matches first character of any item tag in
460          * list.  If there is more than one match, we will cycle through
461          * each one as the same key is pressed repeatedly.
462          */
463         found = FALSE;
464         if (!fkey) {
465             if (button < 0 || !dialog_state.visit_items) {
466                 for (j = scrollamt + choice + 1; j < item_no; j++) {
467                     if (check_hotkey(items, j)) {
468                         found = TRUE;
469                         i = j - scrollamt;
470                         break;
471                     }
472                 }
473                 if (!found) {
474                     for (j = 0; j <= scrollamt + choice; j++) {
475                         if (check_hotkey(items, j)) {
476                             found = TRUE;
477                             i = j - scrollamt;
478                             break;
479                         }
480                     }
481                 }
482                 if (found)
483                     dlg_flush_getc();
484             } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
485                 button = j;
486                 ungetch('\n');
487                 continue;
488             }
489         }
490
491         /*
492          * A single digit (1-9) positions the selection to that line in the
493          * current screen.
494          */
495         if (!found
496             && (key <= '9')
497             && (key > '0')
498             && (key - '1' < max_choice)) {
499             found = TRUE;
500             i = key - '1';
501         }
502
503         if (!found) {
504             if (fkey) {
505                 found = TRUE;
506                 switch (key) {
507                 case DLGK_ITEM_FIRST:
508                     i = -scrollamt;
509                     break;
510                 case DLGK_ITEM_LAST:
511                     i = item_no - 1 - scrollamt;
512                     break;
513                 case DLGK_PAGE_PREV:
514                     if (choice)
515                         i = 0;
516                     else if (scrollamt != 0)
517                         i = -MIN(scrollamt, max_choice);
518                     else
519                         continue;
520                     break;
521                 case DLGK_PAGE_NEXT:
522                     i = MIN(choice + max_choice, item_no - scrollamt - 1);
523                     break;
524                 case DLGK_ITEM_PREV:
525                     i = choice - 1;
526                     if (choice == 0 && scrollamt == 0)
527                         continue;
528                     break;
529                 case DLGK_ITEM_NEXT:
530                     i = choice + 1;
531                     if (scrollamt + choice >= item_no - 1)
532                         continue;
533                     break;
534                 default:
535                     found = FALSE;
536                     break;
537                 }
538             }
539         }
540
541         if (found) {
542             if (i != choice) {
543                 getyx(dialog, cur_y, cur_x);
544                 if (i < 0 || i >= max_choice) {
545                     if (i < 0) {
546                         scrollamt += i;
547                         choice = 0;
548                     } else {
549                         choice = max_choice - 1;
550                         scrollamt += (i - max_choice + 1);
551                     }
552                     print_list(&all, choice, scrollamt, max_choice);
553                 } else {
554                     choice = i;
555                     print_list(&all, choice, scrollamt, max_choice);
556                 }
557             }
558             continue;           /* wait for another key press */
559         }
560
561         if (fkey) {
562             switch (key) {
563             case DLGK_ENTER:
564                 result = dlg_enter_buttoncode(button);
565                 break;
566             case DLGK_LEAVE:
567                 result = dlg_ok_buttoncode(button);
568                 break;
569             case DLGK_FIELD_PREV:
570                 button = dlg_prev_button(buttons, button);
571                 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
572                                  FALSE, width);
573                 break;
574             case DLGK_FIELD_NEXT:
575                 button = dlg_next_button(buttons, button);
576                 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
577                                  FALSE, width);
578                 break;
579 #ifdef KEY_RESIZE
580             case KEY_RESIZE:
581                 dlg_will_resize(dialog);
582                 /* reset data */
583                 height = old_height;
584                 width = old_width;
585                 free(prompt);
586                 _dlg_resize_cleanup(dialog);
587                 /* repaint */
588                 goto retry;
589 #endif
590             default:
591                 if (was_mouse) {
592                     if ((key2 = dlg_ok_buttoncode(key)) >= 0) {
593                         result = key2;
594                         break;
595                     }
596                     beep();
597                 }
598             }
599         } else if (key > 0) {
600             beep();
601         }
602     }
603
604     dlg_del_window(dialog);
605     dlg_mouse_free_regions();
606     free(prompt);
607     *current_item = (scrollamt + choice);
608     return result;
609 }
610
611 /*
612  * Display a dialog box with a list of options that can be turned on or off
613  * The `flag' parameter is used to select between radiolist and checklist.
614  */
615 int
616 dialog_checklist(const char *title,
617                  const char *cprompt,
618                  int height,
619                  int width,
620                  int list_height,
621                  int item_no,
622                  char **items,
623                  int flag)
624 {
625     int result;
626     int i, j;
627     DIALOG_LISTITEM *listitems;
628     bool separate_output = ((flag == FLAG_CHECK)
629                             && (dialog_vars.separate_output));
630     bool show_status = FALSE;
631     int current = 0;
632     char *help_result;
633
634     listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
635     assert_ptr(listitems, "dialog_checklist");
636
637     for (i = j = 0; i < item_no; ++i) {
638         listitems[i].name = items[j++];
639         listitems[i].text = (dialog_vars.no_items
640                              ? dlg_strempty()
641                              : items[j++]);
642         listitems[i].state = !dlg_strcmp(items[j++], "on");
643         listitems[i].help = ((dialog_vars.item_help)
644                              ? items[j++]
645                              : dlg_strempty());
646     }
647     dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
648
649     result = dlg_checklist(title,
650                            cprompt,
651                            height,
652                            width,
653                            list_height,
654                            item_no,
655                            listitems,
656                            NULL,
657                            flag,
658                            &current);
659
660     switch (result) {
661     case DLG_EXIT_OK:           /* FALLTHRU */
662     case DLG_EXIT_EXTRA:
663         show_status = TRUE;
664         break;
665     case DLG_EXIT_HELP:
666         dlg_add_help_listitem(&result, &help_result, &listitems[current]);
667         if ((show_status = dialog_vars.help_status)) {
668             if (separate_output) {
669                 dlg_add_string(help_result);
670                 dlg_add_separator();
671             } else {
672                 dlg_add_quoted(help_result);
673             }
674         } else {
675             dlg_add_string(help_result);
676         }
677         break;
678     }
679
680     if (show_status) {
681         for (i = 0; i < item_no; i++) {
682             if (listitems[i].state) {
683                 if (separate_output) {
684                     dlg_add_string(listitems[i].name);
685                     dlg_add_separator();
686                 } else {
687                     if (dlg_need_separator())
688                         dlg_add_separator();
689                     if (flag == FLAG_CHECK)
690                         dlg_add_quoted(listitems[i].name);
691                     else
692                         dlg_add_string(listitems[i].name);
693                 }
694             }
695         }
696         dlg_add_last_key(separate_output);
697     }
698
699     dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
700     free(listitems);
701     return result;
702 }