gui: Include only <gtk/gtk.h> to use GTK+.
[pspp-builds.git] / src / ui / gui / psppire-buttonbox.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010  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
35 static void psppire_button_box_class_init          (PsppireButtonBoxClass *);
36 static void psppire_button_box_init                (PsppireButtonBox      *);
37
38
39 GType
40 psppire_button_box_get_type (void)
41 {
42   static GType button_box_type = 0;
43
44   if (!button_box_type)
45     {
46       static const GTypeInfo button_box_info =
47       {
48         sizeof (PsppireButtonBoxClass),
49         NULL, /* base_init */
50         NULL, /* base_finalize */
51         (GClassInitFunc) psppire_button_box_class_init,
52         NULL, /* class_finalize */
53         NULL, /* class_data */
54         sizeof (PsppireButtonBox),
55         0,
56         (GInstanceInitFunc) psppire_button_box_init,
57       };
58
59       button_box_type = g_type_register_static (GTK_TYPE_BUTTON_BOX,
60                                             "PsppireButtonBox", &button_box_info, G_TYPE_FLAG_ABSTRACT);
61     }
62
63   return button_box_type;
64 }
65
66 enum {
67   PROP_BUTTONS = 1
68 };
69
70 static void
71 psppire_buttonbox_set_property (GObject         *object,
72                                guint            prop_id,
73                                const GValue    *value,
74                                GParamSpec      *pspec)
75 {
76   gint i;
77   guint flags;
78   PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
79   if ( prop_id != PROP_BUTTONS)
80     {
81       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
82       return ;
83     }
84
85   flags = g_value_get_flags (value);
86
87   for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
88     g_object_set (bb->button[i], "visible", 0x01 & (flags >> i)  , NULL);
89 }
90
91 static void
92 psppire_buttonbox_get_property (GObject         *object,
93                                guint            prop_id,
94                                GValue          *value,
95                                GParamSpec      *pspec)
96 {
97   guint flags = 0;
98   gint i;
99
100   PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
101
102   if  (PROP_BUTTONS != prop_id)
103     {
104       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
105       return;
106     }
107
108   for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
109     {
110       gboolean visibility;
111       g_object_get (bb->button[i], "visible", &visibility, NULL);
112
113       if ( visibility )
114         flags |= (0x01 << i);
115     }
116
117   g_value_set_flags (value, flags);
118 }
119
120
121 typedef enum
122   {
123     PSPPIRE_BUTTON_OK_MASK     = (1 << PSPPIRE_BUTTON_OK),
124     PSPPIRE_BUTTON_GOTO_MASK   = (1 << PSPPIRE_BUTTON_GOTO),
125     PSPPIRE_BUTTON_CONTINUE_MASK = (1 << PSPPIRE_BUTTON_CONTINUE),
126     PSPPIRE_BUTTON_CANCEL_MASK = (1 << PSPPIRE_BUTTON_CANCEL),
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
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
154
155   g_object_class_install_property (object_class,
156                                    PROP_BUTTONS,
157                                    button_flags);
158
159 }
160
161 static void
162 close_and_respond (GtkWidget *w, gint response)
163 {
164   PsppireDialog *dialog;
165
166   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
167
168   /* If we're not in a psppire dialog (for example when in glade)
169      then do nothing */
170   if ( ! PSPPIRE_IS_DIALOG (toplevel))
171     return;
172
173   dialog = PSPPIRE_DIALOG (toplevel);
174
175   dialog->response = response;
176
177   psppire_dialog_close (dialog);
178 }
179
180
181 static void
182 close_dialog (GtkWidget *w, gpointer data)
183 {
184   close_and_respond (w, GTK_RESPONSE_CLOSE);
185 }
186
187 static void
188 continue_button_clicked (GtkWidget *w, gpointer data)
189 {
190   close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
191 }
192
193
194 static void
195 ok_button_clicked (GtkWidget *w, gpointer data)
196 {
197   close_and_respond (w, GTK_RESPONSE_OK);
198 }
199
200
201 static void
202 paste_button_clicked (GtkWidget *w, gpointer data)
203 {
204   close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
205 }
206
207 static void
208 goto_button_clicked (GtkWidget *w, gpointer data)
209 {
210   close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
211 }
212
213
214 static void
215 refresh_clicked (GtkWidget *w, gpointer data)
216 {
217   GtkWidget *toplevel = gtk_widget_get_toplevel (w);
218   PsppireDialog *dialog;
219
220   if ( ! PSPPIRE_IS_DIALOG (toplevel))
221     return;
222
223   dialog = PSPPIRE_DIALOG (toplevel);
224
225   psppire_dialog_reload (dialog);
226 }
227
228
229
230 static void
231 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
232 {
233   PsppireButtonBox *bb = data;
234
235   /* Set the sensitivity of all the 'executive order' buttons */
236   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
237   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
238   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
239   gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
240 }
241
242 static void
243 on_realize (GtkWidget *buttonbox, gpointer data)
244 {
245   GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
246
247   if ( PSPPIRE_IS_DIALOG (toplevel))
248     {
249       g_signal_connect (toplevel, "validity-changed",
250                         G_CALLBACK (on_validity_change), buttonbox);
251     }
252 }
253
254 static void
255 psppire_button_box_init (PsppireButtonBox *bb)
256 {
257
258   bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_from_stock (GTK_STOCK_OK);
259   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
260   g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
261                     G_CALLBACK (ok_button_clicked), NULL);
262   g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
263
264
265   bb->button[PSPPIRE_BUTTON_GOTO] =
266     gtk_button_new_from_stock (GTK_STOCK_JUMP_TO);
267   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
268   g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
269                     G_CALLBACK (goto_button_clicked), NULL);
270   g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
271
272
273   bb->button[PSPPIRE_BUTTON_CONTINUE] =
274     gtk_button_new_with_mnemonic (_("Continue"));
275
276   GTK_WIDGET_SET_FLAGS (bb->button[PSPPIRE_BUTTON_CONTINUE],
277                         GTK_CAN_DEFAULT);
278
279   g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "realize",
280          G_CALLBACK (gtk_widget_grab_default), NULL);
281
282   psppire_box_pack_start_defaults (GTK_BOX (bb),
283                                bb->button[PSPPIRE_BUTTON_CONTINUE]);
284   g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
285                     G_CALLBACK (continue_button_clicked), NULL);
286
287   g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
288                 "no-show-all", TRUE, NULL);
289
290
291
292   bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_from_stock (GTK_STOCK_PASTE);
293   g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
294                     G_CALLBACK (paste_button_clicked), NULL);
295   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
296   g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
297
298   bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
299   g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
300                     G_CALLBACK (close_dialog), NULL);
301   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
302   g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
303
304
305   bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_from_stock ("pspp-stock-reset");
306   g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
307                     G_CALLBACK (refresh_clicked), NULL);
308   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
309   g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
310
311
312   bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_from_stock (GTK_STOCK_HELP);
313   psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
314   g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
315
316
317   /* Set the default visibilities */
318   {
319     GValue value = { 0 };
320     guint flags;
321     gint i;
322     g_value_init (&value, button_flags->value_type);
323     g_param_value_set_default(button_flags, &value);
324
325
326     flags = g_value_get_flags (&value);
327
328     for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
329       g_object_set (bb->button[i], "visible", 0x01 & (flags >> i)  , NULL);
330
331     g_value_unset (&value);
332   }
333
334
335   g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
336 }
337
338
339 /* This function is lifted verbatim from the Gtk2.10.6 library */
340
341 void
342 _psppire_button_box_child_requisition (GtkWidget *widget,
343                                        int       *nvis_children,
344                                        int       *nvis_secondaries,
345                                        int       *width,
346                                        int       *height)
347 {
348   GtkButtonBox *bbox;
349   GtkBoxChild *child;
350   GList *children;
351   gint nchildren;
352   gint nsecondaries;
353   gint needed_width;
354   gint needed_height;
355   GtkRequisition child_requisition;
356   gint ipad_w;
357   gint ipad_h;
358   gint width_default;
359   gint height_default;
360   gint ipad_x_default;
361   gint ipad_y_default;
362
363   gint child_min_width;
364   gint child_min_height;
365   gint ipad_x;
366   gint ipad_y;
367
368   g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
369
370   bbox = GTK_BUTTON_BOX (widget);
371
372   gtk_widget_style_get (widget,
373                         "child-min-width", &width_default,
374                         "child-min-height", &height_default,
375                         "child-internal-pad-x", &ipad_x_default,
376                         "child-internal-pad-y", &ipad_y_default,
377                         NULL);
378
379   child_min_width = bbox->child_min_width   != GTK_BUTTONBOX_DEFAULT
380     ? bbox->child_min_width : width_default;
381   child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
382     ? bbox->child_min_height : height_default;
383   ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
384     ? bbox->child_ipad_x : ipad_x_default;
385   ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
386     ? bbox->child_ipad_y : ipad_y_default;
387
388   nchildren = 0;
389   nsecondaries = 0;
390   children = GTK_BOX(bbox)->children;
391   needed_width = child_min_width;
392   needed_height = child_min_height;
393   ipad_w = ipad_x * 2;
394   ipad_h = ipad_y * 2;
395
396   while (children)
397     {
398       child = children->data;
399       children = children->next;
400
401       if (GTK_WIDGET_VISIBLE (child->widget))
402         {
403           nchildren += 1;
404           gtk_widget_size_request (child->widget, &child_requisition);
405
406           if (child_requisition.width + ipad_w > needed_width)
407             needed_width = child_requisition.width + ipad_w;
408           if (child_requisition.height + ipad_h > needed_height)
409             needed_height = child_requisition.height + ipad_h;
410           if (child->is_secondary)
411             nsecondaries++;
412         }
413     }
414
415   if (nvis_children)
416     *nvis_children = nchildren;
417   if (nvis_secondaries)
418     *nvis_secondaries = nsecondaries;
419   if (width)
420     *width = needed_width;
421   if (height)
422     *height = needed_height;
423 }
424
425
426 GType
427 psppire_button_flags_get_type (void)
428 {
429   static GType ftype = 0;
430   if (ftype == 0)
431     {
432       static const GFlagsValue values[] =
433         {
434           { PSPPIRE_BUTTON_OK_MASK,     "PSPPIRE_BUTTON_OK_MASK",     N_("OK") },
435           { PSPPIRE_BUTTON_GOTO_MASK,   "PSPPIRE_BUTTON_GOTO_MASK", N_("Go To") },
436           { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", N_("Continue") },
437           { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", N_("Cancel") },
438           { PSPPIRE_BUTTON_HELP_MASK,   "PSPPIRE_BUTTON_HELP_MASK",   N_("Help") },
439           { PSPPIRE_BUTTON_RESET_MASK,  "PSPPIRE_BUTTON_RESET_MASK",  N_("Reset") },
440           { PSPPIRE_BUTTON_PASTE_MASK,  "PSPPIRE_BUTTON_PASTE_MASK",  N_("Paste") },
441           { 0, NULL, NULL }
442         };
443
444       ftype = g_flags_register_static
445         (g_intern_static_string ("PsppireButtonFlags"), values);
446
447     }
448   return ftype;
449 }
450