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 G_TYPE_PSPPIRE_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,
471 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
472 g_list_free (selected_rows);
474 gtk_tree_model_filter_refilter (selector->filtered_source);
476 g_signal_emit (selector, signals [SELECTED], 0);
478 selector->selecting = FALSE;
481 /* Callback for when the source treeview is activated (double clicked) */
483 on_row_activate (GtkTreeView *tree_view,
485 GtkTreeViewColumn *column,
488 PsppireSelector *selector = data;
490 gtk_action_activate (selector->action);
493 /* Callback for when the selector button is clicked,
494 or other event which causes the selector's action to occur.
497 on_activate (PsppireSelector *selector, gpointer data)
499 switch (selector->direction)
501 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
502 select_selection (selector);
504 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
505 de_select_selection (selector);
508 g_assert_not_reached ();
513 /* Default visibility filter for GtkTreeView DEST widget */
515 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
516 PsppireSelector *selector)
518 GtkTreeModel *dest_model;
519 GtkTreeIter dest_iter;
520 GtkTreeIter source_iter;
523 GtkTreeModel *source_model;
525 if ( GTK_IS_TREE_MODEL_FILTER (model) )
527 source_model = gtk_tree_model_filter_get_model
528 (GTK_TREE_MODEL_FILTER (model));
530 gtk_tree_model_filter_convert_iter_to_child_iter
531 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
535 source_model = model;
539 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
541 path = gtk_tree_model_get_path (source_model, &source_iter);
543 index = *gtk_tree_path_get_indices (path);
545 gtk_tree_path_free (path);
547 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
553 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
555 if ( g_value_get_int (&value) == index)
558 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
563 /* Visibility function for items in the SOURCE widget.
564 Returns TRUE iff *all* the selectors for which SOURCE is associated
567 is_source_item_visible (GtkTreeModel *childmodel,
568 GtkTreeIter *iter, gpointer data)
570 PsppireSelector *selector = data;
571 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
575 list = g_hash_table_lookup (class->source_hash, selector->source);
579 PsppireSelector *selector = list->data;
581 if ( selector->filter && selector->filter (childmodel, iter, selector))
591 /* set the source widget to SOURCE */
593 set_tree_view_source (PsppireSelector *selector,
596 GtkTreeSelection* selection ;
599 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
601 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
603 selector->filtered_source =
604 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
605 (gtk_tree_view_get_model (source), NULL));
607 gtk_tree_view_set_model (source, NULL);
609 gtk_tree_view_set_model (source,
610 GTK_TREE_MODEL (selector->filtered_source));
612 list = g_list_append (list, selector);
613 g_hash_table_insert (class->source_hash, source, list);
616 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
617 is_source_item_visible,
622 { /* Append this selector to the list and push the <source,list>
623 pair onto the hash table */
625 selector->filtered_source = GTK_TREE_MODEL_FILTER (
626 gtk_tree_view_get_model (source));
628 list = g_list_append (list, selector);
629 g_hash_table_replace (class->source_hash, source, list);
632 selection = gtk_tree_view_get_selection (source);
634 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
637 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
643 Callback for when the destination treeview's data changes
646 on_dest_data_change (GtkTreeModel *tree_model,
651 PsppireSelector *selector = user_data;
653 if ( selector->selecting) return;
655 gtk_tree_model_filter_refilter (selector->filtered_source);
660 on_dest_data_delete (GtkTreeModel *tree_model,
664 PsppireSelector *selector = user_data;
666 if ( selector->selecting ) return;
668 gtk_tree_model_filter_refilter (selector->filtered_source);
674 /* Set the destination widget to DEST */
676 set_tree_view_dest (PsppireSelector *selector,
679 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
680 GtkTreeModel *model = gtk_tree_view_get_model (dest);
682 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
684 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
687 g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
690 g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
695 /* Callback which causes the filter to be refiltered.
696 Called when the DEST GtkEntry is activated (Enter is pressed), or when it
700 refilter (PsppireSelector *selector)
702 gtk_tree_model_filter_refilter (selector->filtered_source);
706 /* Callback for when the DEST GtkEntry is selected (clicked) */
708 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
710 PsppireSelector * selector = data;
712 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
713 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
720 /* Callback for when an item disappears from the source list.
721 By implication, this means that the item has been inserted into the
725 on_row_deleted (PsppireSelector *selector)
727 g_signal_emit (selector, signals [SELECTED], 0);
730 /* Callback for when a new item appears in the source list.
731 By implication, this means that an item has been deleted from the
735 on_row_inserted (PsppireSelector *selector)
737 g_signal_emit (selector, signals [DE_SELECTED], 0);
742 /* Set DEST to be the destination GtkEntry widget */
744 set_entry_dest (PsppireSelector *selector,
747 g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
750 g_signal_connect_swapped (dest, "changed", G_CALLBACK (refilter),
753 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
756 g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
760 g_signal_connect_swapped (selector->filtered_source, "row-deleted",
761 G_CALLBACK (on_row_deleted), selector);
763 g_signal_connect_swapped (selector->filtered_source, "row-inserted",
764 G_CALLBACK (on_row_inserted), selector);
768 /* Set SOURCE and DEST for this selector, and
769 set SELECT_FUNC and FILTER_FUNC */
771 psppire_selector_set_subjects (PsppireSelector *selector,
774 SelectItemsFunc *select_func,
775 FilterItemsFunc *filter_func )
779 selector->filter = filter_func ;
781 selector->source = source;
782 selector->dest = dest;
784 if ( filter_func == NULL)
786 if (GTK_IS_TREE_VIEW (dest))
787 selector->filter = is_item_in_dest;
790 if ( GTK_IS_TREE_VIEW (source))
791 set_tree_view_source (selector, GTK_TREE_VIEW (source) );
793 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
795 g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
797 if ( GTK_IS_TREE_VIEW (dest))
798 set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
800 else if ( GTK_IS_ENTRY (dest))
801 set_entry_dest (selector, GTK_ENTRY (dest));
803 else if (GTK_IS_TEXT_VIEW (dest))
805 /* Nothing to be done */
809 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
811 selector->select_items = select_func;
817 psppire_selector_set_allow (PsppireSelector *selector , AllowSelectionFunc *allow)
819 selector->allow_selection = allow;
825 psppire_selector_orientation_get_type (void)
827 static GType etype = 0;
829 static const GEnumValue values[] = {
830 { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
831 { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
832 { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
833 { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
836 etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);