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);
78 enum {SELECTED, /* Emitted when an item is inserted into dest */
79 DE_SELECTED, /* Emitted when an item is removed from dest */
82 static guint signals [n_SIGNALS];
86 psppire_selector_get_type (void)
88 static GType psppire_selector_type = 0;
90 if (!psppire_selector_type)
92 static const GTypeInfo psppire_selector_info =
94 sizeof (PsppireSelectorClass),
95 (GBaseInitFunc) psppire_selector_base_init,
96 (GBaseFinalizeFunc) psppire_selector_base_finalize,
97 (GClassInitFunc)psppire_selector_class_init,
98 (GClassFinalizeFunc) NULL,
100 sizeof (PsppireSelector),
102 (GInstanceInitFunc) psppire_selector_init,
105 psppire_selector_type =
106 g_type_register_static (GTK_TYPE_BUTTON, "PsppireSelector",
107 &psppire_selector_info, 0);
110 return psppire_selector_type;
115 psppire_selector_finalize (GObject *object)
122 psppire_selector_class_init (PsppireSelectorClass *class)
125 g_signal_new ("selected",
126 G_TYPE_FROM_CLASS (class),
130 g_cclosure_marshal_VOID__VOID,
134 signals [DE_SELECTED] =
135 g_signal_new ("de-selected",
136 G_TYPE_FROM_CLASS (class),
140 g_cclosure_marshal_VOID__VOID,
149 psppire_selector_base_init (PsppireSelectorClass *class)
151 GObjectClass *object_class = G_OBJECT_CLASS (class);
153 object_class->finalize = psppire_selector_finalize;
155 class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
161 psppire_selector_base_finalize(PsppireSelectorClass *class,
164 g_hash_table_destroy (class->source_hash);
169 psppire_selector_init (PsppireSelector *selector)
171 selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
172 selector->filtered_source = NULL;
174 gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
176 gtk_widget_show (selector->arrow);
178 /* FIXME: This shouldn't be necessary, but Glade interfaces seem to
180 gtk_widget_show (GTK_WIDGET (selector));
185 psppire_selector_new (void)
187 return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
192 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
194 selector->direction = d;
196 /* FIXME: Need to reverse the arrow direction if an RTL locale is in
198 if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
199 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
201 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
204 /* Callback for when the source selection changes */
206 on_source_select (GtkTreeSelection *treeselection, gpointer data)
208 PsppireSelector *selector = data;
210 set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
212 if ( GTK_IS_ENTRY (selector->dest) )
214 gtk_widget_set_sensitive (GTK_WIDGET (selector),
215 gtk_tree_selection_count_selected_rows
216 (treeselection) <= 1 );
220 /* Callback for when the destination treeview selection changes */
222 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
224 PsppireSelector *selector = data;
226 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
229 /* Callback for source deselection, when the dest is GtkEntry */
231 de_select_selection_entry (PsppireSelector *selector)
233 gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
236 /* Callback for source deselection, when the dest is GtkTreeView */
238 de_select_selection_tree_view (PsppireSelector *selector)
242 GtkTreeSelection* selection =
243 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
245 GtkTreeModel *model =
246 gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
248 GList *selected_rows =
249 gtk_tree_selection_get_selected_rows (selection, NULL);
251 g_return_if_fail (selector->select_items);
253 /* Convert paths to RowRefs */
254 for (item = g_list_first (selected_rows);
256 item = g_list_next (item))
258 GtkTreeRowReference* rowref;
259 GtkTreePath *path = item->data;
261 rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
263 item->data = rowref ;
264 gtk_tree_path_free (path);
267 /* Remove each selected row from the dest widget */
268 for (item = g_list_first (selected_rows);
270 item = g_list_next (item))
273 GtkTreeRowReference *rr = item->data;
275 GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
277 gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
279 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
281 gtk_tree_path_free (path);
284 /* Delete list of RowRefs and its contents */
285 g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
286 g_list_free (selected_rows);
290 /* Removes something from the DEST widget */
292 de_select_selection (PsppireSelector *selector)
294 if ( GTK_IS_TREE_VIEW (selector->dest ) )
295 de_select_selection_tree_view (selector);
297 else if ( GTK_IS_ENTRY (selector->dest))
298 de_select_selection_entry (selector);
301 g_assert_not_reached ();
303 gtk_tree_model_filter_refilter (selector->filtered_source);
305 g_signal_emit (selector, signals [DE_SELECTED], 0);
309 /* Puts something into the DEST widget */
311 select_selection (PsppireSelector *selector)
314 GtkTreeSelection* selection =
315 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
317 GList *selected_rows =
318 gtk_tree_selection_get_selected_rows (selection, NULL);
320 GtkTreeModel *childmodel = gtk_tree_model_filter_get_model
321 (selector->filtered_source);
323 g_return_if_fail (selector->select_items);
326 for (item = g_list_first (selected_rows);
328 item = g_list_next (item))
330 GtkTreeIter child_iter;
332 GtkTreePath *path = item->data;
334 gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
337 gtk_tree_model_filter_convert_iter_to_child_iter
338 (selector->filtered_source,
342 selector->select_items (child_iter,
347 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
348 g_list_free (selected_rows);
350 gtk_tree_model_filter_refilter (selector->filtered_source);
352 g_signal_emit (selector, signals [SELECTED], 0);
355 /* Callback fro then the source treeview is activated (double clicked) */
357 on_row_activate (GtkTreeView *tree_view,
359 GtkTreeViewColumn *column,
362 PsppireSelector *selector = data;
364 select_selection (selector);
367 /* Callback for when the selector button is clicked */
369 on_click (PsppireSelector *selector, gpointer data)
371 switch (selector->direction)
373 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
374 select_selection (selector);
376 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
377 de_select_selection (selector);
380 g_assert_not_reached ();
385 /* Default visibility filter for GtkTreeView DEST widget */
387 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
388 PsppireSelector *selector)
390 GtkTreeModel *dest_model;
391 GtkTreeIter dest_iter;
392 GtkTreeIter source_iter;
395 GtkTreeModel *source_model;
397 if ( GTK_IS_TREE_MODEL_FILTER (model) )
399 source_model = gtk_tree_model_filter_get_model
400 (GTK_TREE_MODEL_FILTER (model));
402 gtk_tree_model_filter_convert_iter_to_child_iter
403 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
407 source_model = model;
411 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
413 path = gtk_tree_model_get_path (source_model, &source_iter);
415 index = *gtk_tree_path_get_indices (path);
417 gtk_tree_path_free (path);
419 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
425 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
427 if ( g_value_get_int (&value) == index)
430 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
435 /* Visibility function for items in the SOURCE widget.
436 Returns TRUE iff *all* the selectors for which SOURCE is associated
439 is_source_item_visible (GtkTreeModel *childmodel,
440 GtkTreeIter *iter, gpointer data)
442 PsppireSelector *selector = data;
443 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
447 list = g_hash_table_lookup (class->source_hash, selector->source);
451 PsppireSelector *selector = list->data;
453 if ( selector->filter (childmodel, iter, selector))
463 /* set the source widget to SOURCE */
465 set_tree_view_source (PsppireSelector *selector,
468 GtkTreeSelection* selection ;
471 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
473 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
475 selector->filtered_source =
476 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
477 (gtk_tree_view_get_model (source), NULL));
479 gtk_tree_view_set_model (source, NULL);
481 gtk_tree_view_set_model (source,
482 GTK_TREE_MODEL (selector->filtered_source));
484 list = g_list_append (list, selector);
485 g_hash_table_insert (class->source_hash, source, list);
488 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
489 is_source_item_visible,
494 { /* Append this selector to the list and push the <source,list>
495 pair onto the hash table */
497 selector->filtered_source = GTK_TREE_MODEL_FILTER (
498 gtk_tree_view_get_model (source));
500 list = g_list_append (list, selector);
501 g_hash_table_replace (class->source_hash, source, list);
504 selection = gtk_tree_view_get_selection (source);
506 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
509 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
514 /* Set the destination widget to DEST */
516 set_tree_view_dest (PsppireSelector *selector,
519 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
521 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
523 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
527 /* Callback for when the DEST GtkEntry is activated (Enter is pressed) */
529 on_entry_activate (GtkEntry *w, gpointer data)
531 PsppireSelector * selector = data;
533 gtk_tree_model_filter_refilter (selector->filtered_source);
536 /* Callback for when the DEST GtkEntry is selected (clicked) */
538 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
540 PsppireSelector * selector = data;
542 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
547 /* Set DEST to be the destination GtkEntry widget */
549 set_entry_dest (PsppireSelector *selector,
552 g_signal_connect (dest, "activate", G_CALLBACK (on_entry_activate),
555 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
560 /* Set SOURCE and DEST for this selector, and
561 set SELECT_FUNC and FILTER_FUNC */
563 psppire_selector_set_subjects (PsppireSelector *selector,
566 SelectItemsFunc *select_func,
567 FilterItemsFunc *filter_func )
569 selector->filter = filter_func ;
571 selector->source = source;
572 selector->dest = dest;
574 if ( filter_func == NULL)
576 if (GTK_IS_TREE_VIEW (dest))
577 selector->filter = is_item_in_dest;
580 g_signal_connect (selector, "clicked", G_CALLBACK (on_click), NULL);
582 if ( GTK_IS_TREE_VIEW (source))
583 set_tree_view_source (selector, GTK_TREE_VIEW (source) );
585 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
587 g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
589 if ( GTK_IS_TREE_VIEW (dest))
590 set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
592 else if ( GTK_IS_ENTRY (dest))
593 set_entry_dest (selector, GTK_ENTRY (dest));
596 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
598 selector->select_items = select_func;