gui: Factor out duplicated code for executing and pasting syntax.
[pspp] / 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 #include "psppire-dict.h"
64 #include "psppire-select-dest.h"
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 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter, PsppireSelector *selector)
655 {
656   gboolean result = FALSE;
657   GtkTreeIter source_iter;
658   GtkTreeModel *source_model;
659   GValue value = {0};
660
661   if (GTK_IS_TREE_MODEL_FILTER (model))
662     {
663       source_model = gtk_tree_model_filter_get_model
664         (GTK_TREE_MODEL_FILTER (model));
665
666       gtk_tree_model_filter_convert_iter_to_child_iter
667         (GTK_TREE_MODEL_FILTER (model),  &source_iter, iter);
668     }
669   else
670     {
671       source_model = model;
672       source_iter = *iter;
673     }
674
675   gtk_tree_model_get_value (source_model, &source_iter, DICT_TVM_COL_VAR, &value);
676
677   result = psppire_select_dest_widget_contains_var (PSPPIRE_SELECT_DEST_WIDGET (selector->dest),
678                                                     &value);
679
680   g_value_unset (&value);
681
682   return result;
683 }
684
685
686
687 /* Visibility function for items in the SOURCE widget.
688    Returns TRUE iff *all* the selectors for which SOURCE is associated
689    are visible */
690 static gboolean
691 is_source_item_visible (GtkTreeModel *childmodel,
692                         GtkTreeIter *iter, gpointer data)
693 {
694   PsppireSelector *selector = data;
695   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
696
697   GList *list = NULL;
698
699   list = g_hash_table_lookup (class->source_hash, selector->source);
700
701   while (list)
702     {
703       PsppireSelector *selector = list->data;
704
705       if ( selector->filter && selector->filter (childmodel, iter, selector))
706         return FALSE;
707
708       list = list->next;
709     }
710
711
712   return TRUE;
713 }
714
715 /* set the source widget to SOURCE */
716 static void
717 set_tree_view_source (PsppireSelector *selector,
718                       GtkTreeView *source)
719 {
720   GList *list = NULL;
721
722   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
723   
724   GtkTreeModel *model = gtk_tree_view_get_model (source);
725
726   if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
727     {
728       selector->filtered_source =
729         GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
730
731       gtk_tree_view_set_model (source,
732                                GTK_TREE_MODEL (selector->filtered_source));
733
734       list = g_list_append (list, selector);
735       g_hash_table_insert (class->source_hash, source, list);
736
737
738       gtk_tree_model_filter_set_visible_func (selector->filtered_source,
739                                               is_source_item_visible,
740                                               selector,
741                                               NULL);
742     }
743   else
744     {  /* Append this selector to the list and push the <source,list>
745           pair onto the hash table */
746
747       selector->filtered_source = GTK_TREE_MODEL_FILTER (model);
748
749       if ( NULL == g_list_find (list, selector) )
750         {
751           if ( selector->primary_requested )
752             list = g_list_prepend (list, selector);
753           else
754             list = g_list_append (list, selector);
755           g_hash_table_replace (class->source_hash, source, list);
756         }
757     }
758 }
759
760
761 /*
762    Callback for when the destination treeview's data changes
763  */
764 static void
765 on_dest_data_change (GtkTreeModel *tree_model,
766                      GtkTreePath  *path,
767                      GtkTreeIter  *iter,
768                      gpointer      user_data)
769 {
770   PsppireSelector *selector = user_data;
771
772   if ( selector->selecting) return;
773
774   gtk_tree_model_filter_refilter (selector->filtered_source);
775 }
776
777
778 static void
779 on_dest_data_delete (GtkTreeModel *tree_model,
780                      GtkTreePath  *path,
781                      gpointer      user_data)
782 {
783   PsppireSelector *selector = user_data;
784
785   if ( selector->selecting ) return;
786
787   gtk_tree_model_filter_refilter (selector->filtered_source);
788 }
789
790
791 static void
792 on_dest_model_changed (PsppireSelector *selector)
793 {
794   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
795
796   g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
797                     selector);
798
799   g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
800                     selector);
801 }
802
803 /* Set the destination widget to DEST */
804 static void
805 set_tree_view_dest (PsppireSelector *selector,
806                     GtkTreeView *dest)
807 {
808   GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
809
810
811   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
812
813   g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
814                     selector);
815
816   on_dest_model_changed (selector);
817   g_signal_connect_swapped (dest, "notify::model",
818                             G_CALLBACK (on_dest_model_changed), selector);
819 }
820
821
822 /* Callback which causes the filter to be refiltered.
823    Called when the DEST GtkEntry is activated (Enter is pressed), or when it
824    looses focus.
825 */
826 static gboolean
827 refilter (PsppireSelector *selector)
828 {
829   gtk_tree_model_filter_refilter (selector->filtered_source);
830   return FALSE;
831 }
832
833
834 /* Callback for when the DEST GtkEntry is selected (clicked) */
835 static gboolean
836 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
837 {
838   PsppireSelector * selector = data;
839
840   gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source)));
841   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
842
843   return FALSE;
844 }
845
846
847 /* Callback for when an item disappears from the source list.
848    By implication, this means that the item has been inserted into the
849    destination.
850  */
851 static void
852 on_row_deleted (PsppireSelector *selector)
853 {
854   g_signal_emit (selector, signals [SELECTED], 0);
855 }
856
857 /* Callback for when a new item appears in the source list.
858    By implication, this means that an item has been deleted from the
859    destination.
860  */
861 static void
862 on_row_inserted (PsppireSelector *selector)
863 {
864   g_signal_emit (selector, signals [DE_SELECTED], 0);
865 }
866
867
868
869 /* Set DEST to be the destination GtkEntry widget */
870 static void
871 set_entry_dest (PsppireSelector *selector,
872                 GtkEntry *dest)
873 {
874   g_signal_connect_swapped (dest, "activate", G_CALLBACK (refilter),
875                     selector);
876
877   g_signal_connect_swapped (dest, "changed", G_CALLBACK (refilter),
878                     selector);
879
880   g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
881                     selector);
882
883   g_signal_connect_swapped (dest, "focus-out-event", G_CALLBACK (refilter),
884                     selector);
885
886
887   g_signal_connect_swapped (selector->filtered_source, "row-deleted",
888                     G_CALLBACK (on_row_deleted), selector);
889
890   g_signal_connect_swapped (selector->filtered_source, "row-inserted",
891                     G_CALLBACK (on_row_inserted), selector);
892 }
893
894 static void
895 set_default_filter (PsppireSelector *selector)
896 {
897   if ( selector->filter == NULL)
898     {
899       if  (GTK_IS_TREE_VIEW (selector->dest))
900         selector->filter = is_item_in_dest;
901     }
902 }
903
904 static void
905 update_subjects (PsppireSelector *selector)
906 {
907   GtkTreeModel *model = NULL;
908
909   if ( NULL == selector->dest )
910     return;
911
912   set_default_filter (selector);
913
914   if ( NULL == selector->source )
915     return;
916
917   g_signal_connect_swapped (selector->source, "notify::model",
918                             G_CALLBACK (update_subjects), selector);
919
920   model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
921
922   if ( NULL == model)
923     return;
924
925
926   if ( GTK_IS_TREE_VIEW (selector->source))
927     set_tree_view_source (selector, GTK_TREE_VIEW (selector->source) );
928   else
929     g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (selector->source));
930
931   if ( NULL == selector->dest)
932     ;
933   else if  ( GTK_IS_TREE_VIEW (selector->dest))
934     {
935       set_tree_view_dest (selector, GTK_TREE_VIEW (selector->dest));
936     }
937
938   else if ( GTK_IS_ENTRY (selector->dest))
939     set_entry_dest (selector, GTK_ENTRY (selector->dest));
940
941   else if (GTK_IS_TEXT_VIEW (selector->dest))
942     {
943       /* Nothing to be done */
944     }
945   else
946     g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (selector->dest));
947
948
949   /* FIXME: Remove this dependency */
950   if ( PSPPIRE_IS_DICT_VIEW (selector->source) )
951     {
952       GObjectClass *class = G_OBJECT_GET_CLASS (selector);
953       GType type = G_OBJECT_TYPE (selector->dest);
954
955       SelectItemsFunc *func  = 
956         g_hash_table_lookup (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type);
957
958       if ( func )
959         psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
960                                           func, NULL);
961     }
962 }
963
964
965 void
966 psppire_selector_set_default_selection_func (GType type, SelectItemsFunc *func)
967 {
968   GObjectClass *class = g_type_class_ref (PSPPIRE_SELECTOR_TYPE);
969
970   g_hash_table_insert (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type, func);
971
972   g_type_class_unref (class);
973 }
974
975
976
977
978 /* Set FILTER_FUNC for this selector */
979 void
980 psppire_selector_set_filter_func (PsppireSelector *selector,
981                                   FilterItemsFunc *filter_func)
982 {
983   selector->filter = filter_func ;
984 }
985
986
987 /* Set SELECT_FUNC for this selector */
988 void
989 psppire_selector_set_select_func (PsppireSelector *selector,
990                                SelectItemsFunc *select_func,
991                                gpointer user_data)
992 {
993   selector->select_user_data = user_data;
994   selector->select_items = select_func;
995 }
996
997
998
999 void
1000 psppire_selector_set_allow (PsppireSelector *selector, AllowSelectionFunc *allow)
1001 {
1002   selector->allow_selection = allow;
1003 }
1004
1005
1006 GType
1007 psppire_selector_orientation_get_type (void)
1008 {
1009   static GType etype = 0;
1010   if (etype == 0) {
1011     static const GEnumValue values[] = {
1012       { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" },
1013       { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" },
1014       { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" },
1015       { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" },
1016       { 0, NULL, NULL }
1017     };
1018     etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values);
1019   }
1020   return etype;
1021 }