1 /*
2  *  yesno.c -- implements the yes/no box
3  *
4  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include "dialog.h"
11 
12 /*
13  * Display termination buttons
14  */
15 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
16 {
17 	int x = width / 2 - 10;
18 	int y = height - 2;
19 
20 	print_button(dialog, gettext(" Yes "), y, x, selected == 0);
21 	print_button(dialog, gettext("  No  "), y, x + 13, selected == 1);
22 
23 	wmove(dialog, y, x + 1 + 13 * selected);
24 	wrefresh(dialog);
25 }
26 
27 /*
28  * Display a dialog box with two buttons - Yes and No
29  */
30 int dialog_yesno(const char *title, const char *prompt, int height, int width)
31 {
32 	int i, x, y, key = 0, button = 0;
33 	WINDOW *dialog;
34 
35 do_resize:
36 	if (getmaxy(stdscr) < (height + YESNO_HEIGTH_MIN))
37 		return -ERRDISPLAYTOOSMALL;
38 	if (getmaxx(stdscr) < (width + YESNO_WIDTH_MIN))
39 		return -ERRDISPLAYTOOSMALL;
40 
41 	/* center dialog box on screen */
42 	x = (getmaxx(stdscr) - width) / 2;
43 	y = (getmaxy(stdscr) - height) / 2;
44 
45 	draw_shadow(stdscr, y, x, height, width);
46 
47 	dialog = newwin(height, width, y, x);
48 	keypad(dialog, TRUE);
49 
50 	draw_box(dialog, 0, 0, height, width,
51 		 dlg.dialog.atr, dlg.border.atr);
52 	wattrset(dialog, dlg.border.atr);
53 	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
54 	for (i = 0; i < width - 2; i++)
55 		waddch(dialog, ACS_HLINE);
56 	wattrset(dialog, dlg.dialog.atr);
57 	waddch(dialog, ACS_RTEE);
58 
59 	print_title(dialog, title, width);
60 
61 	wattrset(dialog, dlg.dialog.atr);
62 	print_autowrap(dialog, prompt, width - 2, 1, 3);
63 
64 	print_buttons(dialog, height, width, 0);
65 
66 	while (key != KEY_ESC) {
67 		key = wgetch(dialog);
68 		switch (key) {
69 		case 'Y':
70 		case 'y':
71 			delwin(dialog);
72 			return 0;
73 		case 'N':
74 		case 'n':
75 			delwin(dialog);
76 			return 1;
77 
78 		case TAB:
79 		case KEY_LEFT:
80 		case KEY_RIGHT:
81 			button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button);
82 
83 			print_buttons(dialog, height, width, button);
84 			wrefresh(dialog);
85 			break;
86 		case ' ':
87 		case '\n':
88 			delwin(dialog);
89 			return button;
90 		case KEY_ESC:
91 			key = on_key_esc(dialog);
92 			break;
93 		case KEY_RESIZE:
94 			delwin(dialog);
95 			on_key_resize();
96 			goto do_resize;
97 		}
98 	}
99 
100 	delwin(dialog);
101 	return key;		/* ESC pressed */
102 }
103