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);
109 else if ( GTK_IS_TABLE (dialog->box))
110 g_value_set_enum (value, PSPPIRE_TABULAR);
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 dialog_set_orientation (PsppireDialog *dialog, const GValue *orval)
123 PsppireOrientation orientation = g_value_get_enum (orval);
125 if ( dialog->box != NULL)
127 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
130 switch ( orientation )
132 case PSPPIRE_HORIZONTAL:
133 dialog->box = gtk_hbox_new (FALSE, 5);
135 case PSPPIRE_VERTICAL:
136 dialog->box = gtk_vbox_new (FALSE, 5);
138 case PSPPIRE_TABULAR:
139 dialog->box = gtk_table_new (2, 3, FALSE);
140 g_object_set (dialog->box,
147 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
152 psppire_dialog_set_property (GObject *object,
158 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
162 case PROP_ORIENTATION:
163 dialog_set_orientation (dialog, value);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 static GParamSpec *orientation_spec ;
175 psppire_dialog_class_init (PsppireDialogClass *class)
177 GObjectClass *object_class = (GObjectClass *) class;
181 g_param_spec_enum ("orientation",
183 "Which way widgets are packed",
184 G_TYPE_PSPPIRE_ORIENTATION,
185 PSPPIRE_HORIZONTAL /* default value */,
186 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
189 object_class->set_property = psppire_dialog_set_property;
190 object_class->get_property = psppire_dialog_get_property;
192 g_object_class_install_property (object_class,
198 signals [DIALOG_REFRESH] =
199 g_signal_new ("refresh",
200 G_TYPE_FROM_CLASS (class),
204 g_cclosure_marshal_VOID__VOID,
209 signals [VALIDITY_CHANGED] =
210 g_signal_new ("validity-changed",
211 G_TYPE_FROM_CLASS (class),
215 g_cclosure_marshal_VOID__BOOLEAN,
221 object_class->finalize = psppire_dialog_finalize;
228 close_dialog (GtkWidget *w, gpointer data)
230 PsppireDialog *dialog = data;
232 psppire_dialog_close (dialog);
236 psppire_dialog_close (PsppireDialog *dialog)
238 g_main_loop_quit (dialog->loop);
239 gtk_widget_hide (GTK_WIDGET (dialog));
244 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
246 close_dialog (w, data);
251 psppire_dialog_init (PsppireDialog *dialog)
255 dialog->contents_are_valid = NULL;
256 dialog->validity_data = NULL;
258 g_value_init (&value, orientation_spec->value_type);
259 g_param_value_set_default (orientation_spec, &value);
261 gtk_window_set_type_hint (GTK_WINDOW (dialog),
262 GDK_WINDOW_TYPE_HINT_DIALOG);
264 dialog_set_orientation (dialog, &value);
266 g_value_unset (&value);
268 g_signal_connect (G_OBJECT (dialog), "delete-event",
269 G_CALLBACK (delete_event_callback),
272 gtk_window_set_type_hint (GTK_WINDOW (dialog),
273 GDK_WINDOW_TYPE_HINT_DIALOG);
275 gtk_widget_show_all (dialog->box);
280 psppire_dialog_new (void)
282 PsppireDialog *dialog ;
284 dialog = g_object_new (psppire_dialog_get_type (), NULL);
286 return GTK_WIDGET (dialog) ;
291 psppire_dialog_notify_change (PsppireDialog *dialog)
293 if ( dialog->contents_are_valid )
295 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
297 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
302 /* Descend the widget tree, connecting appropriate signals to the
303 psppire_dialog_notify_change callback */
305 connect_notify_signal (GtkWidget *w, gpointer data)
307 PsppireDialog *dialog = data;
309 if ( PSPPIRE_IS_BUTTONBOX (w))
314 if ( GTK_IS_CONTAINER (w))
316 gtk_container_foreach (GTK_CONTAINER (w),
317 connect_notify_signal,
322 /* It's unfortunate that GTK+ doesn't have a generic
323 "user-modified-state-changed" signal. Instead, we have to try and
324 predict what widgets and signals are likely to exist in our dialogs. */
326 if ( GTK_IS_TOGGLE_BUTTON (w))
328 g_signal_connect_swapped (w, "toggled",
329 G_CALLBACK (psppire_dialog_notify_change),
333 if ( PSPPIRE_IS_SELECTOR (w))
335 g_signal_connect_swapped (w, "selected",
336 G_CALLBACK (psppire_dialog_notify_change),
339 g_signal_connect_swapped (w, "de-selected",
340 G_CALLBACK (psppire_dialog_notify_change),
344 if ( GTK_IS_EDITABLE (w))
346 g_signal_connect_swapped (w, "changed",
347 G_CALLBACK (psppire_dialog_notify_change),
351 if ( GTK_IS_CELL_EDITABLE (w))
353 g_signal_connect_swapped (w, "editing-done",
354 G_CALLBACK (psppire_dialog_notify_change),
358 if ( GTK_IS_TEXT_VIEW (w))
360 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
362 g_signal_connect_swapped (buffer, "changed",
363 G_CALLBACK (psppire_dialog_notify_change),
367 if ( GTK_IS_TREE_VIEW (w))
370 GtkTreeView *tv = GTK_TREE_VIEW (w);
371 GtkTreeSelection *selection =
372 gtk_tree_view_get_selection (tv);
373 GtkTreeViewColumn *col;
374 GtkTreeModel *model = gtk_tree_view_get_model (tv);
376 g_signal_connect_swapped (model, "row-changed",
377 G_CALLBACK (psppire_dialog_notify_change),
380 g_signal_connect_swapped (model, "row-deleted",
381 G_CALLBACK (psppire_dialog_notify_change),
384 g_signal_connect_swapped (model, "row-inserted",
385 G_CALLBACK (psppire_dialog_notify_change),
388 g_signal_connect_swapped (selection, "changed",
389 G_CALLBACK (psppire_dialog_notify_change),
392 while ((col = gtk_tree_view_get_column (tv, i++)))
394 GList *renderers = gtk_tree_view_column_get_cell_renderers (col);
395 GList *start = renderers;
398 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
399 g_signal_connect_swapped (renderers->data, "toggled",
400 G_CALLBACK (psppire_dialog_notify_change), dialog);
401 renderers = renderers->next;
410 psppire_dialog_run (PsppireDialog *dialog)
412 if ( dialog->contents_are_valid != NULL )
413 gtk_container_foreach (GTK_CONTAINER (dialog->box),
414 connect_notify_signal,
417 dialog->loop = g_main_loop_new (NULL, FALSE);
419 gtk_widget_show (GTK_WIDGET (dialog));
421 if ( dialog->contents_are_valid != NULL)
422 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
424 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
426 g_main_loop_run (dialog->loop);
428 return dialog->response;
433 psppire_dialog_reload (PsppireDialog *dialog)
435 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
442 psppire_orientation_get_type (void)
444 static GType etype = 0;
447 static const GEnumValue values[] =
449 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
450 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
451 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
455 etype = g_enum_register_static
456 (g_intern_static_string ("PsppireOrientation"), values);
464 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
465 ContentsAreValid contents_are_valid,
468 dialog->contents_are_valid = contents_are_valid;
469 dialog->validity_data = data;