1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009 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/>. */
18 This module provides a widget, PsppireSelector derived from
21 It contains a GtkArrow, and is used for selecting objects from a
22 GtkTreeView and putting them into a destination widget (often
23 another GtkTreeView). Typically this is used in psppire for
24 selecting variables, thus:
27 +----------------------------------------------------------+
29 | Source Widget Dest Widget |
30 | +----------------+ +----------------+ |
31 | | Variable0 | | Variable2 | |
46 | +----------------+ +----------------+ |
48 +----------------------------------------------------------+
50 The Source Widget is always a GtkTreeView. The Dest Widget may be a
51 GtkTreeView or a GtkEntry (other destination widgets may be
52 supported in the future).
54 Widgets may be source to more than one PsppireSelector.
60 #include <gtk/gtksignal.h>
61 #include <gtk/gtkbutton.h>
62 #include <gtk/gtkentry.h>
64 #include "psppire-selector.h"
66 #include <gtk/gtktreeview.h>
67 #include <gtk/gtktreeselection.h>
68 #include <gtk/gtktextview.h>
69 #include <gtk/gtkwidget.h>
71 static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
72 static void psppire_selector_base_init (PsppireSelectorClass *class);
73 static void psppire_selector_class_init (PsppireSelectorClass *class);
74 static void psppire_selector_init (PsppireSelector *selector);
77 static void set_direction (PsppireSelector *, enum psppire_selector_dir);
80 enum {SELECTED, /* Emitted when an item is inserted into dest */
81 DE_SELECTED, /* Emitted when an item is removed from dest */
84 static guint signals [n_SIGNALS];
88 psppire_selector_get_type (void)
90 static GType psppire_selector_type = 0;
92 if (!psppire_selector_type)
94 static const GTypeInfo psppire_selector_info =
96 sizeof (PsppireSelectorClass),
97 (GBaseInitFunc) psppire_selector_base_init,
98 (GBaseFinalizeFunc) psppire_selector_base_finalize,
99 (GClassInitFunc)psppire_selector_class_init,
100 (GClassFinalizeFunc) NULL,
102 sizeof (PsppireSelector),
104 (GInstanceInitFunc) psppire_selector_init,
107 psppire_selector_type =
108 g_type_register_static (GTK_TYPE_BUTTON, "PsppireSelector",
109 &psppire_selector_info, 0);
112 return psppire_selector_type;
115 static GObjectClass * parent_class = NULL;
118 psppire_selector_finalize (GObject *obj)
120 /* Chain up to the parent class */
121 G_OBJECT_CLASS (parent_class)->finalize (obj);
126 psppire_selector_dispose (GObject *obj)
128 PsppireSelector *sel = PSPPIRE_SELECTOR (obj);
130 if (sel->dispose_has_run)
133 /* Make sure dispose does not run twice. */
134 sel->dispose_has_run = TRUE;
136 g_object_unref (sel->dest);
137 g_object_unref (sel->source);
139 /* Chain up to the parent class */
140 G_OBJECT_CLASS (parent_class)->dispose (obj);
154 static void on_activate (PsppireSelector *selector, gpointer data);
156 static void update_subjects (PsppireSelector *selector);
160 psppire_selector_set_property (GObject *object,
165 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
169 case PROP_ORIENTATION:
170 selector->orientation = g_value_get_enum (value);
171 set_direction (selector, selector->direction);
173 case PROP_SOURCE_WIDGET:
174 selector->source = g_value_dup_object (value);
175 update_subjects (selector);
177 case PROP_DEST_WIDGET:
178 selector->dest = g_value_dup_object (value);
179 update_subjects (selector);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 psppire_selector_get_property (GObject *object,
194 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
198 case PROP_ORIENTATION:
199 g_value_set_enum (value, selector->orientation);
201 case PROP_SOURCE_WIDGET:
202 g_value_take_object (value, selector->source);
204 case PROP_DEST_WIDGET:
205 g_value_take_object (value, selector->dest);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 psppire_selector_class_init (PsppireSelectorClass *class)
218 GObjectClass *object_class = G_OBJECT_CLASS (class);
219 GParamSpec *orientation_spec =
220 g_param_spec_enum ("orientation",
222 "Where the selector is relative to its subjects",
223 PSPPIRE_TYPE_SELECTOR_ORIENTATION,
224 PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
225 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
227 GParamSpec *source_widget_spec =
228 g_param_spec_object ("source-widget",
230 "The widget to be used as the source for this selector",
234 GParamSpec *dest_widget_spec =
235 g_param_spec_object ("dest-widget",
236 "Destination Widget",
237 "The widget to be used as the destination for this selector",
242 object_class->set_property = psppire_selector_set_property;
243 object_class->get_property = psppire_selector_get_property;
245 g_object_class_install_property (object_class,
249 g_object_class_install_property (object_class,
253 g_object_class_install_property (object_class,
257 parent_class = g_type_class_peek_parent (class);
260 g_signal_new ("selected",
261 G_TYPE_FROM_CLASS (class),
265 g_cclosure_marshal_VOID__VOID,
269 signals [DE_SELECTED] =
270 g_signal_new ("de-selected",
271 G_TYPE_FROM_CLASS (class),
275 g_cclosure_marshal_VOID__VOID,
282 psppire_selector_base_init (PsppireSelectorClass *class)
284 GObjectClass *object_class = G_OBJECT_CLASS (class);
286 object_class->finalize = psppire_selector_finalize;
287 object_class->dispose = psppire_selector_dispose;
289 class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
295 psppire_selector_base_finalize(PsppireSelectorClass *class,
298 g_hash_table_destroy (class->source_hash);
304 psppire_selector_init (PsppireSelector *selector)
306 selector->select_user_data = NULL;
307 selector->select_items = NULL;
308 selector->allow_selection = NULL;
309 selector->filter = NULL;
311 selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
312 selector->filtered_source = NULL;
314 selector->action = gtk_action_new ("select", NULL, NULL, "pspp-stock-select");
316 gtk_action_connect_proxy (selector->action, GTK_WIDGET (selector));
318 gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
320 gtk_widget_show (selector->arrow);
322 g_signal_connect_swapped (selector->action, "activate", G_CALLBACK (on_activate), selector);
324 selector->selecting = FALSE;
326 selector->source = NULL;
327 selector->dest = NULL;
328 selector->dispose_has_run = FALSE;
333 psppire_selector_new (void)
335 return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
340 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
342 selector->direction = d;
344 /* FIXME: Need to reverse the arrow direction if an RTL locale is in
346 if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
348 switch (selector->orientation)
350 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
351 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
353 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
354 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
356 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
357 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
359 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
360 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
363 g_assert_not_reached ();
369 switch (selector->orientation)
371 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
372 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
374 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
375 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
377 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
378 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
380 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
381 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
384 g_assert_not_reached ();
391 /* Callback for when the source selection changes */
393 on_source_select (GtkTreeSelection *treeselection, gpointer data)
395 PsppireSelector *selector = data;
397 set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
399 if ( selector->allow_selection )
401 gtk_action_set_sensitive (selector->action,
402 selector->allow_selection (selector->source, selector->dest));
404 else if ( GTK_IS_ENTRY (selector->dest) )
406 gtk_action_set_sensitive (selector->action,
407 gtk_tree_selection_count_selected_rows
408 (treeselection) <= 1 );
412 /* Callback for when the destination treeview selection changes */
414 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
416 PsppireSelector *selector = data;
418 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
420 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
423 /* Callback for source deselection, when the dest is GtkEntry */
425 de_select_selection_entry (PsppireSelector *selector)
427 gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
430 /* Callback for source deselection, when the dest is GtkTreeView */
432 de_select_selection_tree_view (PsppireSelector *selector)
436 GtkTreeSelection* selection =
437 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
439 GtkTreeModel *model =
440 gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
442 GList *selected_rows =
443 gtk_tree_selection_get_selected_rows (selection, NULL);
445 g_return_if_fail (selector->select_items);
447 /* Convert paths to RowRefs */
448 for (item = g_list_first (selected_rows);
450 item = g_list_next (item))
452 GtkTreeRowReference* rowref;
453 GtkTreePath *path = item->data;
455 rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
457 item->data = rowref ;
458 gtk_tree_path_free (path);
461 /* Remove each selected row from the dest widget */
462 for (item = g_list_first (selected_rows);
464 item = g_list_next (item))
467 GtkTreeRowReference *rr = item->data;
469 GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
471 gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
473 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
475 gtk_tree_path_free (path);
478 /* Delete list of RowRefs and its contents */
479 g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
480 g_list_free (selected_rows);
484 /* Removes something from the DEST widget */
486 de_select_selection (PsppireSelector *selector)
488 selector->selecting = TRUE;
490 if ( GTK_IS_TREE_VIEW (selector->dest ) )
491 de_select_selection_tree_view (selector);
493 else if ( GTK_IS_ENTRY (selector->dest))
494 de_select_selection_entry (selector);
497 g_assert_not_reached ();
499 selector->selecting = FALSE;
501 gtk_tree_model_filter_refilter (selector->filtered_source);
503 g_signal_emit (selector, signals [DE_SELECTED], 0);
507 /* Puts something into the DEST widget */
509 select_selection (PsppireSelector *selector)
512 GtkTreeSelection* selection =
513 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
515 GList *selected_rows =
516 gtk_tree_selection_get_selected_rows (selection, NULL);
518 GtkTreeModel *childmodel = gtk_tree_model_filter_get_model
519 (selector->filtered_source);
521 g_return_if_fail (selector->select_items);
523 selector->selecting = TRUE;
525 for (item = g_list_first (selected_rows);
527 item = g_list_next (item))
529 GtkTreeIter child_iter;
531 GtkTreePath *path = item->data;
533 gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
536 gtk_tree_model_filter_convert_iter_to_child_iter
537 (selector->filtered_source,
541 selector->select_items (child_iter,
544 selector->select_user_data
548 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
549 g_list_free (selected_rows);
551 gtk_tree_model_filter_refilter (selector->filtered_source);
553 g_signal_emit (selector, signals [SELECTED], 0);
555 selector->selecting = FALSE;
558 /* Callback for when the source treeview is activated (double clicked) */
560 on_row_activate (GtkTreeView *tree_view,
562 GtkTreeViewColumn *column,
565 PsppireSelector *selector = data;
567 gtk_action_activate (selector->action);
570 /* Callback for when the selector button is clicked,
571 or other event which causes the selector's action to occur.
574 on_activate (PsppireSelector *selector, gpointer data)
576 switch (selector->direction)
578 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
579 select_selection (selector);
581 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
582 de_select_selection (selector);
585 g_assert_not_reached ();
590 /* Default visibility filter for GtkTreeView DEST widget */
592 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
593 PsppireSelector *selector)
595 GtkTreeModel *dest_model;
596 GtkTreeIter dest_iter;
597 GtkTreeIter source_iter;
600 GtkTreeModel *source_model;
602 if ( GTK_IS_TREE_MODEL_FILTER (model) )
604 source_model = gtk_tree_model_filter_get_model
605 (GTK_TREE_MODEL_FILTER (model));
607 gtk_tree_model_filter_convert_iter_to_child_iter
608 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
612 source_model = model;
616 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
618 path = gtk_tree_model_get_path (source_model, &source_iter);
620 index = *gtk_tree_path_get_indices (path);
622 gtk_tree_path_free (path);
624 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
631 GValue int_value = {0};
632 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
634 g_value_init (&int_value, G_TYPE_INT);
636 g_value_transform (&value, &int_value);
638 x = g_value_get_int (&int_value);
640 g_value_unset (&int_value);
641 g_value_unset (&value);
646 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
651 /* Visibility function for items in the SOURCE widget.
652 Returns TRUE iff *all* the selectors for which SOURCE is associated
655 is_source_item_visible (GtkTreeModel *childmodel,
656 GtkTreeIter *iter, gpointer data)
658 PsppireSelector *selector = data;
659 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
663 list = g_hash_table_lookup (class->source_hash, selector->source);
667 PsppireSelector *selector = list->data;
669 if ( selector->filter && selector->filter (childmodel, iter, selector))
679 /* set the source widget to SOURCE */
681 set_tree_view_source (PsppireSelector *selector,
684 GtkTreeSelection* selection ;
687 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
689 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
691 selector->filtered_source =
692 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
693 (gtk_tree_view_get_model (source), NULL));
695 gtk_tree_view_set_model (source, NULL);
697 gtk_tree_view_set_model (source,
698 GTK_TREE_MODEL (selector->filtered_source));
700 list = g_list_append (list, selector);
701 g_hash_table_insert (class->source_hash, source, list);
704 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
705 is_source_item_visible,
710 { /* Append this selector to the list and push the <source,list>
711 pair onto the hash table */
713 selector->filtered_source = GTK_TREE_MODEL_FILTER (
714 gtk_tree_view_get_model (source));
716 list = g_list_append (list, selector);
717 g_hash_table_replace (class->source_hash, source, list);
720 selection = gtk_tree_view_get_selection (source);
722 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
725 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
731 Callback for when the destination treeview's data changes
734 on_dest_data_change (GtkTreeModel *tree_model,
739 PsppireSelector *selector = user_data;
741 if ( selector->selecting) return;
743 gtk_tree_model_filter_refilter (selector->filtered_source);
748 on_dest_data_delete (GtkTreeModel *tree_model,
752 PsppireSelector *selector = user_data;
754 if ( selector->selecting ) return;
756 gtk_tree_model_filter_refilter (selector->filtered_source);
761 xxx (PsppireSelector *selector)
763 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
765 g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
768 g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
772 /* Set the destination widget to DEST */
774 set_tree_view_dest (PsppireSelector *selector,
777 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
780 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
782 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
786 g_signal_connect_swapped (dest, "notify::model",
787 G_CALLBACK (xxx), selector);
791 /* Callback which causes the filter to be refiltered.
792 Called when the DEST GtkEntry is activated (Enter is pressed), or when it
796 refilter (PsppireSelector *selector)
798 gtk_tree_model_filter_refilter (selector->filtered_source);
802 /* Callback for when the DEST GtkEntry is selected (clicked) */
804 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
806 PsppireSelector * selector = data;
808 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
809 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
816 /* Callback for when an item disappears from the source list.
817 By implication, this means that the item has been inserted into the
821 on_row_deleted (PsppireSelector *selector)
823 g_signal_emit (selector, signals [SELECTED], 0);
826 /* Callback for when a new item appears in the source list.
827 By implication, this means that an item has been deleted from the
831 on_row_inserted (PsppireSelector *selector)
833 g_signal_emit (selector, signals [DE_SELECTED], 0);
838 /* Set DEST to be the destination GtkEntry widget */
840 set_entry_dest (PsppireSelector *selector,
843 g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
846 g_signal_connect_swapped (dest, "changed", G_CALLBACK (refilter),
849 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
852 g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
856 g_signal_connect_swapped (selector->filtered_source, "row-deleted",
857 G_CALLBACK (on_row_deleted), selector);
859 g_signal_connect_swapped (selector->filtered_source, "row-inserted",
860 G_CALLBACK (on_row_inserted), selector);
864 update_subjects (PsppireSelector *selector)
866 GtkTreeModel *model = NULL;
868 if ( NULL == selector->dest )
871 if ( selector->filter == NULL)
873 if (GTK_IS_TREE_VIEW (selector->dest))
874 selector->filter = is_item_in_dest;
877 if ( NULL == selector->source )
880 g_signal_connect_swapped (selector->source, "notify::model",
881 G_CALLBACK (update_subjects), selector);
883 model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
889 if ( GTK_IS_TREE_VIEW (selector->source))
890 set_tree_view_source (selector, GTK_TREE_VIEW (selector->source) );
892 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (selector->source));
894 if ( NULL == selector->dest)
896 else if ( GTK_IS_TREE_VIEW (selector->dest))
898 set_tree_view_dest (selector, GTK_TREE_VIEW (selector->dest));
901 else if ( GTK_IS_ENTRY (selector->dest))
902 set_entry_dest (selector, GTK_ENTRY (selector->dest));
904 else if (GTK_IS_TEXT_VIEW (selector->dest))
906 /* Nothing to be done */
909 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (selector->dest));
914 /* Set FILTER_FUNC for this selector */
916 psppire_selector_set_filter_func (PsppireSelector *selector,
917 FilterItemsFunc *filter_func)
919 selector->filter = filter_func ;
921 if ( selector->filter == NULL)
923 if (GTK_IS_TREE_VIEW (selector->dest))
924 selector->filter = is_item_in_dest;
929 /* Set SELECT_FUNC for this selector */
931 psppire_selector_set_select_func (PsppireSelector *selector,
932 SelectItemsFunc *select_func,
935 selector->select_user_data = user_data;
936 selector->select_items = select_func;
942 psppire_selector_set_allow (PsppireSelector *selector, AllowSelectionFunc *allow)
944 selector->allow_selection = allow;
950 psppire_selector_orientation_get_type (void)
952 static GType etype = 0;
954 static const GEnumValue values[] = {
955 { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
956 { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
957 { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
958 { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
961 etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);