Warnings: removed fallthrough in case statements
[pspp] / src / ui / gui / psppire-buttonbox.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 2011, 2012  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19
20 #include <glib.h>
21 #include <gtk/gtk.h>
22 #include "psppire-buttonbox.h"
23 #include "psppire-dialog.h"
24
25 #include "helper.h"
26
27 #include <gettext.h>
28
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
31
32 GType psppire_button_flags_get_type (void);
33
34 G_DEFINE_TYPE (PsppireButtonBox, psppire_button_box, GTK_TYPE_BUTTON_BOX)
35
36 enum {
37   PROP_BUTTONS = 1,
38   PROP_DEFAULT = 2
39 };
40
41 static void
42 set_default (PsppireButtonBox *bb)
43 {
44   int i;
45
46   for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
47     if (bb->def == (1 << i))
48       {
49         gtk_widget_set_can_default (bb->button[i], TRUE);
50         gtk_widget_grab_default (bb->button[i]);
51       }
52 }
53
54 static void
55 psppire_buttonbox_set_property (GObject         *object,
56                                 guint            prop_id,
57                                 const GValue    *value,
58                                 GParamSpec      *pspec)
59 {
60   gint i;
61   guint flags;
62   PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (object);
63
64   switch (prop_id)
65     {
66     case PROP_BUTTONS:
67       flags = g_value_get_flags (value);
68       for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
69         g_object_set (bb->button[i], "visible", 0x01 & (flags >> i)  , NULL);
70       break;
71
72     case PROP_DEFAULT:
73       bb->def = g_value_get_flags (value);
74       if (gtk_widget_get_realized (GTK_WIDGET (bb)))
75         set_default (bb);
76       break;
77
78     default:
79       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
80     }
81 }
82
83 static void
84 psppire_buttonbox_get_property (GObject         *object,
85                                 guint            prop_id,
86                                 GValue          *value,
87                                 GParamSpec      *pspec)
88 {
89   guint flags = 0;
90   gint i;
91
92   PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (object);
93
94   switch (prop_id)
95     {
96     case PROP_BUTTONS:
97       for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
98         {
99           gboolean visibility;
100           g_object_get (bb->button[i], "visible", &visibility, NULL);
101
102           if (visibility)
103             flags |= (0x01 << i);
104         }
105
106       g_value_set_flags (value, flags);
107       break;
108
109     case PROP_DEFAULT:
110       g_value_set_flags (value, bb->def);
111       break;
112
113     default:
114       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
115       break;
116     }
117 }
118
119
120 typedef enum
121   {
122     PSPPIRE_BUTTON_OK_MASK     = (1 << PSPPIRE_BUTTON_OK),
123     PSPPIRE_BUTTON_GOTO_MASK   = (1 << PSPPIRE_BUTTON_GOTO),
124     PSPPIRE_BUTTON_CONTINUE_MASK = (1 << PSPPIRE_BUTTON_CONTINUE),
125     PSPPIRE_BUTTON_CANCEL_MASK = (1 << PSPPIRE_BUTTON_CANCEL),
126     PSPPIRE_BUTTON_CLOSE_MASK  = (1 << PSPPIRE_BUTTON_CLOSE),
127     PSPPIRE_BUTTON_HELP_MASK   = (1 << PSPPIRE_BUTTON_HELP),
128     PSPPIRE_BUTTON_RESET_MASK  = (1 << PSPPIRE_BUTTON_RESET),
129     PSPPIRE_BUTTON_PASTE_MASK  = (1 << PSPPIRE_BUTTON_PASTE)
130   } PsppireButtonMask;
131
132 static GParamSpec *button_flags;
133 static GParamSpec *default_flags;
134
135 static void
136 psppire_button_box_class_init (PsppireButtonBoxClass *class)
137 {
138   GObjectClass *object_class = G_OBJECT_CLASS (class);
139
140   object_class->set_property = psppire_buttonbox_set_property;
141   object_class->get_property = psppire_buttonbox_get_property;
142
143   button_flags =
144     g_param_spec_flags ("buttons",
145                         "Buttons",
146                         "The mask that decides what buttons appear in the button box",
147                         PSPPIRE_TYPE_BUTTON_MASK,
148                         PSPPIRE_BUTTON_OK_MASK |
149                         PSPPIRE_BUTTON_CANCEL_MASK |
150                         PSPPIRE_BUTTON_RESET_MASK |
151                         PSPPIRE_BUTTON_HELP_MASK |
152                         PSPPIRE_BUTTON_PASTE_MASK,
153                         G_PARAM_READWRITE);
154   g_object_class_install_property (object_class,
155                                    PROP_BUTTONS,
156                                    button_flags);
157
158   default_flags =
159     g_param_spec_flags ("default",
160                         "Default",
161                         "The mask that decides what what button grabs the default",
162                         PSPPIRE_TYPE_BUTTON_MASK,
163                         0,
164                         G_PARAM_READWRITE);
165   g_object_class_install_property (object_class,
166                                    PROP_DEFAULT,
167                                    default_flags);
168 }
169
170 static void
171 close_and_respond (GtkWidget *w, gint response)
172 {
173   PsppireDialog *dialog;
174
175   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
176
177   /* If we're not in a psppire dialog (for example when in glade)
178      then do nothing */
179   if (! PSPPIRE_IS_DIALOG (toplevel))
180     return;
181
182   dialog = PSPPIRE_DIALOG (toplevel);
183
184   dialog->response = response;
185
186   psppire_dialog_close (dialog);
187 }
188
189 static gboolean
190 is_acceptable (GtkWidget *w)
191 {
192   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
193
194   return (PSPPIRE_IS_DIALOG (toplevel)
195           && psppire_dialog_is_acceptable (PSPPIRE_DIALOG (toplevel)));
196 }
197
198 static void
199 close_dialog (GtkWidget *w, gpointer data)
200 {
201   close_and_respond (w, GTK_RESPONSE_CLOSE);
202 }
203
204 static void
205 continue_button_clicked (GtkWidget *w, gpointer data)
206 {
207   if (is_acceptable (w))
208     close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
209 }
210
211
212 static void
213 ok_button_clicked (GtkWidget *w, gpointer data)
214 {
215   if (is_acceptable (w))
216     close_and_respond (w, GTK_RESPONSE_OK);
217 }
218
219
220 static void
221 paste_button_clicked (GtkWidget *w, gpointer data)
222 {
223   if (is_acceptable (w))
224     close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
225 }
226
227 static void
228 goto_button_clicked (GtkWidget *w, gpointer data)
229 {
230   if (is_acceptable (w))
231     close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
232 }
233
234
235 static void
236 refresh_clicked (GtkWidget *w, gpointer data)
237 {
238   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
239   PsppireDialog *dialog;
240
241   if (! PSPPIRE_IS_DIALOG (toplevel))
242     return;
243
244   dialog = PSPPIRE_DIALOG (toplevel);
245
246   psppire_dialog_reload (dialog);
247 }
248
249 static void
250 help_clicked (GtkWidget *w, gpointer data)
251 {
252   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
253   PsppireDialog *dialog;
254
255   if (! PSPPIRE_IS_DIALOG (toplevel))
256     return;
257
258   dialog = PSPPIRE_DIALOG (toplevel);
259
260   psppire_dialog_help (dialog);
261 }
262
263 static void
264 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
265 {
266   PsppireButtonBox *bb = data;
267
268   /* Set the sensitivity of all the 'executive order' buttons */
269   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
270   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
271   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
272   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
273 }
274
275 static gboolean
276 on_key_press (GtkWidget *w, GdkEventKey *e, gpointer ud)
277 {
278   PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (ud);
279   if (e->keyval == GDK_KEY_Escape)
280     {
281       g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CANCEL], "activate");
282       g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CLOSE], "activate");
283     }
284   return FALSE;
285 }
286
287
288 static void
289 on_realize (GtkWidget *buttonbox, gpointer data)
290 {
291   GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
292
293   if (PSPPIRE_IS_DIALOG (toplevel))
294     {
295       g_signal_connect (toplevel, "validity-changed",
296                         G_CALLBACK (on_validity_change), buttonbox);
297
298       g_signal_connect (toplevel, "key-press-event",
299                         G_CALLBACK (on_key_press), buttonbox);
300     }
301
302   set_default (PSPPIRE_BUTTON_BOX (buttonbox));
303 }
304
305
306 static void
307 psppire_button_box_init (PsppireButtonBox *bb)
308 {
309   bb->def = PSPPIRE_BUTTON_CONTINUE;
310
311   bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_with_label (_("OK"));
312   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
313   g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
314                     G_CALLBACK (ok_button_clicked), NULL);
315   g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
316
317
318   bb->button[PSPPIRE_BUTTON_GOTO] =
319     gtk_button_new_with_label (_("Go To"));
320   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
321   g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
322                     G_CALLBACK (goto_button_clicked), NULL);
323   g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
324
325
326   bb->button[PSPPIRE_BUTTON_CONTINUE] =
327     gtk_button_new_with_mnemonic (_("Continue"));
328
329   psppire_box_pack_start_defaults (GTK_BOX (bb),
330                                bb->button[PSPPIRE_BUTTON_CONTINUE]);
331   g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
332                     G_CALLBACK (continue_button_clicked), NULL);
333
334   g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
335                 "no-show-all", TRUE, NULL);
336
337
338
339   bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_with_label (_("Paste"));
340   g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
341                     G_CALLBACK (paste_button_clicked), NULL);
342   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
343   g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
344
345   bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_with_label (_("Cancel"));
346   g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
347                     G_CALLBACK (close_dialog), NULL);
348   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
349   g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
350
351   bb->button[PSPPIRE_BUTTON_CLOSE] = gtk_button_new_with_label (_("Close"));
352   g_signal_connect (bb->button[PSPPIRE_BUTTON_CLOSE], "clicked",
353                     G_CALLBACK (close_dialog), NULL);
354   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CLOSE]);
355   g_object_set (bb->button[PSPPIRE_BUTTON_CLOSE], "no-show-all", TRUE, NULL);
356
357
358   bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_with_label (_("Reset"));
359   g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
360                     G_CALLBACK (refresh_clicked), NULL);
361   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
362   g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
363
364
365   bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_with_label (_("Help"));
366   g_signal_connect (bb->button[PSPPIRE_BUTTON_HELP], "clicked",
367                     G_CALLBACK (help_clicked), NULL);
368   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
369   g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
370
371
372   /* Set the default visibilities */
373   {
374     GValue value = { 0 };
375     guint flags;
376     gint i;
377     g_value_init (&value, button_flags->value_type);
378     g_param_value_set_default(button_flags, &value);
379
380
381     flags = g_value_get_flags (&value);
382
383     for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
384       g_object_set (bb->button[i], "visible", 0x01 & (flags >> i)  , NULL);
385
386     g_value_unset (&value);
387   }
388
389
390   g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
391 }
392
393 GType
394 psppire_button_flags_get_type (void)
395 {
396   static GType ftype = 0;
397   if (ftype == 0)
398     {
399       static const GFlagsValue values[] =
400         {
401           { PSPPIRE_BUTTON_OK_MASK,      "PSPPIRE_BUTTON_OK_MASK",       "Accept dialog and run it" },
402           { PSPPIRE_BUTTON_GOTO_MASK,    "PSPPIRE_BUTTON_GOTO_MASK",     "Goto case/variable" },
403           { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", "Accept and close the subdialog" },
404           { PSPPIRE_BUTTON_CANCEL_MASK,  "PSPPIRE_BUTTON_CANCEL_MASK",   "Close dialog and discard settings" },
405           { PSPPIRE_BUTTON_CLOSE_MASK,   "PSPPIRE_BUTTON_CLOSE_MASK",    "Close dialog" },
406           { PSPPIRE_BUTTON_HELP_MASK,    "PSPPIRE_BUTTON_HELP_MASK",     "Invoke context sensitive help" },
407           { PSPPIRE_BUTTON_RESET_MASK,   "PSPPIRE_BUTTON_RESET_MASK",    "Restore dialog to its default settings" },
408           { PSPPIRE_BUTTON_PASTE_MASK,   "PSPPIRE_BUTTON_PASTE_MASK",    "Accept dialog and paste syntax" },
409           { 0, NULL, NULL }
410         };
411
412       ftype = g_flags_register_static
413         (g_intern_static_string ("PsppireButtonFlags"), values);
414
415     }
416   return ftype;
417 }
418