1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011 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"
27 static void psppire_dialog_class_init (PsppireDialogClass *);
28 static void psppire_dialog_init (PsppireDialog *);
35 static guint signals [n_SIGNALS];
38 static void psppire_dialog_buildable_init (GtkBuildableIface *iface);
42 psppire_dialog_get_type (void)
44 static GType dialog_type = 0;
48 static const GTypeInfo dialog_info =
50 sizeof (PsppireDialogClass),
52 NULL, /* base_finalize */
53 (GClassInitFunc) psppire_dialog_class_init,
54 NULL, /* class_finalize */
55 NULL, /* class_data */
56 sizeof (PsppireDialog),
58 (GInstanceInitFunc) psppire_dialog_init,
61 static const GInterfaceInfo buildable_info =
63 (GInterfaceInitFunc) psppire_dialog_buildable_init,
68 dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
69 "PsppireDialog", &dialog_info, 0);
71 g_type_add_interface_static (dialog_type,
81 static GObjectClass *parent_class = NULL;
85 psppire_dialog_finalize (GObject *object)
87 PsppireDialog *dialog ;
89 g_return_if_fail (object != NULL);
90 g_return_if_fail (PSPPIRE_IS_DIALOG (object));
92 dialog = PSPPIRE_DIALOG (object);
94 if (G_OBJECT_CLASS (parent_class)->finalize)
95 G_OBJECT_CLASS (parent_class)->finalize (object);
110 psppire_dialog_get_property (GObject *object,
115 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
119 case PROP_ORIENTATION:
121 if ( GTK_IS_VBOX (dialog->box) || GTK_VPANED (dialog->box))
122 g_value_set_enum (value, PSPPIRE_VERTICAL);
123 else if ( GTK_IS_HBOX (dialog->box) || GTK_HPANED (dialog->box))
124 g_value_set_enum (value, PSPPIRE_HORIZONTAL);
125 else if ( GTK_IS_TABLE (dialog->box))
126 g_value_set_enum (value, PSPPIRE_TABULAR);
130 g_value_set_boolean (value, dialog->slidable);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 dialog_set_container (PsppireDialog *dialog)
142 if ( dialog->box != NULL)
144 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
147 switch (dialog->orientation)
149 case PSPPIRE_HORIZONTAL:
150 if ( dialog->slidable)
151 dialog->box = gtk_hpaned_new();
153 dialog->box = gtk_hbox_new (FALSE, 5);
155 case PSPPIRE_VERTICAL:
156 if ( dialog->slidable)
157 dialog->box = gtk_vpaned_new();
159 dialog->box = gtk_vbox_new (FALSE, 5);
161 case PSPPIRE_TABULAR:
162 dialog->box = gtk_table_new (2, 3, FALSE);
163 g_object_set (dialog->box,
170 gtk_widget_show_all (dialog->box);
171 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
176 psppire_dialog_set_property (GObject *object,
182 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
187 dialog->slidable = g_value_get_boolean (value);
189 case PROP_ORIENTATION:
190 dialog->orientation = g_value_get_enum (value);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197 dialog_set_container (dialog);
201 static GParamSpec *orientation_spec ;
204 psppire_dialog_class_init (PsppireDialogClass *class)
206 GObjectClass *object_class = (GObjectClass *) class;
208 GParamSpec *sliding_spec ;
211 g_param_spec_enum ("orientation",
213 "Which way widgets are packed",
214 PSPPIRE_TYPE_ORIENTATION,
215 PSPPIRE_HORIZONTAL /* default value */,
216 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
219 g_param_spec_boolean ("slidable",
221 "Can the container be sized by the user",
223 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
227 object_class->set_property = psppire_dialog_set_property;
228 object_class->get_property = psppire_dialog_get_property;
230 g_object_class_install_property (object_class,
235 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,
252 signals [VALIDITY_CHANGED] =
253 g_signal_new ("validity-changed",
254 G_TYPE_FROM_CLASS (class),
258 g_cclosure_marshal_VOID__BOOLEAN,
264 object_class->finalize = psppire_dialog_finalize;
271 close_dialog (GtkWidget *w, gpointer data)
273 PsppireDialog *dialog = data;
275 psppire_dialog_close (dialog);
279 psppire_dialog_close (PsppireDialog *dialog)
281 g_main_loop_quit (dialog->loop);
282 gtk_widget_hide (GTK_WIDGET (dialog));
287 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
289 close_dialog (w, data);
294 configure_event_callback (GtkDialog *dialog,
295 GdkEvent *event, gpointer data)
299 PsppireConf *conf = psppire_conf_new ();
301 if ( ! GTK_WIDGET_MAPPED (dialog))
304 base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
306 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (dialog));
313 on_realize (GtkWindow *dialog, gpointer data)
315 PsppireConf *conf = psppire_conf_new ();
317 const gchar *base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
319 psppire_conf_set_window_geometry (conf, base, dialog);
325 psppire_dialog_init (PsppireDialog *dialog)
329 dialog->contents_are_valid = NULL;
330 dialog->validity_data = NULL;
331 dialog->slidable = FALSE;
333 g_value_init (&value, orientation_spec->value_type);
334 g_param_value_set_default (orientation_spec, &value);
336 gtk_window_set_type_hint (GTK_WINDOW (dialog),
337 GDK_WINDOW_TYPE_HINT_DIALOG);
339 g_value_unset (&value);
341 g_signal_connect (dialog, "delete-event",
342 G_CALLBACK (delete_event_callback),
345 g_signal_connect (dialog, "configure-event",
346 G_CALLBACK (configure_event_callback),
349 g_signal_connect (dialog, "realize",
350 G_CALLBACK (on_realize),
354 gtk_window_set_type_hint (GTK_WINDOW (dialog),
355 GDK_WINDOW_TYPE_HINT_DIALOG);
357 g_object_set (dialog, "icon-name", "psppicon", NULL);
362 psppire_dialog_new (void)
364 PsppireDialog *dialog ;
366 dialog = g_object_new (psppire_dialog_get_type (),
369 return GTK_WIDGET (dialog) ;
374 psppire_dialog_notify_change (PsppireDialog *dialog)
376 if ( dialog->contents_are_valid )
378 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
380 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
385 /* Descend the widget tree, connecting appropriate signals to the
386 psppire_dialog_notify_change callback */
388 connect_notify_signal (GtkWidget *w, gpointer data)
390 PsppireDialog *dialog = data;
392 if ( PSPPIRE_IS_BUTTONBOX (w))
397 if ( GTK_IS_CONTAINER (w))
399 gtk_container_foreach (GTK_CONTAINER (w),
400 connect_notify_signal,
405 /* It's unfortunate that GTK+ doesn't have a generic
406 "user-modified-state-changed" signal. Instead, we have to try and
407 predict what widgets and signals are likely to exist in our dialogs. */
409 if ( GTK_IS_TOGGLE_BUTTON (w))
411 g_signal_connect_swapped (w, "toggled",
412 G_CALLBACK (psppire_dialog_notify_change),
416 if ( PSPPIRE_IS_SELECTOR (w))
418 g_signal_connect_swapped (w, "selected",
419 G_CALLBACK (psppire_dialog_notify_change),
422 g_signal_connect_swapped (w, "de-selected",
423 G_CALLBACK (psppire_dialog_notify_change),
427 if ( GTK_IS_EDITABLE (w))
429 g_signal_connect_swapped (w, "changed",
430 G_CALLBACK (psppire_dialog_notify_change),
434 if ( GTK_IS_CELL_EDITABLE (w))
436 g_signal_connect_swapped (w, "editing-done",
437 G_CALLBACK (psppire_dialog_notify_change),
441 if ( GTK_IS_TEXT_VIEW (w))
443 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
445 g_signal_connect_swapped (buffer, "changed",
446 G_CALLBACK (psppire_dialog_notify_change),
450 if ( GTK_IS_TREE_VIEW (w))
453 GtkTreeView *tv = GTK_TREE_VIEW (w);
454 GtkTreeSelection *selection =
455 gtk_tree_view_get_selection (tv);
456 GtkTreeViewColumn *col;
457 GtkTreeModel *model = gtk_tree_view_get_model (tv);
461 g_signal_connect_swapped (model, "row-changed",
462 G_CALLBACK (psppire_dialog_notify_change),
465 g_signal_connect_swapped (model, "row-deleted",
466 G_CALLBACK (psppire_dialog_notify_change),
469 g_signal_connect_swapped (model, "row-inserted",
470 G_CALLBACK (psppire_dialog_notify_change),
474 g_signal_connect_swapped (selection, "changed",
475 G_CALLBACK (psppire_dialog_notify_change),
478 while ((col = gtk_tree_view_get_column (tv, i++)))
480 GList *renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
481 GList *start = renderers;
484 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
485 g_signal_connect_swapped (renderers->data, "toggled",
486 G_CALLBACK (psppire_dialog_notify_change), dialog);
487 renderers = renderers->next;
496 psppire_dialog_run (PsppireDialog *dialog)
498 if ( dialog->contents_are_valid != NULL )
499 gtk_container_foreach (GTK_CONTAINER (dialog->box),
500 connect_notify_signal,
503 dialog->loop = g_main_loop_new (NULL, FALSE);
505 gtk_widget_show (GTK_WIDGET (dialog));
507 if ( dialog->contents_are_valid != NULL)
508 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
510 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
512 gdk_threads_leave ();
513 g_main_loop_run (dialog->loop);
514 gdk_threads_enter ();
516 g_main_loop_unref (dialog->loop);
518 return dialog->response;
523 psppire_dialog_reload (PsppireDialog *dialog)
525 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
532 psppire_orientation_get_type (void)
534 static GType etype = 0;
537 static const GEnumValue values[] =
539 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
540 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
541 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
545 etype = g_enum_register_static
546 (g_intern_static_string ("PsppireOrientation"), values);
554 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
555 ContentsAreValid contents_are_valid,
558 dialog->contents_are_valid = contents_are_valid;
559 dialog->validity_data = data;
567 get_internal_child (GtkBuildable *buildable,
569 const gchar *childname)
571 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
573 if ( 0 == strcmp (childname, "hbox"))
574 return G_OBJECT (dialog->box);
582 psppire_dialog_buildable_init (GtkBuildableIface *iface)
584 iface->get_internal_child = get_internal_child;