blob: 8364f9dd01c3340f8676cc9d7c70ad35a599e0f2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "dialog.h"
23
24/*
25 * Display termination buttons
26 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010027static void print_buttons(WINDOW * dialog, int height, int width, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010029 int x = width / 2 - 10;
30 int y = height - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010032 print_button(dialog, " Yes ", y, x, selected == 0);
33 print_button(dialog, " No ", y, x + 13, selected == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010035 wmove(dialog, y, x + 1 + 13 * selected);
36 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39/*
40 * Display a dialog box with two buttons - Yes and No
41 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010042int dialog_yesno(const char *title, const char *prompt, int height, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010044 int i, x, y, key = 0, button = 0;
45 WINDOW *dialog;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010047 /* center dialog box on screen */
48 x = (COLS - width) / 2;
49 y = (LINES - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010051 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010053 dialog = newwin(height, width, y, x);
54 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Sam Ravnborg98e5a152006-07-24 21:40:46 +020056 draw_box(dialog, 0, 0, height, width,
57 dlg.dialog.atr, dlg.border.atr);
58 wattrset(dialog, dlg.border.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010059 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
60 for (i = 0; i < width - 2; i++)
61 waddch(dialog, ACS_HLINE);
Sam Ravnborg98e5a152006-07-24 21:40:46 +020062 wattrset(dialog, dlg.dialog.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010063 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Sam Ravnborgfa7009d2005-11-19 23:38:06 +010065 print_title(dialog, title, width);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010066
Sam Ravnborg98e5a152006-07-24 21:40:46 +020067 wattrset(dialog, dlg.dialog.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010068 print_autowrap(dialog, prompt, width - 2, 1, 3);
69
70 print_buttons(dialog, height, width, 0);
71
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +020072 while (key != KEY_ESC) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010073 key = wgetch(dialog);
74 switch (key) {
75 case 'Y':
76 case 'y':
77 delwin(dialog);
78 return 0;
79 case 'N':
80 case 'n':
81 delwin(dialog);
82 return 1;
83
84 case TAB:
85 case KEY_LEFT:
86 case KEY_RIGHT:
Sam Ravnborgdec69da2005-11-19 21:56:20 +010087 button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010088
89 print_buttons(dialog, height, width, button);
90 wrefresh(dialog);
91 break;
92 case ' ':
93 case '\n':
94 delwin(dialog);
95 return button;
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +020096 case KEY_ESC:
97 key = on_key_esc(dialog);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010098 break;
99 }
100 }
101
102 delwin(dialog);
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +0200103 return key; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}