1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011 Free Software Foundation
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.
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.
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/>. */
22 #include "psppire-buttonbox.h"
23 #include "psppire-dialog.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
32 GType psppire_button_flags_get_type (void);
35 static void psppire_button_box_class_init (PsppireButtonBoxClass *);
36 static void psppire_button_box_init (PsppireButtonBox *);
40 psppire_button_box_get_type (void)
42 static GType button_box_type = 0;
46 static const GTypeInfo button_box_info =
48 sizeof (PsppireButtonBoxClass),
50 NULL, /* base_finalize */
51 (GClassInitFunc) psppire_button_box_class_init,
52 NULL, /* class_finalize */
53 NULL, /* class_data */
54 sizeof (PsppireButtonBox),
56 (GInstanceInitFunc) psppire_button_box_init,
59 button_box_type = g_type_register_static (GTK_TYPE_BUTTON_BOX,
60 "PsppireButtonBox", &button_box_info, G_TYPE_FLAG_ABSTRACT);
63 return button_box_type;
72 set_default (PsppireButtonBox *bb)
76 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
77 if (bb->def == (1 << i))
79 gtk_widget_set_can_default (bb->button[i], TRUE);
80 gtk_widget_grab_default (bb->button[i]);
84 #if !GTK_CHECK_VERSION(2,20,0)
86 * gtk_widget_get_realized:
87 * @widget: a #GtkWidget
89 * Determines whether @widget is realized.
91 * Return value: %TRUE if @widget is realized, %FALSE otherwise
95 static inline gboolean
96 gtk_widget_get_realized (GtkWidget *widget)
98 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
100 return (GTK_WIDGET_FLAGS (widget) & GTK_REALIZED) != 0;
102 #endif /* gtk < 2.20 */
105 psppire_buttonbox_set_property (GObject *object,
112 PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
117 flags = g_value_get_flags (value);
118 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
119 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
123 bb->def = g_value_get_flags (value);
124 if (gtk_widget_get_realized (GTK_WIDGET (bb)))
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 psppire_buttonbox_get_property (GObject *object,
142 PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
147 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
150 g_object_get (bb->button[i], "visible", &visibility, NULL);
153 flags |= (0x01 << i);
156 g_value_set_flags (value, flags);
160 g_value_set_flags (value, bb->def);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 PSPPIRE_BUTTON_OK_MASK = (1 << PSPPIRE_BUTTON_OK),
172 PSPPIRE_BUTTON_GOTO_MASK = (1 << PSPPIRE_BUTTON_GOTO),
173 PSPPIRE_BUTTON_CONTINUE_MASK = (1 << PSPPIRE_BUTTON_CONTINUE),
174 PSPPIRE_BUTTON_CANCEL_MASK = (1 << PSPPIRE_BUTTON_CANCEL),
175 PSPPIRE_BUTTON_HELP_MASK = (1 << PSPPIRE_BUTTON_HELP),
176 PSPPIRE_BUTTON_RESET_MASK = (1 << PSPPIRE_BUTTON_RESET),
177 PSPPIRE_BUTTON_PASTE_MASK = (1 << PSPPIRE_BUTTON_PASTE)
180 static GParamSpec *button_flags;
181 static GParamSpec *default_flags;
184 psppire_button_box_class_init (PsppireButtonBoxClass *class)
186 GObjectClass *object_class = G_OBJECT_CLASS (class);
188 object_class->set_property = psppire_buttonbox_set_property;
189 object_class->get_property = psppire_buttonbox_get_property;
192 g_param_spec_flags ("buttons",
194 "The mask that decides what buttons appear in the button box",
195 PSPPIRE_TYPE_BUTTON_MASK,
196 PSPPIRE_BUTTON_OK_MASK |
197 PSPPIRE_BUTTON_CANCEL_MASK |
198 PSPPIRE_BUTTON_RESET_MASK |
199 PSPPIRE_BUTTON_HELP_MASK |
200 PSPPIRE_BUTTON_PASTE_MASK,
202 g_object_class_install_property (object_class,
207 g_param_spec_flags ("default",
209 "The mask that decides what what button grabs the default",
210 PSPPIRE_TYPE_BUTTON_MASK,
213 g_object_class_install_property (object_class,
219 close_and_respond (GtkWidget *w, gint response)
221 PsppireDialog *dialog;
223 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
225 /* If we're not in a psppire dialog (for example when in glade)
227 if ( ! PSPPIRE_IS_DIALOG (toplevel))
230 dialog = PSPPIRE_DIALOG (toplevel);
232 dialog->response = response;
234 psppire_dialog_close (dialog);
239 close_dialog (GtkWidget *w, gpointer data)
241 close_and_respond (w, GTK_RESPONSE_CLOSE);
245 continue_button_clicked (GtkWidget *w, gpointer data)
247 close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
252 ok_button_clicked (GtkWidget *w, gpointer data)
254 close_and_respond (w, GTK_RESPONSE_OK);
259 paste_button_clicked (GtkWidget *w, gpointer data)
261 close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
265 goto_button_clicked (GtkWidget *w, gpointer data)
267 close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
272 refresh_clicked (GtkWidget *w, gpointer data)
274 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
275 PsppireDialog *dialog;
277 if ( ! PSPPIRE_IS_DIALOG (toplevel))
280 dialog = PSPPIRE_DIALOG (toplevel);
282 psppire_dialog_reload (dialog);
288 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
290 PsppireButtonBox *bb = data;
292 /* Set the sensitivity of all the 'executive order' buttons */
293 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
294 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
295 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
296 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
300 on_realize (GtkWidget *buttonbox, gpointer data)
302 GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
304 if ( PSPPIRE_IS_DIALOG (toplevel))
306 g_signal_connect (toplevel, "validity-changed",
307 G_CALLBACK (on_validity_change), buttonbox);
309 set_default (PSPPIRE_BUTTONBOX (buttonbox));
313 psppire_button_box_init (PsppireButtonBox *bb)
315 bb->def = PSPPIRE_BUTTON_CONTINUE;
317 bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_from_stock (GTK_STOCK_OK);
318 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
319 g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
320 G_CALLBACK (ok_button_clicked), NULL);
321 g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
324 bb->button[PSPPIRE_BUTTON_GOTO] =
325 gtk_button_new_from_stock (GTK_STOCK_JUMP_TO);
326 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
327 g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
328 G_CALLBACK (goto_button_clicked), NULL);
329 g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
332 bb->button[PSPPIRE_BUTTON_CONTINUE] =
333 gtk_button_new_with_mnemonic (_("Continue"));
335 psppire_box_pack_start_defaults (GTK_BOX (bb),
336 bb->button[PSPPIRE_BUTTON_CONTINUE]);
337 g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
338 G_CALLBACK (continue_button_clicked), NULL);
340 g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
341 "no-show-all", TRUE, NULL);
345 bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_from_stock (GTK_STOCK_PASTE);
346 g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
347 G_CALLBACK (paste_button_clicked), NULL);
348 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
349 g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
351 bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
352 g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
353 G_CALLBACK (close_dialog), NULL);
354 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
355 g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
358 bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_from_stock ("pspp-stock-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);
365 bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_from_stock (GTK_STOCK_HELP);
366 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
367 g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
370 /* Set the default visibilities */
372 GValue value = { 0 };
375 g_value_init (&value, button_flags->value_type);
376 g_param_value_set_default(button_flags, &value);
379 flags = g_value_get_flags (&value);
381 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
382 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
384 g_value_unset (&value);
388 g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
392 /* This function is lifted verbatim from the Gtk2.10.6 library */
395 _psppire_button_box_child_requisition (GtkWidget *widget,
397 int *nvis_secondaries,
408 GtkRequisition child_requisition;
416 gint child_min_width;
417 gint child_min_height;
421 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
423 bbox = GTK_BUTTON_BOX (widget);
425 gtk_widget_style_get (widget,
426 "child-min-width", &width_default,
427 "child-min-height", &height_default,
428 "child-internal-pad-x", &ipad_x_default,
429 "child-internal-pad-y", &ipad_y_default,
432 child_min_width = bbox->child_min_width != GTK_BUTTONBOX_DEFAULT
433 ? bbox->child_min_width : width_default;
434 child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
435 ? bbox->child_min_height : height_default;
436 ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
437 ? bbox->child_ipad_x : ipad_x_default;
438 ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
439 ? bbox->child_ipad_y : ipad_y_default;
443 children = GTK_BOX(bbox)->children;
444 needed_width = child_min_width;
445 needed_height = child_min_height;
451 child = children->data;
452 children = children->next;
454 if (GTK_WIDGET_VISIBLE (child->widget))
457 gtk_widget_size_request (child->widget, &child_requisition);
459 if (child_requisition.width + ipad_w > needed_width)
460 needed_width = child_requisition.width + ipad_w;
461 if (child_requisition.height + ipad_h > needed_height)
462 needed_height = child_requisition.height + ipad_h;
463 if (child->is_secondary)
469 *nvis_children = nchildren;
470 if (nvis_secondaries)
471 *nvis_secondaries = nsecondaries;
473 *width = needed_width;
475 *height = needed_height;
480 psppire_button_flags_get_type (void)
482 static GType ftype = 0;
485 static const GFlagsValue values[] =
487 { PSPPIRE_BUTTON_OK_MASK, "PSPPIRE_BUTTON_OK_MASK", N_("OK") },
488 { PSPPIRE_BUTTON_GOTO_MASK, "PSPPIRE_BUTTON_GOTO_MASK", N_("Go To") },
489 { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", N_("Continue") },
490 { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", N_("Cancel") },
491 { PSPPIRE_BUTTON_HELP_MASK, "PSPPIRE_BUTTON_HELP_MASK", N_("Help") },
492 { PSPPIRE_BUTTON_RESET_MASK, "PSPPIRE_BUTTON_RESET_MASK", N_("Reset") },
493 { PSPPIRE_BUTTON_PASTE_MASK, "PSPPIRE_BUTTON_PASTE_MASK", N_("Paste") },
497 ftype = g_flags_register_static
498 (g_intern_static_string ("PsppireButtonFlags"), values);