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"
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 g_object_get (dialog, "name", &base, NULL);
308 psppire_conf_save_window_geometry (conf, base, event);
315 on_realize (GtkWindow *dialog, gpointer data)
317 PsppireConf *conf = psppire_conf_new ();
319 const gchar *base = NULL;
321 g_object_get (dialog, "name", &base, NULL);
323 psppire_conf_set_window_geometry (conf, base, dialog);
329 psppire_dialog_init (PsppireDialog *dialog)
333 dialog->contents_are_valid = NULL;
334 dialog->validity_data = NULL;
335 dialog->slidable = FALSE;
337 g_value_init (&value, orientation_spec->value_type);
338 g_param_value_set_default (orientation_spec, &value);
340 gtk_window_set_type_hint (GTK_WINDOW (dialog),
341 GDK_WINDOW_TYPE_HINT_DIALOG);
343 g_value_unset (&value);
345 g_signal_connect (dialog, "delete-event",
346 G_CALLBACK (delete_event_callback),
349 g_signal_connect (dialog, "configure-event",
350 G_CALLBACK (configure_event_callback),
353 g_signal_connect (dialog, "realize",
354 G_CALLBACK (on_realize),
358 gtk_window_set_type_hint (GTK_WINDOW (dialog),
359 GDK_WINDOW_TYPE_HINT_DIALOG);
361 g_object_set (dialog, "icon-name", "psppicon", NULL);
366 psppire_dialog_new (void)
368 PsppireDialog *dialog ;
370 dialog = g_object_new (psppire_dialog_get_type (),
373 return GTK_WIDGET (dialog) ;
378 psppire_dialog_notify_change (PsppireDialog *dialog)
380 if ( dialog->contents_are_valid )
382 gboolean valid = dialog->contents_are_valid (dialog->validity_data);
384 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, valid);
389 /* Descend the widget tree, connecting appropriate signals to the
390 psppire_dialog_notify_change callback */
392 connect_notify_signal (GtkWidget *w, gpointer data)
394 PsppireDialog *dialog = data;
396 if ( PSPPIRE_IS_BUTTONBOX (w))
401 if ( GTK_IS_CONTAINER (w))
403 gtk_container_foreach (GTK_CONTAINER (w),
404 connect_notify_signal,
409 /* It's unfortunate that GTK+ doesn't have a generic
410 "user-modified-state-changed" signal. Instead, we have to try and
411 predict what widgets and signals are likely to exist in our dialogs. */
413 if ( GTK_IS_TOGGLE_BUTTON (w))
415 g_signal_connect_swapped (w, "toggled",
416 G_CALLBACK (psppire_dialog_notify_change),
420 if ( PSPPIRE_IS_SELECTOR (w))
422 g_signal_connect_swapped (w, "selected",
423 G_CALLBACK (psppire_dialog_notify_change),
426 g_signal_connect_swapped (w, "de-selected",
427 G_CALLBACK (psppire_dialog_notify_change),
431 if ( GTK_IS_EDITABLE (w))
433 g_signal_connect_swapped (w, "changed",
434 G_CALLBACK (psppire_dialog_notify_change),
438 if ( GTK_IS_CELL_EDITABLE (w))
440 g_signal_connect_swapped (w, "editing-done",
441 G_CALLBACK (psppire_dialog_notify_change),
445 if ( GTK_IS_TEXT_VIEW (w))
447 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w));
449 g_signal_connect_swapped (buffer, "changed",
450 G_CALLBACK (psppire_dialog_notify_change),
454 if ( GTK_IS_TREE_VIEW (w))
457 GtkTreeView *tv = GTK_TREE_VIEW (w);
458 GtkTreeSelection *selection =
459 gtk_tree_view_get_selection (tv);
460 GtkTreeViewColumn *col;
461 GtkTreeModel *model = gtk_tree_view_get_model (tv);
465 g_signal_connect_swapped (model, "row-changed",
466 G_CALLBACK (psppire_dialog_notify_change),
469 g_signal_connect_swapped (model, "row-deleted",
470 G_CALLBACK (psppire_dialog_notify_change),
473 g_signal_connect_swapped (model, "row-inserted",
474 G_CALLBACK (psppire_dialog_notify_change),
478 g_signal_connect_swapped (selection, "changed",
479 G_CALLBACK (psppire_dialog_notify_change),
482 while ((col = gtk_tree_view_get_column (tv, i++)))
484 GList *renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (col));
485 GList *start = renderers;
488 if ( GTK_IS_CELL_RENDERER_TOGGLE (renderers->data))
489 g_signal_connect_swapped (renderers->data, "toggled",
490 G_CALLBACK (psppire_dialog_notify_change), dialog);
491 renderers = renderers->next;
500 psppire_dialog_run (PsppireDialog *dialog)
502 if ( dialog->contents_are_valid != NULL )
503 gtk_container_foreach (GTK_CONTAINER (dialog->box),
504 connect_notify_signal,
507 dialog->loop = g_main_loop_new (NULL, FALSE);
509 gtk_widget_show (GTK_WIDGET (dialog));
511 if ( dialog->contents_are_valid != NULL)
512 g_signal_emit (dialog, signals [VALIDITY_CHANGED], 0, FALSE);
514 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
516 gdk_threads_leave ();
517 g_main_loop_run (dialog->loop);
518 gdk_threads_enter ();
520 g_main_loop_unref (dialog->loop);
522 return dialog->response;
527 psppire_dialog_reload (PsppireDialog *dialog)
529 g_signal_emit (dialog, signals [DIALOG_REFRESH], 0);
536 psppire_orientation_get_type (void)
538 static GType etype = 0;
541 static const GEnumValue values[] =
543 { PSPPIRE_HORIZONTAL, "PSPPIRE_HORIZONTAL", "Horizontal" },
544 { PSPPIRE_VERTICAL, "PSPPIRE_VERTICAL", "Vertical" },
545 { PSPPIRE_TABULAR, "PSPPIRE_TABULAR", "Tabular" },
549 etype = g_enum_register_static
550 (g_intern_static_string ("PsppireOrientation"), values);
558 psppire_dialog_set_valid_predicate (PsppireDialog *dialog,
559 ContentsAreValid contents_are_valid,
562 dialog->contents_are_valid = contents_are_valid;
563 dialog->validity_data = data;
571 get_internal_child (GtkBuildable *buildable,
573 const gchar *childname)
575 PsppireDialog *dialog = PSPPIRE_DIALOG (buildable);
577 if ( 0 == strcmp (childname, "hbox"))
578 return G_OBJECT (dialog->box);
586 psppire_dialog_buildable_init (GtkBuildableIface *iface)
588 iface->get_internal_child = get_internal_child;