2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2007 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 This module provides a widget, PsppireSelector derived from
24 It contains a GtkArrow, and is used for selecting objects from a
25 GtkTreeView and putting them into a destination widget (often
26 another GtkTreeView). Typically this is used in psppire for
27 selecting variables, thus:
30 +----------------------------------------------------------+
32 | Source Widget Dest Widget |
33 | +----------------+ +----------------+ |
34 | | Variable0 | | Variable2 | |
49 | +----------------+ +----------------+ |
51 +----------------------------------------------------------+
53 The Source Widget is always a GtkTreeView. The Dest Widget may be a
54 GtkTreeView or a GtkEntry (other destination widgets may be
55 supported in the future).
57 Widgets may be source to more than one PsppireSelector.
63 #include <gtk/gtksignal.h>
64 #include <gtk/gtkbutton.h>
65 #include <gtk/gtkentry.h>
67 #include "psppire-selector.h"
69 #include <gtk/gtktreeview.h>
70 #include <gtk/gtktreeselection.h>
71 #include <gtk/gtkwidget.h>
73 static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
74 static void psppire_selector_base_init (PsppireSelectorClass *class);
75 static void psppire_selector_class_init (PsppireSelectorClass *class);
76 static void psppire_selector_init (PsppireSelector *selector);
79 static void set_direction (PsppireSelector *, enum psppire_selector_dir);
82 enum {SELECTED, /* Emitted when an item is inserted into dest */
83 DE_SELECTED, /* Emitted when an item is removed from dest */
86 static guint signals [n_SIGNALS];
90 psppire_selector_get_type (void)
92 static GType psppire_selector_type = 0;
94 if (!psppire_selector_type)
96 static const GTypeInfo psppire_selector_info =
98 sizeof (PsppireSelectorClass),
99 (GBaseInitFunc) psppire_selector_base_init,
100 (GBaseFinalizeFunc) psppire_selector_base_finalize,
101 (GClassInitFunc)psppire_selector_class_init,
102 (GClassFinalizeFunc) NULL,
104 sizeof (PsppireSelector),
106 (GInstanceInitFunc) psppire_selector_init,
109 psppire_selector_type =
110 g_type_register_static (GTK_TYPE_BUTTON, "PsppireSelector",
111 &psppire_selector_info, 0);
114 return psppire_selector_type;
119 psppire_selector_finalize (GObject *object)
132 psppire_selector_set_property (GObject *object,
137 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
141 case PROP_ORIENTATION:
142 selector->orientation = g_value_get_enum (value);
143 set_direction (selector, selector->direction);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 psppire_selector_get_property (GObject *object,
158 PsppireSelector *selector = PSPPIRE_SELECTOR (object);
162 case PROP_ORIENTATION:
163 g_value_set_enum (value, selector->orientation);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 psppire_selector_class_init (PsppireSelectorClass *class)
176 GObjectClass *object_class = G_OBJECT_CLASS (class);
177 GParamSpec *orientation_spec =
178 g_param_spec_enum ("orientation",
180 "Where the selector is relative to its subjects",
181 G_TYPE_PSPPIRE_SELECTOR_ORIENTATION,
182 PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
183 G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
186 object_class->set_property = psppire_selector_set_property;
187 object_class->get_property = psppire_selector_get_property;
189 g_object_class_install_property (object_class,
194 g_signal_new ("selected",
195 G_TYPE_FROM_CLASS (class),
199 g_cclosure_marshal_VOID__VOID,
203 signals [DE_SELECTED] =
204 g_signal_new ("de-selected",
205 G_TYPE_FROM_CLASS (class),
209 g_cclosure_marshal_VOID__VOID,
216 psppire_selector_base_init (PsppireSelectorClass *class)
218 GObjectClass *object_class = G_OBJECT_CLASS (class);
220 object_class->finalize = psppire_selector_finalize;
222 class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
228 psppire_selector_base_finalize(PsppireSelectorClass *class,
231 g_hash_table_destroy (class->source_hash);
236 psppire_selector_init (PsppireSelector *selector)
238 selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
239 selector->filtered_source = NULL;
241 gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
243 gtk_widget_show (selector->arrow);
245 /* FIXME: This shouldn't be necessary, but Glade interfaces seem to
247 gtk_widget_show (GTK_WIDGET (selector));
252 psppire_selector_new (void)
254 return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
259 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
261 selector->direction = d;
263 /* FIXME: Need to reverse the arrow direction if an RTL locale is in
265 if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
267 switch (selector->orientation)
269 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
270 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
272 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
273 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
275 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
276 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
278 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
279 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
282 g_assert_not_reached ();
288 switch (selector->orientation)
290 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
291 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
293 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
294 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
296 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
297 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
299 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
300 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
303 g_assert_not_reached ();
310 /* Callback for when the source selection changes */
312 on_source_select (GtkTreeSelection *treeselection, gpointer data)
314 PsppireSelector *selector = data;
316 set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
318 if ( GTK_IS_ENTRY (selector->dest) )
320 gtk_widget_set_sensitive (GTK_WIDGET (selector),
321 gtk_tree_selection_count_selected_rows
322 (treeselection) <= 1 );
326 /* Callback for when the destination treeview selection changes */
328 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
330 PsppireSelector *selector = data;
332 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
335 /* Callback for source deselection, when the dest is GtkEntry */
337 de_select_selection_entry (PsppireSelector *selector)
339 gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
342 /* Callback for source deselection, when the dest is GtkTreeView */
344 de_select_selection_tree_view (PsppireSelector *selector)
348 GtkTreeSelection* selection =
349 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
351 GtkTreeModel *model =
352 gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
354 GList *selected_rows =
355 gtk_tree_selection_get_selected_rows (selection, NULL);
357 g_return_if_fail (selector->select_items);
359 /* Convert paths to RowRefs */
360 for (item = g_list_first (selected_rows);
362 item = g_list_next (item))
364 GtkTreeRowReference* rowref;
365 GtkTreePath *path = item->data;
367 rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
369 item->data = rowref ;
370 gtk_tree_path_free (path);
373 /* Remove each selected row from the dest widget */
374 for (item = g_list_first (selected_rows);
376 item = g_list_next (item))
379 GtkTreeRowReference *rr = item->data;
381 GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
383 gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
385 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
387 gtk_tree_path_free (path);
390 /* Delete list of RowRefs and its contents */
391 g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
392 g_list_free (selected_rows);
396 /* Removes something from the DEST widget */
398 de_select_selection (PsppireSelector *selector)
400 if ( GTK_IS_TREE_VIEW (selector->dest ) )
401 de_select_selection_tree_view (selector);
403 else if ( GTK_IS_ENTRY (selector->dest))
404 de_select_selection_entry (selector);
407 g_assert_not_reached ();
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);
432 for (item = g_list_first (selected_rows);
434 item = g_list_next (item))
436 GtkTreeIter child_iter;
438 GtkTreePath *path = item->data;
440 gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
443 gtk_tree_model_filter_convert_iter_to_child_iter
444 (selector->filtered_source,
448 selector->select_items (child_iter,
453 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
454 g_list_free (selected_rows);
456 gtk_tree_model_filter_refilter (selector->filtered_source);
458 g_signal_emit (selector, signals [SELECTED], 0);
461 /* Callback fro then the source treeview is activated (double clicked) */
463 on_row_activate (GtkTreeView *tree_view,
465 GtkTreeViewColumn *column,
468 PsppireSelector *selector = data;
470 select_selection (selector);
473 /* Callback for when the selector button is clicked */
475 on_click (PsppireSelector *selector, gpointer data)
477 switch (selector->direction)
479 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
480 select_selection (selector);
482 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
483 de_select_selection (selector);
486 g_assert_not_reached ();
491 /* Default visibility filter for GtkTreeView DEST widget */
493 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
494 PsppireSelector *selector)
496 GtkTreeModel *dest_model;
497 GtkTreeIter dest_iter;
498 GtkTreeIter source_iter;
501 GtkTreeModel *source_model;
503 if ( GTK_IS_TREE_MODEL_FILTER (model) )
505 source_model = gtk_tree_model_filter_get_model
506 (GTK_TREE_MODEL_FILTER (model));
508 gtk_tree_model_filter_convert_iter_to_child_iter
509 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
513 source_model = model;
517 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
519 path = gtk_tree_model_get_path (source_model, &source_iter);
521 index = *gtk_tree_path_get_indices (path);
523 gtk_tree_path_free (path);
525 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
531 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
533 if ( g_value_get_int (&value) == index)
536 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
541 /* Visibility function for items in the SOURCE widget.
542 Returns TRUE iff *all* the selectors for which SOURCE is associated
545 is_source_item_visible (GtkTreeModel *childmodel,
546 GtkTreeIter *iter, gpointer data)
548 PsppireSelector *selector = data;
549 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
553 list = g_hash_table_lookup (class->source_hash, selector->source);
557 PsppireSelector *selector = list->data;
559 if ( selector->filter (childmodel, iter, selector))
569 /* set the source widget to SOURCE */
571 set_tree_view_source (PsppireSelector *selector,
574 GtkTreeSelection* selection ;
577 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
579 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
581 selector->filtered_source =
582 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
583 (gtk_tree_view_get_model (source), NULL));
585 gtk_tree_view_set_model (source, NULL);
587 gtk_tree_view_set_model (source,
588 GTK_TREE_MODEL (selector->filtered_source));
590 list = g_list_append (list, selector);
591 g_hash_table_insert (class->source_hash, source, list);
594 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
595 is_source_item_visible,
600 { /* Append this selector to the list and push the <source,list>
601 pair onto the hash table */
603 selector->filtered_source = GTK_TREE_MODEL_FILTER (
604 gtk_tree_view_get_model (source));
606 list = g_list_append (list, selector);
607 g_hash_table_replace (class->source_hash, source, list);
610 selection = gtk_tree_view_get_selection (source);
612 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
615 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
620 /* Set the destination widget to DEST */
622 set_tree_view_dest (PsppireSelector *selector,
625 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
627 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
629 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
633 /* Callback for when the DEST GtkEntry is activated (Enter is pressed) */
635 on_entry_activate (GtkEntry *w, gpointer data)
637 PsppireSelector * selector = data;
639 gtk_tree_model_filter_refilter (selector->filtered_source);
642 /* Callback for when the DEST GtkEntry is selected (clicked) */
644 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
646 PsppireSelector * selector = data;
648 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
653 /* Set DEST to be the destination GtkEntry widget */
655 set_entry_dest (PsppireSelector *selector,
658 g_signal_connect (dest, "activate", G_CALLBACK (on_entry_activate),
661 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
666 /* Set SOURCE and DEST for this selector, and
667 set SELECT_FUNC and FILTER_FUNC */
669 psppire_selector_set_subjects (PsppireSelector *selector,
672 SelectItemsFunc *select_func,
673 FilterItemsFunc *filter_func )
675 selector->filter = filter_func ;
677 selector->source = source;
678 selector->dest = dest;
680 if ( filter_func == NULL)
682 if (GTK_IS_TREE_VIEW (dest))
683 selector->filter = is_item_in_dest;
686 g_signal_connect (selector, "clicked", G_CALLBACK (on_click), NULL);
688 if ( GTK_IS_TREE_VIEW (source))
689 set_tree_view_source (selector, GTK_TREE_VIEW (source) );
691 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
693 g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
695 if ( GTK_IS_TREE_VIEW (dest))
696 set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
698 else if ( GTK_IS_ENTRY (dest))
699 set_entry_dest (selector, GTK_ENTRY (dest));
702 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
704 selector->select_items = select_func;
710 psppire_selector_orientation_get_type (void)
712 static GType etype = 0;
714 static const GEnumValue values[] = {
715 { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
716 { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
717 { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
718 { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
721 etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);