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);
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_CLOSE_MASK = (1 << PSPPIRE_BUTTON_CLOSE),
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)
132 static GParamSpec *button_flags;
133 static GParamSpec *default_flags;
136 psppire_button_box_class_init (PsppireButtonBoxClass *class)
138 GObjectClass *object_class = G_OBJECT_CLASS (class);
140 object_class->set_property = psppire_buttonbox_set_property;
141 object_class->get_property = psppire_buttonbox_get_property;
144 g_param_spec_flags ("buttons",
146 "The mask that decides what buttons appear in the button box",
147 PSPPIRE_TYPE_BUTTON_MASK,
148 PSPPIRE_BUTTON_OK_MASK |
149 PSPPIRE_BUTTON_CANCEL_MASK |
150 PSPPIRE_BUTTON_RESET_MASK |
151 PSPPIRE_BUTTON_HELP_MASK |
152 PSPPIRE_BUTTON_PASTE_MASK,
154 g_object_class_install_property (object_class,
159 g_param_spec_flags ("default",
161 "The mask that decides what what button grabs the default",
162 PSPPIRE_TYPE_BUTTON_MASK,
165 g_object_class_install_property (object_class,
171 close_and_respond (GtkWidget *w, gint response)
173 PsppireDialog *dialog;
175 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
177 /* If we're not in a psppire dialog (for example when in glade)
179 if (! PSPPIRE_IS_DIALOG (toplevel))
182 dialog = PSPPIRE_DIALOG (toplevel);
184 dialog->response = response;
186 psppire_dialog_close (dialog);
190 is_acceptable (GtkWidget *w)
192 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
194 return (PSPPIRE_IS_DIALOG (toplevel)
195 && psppire_dialog_is_acceptable (PSPPIRE_DIALOG (toplevel)));
199 close_dialog (GtkWidget *w, gpointer data)
201 close_and_respond (w, GTK_RESPONSE_CLOSE);
205 continue_button_clicked (GtkWidget *w, gpointer data)
207 if (is_acceptable (w))
208 close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
213 ok_button_clicked (GtkWidget *w, gpointer data)
215 if (is_acceptable (w))
216 close_and_respond (w, GTK_RESPONSE_OK);
221 paste_button_clicked (GtkWidget *w, gpointer data)
223 if (is_acceptable (w))
224 close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
228 goto_button_clicked (GtkWidget *w, gpointer data)
230 if (is_acceptable (w))
231 close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
236 refresh_clicked (GtkWidget *w, gpointer data)
238 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
239 PsppireDialog *dialog;
241 if (! PSPPIRE_IS_DIALOG (toplevel))
244 dialog = PSPPIRE_DIALOG (toplevel);
246 psppire_dialog_reload (dialog);
250 help_clicked (GtkWidget *w, gpointer data)
252 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
253 PsppireDialog *dialog;
255 if (! PSPPIRE_IS_DIALOG (toplevel))
258 dialog = PSPPIRE_DIALOG (toplevel);
260 psppire_dialog_help (dialog);
264 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
266 PsppireButtonBox *bb = data;
268 /* Set the sensitivity of all the 'executive order' buttons */
269 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
270 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
271 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
272 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
276 on_key_press (GtkWidget *w, GdkEventKey *e, gpointer ud)
278 PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (ud);
279 if (e->keyval == GDK_KEY_Escape)
281 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CANCEL], "activate");
282 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CLOSE], "activate");
289 on_realize (GtkWidget *buttonbox, gpointer data)
291 GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
293 if (PSPPIRE_IS_DIALOG (toplevel))
295 g_signal_connect (toplevel, "validity-changed",
296 G_CALLBACK (on_validity_change), buttonbox);
298 g_signal_connect (toplevel, "key-press-event",
299 G_CALLBACK (on_key_press), buttonbox);
302 set_default (PSPPIRE_BUTTON_BOX (buttonbox));
307 psppire_button_box_init (PsppireButtonBox *bb)
309 bb->def = PSPPIRE_BUTTON_CONTINUE;
311 bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_with_label (_("OK"));
312 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
313 g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
314 G_CALLBACK (ok_button_clicked), NULL);
315 g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
318 bb->button[PSPPIRE_BUTTON_GOTO] =
319 gtk_button_new_with_label (_("Go To"));
320 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
321 g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
322 G_CALLBACK (goto_button_clicked), NULL);
323 g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
326 bb->button[PSPPIRE_BUTTON_CONTINUE] =
327 gtk_button_new_with_mnemonic (_("Continue"));
329 psppire_box_pack_start_defaults (GTK_BOX (bb),
330 bb->button[PSPPIRE_BUTTON_CONTINUE]);
331 g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
332 G_CALLBACK (continue_button_clicked), NULL);
334 g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
335 "no-show-all", TRUE, NULL);
339 bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_with_label (_("Paste"));
340 g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
341 G_CALLBACK (paste_button_clicked), NULL);
342 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
343 g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
345 bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_with_label (_("Cancel"));
346 g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
347 G_CALLBACK (close_dialog), NULL);
348 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
349 g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
351 bb->button[PSPPIRE_BUTTON_CLOSE] = gtk_button_new_with_label (_("Close"));
352 g_signal_connect (bb->button[PSPPIRE_BUTTON_CLOSE], "clicked",
353 G_CALLBACK (close_dialog), NULL);
354 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CLOSE]);
355 g_object_set (bb->button[PSPPIRE_BUTTON_CLOSE], "no-show-all", TRUE, NULL);
358 bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_with_label (_("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_with_label (_("Help"));
366 g_signal_connect (bb->button[PSPPIRE_BUTTON_HELP], "clicked",
367 G_CALLBACK (help_clicked), NULL);
368 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
369 g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
372 /* Set the default visibilities */
374 GValue value = { 0 };
377 g_value_init (&value, button_flags->value_type);
378 g_param_value_set_default(button_flags, &value);
381 flags = g_value_get_flags (&value);
383 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
384 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
386 g_value_unset (&value);
390 g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
394 psppire_button_flags_get_type (void)
396 static GType ftype = 0;
399 static const GFlagsValue values[] =
401 { PSPPIRE_BUTTON_OK_MASK, "PSPPIRE_BUTTON_OK_MASK", "Accept dialog and run it" },
402 { PSPPIRE_BUTTON_GOTO_MASK, "PSPPIRE_BUTTON_GOTO_MASK", "Goto case/variable" },
403 { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", "Accept and close the subdialog" },
404 { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", "Close dialog and discard settings" },
405 { PSPPIRE_BUTTON_CLOSE_MASK, "PSPPIRE_BUTTON_CLOSE_MASK", "Close dialog" },
406 { PSPPIRE_BUTTON_HELP_MASK, "PSPPIRE_BUTTON_HELP_MASK", "Invoke context sensitive help" },
407 { PSPPIRE_BUTTON_RESET_MASK, "PSPPIRE_BUTTON_RESET_MASK", "Restore dialog to its default settings" },
408 { PSPPIRE_BUTTON_PASTE_MASK, "PSPPIRE_BUTTON_PASTE_MASK", "Accept dialog and paste syntax" },
412 ftype = g_flags_register_static
413 (g_intern_static_string ("PsppireButtonFlags"), values);