kconfig/lxdialog/checklist.c
changeset 747 d3e603e7c17c
child 943 1cca90ce0481
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/kconfig/lxdialog/checklist.c	Mon Jul 28 21:32:33 2008 +0000
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 + *  checklist.c -- implements the checklist box
     1.6 + *
     1.7 + *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
     1.8 + *     Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
     1.9 + *     Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
    1.10 + *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
    1.11 + *
    1.12 + *  This program is free software; you can redistribute it and/or
    1.13 + *  modify it under the terms of the GNU General Public License
    1.14 + *  as published by the Free Software Foundation; either version 2
    1.15 + *  of the License, or (at your option) any later version.
    1.16 + *
    1.17 + *  This program is distributed in the hope that it will be useful,
    1.18 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.20 + *  GNU General Public License for more details.
    1.21 + *
    1.22 + *  You should have received a copy of the GNU General Public License
    1.23 + *  along with this program; if not, write to the Free Software
    1.24 + *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    1.25 + */
    1.26 +
    1.27 +#include "dialog.h"
    1.28 +
    1.29 +static int list_width, check_x, item_x;
    1.30 +
    1.31 +/*
    1.32 + * Print list item
    1.33 + */
    1.34 +static void print_item(WINDOW * win, int choice, int selected)
    1.35 +{
    1.36 +	int i;
    1.37 +
    1.38 +	/* Clear 'residue' of last item */
    1.39 +	wattrset(win, dlg.menubox.atr);
    1.40 +	wmove(win, choice, 0);
    1.41 +	for (i = 0; i < list_width; i++)
    1.42 +		waddch(win, ' ');
    1.43 +
    1.44 +	wmove(win, choice, check_x);
    1.45 +	wattrset(win, selected ? dlg.check_selected.atr
    1.46 +		 : dlg.check.atr);
    1.47 +	wprintw(win, "(%c)", item_is_tag('X') ? 'X' : ' ');
    1.48 +
    1.49 +	wattrset(win, selected ? dlg.tag_selected.atr : dlg.tag.atr);
    1.50 +	mvwaddch(win, choice, item_x, item_str()[0]);
    1.51 +	wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
    1.52 +	waddstr(win, (char *)item_str() + 1);
    1.53 +	if (selected) {
    1.54 +		wmove(win, choice, check_x + 1);
    1.55 +		wrefresh(win);
    1.56 +	}
    1.57 +}
    1.58 +
    1.59 +/*
    1.60 + * Print the scroll indicators.
    1.61 + */
    1.62 +static void print_arrows(WINDOW * win, int choice, int item_no, int scroll,
    1.63 +	     int y, int x, int height)
    1.64 +{
    1.65 +	wmove(win, y, x);
    1.66 +
    1.67 +	if (scroll > 0) {
    1.68 +		wattrset(win, dlg.uarrow.atr);
    1.69 +		waddch(win, ACS_UARROW);
    1.70 +		waddstr(win, "(-)");
    1.71 +	} else {
    1.72 +		wattrset(win, dlg.menubox.atr);
    1.73 +		waddch(win, ACS_HLINE);
    1.74 +		waddch(win, ACS_HLINE);
    1.75 +		waddch(win, ACS_HLINE);
    1.76 +		waddch(win, ACS_HLINE);
    1.77 +	}
    1.78 +
    1.79 +	y = y + height + 1;
    1.80 +	wmove(win, y, x);
    1.81 +
    1.82 +	if ((height < item_no) && (scroll + choice < item_no - 1)) {
    1.83 +		wattrset(win, dlg.darrow.atr);
    1.84 +		waddch(win, ACS_DARROW);
    1.85 +		waddstr(win, "(+)");
    1.86 +	} else {
    1.87 +		wattrset(win, dlg.menubox_border.atr);
    1.88 +		waddch(win, ACS_HLINE);
    1.89 +		waddch(win, ACS_HLINE);
    1.90 +		waddch(win, ACS_HLINE);
    1.91 +		waddch(win, ACS_HLINE);
    1.92 +	}
    1.93 +}
    1.94 +
    1.95 +/*
    1.96 + *  Display the termination buttons
    1.97 + */
    1.98 +static void print_buttons(WINDOW * dialog, int height, int width, int selected)
    1.99 +{
   1.100 +	int x = width / 2 - 11;
   1.101 +	int y = height - 2;
   1.102 +
   1.103 +	print_button(dialog, "Select", y, x, selected == 0);
   1.104 +	print_button(dialog, " Help ", y, x + 14, selected == 1);
   1.105 +
   1.106 +	wmove(dialog, y, x + 1 + 14 * selected);
   1.107 +	wrefresh(dialog);
   1.108 +}
   1.109 +
   1.110 +/*
   1.111 + * Display a dialog box with a list of options that can be turned on or off
   1.112 + * in the style of radiolist (only one option turned on at a time).
   1.113 + */
   1.114 +int dialog_checklist(const char *title, const char *prompt, int height,
   1.115 +		     int width, int list_height)
   1.116 +{
   1.117 +	int i, x, y, box_x, box_y;
   1.118 +	int key = 0, button = 0, choice = 0, scroll = 0, max_choice;
   1.119 +	WINDOW *dialog, *list;
   1.120 +
   1.121 +	/* which item to highlight */
   1.122 +	item_foreach() {
   1.123 +		if (item_is_tag('X'))
   1.124 +			choice = item_n();
   1.125 +		if (item_is_selected()) {
   1.126 +			choice = item_n();
   1.127 +			break;
   1.128 +		}
   1.129 +	}
   1.130 +
   1.131 +do_resize:
   1.132 +	if (getmaxy(stdscr) < (height + 6))
   1.133 +		return -ERRDISPLAYTOOSMALL;
   1.134 +	if (getmaxx(stdscr) < (width + 6))
   1.135 +		return -ERRDISPLAYTOOSMALL;
   1.136 +
   1.137 +	max_choice = MIN(list_height, item_count());
   1.138 +
   1.139 +	/* center dialog box on screen */
   1.140 +	x = (COLS - width) / 2;
   1.141 +	y = (LINES - height) / 2;
   1.142 +
   1.143 +	draw_shadow(stdscr, y, x, height, width);
   1.144 +
   1.145 +	dialog = newwin(height, width, y, x);
   1.146 +	keypad(dialog, TRUE);
   1.147 +
   1.148 +	draw_box(dialog, 0, 0, height, width,
   1.149 +		 dlg.dialog.atr, dlg.border.atr);
   1.150 +	wattrset(dialog, dlg.border.atr);
   1.151 +	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
   1.152 +	for (i = 0; i < width - 2; i++)
   1.153 +		waddch(dialog, ACS_HLINE);
   1.154 +	wattrset(dialog, dlg.dialog.atr);
   1.155 +	waddch(dialog, ACS_RTEE);
   1.156 +
   1.157 +	print_title(dialog, title, width);
   1.158 +
   1.159 +	wattrset(dialog, dlg.dialog.atr);
   1.160 +	print_autowrap(dialog, prompt, width - 2, 1, 3);
   1.161 +
   1.162 +	list_width = width - 6;
   1.163 +	box_y = height - list_height - 5;
   1.164 +	box_x = (width - list_width) / 2 - 1;
   1.165 +
   1.166 +	/* create new window for the list */
   1.167 +	list = subwin(dialog, list_height, list_width, y + box_y + 1,
   1.168 +	              x + box_x + 1);
   1.169 +
   1.170 +	keypad(list, TRUE);
   1.171 +
   1.172 +	/* draw a box around the list items */
   1.173 +	draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2,
   1.174 +	         dlg.menubox_border.atr, dlg.menubox.atr);
   1.175 +
   1.176 +	/* Find length of longest item in order to center checklist */
   1.177 +	check_x = 0;
   1.178 +	item_foreach()
   1.179 +		check_x = MAX(check_x, strlen(item_str()) + 4);
   1.180 +
   1.181 +	check_x = (list_width - check_x) / 2;
   1.182 +	item_x = check_x + 4;
   1.183 +
   1.184 +	if (choice >= list_height) {
   1.185 +		scroll = choice - list_height + 1;
   1.186 +		choice -= scroll;
   1.187 +	}
   1.188 +
   1.189 +	/* Print the list */
   1.190 +	for (i = 0; i < max_choice; i++) {
   1.191 +		item_set(scroll + i);
   1.192 +		print_item(list, i, i == choice);
   1.193 +	}
   1.194 +
   1.195 +	print_arrows(dialog, choice, item_count(), scroll,
   1.196 +		     box_y, box_x + check_x + 5, list_height);
   1.197 +
   1.198 +	print_buttons(dialog, height, width, 0);
   1.199 +
   1.200 +	wnoutrefresh(dialog);
   1.201 +	wnoutrefresh(list);
   1.202 +	doupdate();
   1.203 +
   1.204 +	while (key != KEY_ESC) {
   1.205 +		key = wgetch(dialog);
   1.206 +
   1.207 +		for (i = 0; i < max_choice; i++) {
   1.208 +			item_set(i + scroll);
   1.209 +			if (toupper(key) == toupper(item_str()[0]))
   1.210 +				break;
   1.211 +		}
   1.212 +
   1.213 +		if (i < max_choice || key == KEY_UP || key == KEY_DOWN ||
   1.214 +		    key == '+' || key == '-') {
   1.215 +			if (key == KEY_UP || key == '-') {
   1.216 +				if (!choice) {
   1.217 +					if (!scroll)
   1.218 +						continue;
   1.219 +					/* Scroll list down */
   1.220 +					if (list_height > 1) {
   1.221 +						/* De-highlight current first item */
   1.222 +						item_set(scroll);
   1.223 +						print_item(list, 0, FALSE);
   1.224 +						scrollok(list, TRUE);
   1.225 +						wscrl(list, -1);
   1.226 +						scrollok(list, FALSE);
   1.227 +					}
   1.228 +					scroll--;
   1.229 +					item_set(scroll);
   1.230 +					print_item(list, 0, TRUE);
   1.231 +					print_arrows(dialog, choice, item_count(),
   1.232 +						     scroll, box_y, box_x + check_x + 5, list_height);
   1.233 +
   1.234 +					wnoutrefresh(dialog);
   1.235 +					wrefresh(list);
   1.236 +
   1.237 +					continue;	/* wait for another key press */
   1.238 +				} else
   1.239 +					i = choice - 1;
   1.240 +			} else if (key == KEY_DOWN || key == '+') {
   1.241 +				if (choice == max_choice - 1) {
   1.242 +					if (scroll + choice >= item_count() - 1)
   1.243 +						continue;
   1.244 +					/* Scroll list up */
   1.245 +					if (list_height > 1) {
   1.246 +						/* De-highlight current last item before scrolling up */
   1.247 +						item_set(scroll + max_choice - 1);
   1.248 +						print_item(list,
   1.249 +							    max_choice - 1,
   1.250 +							    FALSE);
   1.251 +						scrollok(list, TRUE);
   1.252 +						wscrl(list, 1);
   1.253 +						scrollok(list, FALSE);
   1.254 +					}
   1.255 +					scroll++;
   1.256 +					item_set(scroll + max_choice - 1);
   1.257 +					print_item(list, max_choice - 1, TRUE);
   1.258 +
   1.259 +					print_arrows(dialog, choice, item_count(),
   1.260 +						     scroll, box_y, box_x + check_x + 5, list_height);
   1.261 +
   1.262 +					wnoutrefresh(dialog);
   1.263 +					wrefresh(list);
   1.264 +
   1.265 +					continue;	/* wait for another key press */
   1.266 +				} else
   1.267 +					i = choice + 1;
   1.268 +			}
   1.269 +			if (i != choice) {
   1.270 +				/* De-highlight current item */
   1.271 +				item_set(scroll + choice);
   1.272 +				print_item(list, choice, FALSE);
   1.273 +				/* Highlight new item */
   1.274 +				choice = i;
   1.275 +				item_set(scroll + choice);
   1.276 +				print_item(list, choice, TRUE);
   1.277 +				wnoutrefresh(dialog);
   1.278 +				wrefresh(list);
   1.279 +			}
   1.280 +			continue;	/* wait for another key press */
   1.281 +		}
   1.282 +		switch (key) {
   1.283 +		case 'H':
   1.284 +		case 'h':
   1.285 +		case '?':
   1.286 +			button = 1;
   1.287 +			/* fall-through */
   1.288 +		case 'S':
   1.289 +		case 's':
   1.290 +		case ' ':
   1.291 +		case '\n':
   1.292 +			item_foreach()
   1.293 +				item_set_selected(0);
   1.294 +			item_set(scroll + choice);
   1.295 +			item_set_selected(1);
   1.296 +			delwin(list);
   1.297 +			delwin(dialog);
   1.298 +			return button;
   1.299 +		case TAB:
   1.300 +		case KEY_LEFT:
   1.301 +		case KEY_RIGHT:
   1.302 +			button = ((key == KEY_LEFT ? --button : ++button) < 0)
   1.303 +			    ? 1 : (button > 1 ? 0 : button);
   1.304 +
   1.305 +			print_buttons(dialog, height, width, button);
   1.306 +			wrefresh(dialog);
   1.307 +			break;
   1.308 +		case 'X':
   1.309 +		case 'x':
   1.310 +			key = KEY_ESC;
   1.311 +			break;
   1.312 +		case KEY_ESC:
   1.313 +			key = on_key_esc(dialog);
   1.314 +			break;
   1.315 +		case KEY_RESIZE:
   1.316 +			delwin(list);
   1.317 +			delwin(dialog);
   1.318 +			on_key_resize();
   1.319 +			goto do_resize;
   1.320 +		}
   1.321 +
   1.322 +		/* Now, update everything... */
   1.323 +		doupdate();
   1.324 +	}
   1.325 +	delwin(list);
   1.326 +	delwin(dialog);
   1.327 +	return key;		/* ESC pressed */
   1.328 +}