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