abb90123ef95b4b51e5d47f4967dffa8aedb947f
[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
112     default:
113       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114       break;
115     }
116 }
117
118
119 typedef enum
120   {
121     PSPPIRE_BUTTON_OK_MASK     = (1 << PSPPIRE_BUTTON_OK),
122     PSPPIRE_BUTTON_GOTO_MASK   = (1 << PSPPIRE_BUTTON_GOTO),
123     PSPPIRE_BUTTON_CONTINUE_MASK = (1 << PSPPIRE_BUTTON_CONTINUE),
124     PSPPIRE_BUTTON_CANCEL_MASK = (1 << PSPPIRE_BUTTON_CANCEL),
125     PSPPIRE_BUTTON_CLOSE_MASK  = (1 << PSPPIRE_BUTTON_CLOSE),
126     PSPPIRE_BUTTON_HELP_MASK   = (1 << PSPPIRE_BUTTON_HELP),
127     PSPPIRE_BUTTON_RESET_MASK  = (1 << PSPPIRE_BUTTON_RESET),
128     PSPPIRE_BUTTON_PASTE_MASK  = (1 << PSPPIRE_BUTTON_PASTE)
129   } PsppireButtonMask;
130
131 static GParamSpec *button_flags;
132 static GParamSpec *default_flags;
133
134 static void
135 psppire_button_box_class_init (PsppireButtonBoxClass *class)
136 {
137   GObjectClass *object_class = G_OBJECT_CLASS (class);
138
139   object_class->set_property = psppire_buttonbox_set_property;
140   object_class->get_property = psppire_buttonbox_get_property;
141
142   button_flags =
143     g_param_spec_flags ("buttons",
144                         "Buttons",
145                         "The mask that decides what buttons appear in the button box",
146                         PSPPIRE_TYPE_BUTTON_MASK,
147                         PSPPIRE_BUTTON_OK_MASK |
148                         PSPPIRE_BUTTON_CANCEL_MASK |
149                         PSPPIRE_BUTTON_RESET_MASK |
150                         PSPPIRE_BUTTON_HELP_MASK |
151                         PSPPIRE_BUTTON_PASTE_MASK,
152                         G_PARAM_READWRITE);
153   g_object_class_install_property (object_class,
154                                    PROP_BUTTONS,
155                                    button_flags);
156
157   default_flags =
158     g_param_spec_flags ("default",
159                         "Default",
160                         "The mask that decides what what button grabs the default",
161                         PSPPIRE_TYPE_BUTTON_MASK,
162                         0,
163                         G_PARAM_READWRITE);
164   g_object_class_install_property (object_class,
165                                    PROP_DEFAULT,
166                                    default_flags);
167 }
168
169 static void
170 close_and_respond (GtkWidget *w, gint response)
171 {
172   PsppireDialog *dialog;
173
174   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
175
176   /* If we're not in a psppire dialog (for example when in glade)
177      then do nothing */
178   if (! PSPPIRE_IS_DIALOG (toplevel))
179     return;
180
181   dialog = PSPPIRE_DIALOG (toplevel);
182
183   dialog->response = response;
184
185   psppire_dialog_close (dialog);
186 }
187
188 static gboolean
189 is_acceptable (GtkWidget *w)
190 {
191   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
192
193   return (PSPPIRE_IS_DIALOG (toplevel)
194           && psppire_dialog_is_acceptable (PSPPIRE_DIALOG (toplevel)));
195 }
196
197 static void
198 close_dialog (GtkWidget *w, gpointer data)
199 {
200   close_and_respond (w, GTK_RESPONSE_CLOSE);
201 }
202
203 static void
204 continue_button_clicked (GtkWidget *w, gpointer data)
205 {
206   if (is_acceptable (w))
207     close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
208 }
209
210
211 static void
212 ok_button_clicked (GtkWidget *w, gpointer data)
213 {
214   if (is_acceptable (w))
215     close_and_respond (w, GTK_RESPONSE_OK);
216 }
217
218
219 static void
220 paste_button_clicked (GtkWidget *w, gpointer data)
221 {
222   if (is_acceptable (w))
223     close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
224 }
225
226 static void
227 goto_button_clicked (GtkWidget *w, gpointer data)
228 {
229   if (is_acceptable (w))
230     close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
231 }
232
233
234 static void
235 refresh_clicked (GtkWidget *w, gpointer data)
236 {
237   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
238   PsppireDialog *dialog;
239
240   if (! PSPPIRE_IS_DIALOG (toplevel))
241     return;
242
243   dialog = PSPPIRE_DIALOG (toplevel);
244
245   psppire_dialog_reload (dialog);
246 }
247
248 static void
249 help_clicked (GtkWidget *w, gpointer data)
250 {
251   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
252   PsppireDialog *dialog;
253
254   if (! PSPPIRE_IS_DIALOG (toplevel))
255     return;
256
257   dialog = PSPPIRE_DIALOG (toplevel);
258
259   psppire_dialog_help (dialog);
260 }
261
262 static void
263 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
264 {
265   PsppireButtonBox *bb = data;
266
267   /* Set the sensitivity of all the 'executive order' buttons */
268   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
269   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
270   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
271   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
272 }
273
274 static gboolean
275 on_key_press (GtkWidget *w, GdkEventKey *e, gpointer ud)
276 {
277   PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (ud);
278   if (e->keyval == GDK_KEY_Escape)
279     {
280       g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CANCEL], "activate");
281       g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CLOSE], "activate");
282     }
283   return FALSE;
284 }
285
286
287 static void
288 on_realize (GtkWidget *buttonbox, gpointer data)
289 {
290   GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
291
292   if (PSPPIRE_IS_DIALOG (toplevel))
293     {
294       g_signal_connect (toplevel, "validity-changed",
295                         G_CALLBACK (on_validity_change), buttonbox);
296
297       g_signal_connect (toplevel, "key-press-event",
298                         G_CALLBACK (on_key_press), buttonbox);
299     }
300
301   set_default (PSPPIRE_BUTTON_BOX (buttonbox));
302 }
303
304
305 static void
306 psppire_button_box_init (PsppireButtonBox *bb)
307 {
308   bb->def = PSPPIRE_BUTTON_CONTINUE;
309
310   bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_with_label (_("OK"));
311   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
312   g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
313                     G_CALLBACK (ok_button_clicked), NULL);
314   g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
315
316
317   bb->button[PSPPIRE_BUTTON_GOTO] =
318     gtk_button_new_with_label (_("Go To"));
319   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
320   g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
321                     G_CALLBACK (goto_button_clicked), NULL);
322   g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
323
324
325   bb->button[PSPPIRE_BUTTON_CONTINUE] =
326     gtk_button_new_with_mnemonic (_("Continue"));
327
328   psppire_box_pack_start_defaults (GTK_BOX (bb),
329                                bb->button[PSPPIRE_BUTTON_CONTINUE]);
330   g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
331                     G_CALLBACK (continue_button_clicked), NULL);
332
333   g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
334                 "no-show-all", TRUE, NULL);
335
336
337
338   bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_with_label (_("Paste"));
339   g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
340                     G_CALLBACK (paste_button_clicked), NULL);
341   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
342   g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
343
344   bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_with_label (_("Cancel"));
345   g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
346                     G_CALLBACK (close_dialog), NULL);
347   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
348   g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
349
350   bb->button[PSPPIRE_BUTTON_CLOSE] = gtk_button_new_with_label (_("Close"));
351   g_signal_connect (bb->button[PSPPIRE_BUTTON_CLOSE], "clicked",
352                     G_CALLBACK (close_dialog), NULL);
353   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CLOSE]);
354   g_object_set (bb->button[PSPPIRE_BUTTON_CLOSE], "no-show-all", TRUE, NULL);
355
356
357   bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_with_label (_("Reset"));
358   g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
359                     G_CALLBACK (refresh_clicked), NULL);
360   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
361   g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
362
363
364   bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_with_label (_("Help"));
365   g_signal_connect (bb->button[PSPPIRE_BUTTON_HELP], "clicked",
366                     G_CALLBACK (help_clicked), NULL);
367   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
368   g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
369
370
371   /* Set the default visibilities */
372   {
373     GValue value = { 0 };
374     guint flags;
375     gint i;
376     g_value_init (&value, button_flags->value_type);
377     g_param_value_set_default(button_flags, &value);
378
379
380     flags = g_value_get_flags (&value);
381
382     for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
383       g_object_set (bb->button[i], "visible", 0x01 & (flags >> i)  , NULL);
384
385     g_value_unset (&value);
386   }
387
388
389   g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
390 }
391
392 GType
393 psppire_button_flags_get_type (void)
394 {
395   static GType ftype = 0;
396   if (ftype == 0)
397     {
398       static const GFlagsValue values[] =
399         {
400           { PSPPIRE_BUTTON_OK_MASK,      "PSPPIRE_BUTTON_OK_MASK",       "Accept dialog and run it" },
401           { PSPPIRE_BUTTON_GOTO_MASK,    "PSPPIRE_BUTTON_GOTO_MASK",     "Goto case/variable" },
402           { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", "Accept and close the subdialog" },
403           { PSPPIRE_BUTTON_CANCEL_MASK,  "PSPPIRE_BUTTON_CANCEL_MASK",   "Close dialog and discard settings" },
404           { PSPPIRE_BUTTON_CLOSE_MASK,   "PSPPIRE_BUTTON_CLOSE_MASK",    "Close dialog" },
405           { PSPPIRE_BUTTON_HELP_MASK,    "PSPPIRE_BUTTON_HELP_MASK",     "Invoke context sensitive help" },
406           { PSPPIRE_BUTTON_RESET_MASK,   "PSPPIRE_BUTTON_RESET_MASK",    "Restore dialog to its default settings" },
407           { PSPPIRE_BUTTON_PASTE_MASK,   "PSPPIRE_BUTTON_PASTE_MASK",    "Accept dialog and paste syntax" },
408           { 0, NULL, NULL }
409         };
410
411       ftype = g_flags_register_static
412         (g_intern_static_string ("PsppireButtonFlags"), values);
413
414     }
415   return ftype;
416 }
417