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);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 PSPPIRE_BUTTON_OK_MASK = (1 << PSPPIRE_BUTTON_OK),
122 PSPPIRE_BUTTON_GOTO_MASK = (1 << PSPPIRE_BUTTON_GOTO),
123 PSPPIRE_BUTTON_CONTINUE_MASK = (1 << PSPPIRE_BUTTON_CONTINUE),
124 PSPPIRE_BUTTON_CANCEL_MASK = (1 << PSPPIRE_BUTTON_CANCEL),
125 PSPPIRE_BUTTON_CLOSE_MASK = (1 << PSPPIRE_BUTTON_CLOSE),
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;
132 static GParamSpec *default_flags;
135 psppire_button_box_class_init (PsppireButtonBoxClass *class)
137 GObjectClass *object_class = G_OBJECT_CLASS (class);
139 object_class->set_property = psppire_buttonbox_set_property;
140 object_class->get_property = psppire_buttonbox_get_property;
143 g_param_spec_flags ("buttons",
145 "The mask that decides what buttons appear in the button box",
146 PSPPIRE_TYPE_BUTTON_MASK,
147 PSPPIRE_BUTTON_OK_MASK |
148 PSPPIRE_BUTTON_CANCEL_MASK |
149 PSPPIRE_BUTTON_RESET_MASK |
150 PSPPIRE_BUTTON_HELP_MASK |
151 PSPPIRE_BUTTON_PASTE_MASK,
153 g_object_class_install_property (object_class,
158 g_param_spec_flags ("default",
160 "The mask that decides what what button grabs the default",
161 PSPPIRE_TYPE_BUTTON_MASK,
164 g_object_class_install_property (object_class,
170 close_and_respond (GtkWidget *w, gint response)
172 PsppireDialog *dialog;
174 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
176 /* If we're not in a psppire dialog (for example when in glade)
178 if (! PSPPIRE_IS_DIALOG (toplevel))
181 dialog = PSPPIRE_DIALOG (toplevel);
183 dialog->response = response;
185 psppire_dialog_close (dialog);
189 is_acceptable (GtkWidget *w)
191 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
193 return (PSPPIRE_IS_DIALOG (toplevel)
194 && psppire_dialog_is_acceptable (PSPPIRE_DIALOG (toplevel)));
198 close_dialog (GtkWidget *w, gpointer data)
200 close_and_respond (w, GTK_RESPONSE_CLOSE);
204 continue_button_clicked (GtkWidget *w, gpointer data)
206 if (is_acceptable (w))
207 close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
212 ok_button_clicked (GtkWidget *w, gpointer data)
214 if (is_acceptable (w))
215 close_and_respond (w, GTK_RESPONSE_OK);
220 paste_button_clicked (GtkWidget *w, gpointer data)
222 if (is_acceptable (w))
223 close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
227 goto_button_clicked (GtkWidget *w, gpointer data)
229 if (is_acceptable (w))
230 close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
235 refresh_clicked (GtkWidget *w, gpointer data)
237 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
238 PsppireDialog *dialog;
240 if (! PSPPIRE_IS_DIALOG (toplevel))
243 dialog = PSPPIRE_DIALOG (toplevel);
245 psppire_dialog_reload (dialog);
249 help_clicked (GtkWidget *w, gpointer data)
251 GtkWidget *toplevel = gtk_widget_get_toplevel (w);
252 PsppireDialog *dialog;
254 if (! PSPPIRE_IS_DIALOG (toplevel))
257 dialog = PSPPIRE_DIALOG (toplevel);
259 psppire_dialog_help (dialog);
263 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
265 PsppireButtonBox *bb = data;
267 /* Set the sensitivity of all the 'executive order' buttons */
268 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_OK]), valid);
269 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_PASTE]), valid);
270 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_GOTO]), valid);
271 gtk_widget_set_sensitive (GTK_WIDGET (bb->button[PSPPIRE_BUTTON_CONTINUE]), valid);
275 on_key_press (GtkWidget *w, GdkEventKey *e, gpointer ud)
277 PsppireButtonBox *bb = PSPPIRE_BUTTON_BOX (ud);
278 if (e->keyval == GDK_KEY_Escape)
280 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CANCEL], "activate");
281 g_signal_emit_by_name (bb->button[PSPPIRE_BUTTON_CLOSE], "activate");
288 on_realize (GtkWidget *buttonbox, gpointer data)
290 GtkWidget *toplevel = gtk_widget_get_toplevel (buttonbox);
292 if (PSPPIRE_IS_DIALOG (toplevel))
294 g_signal_connect (toplevel, "validity-changed",
295 G_CALLBACK (on_validity_change), buttonbox);
297 g_signal_connect (toplevel, "key-press-event",
298 G_CALLBACK (on_key_press), buttonbox);
301 set_default (PSPPIRE_BUTTON_BOX (buttonbox));
306 psppire_button_box_init (PsppireButtonBox *bb)
308 bb->def = PSPPIRE_BUTTON_CONTINUE;
310 bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_with_label (_("OK"));
311 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
312 g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
313 G_CALLBACK (ok_button_clicked), NULL);
314 g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
317 bb->button[PSPPIRE_BUTTON_GOTO] =
318 gtk_button_new_with_label (_("Go To"));
319 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
320 g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
321 G_CALLBACK (goto_button_clicked), NULL);
322 g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
325 bb->button[PSPPIRE_BUTTON_CONTINUE] =
326 gtk_button_new_with_mnemonic (_("Continue"));
328 psppire_box_pack_start_defaults (GTK_BOX (bb),
329 bb->button[PSPPIRE_BUTTON_CONTINUE]);
330 g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
331 G_CALLBACK (continue_button_clicked), NULL);
333 g_object_set (bb->button[PSPPIRE_BUTTON_CONTINUE],
334 "no-show-all", TRUE, NULL);
338 bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_with_label (_("Paste"));
339 g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
340 G_CALLBACK (paste_button_clicked), NULL);
341 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
342 g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
344 bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_with_label (_("Cancel"));
345 g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
346 G_CALLBACK (close_dialog), NULL);
347 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
348 g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
350 bb->button[PSPPIRE_BUTTON_CLOSE] = gtk_button_new_with_label (_("Close"));
351 g_signal_connect (bb->button[PSPPIRE_BUTTON_CLOSE], "clicked",
352 G_CALLBACK (close_dialog), NULL);
353 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CLOSE]);
354 g_object_set (bb->button[PSPPIRE_BUTTON_CLOSE], "no-show-all", TRUE, NULL);
357 bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_with_label (_("Reset"));
358 g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
359 G_CALLBACK (refresh_clicked), NULL);
360 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
361 g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
364 bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_with_label (_("Help"));
365 g_signal_connect (bb->button[PSPPIRE_BUTTON_HELP], "clicked",
366 G_CALLBACK (help_clicked), NULL);
367 psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
368 g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
371 /* Set the default visibilities */
373 GValue value = { 0 };
376 g_value_init (&value, button_flags->value_type);
377 g_param_value_set_default(button_flags, &value);
380 flags = g_value_get_flags (&value);
382 for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i)
383 g_object_set (bb->button[i], "visible", 0x01 & (flags >> i) , NULL);
385 g_value_unset (&value);
389 g_signal_connect (bb, "realize", G_CALLBACK (on_realize), NULL);
393 psppire_button_flags_get_type (void)
395 static GType ftype = 0;
398 static const GFlagsValue values[] =
400 { PSPPIRE_BUTTON_OK_MASK, "PSPPIRE_BUTTON_OK_MASK", "Accept dialog and run it" },
401 { PSPPIRE_BUTTON_GOTO_MASK, "PSPPIRE_BUTTON_GOTO_MASK", "Goto case/variable" },
402 { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", "Accept and close the subdialog" },
403 { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", "Close dialog and discard settings" },
404 { PSPPIRE_BUTTON_CLOSE_MASK, "PSPPIRE_BUTTON_CLOSE_MASK", "Close dialog" },
405 { PSPPIRE_BUTTON_HELP_MASK, "PSPPIRE_BUTTON_HELP_MASK", "Invoke context sensitive help" },
406 { PSPPIRE_BUTTON_RESET_MASK, "PSPPIRE_BUTTON_RESET_MASK", "Restore dialog to its default settings" },
407 { PSPPIRE_BUTTON_PASTE_MASK, "PSPPIRE_BUTTON_PASTE_MASK", "Accept dialog and paste syntax" },
411 ftype = g_flags_register_static
412 (g_intern_static_string ("PsppireButtonFlags"), values);