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