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);
35 static void psppire_button_box_class_init (PsppireButtonboxClass *);
36 static void psppire_button_box_init (PsppireButtonbox *);
40 psppire_buttonbox_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, 0);
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]);
85 psppire_buttonbox_set_property (GObject *object,
92 PsppireButtonbox *bb = PSPPIRE_BUTTONBOX (object);
97 flags = g_value_get_flags (value);
98 for (i = 0 ; i < n_PsppireButtonboxButtons ; ++i )
99 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
103 bb->def = g_value_get_flags (value);
104 if (gtk_widget_get_realized (GTK_WIDGET (bb)))
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 psppire_buttonbox_get_property (GObject *object,
122 PsppireButtonbox *bb = PSPPIRE_BUTTONBOX (object);
127 for (i = 0 ; i < n_PsppireButtonboxButtons ; ++i )
130 g_object_get (bb->button[i], "visible", &visibility, NULL);
133 flags |= (0x01 << i);
136 g_value_set_flags (value, flags);
140 g_value_set_flags (value, bb->def);
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 PSPPIRE_BUTTON_OK_MASK = (1 << PSPPIRE_BUTTON_OK),
152 PSPPIRE_BUTTON_GOTO_MASK = (1 << PSPPIRE_BUTTON_GOTO),
153 PSPPIRE_BUTTON_CONTINUE_MASK = (1 << PSPPIRE_BUTTON_CONTINUE),
154 PSPPIRE_BUTTON_CANCEL_MASK = (1 << PSPPIRE_BUTTON_CANCEL),
155 PSPPIRE_BUTTON_CLOSE_MASK = (1 << PSPPIRE_BUTTON_CLOSE),
156 PSPPIRE_BUTTON_HELP_MASK = (1 << PSPPIRE_BUTTON_HELP),
157 PSPPIRE_BUTTON_RESET_MASK = (1 << PSPPIRE_BUTTON_RESET),
158 PSPPIRE_BUTTON_PASTE_MASK = (1 << PSPPIRE_BUTTON_PASTE)
161 static GParamSpec *button_flags;
162 static GParamSpec *default_flags;
165 psppire_button_box_class_init (PsppireButtonboxClass *class)
167 GObjectClass *object_class = G_OBJECT_CLASS (class);
169 object_class->set_property = psppire_buttonbox_set_property;
170 object_class->get_property = psppire_buttonbox_get_property;
173 g_param_spec_flags ("buttons",
175 "The mask that decides what buttons appear in the button box",
176 PSPPIRE_TYPE_BUTTON_MASK,
177 PSPPIRE_BUTTON_OK_MASK |
178 PSPPIRE_BUTTON_CANCEL_MASK |
179 PSPPIRE_BUTTON_RESET_MASK |
180 PSPPIRE_BUTTON_HELP_MASK |
181 PSPPIRE_BUTTON_PASTE_MASK,
183 g_object_class_install_property (object_class,
188 g_param_spec_flags ("default",
190 "The mask that decides what what button grabs the default",
191 PSPPIRE_TYPE_BUTTON_MASK,
194 g_object_class_install_property (object_class,
200 close_and_respond (GtkWidget *w, gint response)
202 PsppireDialog *dialog;
204 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
206 /* If we're not in a psppire dialog (for example when in glade)
208 if ( ! PSPPIRE_IS_DIALOG (toplevel))
211 dialog = PSPPIRE_DIALOG (toplevel);
213 dialog->response = response;
215 psppire_dialog_close (dialog);
219 is_acceptable (GtkWidget *w)
221 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
223 return (PSPPIRE_IS_DIALOG (toplevel)
224 && psppire_dialog_is_acceptable (PSPPIRE_DIALOG (toplevel)));
228 close_dialog (GtkWidget *w, gpointer data)
230 close_and_respond (w, GTK_RESPONSE_CLOSE);
234 continue_button_clicked (GtkWidget *w, gpointer data)
236 if (is_acceptable (w))
237 close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
242 ok_button_clicked (GtkWidget *w, gpointer data)
244 if (is_acceptable (w))
245 close_and_respond (w, GTK_RESPONSE_OK);
250 paste_button_clicked (GtkWidget *w, gpointer data)
252 if (is_acceptable (w))
253 close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
257 goto_button_clicked (GtkWidget *w, gpointer data)
259 if (is_acceptable (w))
260 close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
265 refresh_clicked (GtkWidget *w, gpointer data)
267 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
268 PsppireDialog *dialog;
270 if ( ! PSPPIRE_IS_DIALOG (toplevel))
273 dialog = PSPPIRE_DIALOG (toplevel);
275 psppire_dialog_reload (dialog);
279 help_clicked (GtkWidget *w, gpointer data)
281 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
282 PsppireDialog *dialog;
284 if ( ! PSPPIRE_IS_DIALOG (toplevel))
287 dialog = PSPPIRE_DIALOG (toplevel);
289 psppire_dialog_help (dialog);
293 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
295 PsppireButtonbox *bb = data;
297 /* Set the sensitivity of all the 'executive order' buttons */
298 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
299 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
300 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
301 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
305 on_key_press (GtkWidget *w, GdkEventKey *e, gpointer ud)
307 PsppireButtonbox *bb = PSPPIRE_BUTTONBOX (ud);
308 if (e->keyval == GDK_KEY_Escape)
310 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CANCEL], "activate");
311 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CLOSE], "activate");
318 on_realize (GtkWidget *buttonbox, gpointer data)
320 GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
322 if (PSPPIRE_IS_DIALOG (toplevel))
324 g_signal_connect (toplevel, "validity-changed",
325 G_CALLBACK (on_validity_change), buttonbox);
327 g_signal_connect (toplevel, "key-press-event",
328 G_CALLBACK (on_key_press), buttonbox);
331 set_default (PSPPIRE_BUTTONBOX (buttonbox));
336 psppire_button_box_init (PsppireButtonbox *bb)
338 bb->def = PSPPIRE_BUTTON_CONTINUE;
340 bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_with_label (_("OK"));
341 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
342 g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
343 G_CALLBACK (ok_button_clicked), NULL);
344 g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
347 bb->button[PSPPIRE_BUTTON_GOTO] =
348 gtk_button_new_with_label (_("Go To"));
349 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
350 g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
351 G_CALLBACK (goto_button_clicked), NULL);
352 g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
355 bb->button[PSPPIRE_BUTTON_CONTINUE] =
356 gtk_button_new_with_mnemonic (_("Continue"));
358 psppire_box_pack_start_defaults (GTK_BOX (bb),
359 bb->button[PSPPIRE_BUTTON_CONTINUE]);
360 g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
361 G_CALLBACK (continue_button_clicked), NULL);
363 g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
364 "no-show-all", TRUE, NULL);
368 bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_with_label (_("Paste"));
369 g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
370 G_CALLBACK (paste_button_clicked), NULL);
371 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
372 g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
374 bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_with_label (_("Cancel"));
375 g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
376 G_CALLBACK (close_dialog), NULL);
377 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
378 g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
380 bb->button[PSPPIRE_BUTTON_CLOSE] = gtk_button_new_with_label (_("Close"));
381 g_signal_connect (bb->button[PSPPIRE_BUTTON_CLOSE], "clicked",
382 G_CALLBACK (close_dialog), NULL);
383 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CLOSE]);
384 g_object_set (bb->button[PSPPIRE_BUTTON_CLOSE], "no-show-all", TRUE, NULL);
387 bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_with_label (_("Reset"));
388 g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
389 G_CALLBACK (refresh_clicked), NULL);
390 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
391 g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
394 bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_with_label (_("Help"));
395 g_signal_connect (bb->button[PSPPIRE_BUTTON_HELP], "clicked",
396 G_CALLBACK (help_clicked), NULL);
397 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
398 g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
401 /* Set the default visibilities */
403 GValue value = { 0 };
406 g_value_init (&value, button_flags->value_type);
407 g_param_value_set_default(button_flags, &value);
410 flags = g_value_get_flags (&value);
412 for (i = 0 ; i < n_PsppireButtonboxButtons ; ++i )
413 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
415 g_value_unset (&value);
419 g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
423 psppire_button_flags_get_type (void)
425 static GType ftype = 0;
428 static const GFlagsValue values[] =
430 { PSPPIRE_BUTTON_OK_MASK, "PSPPIRE_BUTTON_OK_MASK", "Accept dialog and run it" },
431 { PSPPIRE_BUTTON_GOTO_MASK, "PSPPIRE_BUTTON_GOTO_MASK", "Goto case/variable" },
432 { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", "Accept and close the subdialog" },
433 { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", "Close dialog and discard settings" },
434 { PSPPIRE_BUTTON_CLOSE_MASK, "PSPPIRE_BUTTON_CLOSE_MASK", "Close dialog" },
435 { PSPPIRE_BUTTON_HELP_MASK, "PSPPIRE_BUTTON_HELP_MASK", "Invoke context sensitive help" },
436 { PSPPIRE_BUTTON_RESET_MASK, "PSPPIRE_BUTTON_RESET_MASK", "Restore dialog to its default settings" },
437 { PSPPIRE_BUTTON_PASTE_MASK, "PSPPIRE_BUTTON_PASTE_MASK", "Accept dialog and paste syntax" },
441 ftype = g_flags_register_static
442 (g_intern_static_string ("PsppireButtonFlags"), values);