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