2 * $Id: treeview.c,v 1.43 2020/11/23 00:38:31 tom Exp $
4 * treeview.c -- implements the treeview dialog
6 * Copyright 2012-2019,2020 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.
24 #include <dlg_internals.h>
28 #define MIN_HIGH (1 + (5 * MARGIN))
31 /* the outer-window */
40 /* the inner-window */
42 DIALOG_LISTITEM *items;
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.
53 print_item(ALL_DATA * data,
54 DIALOG_LISTITEM * item,
60 WINDOW *win = data->list;
61 chtype save = dlg_get_attrs(win);
64 int climit = (getmaxx(win) - data->check_x + 1);
65 const char *show = (dialog_vars.no_items
69 /* Clear 'residue' of last item */
70 dlg_attrset(win, menubox_attr);
71 (void) wmove(win, choice, 0);
72 for (i = 0; i < data->use_width; i++)
73 (void) waddch(win, ' ');
75 (void) wmove(win, choice, data->check_x);
76 dlg_attrset(win, selected ? check_selected_attr : check_attr);
78 data->is_check ? "[%c]" : "(%c)",
80 dlg_attrset(win, menubox_attr);
82 dlg_attrset(win, selected ? item_selected_attr : item_attr);
83 for (i = 0; i < depths; ++i) {
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, ' ');
90 (void) wmove(win, choice, data->item_x + INDENT * depths);
92 dlg_print_listitem(win, show, climit, first, selected);
95 dlg_item_help(item->help);
97 dlg_attrset(win, save);
101 print_list(ALL_DATA * data,
109 getyx(data->dialog, cur_y, cur_x);
111 for (i = 0; i < max_choice; i++) {
113 &data->items[scrollamt + i],
115 data->depths[scrollamt + i],
118 (void) wnoutrefresh(data->list);
120 dlg_draw_scrollbar(data->dialog,
123 (long) (scrollamt + max_choice),
124 (long) (data->item_no),
125 data->box_x + data->check_x,
126 data->box_x + data->use_width,
128 data->box_y + data->use_height + 1,
129 menubox_border2_attr,
130 menubox_border_attr);
132 (void) wmove(data->dialog, cur_y, cur_x);
136 check_hotkey(DIALOG_LISTITEM * items, int choice)
140 if (dlg_match_char(dlg_last_getc(),
143 : items[choice].name))) {
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
155 dlg_treeview(const char *title,
161 DIALOG_LISTITEM * items,
168 static DLG_KEYS_BINDING binding[] = {
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) ),
194 int old_height = height;
195 int old_width = width;
198 int i, j, key2, found, x, y, cur_y, box_x, box_y;
200 int button = dialog_state.visit_items ? -1 : dlg_default_button();
201 int choice = dlg_default_listitem(items);
205 int use_width, name_width, text_width, tree_width;
206 int result = DLG_EXIT_UNKNOWN;
208 WINDOW *dialog, *list;
209 char *prompt = dlg_strclone(cprompt);
210 const char **buttons = dlg_ok_labels();
211 const char *widget_name;
213 /* we need at least two states */
214 if (states == 0 || strlen(states) < 2)
216 num_states = (int) strlen(states);
218 dialog_state.plain_buttons = TRUE;
220 memset(&all, 0, sizeof(all));
222 all.item_no = item_no;
227 dlg_tab_correct_str(prompt);
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.
234 if (flag == FLAG_RADIO) {
237 for (i = 0; i < item_no; i++) {
238 if (items[i].state) {
249 widget_name = "treeview";
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);
262 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, use_width);
264 dlg_button_layout(buttons, &width);
265 dlg_print_size(height, width);
266 dlg_ctl_size(height, width);
268 x = dlg_box_x_ordinate(width);
269 y = dlg_box_y_ordinate(height);
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);
275 dlg_mouse_setbase(x, y);
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);
281 dlg_attrset(dialog, dialog_attr);
282 dlg_print_autowrap(dialog, prompt, height, width);
284 all.use_width = width - 4;
285 cur_y = getcury(dialog);
287 box_x = (width - all.use_width) / 2 - 1;
290 * After displaying the prompt, we know how much space we really have.
291 * Limit the list to avoid overwriting the ok-button.
293 use_height = height - MIN_HIGH - cur_y;
297 max_choice = MIN(use_height, item_no);
299 /* create new window for the list */
300 list = dlg_sub_window(dialog, use_height, all.use_width,
301 y + box_y + 1, x + box_x + 1);
303 /* draw a box around the list items */
304 dlg_draw_box(dialog, box_y, box_x,
305 use_height + 2 * MARGIN,
306 all.use_width + 2 * MARGIN,
307 menubox_border_attr, menubox_border2_attr);
312 /* Find length of longest item to center treeview */
313 for (i = 0; i < item_no; i++) {
314 tree_width = MAX(tree_width, INDENT * depths[i]);
315 text_width = MAX(text_width, dlg_count_columns(items[i].text));
316 name_width = MAX(name_width, dlg_count_columns(items[i].name));
318 if (dialog_vars.no_tags && !dialog_vars.no_items) {
319 tree_width += text_width;
320 } else if (dialog_vars.no_items) {
321 tree_width += name_width;
323 tree_width += (text_width + name_width);
326 use_width = (all.use_width - 4);
327 tree_width = MIN(tree_width, all.use_width);
329 all.check_x = (use_width - tree_width) / 2;
330 all.item_x = ((dialog_vars.no_tags
332 : (dialog_vars.no_items
337 /* ensure we are scrolled to show the current choice */
338 if (choice >= (max_choice + scrollamt)) {
339 scrollamt = choice - max_choice + 1;
340 choice = max_choice - 1;
343 /* register the new window, along with its borders */
344 dlg_mouse_mkbigregion(box_y + 1, box_x,
345 use_height, all.use_width + 2,
346 KEY_MAX, 1, 1, 1 /* by lines */ );
351 all.use_height = use_height;
353 print_list(&all, choice, scrollamt, max_choice);
355 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
357 dlg_trace_win(dialog);
359 while (result == DLG_EXIT_UNKNOWN) {
362 if (button < 0) /* --visit-items */
363 wmove(dialog, box_y + choice + 1, box_x + all.check_x + 2);
365 key = dlg_mouse_wgetch(dialog, &fkey);
366 if (dlg_result_key(key, fkey, &result)) {
367 if (!dlg_button_key(result, &button, &key, &fkey))
371 was_mouse = (fkey && is_DLGK_MOUSE(key));
375 if (was_mouse && (key >= KEY_MAX)) {
377 if (i < max_choice) {
378 choice = (key - KEY_MAX);
379 print_list(&all, choice, scrollamt, max_choice);
381 key = DLGK_TOGGLE; /* force the selected item to toggle */
387 } else if (was_mouse && key >= KEY_MIN) {
388 key = dlg_lookup_key(dialog, key, &fkey);
392 * A space toggles the item status.
394 if (key == DLGK_TOGGLE) {
395 int current = scrollamt + choice;
396 int next = items[current].state + 1;
398 if (next >= num_states)
401 if (flag == FLAG_CHECK) { /* checklist? */
402 items[current].state = next;
404 for (i = 0; i < item_no; i++) {
409 if (items[current].state) {
410 items[current].state = next ? next : 1;
412 items[current].state = 1;
415 print_list(&all, choice, scrollamt, max_choice);
416 continue; /* wait for another key press */
420 * Check if key pressed matches first character of any item tag in
421 * list. If there is more than one match, we will cycle through
422 * each one as the same key is pressed repeatedly.
426 if (button < 0 || !dialog_state.visit_items) {
427 for (j = scrollamt + choice + 1; j < item_no; j++) {
428 if (check_hotkey(items, j)) {
435 for (j = 0; j <= scrollamt + choice; j++) {
436 if (check_hotkey(items, j)) {
445 } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
453 * A single digit (1-9) positions the selection to that line in the
459 && (key - '1' < max_choice)) {
468 case DLGK_ITEM_FIRST:
472 i = item_no - 1 - scrollamt;
477 else if (scrollamt != 0)
478 i = -MIN(scrollamt, max_choice);
483 i = MIN(choice + max_choice, item_no - scrollamt - 1);
487 if (choice == 0 && scrollamt == 0)
492 if (scrollamt + choice >= item_no - 1)
504 if (i < 0 || i >= max_choice) {
509 choice = max_choice - 1;
510 scrollamt += (i - max_choice + 1);
512 print_list(&all, choice, scrollamt, max_choice);
515 print_list(&all, choice, scrollamt, max_choice);
518 continue; /* wait for another key press */
524 result = dlg_enter_buttoncode(button);
527 result = dlg_ok_buttoncode(button);
529 case DLGK_FIELD_PREV:
530 button = dlg_prev_button(buttons, button);
531 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
534 case DLGK_FIELD_NEXT:
535 button = dlg_next_button(buttons, button);
536 dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
541 dlg_will_resize(dialog);
546 _dlg_resize_cleanup(dialog);
551 if ((key2 = dlg_ok_buttoncode(key)) >= 0) {
558 } else if (key > 0) {
563 dlg_del_window(dialog);
564 dlg_mouse_free_regions();
566 *current_item = (scrollamt + choice);
571 * Display a set of items as a tree.
574 dialog_treeview(const char *title,
585 DIALOG_LISTITEM *listitems;
587 bool show_status = FALSE;
591 DLG_TRACE(("# treeview args:\n"));
592 DLG_TRACE2S("title", title);
593 DLG_TRACE2S("message", cprompt);
594 DLG_TRACE2N("height", height);
595 DLG_TRACE2N("width", width);
596 DLG_TRACE2N("lheight", list_height);
597 DLG_TRACE2N("llength", item_no);
598 /* FIXME dump the items[][] too */
599 DLG_TRACE2N("flag", flag);
601 listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
602 assert_ptr(listitems, "dialog_treeview");
604 depths = dlg_calloc(int, (size_t) item_no + 1);
605 assert_ptr(depths, "dialog_treeview");
607 for (i = j = 0; i < item_no; ++i) {
608 listitems[i].name = items[j++];
609 listitems[i].text = (dialog_vars.no_items
612 listitems[i].state = !dlg_strcmp(items[j++], "on");
613 depths[i] = atoi(items[j++]);
614 listitems[i].help = ((dialog_vars.item_help)
618 dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
620 result = dlg_treeview(title,
633 case DLG_EXIT_OK: /* FALLTHRU */
638 dlg_add_help_listitem(&result, &help_result, &listitems[current]);
639 if ((show_status = dialog_vars.help_status)) {
640 if (dialog_vars.separate_output) {
641 dlg_add_string(help_result);
644 dlg_add_quoted(help_result);
647 dlg_add_string(help_result);
653 for (i = 0; i < item_no; i++) {
654 if (listitems[i].state) {
655 if (dlg_need_separator())
657 if (dialog_vars.separate_output) {
658 dlg_add_string(listitems[i].name);
660 if (flag == FLAG_CHECK)
661 dlg_add_quoted(listitems[i].name);
663 dlg_add_string(listitems[i].name);
670 dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);