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