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/gtktextview.h>
72 #include <gtk/gtkwidget.h>
74 static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
75 static void psppire_selector_base_init (PsppireSelectorClass *class);
76 static void psppire_selector_class_init (PsppireSelectorClass *class);
77 static void psppire_selector_init (PsppireSelector *selector);
80 static void set_direction (PsppireSelector *, enum psppire_selector_dir);
83 enum {SELECTED, /* Emitted when an item is inserted into dest */
84 DE_SELECTED, /* Emitted when an item is removed from dest */
87 static guint signals [n_SIGNALS];
91 psppire_selector_get_type (void)
93 static GType psppire_selector_type = 0;
95 if (!psppire_selector_type)
97 static const GTypeInfo psppire_selector_info =
99 sizeof (PsppireSelectorClass),
100 (GBaseInitFunc) psppire_selector_base_init,
101 (GBaseFinalizeFunc) psppire_selector_base_finalize,
102 (GClassInitFunc)psppire_selector_class_init,
103 (GClassFinalizeFunc) NULL,
105 sizeof (PsppireSelector),
107 (GInstanceInitFunc) psppire_selector_init,
110 psppire_selector_type =
111 g_type_register_static (GTK_TYPE_BUTTON, "PsppireSelector",
112 &psppire_selector_info, 0);
115 return psppire_selector_type;
120 psppire_selector_finalize (GObject *object)
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);
237 psppire_selector_init (PsppireSelector *selector)
239 selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
240 selector->filtered_source = NULL;
242 gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
244 gtk_widget_show (selector->arrow);
246 selector->selecting = FALSE;
251 psppire_selector_new (void)
253 return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
258 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
260 selector->direction = d;
262 /* FIXME: Need to reverse the arrow direction if an RTL locale is in
264 if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
266 switch (selector->orientation)
268 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
269 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
271 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
272 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
274 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
275 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
277 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
278 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
281 g_assert_not_reached ();
287 switch (selector->orientation)
289 case PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
290 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
292 case PSPPIRE_SELECT_SOURCE_AFTER_DEST:
293 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
295 case PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
296 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
298 case PSPPIRE_SELECT_SOURCE_BELOW_DEST:
299 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
302 g_assert_not_reached ();
309 /* Callback for when the source selection changes */
311 on_source_select (GtkTreeSelection *treeselection, gpointer data)
313 PsppireSelector *selector = data;
315 set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
317 if ( GTK_IS_ENTRY (selector->dest) )
319 gtk_widget_set_sensitive (GTK_WIDGET (selector),
320 gtk_tree_selection_count_selected_rows
321 (treeselection) <= 1 );
325 /* Callback for when the destination treeview selection changes */
327 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
329 PsppireSelector *selector = data;
331 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
334 /* Callback for source deselection, when the dest is GtkEntry */
336 de_select_selection_entry (PsppireSelector *selector)
338 gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
341 /* Callback for source deselection, when the dest is GtkTreeView */
343 de_select_selection_tree_view (PsppireSelector *selector)
347 GtkTreeSelection* selection =
348 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
350 GtkTreeModel *model =
351 gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
353 GList *selected_rows =
354 gtk_tree_selection_get_selected_rows (selection, NULL);
356 g_return_if_fail (selector->select_items);
358 /* Convert paths to RowRefs */
359 for (item = g_list_first (selected_rows);
361 item = g_list_next (item))
363 GtkTreeRowReference* rowref;
364 GtkTreePath *path = item->data;
366 rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
368 item->data = rowref ;
369 gtk_tree_path_free (path);
372 /* Remove each selected row from the dest widget */
373 for (item = g_list_first (selected_rows);
375 item = g_list_next (item))
378 GtkTreeRowReference *rr = item->data;
380 GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
382 gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
384 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
386 gtk_tree_path_free (path);
389 /* Delete list of RowRefs and its contents */
390 g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
391 g_list_free (selected_rows);
395 /* Removes something from the DEST widget */
397 de_select_selection (PsppireSelector *selector)
399 selector->selecting = TRUE;
401 if ( GTK_IS_TREE_VIEW (selector->dest ) )
402 de_select_selection_tree_view (selector);
404 else if ( GTK_IS_ENTRY (selector->dest))
405 de_select_selection_entry (selector);
408 g_assert_not_reached ();
410 selector->selecting = FALSE;
412 gtk_tree_model_filter_refilter (selector->filtered_source);
414 g_signal_emit (selector, signals [DE_SELECTED], 0);
418 /* Puts something into the DEST widget */
420 select_selection (PsppireSelector *selector)
423 GtkTreeSelection* selection =
424 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
426 GList *selected_rows =
427 gtk_tree_selection_get_selected_rows (selection, NULL);
429 GtkTreeModel *childmodel = gtk_tree_model_filter_get_model
430 (selector->filtered_source);
432 g_return_if_fail (selector->select_items);
434 selector->selecting = TRUE;
436 for (item = g_list_first (selected_rows);
438 item = g_list_next (item))
440 GtkTreeIter child_iter;
442 GtkTreePath *path = item->data;
444 gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
447 gtk_tree_model_filter_convert_iter_to_child_iter
448 (selector->filtered_source,
452 selector->select_items (child_iter,
457 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
458 g_list_free (selected_rows);
460 gtk_tree_model_filter_refilter (selector->filtered_source);
462 g_signal_emit (selector, signals [SELECTED], 0);
464 selector->selecting = FALSE;
467 /* Callback for when the source treeview is activated (double clicked) */
469 on_row_activate (GtkTreeView *tree_view,
471 GtkTreeViewColumn *column,
474 PsppireSelector *selector = data;
476 select_selection (selector);
479 /* Callback for when the selector button is clicked */
481 on_click (PsppireSelector *selector, gpointer data)
483 switch (selector->direction)
485 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
486 select_selection (selector);
488 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
489 de_select_selection (selector);
492 g_assert_not_reached ();
497 /* Default visibility filter for GtkTreeView DEST widget */
499 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
500 PsppireSelector *selector)
502 GtkTreeModel *dest_model;
503 GtkTreeIter dest_iter;
504 GtkTreeIter source_iter;
507 GtkTreeModel *source_model;
509 if ( GTK_IS_TREE_MODEL_FILTER (model) )
511 source_model = gtk_tree_model_filter_get_model
512 (GTK_TREE_MODEL_FILTER (model));
514 gtk_tree_model_filter_convert_iter_to_child_iter
515 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
519 source_model = model;
523 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
525 path = gtk_tree_model_get_path (source_model, &source_iter);
527 index = *gtk_tree_path_get_indices (path);
529 gtk_tree_path_free (path);
531 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
537 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
539 if ( g_value_get_int (&value) == index)
542 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
547 /* Visibility function for items in the SOURCE widget.
548 Returns TRUE iff *all* the selectors for which SOURCE is associated
551 is_source_item_visible (GtkTreeModel *childmodel,
552 GtkTreeIter *iter, gpointer data)
554 PsppireSelector *selector = data;
555 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
559 list = g_hash_table_lookup (class->source_hash, selector->source);
563 PsppireSelector *selector = list->data;
565 if ( selector->filter && selector->filter (childmodel, iter, selector))
575 /* set the source widget to SOURCE */
577 set_tree_view_source (PsppireSelector *selector,
580 GtkTreeSelection* selection ;
583 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
585 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
587 selector->filtered_source =
588 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
589 (gtk_tree_view_get_model (source), NULL));
591 gtk_tree_view_set_model (source, NULL);
593 gtk_tree_view_set_model (source,
594 GTK_TREE_MODEL (selector->filtered_source));
596 list = g_list_append (list, selector);
597 g_hash_table_insert (class->source_hash, source, list);
600 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
601 is_source_item_visible,
606 { /* Append this selector to the list and push the <source,list>
607 pair onto the hash table */
609 selector->filtered_source = GTK_TREE_MODEL_FILTER (
610 gtk_tree_view_get_model (source));
612 list = g_list_append (list, selector);
613 g_hash_table_replace (class->source_hash, source, list);
616 selection = gtk_tree_view_get_selection (source);
618 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
621 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
627 Callback for when the destination treeview's data changes
630 on_dest_data_change (GtkTreeModel *tree_model,
635 PsppireSelector *selector = user_data;
637 if ( selector->selecting) return;
639 gtk_tree_model_filter_refilter (selector->filtered_source);
644 on_dest_data_delete (GtkTreeModel *tree_model,
648 PsppireSelector *selector = user_data;
650 if ( selector->selecting ) return;
652 gtk_tree_model_filter_refilter (selector->filtered_source);
658 /* Set the destination widget to DEST */
660 set_tree_view_dest (PsppireSelector *selector,
663 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
664 GtkTreeModel *model = gtk_tree_view_get_model (dest);
666 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
668 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
671 g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
674 g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
679 /* Callback for when the DEST GtkEntry is activated (Enter is pressed) */
681 on_entry_activate (GtkEntry *w, gpointer data)
683 PsppireSelector * selector = data;
685 gtk_tree_model_filter_refilter (selector->filtered_source);
688 /* Callback for when the DEST GtkEntry is selected (clicked) */
690 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
692 PsppireSelector * selector = data;
694 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
699 /* Set DEST to be the destination GtkEntry widget */
701 set_entry_dest (PsppireSelector *selector,
704 g_signal_connect (dest, "activate", G_CALLBACK (on_entry_activate),
707 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
712 /* Set SOURCE and DEST for this selector, and
713 set SELECT_FUNC and FILTER_FUNC */
715 psppire_selector_set_subjects (PsppireSelector *selector,
718 SelectItemsFunc *select_func,
719 FilterItemsFunc *filter_func )
723 selector->filter = filter_func ;
725 selector->source = source;
726 selector->dest = dest;
728 if ( filter_func == NULL)
730 if (GTK_IS_TREE_VIEW (dest))
731 selector->filter = is_item_in_dest;
734 g_signal_connect (selector, "clicked", G_CALLBACK (on_click), NULL);
736 if ( GTK_IS_TREE_VIEW (source))
737 set_tree_view_source (selector, GTK_TREE_VIEW (source) );
739 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
741 g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
743 if ( GTK_IS_TREE_VIEW (dest))
744 set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
746 else if ( GTK_IS_ENTRY (dest))
747 set_entry_dest (selector, GTK_ENTRY (dest));
749 else if (GTK_IS_TEXT_VIEW (dest))
751 /* Nothing to be done */
755 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
757 selector->select_items = select_func;
763 psppire_selector_orientation_get_type (void)
765 static GType etype = 0;
767 static const GEnumValue values[] = {
768 { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
769 { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
770 { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
771 { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
774 etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);