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 | #include "dialog.h" |
---|
22 | |
---|
23 | /* Prototypes */ |
---|
24 | |
---|
25 | int dialog_yesno_with_default(const char *title, const char *prompt, |
---|
26 | int height, int width, int yesno_default); |
---|
27 | |
---|
28 | static void callback_yn(GtkWidget *w, gint button, gpointer *unused) |
---|
29 | { |
---|
30 | /* |
---|
31 | * We have to do this, because GTK_RESPONSE_[YES/NO] enums are -8 and |
---|
32 | * -9, respectively. |
---|
33 | */ |
---|
34 | if (button == GTK_RESPONSE_YES) |
---|
35 | exit (0); |
---|
36 | else if (button == GTK_RESPONSE_NO) |
---|
37 | exit (1); |
---|
38 | } |
---|
39 | |
---|
40 | static void callback_err(GtkWidget *w, gpointer *unused) |
---|
41 | { |
---|
42 | exit(-1); |
---|
43 | } |
---|
44 | |
---|
45 | void dialog_yesno(const char *title, const char *prompt, int height, int width) |
---|
46 | { |
---|
47 | dialog_yesno_with_default(title, prompt, height, width, 1); |
---|
48 | } |
---|
49 | |
---|
50 | /* |
---|
51 | * Display a dialog box with two buttons - Yes and No |
---|
52 | */ |
---|
53 | int dialog_yesno_with_default(const char *title, const char *prompt, int height, int width, int yesno_default) |
---|
54 | { |
---|
55 | int i, x, y, key = GTK_RESPONSE_YES, button = 0; |
---|
56 | WINDOW *dialog; |
---|
57 | |
---|
58 | if (gnome_mode) { |
---|
59 | |
---|
60 | GtkWidget *w = gtk_dialog_new_with_buttons (title, |
---|
61 | NULL, |
---|
62 | GTK_DIALOG_DESTROY_WITH_PARENT, |
---|
63 | GTK_STOCK_NO, |
---|
64 | GTK_RESPONSE_NO, |
---|
65 | GTK_STOCK_YES, |
---|
66 | GTK_RESPONSE_YES, |
---|
67 | NULL); |
---|
68 | GtkWidget *hbox; |
---|
69 | GtkWidget *vbox; |
---|
70 | |
---|
71 | if(yesno_default) |
---|
72 | gtk_dialog_set_default_response (GTK_DIALOG(w),GTK_RESPONSE_YES); |
---|
73 | else |
---|
74 | gtk_dialog_set_default_response (GTK_DIALOG(w),GTK_RESPONSE_NO); |
---|
75 | |
---|
76 | label_autowrap(GTK_DIALOG (w)->vbox, prompt, width); |
---|
77 | |
---|
78 | gtk_window_set_position(GTK_WINDOW(w), GTK_WIN_POS_CENTER); |
---|
79 | |
---|
80 | gtk_signal_connect(GTK_OBJECT(w), "response", GTK_SIGNAL_FUNC(callback_yn), NULL); |
---|
81 | gtk_signal_connect(GTK_OBJECT(w), "close", GTK_SIGNAL_FUNC(callback_err), NULL); |
---|
82 | gtk_widget_show_all(w); |
---|
83 | gtk_main(); |
---|
84 | return -1; |
---|
85 | } |
---|
86 | /* center dialog box on screen */ |
---|
87 | x = (COLS - width) / 2; |
---|
88 | y = (LINES - height) / 2; |
---|
89 | |
---|
90 | #ifndef NO_COLOR_CURSES |
---|
91 | if (use_shadow) |
---|
92 | draw_shadow(stdscr, y, x, height, width); |
---|
93 | #endif |
---|
94 | dialog = newwin(height, width, y, x); |
---|
95 | #ifdef WITH_GPM |
---|
96 | mouse_setbase(x, y); |
---|
97 | #endif |
---|
98 | keypad(dialog, TRUE); |
---|
99 | |
---|
100 | draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); |
---|
101 | wattrset(dialog, border_attr); |
---|
102 | waddch(dialog, ACS_LTEE); |
---|
103 | for (i = 0; i < width - 2; i++) |
---|
104 | waddch(dialog, ACS_HLINE); |
---|
105 | wattrset(dialog, dialog_attr); |
---|
106 | waddch(dialog, ACS_RTEE); |
---|
107 | wmove(dialog, height - 2, 1); |
---|
108 | for (i = 0; i < width - 2; i++) |
---|
109 | waddch(dialog, ' '); |
---|
110 | |
---|
111 | if (title != NULL) { |
---|
112 | wattrset(dialog, title_attr); |
---|
113 | wmove(dialog, 0, (width - strlen(title)) / 2 - 1); |
---|
114 | waddch(dialog, ' '); |
---|
115 | waddstr(dialog, title); |
---|
116 | waddch(dialog, ' '); |
---|
117 | } |
---|
118 | wattrset(dialog, dialog_attr); |
---|
119 | print_autowrap(dialog, prompt, width - 2, 1, 3); |
---|
120 | |
---|
121 | x = width / 2 - 10; |
---|
122 | y = height - 2; |
---|
123 | print_button(dialog, " No ", y, x + 13, !yesno_default); |
---|
124 | print_button(dialog, " Yes ", y, x, yesno_default); |
---|
125 | wmove(dialog, y, |
---|
126 | (yesno_default ? x : x + 13)); |
---|
127 | |
---|
128 | if(yesno_default) |
---|
129 | button = GTK_RESPONSE_YES; |
---|
130 | else |
---|
131 | button = GTK_RESPONSE_NO; |
---|
132 | |
---|
133 | wrefresh(dialog); |
---|
134 | |
---|
135 | while (key != ESC) { |
---|
136 | key = mouse_wgetch(dialog); |
---|
137 | switch (key) { |
---|
138 | case 'Y': |
---|
139 | case 'y': |
---|
140 | delwin(dialog); |
---|
141 | return 0; |
---|
142 | case 'N': |
---|
143 | case 'n': |
---|
144 | delwin(dialog); |
---|
145 | return 1; |
---|
146 | case M_EVENT + 'y': /* mouse enter... */ |
---|
147 | case M_EVENT + 'n': /* use the code for toggling */ |
---|
148 | if(key == M_EVENT + 'y') |
---|
149 | button = GTK_RESPONSE_YES; |
---|
150 | else |
---|
151 | button = GTK_RESPONSE_NO; |
---|
152 | |
---|
153 | case TAB: |
---|
154 | case KEY_UP: |
---|
155 | case KEY_DOWN: |
---|
156 | case KEY_LEFT: |
---|
157 | case KEY_RIGHT: |
---|
158 | if (button == GTK_RESPONSE_YES) { |
---|
159 | button = GTK_RESPONSE_NO; /* "No" button selected */ |
---|
160 | print_button(dialog, " Yes ", y, x, FALSE); |
---|
161 | print_button(dialog, " No ", y, x + 13, TRUE); |
---|
162 | } else { |
---|
163 | button = GTK_RESPONSE_YES; /* "Yes" button selected */ |
---|
164 | print_button(dialog, " No ", y, x + 13, FALSE); |
---|
165 | print_button(dialog, " Yes ", y, x, TRUE); |
---|
166 | } |
---|
167 | wrefresh(dialog); |
---|
168 | break; |
---|
169 | case ' ': |
---|
170 | case '\n': |
---|
171 | delwin(dialog); |
---|
172 | if (button == GTK_RESPONSE_YES) |
---|
173 | return 0; |
---|
174 | else if (button == GTK_RESPONSE_NO) |
---|
175 | return 1; |
---|
176 | |
---|
177 | case ESC: |
---|
178 | break; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | delwin(dialog); |
---|
184 | return -1; /* ESC pressed */ |
---|
185 | } |
---|