xref: /third_party/toybox/kconfig/lxdialog/yesno.c (revision 0f66f451)
10f66f451Sopenharmony_ci/*
20f66f451Sopenharmony_ci *  yesno.c -- implements the yes/no box
30f66f451Sopenharmony_ci *
40f66f451Sopenharmony_ci *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
50f66f451Sopenharmony_ci *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
60f66f451Sopenharmony_ci *
70f66f451Sopenharmony_ci *  This program is free software; you can redistribute it and/or
80f66f451Sopenharmony_ci *  modify it under the terms of the GNU General Public License
90f66f451Sopenharmony_ci *  as published by the Free Software Foundation; either version 2
100f66f451Sopenharmony_ci *  of the License, or (at your option) any later version.
110f66f451Sopenharmony_ci *
120f66f451Sopenharmony_ci *  This program is distributed in the hope that it will be useful,
130f66f451Sopenharmony_ci *  but WITHOUT ANY WARRANTY; without even the implied warranty of
140f66f451Sopenharmony_ci *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
150f66f451Sopenharmony_ci *  GNU General Public License for more details.
160f66f451Sopenharmony_ci *
170f66f451Sopenharmony_ci *  You should have received a copy of the GNU General Public License
180f66f451Sopenharmony_ci *  along with this program; if not, write to the Free Software
190f66f451Sopenharmony_ci *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
200f66f451Sopenharmony_ci */
210f66f451Sopenharmony_ci
220f66f451Sopenharmony_ci#include "dialog.h"
230f66f451Sopenharmony_ci
240f66f451Sopenharmony_ci/*
250f66f451Sopenharmony_ci * Display termination buttons
260f66f451Sopenharmony_ci */
270f66f451Sopenharmony_cistatic void print_buttons(WINDOW * dialog, int height, int width, int selected)
280f66f451Sopenharmony_ci{
290f66f451Sopenharmony_ci	int x = width / 2 - 10;
300f66f451Sopenharmony_ci	int y = height - 2;
310f66f451Sopenharmony_ci
320f66f451Sopenharmony_ci	print_button(dialog, " Yes ", y, x, selected == 0);
330f66f451Sopenharmony_ci	print_button(dialog, "  No  ", y, x + 13, selected == 1);
340f66f451Sopenharmony_ci
350f66f451Sopenharmony_ci	wmove(dialog, y, x + 1 + 13 * selected);
360f66f451Sopenharmony_ci	wrefresh(dialog);
370f66f451Sopenharmony_ci}
380f66f451Sopenharmony_ci
390f66f451Sopenharmony_ci/*
400f66f451Sopenharmony_ci * Display a dialog box with two buttons - Yes and No
410f66f451Sopenharmony_ci */
420f66f451Sopenharmony_ciint dialog_yesno(const char *title, const char *prompt, int height, int width)
430f66f451Sopenharmony_ci{
440f66f451Sopenharmony_ci	int i, x, y, key = 0, button = 0;
450f66f451Sopenharmony_ci	WINDOW *dialog;
460f66f451Sopenharmony_ci
470f66f451Sopenharmony_cido_resize:
480f66f451Sopenharmony_ci	if (getmaxy(stdscr) < (height + 4))
490f66f451Sopenharmony_ci		return -ERRDISPLAYTOOSMALL;
500f66f451Sopenharmony_ci	if (getmaxx(stdscr) < (width + 4))
510f66f451Sopenharmony_ci		return -ERRDISPLAYTOOSMALL;
520f66f451Sopenharmony_ci
530f66f451Sopenharmony_ci	/* center dialog box on screen */
540f66f451Sopenharmony_ci	x = (COLS - width) / 2;
550f66f451Sopenharmony_ci	y = (LINES - height) / 2;
560f66f451Sopenharmony_ci
570f66f451Sopenharmony_ci	draw_shadow(stdscr, y, x, height, width);
580f66f451Sopenharmony_ci
590f66f451Sopenharmony_ci	dialog = newwin(height, width, y, x);
600f66f451Sopenharmony_ci	keypad(dialog, TRUE);
610f66f451Sopenharmony_ci
620f66f451Sopenharmony_ci	draw_box(dialog, 0, 0, height, width,
630f66f451Sopenharmony_ci		 dlg.dialog.atr, dlg.border.atr);
640f66f451Sopenharmony_ci	wattrset(dialog, dlg.border.atr);
650f66f451Sopenharmony_ci	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
660f66f451Sopenharmony_ci	for (i = 0; i < width - 2; i++)
670f66f451Sopenharmony_ci		waddch(dialog, ACS_HLINE);
680f66f451Sopenharmony_ci	wattrset(dialog, dlg.dialog.atr);
690f66f451Sopenharmony_ci	waddch(dialog, ACS_RTEE);
700f66f451Sopenharmony_ci
710f66f451Sopenharmony_ci	print_title(dialog, title, width);
720f66f451Sopenharmony_ci
730f66f451Sopenharmony_ci	wattrset(dialog, dlg.dialog.atr);
740f66f451Sopenharmony_ci	print_autowrap(dialog, prompt, width - 2, 1, 3);
750f66f451Sopenharmony_ci
760f66f451Sopenharmony_ci	print_buttons(dialog, height, width, 0);
770f66f451Sopenharmony_ci
780f66f451Sopenharmony_ci	while (key != KEY_ESC) {
790f66f451Sopenharmony_ci		key = wgetch(dialog);
800f66f451Sopenharmony_ci		switch (key) {
810f66f451Sopenharmony_ci		case 'Y':
820f66f451Sopenharmony_ci		case 'y':
830f66f451Sopenharmony_ci			delwin(dialog);
840f66f451Sopenharmony_ci			return 0;
850f66f451Sopenharmony_ci		case 'N':
860f66f451Sopenharmony_ci		case 'n':
870f66f451Sopenharmony_ci			delwin(dialog);
880f66f451Sopenharmony_ci			return 1;
890f66f451Sopenharmony_ci
900f66f451Sopenharmony_ci		case TAB:
910f66f451Sopenharmony_ci		case KEY_LEFT:
920f66f451Sopenharmony_ci		case KEY_RIGHT:
930f66f451Sopenharmony_ci			button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button);
940f66f451Sopenharmony_ci
950f66f451Sopenharmony_ci			print_buttons(dialog, height, width, button);
960f66f451Sopenharmony_ci			wrefresh(dialog);
970f66f451Sopenharmony_ci			break;
980f66f451Sopenharmony_ci		case ' ':
990f66f451Sopenharmony_ci		case '\n':
1000f66f451Sopenharmony_ci			delwin(dialog);
1010f66f451Sopenharmony_ci			return button;
1020f66f451Sopenharmony_ci		case KEY_ESC:
1030f66f451Sopenharmony_ci			key = on_key_esc(dialog);
1040f66f451Sopenharmony_ci			break;
1050f66f451Sopenharmony_ci		case KEY_RESIZE:
1060f66f451Sopenharmony_ci			delwin(dialog);
1070f66f451Sopenharmony_ci			on_key_resize();
1080f66f451Sopenharmony_ci			goto do_resize;
1090f66f451Sopenharmony_ci		}
1100f66f451Sopenharmony_ci	}
1110f66f451Sopenharmony_ci
1120f66f451Sopenharmony_ci	delwin(dialog);
1130f66f451Sopenharmony_ci	return key;		/* ESC pressed */
1140f66f451Sopenharmony_ci}
115