Remove awkward dependency from psppire-selector.c
[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 static void
225 psppire_selector_class_init (PsppireSelectorClass *class)
226 {
227   GObjectClass *object_class = G_OBJECT_CLASS (class);
228   GParamSpec *orientation_spec =
229     g_param_spec_enum ("orientation",
230                        "Orientation",
231                        "Where the selector is relative to its subjects",
232                        PSPPIRE_TYPE_SELECTOR_ORIENTATION,
233                        PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
234                        G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
235
236
237  /* Meaningfull only if more than one selector shares this selectors source */
238   GParamSpec *primary_spec =
239     g_param_spec_boolean ("primary",
240                           "Primary",
241                           "Whether this selector should be the primary selector for the source",
242                           FALSE,
243                           G_PARAM_READWRITE);
244
245   GParamSpec *source_widget_spec = 
246     g_param_spec_object ("source-widget",
247                          "Source Widget",
248                          "The widget to be used as the source for this selector",
249                          GTK_TYPE_WIDGET,
250                          G_PARAM_READWRITE);
251
252   GParamSpec *dest_widget_spec = 
253     g_param_spec_object ("dest-widget",
254                          "Destination Widget",
255                          "The widget to be used as the destination for this selector",
256                          GTK_TYPE_WIDGET,
257                          G_PARAM_READWRITE);
258
259
260   object_class->set_property = psppire_selector_set_property;
261   object_class->get_property = psppire_selector_get_property;
262
263   g_object_class_install_property (object_class,
264                                    PROP_ORIENTATION,
265                                    orientation_spec);
266
267   g_object_class_install_property (object_class,
268                                    PROP_PRIMARY,
269                                    primary_spec);
270
271   g_object_class_install_property (object_class,
272                                    PROP_SOURCE_WIDGET,
273                                    source_widget_spec);
274
275   g_object_class_install_property (object_class,
276                                    PROP_DEST_WIDGET,
277                                    dest_widget_spec);
278
279   parent_class = g_type_class_peek_parent (class);
280
281   signals [SELECTED] =
282     g_signal_new ("selected",
283                   G_TYPE_FROM_CLASS (class),
284                   G_SIGNAL_RUN_FIRST,
285                   0,
286                   NULL, NULL,
287                   g_cclosure_marshal_VOID__VOID,
288                   G_TYPE_NONE,
289                   0);
290
291   signals [DE_SELECTED] =
292     g_signal_new ("de-selected",
293                   G_TYPE_FROM_CLASS (class),
294                   G_SIGNAL_RUN_FIRST,
295                   0,
296                   NULL, NULL,
297                   g_cclosure_marshal_VOID__VOID,
298                   G_TYPE_NONE,
299                   0);
300
301   class->default_selection_funcs = g_hash_table_new (g_direct_hash, g_direct_equal);
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   g_hash_table_destroy (class->default_selection_funcs);
324 }
325
326 /* Callback for when the source treeview is activated (double clicked) */
327 static void
328 on_row_activate (GtkTreeView       *tree_view,
329                  GtkTreePath       *path,
330                  GtkTreeViewColumn *column,
331                  gpointer           data)
332 {
333   PsppireSelector *selector  = data;
334
335   gtk_action_activate (selector->action);
336 }
337
338 /* Callback for when the source selection changes */
339 static void
340 on_source_select (GtkTreeSelection *treeselection, gpointer data)
341 {
342   PsppireSelector *selector = data;
343
344   set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
345
346   if ( selector->allow_selection )
347     {
348       gtk_action_set_sensitive (selector->action,
349                                 selector->allow_selection (selector->source, selector->dest));
350     }
351   else if ( GTK_IS_ENTRY (selector->dest) )
352     {
353       gtk_action_set_sensitive (selector->action,
354                                 gtk_tree_selection_count_selected_rows
355                                 (treeselection) <= 1 );
356     }
357 }
358
359
360 static void
361 on_realize (PsppireSelector *selector)
362 {
363   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
364   GtkTreeSelection* selection ;
365
366   GList *list = g_hash_table_lookup (class->source_hash, selector->source);
367
368   if ( NULL == list)
369     return;
370
371   if ( g_list_first (list)->data == selector)
372     {
373       if ( selector->row_activate_id )
374         g_signal_handler_disconnect (selector->source, selector->row_activate_id);
375
376       selector->row_activate_id =  
377         g_signal_connect (selector->source, "row-activated", G_CALLBACK (on_row_activate), selector);
378     }
379
380   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source));
381
382   if ( selector->source_select_id )
383     g_signal_handler_disconnect (selection, selector->source_select_id);
384
385   selector->source_select_id = 
386     g_signal_connect (selection, "changed", G_CALLBACK (on_source_select), selector);
387 }
388
389
390 static void
391 psppire_selector_init (PsppireSelector *selector)
392 {
393   selector->primary_requested = FALSE;
394   selector->select_user_data = NULL;
395   selector->select_items = NULL;
396   selector->allow_selection = NULL;
397   selector->filter = NULL;
398
399   selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
400   selector->filtered_source = NULL;
401
402   selector->action = gtk_action_new ("select", NULL, NULL, "pspp-stock-select");
403
404   gtk_action_connect_proxy (selector->action, GTK_WIDGET (selector));
405
406   gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
407
408   gtk_widget_show (selector->arrow);
409
410   g_signal_connect_swapped (selector->action, "activate", G_CALLBACK (on_activate), selector);
411
412   selector->selecting = FALSE;
413
414   selector->source = NULL;
415   selector->dest = NULL;
416   selector->dispose_has_run = FALSE;
417
418
419   selector->row_activate_id = 0;
420   selector->source_select_id  = 0;
421
422   g_signal_connect (selector, "realize",
423                     G_CALLBACK (on_realize), NULL);
424
425 }
426
427
428 GtkWidget*
429 psppire_selector_new (void)
430 {
431   return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
432 }
433
434
435 static void
436 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
437 {
438   selector->direction = d;
439
440   /* FIXME: Need to reverse the arrow direction if an RTL locale is in
441      effect */
442   if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
443     {
444       switch (selector->orientation)
445         {
446         case   PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
447           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
448           break;
449         case   PSPPIRE_SELECT_SOURCE_AFTER_DEST:
450           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
451           break;
452         case   PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
453           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
454           break;
455         case   PSPPIRE_SELECT_SOURCE_BELOW_DEST:
456           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
457           break;
458         default:
459           g_assert_not_reached ();
460           break;
461         };
462     }
463   else
464     {
465       switch (selector->orientation)
466         {
467         case   PSPPIRE_SELECT_SOURCE_BEFORE_DEST:
468           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
469           break;
470         case   PSPPIRE_SELECT_SOURCE_AFTER_DEST:
471           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
472           break;
473         case   PSPPIRE_SELECT_SOURCE_ABOVE_DEST:
474           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL);
475           break;
476         case   PSPPIRE_SELECT_SOURCE_BELOW_DEST:
477           g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL);
478           break;
479         default:
480           g_assert_not_reached ();
481           break;
482         };
483
484     }
485 }
486
487 /* Callback for when the destination treeview selection changes */
488 static void
489 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
490 {
491   PsppireSelector *selector = data;
492
493   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
494
495   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
496 }
497
498 /* Callback for source deselection, when the dest is GtkEntry */
499 static void
500 de_select_selection_entry (PsppireSelector *selector)
501 {
502   gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
503 }
504
505 /* Callback for source deselection, when the dest is GtkTreeView */
506 static void
507 de_select_selection_tree_view (PsppireSelector *selector)
508 {
509   GList *item;
510
511   GtkTreeSelection* selection =
512     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
513
514   GtkTreeModel *model =
515     gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
516
517   GList *selected_rows =
518     gtk_tree_selection_get_selected_rows (selection, NULL);
519
520   g_return_if_fail (selector->select_items);
521
522   /* Convert paths to RowRefs */
523   for (item = g_list_first (selected_rows);
524        item != NULL;
525        item = g_list_next (item))
526     {
527       GtkTreeRowReference* rowref;
528       GtkTreePath *path  = item->data;
529
530       rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
531
532       item->data = rowref ;
533       gtk_tree_path_free (path);
534     }
535
536   /* Remove each selected row from the dest widget */
537   for (item = g_list_first (selected_rows);
538        item != NULL;
539        item = g_list_next (item))
540     {
541       GtkTreeIter iter;
542       GtkTreeRowReference *rr = item->data;
543
544       GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
545
546       gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
547
548       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
549
550       gtk_tree_path_free (path);
551     }
552
553   /* Delete list of RowRefs and its contents */
554   g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
555   g_list_free (selected_rows);
556 }
557
558
559 /* Removes something from the DEST widget */
560 static void
561 de_select_selection (PsppireSelector *selector)
562 {
563   selector->selecting = TRUE;
564
565   if ( GTK_IS_TREE_VIEW (selector->dest ) )
566     de_select_selection_tree_view (selector);
567
568   else if ( GTK_IS_ENTRY (selector->dest))
569     de_select_selection_entry (selector);
570
571   else
572     g_assert_not_reached ();
573
574   selector->selecting = FALSE;
575
576   gtk_tree_model_filter_refilter (selector->filtered_source);
577
578   g_signal_emit (selector, signals [DE_SELECTED], 0);
579 }
580
581
582 /* Puts something into the DEST widget */
583 static void
584 select_selection (PsppireSelector *selector)
585 {
586   GList *item ;
587   GtkTreeSelection* selection =
588     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
589
590   GList *selected_rows =
591     gtk_tree_selection_get_selected_rows (selection, NULL);
592
593   GtkTreeModel *childmodel  = gtk_tree_model_filter_get_model
594     (selector->filtered_source);
595
596   g_return_if_fail (selector->select_items);
597
598   selector->selecting = TRUE;
599
600   for (item = g_list_first (selected_rows);
601        item != NULL;
602        item = g_list_next (item))
603     {
604       GtkTreeIter child_iter;
605       GtkTreeIter iter;
606       GtkTreePath *path  = item->data;
607
608       gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
609                                &iter, path);
610
611       gtk_tree_model_filter_convert_iter_to_child_iter
612         (selector->filtered_source,
613          &child_iter,
614          &iter);
615
616       selector->select_items (child_iter,
617                               selector->dest,
618                               childmodel,
619                               selector->select_user_data
620                               );
621     }
622
623   g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
624   g_list_free (selected_rows);
625
626   gtk_tree_model_filter_refilter (selector->filtered_source);
627
628   g_signal_emit (selector, signals [SELECTED], 0);
629
630   selector->selecting = FALSE;
631 }
632
633 /* Callback for when the selector button is clicked,
634    or other event which causes the selector's action to occur.
635  */
636 static void
637 on_activate (PsppireSelector *selector, gpointer data)
638 {
639   switch (selector->direction)
640     {
641     case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
642       select_selection (selector);
643       break;
644     case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
645       de_select_selection (selector);
646       break;
647     default:
648       g_assert_not_reached ();
649       break;
650     }
651 }
652
653 static gboolean
654 permissive_filter (GtkTreeModel *model, GtkTreeIter *iter,
655                  PsppireSelector *selector)
656 {
657   return FALSE;
658 }
659
660 /* Default visibility filter for GtkTreeView DEST widget */
661 static gboolean
662 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
663                  PsppireSelector *selector)
664 {
665   GtkTreeModel *dest_model;
666   GtkTreeIter dest_iter;
667   GtkTreeIter source_iter;
668   gint index;
669   GtkTreePath *path ;
670   GtkTreeModel *source_model;
671
672   if ( GTK_IS_TREE_MODEL_FILTER (model) )
673     {
674       source_model = gtk_tree_model_filter_get_model
675         (GTK_TREE_MODEL_FILTER (model));
676
677       gtk_tree_model_filter_convert_iter_to_child_iter
678         ( GTK_TREE_MODEL_FILTER (model),  &source_iter,  iter  );
679     }
680   else
681     {
682       source_model = model;
683       source_iter = *iter;
684     }
685
686   dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
687
688   path = gtk_tree_model_get_path (source_model, &source_iter);
689
690   index = *gtk_tree_path_get_indices (path);
691
692   gtk_tree_path_free (path);
693
694   if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
695     return FALSE;
696
697   do
698     {
699       int x;
700       GValue value = {0};
701       GValue int_value = {0};
702       gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
703
704       g_value_init (&int_value, G_TYPE_INT);
705
706       g_value_transform (&value, &int_value);
707
708       x = g_value_get_int (&int_value);
709
710       g_value_unset (&int_value);
711       g_value_unset (&value);
712
713       if ( x == index )
714         return TRUE;
715     }
716   while (gtk_tree_model_iter_next (dest_model, &dest_iter));
717
718   return FALSE;
719 }
720
721 /* Visibility function for items in the SOURCE widget.
722    Returns TRUE iff *all* the selectors for which SOURCE is associated
723    are visible */
724 static gboolean
725 is_source_item_visible (GtkTreeModel *childmodel,
726                         GtkTreeIter *iter, gpointer data)
727 {
728   PsppireSelector *selector = data;
729   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
730
731   GList *list = NULL;
732
733   list = g_hash_table_lookup (class->source_hash, selector->source);
734
735   while (list)
736     {
737       PsppireSelector *selector = list->data;
738
739       if ( selector->filter && selector->filter (childmodel, iter, selector))
740         return FALSE;
741
742       list = list->next;
743     }
744
745
746   return TRUE;
747 }
748
749 /* set the source widget to SOURCE */
750 static void
751 set_tree_view_source (PsppireSelector *selector,
752                       GtkTreeView *source)
753 {
754
755   GList *list = NULL;
756
757   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
758   
759   GtkTreeModel *model = gtk_tree_view_get_model (source);
760
761   if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
762     {
763       selector->filtered_source =
764         GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
765
766       gtk_tree_view_set_model (source,
767                                GTK_TREE_MODEL (selector->filtered_source));
768
769       list = g_list_append (list, selector);
770       g_hash_table_insert (class->source_hash, source, list);
771
772
773       gtk_tree_model_filter_set_visible_func (selector->filtered_source,
774                                               is_source_item_visible,
775                                               selector,
776                                               NULL);
777     }
778   else
779     {  /* Append this selector to the list and push the <source,list>
780           pair onto the hash table */
781
782       selector->filtered_source = GTK_TREE_MODEL_FILTER (model);
783
784       if ( NULL == g_list_find (list, selector) )
785         {
786           if ( selector->primary_requested )
787             list = g_list_prepend (list, selector);
788           else
789             list = g_list_append (list, selector);
790           g_hash_table_replace (class->source_hash, source, list);
791         }
792     }
793
794
795 }
796
797
798 /*
799    Callback for when the destination treeview's data changes
800  */
801 static void
802 on_dest_data_change (GtkTreeModel *tree_model,
803                      GtkTreePath  *path,
804                      GtkTreeIter  *iter,
805                      gpointer      user_data)
806 {
807   PsppireSelector *selector = user_data;
808
809   if ( selector->selecting) return;
810
811   gtk_tree_model_filter_refilter (selector->filtered_source);
812 }
813
814
815 static void
816 on_dest_data_delete (GtkTreeModel *tree_model,
817                      GtkTreePath  *path,
818                      gpointer      user_data)
819 {
820   PsppireSelector *selector = user_data;
821
822   if ( selector->selecting ) return;
823
824   gtk_tree_model_filter_refilter (selector->filtered_source);
825 }
826
827
828 static void
829 xxx (PsppireSelector *selector)
830 {
831   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
832
833   g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
834                     selector);
835
836   g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
837                     selector);
838 }
839
840 /* Set the destination widget to DEST */
841 static void
842 set_tree_view_dest (PsppireSelector *selector,
843                     GtkTreeView *dest)
844 {
845   GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
846
847
848   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
849
850   g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
851                     selector);
852
853
854   g_signal_connect_swapped (dest, "notify::model",
855                             G_CALLBACK (xxx), selector);
856
857 }
858
859 /* Callback which causes the filter to be refiltered.
860    Called when the DEST GtkEntry is activated (Enter is pressed), or when it
861    looses focus.
862 */
863 static gboolean
864 refilter (PsppireSelector *selector)
865 {
866   gtk_tree_model_filter_refilter (selector->filtered_source);
867   return FALSE;
868 }
869
870 /* Callback for when the DEST GtkEntry is selected (clicked) */
871 static gboolean
872 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
873 {
874   PsppireSelector * selector = data;
875
876   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
877   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
878
879   return FALSE;
880 }
881
882
883
884 /* Callback for when an item disappears from the source list.
885    By implication, this means that the item has been inserted into the
886    destination.
887  */
888 static void
889 on_row_deleted (PsppireSelector *selector)
890 {
891   g_signal_emit (selector, signals [SELECTED], 0);
892 }
893
894 /* Callback for when a new item appears in the source list.
895    By implication, this means that an item has been deleted from the
896    destination.
897  */
898 static void
899 on_row_inserted (PsppireSelector *selector)
900 {
901   g_signal_emit (selector, signals [DE_SELECTED], 0);
902 }
903
904
905
906 /* Set DEST to be the destination GtkEntry widget */
907 static void
908 set_entry_dest (PsppireSelector *selector,
909                 GtkEntry *dest)
910 {
911   g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
912                     selector);
913
914   g_signal_connect_swapped (dest, "changed", G_CALLBACK (refilter),
915                     selector);
916
917   g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
918                     selector);
919
920   g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
921                     selector);
922
923
924   g_signal_connect_swapped (selector->filtered_source, "row-deleted",
925                     G_CALLBACK (on_row_deleted), selector);
926
927   g_signal_connect_swapped (selector->filtered_source, "row-inserted",
928                     G_CALLBACK (on_row_inserted), selector);
929 }
930
931 static void
932 set_default_filter (PsppireSelector *selector)
933 {
934   if ( selector->filter == NULL)
935     {
936       if  (GTK_IS_TREE_VIEW (selector->dest))
937         selector->filter = permissive_filter;
938     }
939 }
940
941 static void
942 update_subjects (PsppireSelector *selector)
943 {
944   GtkTreeModel *model = NULL;
945
946   if ( NULL == selector->dest )
947     return;
948
949   set_default_filter (selector);
950
951   if ( NULL == selector->source )
952     return;
953
954   g_signal_connect_swapped (selector->source, "notify::model",
955                             G_CALLBACK (update_subjects), selector);
956
957   model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
958
959   if ( NULL == model)
960     return;
961
962
963   if ( GTK_IS_TREE_VIEW (selector->source))
964     set_tree_view_source (selector, GTK_TREE_VIEW (selector->source) );
965   else
966     g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (selector->source));
967
968   if ( NULL == selector->dest)
969     ;
970   else if  ( GTK_IS_TREE_VIEW (selector->dest))
971     {
972       set_tree_view_dest (selector, GTK_TREE_VIEW (selector->dest));
973     }
974
975   else if ( GTK_IS_ENTRY (selector->dest))
976     set_entry_dest (selector, GTK_ENTRY (selector->dest));
977
978   else if (GTK_IS_TEXT_VIEW (selector->dest))
979     {
980       /* Nothing to be done */
981     }
982   else
983     g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (selector->dest));
984
985
986   /* FIXME: Remove this dependency */
987   if ( PSPPIRE_IS_DICT_VIEW (selector->source) )
988     {
989       GObjectClass *class = G_OBJECT_GET_CLASS (selector);
990       GType type = G_OBJECT_TYPE (selector->dest);
991
992       SelectItemsFunc *func  = 
993         g_hash_table_lookup (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type);
994
995       if ( func )
996         psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
997                                           func, NULL);
998     }
999 }
1000
1001
1002 void
1003 psppire_selector_set_default_selection_func (GType type, SelectItemsFunc *func)
1004 {
1005   GObjectClass *class = g_type_class_ref (PSPPIRE_SELECTOR_TYPE);
1006
1007   g_hash_table_insert (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type, func);
1008
1009   g_type_class_unref (class);
1010 }
1011
1012
1013
1014
1015 /* Set FILTER_FUNC for this selector */
1016 void
1017 psppire_selector_set_filter_func (PsppireSelector *selector,
1018                                   FilterItemsFunc *filter_func)
1019 {
1020   selector->filter = filter_func ;
1021   
1022   set_default_filter (selector);
1023 }
1024
1025
1026 /* Set SELECT_FUNC for this selector */
1027 void
1028 psppire_selector_set_select_func (PsppireSelector *selector,
1029                                SelectItemsFunc *select_func,
1030                                gpointer user_data)
1031 {
1032   selector->select_user_data = user_data;
1033   selector->select_items = select_func;
1034 }
1035
1036
1037
1038 void
1039 psppire_selector_set_allow (PsppireSelector *selector, AllowSelectionFunc *allow)
1040 {
1041   selector->allow_selection = allow;
1042 }
1043
1044
1045 GType
1046 psppire_selector_orientation_get_type (void)
1047 {
1048   static GType etype = 0;
1049   if (etype == 0) {
1050     static const GEnumValue values[] = {
1051       { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
1052       { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
1053       { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
1054       { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
1055       { 0, NULL, NULL }
1056     };
1057     etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);
1058   }
1059   return etype;
1060 }