]> Pileus Git - ~andy/gtk/blob - gtk/gtktreeselection.c
Fix function name in warning message. (#118156, Tim-Philipp Müller)
[~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 "gtkmarshalers.h"
24
25 static void gtk_tree_selection_init              (GtkTreeSelection      *selection);
26 static void gtk_tree_selection_class_init        (GtkTreeSelectionClass *class);
27
28 static void gtk_tree_selection_finalize          (GObject               *object);
29 static gint gtk_tree_selection_real_select_all   (GtkTreeSelection      *selection);
30 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection      *selection);
31 static gint gtk_tree_selection_real_select_node  (GtkTreeSelection      *selection,
32                                                   GtkRBTree             *tree,
33                                                   GtkRBNode             *node,
34                                                   gboolean               select);
35
36 enum
37 {
38   CHANGED,
39   LAST_SIGNAL
40 };
41
42 static GObjectClass *parent_class = NULL;
43 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
44
45 GType
46 gtk_tree_selection_get_type (void)
47 {
48   static GType selection_type = 0;
49
50   if (!selection_type)
51     {
52       static const GTypeInfo selection_info =
53       {
54         sizeof (GtkTreeSelectionClass),
55         NULL,           /* base_init */
56         NULL,           /* base_finalize */
57         (GClassInitFunc) gtk_tree_selection_class_init,
58         NULL,           /* class_finalize */
59         NULL,           /* class_data */
60         sizeof (GtkTreeSelection),
61         0,              /* n_preallocs */
62         (GInstanceInitFunc) gtk_tree_selection_init
63       };
64
65       selection_type =
66         g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection",
67                                 &selection_info, 0);
68     }
69
70   return selection_type;
71 }
72
73 static void
74 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
75 {
76   GObjectClass *object_class;
77
78   object_class = (GObjectClass*) class;
79   parent_class = g_type_class_peek_parent (class);
80
81   object_class->finalize = gtk_tree_selection_finalize;
82   class->changed = NULL;
83
84   tree_selection_signals[CHANGED] =
85     g_signal_new ("changed",
86                   G_OBJECT_CLASS_TYPE (object_class),
87                   G_SIGNAL_RUN_FIRST,
88                   G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
89                   NULL, NULL,
90                   _gtk_marshal_VOID__VOID,
91                   G_TYPE_NONE, 0);
92 }
93
94 static void
95 gtk_tree_selection_init (GtkTreeSelection *selection)
96 {
97   selection->type = GTK_SELECTION_SINGLE;
98 }
99
100 static void
101 gtk_tree_selection_finalize (GObject *object)
102 {
103   GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
104
105   if (selection->destroy)
106     {
107       GtkDestroyNotify d = selection->destroy;
108
109       selection->destroy = NULL;
110       d (selection->user_data);
111     }
112
113   /* chain parent_class' handler */
114   G_OBJECT_CLASS (parent_class)->finalize (object);
115 }
116
117 /**
118  * _gtk_tree_selection_new:
119  *
120  * Creates a new #GtkTreeSelection object.  This function should not be invoked,
121  * as each #GtkTreeView will create its own #GtkTreeSelection.
122  *
123  * Return value: A newly created #GtkTreeSelection object.
124  **/
125 GtkTreeSelection*
126 _gtk_tree_selection_new (void)
127 {
128   GtkTreeSelection *selection;
129
130   selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
131
132   return selection;
133 }
134
135 /**
136  * _gtk_tree_selection_new_with_tree_view:
137  * @tree_view: The #GtkTreeView.
138  *
139  * Creates a new #GtkTreeSelection object.  This function should not be invoked,
140  * as each #GtkTreeView will create its own #GtkTreeSelection.
141  *
142  * Return value: A newly created #GtkTreeSelection object.
143  **/
144 GtkTreeSelection*
145 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
146 {
147   GtkTreeSelection *selection;
148
149   g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
150
151   selection = _gtk_tree_selection_new ();
152   _gtk_tree_selection_set_tree_view (selection, tree_view);
153
154   return selection;
155 }
156
157 /**
158  * _gtk_tree_selection_set_tree_view:
159  * @selection: A #GtkTreeSelection.
160  * @tree_view: The #GtkTreeView.
161  *
162  * Sets the #GtkTreeView of @selection.  This function should not be invoked, as
163  * it is used internally by #GtkTreeView.
164  **/
165 void
166 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
167                                    GtkTreeView      *tree_view)
168 {
169   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
170   if (tree_view != NULL)
171     g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
172
173   selection->tree_view = tree_view;
174 }
175
176 /**
177  * gtk_tree_selection_set_mode:
178  * @selection: A #GtkTreeSelection.
179  * @type: The selection mode
180  *
181  * Sets the selection mode of the @selection.  If the previous type was
182  * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
183  * previously selected.
184  **/
185 void
186 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
187                              GtkSelectionMode  type)
188 {
189   GtkTreeSelectionFunc tmp_func;
190   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
191
192   if (selection->type == type)
193     return;
194
195   
196   if (type == GTK_SELECTION_NONE)
197     {
198       /* We do this so that we unconditionally unset all rows
199        */
200       tmp_func = selection->user_func;
201       selection->user_func = NULL;
202       gtk_tree_selection_unselect_all (selection);
203       selection->user_func = tmp_func;
204
205       gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
206       selection->tree_view->priv->anchor = NULL;
207     }
208   else if (type == GTK_SELECTION_SINGLE ||
209            type == GTK_SELECTION_BROWSE)
210     {
211       GtkRBTree *tree = NULL;
212       GtkRBNode *node = NULL;
213       gint selected = FALSE;
214       GtkTreePath *anchor_path = NULL;
215
216       if (selection->tree_view->priv->anchor)
217         {
218           anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
219
220           if (anchor_path)
221             {
222               _gtk_tree_view_find_node (selection->tree_view,
223                                         anchor_path,
224                                         &tree,
225                                         &node);
226
227               if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
228                 selected = TRUE;
229             }
230         }
231
232       /* We do this so that we unconditionally unset all rows
233        */
234       tmp_func = selection->user_func;
235       selection->user_func = NULL;
236       gtk_tree_selection_unselect_all (selection);
237       selection->user_func = tmp_func;
238
239       if (node && selected)
240         _gtk_tree_selection_internal_select_node (selection,
241                                                   node,
242                                                   tree,
243                                                   anchor_path,
244                                                   0,
245                                                   FALSE);
246       if (anchor_path)
247         gtk_tree_path_free (anchor_path);
248     }
249
250   selection->type = type;
251 }
252
253 /**
254  * gtk_tree_selection_get_mode:
255  * @selection: a #GtkTreeSelection
256  *
257  * Gets the selection mode for @selection. See
258  * gtk_tree_selection_set_mode().
259  *
260  * Return value: the current selection mode
261  **/
262 GtkSelectionMode
263 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
264 {
265   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
266
267   return selection->type;
268 }
269
270 /**
271  * gtk_tree_selection_set_select_function:
272  * @selection: A #GtkTreeSelection.
273  * @func: The selection function.
274  * @data: The selection function's data.
275  * @destroy: The destroy function for user data.  May be NULL.
276  *
277  * Sets the selection function.  If set, this function is called before any node
278  * is selected or unselected, giving some control over which nodes are selected.
279  * The select function should return %TRUE if the state of the node may be toggled,
280  * and %FALSE if the state of the node should be left unchanged.
281  **/
282 void
283 gtk_tree_selection_set_select_function (GtkTreeSelection     *selection,
284                                         GtkTreeSelectionFunc  func,
285                                         gpointer              data,
286                                         GtkDestroyNotify      destroy)
287 {
288   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
289   g_return_if_fail (func != NULL);
290
291   if (selection->destroy)
292     {
293       GtkDestroyNotify d = selection->destroy;
294
295       selection->destroy = NULL;
296       d (selection->user_data);
297     }
298
299   selection->user_func = func;
300   selection->user_data = data;
301   selection->destroy = destroy;
302 }
303
304 /**
305  * gtk_tree_selection_get_user_data:
306  * @selection: A #GtkTreeSelection.
307  *
308  * Returns the user data for the selection function.
309  *
310  * Return value: The user data.
311  **/
312 gpointer
313 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
314 {
315   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
316
317   return selection->user_data;
318 }
319
320 /**
321  * gtk_tree_selection_get_tree_view:
322  * @selection: A #GtkTreeSelection
323  * 
324  * Returns the tree view associated with @selection.
325  * 
326  * Return value: A #GtkTreeView
327  **/
328 GtkTreeView *
329 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
330 {
331   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
332
333   return selection->tree_view;
334 }
335
336 /**
337  * gtk_tree_selection_get_selected:
338  * @selection: A #GtkTreeSelection.
339  * @model: A pointer to set to the #GtkTreeModel, or NULL.
340  * @iter: The #GtkTreeIter, or NULL.
341  *
342  * Sets @iter to the currently selected node if @selection is set to
343  * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE.  @iter may be NULL if you
344  * just want to test if @selection has any selected nodes.  @model is filled
345  * with the current model as a convenience.  This function will not work if you
346  * use @selection is #GTK_SELECTION_MULTIPLE.
347  *
348  * Return value: TRUE, if there is a selected node.
349  **/
350 gboolean
351 gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
352                                  GtkTreeModel     **model,
353                                  GtkTreeIter       *iter)
354 {
355   GtkRBTree *tree;
356   GtkRBNode *node;
357   GtkTreePath *anchor_path;
358   gboolean retval;
359   gboolean found_node;
360
361   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
362   g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
363   g_return_val_if_fail (selection->tree_view != NULL, FALSE);
364   g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
365
366   if (model)
367     *model = selection->tree_view->priv->model;
368
369   if (selection->tree_view->priv->anchor == NULL)
370     return FALSE;
371
372   anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
373
374   if (anchor_path == NULL)
375     return FALSE;
376
377   retval = FALSE;
378
379   found_node = !_gtk_tree_view_find_node (selection->tree_view,
380                                           anchor_path,
381                                           &tree,
382                                           &node);
383
384   if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
385     {
386       /* we only want to return the anchor if it exists in the rbtree and
387        * is selected.
388        */
389       if (iter == NULL)
390         retval = TRUE;
391       else
392         retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
393                                           iter,
394                                           anchor_path);
395     }
396   else
397     {
398       /* We don't want to return the anchor if it isn't actually selected.
399        */
400       retval = FALSE;
401     }
402
403   gtk_tree_path_free (anchor_path);
404
405   return retval;
406 }
407
408 /**
409  * gtk_tree_selection_get_selected_rows:
410  * @selection: A #GtkTreeSelection.
411  * @model: A pointer to set to the #GtkTreeModel, or NULL.
412  *
413  * Creates a list of path of all selected rows. Additionally, if you are
414  * planning on modifying the model after calling this function, you may
415  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
416  * To do this, you can use gtk_tree_row_reference_new_proxy().
417  *
418  * To free the return value, use:
419  * <informalexample><programlisting>
420  * g_list_foreach (list, gtk_tree_path_free, NULL);
421  * g_list_free (list);
422  * </programlisting></informalexample>
423  *
424  * Return value: A #GList containing a #GtkTreePath for each selected row.
425  *
426  * Since: 2.2
427  **/
428 GList *
429 gtk_tree_selection_get_selected_rows (GtkTreeSelection   *selection,
430                                       GtkTreeModel      **model)
431 {
432   GList *list = NULL;
433   GtkRBTree *tree = NULL;
434   GtkRBNode *node = NULL;
435   GtkTreePath *path;
436
437   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
438   g_return_val_if_fail (selection->tree_view != NULL, NULL);
439   g_return_val_if_fail (selection->tree_view->priv->model != NULL, NULL);
440
441   if (selection->tree_view->priv->tree == NULL ||
442       selection->tree_view->priv->tree->root == NULL)
443     return NULL;
444
445   if (model)
446     *model = selection->tree_view->priv->model;
447
448   if (selection->type == GTK_SELECTION_NONE)
449     return NULL;
450   else if (selection->type != GTK_SELECTION_MULTIPLE)
451     {
452       GtkTreeIter iter;
453
454       if (gtk_tree_selection_get_selected (selection, NULL, &iter))
455         {
456           GtkTreePath *path;
457
458           path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
459           list = g_list_append (list, path);
460
461           return list;
462         }
463
464       return NULL;
465     }
466
467   tree = selection->tree_view->priv->tree;
468   node = selection->tree_view->priv->tree->root;
469
470   while (node->left != tree->nil)
471     node = node->left;
472   path = gtk_tree_path_new_first ();
473
474   do
475     {
476       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
477         list = g_list_append (list, gtk_tree_path_copy (path));
478
479       if (node->children)
480         {
481           tree = node->children;
482           node = tree->root;
483
484           while (node->left != tree->nil)
485             node = node->left;
486
487           gtk_tree_path_append_index (path, 0);
488         }
489       else
490         {
491           gboolean done = FALSE;
492
493           do
494             {
495               node = _gtk_rbtree_next (tree, node);
496               if (node != NULL)
497                 {
498                   done = TRUE;
499                   gtk_tree_path_next (path);
500                 }
501               else
502                 {
503                   node = tree->parent_node;
504                   tree = tree->parent_tree;
505
506                   if (!tree)
507                     {
508                       gtk_tree_path_free (path);
509                       return list;
510                     }
511
512                   gtk_tree_path_up (path);
513                 }
514             }
515           while (!done);
516         }
517     }
518   while (TRUE);
519
520   gtk_tree_path_free (path);
521
522   return list;
523 }
524
525 static void
526 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
527                                                GtkRBNode *node,
528                                                gpointer   data)
529 {
530   gint *count = (gint *)data;
531
532   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
533     (*count)++;
534
535   if (node->children)
536     _gtk_rbtree_traverse (node->children, node->children->root,
537                           G_PRE_ORDER,
538                           gtk_tree_selection_count_selected_rows_helper, data);
539 }
540
541 /**
542  * gtk_tree_selection_count_selected_rows:
543  * @selection: A #GtkTreeSelection.
544  *
545  * Returns the number of rows that have been selected in @tree.
546  *
547  * Return value: The number of rows selected.
548  * 
549  * Since: 2.2
550  **/
551 gint
552 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
553 {
554   gint count = 0;
555   GtkRBTree *tree;
556   GtkRBNode *node;
557
558   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
559   g_return_val_if_fail (selection->tree_view != NULL, 0);
560   g_return_val_if_fail (selection->tree_view->priv->model != NULL, 0);
561
562   if (selection->tree_view->priv->tree == NULL ||
563       selection->tree_view->priv->tree->root == NULL)
564     return 0;
565
566   if (selection->type == GTK_SELECTION_SINGLE ||
567       selection->type == GTK_SELECTION_BROWSE)
568     {
569       if (gtk_tree_selection_get_selected (selection, NULL, NULL))
570         return 1;
571       else
572         return 0;
573     }
574
575   tree = selection->tree_view->priv->tree;
576   node = selection->tree_view->priv->tree->root;
577
578   _gtk_rbtree_traverse (selection->tree_view->priv->tree,
579                         selection->tree_view->priv->tree->root,
580                         G_PRE_ORDER,
581                         gtk_tree_selection_count_selected_rows_helper,
582                         &count);
583
584   return count;
585 }
586
587 /* gtk_tree_selection_selected_foreach helper */
588 static void
589 model_changed (gpointer data)
590 {
591   gboolean *stop = (gboolean *)data;
592
593   *stop = TRUE;
594 }
595
596 /**
597  * gtk_tree_selection_selected_foreach:
598  * @selection: A #GtkTreeSelection.
599  * @func: The function to call for each selected node.
600  * @data: user data to pass to the function.
601  *
602  * Calls a function for each selected node. Note that you cannot modify
603  * the tree or selection from within this function. As a result,
604  * gtk_tree_selection_get_selected_rows() might be more useful.
605  **/
606 void
607 gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
608                                      GtkTreeSelectionForeachFunc  func,
609                                      gpointer                     data)
610 {
611   GtkTreePath *path;
612   GtkRBTree *tree;
613   GtkRBNode *node;
614   GtkTreeIter iter;
615
616   guint inserted_id, deleted_id, reordered_id;
617   gboolean stop = FALSE, has_next = TRUE, has_parent = TRUE;
618
619   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
620   g_return_if_fail (selection->tree_view != NULL);
621   g_return_if_fail (selection->tree_view->priv->model != NULL);
622
623   if (func == NULL ||
624       selection->tree_view->priv->tree == NULL ||
625       selection->tree_view->priv->tree->root == NULL)
626     return;
627
628   if (selection->type == GTK_SELECTION_SINGLE ||
629       selection->type == GTK_SELECTION_BROWSE)
630     {
631       if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
632         {
633           path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
634           gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
635           (* func) (selection->tree_view->priv->model, path, &iter, data);
636           gtk_tree_path_free (path);
637         }
638       return;
639     }
640
641   tree = selection->tree_view->priv->tree;
642   node = selection->tree_view->priv->tree->root;
643   
644   while (node->left != tree->nil)
645     node = node->left;
646
647   /* connect to signals to monitor changes in treemodel */
648   inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
649                                           "row_inserted",
650                                           G_CALLBACK (model_changed),
651                                           &stop);
652   deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
653                                          "row_deleted",
654                                          G_CALLBACK (model_changed),
655                                          &stop);
656   reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
657                                            "rows_reordered",
658                                            G_CALLBACK (model_changed),
659                                            &stop);
660
661   /* find the node internally */
662   path = gtk_tree_path_new_first ();
663   gtk_tree_model_get_iter (selection->tree_view->priv->model,
664                            &iter, path);
665
666   do
667     {
668       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
669         (* func) (selection->tree_view->priv->model, path, &iter, data);
670
671       if (stop)
672         goto out;
673
674       if (node->children)
675         {
676           gboolean has_child;
677           GtkTreeIter tmp;
678
679           tree = node->children;
680           node = tree->root;
681           while (node->left != tree->nil)
682             node = node->left;
683           tmp = iter;
684           has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
685           gtk_tree_path_append_index (path, 0);
686
687           /* we do the sanity check at the bottom of this function */
688           if (!has_child)
689             goto out;
690         }
691       else
692         {
693           gboolean done = FALSE;
694           do
695             {
696               node = _gtk_rbtree_next (tree, node);
697               if (node != NULL)
698                 {
699                   gboolean has_next;
700
701                   has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
702                   done = TRUE;
703                   gtk_tree_path_next (path);
704
705                   /* we do the sanity check at the bottom of this function */
706                   if (!has_next)
707                     goto out;
708                 }
709               else
710                 {
711                   gboolean has_parent;
712                   GtkTreeIter tmp_iter = iter;
713
714                   node = tree->parent_node;
715                   tree = tree->parent_tree;
716                   if (tree == NULL)
717                     {
718                       /* we've run out of tree */
719                       /* We're done with this function */
720
721                       goto out;
722                     }
723
724                   has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
725                   gtk_tree_path_up (path);
726
727                   /* we do the sanity check at the bottom of this function */
728                   if (!has_parent)
729                     goto out;
730                 }
731             }
732           while (!done);
733         }
734     }
735   while (TRUE);
736
737 out:
738   if (path)
739     gtk_tree_path_free (path);
740
741   g_signal_handler_disconnect (selection->tree_view->priv->model,
742                                inserted_id);
743   g_signal_handler_disconnect (selection->tree_view->priv->model,
744                                deleted_id);
745   g_signal_handler_disconnect (selection->tree_view->priv->model,
746                                reordered_id);
747
748   /* check if we have to spew a scary message */
749   if (!has_next)
750     TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
751   if (!has_parent)
752     TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
753   if (stop)
754     g_warning
755       ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
756        "This function is for observing the selections of the tree only.  If\n"
757        "you are trying to get all selected items from the tree, try using\n"
758        "gtk_tree_selection_get_selected_rows instead.\n");
759 }
760
761 /**
762  * gtk_tree_selection_select_path:
763  * @selection: A #GtkTreeSelection.
764  * @path: The #GtkTreePath to be selected.
765  *
766  * Select the row at @path.
767  **/
768 void
769 gtk_tree_selection_select_path (GtkTreeSelection *selection,
770                                 GtkTreePath      *path)
771 {
772   GtkRBNode *node;
773   GtkRBTree *tree;
774   GdkModifierType state = 0;
775   gboolean ret;
776
777   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
778   g_return_if_fail (selection->tree_view != NULL);
779   g_return_if_fail (path != NULL);
780
781   ret = _gtk_tree_view_find_node (selection->tree_view,
782                                   path,
783                                   &tree,
784                                   &node);
785
786   if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
787       ret == TRUE)
788     return;
789
790   if (selection->type == GTK_SELECTION_MULTIPLE)
791     state = GDK_CONTROL_MASK;
792
793   _gtk_tree_selection_internal_select_node (selection,
794                                             node,
795                                             tree,
796                                             path,
797                                             state,
798                                             FALSE);
799 }
800
801 /**
802  * gtk_tree_selection_unselect_path:
803  * @selection: A #GtkTreeSelection.
804  * @path: The #GtkTreePath to be unselected.
805  *
806  * Unselects the row at @path.
807  **/
808 void
809 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
810                                   GtkTreePath      *path)
811 {
812   GtkRBNode *node;
813   GtkRBTree *tree;
814   gboolean ret;
815
816   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
817   g_return_if_fail (selection->tree_view != NULL);
818   g_return_if_fail (path != NULL);
819
820   ret = _gtk_tree_view_find_node (selection->tree_view,
821                                   path,
822                                   &tree,
823                                   &node);
824
825   if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
826       ret == TRUE)
827     return;
828
829   _gtk_tree_selection_internal_select_node (selection,
830                                             node,
831                                             tree,
832                                             path,
833                                             GDK_CONTROL_MASK,
834                                             TRUE);
835 }
836
837 /**
838  * gtk_tree_selection_select_iter:
839  * @selection: A #GtkTreeSelection.
840  * @iter: The #GtkTreeIter to be selected.
841  *
842  * Selects the specified iterator.
843  **/
844 void
845 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
846                                 GtkTreeIter      *iter)
847 {
848   GtkTreePath *path;
849
850   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
851   g_return_if_fail (selection->tree_view != NULL);
852   g_return_if_fail (selection->tree_view->priv->model != NULL);
853   g_return_if_fail (iter != NULL);
854
855   path = gtk_tree_model_get_path (selection->tree_view->priv->model,
856                                   iter);
857
858   if (path == NULL)
859     return;
860
861   gtk_tree_selection_select_path (selection, path);
862   gtk_tree_path_free (path);
863 }
864
865
866 /**
867  * gtk_tree_selection_unselect_iter:
868  * @selection: A #GtkTreeSelection.
869  * @iter: The #GtkTreeIter to be unselected.
870  *
871  * Unselects the specified iterator.
872  **/
873 void
874 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
875                                   GtkTreeIter      *iter)
876 {
877   GtkTreePath *path;
878
879   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
880   g_return_if_fail (selection->tree_view != NULL);
881   g_return_if_fail (selection->tree_view->priv->model != NULL);
882   g_return_if_fail (iter != NULL);
883
884   path = gtk_tree_model_get_path (selection->tree_view->priv->model,
885                                   iter);
886
887   if (path == NULL)
888     return;
889
890   gtk_tree_selection_unselect_path (selection, path);
891   gtk_tree_path_free (path);
892 }
893
894 /**
895  * gtk_tree_selection_path_is_selected:
896  * @selection: A #GtkTreeSelection.
897  * @path: A #GtkTreePath to check selection on.
898  * 
899  * Returns %TRUE if the row pointed to by @path is currently selected.  If @path
900  * does not point to a valid location, %FALSE is returned
901  * 
902  * Return value: %TRUE if @path is selected.
903  **/
904 gboolean
905 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
906                                      GtkTreePath      *path)
907 {
908   GtkRBNode *node;
909   GtkRBTree *tree;
910   gboolean ret;
911
912   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
913   g_return_val_if_fail (path != NULL, FALSE);
914   g_return_val_if_fail (selection->tree_view != NULL, FALSE);
915   g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
916
917   ret = _gtk_tree_view_find_node (selection->tree_view,
918                                   path,
919                                   &tree,
920                                   &node);
921
922   if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
923       ret == TRUE)
924     return FALSE;
925
926   return TRUE;
927 }
928
929 /**
930  * gtk_tree_selection_iter_is_selected:
931  * @selection: A #GtkTreeSelection
932  * @iter: A valid #GtkTreeIter
933  * 
934  * Returns %TRUE if the row at @iter is currently selected.
935  * 
936  * Return value: %TRUE, if @iter is selected
937  **/
938 gboolean
939 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
940                                      GtkTreeIter      *iter)
941 {
942   GtkTreePath *path;
943   gboolean retval;
944
945   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
946   g_return_val_if_fail (iter != NULL, FALSE);
947   g_return_val_if_fail (selection->tree_view != NULL, FALSE);
948   g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
949
950   path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
951   if (path == NULL)
952     return FALSE;
953
954   retval = gtk_tree_selection_path_is_selected (selection, path);
955   gtk_tree_path_free (path);
956
957   return retval;
958 }
959
960
961 /* Wish I was in python, right now... */
962 struct _TempTuple {
963   GtkTreeSelection *selection;
964   gint dirty;
965 };
966
967 static void
968 select_all_helper (GtkRBTree  *tree,
969                    GtkRBNode  *node,
970                    gpointer    data)
971 {
972   struct _TempTuple *tuple = data;
973
974   if (node->children)
975     _gtk_rbtree_traverse (node->children,
976                           node->children->root,
977                           G_PRE_ORDER,
978                           select_all_helper,
979                           data);
980   if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
981     {
982       tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
983     }
984 }
985
986
987 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
988  * can use it in other places without fear of the signal being emitted.
989  */
990 static gint
991 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
992 {
993   struct _TempTuple *tuple;
994
995   if (selection->tree_view->priv->tree == NULL)
996     return FALSE;
997
998   /* Mark all nodes selected */
999   tuple = g_new (struct _TempTuple, 1);
1000   tuple->selection = selection;
1001   tuple->dirty = FALSE;
1002
1003   _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1004                         selection->tree_view->priv->tree->root,
1005                         G_PRE_ORDER,
1006                         select_all_helper,
1007                         tuple);
1008   if (tuple->dirty)
1009     {
1010       g_free (tuple);
1011       return TRUE;
1012     }
1013   g_free (tuple);
1014   return FALSE;
1015 }
1016
1017 /**
1018  * gtk_tree_selection_select_all:
1019  * @selection: A #GtkTreeSelection.
1020  *
1021  * Selects all the nodes.  @selection is must be set to #GTK_SELECTION_MULTIPLE
1022  * mode.
1023  **/
1024 void
1025 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1026 {
1027   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1028   g_return_if_fail (selection->tree_view != NULL);
1029   if (selection->tree_view->priv->tree == NULL)
1030     return;
1031   g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1032
1033   if (gtk_tree_selection_real_select_all (selection))
1034     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1035 }
1036
1037 static void
1038 unselect_all_helper (GtkRBTree  *tree,
1039                      GtkRBNode  *node,
1040                      gpointer    data)
1041 {
1042   struct _TempTuple *tuple = data;
1043
1044   if (node->children)
1045     _gtk_rbtree_traverse (node->children,
1046                           node->children->root,
1047                           G_PRE_ORDER,
1048                           unselect_all_helper,
1049                           data);
1050   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1051     {
1052       tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1053     }
1054 }
1055
1056 static gint
1057 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1058 {
1059   struct _TempTuple *tuple;
1060
1061   if (selection->type == GTK_SELECTION_SINGLE ||
1062       selection->type == GTK_SELECTION_BROWSE)
1063     {
1064       GtkRBTree *tree = NULL;
1065       GtkRBNode *node = NULL;
1066       GtkTreePath *anchor_path;
1067
1068       if (selection->tree_view->priv->anchor == NULL)
1069         return FALSE;
1070
1071       anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1072
1073       if (anchor_path == NULL)
1074         return FALSE;
1075
1076       _gtk_tree_view_find_node (selection->tree_view,
1077                                 anchor_path,
1078                                 &tree,
1079                                 &node);
1080
1081       gtk_tree_path_free (anchor_path);
1082
1083       if (tree == NULL)
1084         return FALSE;
1085
1086       if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1087         {
1088           if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1089             {
1090               gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1091               selection->tree_view->priv->anchor = NULL;
1092               return TRUE;
1093             }
1094         }
1095       return FALSE;
1096     }
1097   else
1098     {
1099       tuple = g_new (struct _TempTuple, 1);
1100       tuple->selection = selection;
1101       tuple->dirty = FALSE;
1102
1103       _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1104                             selection->tree_view->priv->tree->root,
1105                             G_PRE_ORDER,
1106                             unselect_all_helper,
1107                             tuple);
1108
1109       if (tuple->dirty)
1110         {
1111           g_free (tuple);
1112           return TRUE;
1113         }
1114       g_free (tuple);
1115       return FALSE;
1116     }
1117 }
1118
1119 /**
1120  * gtk_tree_selection_unselect_all:
1121  * @selection: A #GtkTreeSelection.
1122  *
1123  * Unselects all the nodes.
1124  **/
1125 void
1126 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1127 {
1128   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1129   g_return_if_fail (selection->tree_view != NULL);
1130   if (selection->tree_view->priv->tree == NULL)
1131     return;
1132   
1133   if (selection->tree_view->priv->tree == NULL)
1134     return;
1135
1136   if (gtk_tree_selection_real_unselect_all (selection))
1137     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1138 }
1139
1140 enum
1141 {
1142   RANGE_SELECT,
1143   RANGE_UNSELECT
1144 };
1145
1146 static gint
1147 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1148                                       gint              mode,
1149                                       GtkTreePath      *start_path,
1150                                       GtkTreePath      *end_path)
1151 {
1152   GtkRBNode *start_node, *end_node;
1153   GtkRBTree *start_tree, *end_tree;
1154   GtkTreePath *anchor_path = NULL;
1155   gboolean dirty = FALSE;
1156
1157   switch (gtk_tree_path_compare (start_path, end_path))
1158     {
1159     case 1:
1160       _gtk_tree_view_find_node (selection->tree_view,
1161                                 end_path,
1162                                 &start_tree,
1163                                 &start_node);
1164       _gtk_tree_view_find_node (selection->tree_view,
1165                                 start_path,
1166                                 &end_tree,
1167                                 &end_node);
1168       anchor_path = start_path;
1169       break;
1170     case 0:
1171       _gtk_tree_view_find_node (selection->tree_view,
1172                                 start_path,
1173                                 &start_tree,
1174                                 &start_node);
1175       end_tree = start_tree;
1176       end_node = start_node;
1177       anchor_path = start_path;
1178       break;
1179     case -1:
1180       _gtk_tree_view_find_node (selection->tree_view,
1181                                 start_path,
1182                                 &start_tree,
1183                                 &start_node);
1184       _gtk_tree_view_find_node (selection->tree_view,
1185                                 end_path,
1186                                 &end_tree,
1187                                 &end_node);
1188       anchor_path = start_path;
1189       break;
1190     }
1191
1192   g_return_val_if_fail (start_node != NULL, FALSE);
1193   g_return_val_if_fail (end_node != NULL, FALSE);
1194
1195   if (anchor_path)
1196     {
1197       if (selection->tree_view->priv->anchor)
1198         gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1199
1200       selection->tree_view->priv->anchor =
1201         gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1202                                           selection->tree_view->priv->model,
1203                                           anchor_path);
1204     }
1205
1206   do
1207     {
1208       dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1209
1210       if (start_node == end_node)
1211         break;
1212
1213       if (start_node->children)
1214         {
1215           start_tree = start_node->children;
1216           start_node = start_tree->root;
1217           while (start_node->left != start_tree->nil)
1218             start_node = start_node->left;
1219         }
1220       else
1221         {
1222           _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1223           if (start_tree == NULL)
1224             {
1225               /* we just ran out of tree.  That means someone passed in bogus values.
1226                */
1227               return dirty;
1228             }
1229         }
1230     }
1231   while (TRUE);
1232
1233   return dirty;
1234 }
1235
1236 /**
1237  * gtk_tree_selection_select_range:
1238  * @selection: A #GtkTreeSelection.
1239  * @start_path: The initial node of the range.
1240  * @end_path: The final node of the range.
1241  *
1242  * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1243  **/
1244 void
1245 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1246                                  GtkTreePath      *start_path,
1247                                  GtkTreePath      *end_path)
1248 {
1249   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1250   g_return_if_fail (selection->tree_view != NULL);
1251
1252   if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1253     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1254 }
1255
1256 /**
1257  * gtk_tree_selection_unselect_range:
1258  * @selection: A #GtkTreeSelection.
1259  * @start_path: The initial node of the range.
1260  * @end_path: The initial node of the range.
1261  *
1262  * Unselects a range of nodes, determined by @start_path and @end_path
1263  * inclusive.
1264  *
1265  * Since: 2.2
1266  **/
1267 void
1268 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1269                                    GtkTreePath      *start_path,
1270                                    GtkTreePath      *end_path)
1271 {
1272   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1273   g_return_if_fail (selection->tree_view != NULL);
1274
1275   if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1276     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1277 }
1278
1279 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1280  */
1281
1282 /*
1283  * docs about the 'override_browse_mode', we set this flag when we want to
1284  * unset select the node and override the select browse mode behaviour (that is
1285  * 'one node should *always* be selected').
1286  */
1287 void
1288 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1289                                           GtkRBNode        *node,
1290                                           GtkRBTree        *tree,
1291                                           GtkTreePath      *path,
1292                                           GdkModifierType   state,
1293                                           gboolean          override_browse_mode)
1294 {
1295   gint flags;
1296   gint dirty = FALSE;
1297   GtkTreePath *anchor_path = NULL;
1298
1299   if (selection->type == GTK_SELECTION_NONE)
1300     return;
1301
1302   if (selection->tree_view->priv->anchor)
1303     anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1304
1305   if (selection->type == GTK_SELECTION_SINGLE ||
1306       selection->type == GTK_SELECTION_BROWSE)
1307     {
1308       /* just unselect */
1309       if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1310         {
1311           dirty = gtk_tree_selection_real_unselect_all (selection);
1312         }
1313       /* Did we try to select the same node again? */
1314       else if (selection->type == GTK_SELECTION_SINGLE &&
1315                anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1316         {
1317           if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1318             {
1319               dirty = gtk_tree_selection_real_unselect_all (selection);
1320             }
1321         }
1322       else
1323         {
1324           if (anchor_path)
1325             {
1326               /* We only want to select the new node if we can unselect the old one,
1327                * and we can select the new one. */
1328               if (selection->user_func)
1329                 {
1330                   if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1331                                                GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1332                                                selection->user_data))
1333                     dirty = TRUE;
1334                 }
1335               else
1336                 {
1337                   dirty = TRUE;
1338                 }
1339
1340               /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1341                * unselect the new one
1342                */
1343               if (dirty)
1344                 dirty = gtk_tree_selection_real_unselect_all (selection);
1345
1346               /* if dirty is TRUE at this point, we successfully unselected the
1347                * old one, and can then select the new one */
1348               if (dirty)
1349                 {
1350                   if (selection->tree_view->priv->anchor)
1351                     {
1352                       gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1353                       selection->tree_view->priv->anchor = NULL;
1354                     }
1355
1356                   if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1357                     {
1358                       selection->tree_view->priv->anchor =
1359                         gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1360                     }
1361                 }
1362             }
1363           else
1364             {
1365               if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1366                 {
1367                   dirty = TRUE;
1368                   if (selection->tree_view->priv->anchor)
1369                     gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1370
1371                   selection->tree_view->priv->anchor =
1372                     gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1373                 }
1374             }
1375         }
1376     }
1377   else if (selection->type == GTK_SELECTION_MULTIPLE)
1378     {
1379       if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
1380         {
1381           if (selection->tree_view->priv->anchor)
1382             gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1383
1384           selection->tree_view->priv->anchor =
1385             gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1386           dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1387         }
1388       else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
1389         {
1390           gtk_tree_selection_select_range (selection,
1391                                            anchor_path,
1392                                            path);
1393         }
1394       else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1395         {
1396           flags = node->flags;
1397           if (selection->tree_view->priv->anchor)
1398             gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1399
1400           selection->tree_view->priv->anchor =
1401             gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1402
1403           if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1404             dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1405           else
1406             dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1407         }
1408       else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
1409         {
1410           dirty = gtk_tree_selection_real_unselect_all (selection);
1411           dirty |= gtk_tree_selection_real_modify_range (selection,
1412                                                          RANGE_SELECT,
1413                                                          anchor_path,
1414                                                          path);
1415         }
1416       else
1417         {
1418           dirty = gtk_tree_selection_real_unselect_all (selection);
1419
1420           if (selection->tree_view->priv->anchor)
1421             gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1422
1423           selection->tree_view->priv->anchor =
1424             gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1425
1426           dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1427         }
1428     }
1429
1430   if (anchor_path)
1431     gtk_tree_path_free (anchor_path);
1432
1433   if (dirty)
1434     g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1435 }
1436
1437 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1438  */
1439
1440 static gint
1441 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1442                                      GtkRBTree        *tree,
1443                                      GtkRBNode        *node,
1444                                      gboolean          select)
1445 {
1446   gboolean selected = FALSE;
1447   GtkTreePath *path = NULL;
1448
1449   select = !! select;
1450
1451   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1452     {
1453       path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1454       if (selection->user_func)
1455         {
1456           if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1457                                        GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1458                                        selection->user_data))
1459             selected = TRUE;
1460         }
1461       else
1462         selected = TRUE;
1463       gtk_tree_path_free (path);
1464     }
1465
1466   if (selected == TRUE)
1467     {
1468       node->flags ^= GTK_RBNODE_IS_SELECTED;
1469
1470       _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);
1471       
1472       return TRUE;
1473     }
1474
1475   return FALSE;
1476 }
1477