]> Pileus Git - ~andy/gtk/blob - gtk/gtktreeselection.c
Fix stupid typo pointed out by Kristian Rietveld <kristian@planet.nl>.
[~andy/gtk] / gtk / gtktreeselection.c
1 /* gtktreeselection.h
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "gtktreeselection.h"
21 #include "gtktreeprivate.h"
22 #include "gtkrbtree.h"
23 #include "gtksignal.h"
24
25 static void gtk_tree_selection_init              (GtkTreeSelection      *selection);
26 static void gtk_tree_selection_class_init        (GtkTreeSelectionClass *class);
27 static gint gtk_tree_selection_real_select_all   (GtkTreeSelection      *selection);
28 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection      *selection);
29 static gint gtk_tree_selection_real_select_node  (GtkTreeSelection      *selection,
30                                                   GtkRBTree             *tree,
31                                                   GtkRBNode             *node,
32                                                   gboolean               select);
33
34 enum
35 {
36   SELECTION_CHANGED,
37   LAST_SIGNAL
38 };
39
40 static GtkObjectClass *parent_class = NULL;
41 static guint tree_selection_signals[LAST_SIGNAL] = { 0 };
42
43 GtkType
44 gtk_tree_selection_get_type (void)
45 {
46   static GtkType selection_type = 0;
47
48   if (!selection_type)
49     {
50       static const GTypeInfo selection_info =
51       {
52         sizeof (GtkTreeSelectionClass),
53         NULL,           /* base_init */
54         NULL,           /* base_finalize */
55         (GClassInitFunc) gtk_tree_selection_class_init,
56         NULL,           /* class_finalize */
57         NULL,           /* class_data */
58         sizeof (GtkTreeSelection),
59         0,              /* n_preallocs */
60         (GInstanceInitFunc) gtk_tree_selection_init
61       };
62
63       selection_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkTreeSelection", &selection_info, 0);
64     }
65
66   return selection_type;
67 }
68
69 static void
70 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
71 {
72   GtkObjectClass *object_class;
73
74   object_class = (GtkObjectClass*) class;
75   parent_class = g_type_class_peek_parent (class);
76
77   class->selection_changed = NULL;
78
79   tree_selection_signals[SELECTION_CHANGED] =
80     gtk_signal_new ("selection_changed",
81                     GTK_RUN_FIRST,
82                     GTK_CLASS_TYPE (object_class),
83                     GTK_SIGNAL_OFFSET (GtkTreeSelectionClass, selection_changed),
84                     gtk_marshal_VOID__VOID,
85                     GTK_TYPE_NONE, 0);
86 }
87
88 static void
89 gtk_tree_selection_init (GtkTreeSelection *selection)
90 {
91   selection->type = GTK_TREE_SELECTION_SINGLE;
92 }
93
94 /**
95  * _gtk_tree_selection_new:
96  * 
97  * Creates a new #GtkTreeSelection object.  This function should not be invoked,
98  * as each #GtkTreeView will create it's own #GtkTreeSelection.
99  * 
100  * Return value: A newly created #GtkTreeSelection object.
101  **/
102 GtkTreeSelection*
103 _gtk_tree_selection_new (void)
104 {
105   GtkTreeSelection *selection;
106
107   selection = GTK_TREE_SELECTION (gtk_type_new (GTK_TYPE_TREE_SELECTION));
108
109   return selection;
110 }
111
112 /**
113  * _gtk_tree_selection_new_with_tree_view:
114  * @tree_view: The #GtkTreeView.
115  * 
116  * Creates a new #GtkTreeSelection object.  This function should not be invoked,
117  * as each #GtkTreeView will create it's own #GtkTreeSelection.
118  * 
119  * Return value: A newly created #GtkTreeSelection object.
120  **/
121 GtkTreeSelection*
122 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
123 {
124   GtkTreeSelection *selection;
125
126   g_return_val_if_fail (tree_view != NULL, NULL);
127   g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
128
129   selection = _gtk_tree_selection_new ();
130   _gtk_tree_selection_set_tree_view (selection, tree_view);
131
132   return selection;
133 }
134
135 /**
136  * _gtk_tree_selection_set_tree_view:
137  * @selection: A #GtkTreeSelection.
138  * @tree_view: The #GtkTreeView.
139  * 
140  * Sets the #GtkTreeView of @selection.  This function should not be invoked, as
141  * it is used internally by #GtkTreeView.
142  **/
143 void
144 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
145                                    GtkTreeView      *tree_view)
146 {
147   g_return_if_fail (selection != NULL);
148   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
149   if (tree_view != NULL)
150     g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
151
152   selection->tree_view = tree_view;
153 }
154
155 /* FIXME explain what the anchor is */
156 /**
157  * gtk_tree_selection_set_mode:
158  * @selection: A #GtkTreeSelection.
159  * @type: The selection type.
160  * 
161  * Sets the selection type of the @selection.  If the previous type was
162  * #GTK_TREE_SELECTION_MULTI and @type is #GTK_TREE_SELECTION_SINGLE, then
163  * the anchor is kept selected, if it was previously selected.
164  **/
165 void
166 gtk_tree_selection_set_mode (GtkTreeSelection     *selection,
167                              GtkTreeSelectionMode  type)
168 {
169   g_return_if_fail (selection != NULL);
170   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
171
172   if (selection->type == type)
173     return;
174
175   if (type == GTK_TREE_SELECTION_SINGLE)
176     {
177       GtkRBTree *tree = NULL;
178       GtkRBNode *node = NULL;
179       gint selected = FALSE;
180
181       if (selection->tree_view->priv->anchor)
182         {
183           GtkTreePath *anchor_path;
184
185           anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
186
187           if (anchor_path)
188             {
189               _gtk_tree_view_find_node (selection->tree_view,
190                                         anchor_path,
191                                         &tree,
192                                         &node);
193               
194               if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
195                 selected = TRUE;
196
197               gtk_tree_path_free (anchor_path);
198             }
199         }
200       /* FIXME: if user_func is set, then it needs to unconditionally unselect
201        * all.
202        */
203       gtk_tree_selection_unselect_all (selection);
204
205       /* FIXME are we properly emitting the selection_changed signal here? */
206       if (node && selected)
207         GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_IS_SELECTED);
208     }
209   selection->type = type;
210 }
211
212 /**
213  * gtk_tree_selection_set_select_function:
214  * @selection: A #GtkTreeSelection.
215  * @func: The selection function.
216  * @data: The selection function's data.
217  * 
218  * Sets the selection function.  If set, this function is called before any node
219  * is selected or unselected, giving some control over which nodes are selected.
220  **/
221 void
222 gtk_tree_selection_set_select_function (GtkTreeSelection     *selection,
223                                         GtkTreeSelectionFunc  func,
224                                         gpointer              data)
225 {
226   g_return_if_fail (selection != NULL);
227   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
228   g_return_if_fail (func != NULL);
229
230   selection->user_func = func;
231   selection->user_data = data;
232 }
233
234 /**
235  * gtk_tree_selection_get_user_data:
236  * @selection: A #GtkTreeSelection.
237  * 
238  * Returns the user data for the selection function.
239  * 
240  * Return value: The user data.
241  **/
242 gpointer
243 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
244 {
245   g_return_val_if_fail (selection != NULL, NULL);
246   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
247
248   return selection->user_data;
249 }
250
251 GtkTreeView*
252 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
253 {
254   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
255
256   return selection->tree_view;
257 }
258
259 /**
260  * gtk_tree_selection_get_selected:
261  * @selection: A #GtkTreeSelection.
262  * @model: A pointer set to the #GtkTreeModel, or NULL.
263  * @iter: The #GtkTreeIter, or NULL.
264  * 
265  * Sets @iter to the currently selected node if @selection is set to
266  * #GTK_TREE_SELECTION_SINGLE.  Otherwise, it uses the anchor.  @iter may be
267  * NULL if you just want to test if @selection has any selected nodes.  @model
268  * is filled with the current model as a convenience.
269  * 
270  * Return value: TRUE, if there is a selected node.
271  **/
272 gboolean
273 gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
274                                  GtkTreeModel     **model,
275                                  GtkTreeIter       *iter)
276 {
277   GtkRBTree *tree;
278   GtkRBNode *node;
279   GtkTreePath *anchor_path;
280   gboolean retval;
281   
282   g_return_val_if_fail (selection != NULL, FALSE);
283   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
284
285   if (model)
286     *model = selection->tree_view->priv->model;
287   
288   if (selection->tree_view->priv->anchor == NULL)
289     return FALSE;
290
291   anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
292
293   if (anchor_path == NULL)
294     return FALSE;
295   
296   if (iter == NULL)
297     {
298       gtk_tree_path_free (anchor_path);
299       return TRUE;
300     }
301
302   g_return_val_if_fail (selection->tree_view != NULL, FALSE);
303   g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
304
305   retval = FALSE;
306   
307   if (!_gtk_tree_view_find_node (selection->tree_view,
308                                  anchor_path,
309                                  &tree,
310                                  &node) &&
311       ! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
312     {
313       /* We don't want to return the anchor if it isn't actually selected.
314        */
315       retval = FALSE;
316     }
317   else
318     {
319       retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
320                                         iter,
321                                         anchor_path);
322     }
323
324   gtk_tree_path_free (anchor_path);
325   
326   return retval;
327 }
328
329 /**
330  * gtk_tree_selection_selected_foreach:
331  * @selection: A #GtkTreeSelection.
332  * @func: The function to call for each selected node.
333  * @data: user data to pass to the function.
334  * 
335  * Calls a function for each selected node.
336  **/
337 void
338 gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
339                                      GtkTreeSelectionForeachFunc  func,
340                                      gpointer                     data)
341 {
342   GtkTreePath *path;
343   GtkRBTree *tree;
344   GtkRBNode *node;
345   GtkTreeIter iter;
346
347   g_return_if_fail (selection != NULL);
348   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
349   g_return_if_fail (selection->tree_view != NULL);
350   g_return_if_fail (selection->tree_view->priv->model != NULL);
351
352   if (func == NULL ||
353       selection->tree_view->priv->tree == NULL ||
354       selection->tree_view->priv->tree->root == NULL)
355     return;
356
357   tree = selection->tree_view->priv->tree;
358   node = selection->tree_view->priv->tree->root;
359
360   while (node->left != tree->nil)
361     node = node->left;
362
363   /* find the node internally */
364   path = gtk_tree_path_new_root ();
365   gtk_tree_model_get_iter (selection->tree_view->priv->model,
366                            &iter, path);
367   gtk_tree_path_free (path);
368
369   do
370     {
371       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
372         (* func) (selection->tree_view->priv->model, &iter, data);
373       if (node->children)
374         {
375           gboolean has_child;
376           GtkTreeIter tmp;
377
378           tree = node->children;
379           node = tree->root;
380           while (node->left != tree->nil)
381             node = node->left;
382           tmp = iter;
383           has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
384
385           /* Sanity Check! */
386           TREE_VIEW_INTERNAL_ASSERT_VOID (has_child);
387         }
388       else
389         {
390           gboolean done = FALSE;
391           do
392             {
393               node = _gtk_rbtree_next (tree, node);
394               if (node != NULL)
395                 {
396                   gboolean has_next;
397
398                   has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
399                   done = TRUE;
400
401                   /* Sanity Check! */
402                   TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
403                 }
404               else
405                 {
406                   gboolean has_parent;
407                   GtkTreeIter tmp_iter = iter;
408
409                   node = tree->parent_node;
410                   tree = tree->parent_tree;
411                   if (tree == NULL)
412                     /* we've run out of tree */
413                     /* We're done with this function */
414                     return;
415                   has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
416
417                   /* Sanity check */
418                   TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
419                 }
420             }
421           while (!done);
422         }
423     }
424   while (TRUE);
425 }
426
427 /**
428  * gtk_tree_selection_select_path:
429  * @selection: A #GtkTreeSelection.
430  * @path: The #GtkTreePath to be selected.
431  * 
432  * Select the row at @path.
433  **/
434 void
435 gtk_tree_selection_select_path (GtkTreeSelection *selection,
436                                 GtkTreePath      *path)
437 {
438   GtkRBNode *node;
439   GtkRBTree *tree;
440   GdkModifierType state = 0;
441
442   g_return_if_fail (selection != NULL);
443   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
444   g_return_if_fail (selection->tree_view != NULL);
445   g_return_if_fail (path != NULL);
446
447   _gtk_tree_view_find_node (selection->tree_view,
448                             path,
449                             &tree,
450                             &node);
451
452   if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
453     return;
454
455   if (selection->type == GTK_TREE_SELECTION_MULTI)
456     state = GDK_CONTROL_MASK;
457
458   _gtk_tree_selection_internal_select_node (selection,
459                                             node,
460                                             tree,
461                                             path,
462                                             state);
463 }
464
465 /**
466  * gtk_tree_selection_unselect_path:
467  * @selection: A #GtkTreeSelection.
468  * @path: The #GtkTreePath to be unselected.
469  * 
470  * Unselects the row at @path.
471  **/
472 void
473 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
474                                   GtkTreePath      *path)
475 {
476   GtkRBNode *node;
477   GtkRBTree *tree;
478
479   g_return_if_fail (selection != NULL);
480   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
481   g_return_if_fail (selection->tree_view != NULL);
482   g_return_if_fail (path != NULL);
483
484   _gtk_tree_view_find_node (selection->tree_view,
485                             path,
486                             &tree,
487                             &node);
488
489   if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
490     return;
491
492   _gtk_tree_selection_internal_select_node (selection,
493                                             node,
494                                             tree,
495                                             path,
496                                             GDK_CONTROL_MASK);
497 }
498
499 /**
500  * gtk_tree_selection_select_iter:
501  * @selection: A #GtkTreeSelection.
502  * @iter: The #GtkTreeIter to be selected.
503  * 
504  * Selects the specified iterator.
505  **/
506 void
507 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
508                                 GtkTreeIter      *iter)
509 {
510   GtkTreePath *path;
511
512   g_return_if_fail (selection != NULL);
513   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
514   g_return_if_fail (selection->tree_view != NULL);
515   g_return_if_fail (selection->tree_view->priv->model != NULL);
516   g_return_if_fail (iter != NULL);
517
518   path = gtk_tree_model_get_path (selection->tree_view->priv->model,
519                                   iter);
520
521   if (path == NULL)
522     return;
523
524   gtk_tree_selection_select_path (selection, path);
525   gtk_tree_path_free (path);
526 }
527
528
529 /**
530  * gtk_tree_selection_unselect_iter:
531  * @selection: A #GtkTreeSelection.
532  * @iter: The #GtkTreeIter to be unselected.
533  * 
534  * Unselects the specified iterator.
535  **/
536 void
537 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
538                                   GtkTreeIter      *iter)
539 {
540   GtkTreePath *path;
541
542   g_return_if_fail (selection != NULL);
543   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
544   g_return_if_fail (selection->tree_view != NULL);
545   g_return_if_fail (selection->tree_view->priv->model != NULL);
546   g_return_if_fail (iter != NULL);
547
548   path = gtk_tree_model_get_path (selection->tree_view->priv->model,
549                                   iter);
550
551   if (path == NULL)
552     return;
553
554   gtk_tree_selection_select_path (selection, path);
555   gtk_tree_path_free (path);
556 }
557
558 /* Wish I was in python, right now... */
559 struct _TempTuple {
560   GtkTreeSelection *selection;
561   gint dirty;
562 };
563
564 static void
565 select_all_helper (GtkRBTree  *tree,
566                    GtkRBNode  *node,
567                    gpointer    data)
568 {
569   struct _TempTuple *tuple = data;
570
571   if (node->children)
572     _gtk_rbtree_traverse (node->children,
573                           node->children->root,
574                           G_PRE_ORDER,
575                           select_all_helper,
576                           data);
577   if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
578     {
579       tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
580     }
581 }
582
583
584 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
585  * can use it in other places without fear of the signal being emitted.
586  */
587 static gint
588 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
589 {
590   struct _TempTuple *tuple;
591   if (selection->tree_view->priv->tree == NULL)
592     return FALSE;
593
594   if (selection->type == GTK_TREE_SELECTION_SINGLE)
595     {
596       GtkRBTree *tree;
597       GtkRBNode *node;
598       gint dirty;
599
600       /* Just select the last row */
601       
602       dirty = gtk_tree_selection_real_unselect_all (selection);
603
604       tree = selection->tree_view->priv->tree;
605       node = tree->root;
606       do
607         {
608           while (node->right != selection->tree_view->priv->tree->nil)
609             node = node->right;
610
611           if (node->children)
612             {
613               tree = node->children;
614               node = tree->root;
615             }
616           else
617             break;
618         } while (TRUE);
619
620       dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
621
622       return dirty;
623     }
624   else
625     {
626       /* Mark all nodes selected */
627       
628       tuple = g_new (struct _TempTuple, 1);
629       tuple->selection = selection;
630       tuple->dirty = FALSE;
631
632       _gtk_rbtree_traverse (selection->tree_view->priv->tree,
633                             selection->tree_view->priv->tree->root,
634                             G_PRE_ORDER,
635                             select_all_helper,
636                             tuple);
637       if (tuple->dirty)
638         {
639           g_free (tuple);
640           return TRUE;
641         }
642       g_free (tuple);
643       return FALSE;
644     }
645 }
646
647 /**
648  * gtk_tree_selection_select_all:
649  * @selection: A #GtkTreeSelection.
650  * 
651  * Selects all the nodes.  If the type of @selection is
652  * #GTK_TREE_SELECTION_SINGLE, then the last row is selected.
653  **/
654 void
655 gtk_tree_selection_select_all (GtkTreeSelection *selection)
656 {
657   g_return_if_fail (selection != NULL);
658   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
659   g_return_if_fail (selection->tree_view != NULL);
660   g_return_if_fail (selection->tree_view->priv->tree != NULL);
661
662   if (gtk_tree_selection_real_select_all (selection))
663     gtk_signal_emit (GTK_OBJECT (selection), tree_selection_signals[SELECTION_CHANGED]);
664 }
665
666 static void
667 unselect_all_helper (GtkRBTree  *tree,
668                      GtkRBNode  *node,
669                      gpointer    data)
670 {
671   struct _TempTuple *tuple = data;
672
673   if (node->children)
674     _gtk_rbtree_traverse (node->children,
675                           node->children->root,
676                           G_PRE_ORDER,
677                           unselect_all_helper,
678                           data);
679   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
680     {
681       tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
682     }
683 }
684
685 static gint
686 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
687 {
688   struct _TempTuple *tuple;
689
690   if (selection->type == GTK_TREE_SELECTION_SINGLE)
691     {
692       GtkRBTree *tree = NULL;
693       GtkRBNode *node = NULL;
694       GtkTreePath *anchor_path;
695       
696       if (selection->tree_view->priv->anchor == NULL)
697         return FALSE;
698
699       anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
700
701       if (anchor_path == NULL)
702         return FALSE;
703       
704       _gtk_tree_view_find_node (selection->tree_view,
705                                 anchor_path,
706                                 &tree,
707                                 &node);
708
709       gtk_tree_path_free (anchor_path);
710       
711       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
712         {
713           gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
714           return TRUE;
715         }
716       return FALSE;
717     }
718   else
719     {  
720       tuple = g_new (struct _TempTuple, 1);
721       tuple->selection = selection;
722       tuple->dirty = FALSE;
723       
724       _gtk_rbtree_traverse (selection->tree_view->priv->tree,
725                             selection->tree_view->priv->tree->root,
726                             G_PRE_ORDER,
727                             unselect_all_helper,
728                             tuple);
729       
730       if (tuple->dirty)
731         {
732           g_free (tuple);
733           return TRUE;
734         }
735       g_free (tuple);
736       return FALSE;
737     }
738 }
739
740 /**
741  * gtk_tree_selection_unselect_all:
742  * @selection: A #GtkTreeSelection.
743  * 
744  * Unselects all the nodes.
745  **/
746 void
747 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
748 {
749   g_return_if_fail (selection != NULL);
750   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
751   g_return_if_fail (selection->tree_view != NULL);
752   g_return_if_fail (selection->tree_view->priv->tree != NULL);
753   if (selection->tree_view->priv->tree == NULL)
754     return;
755
756   if (gtk_tree_selection_real_unselect_all (selection))
757     gtk_signal_emit (GTK_OBJECT (selection), tree_selection_signals[SELECTION_CHANGED]);
758 }
759
760 static gint
761 gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
762                                       GtkTreePath      *start_path,
763                                       GtkTreePath      *end_path)
764 {
765   GtkRBNode *start_node, *end_node;
766   GtkRBTree *start_tree, *end_tree;
767   gboolean dirty = FALSE;
768
769   switch (gtk_tree_path_compare (start_path, end_path))
770     {
771     case -1:
772       _gtk_tree_view_find_node (selection->tree_view,
773                                 end_path,
774                                 &start_tree,
775                                 &start_node);
776       _gtk_tree_view_find_node (selection->tree_view,
777                                 start_path,
778                                 &end_tree,
779                                 &end_node);
780       break;
781     case 0:
782       _gtk_tree_view_find_node (selection->tree_view,
783                                 start_path,
784                                 &start_tree,
785                                 &start_node);
786       end_tree = start_tree;
787       end_node = start_node;
788       break;
789     case 1:
790       _gtk_tree_view_find_node (selection->tree_view,
791                                 start_path,
792                                 &start_tree,
793                                 &start_node);
794       _gtk_tree_view_find_node (selection->tree_view,
795                                 end_path,
796                                 &end_tree,
797                                 &end_node);
798       break;
799     }
800
801   g_return_val_if_fail (start_node != NULL, FALSE);
802   g_return_val_if_fail (end_node != NULL, FALSE);
803
804   do
805     {
806       if (GTK_RBNODE_FLAG_SET (start_node, GTK_RBNODE_IS_SELECTED))
807         {
808           dirty = gtk_tree_selection_real_select_node (selection, start_tree, start_node, FALSE);
809         }
810
811       if (start_node == end_node)
812         break;
813
814       if (start_node->children)
815         {
816           start_tree = start_node->children;
817           start_node = start_tree->root;
818           while (start_node->left != start_tree->nil)
819             start_node = start_node->left;
820         }
821       else
822         {
823           gboolean done = FALSE;
824           do
825             {
826               start_node = _gtk_rbtree_next (start_tree, start_node);
827               if (start_node != NULL)
828                 {
829                   done = TRUE;
830                 }
831               else
832                 {
833                   start_node = start_tree->parent_node;
834                   start_tree = start_tree->parent_tree;
835                   if (start_tree == NULL)
836                     /* FIXME should this really be silent, or should it g_warning? */
837                     /* we've run out of tree */
838                     /* This means we never found end node!! */
839                     break;
840                 }
841             }
842           while (!done);
843         }
844     }
845   while (TRUE);
846
847   return dirty;
848 }
849
850 /**
851  * gtk_tree_selection_select_range:
852  * @selection: A #GtkTreeSelection.
853  * @start_path: The initial node of the range.
854  * @end_path: The final node of the range.
855  * 
856  * Selects a range of nodes, determined by @start_path and @end_path inclusive.
857  **/
858 void
859 gtk_tree_selection_select_range (GtkTreeSelection *selection,
860                                  GtkTreePath      *start_path,
861                                  GtkTreePath      *end_path)
862 {
863   g_return_if_fail (selection != NULL);
864   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
865   g_return_if_fail (selection->tree_view != NULL);
866
867   if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
868     gtk_signal_emit (GTK_OBJECT (selection), tree_selection_signals[SELECTION_CHANGED]);
869 }
870 /* Called internally by gtktreeview.c It handles actually selecting the tree.
871  * This should almost certainly ever be called by anywhere else.
872  */
873 void
874 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
875                                           GtkRBNode        *node,
876                                           GtkRBTree        *tree,
877                                           GtkTreePath      *path,
878                                           GdkModifierType   state)
879 {
880   gint flags;
881   gint dirty = FALSE;
882   GtkTreePath *anchor_path = NULL;
883
884   if (selection->tree_view->priv->anchor)
885     anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
886
887   if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
888     {
889       if (selection->tree_view->priv->anchor)
890         gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
891       
892       selection->tree_view->priv->anchor =
893         gtk_tree_row_reference_new (selection->tree_view->priv->model,
894                                     path);
895       dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
896     }
897   else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
898     {
899       gtk_tree_selection_select_range (selection,
900                                        anchor_path,
901                                        path);
902     }
903   else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
904     {
905       flags = node->flags;
906       if (selection->type == GTK_TREE_SELECTION_SINGLE)
907         dirty = gtk_tree_selection_real_unselect_all (selection);
908
909       if (selection->tree_view->priv->anchor)
910         gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
911       
912       selection->tree_view->priv->anchor =
913         gtk_tree_row_reference_new (selection->tree_view->priv->model,
914                                     path);      
915
916       if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
917         dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
918       else
919         dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
920     }
921   else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
922     {
923       dirty = gtk_tree_selection_real_unselect_all (selection);
924       dirty |= gtk_tree_selection_real_select_range (selection,
925                                                      anchor_path,
926                                                      path);
927     }
928   else
929     {
930       dirty = gtk_tree_selection_real_unselect_all (selection);
931
932       if (selection->tree_view->priv->anchor)
933         gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
934       
935       selection->tree_view->priv->anchor =
936         gtk_tree_row_reference_new (selection->tree_view->priv->model,
937                                     path);
938       
939       dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
940     }
941
942   if (anchor_path)
943     gtk_tree_path_free (anchor_path);
944   
945   if (dirty)
946     gtk_signal_emit (GTK_OBJECT (selection), tree_selection_signals[SELECTION_CHANGED]);
947 }
948
949 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
950  */
951
952 /* FIXME: user_func can screw up GTK_TREE_SELECTION_SINGLE.  If it prevents
953  * unselection of a node, it can keep more then one node selected.
954  */
955 /* Perhaps the correct solution is to prevent selecting the new node, if
956  * we fail to unselect the old node.
957  */
958 static gint
959 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
960                                      GtkRBTree        *tree,
961                                      GtkRBNode        *node,
962                                      gboolean          select)
963 {
964   gboolean selected = FALSE;
965   GtkTreePath *path = NULL;
966
967   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
968     {
969       path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
970       if (selection->user_func)
971         {
972           if ((*selection->user_func) (selection, selection->tree_view->priv->model, path, selection->user_data))
973             selected = TRUE;
974         }
975       else
976         selected = TRUE;
977       gtk_tree_path_free (path);
978     }
979   if (selected == TRUE)
980     {
981       node->flags ^= GTK_RBNODE_IS_SELECTED;
982
983       /* FIXME: just draw the one node*/
984       gtk_widget_queue_draw (GTK_WIDGET (selection->tree_view));
985       return TRUE;
986     }
987
988   return FALSE;
989 }
990