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 <gtk/gtksignal.h>
22 #include <gtk/gtkbuildable.h>
23 #include "psppire-dialog.h"
24 #include "psppire-buttonbox.h"
25 #include "psppire-selector.h"
26 #include "psppire-conf.h"
29 static void psppire_dialog_class_init (PsppireDialogClass *);
30 static void psppire_dialog_init (PsppireDialog *);
37 static guint signals [n_SIGNALS];
40 static void psppire_dialog_buildable_init (GtkBuildableIface *iface);
44 psppire_dialog_get_type (void)
46 static GType dialog_type = 0;
50 static const GTypeInfo dialog_info =
52 sizeof (PsppireDialogClass),
54 NULL, /* base_finalize */
55 (GClassInitFunc) psppire_dialog_class_init,
56 NULL, /* class_finalize */
57 NULL, /* class_data */
58 sizeof (PsppireDialog),
60 (GInstanceInitFunc) psppire_dialog_init,
63 static const GInterfaceInfo buildable_info =
65 (GInterfaceInitFunc) psppire_dialog_buildable_init,
70 dialog_type = g_type_register_static (GTK_TYPE_WINDOW,
71 "PsppireDialog", &dialog_info, 0);
73 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);
112 psppire_dialog_get_property (GObject *object,
117 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
121 case PROP_ORIENTATION:
123 if ( GTK_IS_VBOX (dialog->box) || GTK_VPANED (dialog->box))
124 g_value_set_enum (value, PSPPIRE_VERTICAL);
125 else if ( GTK_IS_HBOX (dialog->box) || GTK_HPANED (dialog->box))
126 g_value_set_enum (value, PSPPIRE_HORIZONTAL);
127 else if ( GTK_IS_TABLE (dialog->box))
128 g_value_set_enum (value, PSPPIRE_TABULAR);
132 g_value_set_boolean (value, dialog->slidable);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 dialog_set_container (PsppireDialog *dialog)
144 if ( dialog->box != NULL)
146 gtk_container_remove (GTK_CONTAINER (dialog), dialog->box);
149 switch (dialog->orientation)
151 case PSPPIRE_HORIZONTAL:
152 if ( dialog->slidable)
153 dialog->box = gtk_hpaned_new();
155 dialog->box = gtk_hbox_new (FALSE, 5);
157 case PSPPIRE_VERTICAL:
158 if ( dialog->slidable)
159 dialog->box = gtk_vpaned_new();
161 dialog->box = gtk_vbox_new (FALSE, 5);
163 case PSPPIRE_TABULAR:
164 dialog->box = gtk_table_new (2, 3, FALSE);
165 g_object_set (dialog->box,
172 gtk_widget_show_all (dialog->box);
173 gtk_container_add (GTK_CONTAINER (dialog), dialog->box);
178 psppire_dialog_set_property (GObject *object,
184 PsppireDialog *dialog = PSPPIRE_DIALOG (object);
189 dialog->slidable = g_value_get_boolean (value);
191 case PROP_ORIENTATION:
192 dialog->orientation = g_value_get_enum (value);
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 dialog_set_container (dialog);
203 static GParamSpec *orientation_spec ;
206 psppire_dialog_class_init (PsppireDialogClass *class)
208 GObjectClass *object_class = (GObjectClass *) class;
210 GParamSpec *sliding_spec ;
213 g_param_spec_enum ("orientation",
215 "Which way widgets are packed",
216 PSPPIRE_TYPE_ORIENTATION,
217 PSPPIRE_HORIZONTAL /* default value */,
218 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
221 g_param_spec_boolean ("slidable",
223 "Can the container be sized by the user",
225 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
229 object_class->set_property = psppire_dialog_set_property;
230 object_class->get_property = psppire_dialog_get_property;
232 g_object_class_install_property (object_class,
237 g_object_class_install_property (object_class,
243 signals [DIALOG_REFRESH] =
244 g_signal_new ("refresh",
245 G_TYPE_FROM_CLASS (class),
249 g_cclosure_marshal_VOID__VOID,
254 signals [VALIDITY_CHANGED] =
255 g_signal_new ("validity-changed",
256 G_TYPE_FROM_CLASS (class),
260 g_cclosure_marshal_VOID__BOOLEAN,
266 object_class->finalize = psppire_dialog_finalize;
273 close_dialog (GtkWidget *w, gpointer data)
275 PsppireDialog *dialog = data;
277 psppire_dialog_close (dialog);
281 psppire_dialog_close (PsppireDialog *dialog)
283 g_main_loop_quit (dialog->loop);
284 gtk_widget_hide (GTK_WIDGET (dialog));
289 delete_event_callback (GtkWidget *w, GdkEvent *e, gpointer data)
291 close_dialog (w, data);
296 configure_event_callback (GtkDialog *dialog,
297 GdkEvent *event, gpointer data)
301 PsppireConf *conf = psppire_conf_new ();
303 if ( ! GTK_WIDGET_MAPPED (dialog))
306 base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
308 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (dialog));
315 on_realize (GtkWindow *dialog, gpointer data)
317 PsppireConf *conf = psppire_conf_new ();
319 const gchar *base = gtk_buildable_get_name (GTK_BUILDABLE (dialog));
321 psppire_conf_set_window_geometry (conf, base, dialog);
327 psppire_dialog_init (PsppireDialog *dialog)
331 dialog->contents_are_valid = NULL;
332 dialog->validity_data = NULL;
333 dialog->slidable = FALSE;
335 g_value_init (&value, orientation_spec->value_type);
336 g_param_value_set_default (orientation_spec, &value);
338 gtk_window_set_type_hint (GTK_WINDOW (dialog),
339 GDK_WINDOW_TYPE_HINT_DIALOG);
341 g_value_unset (&value);
343 g_signal_connect (dialog, "delete-event",
344 G_CALLBACK (delete_event_callback),
347 g_signal_connect (dialog, "configure-event",
348 G_CALLBACK (configure_event_callback),
351 g_signal_connect (dialog, "realize",
352 G_CALLBACK (on_realize),
356 gtk_window_set_type_hint (GTK_WINDOW (dialog),
357 GDK_WINDOW_TYPE_HINT_DIALOG);
359 g_object_set (dialog, "icon-name", "psppicon", NULL);
364 psppire_dialog_new (void)
366 PsppireDialog *dialog ;
368 dialog = g_object_new (psppire_dialog_get_type (),
371 return GTK_WIDGET (dialog) ;
376 psppire_dialog_notify_change (PsppireDialog *dialog)
378 if ( dialog->contents_are_valid )
380 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
382 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
387 /* Descend the widget tree, connecting appropriate signals to the
388 psppire_dialog_notify_change callback */
390 connect_notify_signal (GtkWidget *w, gpointer data)
392 PsppireDialog *dialog = data;
394 if ( PSPPIRE_IS_BUTTONBOX (w))
399 if ( GTK_IS_CONTAINER (w))
401 gtk_container_foreach (GTK_CONTAINER (w),
402 connect_notify_signal,
407 /* It's unfortunate that GTK+ doesn't have a generic
408 "user-modified-state-changed" signal. Instead, we have to try and
409 predict what widgets and signals are likely to exist in our dialogs. */
411 if ( GTK_IS_TOGGLE_BUTTON (w))
413 g_signal_connect_swapped (w, "toggled",
414 G_CALLBACK (psppire_dialog_notify_change),
418 if ( PSPPIRE_IS_SELECTOR (w))
420 g_signal_connect_swapped (w, "selected",
421 G_CALLBACK (psppire_dialog_notify_change),
424 g_signal_connect_swapped (w, "de-selected",
425 G_CALLBACK (psppire_dialog_notify_change),
429 if ( GTK_IS_EDITABLE (w))
431 g_signal_connect_swapped (w, "changed",
432 G_CALLBACK (psppire_dialog_notify_change),
436 if ( GTK_IS_CELL_EDITABLE (w))
438 g_signal_connect_swapped (w, "editing-done",
439 G_CALLBACK (psppire_dialog_notify_change),
443 if ( GTK_IS_TEXT_VIEW (w))
445 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
447 g_signal_connect_swapped (buffer, "changed",
448 G_CALLBACK (psppire_dialog_notify_change),
452 if ( GTK_IS_TREE_VIEW (w))
455 GtkTreeView *tv = GTK_TREE_VIEW (w);
456 GtkTreeSelection *selection =
457 gtk_tree_view_get_selection (tv);
458 GtkTreeViewColumn *col;
459 GtkTreeModel *model = gtk_tree_view_get_model (tv);
463 g_signal_connect_swapped (model, "row-changed",
464 G_CALLBACK (psppire_dialog_notify_change),
467 g_signal_connect_swapped (model, "row-deleted",
468 G_CALLBACK (psppire_dialog_notify_change),
471 g_signal_connect_swapped (model, "row-inserted",
472 G_CALLBACK (psppire_dialog_notify_change),
476 g_signal_connect_swapped (selection, "changed",
477 G_CALLBACK (psppire_dialog_notify_change),
480 while ((col = gtk_tree_view_get_column (tv, i++)))
482 GList *renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
483 GList *start = renderers;
486 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
487 g_signal_connect_swapped (renderers->data, "toggled",
488 G_CALLBACK (psppire_dialog_notify_change), dialog);
489 renderers = renderers->next;
498 psppire_dialog_run (PsppireDialog *dialog)
500 if ( dialog->contents_are_valid != NULL )
501 gtk_container_foreach (GTK_CONTAINER (dialog->box),
502 connect_notify_signal,
505 dialog->loop = g_main_loop_new (NULL, FALSE);
507 gtk_widget_show (GTK_WIDGET (dialog));
509 if ( dialog->contents_are_valid != NULL)
510 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
512 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
514 gdk_threads_leave ();
515 g_main_loop_run (dialog->loop);
516 gdk_threads_enter ();
518 g_main_loop_unref (dialog->loop);
520 return dialog->response;
525 psppire_dialog_reload (PsppireDialog *dialog)
527 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
534 psppire_orientation_get_type (void)
536 static GType etype = 0;
539 static const GEnumValue values[] =
541 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
542 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
543 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
547 etype = g_enum_register_static
548 (g_intern_static_string ("PsppireOrientation"), values);
556 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
557 ContentsAreValid contents_are_valid,
560 dialog->contents_are_valid = contents_are_valid;
561 dialog->validity_data = data;
569 get_internal_child (GtkBuildable *buildable,
571 const gchar *childname)
573 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
575 if ( 0 == strcmp (childname, "hbox"))
576 return G_OBJECT (dialog->box);
584 psppire_dialog_buildable_init (GtkBuildableIface *iface)
586 iface->get_internal_child = get_internal_child;