1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 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 <gtk/gtksignal.h>
22 #include <gtk/gtkbuildable.h>
23 #include "psppire-dialog.h"
24 #include "psppire-buttonbox.h"
25 #include "psppire-selector.h"
26 #include "psppire-conf.h"
29 static void psppire_dialog_class_init (PsppireDialogClass *);
30 static void psppire_dialog_init (PsppireDialog *);
37 static guint signals [n_SIGNALS];
40 static void psppire_dialog_buildable_init (GtkBuildableIface *iface);
44 psppire_dialog_get_type (void)
46 static GType dialog_type = 0;
50 static const GTypeInfo dialog_info =
52 sizeof (PsppireDialogClass),
54 NULL, /* base_finalize */
55 (GClassInitFunc) psppire_dialog_class_init,
56 NULL, /* class_finalize */
57 NULL, /* class_data */
58 sizeof (PsppireDialog),
60 (GInstanceInitFunc) psppire_dialog_init,
63 static const GInterfaceInfo buildable_info =
65 (GInterfaceInitFunc) psppire_dialog_buildable_init,
70 dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
71 "PsppireDialog", &dialog_info, 0);
73 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);
112 psppire_dialog_get_property (GObject *object,
117 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
121 case PROP_ORIENTATION:
123 if ( GTK_IS_VBOX (dialog->box) )
124 g_value_set_enum (value, PSPPIRE_VERTICAL);
125 else if ( GTK_IS_HBOX (dialog->box))
126 g_value_set_enum (value, PSPPIRE_HORIZONTAL);
127 else if ( GTK_IS_TABLE (dialog->box))
128 g_value_set_enum (value, PSPPIRE_TABULAR);
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 dialog_set_orientation (PsppireDialog *dialog, const GValue *orval)
141 PsppireOrientation orientation = g_value_get_enum (orval);
143 if ( dialog->box != NULL)
145 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
148 switch ( orientation )
150 case PSPPIRE_HORIZONTAL:
151 dialog->box = gtk_hbox_new (FALSE, 5);
153 case PSPPIRE_VERTICAL:
154 dialog->box = gtk_vbox_new (FALSE, 5);
156 case PSPPIRE_TABULAR:
157 dialog->box = gtk_table_new (2, 3, FALSE);
158 g_object_set (dialog->box,
165 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
170 psppire_dialog_set_property (GObject *object,
176 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
180 case PROP_ORIENTATION:
181 dialog_set_orientation (dialog, value);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 static GParamSpec *orientation_spec ;
193 psppire_dialog_class_init (PsppireDialogClass *class)
195 GObjectClass *object_class = (GObjectClass *) class;
199 g_param_spec_enum ("orientation",
201 "Which way widgets are packed",
202 G_TYPE_PSPPIRE_ORIENTATION,
203 PSPPIRE_HORIZONTAL /* default value */,
204 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
207 object_class->set_property = psppire_dialog_set_property;
208 object_class->get_property = psppire_dialog_get_property;
210 g_object_class_install_property (object_class,
216 signals [DIALOG_REFRESH] =
217 g_signal_new ("refresh",
218 G_TYPE_FROM_CLASS (class),
222 g_cclosure_marshal_VOID__VOID,
227 signals [VALIDITY_CHANGED] =
228 g_signal_new ("validity-changed",
229 G_TYPE_FROM_CLASS (class),
233 g_cclosure_marshal_VOID__BOOLEAN,
239 object_class->finalize = psppire_dialog_finalize;
246 close_dialog (GtkWidget *w, gpointer data)
248 PsppireDialog *dialog = data;
250 psppire_dialog_close (dialog);
254 psppire_dialog_close (PsppireDialog *dialog)
256 g_main_loop_quit (dialog->loop);
257 gtk_widget_hide (GTK_WIDGET (dialog));
262 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
264 close_dialog (w, data);
269 configure_event_callback (GtkDialog *dialog,
270 GdkEventConfigure *event, gpointer data)
274 PsppireConf *conf = psppire_conf_new ();
276 if ( ! GTK_WIDGET_MAPPED (dialog))
279 g_object_get (dialog, "name", &base, NULL);
281 psppire_conf_save_window_geometry (conf, base, event);
288 on_realize (GtkWindow *dialog, gpointer data)
290 PsppireConf *conf = psppire_conf_new ();
292 const gchar *base = NULL;
294 g_object_get (dialog, "name", &base, NULL);
296 psppire_conf_set_window_geometry (conf, base, dialog);
302 psppire_dialog_init (PsppireDialog *dialog)
306 dialog->contents_are_valid = NULL;
307 dialog->validity_data = NULL;
309 g_value_init (&value, orientation_spec->value_type);
310 g_param_value_set_default (orientation_spec, &value);
312 gtk_window_set_type_hint (GTK_WINDOW (dialog),
313 GDK_WINDOW_TYPE_HINT_DIALOG);
315 dialog_set_orientation (dialog, &value);
317 g_value_unset (&value);
319 g_signal_connect (dialog, "delete-event",
320 G_CALLBACK (delete_event_callback),
323 g_signal_connect (dialog, "configure-event",
324 G_CALLBACK (configure_event_callback),
327 g_signal_connect (dialog, "realize",
328 G_CALLBACK (on_realize),
332 gtk_window_set_type_hint (GTK_WINDOW (dialog),
333 GDK_WINDOW_TYPE_HINT_DIALOG);
335 g_object_set (dialog, "icon-name", "psppicon", NULL);
337 gtk_widget_show_all (dialog->box);
342 psppire_dialog_new (void)
344 PsppireDialog *dialog ;
346 dialog = g_object_new (psppire_dialog_get_type (),
349 return GTK_WIDGET (dialog) ;
354 psppire_dialog_notify_change (PsppireDialog *dialog)
356 if ( dialog->contents_are_valid )
358 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
360 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
365 /* Descend the widget tree, connecting appropriate signals to the
366 psppire_dialog_notify_change callback */
368 connect_notify_signal (GtkWidget *w, gpointer data)
370 PsppireDialog *dialog = data;
372 if ( PSPPIRE_IS_BUTTONBOX (w))
377 if ( GTK_IS_CONTAINER (w))
379 gtk_container_foreach (GTK_CONTAINER (w),
380 connect_notify_signal,
385 /* It's unfortunate that GTK+ doesn't have a generic
386 "user-modified-state-changed" signal. Instead, we have to try and
387 predict what widgets and signals are likely to exist in our dialogs. */
389 if ( GTK_IS_TOGGLE_BUTTON (w))
391 g_signal_connect_swapped (w, "toggled",
392 G_CALLBACK (psppire_dialog_notify_change),
396 if ( PSPPIRE_IS_SELECTOR (w))
398 g_signal_connect_swapped (w, "selected",
399 G_CALLBACK (psppire_dialog_notify_change),
402 g_signal_connect_swapped (w, "de-selected",
403 G_CALLBACK (psppire_dialog_notify_change),
407 if ( GTK_IS_EDITABLE (w))
409 g_signal_connect_swapped (w, "changed",
410 G_CALLBACK (psppire_dialog_notify_change),
414 if ( GTK_IS_CELL_EDITABLE (w))
416 g_signal_connect_swapped (w, "editing-done",
417 G_CALLBACK (psppire_dialog_notify_change),
421 if ( GTK_IS_TEXT_VIEW (w))
423 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
425 g_signal_connect_swapped (buffer, "changed",
426 G_CALLBACK (psppire_dialog_notify_change),
430 if ( GTK_IS_TREE_VIEW (w))
433 GtkTreeView *tv = GTK_TREE_VIEW (w);
434 GtkTreeSelection *selection =
435 gtk_tree_view_get_selection (tv);
436 GtkTreeViewColumn *col;
437 GtkTreeModel *model = gtk_tree_view_get_model (tv);
441 g_signal_connect_swapped (model, "row-changed",
442 G_CALLBACK (psppire_dialog_notify_change),
445 g_signal_connect_swapped (model, "row-deleted",
446 G_CALLBACK (psppire_dialog_notify_change),
449 g_signal_connect_swapped (model, "row-inserted",
450 G_CALLBACK (psppire_dialog_notify_change),
454 g_signal_connect_swapped (selection, "changed",
455 G_CALLBACK (psppire_dialog_notify_change),
458 while ((col = gtk_tree_view_get_column (tv, i++)))
460 GList *renderers = gtk_tree_view_column_get_cell_renderers (col);
461 GList *start = renderers;
464 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
465 g_signal_connect_swapped (renderers->data, "toggled",
466 G_CALLBACK (psppire_dialog_notify_change), dialog);
467 renderers = renderers->next;
476 psppire_dialog_run (PsppireDialog *dialog)
478 if ( dialog->contents_are_valid != NULL )
479 gtk_container_foreach (GTK_CONTAINER (dialog->box),
480 connect_notify_signal,
483 dialog->loop = g_main_loop_new (NULL, FALSE);
485 gtk_widget_show (GTK_WIDGET (dialog));
487 if ( dialog->contents_are_valid != NULL)
488 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
490 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
492 g_main_loop_run (dialog->loop);
494 g_main_loop_unref (dialog->loop);
496 return dialog->response;
501 psppire_dialog_reload (PsppireDialog *dialog)
503 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
510 psppire_orientation_get_type (void)
512 static GType etype = 0;
515 static const GEnumValue values[] =
517 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
518 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
519 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
523 etype = g_enum_register_static
524 (g_intern_static_string ("PsppireOrientation"), values);
532 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
533 ContentsAreValid contents_are_valid,
536 dialog->contents_are_valid = contents_are_valid;
537 dialog->validity_data = data;
545 get_internal_child (GtkBuildable *buildable,
547 const gchar *childname)
549 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
551 if ( 0 == strcmp (childname, "hbox"))
552 return G_OBJECT (dialog->box);
560 psppire_dialog_buildable_init (GtkBuildableIface *iface)
562 iface->get_internal_child = get_internal_child;