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>
66 #include "psppire-selector.h"
68 #include <gtk/gtktreeview.h>
69 #include <gtk/gtktreeselection.h>
70 #include <gtk/gtkwidget.h>
72 static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
73 static void psppire_selector_base_init (PsppireSelectorClass *class);
74 static void psppire_selector_class_init (PsppireSelectorClass *class);
75 static void psppire_selector_init (PsppireSelector *selector);
77 enum {SELECTED, /* Emitted when an item is inserted into dest */
78 DE_SELECTED, /* Emitted when an item is removed from dest */
81 static guint signals [n_SIGNALS];
85 psppire_selector_get_type (void)
87 static GType psppire_selector_type = 0;
89 if (!psppire_selector_type)
91 static const GTypeInfo psppire_selector_info =
93 sizeof (PsppireSelectorClass),
94 (GBaseInitFunc) psppire_selector_base_init,
95 (GBaseFinalizeFunc) psppire_selector_base_finalize,
96 (GClassInitFunc)psppire_selector_class_init,
97 (GClassFinalizeFunc) NULL,
99 sizeof (PsppireSelector),
101 (GInstanceInitFunc) psppire_selector_init,
104 psppire_selector_type =
105 g_type_register_static (GTK_TYPE_BUTTON, "PsppireSelector",
106 &psppire_selector_info, 0);
109 return psppire_selector_type;
114 psppire_selector_finalize (GObject *object)
121 psppire_selector_class_init (PsppireSelectorClass *class)
124 g_signal_new ("selected",
125 G_TYPE_FROM_CLASS (class),
129 g_cclosure_marshal_VOID__VOID,
133 signals [DE_SELECTED] =
134 g_signal_new ("de-selected",
135 G_TYPE_FROM_CLASS (class),
139 g_cclosure_marshal_VOID__VOID,
148 psppire_selector_base_init (PsppireSelectorClass *class)
150 GObjectClass *object_class = G_OBJECT_CLASS (class);
152 object_class->finalize = psppire_selector_finalize;
154 class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
160 psppire_selector_base_finalize(PsppireSelectorClass *class,
163 g_hash_table_destroy (class->source_hash);
168 psppire_selector_init (PsppireSelector *selector)
170 selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
171 selector->filtered_source = NULL;
173 gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
175 gtk_widget_show (selector->arrow);
177 /* FIXME: This shouldn't be necessary, but Glade interfaces seem to
179 gtk_widget_show (GTK_WIDGET (selector));
184 psppire_selector_new (void)
186 return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
191 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
193 selector->direction = d;
195 /* FIXME: Need to reverse the arrow direction if an RTL locale is in
197 if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
198 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
200 g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
203 /* Callback for when the source selection changes */
205 on_source_select (GtkTreeSelection *treeselection, gpointer data)
207 PsppireSelector *selector = data;
209 set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
211 if ( GTK_IS_ENTRY (selector->dest) )
213 gtk_widget_set_sensitive (GTK_WIDGET (selector),
214 gtk_tree_selection_count_selected_rows
215 (treeselection) <= 1 );
219 /* Callback for when the destination treeview selection changes */
221 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
223 PsppireSelector *selector = data;
225 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
228 /* Callback for source deselection, when the dest is GtkEntry */
230 de_select_selection_entry (PsppireSelector *selector)
232 gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
235 /* Callback for source deselection, when the dest is GtkTreeView */
237 de_select_selection_tree_view (PsppireSelector *selector)
241 GtkTreeSelection* selection =
242 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
244 GtkTreeModel *model =
245 gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
247 GList *selected_rows =
248 gtk_tree_selection_get_selected_rows (selection, NULL);
250 g_return_if_fail (selector->select_items);
252 /* Convert paths to RowRefs */
253 for (item = g_list_first (selected_rows);
255 item = g_list_next (item))
257 GtkTreeRowReference* rowref;
258 GtkTreePath *path = item->data;
260 rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
262 item->data = rowref ;
263 gtk_tree_path_free (path);
266 /* Remove each selected row from the dest widget */
267 for (item = g_list_first (selected_rows);
269 item = g_list_next (item))
272 GtkTreeRowReference *rr = item->data;
274 GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
276 gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
278 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
280 gtk_tree_path_free (path);
283 /* Delete list of RowRefs and its contents */
284 g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
285 g_list_free (selected_rows);
289 /* Removes something from the DEST widget */
291 de_select_selection (PsppireSelector *selector)
293 if ( GTK_IS_TREE_VIEW (selector->dest ) )
294 de_select_selection_tree_view (selector);
296 else if ( GTK_IS_ENTRY (selector->dest))
297 de_select_selection_entry (selector);
300 g_assert_not_reached ();
302 gtk_tree_model_filter_refilter (selector->filtered_source);
304 g_signal_emit (selector, signals [DE_SELECTED], 0);
308 /* Puts something into the DEST widget */
310 select_selection (PsppireSelector *selector)
313 GtkTreeSelection* selection =
314 gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
316 GList *selected_rows =
317 gtk_tree_selection_get_selected_rows (selection, NULL);
319 GtkTreeModel *childmodel = gtk_tree_model_filter_get_model
320 (selector->filtered_source);
322 g_return_if_fail (selector->select_items);
325 for (item = g_list_first (selected_rows);
327 item = g_list_next (item))
329 GtkTreeIter child_iter;
331 GtkTreePath *path = item->data;
333 gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
336 gtk_tree_model_filter_convert_iter_to_child_iter
337 (selector->filtered_source,
341 selector->select_items (child_iter,
346 g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
347 g_list_free (selected_rows);
349 gtk_tree_model_filter_refilter (selector->filtered_source);
351 g_signal_emit (selector, signals [SELECTED], 0);
354 /* Callback fro then the source treeview is activated (double clicked) */
356 on_row_activate (GtkTreeView *tree_view,
358 GtkTreeViewColumn *column,
361 PsppireSelector *selector = data;
363 select_selection (selector);
366 /* Callback for when the selector button is clicked */
368 on_click (PsppireSelector *selector, gpointer data)
370 switch (selector->direction)
372 case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
373 select_selection (selector);
375 case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
376 de_select_selection (selector);
379 g_assert_not_reached ();
384 /* Default visibility filter for GtkTreeView DEST widget */
386 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
387 PsppireSelector *selector)
389 GtkTreeModel *dest_model;
390 GtkTreeIter dest_iter;
391 GtkTreeIter source_iter;
394 GtkTreeModel *source_model;
396 if ( GTK_IS_TREE_MODEL_FILTER (model) )
398 source_model = gtk_tree_model_filter_get_model
399 (GTK_TREE_MODEL_FILTER (model));
401 gtk_tree_model_filter_convert_iter_to_child_iter
402 ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter );
406 source_model = model;
410 dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
412 path = gtk_tree_model_get_path (source_model, &source_iter);
414 index = *gtk_tree_path_get_indices (path);
416 gtk_tree_path_free (path);
418 if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
424 gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
426 if ( g_value_get_int (&value) == index)
429 while (gtk_tree_model_iter_next (dest_model, &dest_iter));
434 /* Visibility function for items in the SOURCE widget.
435 Returns TRUE iff *all* the selectors for which SOURCE is associated
438 is_source_item_visible (GtkTreeModel *childmodel,
439 GtkTreeIter *iter, gpointer data)
441 PsppireSelector *selector = data;
442 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
446 list = g_hash_table_lookup (class->source_hash, selector->source);
450 PsppireSelector *selector = list->data;
452 if ( selector->filter (childmodel, iter, selector))
462 /* set the source widget to SOURCE */
464 set_tree_view_source (PsppireSelector *selector,
467 GtkTreeSelection* selection ;
470 PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
472 if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
474 selector->filtered_source =
475 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
476 (gtk_tree_view_get_model (source), NULL));
478 gtk_tree_view_set_model (source, NULL);
480 gtk_tree_view_set_model (source,
481 GTK_TREE_MODEL (selector->filtered_source));
483 list = g_list_append (list, selector);
484 g_hash_table_insert (class->source_hash, source, list);
487 gtk_tree_model_filter_set_visible_func (selector->filtered_source,
488 is_source_item_visible,
493 { /* Append this selector to the list and push the <source,list>
494 pair onto the hash table */
496 selector->filtered_source = GTK_TREE_MODEL_FILTER (
497 gtk_tree_view_get_model (source));
499 list = g_list_append (list, selector);
500 g_hash_table_replace (class->source_hash, source, list);
503 selection = gtk_tree_view_get_selection (source);
505 g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
508 g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
513 /* Set the destination widget to DEST */
515 set_tree_view_dest (PsppireSelector *selector,
518 GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
520 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
522 g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
526 /* Callback for when the DEST GtkEntry is activated (Enter is pressed) */
528 on_entry_activate (GtkEntry *w, gpointer data)
530 PsppireSelector * selector = data;
532 gtk_tree_model_filter_refilter (selector->filtered_source);
535 /* Callback for when the DEST GtkEntry is selected (clicked) */
537 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
539 PsppireSelector * selector = data;
541 set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
546 /* Set DEST to be the destination GtkEntry widget */
548 set_entry_dest (PsppireSelector *selector,
551 g_signal_connect (dest, "activate", G_CALLBACK (on_entry_activate),
554 g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
559 /* Set SOURCE and DEST for this selector, and
560 set SELECT_FUNC and FILTER_FUNC */
562 psppire_selector_set_subjects (PsppireSelector *selector,
565 SelectItemsFunc *select_func,
566 FilterItemsFunc *filter_func )
568 selector->filter = filter_func ;
570 selector->source = source;
571 selector->dest = dest;
573 if ( filter_func == NULL)
575 if (GTK_IS_TREE_VIEW (dest))
576 selector->filter = is_item_in_dest;
579 g_signal_connect (selector, "clicked", G_CALLBACK (on_click), NULL);
581 if ( GTK_IS_TREE_VIEW (source))
582 set_tree_view_source (selector, GTK_TREE_VIEW (source) );
584 g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
586 g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
588 if ( GTK_IS_TREE_VIEW (dest))
589 set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
591 else if ( GTK_IS_ENTRY (dest))
592 set_entry_dest (selector, GTK_ENTRY (dest));
595 g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
597 selector->select_items = select_func;