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