Merge from vendor branch TCPDUMP:
[dragonfly.git] / gnu / lib / libdialog / yesno.c
1 /*
2  *  yesno.c -- implements the yes/no box
3  *
4  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version 2
9  *  of the License, or (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $FreeBSD: src/gnu/lib/libdialog/yesno.c,v 1.11.8.3 2001/07/31 20:34:00 eric Exp $
21  * $DragonFly: src/gnu/lib/libdialog/yesno.c,v 1.2 2003/06/17 04:25:43 dillon Exp $
22  */
23
24 #include <dialog.h>
25 #include "dialog.priv.h"
26
27 /* Actual work function */
28 static int dialog_yesno_proc(unsigned char *title, unsigned char *prompt,
29                              int height, int width, int yesdefault);
30
31 /*
32  * Display a dialog box with two buttons - Yes and No
33  */
34 int
35 dialog_yesno(unsigned char *title, unsigned char *prompt, int height, int width)
36 {
37   return dialog_yesno_proc(title, prompt, height, width, TRUE);
38 }
39
40 /*
41  * Display a dialog box with two buttons - No and Yes
42  */
43 int
44 dialog_noyes(unsigned char *title, unsigned char *prompt, int height, int width)
45 {
46   return dialog_yesno_proc(title, prompt, height, width, FALSE);
47 }
48
49 static int
50 dialog_yesno_proc(unsigned char *title, unsigned char *prompt, int height, int width, int yesdefault)
51 {
52   int i, j, x, y, key, button;
53   WINDOW *dialog;
54   char *tmphlp;
55
56   /* disable helpline */
57   tmphlp = get_helpline();
58   use_helpline(NULL);
59
60   if (height < 0)
61         height = strheight(prompt)+4;
62   if (width < 0) {
63         i = strwidth(prompt);
64         j = ((title != NULL) ? strwidth(title) : 0);
65         width = MAX(i,j)+4;
66   }
67   width = MAX(width,23);
68
69   if (width > COLS)
70         width = COLS;
71   if (height > LINES)
72         height = LINES;
73   /* center dialog box on screen */
74   x = DialogX ? DialogX : (COLS - width)/2;
75   y = DialogY ? DialogY : (LINES - height)/2;
76
77 #ifdef HAVE_NCURSES
78   if (use_shadow)
79     draw_shadow(stdscr, y, x, height, width);
80 #endif
81   dialog = newwin(height, width, y, x);
82   if (dialog == NULL) {
83     endwin();
84     fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
85     exit(1);
86   }
87   keypad(dialog, TRUE);
88
89   draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
90   wattrset(dialog, border_attr);
91   wmove(dialog, height-3, 0);
92   waddch(dialog, ACS_LTEE);
93   for (i = 0; i < width-2; i++)
94     waddch(dialog, ACS_HLINE);
95   wattrset(dialog, dialog_attr);
96   waddch(dialog, ACS_RTEE);
97   wmove(dialog, height-2, 1);
98   for (i = 0; i < width-2; i++)
99     waddch(dialog, ' ');
100
101   if (title != NULL) {
102     wattrset(dialog, title_attr);
103     wmove(dialog, 0, (width - strlen(title))/2 - 1);
104     waddch(dialog, ' ');
105     waddstr(dialog, title);
106     waddch(dialog, ' ');
107   }
108   wattrset(dialog, dialog_attr);
109   wmove(dialog, 1, 2);
110   print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
111
112   display_helpline(dialog, height-1, width);
113
114   x = width/2-10;
115   y = height-2;
116
117   /* preset button 0 or 1 for YES or NO as the default */
118   key = 0;
119   button = !yesdefault;
120   while (key != ESC) {
121     print_button(dialog, "  No  ", y, x+13, button);
122     print_button(dialog, " Yes " , y, x, !button);
123     if (button)
124         wmove(dialog, y, x+16);
125     else
126         wmove(dialog, y, x+2);
127     wrefresh(dialog);
128
129     key = wgetch(dialog);
130     switch (key) {
131       case 'Y':
132       case 'y':
133         delwin(dialog);
134         restore_helpline(tmphlp);
135         return 0;
136       case 'N':
137       case 'n':
138         delwin(dialog);
139         restore_helpline(tmphlp);
140         return 1;
141       case KEY_BTAB:
142       case TAB:
143       case KEY_UP:
144       case KEY_DOWN:
145       case KEY_LEFT:
146       case KEY_RIGHT:
147         button = !button;
148         /* redrawn at the loop's entry */
149         break;
150       case ' ':
151       case '\r':
152       case '\n':
153         delwin(dialog);
154         restore_helpline(tmphlp);
155         return button;
156       case ESC:
157         break;
158     case KEY_F(1):
159     case '?':
160         display_helpfile();
161         break;
162     }
163   }
164
165   delwin(dialog);
166   restore_helpline(tmphlp);
167   return -1;    /* ESC pressed */
168 }
169 /* End of dialog_yesno() */