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 "psppire-dialog.h"
23 #include "psppire-buttonbox.h"
24 #include "psppire-selector.h"
26 static void psppire_dialog_class_init (PsppireDialogClass *);
27 static void psppire_dialog_init (PsppireDialog *);
34 static guint signals [n_SIGNALS];
38 psppire_dialog_get_type (void)
40 static GType dialog_type = 0;
44 static const GTypeInfo dialog_info =
46 sizeof (PsppireDialogClass),
48 NULL, /* base_finalize */
49 (GClassInitFunc) psppire_dialog_class_init,
50 NULL, /* class_finalize */
51 NULL, /* class_data */
52 sizeof (PsppireDialog),
54 (GInstanceInitFunc) psppire_dialog_init,
57 dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
58 "PsppireDialog", &dialog_info, 0);
66 static GObjectClass *parent_class = NULL;
70 psppire_dialog_finalize (GObject *object)
72 PsppireDialog *dialog ;
74 g_return_if_fail (object != NULL);
75 g_return_if_fail (PSPPIRE_IS_DIALOG (object));
77 dialog = PSPPIRE_DIALOG (object);
79 if (G_OBJECT_CLASS (parent_class)->finalize)
80 G_OBJECT_CLASS (parent_class)->finalize (object);
94 psppire_dialog_get_property (GObject *object,
99 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
103 case PROP_ORIENTATION:
105 if ( GTK_IS_VBOX (dialog->box) )
106 g_value_set_enum (value, PSPPIRE_VERTICAL);
107 else if ( GTK_IS_HBOX (dialog->box))
108 g_value_set_enum (value, PSPPIRE_HORIZONTAL);
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 dialog_set_orientation (PsppireDialog *dialog, const GValue *orval)
121 PsppireOrientation orientation = g_value_get_enum (orval);
123 if ( dialog->box != NULL)
125 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
128 if ( orientation == PSPPIRE_HORIZONTAL)
130 dialog->box = gtk_hbox_new (FALSE, 5);
134 dialog->box = gtk_vbox_new (FALSE, 5);
137 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
142 psppire_dialog_set_property (GObject *object,
148 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
152 case PROP_ORIENTATION:
153 dialog_set_orientation (dialog, value);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162 static GParamSpec *orientation_spec ;
165 psppire_dialog_class_init (PsppireDialogClass *class)
167 GObjectClass *object_class = (GObjectClass *) class;
171 g_param_spec_enum ("orientation",
173 "Which way widgets are packed",
174 G_TYPE_PSPPIRE_ORIENTATION,
175 PSPPIRE_HORIZONTAL /* default value */,
176 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
179 object_class->set_property = psppire_dialog_set_property;
180 object_class->get_property = psppire_dialog_get_property;
182 g_object_class_install_property (object_class,
188 signals [DIALOG_REFRESH] =
189 g_signal_new ("refresh",
190 G_TYPE_FROM_CLASS (class),
194 g_cclosure_marshal_VOID__VOID,
199 signals [VALIDITY_CHANGED] =
200 g_signal_new ("validity-changed",
201 G_TYPE_FROM_CLASS (class),
205 g_cclosure_marshal_VOID__BOOLEAN,
211 object_class->finalize = psppire_dialog_finalize;
218 close_dialog (GtkWidget *w, gpointer data)
220 PsppireDialog *dialog = data;
222 psppire_dialog_close (dialog);
226 psppire_dialog_close (PsppireDialog *dialog)
228 g_main_loop_quit (dialog->loop);
229 gtk_widget_hide (GTK_WIDGET (dialog));
234 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
236 close_dialog (w, data);
241 psppire_dialog_init (PsppireDialog *dialog)
245 dialog->contents_are_valid = NULL;
246 dialog->validity_data = NULL;
248 g_value_init (&value, orientation_spec->value_type);
249 g_param_value_set_default (orientation_spec, &value);
251 gtk_window_set_type_hint (GTK_WINDOW (dialog),
252 GDK_WINDOW_TYPE_HINT_DIALOG);
254 dialog_set_orientation (dialog, &value);
256 g_value_unset (&value);
258 g_signal_connect (G_OBJECT (dialog), "delete-event",
259 G_CALLBACK (delete_event_callback),
262 gtk_window_set_type_hint (GTK_WINDOW (dialog),
263 GDK_WINDOW_TYPE_HINT_DIALOG);
265 gtk_widget_show_all (dialog->box);
270 psppire_dialog_new (void)
272 PsppireDialog *dialog ;
274 dialog = g_object_new (psppire_dialog_get_type (), NULL);
276 return GTK_WIDGET (dialog) ;
281 notify_change (PsppireDialog *dialog)
283 if ( dialog->contents_are_valid )
285 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
287 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
292 /* Descend the widget tree, connecting appropriate signals to the
293 notify_change callback */
295 connect_notify_signal (GtkWidget *w, gpointer data)
297 PsppireDialog *dialog = data;
299 if ( PSPPIRE_IS_BUTTONBOX (w))
304 if ( GTK_IS_CONTAINER (w))
306 gtk_container_foreach (GTK_CONTAINER (w),
307 connect_notify_signal,
312 /* It's unfortunate that GTK+ doesn't have a generic
313 "user-modified-state-changed" signal. Instead, we have to try and
314 predict what widgets and signals are likely to exist in our dialogs. */
316 if ( GTK_IS_TOGGLE_BUTTON (w))
318 g_signal_connect_swapped (w, "toggled", G_CALLBACK (notify_change),
322 if ( PSPPIRE_IS_SELECTOR (w))
324 g_signal_connect_swapped (w, "selected", G_CALLBACK (notify_change),
327 g_signal_connect_swapped (w, "de-selected", G_CALLBACK (notify_change),
331 if ( GTK_IS_EDITABLE (w))
333 g_signal_connect_swapped (w, "changed", G_CALLBACK (notify_change),
337 if ( GTK_IS_CELL_EDITABLE (w))
339 g_signal_connect_swapped (w, "editing-done", G_CALLBACK (notify_change),
343 if ( GTK_IS_TEXT_VIEW (w))
345 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
347 g_signal_connect_swapped (buffer, "changed", G_CALLBACK (notify_change),
351 if ( GTK_IS_TREE_VIEW (w))
354 GtkTreeView *tv = GTK_TREE_VIEW (w);
355 GtkTreeSelection *selection =
356 gtk_tree_view_get_selection (tv);
357 GtkTreeViewColumn *col;
359 g_signal_connect_swapped (selection, "changed",
360 G_CALLBACK (notify_change), dialog);
362 while ((col = gtk_tree_view_get_column (tv, i++)))
364 GList *renderers = gtk_tree_view_column_get_cell_renderers (col);
365 GList *start = renderers;
368 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
369 g_signal_connect_swapped (renderers->data, "toggled",
370 G_CALLBACK (notify_change), dialog);
371 renderers = renderers->next;
380 psppire_dialog_run (PsppireDialog *dialog)
382 if ( dialog->contents_are_valid != NULL )
383 gtk_container_foreach (GTK_CONTAINER (dialog->box),
384 connect_notify_signal,
387 dialog->loop = g_main_loop_new (NULL, FALSE);
389 gtk_widget_show (GTK_WIDGET (dialog));
391 if ( dialog->contents_are_valid != NULL)
392 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
394 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
396 g_main_loop_run (dialog->loop);
398 return dialog->response;
403 psppire_dialog_reload (PsppireDialog *dialog)
405 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
412 psppire_orientation_get_type (void)
414 static GType etype = 0;
417 static const GEnumValue values[] =
419 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
420 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
424 etype = g_enum_register_static
425 (g_intern_static_string ("PsppireOrientation"), values);
433 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
434 ContentsAreValid contents_are_valid,
437 dialog->contents_are_valid = contents_are_valid;
438 dialog->validity_data = data;