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)
130 psppire_selector_set_property (GObject *object,
135 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
139 case PROP_ORIENTATION:
140 selector->orientation = g_value_get_enum (value);
141 set_direction (selector, selector->direction);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 psppire_selector_get_property (GObject *object,
156 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
160 case PROP_ORIENTATION:
161 g_value_set_enum (value, selector->orientation);
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 psppire_selector_class_init (PsppireSelectorClass *class)
174 GObjectClass *object_class = G_OBJECT_CLASS (class);
175 GParamSpec *orientation_spec =
176 g_param_spec_enum ("orientation",
178 "Where the selector is relative to its subjects",
179 G_TYPE_PSPPIRE_SELECTOR_ORIENTATION,
180 PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
181 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
184 object_class->set_property = psppire_selector_set_property;
185 object_class->get_property = psppire_selector_get_property;
187 g_object_class_install_property (object_class,
192 g_signal_new ("selected",
193 G_TYPE_FROM_CLASS (class),
197 g_cclosure_marshal_VOID__VOID,
201 signals [DE_SELECTED] =
202 g_signal_new ("de-selected",
203 G_TYPE_FROM_CLASS (class),
207 g_cclosure_marshal_VOID__VOID,
214 psppire_selector_base_init (PsppireSelectorClass *class)
216 GObjectClass *object_class = G_OBJECT_CLASS (class);
218 object_class->finalize = psppire_selector_finalize;
220 class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
226 psppire_selector_base_finalize(PsppireSelectorClass *class,
229 g_hash_table_destroy (class->source_hash);
234 psppire_selector_init (PsppireSelector *selector)
236 selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
237 selector->filtered_source = NULL;
239 gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
241 gtk_widget_show (selector->arrow);
243 selector->selecting = FALSE;
248 psppire_selector_new (void)
250 return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
255 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
257 selector->direction = d;
259 /* FIXME: Need to reverse the arrow direction if an RTL locale is in
261 if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
263 switch (selector->orientation)
265 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
266 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
268 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
269 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
271 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
272 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
274 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
275 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
278 g_assert_not_reached ();
284 switch (selector->orientation)
286 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
287 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
289 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
290 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
292 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
293 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
295 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
296 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
299 g_assert_not_reached ();
306 /* Callback for when the source selection changes */
308 on_source_select (GtkTreeSelection *treeselection, gpointer data)
310 PsppireSelector *selector = data;
312 set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
314 if ( GTK_IS_ENTRY (selector->dest) )
316 gtk_widget_set_sensitive (GTK_WIDGET (selector),
317 gtk_tree_selection_count_selected_rows
318 (treeselection) <= 1 );
322 /* Callback for when the destination treeview selection changes */
324 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
326 PsppireSelector *selector = data;
328 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
331 /* Callback for source deselection, when the dest is GtkEntry */
333 de_select_selection_entry (PsppireSelector *selector)
335 gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
338 /* Callback for source deselection, when the dest is GtkTreeView */
340 de_select_selection_tree_view (PsppireSelector *selector)
344 GtkTreeSelection* selection =
345 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
347 GtkTreeModel *model =
348 gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
350 GList *selected_rows =
351 gtk_tree_selection_get_selected_rows (selection, NULL);
353 g_return_if_fail (selector->select_items);
355 /* Convert paths to RowRefs */
356 for (item = g_list_first (selected_rows);
358 item = g_list_next (item))
360 GtkTreeRowReference* rowref;
361 GtkTreePath *path = item->data;
363 rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
365 item->data = rowref ;
366 gtk_tree_path_free (path);
369 /* Remove each selected row from the dest widget */
370 for (item = g_list_first (selected_rows);
372 item = g_list_next (item))
375 GtkTreeRowReference *rr = item->data;
377 GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
379 gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
381 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
383 gtk_tree_path_free (path);
386 /* Delete list of RowRefs and its contents */
387 g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
388 g_list_free (selected_rows);
392 /* Removes something from the DEST widget */
394 de_select_selection (PsppireSelector *selector)
396 selector->selecting = TRUE;
398 if ( GTK_IS_TREE_VIEW (selector->dest ) )
399 de_select_selection_tree_view (selector);
401 else if ( GTK_IS_ENTRY (selector->dest))
402 de_select_selection_entry (selector);
405 g_assert_not_reached ();
407 selector->selecting = FALSE;
409 gtk_tree_model_filter_refilter (selector->filtered_source);
411 g_signal_emit (selector, signals [DE_SELECTED], 0);
415 /* Puts something into the DEST widget */
417 select_selection (PsppireSelector *selector)
420 GtkTreeSelection* selection =
421 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
423 GList *selected_rows =
424 gtk_tree_selection_get_selected_rows (selection, NULL);
426 GtkTreeModel *childmodel = gtk_tree_model_filter_get_model
427 (selector->filtered_source);
429 g_return_if_fail (selector->select_items);
431 selector->selecting = TRUE;
433 for (item = g_list_first (selected_rows);
435 item = g_list_next (item))
437 GtkTreeIter child_iter;
439 GtkTreePath *path = item->data;
441 gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
444 gtk_tree_model_filter_convert_iter_to_child_iter
445 (selector->filtered_source,
449 selector->select_items (child_iter,
454 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
455 g_list_free (selected_rows);
457 gtk_tree_model_filter_refilter (selector->filtered_source);
459 g_signal_emit (selector, signals [SELECTED], 0);
461 selector->selecting = FALSE;
464 /* Callback for when the source treeview is activated (double clicked) */
466 on_row_activate (GtkTreeView *tree_view,
468 GtkTreeViewColumn *column,
471 PsppireSelector *selector = data;
473 select_selection (selector);
476 /* Callback for when the selector button is clicked */
478 on_click (PsppireSelector *selector, gpointer data)
480 switch (selector->direction)
482 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
483 select_selection (selector);
485 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
486 de_select_selection (selector);
489 g_assert_not_reached ();
494 /* Default visibility filter for GtkTreeView DEST widget */
496 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
497 PsppireSelector *selector)
499 GtkTreeModel *dest_model;
500 GtkTreeIter dest_iter;
501 GtkTreeIter source_iter;
504 GtkTreeModel *source_model;
506 if ( GTK_IS_TREE_MODEL_FILTER (model) )
508 source_model = gtk_tree_model_filter_get_model
509 (GTK_TREE_MODEL_FILTER (model));
511 gtk_tree_model_filter_convert_iter_to_child_iter
512 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
516 source_model = model;
520 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
522 path = gtk_tree_model_get_path (source_model, &source_iter);
524 index = *gtk_tree_path_get_indices (path);
526 gtk_tree_path_free (path);
528 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
534 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
536 if ( g_value_get_int (&value) == index)
539 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
544 /* Visibility function for items in the SOURCE widget.
545 Returns TRUE iff *all* the selectors for which SOURCE is associated
548 is_source_item_visible (GtkTreeModel *childmodel,
549 GtkTreeIter *iter, gpointer data)
551 PsppireSelector *selector = data;
552 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
556 list = g_hash_table_lookup (class->source_hash, selector->source);
560 PsppireSelector *selector = list->data;
562 if ( selector->filter && selector->filter (childmodel, iter, selector))
572 /* set the source widget to SOURCE */
574 set_tree_view_source (PsppireSelector *selector,
577 GtkTreeSelection* selection ;
580 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
582 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
584 selector->filtered_source =
585 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
586 (gtk_tree_view_get_model (source), NULL));
588 gtk_tree_view_set_model (source, NULL);
590 gtk_tree_view_set_model (source,
591 GTK_TREE_MODEL (selector->filtered_source));
593 list = g_list_append (list, selector);
594 g_hash_table_insert (class->source_hash, source, list);
597 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
598 is_source_item_visible,
603 { /* Append this selector to the list and push the <source,list>
604 pair onto the hash table */
606 selector->filtered_source = GTK_TREE_MODEL_FILTER (
607 gtk_tree_view_get_model (source));
609 list = g_list_append (list, selector);
610 g_hash_table_replace (class->source_hash, source, list);
613 selection = gtk_tree_view_get_selection (source);
615 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
618 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
624 Callback for when the destination treeview's data changes
627 on_dest_data_change (GtkTreeModel *tree_model,
632 PsppireSelector *selector = user_data;
634 if ( selector->selecting) return;
636 gtk_tree_model_filter_refilter (selector->filtered_source);
641 on_dest_data_delete (GtkTreeModel *tree_model,
645 PsppireSelector *selector = user_data;
647 if ( selector->selecting ) return;
649 gtk_tree_model_filter_refilter (selector->filtered_source);
655 /* Set the destination widget to DEST */
657 set_tree_view_dest (PsppireSelector *selector,
660 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
661 GtkTreeModel *model = gtk_tree_view_get_model (dest);
663 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
665 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
668 g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
671 g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
676 /* Callback which causes the filter to be refiltered.
677 Called when the DEST GtkEntry is activated (Enter is pressed), or when it
681 refilter (PsppireSelector *selector)
683 gtk_tree_model_filter_refilter (selector->filtered_source);
687 /* Callback for when the DEST GtkEntry is selected (clicked) */
689 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
691 PsppireSelector * selector = data;
693 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
700 /* Callback for when an item disappears from the source list.
701 By implication, this means that the item has been inserted into the
705 on_row_deleted (PsppireSelector *selector)
707 g_signal_emit (selector, signals [SELECTED], 0);
710 /* Callback for when a new item appears in the source list.
711 By implication, this means that an item has been deleted from the
715 on_row_inserted (PsppireSelector *selector)
717 g_signal_emit (selector, signals [DE_SELECTED], 0);
722 /* Set DEST to be the destination GtkEntry widget */
724 set_entry_dest (PsppireSelector *selector,
727 g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
730 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
733 g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
737 g_signal_connect_swapped (selector->filtered_source, "row-deleted",
738 G_CALLBACK (on_row_deleted), selector);
740 g_signal_connect_swapped (selector->filtered_source, "row-inserted",
741 G_CALLBACK (on_row_inserted), selector);
745 /* Set SOURCE and DEST for this selector, and
746 set SELECT_FUNC and FILTER_FUNC */
748 psppire_selector_set_subjects (PsppireSelector *selector,
751 SelectItemsFunc *select_func,
752 FilterItemsFunc *filter_func )
756 selector->filter = filter_func ;
758 selector->source = source;
759 selector->dest = dest;
761 if ( filter_func == NULL)
763 if (GTK_IS_TREE_VIEW (dest))
764 selector->filter = is_item_in_dest;
767 g_signal_connect (selector, "clicked", G_CALLBACK (on_click), NULL);
769 if ( GTK_IS_TREE_VIEW (source))
770 set_tree_view_source (selector, GTK_TREE_VIEW (source) );
772 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
774 g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
776 if ( GTK_IS_TREE_VIEW (dest))
777 set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
779 else if ( GTK_IS_ENTRY (dest))
780 set_entry_dest (selector, GTK_ENTRY (dest));
782 else if (GTK_IS_TEXT_VIEW (dest))
784 /* Nothing to be done */
788 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
790 selector->select_items = select_func;
796 psppire_selector_orientation_get_type (void)
798 static GType etype = 0;
800 static const GEnumValue values[] = {
801 { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
802 { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
803 { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
804 { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
807 etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);