Added custom psppire-selector widget.
[pspp-builds.git] / src / ui / gui / psppire-selector.c
1 /*
2    PSPPIRE --- A Graphical User Interface for PSPP
3    Copyright (C) 2007  Free Software Foundation
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 /*
21   This module provides a widget, PsppireSelector derived from
22   GtkButton.
23
24   It contains a GtkArrow, and is used for selecting objects from a
25   GtkTreeView and putting them into a destination widget (often
26   another GtkTreeView).  Typically this is used in psppire for
27   selecting variables, thus:
28
29
30   +----------------------------------------------------------+
31   |                                                          |
32   |     Source Widget                       Dest Widget      |
33   |   +----------------+                 +----------------+  |
34   |   | Variable0      |                 | Variable2      |  |
35   |   | Variable1      |                 |                |  |
36   |   | Variable3      |                 |                |  |
37   |   |                |    Selector     |                |  |
38   |   |                |                 |                |  |
39   |   |                |    +------+     |                |  |
40   |   |                |    | |\   |     |                |  |
41   |   |                |    | | \  |     |                |  |
42   |   |                |    | | /  |     |                |  |
43   |   |                |    | |/   |     |                |  |
44   |   |                |    +------+     |                |  |
45   |   |                |                 |                |  |
46   |   |                |                 |                |  |
47   |   |                |                 |                |  |
48   |   |                |                 |                |  |
49   |   +----------------+                 +----------------+  |
50   |                                                          |
51   +----------------------------------------------------------+
52
53   The Source Widget is always a GtkTreeView.  The Dest Widget may be a
54   GtkTreeView or a GtkEntry (other destination widgets may be
55   supported in the future).
56
57   Widgets may be source to more than one PsppireSelector.
58 */
59
60
61 #include <config.h>
62
63 #include <gtk/gtksignal.h>
64 #include <gtk/gtkbutton.h>
65
66 #include "psppire-selector.h"
67
68 #include <gtk/gtktreeview.h>
69 #include <gtk/gtktreeselection.h>
70 #include <gtk/gtkwidget.h>
71
72 static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
73 static void psppire_selector_base_init     (PsppireSelectorClass *class);
74 static void psppire_selector_class_init    (PsppireSelectorClass *class);
75 static void psppire_selector_init          (PsppireSelector      *selector);
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
84 GType
85 psppire_selector_get_type (void)
86 {
87   static GType psppire_selector_type = 0;
88
89   if (!psppire_selector_type)
90     {
91       static const GTypeInfo psppire_selector_info =
92       {
93         sizeof (PsppireSelectorClass),
94         (GBaseInitFunc) psppire_selector_base_init,
95         (GBaseFinalizeFunc) psppire_selector_base_finalize,
96         (GClassInitFunc)psppire_selector_class_init,
97         (GClassFinalizeFunc) NULL,
98         NULL,
99         sizeof (PsppireSelector),
100         0,
101         (GInstanceInitFunc) psppire_selector_init,
102       };
103
104       psppire_selector_type =
105         g_type_register_static (GTK_TYPE_BUTTON, "PsppireSelector",
106                                 &psppire_selector_info, 0);
107     }
108
109   return psppire_selector_type;
110 }
111
112
113 static void
114 psppire_selector_finalize (GObject *object)
115 {
116 }
117
118
119
120 static void
121 psppire_selector_class_init (PsppireSelectorClass *class)
122 {
123   signals [SELECTED] =
124     g_signal_new ("selected",
125                   G_TYPE_FROM_CLASS (class),
126                   G_SIGNAL_RUN_FIRST,
127                   0,
128                   NULL, NULL,
129                   g_cclosure_marshal_VOID__VOID,
130                   G_TYPE_NONE,
131                   0);
132
133   signals [DE_SELECTED] =
134     g_signal_new ("de-selected",
135                   G_TYPE_FROM_CLASS (class),
136                   G_SIGNAL_RUN_FIRST,
137                   0,
138                   NULL, NULL,
139                   g_cclosure_marshal_VOID__VOID,
140                   G_TYPE_NONE,
141                   0);
142
143
144 }
145
146
147 static void
148 psppire_selector_base_init (PsppireSelectorClass *class)
149 {
150   GObjectClass *object_class = G_OBJECT_CLASS (class);
151
152   object_class->finalize = psppire_selector_finalize;
153
154   class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
155 }
156
157
158
159 static void
160 psppire_selector_base_finalize(PsppireSelectorClass *class,
161                                 gpointer class_data)
162 {
163   g_hash_table_destroy (class->source_hash);
164 }
165
166
167 static void
168 psppire_selector_init (PsppireSelector *selector)
169 {
170   selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
171   selector->filtered_source = NULL;
172
173   gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
174
175   gtk_widget_show (selector->arrow);
176
177   /* FIXME: This shouldn't be necessary, but Glade interfaces seem to
178      need it. */
179   gtk_widget_show (GTK_WIDGET (selector));
180 }
181
182
183 GtkWidget*
184 psppire_selector_new (void)
185 {
186   return GTK_WIDGET (g_object_new (psppire_selector_get_type (), NULL));
187 }
188
189
190 static void
191 set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
192 {
193   selector->direction = d;
194
195   /* FIXME: Need to reverse the arrow direction if an RTL locale is in
196      effect */
197   if ( d == PSPPIRE_SELECTOR_SOURCE_TO_DEST )
198     g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL);
199   else
200     g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL);
201 }
202
203 /* Callback for when the source selection changes */
204 static void
205 on_source_select (GtkTreeSelection *treeselection, gpointer data)
206 {
207   PsppireSelector *selector = data;
208
209   set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
210
211   if ( GTK_IS_ENTRY (selector->dest) )
212     {
213       gtk_widget_set_sensitive (GTK_WIDGET (selector),
214                                 gtk_tree_selection_count_selected_rows
215                                 (treeselection) <= 1 );
216     }
217 }
218
219 /* Callback for when the destination treeview selection changes */
220 static void
221 on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
222 {
223   PsppireSelector *selector = data;
224
225   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
226 }
227
228 /* Callback for source deselection, when the dest is GtkEntry */
229 static void
230 de_select_selection_entry (PsppireSelector *selector)
231 {
232   gtk_entry_set_text (GTK_ENTRY (selector->dest), "");
233 }
234
235 /* Callback for source deselection, when the dest is GtkTreeView */
236 static void
237 de_select_selection_tree_view (PsppireSelector *selector)
238 {
239   GList *item;
240
241   GtkTreeSelection* selection =
242     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest));
243
244   GtkTreeModel *model =
245     gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
246
247   GList *selected_rows =
248     gtk_tree_selection_get_selected_rows (selection, NULL);
249
250   g_return_if_fail (selector->select_items);
251
252   /* Convert paths to RowRefs */
253   for (item = g_list_first (selected_rows);
254        item != NULL;
255        item = g_list_next (item))
256     {
257       GtkTreeRowReference* rowref;
258       GtkTreePath *path  = item->data;
259
260       rowref = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
261
262       item->data = rowref ;
263       gtk_tree_path_free (path);
264     }
265
266   /* Remove each selected row from the dest widget */
267   for (item = g_list_first (selected_rows);
268        item != NULL;
269        item = g_list_next (item))
270     {
271       GtkTreeIter iter;
272       GtkTreeRowReference *rr = item->data;
273
274       GtkTreePath *path = gtk_tree_row_reference_get_path (rr);
275
276       gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
277
278       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
279
280       gtk_tree_path_free (path);
281     }
282
283   /* Delete list of RowRefs and its contents */
284   g_list_foreach (selected_rows, (GFunc) gtk_tree_row_reference_free, NULL);
285   g_list_free (selected_rows);
286 }
287
288
289 /* Removes something from the DEST widget */
290 static void
291 de_select_selection (PsppireSelector *selector)
292 {
293   if ( GTK_IS_TREE_VIEW (selector->dest ) )
294     de_select_selection_tree_view (selector);
295
296   else if ( GTK_IS_ENTRY (selector->dest))
297     de_select_selection_entry (selector);
298
299   else
300     g_assert_not_reached ();
301
302   gtk_tree_model_filter_refilter (selector->filtered_source);
303
304   g_signal_emit (selector, signals [DE_SELECTED], 0);
305 }
306
307
308 /* Puts something into the DEST widget */
309 static void
310 select_selection (PsppireSelector *selector)
311 {
312   GList *item ;
313   GtkTreeSelection* selection =
314     gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source));
315
316   GList *selected_rows =
317     gtk_tree_selection_get_selected_rows (selection, NULL);
318
319   GtkTreeModel *childmodel  = gtk_tree_model_filter_get_model
320     (selector->filtered_source);
321
322   g_return_if_fail (selector->select_items);
323
324
325   for (item = g_list_first (selected_rows);
326        item != NULL;
327        item = g_list_next (item))
328     {
329       GtkTreeIter child_iter;
330       GtkTreeIter iter;
331       GtkTreePath *path  = item->data;
332
333       gtk_tree_model_get_iter (GTK_TREE_MODEL (selector->filtered_source),
334                                &iter, path);
335
336       gtk_tree_model_filter_convert_iter_to_child_iter
337         (selector->filtered_source,
338          &child_iter,
339          &iter);
340
341       selector->select_items (child_iter,
342                               selector->dest,
343                               childmodel);
344     }
345
346   g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
347   g_list_free (selected_rows);
348
349   gtk_tree_model_filter_refilter (selector->filtered_source);
350
351   g_signal_emit (selector, signals [SELECTED], 0);
352 }
353
354 /* Callback fro then the source treeview is activated (double clicked) */
355 static void
356 on_row_activate (GtkTreeView       *tree_view,
357                  GtkTreePath       *path,
358                  GtkTreeViewColumn *column,
359                  gpointer           data)
360 {
361   PsppireSelector *selector  = data;
362
363   select_selection (selector);
364 }
365
366 /* Callback for when the selector button is clicked */
367 static void
368 on_click (PsppireSelector *selector, gpointer data)
369 {
370   switch (selector->direction)
371     {
372     case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
373       select_selection (selector);
374       break;
375     case PSPPIRE_SELECTOR_DEST_TO_SOURCE:
376       de_select_selection (selector);
377       break;
378     default:
379       g_assert_not_reached ();
380       break;
381     }
382 }
383
384 /* Default visibility filter for GtkTreeView DEST widget */
385 static gboolean
386 is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
387                  PsppireSelector *selector)
388 {
389   GtkTreeModel *dest_model;
390   GtkTreeIter dest_iter;
391   GtkTreeIter source_iter;
392   gint index;
393   GtkTreePath *path ;
394   GtkTreeModel *source_model;
395
396   if ( GTK_IS_TREE_MODEL_FILTER (model) )
397     {
398       source_model = gtk_tree_model_filter_get_model
399         (GTK_TREE_MODEL_FILTER (model));
400
401       gtk_tree_model_filter_convert_iter_to_child_iter
402         ( GTK_TREE_MODEL_FILTER (model),  &source_iter,  iter  );
403     }
404   else
405     {
406       source_model = model;
407       source_iter = *iter;
408     }
409
410   dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
411
412   path = gtk_tree_model_get_path (source_model, &source_iter);
413
414   index = *gtk_tree_path_get_indices (path);
415
416   gtk_tree_path_free (path);
417
418   if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
419     return FALSE;
420
421   do
422     {
423       GValue value = {0};
424       gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
425
426       if ( g_value_get_int (&value) == index)
427         return TRUE;
428     }
429   while (gtk_tree_model_iter_next (dest_model, &dest_iter));
430
431   return FALSE;
432 }
433
434 /* Visibility function for items in the SOURCE widget.
435    Returns TRUE iff *all* the selectors for which SOURCE is associated
436    are visible */
437 static gboolean
438 is_source_item_visible (GtkTreeModel *childmodel,
439                         GtkTreeIter *iter, gpointer data)
440 {
441   PsppireSelector *selector = data;
442   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
443
444   GList *list = NULL;
445
446   list = g_hash_table_lookup (class->source_hash, selector->source);
447
448   while (list)
449     {
450       PsppireSelector *selector = list->data;
451
452       if ( selector->filter (childmodel, iter, selector))
453           return FALSE;
454
455       list = list->next;
456     }
457
458
459   return TRUE;
460 }
461
462 /* set the source widget to SOURCE */
463 static void
464 set_tree_view_source (PsppireSelector *selector,
465                       GtkTreeView *source)
466 {
467   GtkTreeSelection* selection ;
468   GList *list = NULL;
469
470   PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
471
472   if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
473     {
474       selector->filtered_source =
475         GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
476                                (gtk_tree_view_get_model (source),  NULL));
477
478       gtk_tree_view_set_model (source, NULL);
479
480       gtk_tree_view_set_model (source,
481                                GTK_TREE_MODEL (selector->filtered_source));
482
483       list = g_list_append (list, selector);
484       g_hash_table_insert (class->source_hash, source, list);
485
486
487       gtk_tree_model_filter_set_visible_func (selector->filtered_source,
488                                               is_source_item_visible,
489                                               selector,
490                                               NULL);
491     }
492   else
493     {  /* Append this selector to the list and push the <source,list>
494           pair onto the hash table */
495
496       selector->filtered_source = GTK_TREE_MODEL_FILTER (
497         gtk_tree_view_get_model (source));
498
499       list = g_list_append (list, selector);
500       g_hash_table_replace (class->source_hash, source, list);
501     }
502
503   selection = gtk_tree_view_get_selection (source);
504
505   g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
506                     selector);
507
508   g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
509                     selector);
510 }
511
512
513 /* Set the destination widget to DEST */
514 static void
515 set_tree_view_dest (PsppireSelector *selector,
516                     GtkTreeView *dest)
517 {
518   GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
519
520   gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
521
522   g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
523                     selector);
524 }
525
526 /* Callback for when the DEST GtkEntry is activated (Enter is pressed) */
527 static void
528 on_entry_activate (GtkEntry *w, gpointer data)
529 {
530   PsppireSelector * selector = data;
531
532   gtk_tree_model_filter_refilter (selector->filtered_source);
533 }
534
535 /* Callback for when the DEST GtkEntry is selected (clicked) */
536 static gboolean
537 on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data)
538 {
539   PsppireSelector * selector = data;
540
541   set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE);
542
543   return FALSE;
544 }
545
546 /* Set DEST to be the destination GtkEntry widget */
547 static void
548 set_entry_dest (PsppireSelector *selector,
549                 GtkEntry *dest)
550 {
551   g_signal_connect (dest, "activate", G_CALLBACK (on_entry_activate),
552                     selector);
553
554   g_signal_connect (dest, "focus-in-event", G_CALLBACK (on_entry_dest_select),
555                     selector);
556 }
557
558
559 /* Set SOURCE and DEST for this selector, and
560    set SELECT_FUNC and FILTER_FUNC */
561 void
562 psppire_selector_set_subjects (PsppireSelector *selector,
563                                GtkWidget *source,
564                                GtkWidget *dest,
565                                SelectItemsFunc *select_func,
566                                FilterItemsFunc *filter_func )
567 {
568   selector->filter = filter_func ;
569
570   selector->source = source;
571   selector->dest = dest;
572
573   if ( filter_func == NULL)
574     {
575       if  (GTK_IS_TREE_VIEW (dest))
576         selector->filter = is_item_in_dest;
577     }
578
579   g_signal_connect (selector, "clicked", G_CALLBACK (on_click), NULL);
580
581   if ( GTK_IS_TREE_VIEW (source))
582     set_tree_view_source (selector, GTK_TREE_VIEW (source) );
583   else
584     g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
585
586   g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
587
588   if  ( GTK_IS_TREE_VIEW (dest))
589     set_tree_view_dest (selector, GTK_TREE_VIEW (dest));
590
591   else if ( GTK_IS_ENTRY (dest))
592     set_entry_dest (selector, GTK_ENTRY (dest));
593
594   else
595     g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
596
597   selector->select_items = select_func;
598 }