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