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"
28 static void psppire_dialog_class_init (PsppireDialogClass *);
29 static void psppire_dialog_init (PsppireDialog *);
36 static guint signals [n_SIGNALS];
39 static void psppire_dialog_buildable_init (GtkBuildableIface *iface);
43 psppire_dialog_get_type (void)
45 static GType dialog_type = 0;
49 static const GTypeInfo dialog_info =
51 sizeof (PsppireDialogClass),
53 NULL, /* base_finalize */
54 (GClassInitFunc) psppire_dialog_class_init,
55 NULL, /* class_finalize */
56 NULL, /* class_data */
57 sizeof (PsppireDialog),
59 (GInstanceInitFunc) psppire_dialog_init,
62 static const GInterfaceInfo buildable_info =
64 (GInterfaceInitFunc) psppire_dialog_buildable_init,
69 dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
70 "PsppireDialog", &dialog_info, 0);
72 g_type_add_interface_static (dialog_type,
83 static GObjectClass *parent_class = NULL;
87 psppire_dialog_finalize (GObject *object)
89 PsppireDialog *dialog ;
91 g_return_if_fail (object != NULL);
92 g_return_if_fail (PSPPIRE_IS_DIALOG (object));
94 dialog = PSPPIRE_DIALOG (object);
96 if (G_OBJECT_CLASS (parent_class)->finalize)
97 G_OBJECT_CLASS (parent_class)->finalize (object);
111 psppire_dialog_get_property (GObject *object,
116 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
120 case PROP_ORIENTATION:
122 if ( GTK_IS_VBOX (dialog->box) )
123 g_value_set_enum (value, PSPPIRE_VERTICAL);
124 else if ( GTK_IS_HBOX (dialog->box))
125 g_value_set_enum (value, PSPPIRE_HORIZONTAL);
126 else if ( GTK_IS_TABLE (dialog->box))
127 g_value_set_enum (value, PSPPIRE_TABULAR);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 dialog_set_orientation (PsppireDialog *dialog, const GValue *orval)
140 PsppireOrientation orientation = g_value_get_enum (orval);
142 if ( dialog->box != NULL)
144 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
147 switch ( orientation )
149 case PSPPIRE_HORIZONTAL:
150 dialog->box = gtk_hbox_new (FALSE, 5);
152 case PSPPIRE_VERTICAL:
153 dialog->box = gtk_vbox_new (FALSE, 5);
155 case PSPPIRE_TABULAR:
156 dialog->box = gtk_table_new (2, 3, FALSE);
157 g_object_set (dialog->box,
164 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
169 psppire_dialog_set_property (GObject *object,
175 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
179 case PROP_ORIENTATION:
180 dialog_set_orientation (dialog, value);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 static GParamSpec *orientation_spec ;
192 psppire_dialog_class_init (PsppireDialogClass *class)
194 GObjectClass *object_class = (GObjectClass *) class;
198 g_param_spec_enum ("orientation",
200 "Which way widgets are packed",
201 G_TYPE_PSPPIRE_ORIENTATION,
202 PSPPIRE_HORIZONTAL /* default value */,
203 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
206 object_class->set_property = psppire_dialog_set_property;
207 object_class->get_property = psppire_dialog_get_property;
209 g_object_class_install_property (object_class,
215 signals [DIALOG_REFRESH] =
216 g_signal_new ("refresh",
217 G_TYPE_FROM_CLASS (class),
221 g_cclosure_marshal_VOID__VOID,
226 signals [VALIDITY_CHANGED] =
227 g_signal_new ("validity-changed",
228 G_TYPE_FROM_CLASS (class),
232 g_cclosure_marshal_VOID__BOOLEAN,
238 object_class->finalize = psppire_dialog_finalize;
245 close_dialog (GtkWidget *w, gpointer data)
247 PsppireDialog *dialog = data;
249 psppire_dialog_close (dialog);
253 psppire_dialog_close (PsppireDialog *dialog)
255 g_main_loop_quit (dialog->loop);
256 gtk_widget_hide (GTK_WIDGET (dialog));
261 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
263 close_dialog (w, data);
268 psppire_dialog_init (PsppireDialog *dialog)
272 dialog->contents_are_valid = NULL;
273 dialog->validity_data = NULL;
275 g_value_init (&value, orientation_spec->value_type);
276 g_param_value_set_default (orientation_spec, &value);
278 gtk_window_set_type_hint (GTK_WINDOW (dialog),
279 GDK_WINDOW_TYPE_HINT_DIALOG);
281 dialog_set_orientation (dialog, &value);
283 g_value_unset (&value);
285 g_signal_connect (G_OBJECT (dialog), "delete-event",
286 G_CALLBACK (delete_event_callback),
289 gtk_window_set_type_hint (GTK_WINDOW (dialog),
290 GDK_WINDOW_TYPE_HINT_DIALOG);
292 gtk_widget_show_all (dialog->box);
297 psppire_dialog_new (void)
299 PsppireDialog *dialog ;
301 dialog = g_object_new (psppire_dialog_get_type (), NULL);
303 return GTK_WIDGET (dialog) ;
308 psppire_dialog_notify_change (PsppireDialog *dialog)
310 if ( dialog->contents_are_valid )
312 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
314 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
319 /* Descend the widget tree, connecting appropriate signals to the
320 psppire_dialog_notify_change callback */
322 connect_notify_signal (GtkWidget *w, gpointer data)
324 PsppireDialog *dialog = data;
326 if ( PSPPIRE_IS_BUTTONBOX (w))
331 if ( GTK_IS_CONTAINER (w))
333 gtk_container_foreach (GTK_CONTAINER (w),
334 connect_notify_signal,
339 /* It's unfortunate that GTK+ doesn't have a generic
340 "user-modified-state-changed" signal. Instead, we have to try and
341 predict what widgets and signals are likely to exist in our dialogs. */
343 if ( GTK_IS_TOGGLE_BUTTON (w))
345 g_signal_connect_swapped (w, "toggled",
346 G_CALLBACK (psppire_dialog_notify_change),
350 if ( PSPPIRE_IS_SELECTOR (w))
352 g_signal_connect_swapped (w, "selected",
353 G_CALLBACK (psppire_dialog_notify_change),
356 g_signal_connect_swapped (w, "de-selected",
357 G_CALLBACK (psppire_dialog_notify_change),
361 if ( GTK_IS_EDITABLE (w))
363 g_signal_connect_swapped (w, "changed",
364 G_CALLBACK (psppire_dialog_notify_change),
368 if ( GTK_IS_CELL_EDITABLE (w))
370 g_signal_connect_swapped (w, "editing-done",
371 G_CALLBACK (psppire_dialog_notify_change),
375 if ( GTK_IS_TEXT_VIEW (w))
377 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
379 g_signal_connect_swapped (buffer, "changed",
380 G_CALLBACK (psppire_dialog_notify_change),
384 if ( GTK_IS_TREE_VIEW (w))
387 GtkTreeView *tv = GTK_TREE_VIEW (w);
388 GtkTreeSelection *selection =
389 gtk_tree_view_get_selection (tv);
390 GtkTreeViewColumn *col;
391 GtkTreeModel *model = gtk_tree_view_get_model (tv);
395 g_signal_connect_swapped (model, "row-changed",
396 G_CALLBACK (psppire_dialog_notify_change),
399 g_signal_connect_swapped (model, "row-deleted",
400 G_CALLBACK (psppire_dialog_notify_change),
403 g_signal_connect_swapped (model, "row-inserted",
404 G_CALLBACK (psppire_dialog_notify_change),
408 g_signal_connect_swapped (selection, "changed",
409 G_CALLBACK (psppire_dialog_notify_change),
412 while ((col = gtk_tree_view_get_column (tv, i++)))
414 GList *renderers = gtk_tree_view_column_get_cell_renderers (col);
415 GList *start = renderers;
418 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
419 g_signal_connect_swapped (renderers->data, "toggled",
420 G_CALLBACK (psppire_dialog_notify_change), dialog);
421 renderers = renderers->next;
430 psppire_dialog_run (PsppireDialog *dialog)
432 if ( dialog->contents_are_valid != NULL )
433 gtk_container_foreach (GTK_CONTAINER (dialog->box),
434 connect_notify_signal,
437 dialog->loop = g_main_loop_new (NULL, FALSE);
439 gtk_widget_show (GTK_WIDGET (dialog));
441 if ( dialog->contents_are_valid != NULL)
442 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
444 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
446 g_main_loop_run (dialog->loop);
448 g_main_loop_unref (dialog->loop);
450 return dialog->response;
455 psppire_dialog_reload (PsppireDialog *dialog)
457 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
464 psppire_orientation_get_type (void)
466 static GType etype = 0;
469 static const GEnumValue values[] =
471 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
472 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
473 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
477 etype = g_enum_register_static
478 (g_intern_static_string ("PsppireOrientation"), values);
486 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
487 ContentsAreValid contents_are_valid,
490 dialog->contents_are_valid = contents_are_valid;
491 dialog->validity_data = data;
499 get_internal_child (GtkBuildable *buildable,
501 const gchar *childname)
503 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
505 if ( 0 == strcmp (childname, "hbox"))
506 return G_OBJECT (dialog->box);
514 psppire_dialog_buildable_init (GtkBuildableIface *iface)
516 iface->get_internal_child = get_internal_child;