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/>. */
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;
117 psppire_selector_finalize (GObject *object)
129 static void on_activate (PsppireSelector *selector, gpointer data);
133 psppire_selector_set_property (GObject *object,
138 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
142 case PROP_ORIENTATION:
143 selector->orientation = g_value_get_enum (value);
144 set_direction (selector, selector->direction);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 psppire_selector_get_property (GObject *object,
159 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
163 case PROP_ORIENTATION:
164 g_value_set_enum (value, selector->orientation);
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 psppire_selector_class_init (PsppireSelectorClass *class)
177 GObjectClass *object_class = G_OBJECT_CLASS (class);
178 GParamSpec *orientation_spec =
179 g_param_spec_enum ("orientation",
181 "Where the selector is relative to its subjects",
182 PSPPIRE_TYPE_SELECTOR_ORIENTATION,
183 PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
184 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
187 object_class->set_property = psppire_selector_set_property;
188 object_class->get_property = psppire_selector_get_property;
190 g_object_class_install_property (object_class,
195 g_signal_new ("selected",
196 G_TYPE_FROM_CLASS (class),
200 g_cclosure_marshal_VOID__VOID,
204 signals [DE_SELECTED] =
205 g_signal_new ("de-selected",
206 G_TYPE_FROM_CLASS (class),
210 g_cclosure_marshal_VOID__VOID,
217 psppire_selector_base_init (PsppireSelectorClass *class)
219 GObjectClass *object_class = G_OBJECT_CLASS (class);
221 object_class->finalize = psppire_selector_finalize;
223 class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
229 psppire_selector_base_finalize(PsppireSelectorClass *class,
232 g_hash_table_destroy (class->source_hash);
238 psppire_selector_init (PsppireSelector *selector)
240 selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
241 selector->filtered_source = NULL;
243 selector->action = gtk_action_new ("select", NULL, NULL, "pspp-stock-select");
245 gtk_action_connect_proxy (selector->action, GTK_WIDGET (selector));
247 gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
249 gtk_widget_show (selector->arrow);
251 g_signal_connect_swapped (selector->action, "activate", G_CALLBACK (on_activate), selector);
253 selector->selecting = FALSE;
258 psppire_selector_new (void)
260 return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
265 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
267 selector->direction = d;
269 /* FIXME: Need to reverse the arrow direction if an RTL locale is in
271 if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
273 switch (selector->orientation)
275 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
276 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
278 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
279 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
281 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
282 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
284 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
285 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
288 g_assert_not_reached ();
294 switch (selector->orientation)
296 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
297 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
299 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
300 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
302 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
303 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
305 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
306 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
309 g_assert_not_reached ();
316 /* Callback for when the source selection changes */
318 on_source_select (GtkTreeSelection *treeselection, gpointer data)
320 PsppireSelector *selector = data;
322 set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
324 if ( selector->allow_selection )
326 gtk_action_set_sensitive (selector->action,
327 selector->allow_selection (selector->source, selector->dest));
329 else if ( GTK_IS_ENTRY (selector->dest) )
331 gtk_action_set_sensitive (selector->action,
332 gtk_tree_selection_count_selected_rows
333 (treeselection) <= 1 );
337 /* Callback for when the destination treeview selection changes */
339 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
341 PsppireSelector *selector = data;
343 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
345 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
348 /* Callback for source deselection, when the dest is GtkEntry */
350 de_select_selection_entry (PsppireSelector *selector)
352 gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
355 /* Callback for source deselection, when the dest is GtkTreeView */
357 de_select_selection_tree_view (PsppireSelector *selector)
361 GtkTreeSelection* selection =
362 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
364 GtkTreeModel *model =
365 gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
367 GList *selected_rows =
368 gtk_tree_selection_get_selected_rows (selection, NULL);
370 g_return_if_fail (selector->select_items);
372 /* Convert paths to RowRefs */
373 for (item = g_list_first (selected_rows);
375 item = g_list_next (item))
377 GtkTreeRowReference* rowref;
378 GtkTreePath *path = item->data;
380 rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
382 item->data = rowref ;
383 gtk_tree_path_free (path);
386 /* Remove each selected row from the dest widget */
387 for (item = g_list_first (selected_rows);
389 item = g_list_next (item))
392 GtkTreeRowReference *rr = item->data;
394 GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
396 gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
398 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
400 gtk_tree_path_free (path);
403 /* Delete list of RowRefs and its contents */
404 g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
405 g_list_free (selected_rows);
409 /* Removes something from the DEST widget */
411 de_select_selection (PsppireSelector *selector)
413 selector->selecting = TRUE;
415 if ( GTK_IS_TREE_VIEW (selector->dest ) )
416 de_select_selection_tree_view (selector);
418 else if ( GTK_IS_ENTRY (selector->dest))
419 de_select_selection_entry (selector);
422 g_assert_not_reached ();
424 selector->selecting = FALSE;
426 gtk_tree_model_filter_refilter (selector->filtered_source);
428 g_signal_emit (selector, signals [DE_SELECTED], 0);
432 /* Puts something into the DEST widget */
434 select_selection (PsppireSelector *selector)
437 GtkTreeSelection* selection =
438 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
440 GList *selected_rows =
441 gtk_tree_selection_get_selected_rows (selection, NULL);
443 GtkTreeModel *childmodel = gtk_tree_model_filter_get_model
444 (selector->filtered_source);
446 g_return_if_fail (selector->select_items);
448 selector->selecting = TRUE;
450 for (item = g_list_first (selected_rows);
452 item = g_list_next (item))
454 GtkTreeIter child_iter;
456 GtkTreePath *path = item->data;
458 gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
461 gtk_tree_model_filter_convert_iter_to_child_iter
462 (selector->filtered_source,
466 selector->select_items (child_iter,
469 selector->select_user_data
473 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
474 g_list_free (selected_rows);
476 gtk_tree_model_filter_refilter (selector->filtered_source);
478 g_signal_emit (selector, signals [SELECTED], 0);
480 selector->selecting = FALSE;
483 /* Callback for when the source treeview is activated (double clicked) */
485 on_row_activate (GtkTreeView *tree_view,
487 GtkTreeViewColumn *column,
490 PsppireSelector *selector = data;
492 gtk_action_activate (selector->action);
495 /* Callback for when the selector button is clicked,
496 or other event which causes the selector's action to occur.
499 on_activate (PsppireSelector *selector, gpointer data)
501 switch (selector->direction)
503 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
504 select_selection (selector);
506 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
507 de_select_selection (selector);
510 g_assert_not_reached ();
515 /* Default visibility filter for GtkTreeView DEST widget */
517 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
518 PsppireSelector *selector)
520 GtkTreeModel *dest_model;
521 GtkTreeIter dest_iter;
522 GtkTreeIter source_iter;
525 GtkTreeModel *source_model;
527 if ( GTK_IS_TREE_MODEL_FILTER (model) )
529 source_model = gtk_tree_model_filter_get_model
530 (GTK_TREE_MODEL_FILTER (model));
532 gtk_tree_model_filter_convert_iter_to_child_iter
533 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
537 source_model = model;
541 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
543 path = gtk_tree_model_get_path (source_model, &source_iter);
545 index = *gtk_tree_path_get_indices (path);
547 gtk_tree_path_free (path);
549 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
556 GValue int_value = {0};
557 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
559 g_value_init (&int_value, G_TYPE_INT);
561 g_value_transform (&value, &int_value);
563 x = g_value_get_int (&int_value);
565 g_value_unset (&int_value);
566 g_value_unset (&value);
571 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
576 /* Visibility function for items in the SOURCE widget.
577 Returns TRUE iff *all* the selectors for which SOURCE is associated
580 is_source_item_visible (GtkTreeModel *childmodel,
581 GtkTreeIter *iter, gpointer data)
583 PsppireSelector *selector = data;
584 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
588 list = g_hash_table_lookup (class->source_hash, selector->source);
592 PsppireSelector *selector = list->data;
594 if ( selector->filter && selector->filter (childmodel, iter, selector))
604 /* set the source widget to SOURCE */
606 set_tree_view_source (PsppireSelector *selector,
609 GtkTreeSelection* selection ;
612 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
614 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
616 selector->filtered_source =
617 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
618 (gtk_tree_view_get_model (source), NULL));
620 gtk_tree_view_set_model (source, NULL);
622 gtk_tree_view_set_model (source,
623 GTK_TREE_MODEL (selector->filtered_source));
625 list = g_list_append (list, selector);
626 g_hash_table_insert (class->source_hash, source, list);
629 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
630 is_source_item_visible,
635 { /* Append this selector to the list and push the <source,list>
636 pair onto the hash table */
638 selector->filtered_source = GTK_TREE_MODEL_FILTER (
639 gtk_tree_view_get_model (source));
641 list = g_list_append (list, selector);
642 g_hash_table_replace (class->source_hash, source, list);
645 selection = gtk_tree_view_get_selection (source);
647 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
650 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
656 Callback for when the destination treeview's data changes
659 on_dest_data_change (GtkTreeModel *tree_model,
664 PsppireSelector *selector = user_data;
666 if ( selector->selecting) return;
668 gtk_tree_model_filter_refilter (selector->filtered_source);
673 on_dest_data_delete (GtkTreeModel *tree_model,
677 PsppireSelector *selector = user_data;
679 if ( selector->selecting ) return;
681 gtk_tree_model_filter_refilter (selector->filtered_source);
687 /* Set the destination widget to DEST */
689 set_tree_view_dest (PsppireSelector *selector,
692 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
693 GtkTreeModel *model = gtk_tree_view_get_model (dest);
695 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
697 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
700 g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
703 g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
708 /* Callback which causes the filter to be refiltered.
709 Called when the DEST GtkEntry is activated (Enter is pressed), or when it
713 refilter (PsppireSelector *selector)
715 gtk_tree_model_filter_refilter (selector->filtered_source);
719 /* Callback for when the DEST GtkEntry is selected (clicked) */
721 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
723 PsppireSelector * selector = data;
725 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
726 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
733 /* Callback for when an item disappears from the source list.
734 By implication, this means that the item has been inserted into the
738 on_row_deleted (PsppireSelector *selector)
740 g_signal_emit (selector, signals [SELECTED], 0);
743 /* Callback for when a new item appears in the source list.
744 By implication, this means that an item has been deleted from the
748 on_row_inserted (PsppireSelector *selector)
750 g_signal_emit (selector, signals [DE_SELECTED], 0);
755 /* Set DEST to be the destination GtkEntry widget */
757 set_entry_dest (PsppireSelector *selector,
760 g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
763 g_signal_connect_swapped (dest, "changed", G_CALLBACK (refilter),
766 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
769 g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
773 g_signal_connect_swapped (selector->filtered_source, "row-deleted",
774 G_CALLBACK (on_row_deleted), selector);
776 g_signal_connect_swapped (selector->filtered_source, "row-inserted",
777 G_CALLBACK (on_row_inserted), selector);
781 /* Set SOURCE and DEST for this selector, and
782 set SELECT_FUNC and FILTER_FUNC */
784 psppire_selector_set_subjects (PsppireSelector *selector,
787 SelectItemsFunc *select_func,
788 FilterItemsFunc *filter_func,
793 selector->filter = filter_func ;
795 selector->source = source;
796 selector->dest = dest;
797 selector->select_user_data = user_data;
799 if ( filter_func == NULL)
801 if (GTK_IS_TREE_VIEW (dest))
802 selector->filter = is_item_in_dest;
805 if ( GTK_IS_TREE_VIEW (source))
806 set_tree_view_source (selector, GTK_TREE_VIEW (source) );
808 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
810 g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
814 else if ( GTK_IS_TREE_VIEW (dest))
815 set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
817 else if ( GTK_IS_ENTRY (dest))
818 set_entry_dest (selector, GTK_ENTRY (dest));
820 else if (GTK_IS_TEXT_VIEW (dest))
822 /* Nothing to be done */
826 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
828 selector->select_items = select_func;
834 psppire_selector_set_allow (PsppireSelector *selector , AllowSelectionFunc *allow)
836 selector->allow_selection = allow;
842 psppire_selector_orientation_get_type (void)
844 static GType etype = 0;
846 static const GEnumValue values[] = {
847 { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
848 { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
849 { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
850 { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
853 etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);