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