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