1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 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 <gtk/gtksignal.h>
23 #include "psppire-buttonbox.h"
24 #include "psppire-dialog.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
31 GType psppire_button_flags_get_type (void);
34 static void psppire_button_box_class_init (PsppireButtonBoxClass *);
35 static void psppire_button_box_init (PsppireButtonBox *);
39 psppire_button_box_get_type (void)
41 static GType button_box_type = 0;
45 static const GTypeInfo button_box_info =
47 sizeof (PsppireButtonBoxClass),
49 NULL, /* base_finalize */
50 (GClassInitFunc) psppire_button_box_class_init,
51 NULL, /* class_finalize */
52 NULL, /* class_data */
53 sizeof (PsppireButtonBox),
55 (GInstanceInitFunc) psppire_button_box_init,
58 button_box_type = g_type_register_static (GTK_TYPE_BUTTON_BOX,
59 "PsppireButtonBox", &button_box_info, 0);
62 return button_box_type;
70 psppire_buttonbox_set_property (GObject *object,
77 PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
78 if ( prop_id != PROP_BUTTONS)
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84 flags = g_value_get_flags (value);
86 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
87 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
91 psppire_buttonbox_get_property (GObject *object,
99 PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
101 if (PROP_BUTTONS != prop_id)
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
110 g_object_get (bb->button[i], "visible", &visibility, NULL);
113 flags |= (0x01 << i);
116 g_value_set_flags (value, flags);
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)
131 static GParamSpec *button_flags;
134 psppire_button_box_class_init (PsppireButtonBoxClass *class)
136 GObjectClass *object_class = G_OBJECT_CLASS (class);
138 object_class->set_property = psppire_buttonbox_set_property;
139 object_class->get_property = psppire_buttonbox_get_property;
142 g_param_spec_flags ("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,
154 g_object_class_install_property (object_class,
161 close_and_respond (GtkWidget *w, gint response)
163 PsppireDialog *dialog;
165 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
167 /* If we're not in a psppire dialog (for example when in glade)
169 if ( ! PSPPIRE_IS_DIALOG (toplevel))
172 dialog = PSPPIRE_DIALOG (toplevel);
174 dialog->response = response;
176 psppire_dialog_close (dialog);
181 close_dialog (GtkWidget *w, gpointer data)
183 close_and_respond (w, GTK_RESPONSE_CLOSE);
187 continue_button_clicked (GtkWidget *w, gpointer data)
189 close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
194 ok_button_clicked (GtkWidget *w, gpointer data)
196 close_and_respond (w, GTK_RESPONSE_OK);
201 paste_button_clicked (GtkWidget *w, gpointer data)
203 close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
207 goto_button_clicked (GtkWidget *w, gpointer data)
209 close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
214 refresh_clicked (GtkWidget *w, gpointer data)
216 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
217 PsppireDialog *dialog;
219 if ( ! PSPPIRE_IS_DIALOG (toplevel))
222 dialog = PSPPIRE_DIALOG (toplevel);
224 psppire_dialog_reload (dialog);
230 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
232 PsppireButtonBox *bb = data;
234 /* Set the sensitivity of all the 'executive order' buttons */
235 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
236 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
237 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
238 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
242 on_realize (GtkWidget *buttonbox, gpointer data)
244 GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
246 if ( PSPPIRE_IS_DIALOG (toplevel))
248 g_signal_connect (toplevel, "validity-changed",
249 G_CALLBACK (on_validity_change), buttonbox);
254 psppire_button_box_init (PsppireButtonBox *bb)
257 bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_from_stock (GTK_STOCK_OK);
258 gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
259 g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
260 G_CALLBACK (ok_button_clicked), NULL);
261 g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
264 bb->button[PSPPIRE_BUTTON_GOTO] =
265 gtk_button_new_from_stock (GTK_STOCK_JUMP_TO);
266 gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
267 g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
268 G_CALLBACK (goto_button_clicked), NULL);
269 g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
272 bb->button[PSPPIRE_BUTTON_CONTINUE] =
273 gtk_button_new_with_mnemonic (_("Continue"));
275 gtk_box_pack_start_defaults (GTK_BOX (bb),
276 bb->button[PSPPIRE_BUTTON_CONTINUE]);
277 g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
278 G_CALLBACK (continue_button_clicked), NULL);
280 g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
281 "no-show-all", TRUE, NULL);
285 bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_from_stock (GTK_STOCK_PASTE);
286 g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
287 G_CALLBACK (paste_button_clicked), NULL);
288 gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
289 g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
291 bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
292 g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
293 G_CALLBACK (close_dialog), NULL);
294 gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
295 g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
298 bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_from_stock ("pspp-stock-reset");
299 g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
300 G_CALLBACK (refresh_clicked), NULL);
301 gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
302 g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
305 bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_from_stock (GTK_STOCK_HELP);
306 gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
307 g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
310 /* Set the default visibilities */
312 GValue value = { 0 };
315 g_value_init (&value, button_flags->value_type);
316 g_param_value_set_default(button_flags, &value);
319 flags = g_value_get_flags (&value);
321 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
322 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
324 g_value_unset (&value);
328 g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
332 /* This function is lifted verbatim from the Gtk2.10.6 library */
335 _psppire_button_box_child_requisition (GtkWidget *widget,
337 int *nvis_secondaries,
348 GtkRequisition child_requisition;
356 gint child_min_width;
357 gint child_min_height;
361 g_return_if_fail (GTK_IS_BUTTON_BOX (widget));
363 bbox = GTK_BUTTON_BOX (widget);
365 gtk_widget_style_get (widget,
366 "child-min-width", &width_default,
367 "child-min-height", &height_default,
368 "child-internal-pad-x", &ipad_x_default,
369 "child-internal-pad-y", &ipad_y_default,
372 child_min_width = bbox->child_min_width != GTK_BUTTONBOX_DEFAULT
373 ? bbox->child_min_width : width_default;
374 child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
375 ? bbox->child_min_height : height_default;
376 ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
377 ? bbox->child_ipad_x : ipad_x_default;
378 ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
379 ? bbox->child_ipad_y : ipad_y_default;
383 children = GTK_BOX(bbox)->children;
384 needed_width = child_min_width;
385 needed_height = child_min_height;
391 child = children->data;
392 children = children->next;
394 if (GTK_WIDGET_VISIBLE (child->widget))
397 gtk_widget_size_request (child->widget, &child_requisition);
399 if (child_requisition.width + ipad_w > needed_width)
400 needed_width = child_requisition.width + ipad_w;
401 if (child_requisition.height + ipad_h > needed_height)
402 needed_height = child_requisition.height + ipad_h;
403 if (child->is_secondary)
409 *nvis_children = nchildren;
410 if (nvis_secondaries)
411 *nvis_secondaries = nsecondaries;
413 *width = needed_width;
415 *height = needed_height;
420 psppire_button_flags_get_type (void)
422 static GType ftype = 0;
425 static const GFlagsValue values[] =
427 { PSPPIRE_BUTTON_OK_MASK, "PSPPIRE_BUTTON_OK_MASK", N_("OK") },
428 { PSPPIRE_BUTTON_GOTO_MASK, "PSPPIRE_BUTTON_GOTO_MASK", N_("Go To") },
429 { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", N_("Continue") },
430 { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", N_("Cancel") },
431 { PSPPIRE_BUTTON_HELP_MASK, "PSPPIRE_BUTTON_HELP_MASK", N_("Help") },
432 { PSPPIRE_BUTTON_RESET_MASK, "PSPPIRE_BUTTON_RESET_MASK", N_("Reset") },
433 { PSPPIRE_BUTTON_PASTE_MASK, "PSPPIRE_BUTTON_PASTE_MASK", N_("Paste") },
437 ftype = g_flags_register_static
438 (g_intern_static_string ("PsppireButtonFlags"), values);