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/>. */
21 #include "psppire-dialog.h"
22 #include "psppire-buttonbox.h"
23 #include "psppire-selector.h"
24 #include "psppire-conf.h"
26 #include "builder-wrapper.h"
27 #include "help-menu.h"
29 static void psppire_dialog_class_init (PsppireDialogClass *);
30 static void psppire_dialog_init (PsppireDialog *);
39 static guint signals [n_SIGNALS];
42 static void psppire_dialog_buildable_init (GtkBuildableIface *iface);
46 psppire_dialog_get_type (void)
48 static GType dialog_type = 0;
52 static const GTypeInfo dialog_info =
54 sizeof (PsppireDialogClass),
56 NULL, /* base_finalize */
57 (GClassInitFunc) psppire_dialog_class_init,
58 NULL, /* class_finalize */
59 NULL, /* class_data */
60 sizeof (PsppireDialog),
62 (GInstanceInitFunc) psppire_dialog_init,
65 static const GInterfaceInfo buildable_info =
67 (GInterfaceInitFunc) psppire_dialog_buildable_init,
72 dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
73 "PsppireDialog", &dialog_info, 0);
75 g_type_add_interface_static (dialog_type,
85 static GObjectClass *parent_class = NULL;
89 psppire_dialog_finalize (GObject *object)
91 PsppireDialog *dialog ;
93 g_return_if_fail (object != NULL);
94 g_return_if_fail (PSPPIRE_IS_DIALOG (object));
96 dialog = PSPPIRE_DIALOG (object);
98 if (G_OBJECT_CLASS (parent_class)->finalize)
99 G_OBJECT_CLASS (parent_class)->finalize (object);
114 psppire_dialog_get_property (GObject *object,
119 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
123 case PROP_ORIENTATION:
125 if ( GTK_IS_VBOX (dialog->box) || GTK_VPANED (dialog->box))
126 g_value_set_enum (value, PSPPIRE_VERTICAL);
127 else if ( GTK_IS_HBOX (dialog->box) || GTK_HPANED (dialog->box))
128 g_value_set_enum (value, PSPPIRE_HORIZONTAL);
129 else if ( GTK_IS_TABLE (dialog->box))
130 g_value_set_enum (value, PSPPIRE_TABULAR);
134 g_value_set_boolean (value, dialog->slidable);
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 dialog_set_container (PsppireDialog *dialog)
146 if ( dialog->box != NULL)
148 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
151 switch (dialog->orientation)
153 case PSPPIRE_HORIZONTAL:
154 if ( dialog->slidable)
155 dialog->box = gtk_hpaned_new();
157 dialog->box = gtk_hbox_new (FALSE, 5);
159 case PSPPIRE_VERTICAL:
160 if ( dialog->slidable)
161 dialog->box = gtk_vpaned_new();
163 dialog->box = gtk_vbox_new (FALSE, 5);
165 case PSPPIRE_TABULAR:
166 dialog->box = gtk_table_new (2, 3, FALSE);
167 g_object_set (dialog->box,
174 gtk_widget_show_all (dialog->box);
175 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
180 psppire_dialog_set_property (GObject *object,
186 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
191 dialog->slidable = g_value_get_boolean (value);
193 case PROP_ORIENTATION:
194 dialog->orientation = g_value_get_enum (value);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 dialog_set_container (dialog);
205 static GParamSpec *orientation_spec ;
208 psppire_dialog_class_init (PsppireDialogClass *class)
210 GObjectClass *object_class = (GObjectClass *) class;
212 GParamSpec *sliding_spec ;
215 g_param_spec_enum ("orientation",
217 "Which way widgets are packed",
218 PSPPIRE_TYPE_ORIENTATION,
219 PSPPIRE_HORIZONTAL /* default value */,
220 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
223 g_param_spec_boolean ("slidable",
225 "Can the container be sized by the user",
227 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
229 object_class->set_property = psppire_dialog_set_property;
230 object_class->get_property = psppire_dialog_get_property;
232 g_object_class_install_property (object_class,
237 g_object_class_install_property (object_class,
241 signals [DIALOG_REFRESH] =
242 g_signal_new ("refresh",
243 G_TYPE_FROM_CLASS (class),
247 g_cclosure_marshal_VOID__VOID,
253 g_signal_new ("response",
254 G_TYPE_FROM_CLASS (class),
258 g_cclosure_marshal_VOID__INT,
264 signals [VALIDITY_CHANGED] =
265 g_signal_new ("validity-changed",
266 G_TYPE_FROM_CLASS (class),
270 g_cclosure_marshal_VOID__BOOLEAN,
276 signals [DIALOG_HELP] =
277 g_signal_new ("help",
278 G_TYPE_FROM_CLASS (class),
282 g_cclosure_marshal_VOID__STRING,
288 object_class->finalize = psppire_dialog_finalize;
295 close_dialog (GtkWidget *w, gpointer data)
297 PsppireDialog *dialog = data;
299 psppire_dialog_close (dialog);
303 psppire_dialog_close (PsppireDialog *dialog)
305 g_main_loop_quit (dialog->loop);
306 gtk_widget_hide (GTK_WIDGET (dialog));
311 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
313 close_dialog (w, data);
318 configure_event_callback (GtkDialog *dialog,
319 GdkEvent *event, gpointer data)
323 PsppireConf *conf = psppire_conf_new ();
325 if ( ! gtk_widget_get_mapped (GTK_WIDGET (dialog)))
328 base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
330 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (dialog));
337 on_realize (GtkWindow *dialog, gpointer data)
339 PsppireConf *conf = psppire_conf_new ();
341 const gchar *base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
343 psppire_conf_set_window_geometry (conf, base, dialog);
349 psppire_dialog_init (PsppireDialog *dialog)
353 dialog->contents_are_valid = NULL;
354 dialog->validity_data = NULL;
355 dialog->slidable = FALSE;
357 g_value_init (&value, orientation_spec->value_type);
358 g_param_value_set_default (orientation_spec, &value);
360 gtk_window_set_type_hint (GTK_WINDOW (dialog),
361 GDK_WINDOW_TYPE_HINT_DIALOG);
363 g_value_unset (&value);
365 g_signal_connect (dialog, "delete-event",
366 G_CALLBACK (delete_event_callback),
369 g_signal_connect (dialog, "configure-event",
370 G_CALLBACK (configure_event_callback),
373 g_signal_connect (dialog, "realize",
374 G_CALLBACK (on_realize),
378 gtk_window_set_type_hint (GTK_WINDOW (dialog),
379 GDK_WINDOW_TYPE_HINT_DIALOG);
381 g_object_set (dialog, "icon-name", "pspp", NULL);
386 psppire_dialog_new (void)
388 PsppireDialog *dialog ;
390 dialog = g_object_new (psppire_dialog_get_type (),
393 return GTK_WIDGET (dialog) ;
398 psppire_dialog_notify_change (PsppireDialog *dialog)
400 if ( dialog->contents_are_valid )
402 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
404 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
409 /* Descend the widget tree, connecting appropriate signals to the
410 psppire_dialog_notify_change callback */
412 connect_notify_signal (GtkWidget *w, gpointer data)
414 PsppireDialog *dialog = data;
416 if ( PSPPIRE_IS_BUTTONBOX (w))
419 if ( GTK_IS_CONTAINER (w))
421 gtk_container_foreach (GTK_CONTAINER (w),
422 connect_notify_signal,
427 /* It's unfortunate that GTK+ doesn't have a generic
428 "user-modified-state-changed" signal. Instead, we have to try and
429 predict what widgets and signals are likely to exist in our dialogs. */
431 if ( GTK_IS_TOGGLE_BUTTON (w))
433 g_signal_connect_swapped (w, "toggled",
434 G_CALLBACK (psppire_dialog_notify_change),
438 if ( PSPPIRE_IS_SELECTOR (w))
440 g_signal_connect_swapped (w, "selected",
441 G_CALLBACK (psppire_dialog_notify_change),
444 g_signal_connect_swapped (w, "de-selected",
445 G_CALLBACK (psppire_dialog_notify_change),
449 if ( GTK_IS_EDITABLE (w))
451 g_signal_connect_swapped (w, "changed",
452 G_CALLBACK (psppire_dialog_notify_change),
456 if ( GTK_IS_CELL_EDITABLE (w))
458 g_signal_connect_swapped (w, "editing-done",
459 G_CALLBACK (psppire_dialog_notify_change),
463 if ( GTK_IS_TEXT_VIEW (w))
465 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
467 g_signal_connect_swapped (buffer, "changed",
468 G_CALLBACK (psppire_dialog_notify_change),
472 if ( GTK_IS_TREE_VIEW (w))
475 GtkTreeView *tv = GTK_TREE_VIEW (w);
476 GtkTreeSelection *selection =
477 gtk_tree_view_get_selection (tv);
478 GtkTreeViewColumn *col;
479 GtkTreeModel *model = gtk_tree_view_get_model (tv);
483 g_signal_connect_swapped (model, "row-changed",
484 G_CALLBACK (psppire_dialog_notify_change),
487 g_signal_connect_swapped (model, "row-deleted",
488 G_CALLBACK (psppire_dialog_notify_change),
491 g_signal_connect_swapped (model, "row-inserted",
492 G_CALLBACK (psppire_dialog_notify_change),
496 g_signal_connect_swapped (selection, "changed",
497 G_CALLBACK (psppire_dialog_notify_change),
500 while ((col = gtk_tree_view_get_column (tv, i++)))
502 GList *renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
503 GList *start = renderers;
506 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
507 g_signal_connect_swapped (renderers->data, "toggled",
508 G_CALLBACK (psppire_dialog_notify_change), dialog);
509 renderers = renderers->next;
518 psppire_dialog_run (PsppireDialog *dialog)
520 if ( dialog->contents_are_valid != NULL )
521 gtk_container_foreach (GTK_CONTAINER (dialog->box),
522 connect_notify_signal,
525 dialog->loop = g_main_loop_new (NULL, FALSE);
527 gtk_widget_show (GTK_WIDGET (dialog));
529 if ( dialog->contents_are_valid != NULL)
530 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
532 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
534 gdk_threads_leave ();
535 g_main_loop_run (dialog->loop);
536 gdk_threads_enter ();
538 g_main_loop_unref (dialog->loop);
540 g_signal_emit (dialog, signals [RESPONSE], 0, dialog->response);
542 return dialog->response;
547 psppire_dialog_reload (PsppireDialog *dialog)
549 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
554 psppire_dialog_help (PsppireDialog *dialog)
557 g_object_get (dialog, "name", &name, NULL);
561 g_signal_emit (dialog, signals [DIALOG_HELP], 0, name);
566 psppire_orientation_get_type (void)
568 static GType etype = 0;
571 static const GEnumValue values[] =
573 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
574 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
575 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
579 etype = g_enum_register_static
580 (g_intern_static_string ("PsppireOrientation"), values);
588 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
589 ContentsAreValid contents_are_valid,
592 dialog->contents_are_valid = contents_are_valid;
593 dialog->validity_data = data;
601 get_internal_child (GtkBuildable *buildable,
603 const gchar *childname)
605 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
607 if ( 0 == strcmp (childname, "hbox"))
608 return G_OBJECT (dialog->box);
616 psppire_dialog_buildable_init (GtkBuildableIface *iface)
618 iface->get_internal_child = get_internal_child;