yesno.c revision 79843
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
21#ifndef lint
22static const char rcsid[] =
23  "$FreeBSD: head/gnu/lib/libdialog/yesno.c 79843 2001-07-18 05:21:37Z eric $";
24#endif
25
26#include <dialog.h>
27#include "dialog.priv.h"
28
29/* Actual work function */
30static int dialog_yesno_proc(unsigned char *title, unsigned char *prompt,
31			     int height, int width, int yesdefault);
32
33/*
34 * Display a dialog box with two buttons - Yes and No
35 */
36int
37dialog_yesno(unsigned char *title, unsigned char *prompt, int height, int width)
38{
39  return dialog_yesno_proc(title, prompt, height, width, TRUE);
40}
41
42/*
43 * Display a dialog box with two buttons - No and Yes
44 */
45int
46dialog_noyes(unsigned char *title, unsigned char *prompt, int height, int width)
47{
48  return dialog_yesno_proc(title, prompt, height, width, FALSE);
49}
50
51static int
52dialog_yesno_proc(unsigned char *title, unsigned char *prompt, int height, int width, int yesdefault)
53{
54  int i, j, x, y, key, button;
55  WINDOW *dialog;
56  char *tmphlp;
57
58  /* disable helpline */
59  tmphlp = get_helpline();
60  use_helpline(NULL);
61
62  if (height < 0)
63	height = strheight(prompt)+4;
64  if (width < 0) {
65	i = strwidth(prompt);
66	j = ((title != NULL) ? strwidth(title) : 0);
67	width = MAX(i,j)+4;
68  }
69  width = MAX(width,23);
70
71  if (width > COLS)
72	width = COLS;
73  if (height > LINES)
74	height = LINES;
75  /* center dialog box on screen */
76  x = DialogX ? DialogX : (COLS - width)/2;
77  y = DialogY ? DialogY : (LINES - height)/2;
78
79#ifdef HAVE_NCURSES
80  if (use_shadow)
81    draw_shadow(stdscr, y, x, height, width);
82#endif
83  dialog = newwin(height, width, y, x);
84  if (dialog == NULL) {
85    endwin();
86    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
87    exit(1);
88  }
89  keypad(dialog, TRUE);
90
91  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
92  wattrset(dialog, border_attr);
93  wmove(dialog, height-3, 0);
94  waddch(dialog, ACS_LTEE);
95  for (i = 0; i < width-2; i++)
96    waddch(dialog, ACS_HLINE);
97  wattrset(dialog, dialog_attr);
98  waddch(dialog, ACS_RTEE);
99  wmove(dialog, height-2, 1);
100  for (i = 0; i < width-2; i++)
101    waddch(dialog, ' ');
102
103  if (title != NULL) {
104    wattrset(dialog, title_attr);
105    wmove(dialog, 0, (width - strlen(title))/2 - 1);
106    waddch(dialog, ' ');
107    waddstr(dialog, title);
108    waddch(dialog, ' ');
109  }
110  wattrset(dialog, dialog_attr);
111  wmove(dialog, 1, 2);
112  print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
113
114  display_helpline(dialog, height-1, width);
115
116  x = width/2-10;
117  y = height-2;
118
119  /* preset button 0 or 1 for YES or NO as the default */
120  key = 0;
121  button = !yesdefault;
122  while (key != ESC) {
123    print_button(dialog, "  No  ", y, x+13, button);
124    print_button(dialog, " Yes " , y, x, !button);
125    if (button)
126	wmove(dialog, y, x+16);
127    else
128	wmove(dialog, y, x+2);
129    wrefresh(dialog);
130
131    key = wgetch(dialog);
132    switch (key) {
133      case 'Y':
134      case 'y':
135        delwin(dialog);
136	restore_helpline(tmphlp);
137        return 0;
138      case 'N':
139      case 'n':
140        delwin(dialog);
141	restore_helpline(tmphlp);
142        return 1;
143      case KEY_BTAB:
144      case TAB:
145      case KEY_UP:
146      case KEY_DOWN:
147      case KEY_LEFT:
148      case KEY_RIGHT:
149        button = !button;
150        /* redrawn at the loop's entry */
151        break;
152      case ' ':
153      case '\r':
154      case '\n':
155        delwin(dialog);
156	restore_helpline(tmphlp);
157        return button;
158      case ESC:
159        break;
160    case KEY_F(1):
161    case '?':
162	display_helpfile();
163	break;
164    }
165  }
166
167  delwin(dialog);
168  restore_helpline(tmphlp);
169  return -1;    /* ESC pressed */
170}
171/* End of dialog_yesno() */
172