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