1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011 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 "psppire-dialog.h"
22 #include "psppire-buttonbox.h"
23 #include "psppire-selector.h"
24 #include "psppire-conf.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,
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);
111 psppire_dialog_get_property (GObject *object,
116 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
120 case PROP_ORIENTATION:
122 if ( GTK_IS_VBOX (dialog->box) || GTK_VPANED (dialog->box))
123 g_value_set_enum (value, PSPPIRE_VERTICAL);
124 else if ( GTK_IS_HBOX (dialog->box) || GTK_HPANED (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_value_set_boolean (value, dialog->slidable);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 dialog_set_container (PsppireDialog *dialog)
143 if ( dialog->box != NULL)
145 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
148 switch (dialog->orientation)
150 case PSPPIRE_HORIZONTAL:
151 if ( dialog->slidable)
152 dialog->box = gtk_hpaned_new();
154 dialog->box = gtk_hbox_new (FALSE, 5);
156 case PSPPIRE_VERTICAL:
157 if ( dialog->slidable)
158 dialog->box = gtk_vpaned_new();
160 dialog->box = gtk_vbox_new (FALSE, 5);
162 case PSPPIRE_TABULAR:
163 dialog->box = gtk_table_new (2, 3, FALSE);
164 g_object_set (dialog->box,
171 gtk_widget_show_all (dialog->box);
172 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
177 psppire_dialog_set_property (GObject *object,
183 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
188 dialog->slidable = g_value_get_boolean (value);
190 case PROP_ORIENTATION:
191 dialog->orientation = g_value_get_enum (value);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 dialog_set_container (dialog);
202 static GParamSpec *orientation_spec ;
205 psppire_dialog_class_init (PsppireDialogClass *class)
207 GObjectClass *object_class = (GObjectClass *) class;
209 GParamSpec *sliding_spec ;
212 g_param_spec_enum ("orientation",
214 "Which way widgets are packed",
215 PSPPIRE_TYPE_ORIENTATION,
216 PSPPIRE_HORIZONTAL /* default value */,
217 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
220 g_param_spec_boolean ("slidable",
222 "Can the container be sized by the user",
224 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
228 object_class->set_property = psppire_dialog_set_property;
229 object_class->get_property = psppire_dialog_get_property;
231 g_object_class_install_property (object_class,
236 g_object_class_install_property (object_class,
242 signals [DIALOG_REFRESH] =
243 g_signal_new ("refresh",
244 G_TYPE_FROM_CLASS (class),
248 g_cclosure_marshal_VOID__VOID,
253 signals [VALIDITY_CHANGED] =
254 g_signal_new ("validity-changed",
255 G_TYPE_FROM_CLASS (class),
259 g_cclosure_marshal_VOID__BOOLEAN,
265 object_class->finalize = psppire_dialog_finalize;
272 close_dialog (GtkWidget *w, gpointer data)
274 PsppireDialog *dialog = data;
276 psppire_dialog_close (dialog);
280 psppire_dialog_close (PsppireDialog *dialog)
282 g_main_loop_quit (dialog->loop);
283 gtk_widget_hide (GTK_WIDGET (dialog));
288 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
290 close_dialog (w, data);
295 configure_event_callback (GtkDialog *dialog,
296 GdkEvent *event, gpointer data)
300 PsppireConf *conf = psppire_conf_new ();
302 if ( ! gtk_widget_get_mapped (GTK_WIDGET (dialog)))
305 base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
307 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (dialog));
314 on_realize (GtkWindow *dialog, gpointer data)
316 PsppireConf *conf = psppire_conf_new ();
318 const gchar *base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
320 psppire_conf_set_window_geometry (conf, base, dialog);
326 psppire_dialog_init (PsppireDialog *dialog)
330 dialog->contents_are_valid = NULL;
331 dialog->validity_data = NULL;
332 dialog->slidable = FALSE;
334 g_value_init (&value, orientation_spec->value_type);
335 g_param_value_set_default (orientation_spec, &value);
337 gtk_window_set_type_hint (GTK_WINDOW (dialog),
338 GDK_WINDOW_TYPE_HINT_DIALOG);
340 g_value_unset (&value);
342 g_signal_connect (dialog, "delete-event",
343 G_CALLBACK (delete_event_callback),
346 g_signal_connect (dialog, "configure-event",
347 G_CALLBACK (configure_event_callback),
350 g_signal_connect (dialog, "realize",
351 G_CALLBACK (on_realize),
355 gtk_window_set_type_hint (GTK_WINDOW (dialog),
356 GDK_WINDOW_TYPE_HINT_DIALOG);
358 g_object_set (dialog, "icon-name", "pspp", NULL);
363 psppire_dialog_new (void)
365 PsppireDialog *dialog ;
367 dialog = g_object_new (psppire_dialog_get_type (),
370 return GTK_WIDGET (dialog) ;
375 psppire_dialog_notify_change (PsppireDialog *dialog)
377 if ( dialog->contents_are_valid )
379 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
381 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
386 /* Descend the widget tree, connecting appropriate signals to the
387 psppire_dialog_notify_change callback */
389 connect_notify_signal (GtkWidget *w, gpointer data)
391 PsppireDialog *dialog = data;
393 if ( PSPPIRE_IS_BUTTONBOX (w))
398 if ( GTK_IS_CONTAINER (w))
400 gtk_container_foreach (GTK_CONTAINER (w),
401 connect_notify_signal,
406 /* It's unfortunate that GTK+ doesn't have a generic
407 "user-modified-state-changed" signal. Instead, we have to try and
408 predict what widgets and signals are likely to exist in our dialogs. */
410 if ( GTK_IS_TOGGLE_BUTTON (w))
412 g_signal_connect_swapped (w, "toggled",
413 G_CALLBACK (psppire_dialog_notify_change),
417 if ( PSPPIRE_IS_SELECTOR (w))
419 g_signal_connect_swapped (w, "selected",
420 G_CALLBACK (psppire_dialog_notify_change),
423 g_signal_connect_swapped (w, "de-selected",
424 G_CALLBACK (psppire_dialog_notify_change),
428 if ( GTK_IS_EDITABLE (w))
430 g_signal_connect_swapped (w, "changed",
431 G_CALLBACK (psppire_dialog_notify_change),
435 if ( GTK_IS_CELL_EDITABLE (w))
437 g_signal_connect_swapped (w, "editing-done",
438 G_CALLBACK (psppire_dialog_notify_change),
442 if ( GTK_IS_TEXT_VIEW (w))
444 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
446 g_signal_connect_swapped (buffer, "changed",
447 G_CALLBACK (psppire_dialog_notify_change),
451 if ( GTK_IS_TREE_VIEW (w))
454 GtkTreeView *tv = GTK_TREE_VIEW (w);
455 GtkTreeSelection *selection =
456 gtk_tree_view_get_selection (tv);
457 GtkTreeViewColumn *col;
458 GtkTreeModel *model = gtk_tree_view_get_model (tv);
462 g_signal_connect_swapped (model, "row-changed",
463 G_CALLBACK (psppire_dialog_notify_change),
466 g_signal_connect_swapped (model, "row-deleted",
467 G_CALLBACK (psppire_dialog_notify_change),
470 g_signal_connect_swapped (model, "row-inserted",
471 G_CALLBACK (psppire_dialog_notify_change),
475 g_signal_connect_swapped (selection, "changed",
476 G_CALLBACK (psppire_dialog_notify_change),
479 while ((col = gtk_tree_view_get_column (tv, i++)))
481 GList *renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
482 GList *start = renderers;
485 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
486 g_signal_connect_swapped (renderers->data, "toggled",
487 G_CALLBACK (psppire_dialog_notify_change), dialog);
488 renderers = renderers->next;
497 psppire_dialog_run (PsppireDialog *dialog)
499 if ( dialog->contents_are_valid != NULL )
500 gtk_container_foreach (GTK_CONTAINER (dialog->box),
501 connect_notify_signal,
504 dialog->loop = g_main_loop_new (NULL, FALSE);
506 gtk_widget_show (GTK_WIDGET (dialog));
508 if ( dialog->contents_are_valid != NULL)
509 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
511 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
513 gdk_threads_leave ();
514 g_main_loop_run (dialog->loop);
515 gdk_threads_enter ();
517 g_main_loop_unref (dialog->loop);
519 return dialog->response;
524 psppire_dialog_reload (PsppireDialog *dialog)
526 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
533 psppire_orientation_get_type (void)
535 static GType etype = 0;
538 static const GEnumValue values[] =
540 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
541 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
542 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
546 etype = g_enum_register_static
547 (g_intern_static_string ("PsppireOrientation"), values);
555 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
556 ContentsAreValid contents_are_valid,
559 dialog->contents_are_valid = contents_are_valid;
560 dialog->validity_data = data;
568 get_internal_child (GtkBuildable *buildable,
570 const gchar *childname)
572 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
574 if ( 0 == strcmp (childname, "hbox"))
575 return G_OBJECT (dialog->box);
583 psppire_dialog_buildable_init (GtkBuildableIface *iface)
585 iface->get_internal_child = get_internal_child;