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