xref: /OK3568_Linux_fs/buildroot/support/kconfig/lxdialog/yesno.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  yesno.c -- implements the yes/no box
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5*4882a593Smuzhiyun  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  This program is free software; you can redistribute it and/or
8*4882a593Smuzhiyun  *  modify it under the terms of the GNU General Public License
9*4882a593Smuzhiyun  *  as published by the Free Software Foundation; either version 2
10*4882a593Smuzhiyun  *  of the License, or (at your option) any later version.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *  This program is distributed in the hope that it will be useful,
13*4882a593Smuzhiyun  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14*4882a593Smuzhiyun  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*4882a593Smuzhiyun  *  GNU General Public License for more details.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *  You should have received a copy of the GNU General Public License
18*4882a593Smuzhiyun  *  along with this program; if not, write to the Free Software
19*4882a593Smuzhiyun  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "dialog.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Display termination buttons
26*4882a593Smuzhiyun  */
print_buttons(WINDOW * dialog,int height,int width,int selected)27*4882a593Smuzhiyun static void print_buttons(WINDOW * dialog, int height, int width, int selected)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	int x = width / 2 - 10;
30*4882a593Smuzhiyun 	int y = height - 2;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	print_button(dialog, gettext(" Yes "), y, x, selected == 0);
33*4882a593Smuzhiyun 	print_button(dialog, gettext("  No  "), y, x + 13, selected == 1);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	wmove(dialog, y, x + 1 + 13 * selected);
36*4882a593Smuzhiyun 	wrefresh(dialog);
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun  * Display a dialog box with two buttons - Yes and No
41*4882a593Smuzhiyun  */
dialog_yesno(const char * title,const char * prompt,int height,int width)42*4882a593Smuzhiyun int dialog_yesno(const char *title, const char *prompt, int height, int width)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	int i, x, y, key = 0, button = 0;
45*4882a593Smuzhiyun 	WINDOW *dialog;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun do_resize:
48*4882a593Smuzhiyun 	if (getmaxy(stdscr) < (height + YESNO_HEIGTH_MIN))
49*4882a593Smuzhiyun 		return -ERRDISPLAYTOOSMALL;
50*4882a593Smuzhiyun 	if (getmaxx(stdscr) < (width + YESNO_WIDTH_MIN))
51*4882a593Smuzhiyun 		return -ERRDISPLAYTOOSMALL;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* center dialog box on screen */
54*4882a593Smuzhiyun 	x = (getmaxx(stdscr) - width) / 2;
55*4882a593Smuzhiyun 	y = (getmaxy(stdscr) - height) / 2;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	draw_shadow(stdscr, y, x, height, width);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	dialog = newwin(height, width, y, x);
60*4882a593Smuzhiyun 	keypad(dialog, TRUE);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	draw_box(dialog, 0, 0, height, width,
63*4882a593Smuzhiyun 		 dlg.dialog.atr, dlg.border.atr);
64*4882a593Smuzhiyun 	wattrset(dialog, dlg.border.atr);
65*4882a593Smuzhiyun 	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
66*4882a593Smuzhiyun 	for (i = 0; i < width - 2; i++)
67*4882a593Smuzhiyun 		waddch(dialog, ACS_HLINE);
68*4882a593Smuzhiyun 	wattrset(dialog, dlg.dialog.atr);
69*4882a593Smuzhiyun 	waddch(dialog, ACS_RTEE);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	print_title(dialog, title, width);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	wattrset(dialog, dlg.dialog.atr);
74*4882a593Smuzhiyun 	print_autowrap(dialog, prompt, width - 2, 1, 3);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	print_buttons(dialog, height, width, 0);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	while (key != KEY_ESC) {
79*4882a593Smuzhiyun 		key = wgetch(dialog);
80*4882a593Smuzhiyun 		switch (key) {
81*4882a593Smuzhiyun 		case 'Y':
82*4882a593Smuzhiyun 		case 'y':
83*4882a593Smuzhiyun 			delwin(dialog);
84*4882a593Smuzhiyun 			return 0;
85*4882a593Smuzhiyun 		case 'N':
86*4882a593Smuzhiyun 		case 'n':
87*4882a593Smuzhiyun 			delwin(dialog);
88*4882a593Smuzhiyun 			return 1;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 		case TAB:
91*4882a593Smuzhiyun 		case KEY_LEFT:
92*4882a593Smuzhiyun 		case KEY_RIGHT:
93*4882a593Smuzhiyun 			button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 			print_buttons(dialog, height, width, button);
96*4882a593Smuzhiyun 			wrefresh(dialog);
97*4882a593Smuzhiyun 			break;
98*4882a593Smuzhiyun 		case ' ':
99*4882a593Smuzhiyun 		case '\n':
100*4882a593Smuzhiyun 			delwin(dialog);
101*4882a593Smuzhiyun 			return button;
102*4882a593Smuzhiyun 		case KEY_ESC:
103*4882a593Smuzhiyun 			key = on_key_esc(dialog);
104*4882a593Smuzhiyun 			break;
105*4882a593Smuzhiyun 		case KEY_RESIZE:
106*4882a593Smuzhiyun 			delwin(dialog);
107*4882a593Smuzhiyun 			on_key_resize();
108*4882a593Smuzhiyun 			goto do_resize;
109*4882a593Smuzhiyun 		}
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	delwin(dialog);
113*4882a593Smuzhiyun 	return key;		/* ESC pressed */
114*4882a593Smuzhiyun }
115