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 g_object_set (dialog, "icon-name", "psppicon", NULL);
294 gtk_widget_show_all (dialog->box);
299 psppire_dialog_new (void)
301 PsppireDialog *dialog ;
303 dialog = g_object_new (psppire_dialog_get_type (),
306 return GTK_WIDGET (dialog) ;
311 psppire_dialog_notify_change (PsppireDialog *dialog)
313 if ( dialog->contents_are_valid )
315 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
317 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
322 /* Descend the widget tree, connecting appropriate signals to the
323 psppire_dialog_notify_change callback */
325 connect_notify_signal (GtkWidget *w, gpointer data)
327 PsppireDialog *dialog = data;
329 if ( PSPPIRE_IS_BUTTONBOX (w))
334 if ( GTK_IS_CONTAINER (w))
336 gtk_container_foreach (GTK_CONTAINER (w),
337 connect_notify_signal,
342 /* It's unfortunate that GTK+ doesn't have a generic
343 "user-modified-state-changed" signal. Instead, we have to try and
344 predict what widgets and signals are likely to exist in our dialogs. */
346 if ( GTK_IS_TOGGLE_BUTTON (w))
348 g_signal_connect_swapped (w, "toggled",
349 G_CALLBACK (psppire_dialog_notify_change),
353 if ( PSPPIRE_IS_SELECTOR (w))
355 g_signal_connect_swapped (w, "selected",
356 G_CALLBACK (psppire_dialog_notify_change),
359 g_signal_connect_swapped (w, "de-selected",
360 G_CALLBACK (psppire_dialog_notify_change),
364 if ( GTK_IS_EDITABLE (w))
366 g_signal_connect_swapped (w, "changed",
367 G_CALLBACK (psppire_dialog_notify_change),
371 if ( GTK_IS_CELL_EDITABLE (w))
373 g_signal_connect_swapped (w, "editing-done",
374 G_CALLBACK (psppire_dialog_notify_change),
378 if ( GTK_IS_TEXT_VIEW (w))
380 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
382 g_signal_connect_swapped (buffer, "changed",
383 G_CALLBACK (psppire_dialog_notify_change),
387 if ( GTK_IS_TREE_VIEW (w))
390 GtkTreeView *tv = GTK_TREE_VIEW (w);
391 GtkTreeSelection *selection =
392 gtk_tree_view_get_selection (tv);
393 GtkTreeViewColumn *col;
394 GtkTreeModel *model = gtk_tree_view_get_model (tv);
398 g_signal_connect_swapped (model, "row-changed",
399 G_CALLBACK (psppire_dialog_notify_change),
402 g_signal_connect_swapped (model, "row-deleted",
403 G_CALLBACK (psppire_dialog_notify_change),
406 g_signal_connect_swapped (model, "row-inserted",
407 G_CALLBACK (psppire_dialog_notify_change),
411 g_signal_connect_swapped (selection, "changed",
412 G_CALLBACK (psppire_dialog_notify_change),
415 while ((col = gtk_tree_view_get_column (tv, i++)))
417 GList *renderers = gtk_tree_view_column_get_cell_renderers (col);
418 GList *start = renderers;
421 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
422 g_signal_connect_swapped (renderers->data, "toggled",
423 G_CALLBACK (psppire_dialog_notify_change), dialog);
424 renderers = renderers->next;
433 psppire_dialog_run (PsppireDialog *dialog)
435 if ( dialog->contents_are_valid != NULL )
436 gtk_container_foreach (GTK_CONTAINER (dialog->box),
437 connect_notify_signal,
440 dialog->loop = g_main_loop_new (NULL, FALSE);
442 gtk_widget_show (GTK_WIDGET (dialog));
444 if ( dialog->contents_are_valid != NULL)
445 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
447 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
449 g_main_loop_run (dialog->loop);
451 g_main_loop_unref (dialog->loop);
453 return dialog->response;
458 psppire_dialog_reload (PsppireDialog *dialog)
460 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
467 psppire_orientation_get_type (void)
469 static GType etype = 0;
472 static const GEnumValue values[] =
474 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
475 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
476 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
480 etype = g_enum_register_static
481 (g_intern_static_string ("PsppireOrientation"), values);
489 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
490 ContentsAreValid contents_are_valid,
493 dialog->contents_are_valid = contents_are_valid;
494 dialog->validity_data = data;
502 get_internal_child (GtkBuildable *buildable,
504 const gchar *childname)
506 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
508 if ( 0 == strcmp (childname, "hbox"))
509 return G_OBJECT (dialog->box);
517 psppire_dialog_buildable_init (GtkBuildableIface *iface)
519 iface->get_internal_child = get_internal_child;