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"
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,
82 static GObjectClass *parent_class = NULL;
86 psppire_dialog_finalize (GObject *object)
88 PsppireDialog *dialog ;
90 g_return_if_fail (object != NULL);
91 g_return_if_fail (PSPPIRE_IS_DIALOG (object));
93 dialog = PSPPIRE_DIALOG (object);
95 if (G_OBJECT_CLASS (parent_class)->finalize)
96 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) )
122 g_value_set_enum (value, PSPPIRE_VERTICAL);
123 else if ( GTK_IS_HBOX (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_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137 dialog_set_orientation (PsppireDialog *dialog, const GValue *orval)
139 PsppireOrientation orientation = g_value_get_enum (orval);
141 if ( dialog->box != NULL)
143 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
146 switch ( orientation )
148 case PSPPIRE_HORIZONTAL:
149 dialog->box = gtk_hbox_new (FALSE, 5);
151 case PSPPIRE_VERTICAL:
152 dialog->box = gtk_vbox_new (FALSE, 5);
154 case PSPPIRE_TABULAR:
155 dialog->box = gtk_table_new (2, 3, FALSE);
156 g_object_set (dialog->box,
163 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
168 psppire_dialog_set_property (GObject *object,
174 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
178 case PROP_ORIENTATION:
179 dialog_set_orientation (dialog, value);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 static GParamSpec *orientation_spec ;
191 psppire_dialog_class_init (PsppireDialogClass *class)
193 GObjectClass *object_class = (GObjectClass *) class;
197 g_param_spec_enum ("orientation",
199 "Which way widgets are packed",
200 G_TYPE_PSPPIRE_ORIENTATION,
201 PSPPIRE_HORIZONTAL /* default value */,
202 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
205 object_class->set_property = psppire_dialog_set_property;
206 object_class->get_property = psppire_dialog_get_property;
208 g_object_class_install_property (object_class,
214 signals [DIALOG_REFRESH] =
215 g_signal_new ("refresh",
216 G_TYPE_FROM_CLASS (class),
220 g_cclosure_marshal_VOID__VOID,
225 signals [VALIDITY_CHANGED] =
226 g_signal_new ("validity-changed",
227 G_TYPE_FROM_CLASS (class),
231 g_cclosure_marshal_VOID__BOOLEAN,
237 object_class->finalize = psppire_dialog_finalize;
244 close_dialog (GtkWidget *w, gpointer data)
246 PsppireDialog *dialog = data;
248 psppire_dialog_close (dialog);
252 psppire_dialog_close (PsppireDialog *dialog)
254 g_main_loop_quit (dialog->loop);
255 gtk_widget_hide (GTK_WIDGET (dialog));
260 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
262 close_dialog (w, data);
267 psppire_dialog_init (PsppireDialog *dialog)
271 dialog->contents_are_valid = NULL;
272 dialog->validity_data = NULL;
274 g_value_init (&value, orientation_spec->value_type);
275 g_param_value_set_default (orientation_spec, &value);
277 gtk_window_set_type_hint (GTK_WINDOW (dialog),
278 GDK_WINDOW_TYPE_HINT_DIALOG);
280 dialog_set_orientation (dialog, &value);
282 g_value_unset (&value);
284 g_signal_connect (G_OBJECT (dialog), "delete-event",
285 G_CALLBACK (delete_event_callback),
288 gtk_window_set_type_hint (GTK_WINDOW (dialog),
289 GDK_WINDOW_TYPE_HINT_DIALOG);
291 gtk_widget_show_all (dialog->box);
296 psppire_dialog_new (void)
298 PsppireDialog *dialog ;
300 dialog = g_object_new (psppire_dialog_get_type (), NULL);
302 return GTK_WIDGET (dialog) ;
307 psppire_dialog_notify_change (PsppireDialog *dialog)
309 if ( dialog->contents_are_valid )
311 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
313 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
318 /* Descend the widget tree, connecting appropriate signals to the
319 psppire_dialog_notify_change callback */
321 connect_notify_signal (GtkWidget *w, gpointer data)
323 PsppireDialog *dialog = data;
325 if ( PSPPIRE_IS_BUTTONBOX (w))
330 if ( GTK_IS_CONTAINER (w))
332 gtk_container_foreach (GTK_CONTAINER (w),
333 connect_notify_signal,
338 /* It's unfortunate that GTK+ doesn't have a generic
339 "user-modified-state-changed" signal. Instead, we have to try and
340 predict what widgets and signals are likely to exist in our dialogs. */
342 if ( GTK_IS_TOGGLE_BUTTON (w))
344 g_signal_connect_swapped (w, "toggled",
345 G_CALLBACK (psppire_dialog_notify_change),
349 if ( PSPPIRE_IS_SELECTOR (w))
351 g_signal_connect_swapped (w, "selected",
352 G_CALLBACK (psppire_dialog_notify_change),
355 g_signal_connect_swapped (w, "de-selected",
356 G_CALLBACK (psppire_dialog_notify_change),
360 if ( GTK_IS_EDITABLE (w))
362 g_signal_connect_swapped (w, "changed",
363 G_CALLBACK (psppire_dialog_notify_change),
367 if ( GTK_IS_CELL_EDITABLE (w))
369 g_signal_connect_swapped (w, "editing-done",
370 G_CALLBACK (psppire_dialog_notify_change),
374 if ( GTK_IS_TEXT_VIEW (w))
376 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
378 g_signal_connect_swapped (buffer, "changed",
379 G_CALLBACK (psppire_dialog_notify_change),
383 if ( GTK_IS_TREE_VIEW (w))
386 GtkTreeView *tv = GTK_TREE_VIEW (w);
387 GtkTreeSelection *selection =
388 gtk_tree_view_get_selection (tv);
389 GtkTreeViewColumn *col;
390 GtkTreeModel *model = gtk_tree_view_get_model (tv);
394 g_signal_connect_swapped (model, "row-changed",
395 G_CALLBACK (psppire_dialog_notify_change),
398 g_signal_connect_swapped (model, "row-deleted",
399 G_CALLBACK (psppire_dialog_notify_change),
402 g_signal_connect_swapped (model, "row-inserted",
403 G_CALLBACK (psppire_dialog_notify_change),
407 g_signal_connect_swapped (selection, "changed",
408 G_CALLBACK (psppire_dialog_notify_change),
411 while ((col = gtk_tree_view_get_column (tv, i++)))
413 GList *renderers = gtk_tree_view_column_get_cell_renderers (col);
414 GList *start = renderers;
417 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
418 g_signal_connect_swapped (renderers->data, "toggled",
419 G_CALLBACK (psppire_dialog_notify_change), dialog);
420 renderers = renderers->next;
429 psppire_dialog_run (PsppireDialog *dialog)
431 if ( dialog->contents_are_valid != NULL )
432 gtk_container_foreach (GTK_CONTAINER (dialog->box),
433 connect_notify_signal,
436 dialog->loop = g_main_loop_new (NULL, FALSE);
438 gtk_widget_show (GTK_WIDGET (dialog));
440 if ( dialog->contents_are_valid != NULL)
441 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
443 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
445 g_main_loop_run (dialog->loop);
447 g_main_loop_unref (dialog->loop);
449 return dialog->response;
454 psppire_dialog_reload (PsppireDialog *dialog)
456 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
463 psppire_orientation_get_type (void)
465 static GType etype = 0;
468 static const GEnumValue values[] =
470 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
471 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
472 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
476 etype = g_enum_register_static
477 (g_intern_static_string ("PsppireOrientation"), values);
485 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
486 ContentsAreValid contents_are_valid,
489 dialog->contents_are_valid = contents_are_valid;
490 dialog->validity_data = data;
498 get_internal_child (GtkBuildable *buildable,
500 const gchar *childname)
502 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
504 if ( 0 == g_strcmp0 (childname, "hbox"))
505 return G_OBJECT (dialog->box);
513 psppire_dialog_buildable_init (GtkBuildableIface *iface)
515 iface->get_internal_child = get_internal_child;