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 *);
38 static guint signals [n_SIGNALS];
41 static void psppire_dialog_buildable_init (GtkBuildableIface *iface);
45 psppire_dialog_get_type (void)
47 static GType dialog_type = 0;
51 static const GTypeInfo dialog_info =
53 sizeof (PsppireDialogClass),
55 NULL, /* base_finalize */
56 (GClassInitFunc) psppire_dialog_class_init,
57 NULL, /* class_finalize */
58 NULL, /* class_data */
59 sizeof (PsppireDialog),
61 (GInstanceInitFunc) psppire_dialog_init,
64 static const GInterfaceInfo buildable_info =
66 (GInterfaceInitFunc) psppire_dialog_buildable_init,
71 dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
72 "PsppireDialog", &dialog_info, 0);
74 g_type_add_interface_static (dialog_type,
84 static GObjectClass *parent_class = NULL;
88 psppire_dialog_finalize (GObject *object)
90 PsppireDialog *dialog ;
92 g_return_if_fail (object != NULL);
93 g_return_if_fail (PSPPIRE_IS_DIALOG (object));
95 dialog = PSPPIRE_DIALOG (object);
97 if (G_OBJECT_CLASS (parent_class)->finalize)
98 G_OBJECT_CLASS (parent_class)->finalize (object);
113 psppire_dialog_get_property (GObject *object,
118 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
122 case PROP_ORIENTATION:
124 if ( GTK_IS_VBOX (dialog->box) || GTK_VPANED (dialog->box))
125 g_value_set_enum (value, PSPPIRE_VERTICAL);
126 else if ( GTK_IS_HBOX (dialog->box) || GTK_HPANED (dialog->box))
127 g_value_set_enum (value, PSPPIRE_HORIZONTAL);
128 else if ( GTK_IS_TABLE (dialog->box))
129 g_value_set_enum (value, PSPPIRE_TABULAR);
133 g_value_set_boolean (value, dialog->slidable);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 dialog_set_container (PsppireDialog *dialog)
145 if ( dialog->box != NULL)
147 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
150 switch (dialog->orientation)
152 case PSPPIRE_HORIZONTAL:
153 if ( dialog->slidable)
154 dialog->box = gtk_hpaned_new();
156 dialog->box = gtk_hbox_new (FALSE, 5);
158 case PSPPIRE_VERTICAL:
159 if ( dialog->slidable)
160 dialog->box = gtk_vpaned_new();
162 dialog->box = gtk_vbox_new (FALSE, 5);
164 case PSPPIRE_TABULAR:
165 dialog->box = gtk_table_new (2, 3, FALSE);
166 g_object_set (dialog->box,
173 gtk_widget_show_all (dialog->box);
174 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
179 psppire_dialog_set_property (GObject *object,
185 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
190 dialog->slidable = g_value_get_boolean (value);
192 case PROP_ORIENTATION:
193 dialog->orientation = g_value_get_enum (value);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 dialog_set_container (dialog);
204 static GParamSpec *orientation_spec ;
207 psppire_dialog_class_init (PsppireDialogClass *class)
209 GObjectClass *object_class = (GObjectClass *) class;
211 GParamSpec *sliding_spec ;
214 g_param_spec_enum ("orientation",
216 "Which way widgets are packed",
217 PSPPIRE_TYPE_ORIENTATION,
218 PSPPIRE_HORIZONTAL /* default value */,
219 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
222 g_param_spec_boolean ("slidable",
224 "Can the container be sized by the user",
226 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
228 object_class->set_property = psppire_dialog_set_property;
229 object_class->get_property = psppire_dialog_get_property;
231 g_object_class_install_property (object_class,
236 g_object_class_install_property (object_class,
240 signals [DIALOG_REFRESH] =
241 g_signal_new ("refresh",
242 G_TYPE_FROM_CLASS (class),
246 g_cclosure_marshal_VOID__VOID,
251 signals [VALIDITY_CHANGED] =
252 g_signal_new ("validity-changed",
253 G_TYPE_FROM_CLASS (class),
257 g_cclosure_marshal_VOID__BOOLEAN,
263 signals [DIALOG_HELP] =
264 g_signal_new ("help",
265 G_TYPE_FROM_CLASS (class),
269 g_cclosure_marshal_VOID__STRING,
275 object_class->finalize = psppire_dialog_finalize;
282 close_dialog (GtkWidget *w, gpointer data)
284 PsppireDialog *dialog = data;
286 psppire_dialog_close (dialog);
290 psppire_dialog_close (PsppireDialog *dialog)
292 g_main_loop_quit (dialog->loop);
293 gtk_widget_hide (GTK_WIDGET (dialog));
298 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
300 close_dialog (w, data);
305 configure_event_callback (GtkDialog *dialog,
306 GdkEvent *event, gpointer data)
310 PsppireConf *conf = psppire_conf_new ();
312 if ( ! gtk_widget_get_mapped (GTK_WIDGET (dialog)))
315 base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
317 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (dialog));
324 on_realize (GtkWindow *dialog, gpointer data)
326 PsppireConf *conf = psppire_conf_new ();
328 const gchar *base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
330 psppire_conf_set_window_geometry (conf, base, dialog);
336 psppire_dialog_init (PsppireDialog *dialog)
340 dialog->contents_are_valid = NULL;
341 dialog->validity_data = NULL;
342 dialog->slidable = FALSE;
344 g_value_init (&value, orientation_spec->value_type);
345 g_param_value_set_default (orientation_spec, &value);
347 gtk_window_set_type_hint (GTK_WINDOW (dialog),
348 GDK_WINDOW_TYPE_HINT_DIALOG);
350 g_value_unset (&value);
352 g_signal_connect (dialog, "delete-event",
353 G_CALLBACK (delete_event_callback),
356 g_signal_connect (dialog, "configure-event",
357 G_CALLBACK (configure_event_callback),
360 g_signal_connect (dialog, "realize",
361 G_CALLBACK (on_realize),
365 gtk_window_set_type_hint (GTK_WINDOW (dialog),
366 GDK_WINDOW_TYPE_HINT_DIALOG);
368 g_object_set (dialog, "icon-name", "pspp", NULL);
373 psppire_dialog_new (void)
375 PsppireDialog *dialog ;
377 dialog = g_object_new (psppire_dialog_get_type (),
380 return GTK_WIDGET (dialog) ;
385 psppire_dialog_notify_change (PsppireDialog *dialog)
387 if ( dialog->contents_are_valid )
389 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
391 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
396 /* Descend the widget tree, connecting appropriate signals to the
397 psppire_dialog_notify_change callback */
399 connect_notify_signal (GtkWidget *w, gpointer data)
401 PsppireDialog *dialog = data;
403 if ( PSPPIRE_IS_BUTTONBOX (w))
406 if ( GTK_IS_CONTAINER (w))
408 gtk_container_foreach (GTK_CONTAINER (w),
409 connect_notify_signal,
414 /* It's unfortunate that GTK+ doesn't have a generic
415 "user-modified-state-changed" signal. Instead, we have to try and
416 predict what widgets and signals are likely to exist in our dialogs. */
418 if ( GTK_IS_TOGGLE_BUTTON (w))
420 g_signal_connect_swapped (w, "toggled",
421 G_CALLBACK (psppire_dialog_notify_change),
425 if ( PSPPIRE_IS_SELECTOR (w))
427 g_signal_connect_swapped (w, "selected",
428 G_CALLBACK (psppire_dialog_notify_change),
431 g_signal_connect_swapped (w, "de-selected",
432 G_CALLBACK (psppire_dialog_notify_change),
436 if ( GTK_IS_EDITABLE (w))
438 g_signal_connect_swapped (w, "changed",
439 G_CALLBACK (psppire_dialog_notify_change),
443 if ( GTK_IS_CELL_EDITABLE (w))
445 g_signal_connect_swapped (w, "editing-done",
446 G_CALLBACK (psppire_dialog_notify_change),
450 if ( GTK_IS_TEXT_VIEW (w))
452 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
454 g_signal_connect_swapped (buffer, "changed",
455 G_CALLBACK (psppire_dialog_notify_change),
459 if ( GTK_IS_TREE_VIEW (w))
462 GtkTreeView *tv = GTK_TREE_VIEW (w);
463 GtkTreeSelection *selection =
464 gtk_tree_view_get_selection (tv);
465 GtkTreeViewColumn *col;
466 GtkTreeModel *model = gtk_tree_view_get_model (tv);
470 g_signal_connect_swapped (model, "row-changed",
471 G_CALLBACK (psppire_dialog_notify_change),
474 g_signal_connect_swapped (model, "row-deleted",
475 G_CALLBACK (psppire_dialog_notify_change),
478 g_signal_connect_swapped (model, "row-inserted",
479 G_CALLBACK (psppire_dialog_notify_change),
483 g_signal_connect_swapped (selection, "changed",
484 G_CALLBACK (psppire_dialog_notify_change),
487 while ((col = gtk_tree_view_get_column (tv, i++)))
489 GList *renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
490 GList *start = renderers;
493 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
494 g_signal_connect_swapped (renderers->data, "toggled",
495 G_CALLBACK (psppire_dialog_notify_change), dialog);
496 renderers = renderers->next;
505 psppire_dialog_run (PsppireDialog *dialog)
507 if ( dialog->contents_are_valid != NULL )
508 gtk_container_foreach (GTK_CONTAINER (dialog->box),
509 connect_notify_signal,
512 dialog->loop = g_main_loop_new (NULL, FALSE);
514 gtk_widget_show (GTK_WIDGET (dialog));
516 if ( dialog->contents_are_valid != NULL)
517 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
519 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
521 gdk_threads_leave ();
522 g_main_loop_run (dialog->loop);
523 gdk_threads_enter ();
525 g_main_loop_unref (dialog->loop);
527 return dialog->response;
532 psppire_dialog_reload (PsppireDialog *dialog)
534 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
539 psppire_dialog_help (PsppireDialog *dialog)
542 g_object_get (dialog, "name", &name, NULL);
546 g_signal_emit (dialog, signals [DIALOG_HELP], 0, name);
551 psppire_orientation_get_type (void)
553 static GType etype = 0;
556 static const GEnumValue values[] =
558 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
559 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
560 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
564 etype = g_enum_register_static
565 (g_intern_static_string ("PsppireOrientation"), values);
573 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
574 ContentsAreValid contents_are_valid,
577 dialog->contents_are_valid = contents_are_valid;
578 dialog->validity_data = data;
586 get_internal_child (GtkBuildable *buildable,
588 const gchar *childname)
590 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
592 if ( 0 == strcmp (childname, "hbox"))
593 return G_OBJECT (dialog->box);
601 psppire_dialog_buildable_init (GtkBuildableIface *iface)
603 iface->get_internal_child = get_internal_child;