kconfig/lxdialog/inputbox.c
changeset 1 eeea35fbf182
child 943 1cca90ce0481
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/kconfig/lxdialog/inputbox.c	Sat Feb 24 11:00:05 2007 +0000
     1.3 @@ -0,0 +1,238 @@
     1.4 +/*
     1.5 + *  inputbox.c -- implements the input box
     1.6 + *
     1.7 + *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
     1.8 + *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
     1.9 + *
    1.10 + *  This program is free software; you can redistribute it and/or
    1.11 + *  modify it under the terms of the GNU General Public License
    1.12 + *  as published by the Free Software Foundation; either version 2
    1.13 + *  of the License, or (at your option) any later version.
    1.14 + *
    1.15 + *  This program is distributed in the hope that it will be useful,
    1.16 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 + *  GNU General Public License for more details.
    1.19 + *
    1.20 + *  You should have received a copy of the GNU General Public License
    1.21 + *  along with this program; if not, write to the Free Software
    1.22 + *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    1.23 + */
    1.24 +
    1.25 +#include "dialog.h"
    1.26 +
    1.27 +char dialog_input_result[MAX_LEN + 1];
    1.28 +
    1.29 +/*
    1.30 + *  Print the termination buttons
    1.31 + */
    1.32 +static void print_buttons(WINDOW * dialog, int height, int width, int selected)
    1.33 +{
    1.34 +	int x = width / 2 - 11;
    1.35 +	int y = height - 2;
    1.36 +
    1.37 +	print_button(dialog, "  Ok  ", y, x, selected == 0);
    1.38 +	print_button(dialog, " Help ", y, x + 14, selected == 1);
    1.39 +
    1.40 +	wmove(dialog, y, x + 1 + 14 * selected);
    1.41 +	wrefresh(dialog);
    1.42 +}
    1.43 +
    1.44 +/*
    1.45 + * Display a dialog box for inputing a string
    1.46 + */
    1.47 +int dialog_inputbox(const char *title, const char *prompt, int height, int width,
    1.48 +                    const char *init)
    1.49 +{
    1.50 +	int i, x, y, box_y, box_x, box_width;
    1.51 +	int input_x = 0, scroll = 0, key = 0, button = -1;
    1.52 +	char *instr = dialog_input_result;
    1.53 +	WINDOW *dialog;
    1.54 +
    1.55 +	if (!init)
    1.56 +		instr[0] = '\0';
    1.57 +	else
    1.58 +		strcpy(instr, init);
    1.59 +
    1.60 +do_resize:
    1.61 +	if (getmaxy(stdscr) <= (height - 2))
    1.62 +		return -ERRDISPLAYTOOSMALL;
    1.63 +	if (getmaxx(stdscr) <= (width - 2))
    1.64 +		return -ERRDISPLAYTOOSMALL;
    1.65 +
    1.66 +	/* center dialog box on screen */
    1.67 +	x = (COLS - width) / 2;
    1.68 +	y = (LINES - height) / 2;
    1.69 +
    1.70 +	draw_shadow(stdscr, y, x, height, width);
    1.71 +
    1.72 +	dialog = newwin(height, width, y, x);
    1.73 +	keypad(dialog, TRUE);
    1.74 +
    1.75 +	draw_box(dialog, 0, 0, height, width,
    1.76 +		 dlg.dialog.atr, dlg.border.atr);
    1.77 +	wattrset(dialog, dlg.border.atr);
    1.78 +	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
    1.79 +	for (i = 0; i < width - 2; i++)
    1.80 +		waddch(dialog, ACS_HLINE);
    1.81 +	wattrset(dialog, dlg.dialog.atr);
    1.82 +	waddch(dialog, ACS_RTEE);
    1.83 +
    1.84 +	print_title(dialog, title, width);
    1.85 +
    1.86 +	wattrset(dialog, dlg.dialog.atr);
    1.87 +	print_autowrap(dialog, prompt, width - 2, 1, 3);
    1.88 +
    1.89 +	/* Draw the input field box */
    1.90 +	box_width = width - 6;
    1.91 +	getyx(dialog, y, x);
    1.92 +	box_y = y + 2;
    1.93 +	box_x = (width - box_width) / 2;
    1.94 +	draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
    1.95 +		 dlg.border.atr, dlg.dialog.atr);
    1.96 +
    1.97 +	print_buttons(dialog, height, width, 0);
    1.98 +
    1.99 +	/* Set up the initial value */
   1.100 +	wmove(dialog, box_y, box_x);
   1.101 +	wattrset(dialog, dlg.inputbox.atr);
   1.102 +
   1.103 +	input_x = strlen(instr);
   1.104 +
   1.105 +	if (input_x >= box_width) {
   1.106 +		scroll = input_x - box_width + 1;
   1.107 +		input_x = box_width - 1;
   1.108 +		for (i = 0; i < box_width - 1; i++)
   1.109 +			waddch(dialog, instr[scroll + i]);
   1.110 +	} else {
   1.111 +		waddstr(dialog, instr);
   1.112 +	}
   1.113 +
   1.114 +	wmove(dialog, box_y, box_x + input_x);
   1.115 +
   1.116 +	wrefresh(dialog);
   1.117 +
   1.118 +	while (key != KEY_ESC) {
   1.119 +		key = wgetch(dialog);
   1.120 +
   1.121 +		if (button == -1) {	/* Input box selected */
   1.122 +			switch (key) {
   1.123 +			case TAB:
   1.124 +			case KEY_UP:
   1.125 +			case KEY_DOWN:
   1.126 +				break;
   1.127 +			case KEY_LEFT:
   1.128 +				continue;
   1.129 +			case KEY_RIGHT:
   1.130 +				continue;
   1.131 +			case KEY_BACKSPACE:
   1.132 +			case 127:
   1.133 +				if (input_x || scroll) {
   1.134 +					wattrset(dialog, dlg.inputbox.atr);
   1.135 +					if (!input_x) {
   1.136 +						scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
   1.137 +						wmove(dialog, box_y, box_x);
   1.138 +						for (i = 0; i < box_width; i++)
   1.139 +							waddch(dialog,
   1.140 +							       instr[scroll + input_x + i] ?
   1.141 +							       instr[scroll + input_x + i] : ' ');
   1.142 +						input_x = strlen(instr) - scroll;
   1.143 +					} else
   1.144 +						input_x--;
   1.145 +					instr[scroll + input_x] = '\0';
   1.146 +					mvwaddch(dialog, box_y, input_x + box_x, ' ');
   1.147 +					wmove(dialog, box_y, input_x + box_x);
   1.148 +					wrefresh(dialog);
   1.149 +				}
   1.150 +				continue;
   1.151 +			default:
   1.152 +				if (key < 0x100 && isprint(key)) {
   1.153 +					if (scroll + input_x < MAX_LEN) {
   1.154 +						wattrset(dialog, dlg.inputbox.atr);
   1.155 +						instr[scroll + input_x] = key;
   1.156 +						instr[scroll + input_x + 1] = '\0';
   1.157 +						if (input_x == box_width - 1) {
   1.158 +							scroll++;
   1.159 +							wmove(dialog, box_y, box_x);
   1.160 +							for (i = 0; i < box_width - 1; i++)
   1.161 +								waddch(dialog, instr [scroll + i]);
   1.162 +						} else {
   1.163 +							wmove(dialog, box_y, input_x++ + box_x);
   1.164 +							waddch(dialog, key);
   1.165 +						}
   1.166 +						wrefresh(dialog);
   1.167 +					} else
   1.168 +						flash();	/* Alarm user about overflow */
   1.169 +					continue;
   1.170 +				}
   1.171 +			}
   1.172 +		}
   1.173 +		switch (key) {
   1.174 +		case 'O':
   1.175 +		case 'o':
   1.176 +			delwin(dialog);
   1.177 +			return 0;
   1.178 +		case 'H':
   1.179 +		case 'h':
   1.180 +			delwin(dialog);
   1.181 +			return 1;
   1.182 +		case KEY_UP:
   1.183 +		case KEY_LEFT:
   1.184 +			switch (button) {
   1.185 +			case -1:
   1.186 +				button = 1;	/* Indicates "Cancel" button is selected */
   1.187 +				print_buttons(dialog, height, width, 1);
   1.188 +				break;
   1.189 +			case 0:
   1.190 +				button = -1;	/* Indicates input box is selected */
   1.191 +				print_buttons(dialog, height, width, 0);
   1.192 +				wmove(dialog, box_y, box_x + input_x);
   1.193 +				wrefresh(dialog);
   1.194 +				break;
   1.195 +			case 1:
   1.196 +				button = 0;	/* Indicates "OK" button is selected */
   1.197 +				print_buttons(dialog, height, width, 0);
   1.198 +				break;
   1.199 +			}
   1.200 +			break;
   1.201 +		case TAB:
   1.202 +		case KEY_DOWN:
   1.203 +		case KEY_RIGHT:
   1.204 +			switch (button) {
   1.205 +			case -1:
   1.206 +				button = 0;	/* Indicates "OK" button is selected */
   1.207 +				print_buttons(dialog, height, width, 0);
   1.208 +				break;
   1.209 +			case 0:
   1.210 +				button = 1;	/* Indicates "Cancel" button is selected */
   1.211 +				print_buttons(dialog, height, width, 1);
   1.212 +				break;
   1.213 +			case 1:
   1.214 +				button = -1;	/* Indicates input box is selected */
   1.215 +				print_buttons(dialog, height, width, 0);
   1.216 +				wmove(dialog, box_y, box_x + input_x);
   1.217 +				wrefresh(dialog);
   1.218 +				break;
   1.219 +			}
   1.220 +			break;
   1.221 +		case ' ':
   1.222 +		case '\n':
   1.223 +			delwin(dialog);
   1.224 +			return (button == -1 ? 0 : button);
   1.225 +		case 'X':
   1.226 +		case 'x':
   1.227 +			key = KEY_ESC;
   1.228 +			break;
   1.229 +		case KEY_ESC:
   1.230 +			key = on_key_esc(dialog);
   1.231 +			break;
   1.232 +		case KEY_RESIZE:
   1.233 +			delwin(dialog);
   1.234 +			on_key_resize();
   1.235 +			goto do_resize;
   1.236 +		}
   1.237 +	}
   1.238 +
   1.239 +	delwin(dialog);
   1.240 +	return KEY_ESC;		/* ESC pressed */
   1.241 +}