yesno.c revision 6458
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
22#include <dialog.h>
23#include "dialog.priv.h"
24
25
26/*
27 * Display a dialog box with two buttons - Yes and No
28 */
29int dialog_yesno(unsigned char *title, unsigned char * prompt, int height, int width)
30{
31  int i, j, x, y, key = 0, button = 0;
32  WINDOW *dialog;
33
34  if (height < 0)
35	height = strheight(prompt)+4;
36  if (width < 0) {
37	i = strwidth(prompt);
38	j = ((title != NULL) ? strwidth(title) : 0);
39	width = MAX(i,j)+4;
40  }
41  width = MAX(width,23);
42
43  if (width > COLS)
44	width = COLS;
45  if (height > LINES)
46	height = LINES;
47  /* center dialog box on screen */
48  x = (COLS - width)/2;
49  y = (LINES - height)/2;
50
51#ifdef HAVE_NCURSES
52  if (use_shadow)
53    draw_shadow(stdscr, y, x, height, width);
54#endif
55  dialog = newwin(height, width, y, x);
56  if (dialog == NULL) {
57    endwin();
58    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
59    exit(1);
60  }
61  keypad(dialog, TRUE);
62
63  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
64  wattrset(dialog, border_attr);
65  wmove(dialog, height-3, 0);
66  waddch(dialog, ACS_LTEE);
67  for (i = 0; i < width-2; i++)
68    waddch(dialog, ACS_HLINE);
69  wattrset(dialog, dialog_attr);
70  waddch(dialog, ACS_RTEE);
71  wmove(dialog, height-2, 1);
72  for (i = 0; i < width-2; i++)
73    waddch(dialog, ' ');
74
75  if (title != NULL) {
76    wattrset(dialog, title_attr);
77    wmove(dialog, 0, (width - strlen(title))/2 - 1);
78    waddch(dialog, ' ');
79    waddstr(dialog, title);
80    waddch(dialog, ' ');
81  }
82  wattrset(dialog, dialog_attr);
83  wmove(dialog, 1, 2);
84  print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
85
86  display_helpline(dialog, height-1, width);
87
88  x = width/2-10;
89  y = height-2;
90  print_button(dialog, "  No  ", y, x+13, FALSE);
91  print_button(dialog, " Yes ", y, x, TRUE);
92  wrefresh(dialog);
93
94  while (key != ESC) {
95    key = wgetch(dialog);
96    switch (key) {
97      case 'Y':
98      case 'y':
99        delwin(dialog);
100        return 0;
101      case 'N':
102      case 'n':
103        delwin(dialog);
104        return 1;
105      case KEY_BTAB:
106      case TAB:
107      case KEY_UP:
108      case KEY_DOWN:
109      case KEY_LEFT:
110      case KEY_RIGHT:
111        if (!button) {
112          button = 1;    /* Indicates "No" button is selected */
113          print_button(dialog, " Yes ", y, x, FALSE);
114          print_button(dialog, "  No  ", y, x+13, TRUE);
115        }
116        else {
117          button = 0;    /* Indicates "Yes" button is selected */
118          print_button(dialog, "  No  ", y, x+13, FALSE);
119          print_button(dialog, " Yes ", y, x, TRUE);
120        }
121        wrefresh(dialog);
122        break;
123      case ' ':
124      case '\r':
125      case '\n':
126        delwin(dialog);
127        return button;
128      case ESC:
129        break;
130    case KEY_F(1):
131    case '?':
132	display_helpfile();
133	break;
134    }
135  }
136
137  delwin(dialog);
138  return -1;    /* ESC pressed */
139}
140/* End of dialog_yesno() */
141