367502b6ea219dca40b1aaeb07bb65a806996de4
[pspp-builds.git] / src / ui / gui / psppire-selector.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2009 Free Software Foundation
3
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.
8
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.
13
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/>.
16 */
17
18 /*
19   This module provides a widget, PsppireSelector derived from
20   GtkButton.
21
22   It contains a GtkArrow, and is used for selecting objects from a
23   GtkTreeView and putting them into a destination widget (often
24   another GtkTreeView).  Typically this is used in psppire for
25   selecting variables, thus:
26
27
28   +----------------------------------------------------------+
29   |                                                          |
30   |     Source Widget                       Dest Widget      |
31   |   +----------------+                 +----------------+  |
32   |   | Variable0      |                 | Variable2      |  |
33   |   | Variable1      |                 |                |  |
34   |   | Variable3      |                 |                |  |
35   |   |                |    Selector     |                |  |
36   |   |                |                 |                |  |
37   |   |                |    +------+     |                |  |
38   |   |                |    | |\   |     |                |  |
39   |   |                |    | | \  |     |                |  |
40   |   |                |    | | /  |     |                |  |
41   |   |                |    | |/   |     |                |  |
42   |   |                |    +------+     |                |  |
43   |   |                |                 |                |  |
44   |   |                |                 |                |  |
45   |   |                |                 |                |  |
46   |   |                |                 |                |  |
47   |   +----------------+                 +----------------+  |
48   |                                                          |
49   +----------------------------------------------------------+
50
51   The Source Widget is always a GtkTreeView.  The Dest Widget may be a
52   GtkTreeView or a GtkEntry (other destination widgets may be
53   supported in the future).
54
55   Widgets may be source to more than one PsppireSelector.
56 */
57
58
59 #include <config.h>
60
61 #include "psppire-dictview.h"
62 #include "psppire-var-view.h"
63
64
65
66 #include <gtk/gtksignal.h>
67 #include <gtk/gtkbutton.h>
68 #include <gtk/gtkentry.h>
69
70 #include "psppire-selector.h"
71
72 #include <gtk/gtktreeview.h>
73 #include <gtk/gtktreeselection.h>
74 #include <gtk/gtktextview.h>
75 #include <gtk/gtkwidget.h>
76
77 static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
78 static void psppire_selector_base_init     (PsppireSelectorClass *class);
79 static void psppire_selector_class_init    (PsppireSelectorClass *class);
80 static void psppire_selector_init          (PsppireSelector      *selector);
81
82
83 static void set_direction (PsppireSelector *, enum psppire_selector_dir);
84
85
86 enum  {SELECTED,    /* Emitted when an item is inserted into dest */
87        DE_SELECTED, /* Emitted when an item is removed from dest */
88        n_SIGNALS};
89
90 static guint signals [n_SIGNALS];
91
92
93 GType
94 psppire_selector_get_type (void)
95 {
96   static GType psppire_selector_type = 0;
97
98   if (!psppire_selector_type)
99     {
100       static const GTypeInfo psppire_selector_info =
101       {
102         sizeof (PsppireSelectorClass),
103         (GBaseInitFunc) psppire_selector_base_init,
104         (GBaseFinalizeFunc) psppire_selector_base_finalize,
105         (GClassInitFunc)psppire_selector_class_init,
106         (GClassFinalizeFunc) NULL,
107         NULL,
108         sizeof (PsppireSelector),
109         0,
110         (GInstanceInitFunc) psppire_selector_init,
111       };
112
113       psppire_selector_type =
114         g_type_register_static (GTK_TYPE_BUTTON, "PsppireSelector",
115                                 &psppire_selector_info, 0);
116     }
117
118   return psppire_selector_type;
119 }
120
121 static GObjectClass * parent_class = NULL;
122
123 static void
124 psppire_selector_finalize (GObject *obj)
125 {
126    /* Chain up to the parent class */
127    G_OBJECT_CLASS (parent_class)->finalize (obj);
128 }
129
130
131 static void
132 psppire_selector_dispose (GObject *obj)
133 {
134   PsppireSelector *sel = PSPPIRE_SELECTOR (obj);
135
136   if (sel->dispose_has_run)
137     return;
138
139   /* Make sure dispose does not run twice. */
140   sel->dispose_has_run = TRUE;
141
142   g_object_unref (sel->dest);
143   g_object_unref (sel->source);
144
145   /* Chain up to the parent class */
146   G_OBJECT_CLASS (parent_class)->dispose (obj);
147 }
148
149
150 /* Properties */
151 enum
152 {
153   PROP_0,
154   PROP_ORIENTATION,
155   PROP_PRIMARY,
156   PROP_SOURCE_WIDGET,
157   PROP_DEST_WIDGET
158 };
159
160
161 static void on_activate (PsppireSelector *selector, gpointer data);
162
163 static void update_subjects (PsppireSelector *selector);
164
165
166 static void
167 psppire_selector_set_property (GObject         *object,
168                                guint            prop_id,
169                                const GValue    *value,
170                                GParamSpec      *pspec)
171 {
172   PsppireSelector *selector = PSPPIRE_SELECTOR (object);
173
174   switch (prop_id)
175     {
176     case PROP_ORIENTATION:
177       selector->orientation = g_value_get_enum (value);
178       set_direction (selector, selector->direction);
179       break;
180     case PROP_PRIMARY:
181       selector->primary_requested = TRUE;
182       update_subjects (selector);
183       break;
184     case PROP_SOURCE_WIDGET:
185       selector->source = g_value_dup_object (value);
186       update_subjects (selector);
187       break;
188     case PROP_DEST_WIDGET:
189       selector->dest = g_value_dup_object (value);
190       update_subjects (selector);
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195     };
196 }
197
198
199 static void
200 psppire_selector_get_property (GObject         *object,
201                                guint            prop_id,
202                                GValue          *value,
203                                GParamSpec      *pspec)
204 {
205   PsppireSelector *selector = PSPPIRE_SELECTOR (object);
206
207   switch (prop_id)
208     {
209     case PROP_ORIENTATION:
210       g_value_set_enum (value, selector->orientation);
211       break;
212     case PROP_SOURCE_WIDGET:
213       g_value_take_object (value, selector->source);
214       break;
215     case PROP_DEST_WIDGET:
216       g_value_take_object (value, selector->dest);
217       break;
218     default:
219       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220       break;
221     };
222 }
223
224
225
226 static void
227 psppire_selector_class_init (PsppireSelectorClass *class)
228 {
229   GObjectClass *object_class = G_OBJECT_CLASS (class);
230   GParamSpec *orientation_spec =
231     g_param_spec_enum ("orientation",
232                        "Orientation",
233                        "Where the selector is relative to its subjects",
234                        PSPPIRE_TYPE_SELECTOR_ORIENTATION,
235                        PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
236                        G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
237
238
239  /* Meaningfull only if more than one selector shares this selectors source */
240   GParamSpec *primary_spec =
241     g_param_spec_boolean ("primary",
242                           "Primary",
243                           "Whether this selector should be the primary selector for the source",
244                           FALSE,
245                           G_PARAM_READWRITE);
246
247   GParamSpec *source_widget_spec = 
248     g_param_spec_object ("source-widget",
249                          "Source Widget",
250                          "The widget to be used as the source for this selector",
251                          GTK_TYPE_WIDGET,
252                          G_PARAM_READWRITE);
253
254   GParamSpec *dest_widget_spec = 
255     g_param_spec_object ("dest-widget",
256                          "Destination Widget",
257                          "The widget to be used as the destination for this selector",
258                          GTK_TYPE_WIDGET,
259                          G_PARAM_READWRITE);
260
261
262   object_class->set_property = psppire_selector_set_property;
263   object_class->get_property = psppire_selector_get_property;
264
265   g_object_class_install_property (object_class,
266                                    PROP_ORIENTATION,
267                                    orientation_spec);
268
269   g_object_class_install_property (object_class,
270                                    PROP_PRIMARY,
271                                    primary_spec);
272
273   g_object_class_install_property (object_class,
274                                    PROP_SOURCE_WIDGET,
275                                    source_widget_spec);
276
277   g_object_class_install_property (object_class,
278                                    PROP_DEST_WIDGET,
279                                    dest_widget_spec);
280
281   parent_class = g_type_class_peek_parent (class);
282
283   signals [SELECTED] =
284     g_signal_new ("selected",
285                   G_TYPE_FROM_CLASS (class),
286                   G_SIGNAL_RUN_FIRST,
287                   0,
288                   NULL, NULL,
289                   g_cclosure_marshal_VOID__VOID,
290                   G_TYPE_NONE,
291                   0);
292
293   signals [DE_SELECTED] =
294     g_signal_new ("de-selected",
295                   G_TYPE_FROM_CLASS (class),
296                   G_SIGNAL_RUN_FIRST,
297                   0,
298                   NULL, NULL,
299                   g_cclosure_marshal_VOID__VOID,
300                   G_TYPE_NONE,
301                   0);
302 }
303
304
305 static void
306 psppire_selector_base_init (PsppireSelectorClass *class)
307 {
308   GObjectClass *object_class = G_OBJECT_CLASS (class);
309
310   object_class->finalize = psppire_selector_finalize;
311   object_class->dispose = psppire_selector_dispose;
312
313   class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
314 }
315
316
317
318 static void
319 psppire_selector_base_finalize(PsppireSelectorClass *class,
320                                 gpointer class_data)
321 {
322   g_hash_table_destroy (class->source_hash);
323 }
324
325 /* Callback for when the source treeview is activated (double clicked) */
326 static void
327 on_row_activate (GtkTreeView       *tree_view,
328                  GtkTreePath       *path,
329                  GtkTreeViewColumn *column,
330                  gpointer           data)
331 {
332   PsppireSelector *selector  = data;
333
334   gtk_action_activate (selector->action);
335 }
336
337 /* Callback for when the source selection changes */
338 static void
339 on_source_select (GtkTreeSelection *treeselection, gpointer data)
340 {
341   PsppireSelector *selector = data;
342
343   set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
344
345   if ( selector->allow_selection )
346     {
347       gtk_action_set_sensitive (selector->action,
348                                 selector->allow_selection (selector->source, selector->dest));
349     }
350   else if ( GTK_IS_ENTRY (selector->dest) )
351     {
352       gtk_action_set_sensitive (selector->action,
353                                 gtk_tree_selection_count_selected_rows
354                                 (treeselection) <= 1 );
355     }
356 }
357
358
359 static void
360 on_realize (PsppireSelector *selector)
361 {
362   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
363   GtkTreeSelection* selection ;
364
365   GList *list = g_hash_table_lookup (class->source_hash, selector->source);
366
367   if ( g_list_first (list)->data == selector)
368     {
369       if ( selector->row_activate_id )
370         g_signal_handler_disconnect (selector->source, selector->row_activate_id);
371
372       selector->row_activate_id =  
373         g_signal_connect (selector->source, "row-activated", G_CALLBACK (on_row_activate), selector);
374     }
375
376   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source));
377
378   if ( selector->source_select_id )
379     g_signal_handler_disconnect (selection, selector->source_select_id);
380
381   selector->source_select_id = 
382     g_signal_connect (selection, "changed", G_CALLBACK (on_source_select), selector);
383 }
384
385
386 static void
387 psppire_selector_init (PsppireSelector *selector)
388 {
389   selector->primary_requested = FALSE;
390   selector->select_user_data = NULL;
391   selector->select_items = NULL;
392   selector->allow_selection = NULL;
393   selector->filter = NULL;
394
395   selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
396   selector->filtered_source = NULL;
397
398   selector->action = gtk_action_new ("select", NULL, NULL, "pspp-stock-select");
399
400   gtk_action_connect_proxy (selector->action, GTK_WIDGET (selector));
401
402   gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
403
404   gtk_widget_show (selector->arrow);
405
406   g_signal_connect_swapped (selector->action, "activate", G_CALLBACK (on_activate), selector);
407
408   selector->selecting = FALSE;
409
410   selector->source = NULL;
411   selector->dest = NULL;
412   selector->dispose_has_run = FALSE;
413
414
415   selector->row_activate_id = 0;
416   selector->source_select_id  = 0;
417
418   g_signal_connect (selector, "realize",
419                     G_CALLBACK (on_realize), NULL);
420
421 }
422
423
424 GtkWidget*
425 psppire_selector_new (void)
426 {
427   return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
428 }
429
430
431 static void
432 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
433 {
434   selector->direction = d;
435
436   /* FIXME: Need to reverse the arrow direction if an RTL locale is in
437      effect */
438   if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
439     {
440       switch (selector->orientation)
441         {
442         case   PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
443           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
444           break;
445         case   PSPPIRE_SELECT_SOURCE_AFTER_DEST:
446           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
447           break;
448         case   PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
449           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
450           break;
451         case   PSPPIRE_SELECT_SOURCE_BELOW_DEST:
452           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
453           break;
454         default:
455           g_assert_not_reached ();
456           break;
457         };
458     }
459   else
460     {
461       switch (selector->orientation)
462         {
463         case   PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
464           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
465           break;
466         case   PSPPIRE_SELECT_SOURCE_AFTER_DEST:
467           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
468           break;
469         case   PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
470           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
471           break;
472         case   PSPPIRE_SELECT_SOURCE_BELOW_DEST:
473           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
474           break;
475         default:
476           g_assert_not_reached ();
477           break;
478         };
479
480     }
481 }
482
483 /* Callback for when the destination treeview selection changes */
484 static void
485 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
486 {
487   PsppireSelector *selector = data;
488
489   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
490
491   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
492 }
493
494 /* Callback for source deselection, when the dest is GtkEntry */
495 static void
496 de_select_selection_entry (PsppireSelector *selector)
497 {
498   gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
499 }
500
501 /* Callback for source deselection, when the dest is GtkTreeView */
502 static void
503 de_select_selection_tree_view (PsppireSelector *selector)
504 {
505   GList *item;
506
507   GtkTreeSelection* selection =
508     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
509
510   GtkTreeModel *model =
511     gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
512
513   GList *selected_rows =
514     gtk_tree_selection_get_selected_rows (selection, NULL);
515
516   g_return_if_fail (selector->select_items);
517
518   /* Convert paths to RowRefs */
519   for (item = g_list_first (selected_rows);
520        item != NULL;
521        item = g_list_next (item))
522     {
523       GtkTreeRowReference* rowref;
524       GtkTreePath *path  = item->data;
525
526       rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
527
528       item->data = rowref ;
529       gtk_tree_path_free (path);
530     }
531
532   /* Remove each selected row from the dest widget */
533   for (item = g_list_first (selected_rows);
534        item != NULL;
535        item = g_list_next (item))
536     {
537       GtkTreeIter iter;
538       GtkTreeRowReference *rr = item->data;
539
540       GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
541
542       gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
543
544       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
545
546       gtk_tree_path_free (path);
547     }
548
549   /* Delete list of RowRefs and its contents */
550   g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
551   g_list_free (selected_rows);
552 }
553
554
555 /* Removes something from the DEST widget */
556 static void
557 de_select_selection (PsppireSelector *selector)
558 {
559   selector->selecting = TRUE;
560
561   if ( GTK_IS_TREE_VIEW (selector->dest ) )
562     de_select_selection_tree_view (selector);
563
564   else if ( GTK_IS_ENTRY (selector->dest))
565     de_select_selection_entry (selector);
566
567   else
568     g_assert_not_reached ();
569
570   selector->selecting = FALSE;
571
572   gtk_tree_model_filter_refilter (selector->filtered_source);
573
574   g_signal_emit (selector, signals [DE_SELECTED], 0);
575 }
576
577
578 /* Puts something into the DEST widget */
579 static void
580 select_selection (PsppireSelector *selector)
581 {
582   GList *item ;
583   GtkTreeSelection* selection =
584     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
585
586   GList *selected_rows =
587     gtk_tree_selection_get_selected_rows (selection, NULL);
588
589   GtkTreeModel *childmodel  = gtk_tree_model_filter_get_model
590     (selector->filtered_source);
591
592   g_return_if_fail (selector->select_items);
593
594   selector->selecting = TRUE;
595
596   for (item = g_list_first (selected_rows);
597        item != NULL;
598        item = g_list_next (item))
599     {
600       GtkTreeIter child_iter;
601       GtkTreeIter iter;
602       GtkTreePath *path  = item->data;
603
604       gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
605                                &iter, path);
606
607       gtk_tree_model_filter_convert_iter_to_child_iter
608         (selector->filtered_source,
609          &child_iter,
610          &iter);
611
612       selector->select_items (child_iter,
613                               selector->dest,
614                               childmodel,
615                               selector->select_user_data
616                               );
617     }
618
619   g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
620   g_list_free (selected_rows);
621
622   gtk_tree_model_filter_refilter (selector->filtered_source);
623
624   g_signal_emit (selector, signals [SELECTED], 0);
625
626   selector->selecting = FALSE;
627 }
628
629 /* Callback for when the selector button is clicked,
630    or other event which causes the selector's action to occur.
631  */
632 static void
633 on_activate (PsppireSelector *selector, gpointer data)
634 {
635   switch (selector->direction)
636     {
637     case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
638       select_selection (selector);
639       break;
640     case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
641       de_select_selection (selector);
642       break;
643     default:
644       g_assert_not_reached ();
645       break;
646     }
647 }
648
649 static gboolean
650 permissive_filter (GtkTreeModel *model, GtkTreeIter *iter,
651                  PsppireSelector *selector)
652 {
653   return FALSE;
654 }
655
656 /* Default visibility filter for GtkTreeView DEST widget */
657 static gboolean
658 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
659                  PsppireSelector *selector)
660 {
661   GtkTreeModel *dest_model;
662   GtkTreeIter dest_iter;
663   GtkTreeIter source_iter;
664   gint index;
665   GtkTreePath *path ;
666   GtkTreeModel *source_model;
667
668   if ( GTK_IS_TREE_MODEL_FILTER (model) )
669     {
670       source_model = gtk_tree_model_filter_get_model
671         (GTK_TREE_MODEL_FILTER (model));
672
673       gtk_tree_model_filter_convert_iter_to_child_iter
674         ( GTK_TREE_MODEL_FILTER (model),  &source_iter,  iter  );
675     }
676   else
677     {
678       source_model = model;
679       source_iter = *iter;
680     }
681
682   dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
683
684   path = gtk_tree_model_get_path (source_model, &source_iter);
685
686   index = *gtk_tree_path_get_indices (path);
687
688   gtk_tree_path_free (path);
689
690   if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
691     return FALSE;
692
693   do
694     {
695       int x;
696       GValue value = {0};
697       GValue int_value = {0};
698       gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
699
700       g_value_init (&int_value, G_TYPE_INT);
701
702       g_value_transform (&value, &int_value);
703
704       x = g_value_get_int (&int_value);
705
706       g_value_unset (&int_value);
707       g_value_unset (&value);
708
709       if ( x == index )
710         return TRUE;
711     }
712   while (gtk_tree_model_iter_next (dest_model, &dest_iter));
713
714   return FALSE;
715 }
716
717 /* Visibility function for items in the SOURCE widget.
718    Returns TRUE iff *all* the selectors for which SOURCE is associated
719    are visible */
720 static gboolean
721 is_source_item_visible (GtkTreeModel *childmodel,
722                         GtkTreeIter *iter, gpointer data)
723 {
724   PsppireSelector *selector = data;
725   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
726
727   GList *list = NULL;
728
729   list = g_hash_table_lookup (class->source_hash, selector->source);
730
731   while (list)
732     {
733       PsppireSelector *selector = list->data;
734
735       if ( selector->filter && selector->filter (childmodel, iter, selector))
736         return FALSE;
737
738       list = list->next;
739     }
740
741
742   return TRUE;
743 }
744
745 /* set the source widget to SOURCE */
746 static void
747 set_tree_view_source (PsppireSelector *selector,
748                       GtkTreeView *source)
749 {
750
751   GList *list = NULL;
752
753   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
754   
755   GtkTreeModel *model = gtk_tree_view_get_model (source);
756
757   if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
758     {
759       selector->filtered_source =
760         GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
761
762       gtk_tree_view_set_model (source,
763                                GTK_TREE_MODEL (selector->filtered_source));
764
765       list = g_list_append (list, selector);
766       g_hash_table_insert (class->source_hash, source, list);
767
768
769       gtk_tree_model_filter_set_visible_func (selector->filtered_source,
770                                               is_source_item_visible,
771                                               selector,
772                                               NULL);
773     }
774   else
775     {  /* Append this selector to the list and push the <source,list>
776           pair onto the hash table */
777
778       selector->filtered_source = GTK_TREE_MODEL_FILTER (model);
779
780       if ( NULL == g_list_find (list, selector) )
781         {
782           if ( selector->primary_requested )
783             list = g_list_prepend (list, selector);
784           else
785             list = g_list_append (list, selector);
786           g_hash_table_replace (class->source_hash, source, list);
787         }
788     }
789
790
791 }
792
793
794 /*
795    Callback for when the destination treeview's data changes
796  */
797 static void
798 on_dest_data_change (GtkTreeModel *tree_model,
799                      GtkTreePath  *path,
800                      GtkTreeIter  *iter,
801                      gpointer      user_data)
802 {
803   PsppireSelector *selector = user_data;
804
805   if ( selector->selecting) return;
806
807   gtk_tree_model_filter_refilter (selector->filtered_source);
808 }
809
810
811 static void
812 on_dest_data_delete (GtkTreeModel *tree_model,
813                      GtkTreePath  *path,
814                      gpointer      user_data)
815 {
816   PsppireSelector *selector = user_data;
817
818   if ( selector->selecting ) return;
819
820   gtk_tree_model_filter_refilter (selector->filtered_source);
821 }
822
823
824 static void
825 xxx (PsppireSelector *selector)
826 {
827   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
828
829   g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
830                     selector);
831
832   g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
833                     selector);
834 }
835
836 /* Set the destination widget to DEST */
837 static void
838 set_tree_view_dest (PsppireSelector *selector,
839                     GtkTreeView *dest)
840 {
841   GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
842
843
844   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
845
846   g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
847                     selector);
848
849
850   g_signal_connect_swapped (dest, "notify::model",
851                             G_CALLBACK (xxx), selector);
852
853 }
854
855 /* Callback which causes the filter to be refiltered.
856    Called when the DEST GtkEntry is activated (Enter is pressed), or when it
857    looses focus.
858 */
859 static gboolean
860 refilter (PsppireSelector *selector)
861 {
862   gtk_tree_model_filter_refilter (selector->filtered_source);
863   return FALSE;
864 }
865
866 /* Callback for when the DEST GtkEntry is selected (clicked) */
867 static gboolean
868 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
869 {
870   PsppireSelector * selector = data;
871
872   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
873   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
874
875   return FALSE;
876 }
877
878
879
880 /* Callback for when an item disappears from the source list.
881    By implication, this means that the item has been inserted into the
882    destination.
883  */
884 static void
885 on_row_deleted (PsppireSelector *selector)
886 {
887   g_signal_emit (selector, signals [SELECTED], 0);
888 }
889
890 /* Callback for when a new item appears in the source list.
891    By implication, this means that an item has been deleted from the
892    destination.
893  */
894 static void
895 on_row_inserted (PsppireSelector *selector)
896 {
897   g_signal_emit (selector, signals [DE_SELECTED], 0);
898 }
899
900
901
902 /* Set DEST to be the destination GtkEntry widget */
903 static void
904 set_entry_dest (PsppireSelector *selector,
905                 GtkEntry *dest)
906 {
907   g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
908                     selector);
909
910   g_signal_connect_swapped (dest, "changed", G_CALLBACK (refilter),
911                     selector);
912
913   g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
914                     selector);
915
916   g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
917                     selector);
918
919
920   g_signal_connect_swapped (selector->filtered_source, "row-deleted",
921                     G_CALLBACK (on_row_deleted), selector);
922
923   g_signal_connect_swapped (selector->filtered_source, "row-inserted",
924                     G_CALLBACK (on_row_inserted), selector);
925 }
926
927 static void
928 set_default_filter (PsppireSelector *selector)
929 {
930   if ( selector->filter == NULL)
931     {
932       if  (GTK_IS_TREE_VIEW (selector->dest))
933         selector->filter = permissive_filter;
934     }
935 }
936
937 static void
938 update_subjects (PsppireSelector *selector)
939 {
940   GtkTreeModel *model = NULL;
941
942   if ( NULL == selector->dest )
943     return;
944
945   set_default_filter (selector);
946
947   if ( NULL == selector->source )
948     return;
949
950   g_signal_connect_swapped (selector->source, "notify::model",
951                             G_CALLBACK (update_subjects), selector);
952
953   model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
954
955   if ( NULL == model)
956     return;
957
958
959   if ( GTK_IS_TREE_VIEW (selector->source))
960     set_tree_view_source (selector, GTK_TREE_VIEW (selector->source) );
961   else
962     g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (selector->source));
963
964   if ( NULL == selector->dest)
965     ;
966   else if  ( GTK_IS_TREE_VIEW (selector->dest))
967     {
968       set_tree_view_dest (selector, GTK_TREE_VIEW (selector->dest));
969     }
970
971   else if ( GTK_IS_ENTRY (selector->dest))
972     set_entry_dest (selector, GTK_ENTRY (selector->dest));
973
974   else if (GTK_IS_TEXT_VIEW (selector->dest))
975     {
976       /* Nothing to be done */
977     }
978   else
979     g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (selector->dest));
980
981   /* FIXME: Remove this dependency */
982   if ( PSPPIRE_IS_DICT_VIEW (selector->source) )
983     {
984       if ( PSPPIRE_IS_VAR_VIEW (selector->dest))
985         psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
986                                           insert_source_row_into_tree_view,
987                                           NULL);
988       else if (GTK_IS_ENTRY (selector->dest))
989         psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
990                                           insert_source_row_into_entry,
991                                           NULL);
992     }
993
994 }
995
996 /* Set FILTER_FUNC for this selector */
997 void
998 psppire_selector_set_filter_func (PsppireSelector *selector,
999                                   FilterItemsFunc *filter_func)
1000 {
1001   selector->filter = filter_func ;
1002   
1003   set_default_filter (selector);
1004 }
1005
1006
1007 /* Set SELECT_FUNC for this selector */
1008 void
1009 psppire_selector_set_select_func (PsppireSelector *selector,
1010                                SelectItemsFunc *select_func,
1011                                gpointer user_data)
1012 {
1013   selector->select_user_data = user_data;
1014   selector->select_items = select_func;
1015 }
1016
1017
1018
1019 void
1020 psppire_selector_set_allow (PsppireSelector *selector, AllowSelectionFunc *allow)
1021 {
1022   selector->allow_selection = allow;
1023 }
1024
1025
1026 GType
1027 psppire_selector_orientation_get_type (void)
1028 {
1029   static GType etype = 0;
1030   if (etype == 0) {
1031     static const GEnumValue values[] = {
1032       { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
1033       { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
1034       { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
1035       { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
1036       { 0, NULL, NULL }
1037     };
1038     etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);
1039   }
1040   return etype;
1041 }