1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011, 2012 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);
34 G_DEFINE_TYPE (PsppireButtonBox, psppire_button_box, GTK_TYPE_BUTTON_BOX)
42 set_default (PsppireButtonBox *bb)
46 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
47 if (bb->def == (1 << i))
49 gtk_widget_set_can_default (bb->button[i], TRUE);
50 gtk_widget_grab_default (bb->button[i]);
55 psppire_buttonbox_set_property (GObject *object,
62 PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (object);
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);
73 bb->def = g_value_get_flags (value);
74 if (gtk_widget_get_realized (GTK_WIDGET (bb)))
79 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84 psppire_buttonbox_get_property (GObject *object,
92 PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (object);
97 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
100 g_object_get (bb->button[i], "visible", &visibility, NULL);
103 flags |= (0x01 << i);
106 g_value_set_flags (value, flags);
110 g_value_set_flags (value, bb->def);
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 static GParamSpec *button_flags;
121 static GParamSpec *default_flags;
124 psppire_button_box_class_init (PsppireButtonBoxClass *class)
126 GObjectClass *object_class = G_OBJECT_CLASS (class);
128 object_class->set_property = psppire_buttonbox_set_property;
129 object_class->get_property = psppire_buttonbox_get_property;
132 g_param_spec_flags ("buttons",
134 "The mask that decides what buttons appear in the button box",
135 PSPPIRE_TYPE_BUTTON_MASK,
136 PSPPIRE_BUTTON_OK_MASK |
137 PSPPIRE_BUTTON_CANCEL_MASK |
138 PSPPIRE_BUTTON_RESET_MASK |
139 PSPPIRE_BUTTON_HELP_MASK |
140 PSPPIRE_BUTTON_PASTE_MASK,
142 g_object_class_install_property (object_class,
147 g_param_spec_flags ("default",
149 "The mask that decides what button grabs the default",
150 PSPPIRE_TYPE_BUTTON_MASK,
153 g_object_class_install_property (object_class,
159 close_and_respond (GtkWidget *w, gint response)
161 PsppireDialog *dialog;
163 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
165 /* If we're not in a psppire dialog (for example when in glade)
167 if (! PSPPIRE_IS_DIALOG (toplevel))
170 dialog = PSPPIRE_DIALOG (toplevel);
172 dialog->response = response;
174 psppire_dialog_close (dialog);
178 is_acceptable (GtkWidget *w)
180 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
182 return (PSPPIRE_IS_DIALOG (toplevel)
183 && psppire_dialog_is_acceptable (PSPPIRE_DIALOG (toplevel)));
187 close_dialog (GtkWidget *w, gpointer data)
189 close_and_respond (w, GTK_RESPONSE_CLOSE);
193 continue_button_clicked (GtkWidget *w, gpointer data)
195 if (is_acceptable (w))
196 close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
201 ok_button_clicked (GtkWidget *w, gpointer data)
203 if (is_acceptable (w))
204 close_and_respond (w, GTK_RESPONSE_OK);
209 paste_button_clicked (GtkWidget *w, gpointer data)
211 if (is_acceptable (w))
212 close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
216 goto_button_clicked (GtkWidget *w, gpointer data)
218 if (is_acceptable (w))
219 close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
224 refresh_clicked (GtkWidget *w, gpointer data)
226 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
227 PsppireDialog *dialog;
229 if (! PSPPIRE_IS_DIALOG (toplevel))
232 dialog = PSPPIRE_DIALOG (toplevel);
234 psppire_dialog_reload (dialog);
238 help_clicked (GtkWidget *w, gpointer data)
240 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
241 PsppireDialog *dialog;
243 if (! PSPPIRE_IS_DIALOG (toplevel))
246 dialog = PSPPIRE_DIALOG (toplevel);
248 psppire_dialog_help (dialog);
252 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
254 PsppireButtonBox *bb = data;
256 /* Set the sensitivity of all the 'executive order' buttons */
257 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
258 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
259 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
260 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
264 on_key_press (GtkWidget *w, GdkEventKey *e, gpointer ud)
266 PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (ud);
267 if (e->keyval == GDK_KEY_Escape)
269 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CANCEL], "activate");
270 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CLOSE], "activate");
277 on_realize (GtkWidget *buttonbox, gpointer data)
279 GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
281 if (PSPPIRE_IS_DIALOG (toplevel))
283 g_signal_connect (toplevel, "validity-changed",
284 G_CALLBACK (on_validity_change), buttonbox);
286 g_signal_connect (toplevel, "key-press-event",
287 G_CALLBACK (on_key_press), buttonbox);
290 set_default (PSPPIRE_BUTTON_BOX (buttonbox));
295 psppire_button_box_init (PsppireButtonBox *bb)
297 bb->def = PSPPIRE_BUTTON_CONTINUE;
299 bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_with_label (_("OK"));
300 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
301 g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
302 G_CALLBACK (ok_button_clicked), NULL);
303 g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
306 bb->button[PSPPIRE_BUTTON_GOTO] =
307 gtk_button_new_with_label (_("Go To"));
308 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
309 g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
310 G_CALLBACK (goto_button_clicked), NULL);
311 g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
314 bb->button[PSPPIRE_BUTTON_CONTINUE] =
315 gtk_button_new_with_mnemonic (_("Continue"));
317 psppire_box_pack_start_defaults (GTK_BOX (bb),
318 bb->button[PSPPIRE_BUTTON_CONTINUE]);
319 g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
320 G_CALLBACK (continue_button_clicked), NULL);
322 g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
323 "no-show-all", TRUE, NULL);
327 bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_with_label (_("Paste"));
328 g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
329 G_CALLBACK (paste_button_clicked), NULL);
330 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
331 g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
333 bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_with_label (_("Cancel"));
334 g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
335 G_CALLBACK (close_dialog), NULL);
336 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
337 g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
339 bb->button[PSPPIRE_BUTTON_CLOSE] = gtk_button_new_with_label (_("Close"));
340 g_signal_connect (bb->button[PSPPIRE_BUTTON_CLOSE], "clicked",
341 G_CALLBACK (close_dialog), NULL);
342 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CLOSE]);
343 g_object_set (bb->button[PSPPIRE_BUTTON_CLOSE], "no-show-all", TRUE, NULL);
346 bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_with_label (_("Reset"));
347 g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
348 G_CALLBACK (refresh_clicked), NULL);
349 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
350 g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
353 bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_with_label (_("Help"));
354 g_signal_connect (bb->button[PSPPIRE_BUTTON_HELP], "clicked",
355 G_CALLBACK (help_clicked), NULL);
356 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
357 g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
360 /* Set the default visibilities */
362 GValue value = { 0 };
365 g_value_init (&value, button_flags->value_type);
366 g_param_value_set_default(button_flags, &value);
369 flags = g_value_get_flags (&value);
371 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
372 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
374 g_value_unset (&value);
378 g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
382 psppire_button_flags_get_type (void)
384 static GType ftype = 0;
387 static const GFlagsValue values[] =
389 { PSPPIRE_BUTTON_OK_MASK, "PSPPIRE_BUTTON_OK_MASK", "Accept dialog and run it" },
390 { PSPPIRE_BUTTON_GOTO_MASK, "PSPPIRE_BUTTON_GOTO_MASK", "Goto case/variable" },
391 { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", "Accept and close the subdialog" },
392 { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", "Close dialog and discard settings" },
393 { PSPPIRE_BUTTON_CLOSE_MASK, "PSPPIRE_BUTTON_CLOSE_MASK", "Close dialog" },
394 { PSPPIRE_BUTTON_HELP_MASK, "PSPPIRE_BUTTON_HELP_MASK", "Invoke context sensitive help" },
395 { PSPPIRE_BUTTON_RESET_MASK, "PSPPIRE_BUTTON_RESET_MASK", "Restore dialog to its default settings" },
396 { PSPPIRE_BUTTON_PASTE_MASK, "PSPPIRE_BUTTON_PASTE_MASK", "Accept dialog and paste syntax" },
400 ftype = g_flags_register_static
401 (g_intern_static_string ("PsppireButtonFlags"), values);
408 psppire_button_box_new (void)
410 return GTK_WIDGET (g_object_new (psppire_button_box_get_type (), NULL));