2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
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.
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.
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.
20 #include "gtktreeselection.h"
21 #include "gtktreeprivate.h"
22 #include "gtkrbtree.h"
23 #include "gtkmarshalers.h"
24 #include "gtksignal.h"
26 static void gtk_tree_selection_init (GtkTreeSelection *selection);
27 static void gtk_tree_selection_class_init (GtkTreeSelectionClass *class);
29 static void gtk_tree_selection_finalize (GObject *object);
30 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
31 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
32 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
43 static GObjectClass *parent_class = NULL;
44 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
47 gtk_tree_selection_get_type (void)
49 static GtkType selection_type = 0;
53 static const GTypeInfo selection_info =
55 sizeof (GtkTreeSelectionClass),
57 NULL, /* base_finalize */
58 (GClassInitFunc) gtk_tree_selection_class_init,
59 NULL, /* class_finalize */
60 NULL, /* class_data */
61 sizeof (GtkTreeSelection),
63 (GInstanceInitFunc) gtk_tree_selection_init
66 selection_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection", &selection_info, 0);
69 return selection_type;
73 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
75 GObjectClass *object_class;
77 object_class = (GObjectClass*) class;
78 parent_class = g_type_class_peek_parent (class);
80 object_class->finalize = gtk_tree_selection_finalize;
81 class->changed = NULL;
83 tree_selection_signals[CHANGED] =
84 gtk_signal_new ("changed",
86 GTK_CLASS_TYPE (object_class),
87 GTK_SIGNAL_OFFSET (GtkTreeSelectionClass, changed),
88 _gtk_marshal_VOID__VOID,
93 gtk_tree_selection_init (GtkTreeSelection *selection)
95 selection->type = GTK_SELECTION_SINGLE;
99 gtk_tree_selection_finalize (GObject *object)
101 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
103 if (selection->destroy)
105 GtkDestroyNotify d = selection->destroy;
107 selection->destroy = NULL;
108 d (selection->user_data);
111 /* chain parent_class' handler */
112 G_OBJECT_CLASS (parent_class)->finalize (object);
116 * _gtk_tree_selection_new:
118 * Creates a new #GtkTreeSelection object. This function should not be invoked,
119 * as each #GtkTreeView will create it's own #GtkTreeSelection.
121 * Return value: A newly created #GtkTreeSelection object.
124 _gtk_tree_selection_new (void)
126 GtkTreeSelection *selection;
128 selection = GTK_TREE_SELECTION (g_object_new (GTK_TYPE_TREE_SELECTION, NULL));
134 * _gtk_tree_selection_new_with_tree_view:
135 * @tree_view: The #GtkTreeView.
137 * Creates a new #GtkTreeSelection object. This function should not be invoked,
138 * as each #GtkTreeView will create it's own #GtkTreeSelection.
140 * Return value: A newly created #GtkTreeSelection object.
143 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
145 GtkTreeSelection *selection;
147 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
149 selection = _gtk_tree_selection_new ();
150 _gtk_tree_selection_set_tree_view (selection, tree_view);
156 * _gtk_tree_selection_set_tree_view:
157 * @selection: A #GtkTreeSelection.
158 * @tree_view: The #GtkTreeView.
160 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
161 * it is used internally by #GtkTreeView.
164 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
165 GtkTreeView *tree_view)
167 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
168 if (tree_view != NULL)
169 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
171 selection->tree_view = tree_view;
175 * gtk_tree_selection_set_mode:
176 * @selection: A #GtkTreeSelection.
177 * @type: The selection mode
179 * Sets the selection mode of the @selection. If the previous type was
180 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
181 * previously selected.
184 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
185 GtkSelectionMode type)
187 GtkTreeSelectionFunc tmp_func;
188 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
190 if (selection->type == type)
194 if (type == GTK_SELECTION_NONE)
196 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
197 /* We do this so that we unconditionally unset all rows
199 tmp_func = selection->user_func;
200 selection->user_func = NULL;
201 gtk_tree_selection_unselect_all (selection);
202 selection->user_func = tmp_func;
204 else if (type == GTK_SELECTION_SINGLE ||
205 type == GTK_SELECTION_BROWSE)
207 GtkRBTree *tree = NULL;
208 GtkRBNode *node = NULL;
209 gint selected = FALSE;
210 GtkTreePath *anchor_path = NULL;
212 if (selection->tree_view->priv->anchor)
214 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
218 _gtk_tree_view_find_node (selection->tree_view,
223 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
228 /* We do this so that we unconditionally unset all rows
230 tmp_func = selection->user_func;
231 selection->user_func = NULL;
232 gtk_tree_selection_unselect_all (selection);
233 selection->user_func = tmp_func;
235 if (node && selected)
236 _gtk_tree_selection_internal_select_node (selection,
242 gtk_tree_path_free (anchor_path);
245 selection->type = type;
249 * gtk_tree_selection_get_mode:
250 * @selection: a #GtkTreeSelection
252 * Gets the selection mode for @selection. See
253 * gtk_tree_selection_set_mode().
255 * Return value: the current selection mode
258 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
260 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
262 return selection->type;
266 * gtk_tree_selection_set_select_function:
267 * @selection: A #GtkTreeSelection.
268 * @func: The selection function.
269 * @data: The selection function's data.
270 * @destroy: The destroy function for user data. May be NULL.
272 * Sets the selection function. If set, this function is called before any node
273 * is selected or unselected, giving some control over which nodes are selected.
274 * The select function should return %TRUE if the state of the node may be toggled,
275 * and %FALSE if the state of the node should be left unchanged.
278 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
279 GtkTreeSelectionFunc func,
281 GtkDestroyNotify destroy)
283 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
284 g_return_if_fail (func != NULL);
286 if (selection->destroy)
288 GtkDestroyNotify d = selection->destroy;
290 selection->destroy = NULL;
291 d (selection->user_data);
294 selection->user_func = func;
295 selection->user_data = data;
296 selection->destroy = destroy;
300 * gtk_tree_selection_get_user_data:
301 * @selection: A #GtkTreeSelection.
303 * Returns the user data for the selection function.
305 * Return value: The user data.
308 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
310 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
312 return selection->user_data;
316 * gtk_tree_selection_get_tree_view:
317 * @selection: A #GtkTreeSelection
319 * Returns the tree view associated with @selection.
321 * Return value: A #GtkTreeView
324 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
326 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
328 return selection->tree_view;
332 * gtk_tree_selection_get_selected:
333 * @selection: A #GtkTreeSelection.
334 * @model: A pointer set to the #GtkTreeModel, or NULL.
335 * @iter: The #GtkTreeIter, or NULL.
337 * Sets @iter to the currently selected node if @selection is set to
338 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
339 * just want to test if @selection has any selected nodes. @model is filled
340 * with the current model as a convenience. This function will not work if you
341 * use @selection is #GTK_SELECTION_MULTIPLE.
343 * Return value: TRUE, if there is a selected node.
346 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
347 GtkTreeModel **model,
352 GtkTreePath *anchor_path;
355 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
356 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
357 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
358 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
361 *model = selection->tree_view->priv->model;
363 if (selection->tree_view->priv->anchor == NULL)
366 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
368 if (anchor_path == NULL)
373 gtk_tree_path_free (anchor_path);
379 if (!_gtk_tree_view_find_node (selection->tree_view,
383 ! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
385 /* We don't want to return the anchor if it isn't actually selected.
391 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
396 gtk_tree_path_free (anchor_path);
402 * gtk_tree_selection_selected_foreach:
403 * @selection: A #GtkTreeSelection.
404 * @func: The function to call for each selected node.
405 * @data: user data to pass to the function.
407 * Calls a function for each selected node.
410 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
411 GtkTreeSelectionForeachFunc func,
419 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
420 g_return_if_fail (selection->tree_view != NULL);
421 g_return_if_fail (selection->tree_view->priv->model != NULL);
424 selection->tree_view->priv->tree == NULL ||
425 selection->tree_view->priv->tree->root == NULL)
428 if (selection->type == GTK_SELECTION_SINGLE ||
429 selection->type == GTK_SELECTION_BROWSE)
431 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
433 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
434 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
435 (* func) (selection->tree_view->priv->model, path, &iter, data);
436 gtk_tree_path_free (path);
441 tree = selection->tree_view->priv->tree;
442 node = selection->tree_view->priv->tree->root;
444 while (node->left != tree->nil)
447 /* find the node internally */
448 path = gtk_tree_path_new_first ();
449 gtk_tree_model_get_iter (selection->tree_view->priv->model,
454 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
455 (* func) (selection->tree_view->priv->model, path, &iter, data);
461 tree = node->children;
463 while (node->left != tree->nil)
466 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
467 gtk_tree_path_append_index (path, 0);
469 TREE_VIEW_INTERNAL_ASSERT_VOID (has_child);
473 gboolean done = FALSE;
476 node = _gtk_rbtree_next (tree, node);
481 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
483 gtk_tree_path_next (path);
486 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
491 GtkTreeIter tmp_iter = iter;
493 node = tree->parent_node;
494 tree = tree->parent_tree;
497 gtk_tree_path_free (path);
498 /* we've run out of tree */
499 /* We're done with this function */
502 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
503 gtk_tree_path_up (path);
505 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
515 * gtk_tree_selection_select_path:
516 * @selection: A #GtkTreeSelection.
517 * @path: The #GtkTreePath to be selected.
519 * Select the row at @path.
522 gtk_tree_selection_select_path (GtkTreeSelection *selection,
527 GdkModifierType state = 0;
529 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
530 g_return_if_fail (selection->tree_view != NULL);
531 g_return_if_fail (path != NULL);
533 _gtk_tree_view_find_node (selection->tree_view,
538 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
541 if (selection->type == GTK_SELECTION_MULTIPLE)
542 state = GDK_CONTROL_MASK;
544 _gtk_tree_selection_internal_select_node (selection,
552 * gtk_tree_selection_unselect_path:
553 * @selection: A #GtkTreeSelection.
554 * @path: The #GtkTreePath to be unselected.
556 * Unselects the row at @path.
559 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
565 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
566 g_return_if_fail (selection->tree_view != NULL);
567 g_return_if_fail (path != NULL);
569 _gtk_tree_view_find_node (selection->tree_view,
574 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
577 _gtk_tree_selection_internal_select_node (selection,
585 * gtk_tree_selection_select_iter:
586 * @selection: A #GtkTreeSelection.
587 * @iter: The #GtkTreeIter to be selected.
589 * Selects the specified iterator.
592 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
597 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
598 g_return_if_fail (selection->tree_view != NULL);
599 g_return_if_fail (selection->tree_view->priv->model != NULL);
600 g_return_if_fail (iter != NULL);
602 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
608 gtk_tree_selection_select_path (selection, path);
609 gtk_tree_path_free (path);
614 * gtk_tree_selection_unselect_iter:
615 * @selection: A #GtkTreeSelection.
616 * @iter: The #GtkTreeIter to be unselected.
618 * Unselects the specified iterator.
621 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
626 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
627 g_return_if_fail (selection->tree_view != NULL);
628 g_return_if_fail (selection->tree_view->priv->model != NULL);
629 g_return_if_fail (iter != NULL);
631 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
637 gtk_tree_selection_select_path (selection, path);
638 gtk_tree_path_free (path);
642 * gtk_tree_selection_path_is_selected:
643 * @selection: A #GtkTreeSelection.
644 * @path: A #GtkTreePath to check selection on.
646 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
647 * does not point to a valid location, %FALSE is returned
649 * Return value: %TRUE if @path is selected.
652 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
658 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
659 g_return_val_if_fail (path != NULL, FALSE);
660 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
661 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
663 _gtk_tree_view_find_node (selection->tree_view,
668 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
675 * gtk_tree_selection_iter_is_selected:
676 * @selection: A #GtkTreeSelection
677 * @iter: A valid #GtkTreeIter
679 * Returns %TRUE if the row pointed to by @path is currently selected.
681 * Return value: %TRUE, if @iter is selected
684 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
690 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
691 g_return_val_if_fail (iter != NULL, FALSE);
692 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
693 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
695 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
699 retval = gtk_tree_selection_path_is_selected (selection, path);
700 gtk_tree_path_free (path);
706 /* Wish I was in python, right now... */
708 GtkTreeSelection *selection;
713 select_all_helper (GtkRBTree *tree,
717 struct _TempTuple *tuple = data;
720 _gtk_rbtree_traverse (node->children,
721 node->children->root,
725 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
727 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
732 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
733 * can use it in other places without fear of the signal being emitted.
736 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
738 struct _TempTuple *tuple;
740 if (selection->tree_view->priv->tree == NULL)
743 /* Mark all nodes selected */
744 tuple = g_new (struct _TempTuple, 1);
745 tuple->selection = selection;
746 tuple->dirty = FALSE;
748 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
749 selection->tree_view->priv->tree->root,
763 * gtk_tree_selection_select_all:
764 * @selection: A #GtkTreeSelection.
766 * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
770 gtk_tree_selection_select_all (GtkTreeSelection *selection)
772 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
773 g_return_if_fail (selection->tree_view != NULL);
774 if (selection->tree_view->priv->tree == NULL)
776 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
778 if (gtk_tree_selection_real_select_all (selection))
779 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
783 unselect_all_helper (GtkRBTree *tree,
787 struct _TempTuple *tuple = data;
790 _gtk_rbtree_traverse (node->children,
791 node->children->root,
795 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
797 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
802 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
804 struct _TempTuple *tuple;
806 if (selection->type == GTK_SELECTION_SINGLE ||
807 selection->type == GTK_SELECTION_BROWSE)
809 GtkRBTree *tree = NULL;
810 GtkRBNode *node = NULL;
811 GtkTreePath *anchor_path;
813 if (selection->tree_view->priv->anchor == NULL)
816 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
818 if (anchor_path == NULL)
821 _gtk_tree_view_find_node (selection->tree_view,
826 gtk_tree_path_free (anchor_path);
831 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
833 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
835 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
836 selection->tree_view->priv->anchor = NULL;
844 tuple = g_new (struct _TempTuple, 1);
845 tuple->selection = selection;
846 tuple->dirty = FALSE;
848 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
849 selection->tree_view->priv->tree->root,
865 * gtk_tree_selection_unselect_all:
866 * @selection: A #GtkTreeSelection.
868 * Unselects all the nodes.
871 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
873 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
874 g_return_if_fail (selection->tree_view != NULL);
875 if (selection->tree_view->priv->tree == NULL)
878 if (selection->tree_view->priv->tree == NULL)
881 if (gtk_tree_selection_real_unselect_all (selection))
882 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
886 gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
887 GtkTreePath *start_path,
888 GtkTreePath *end_path)
890 GtkRBNode *start_node, *end_node;
891 GtkRBTree *start_tree, *end_tree;
892 gboolean dirty = FALSE;
894 switch (gtk_tree_path_compare (start_path, end_path))
897 _gtk_tree_view_find_node (selection->tree_view,
901 _gtk_tree_view_find_node (selection->tree_view,
907 _gtk_tree_view_find_node (selection->tree_view,
911 end_tree = start_tree;
912 end_node = start_node;
915 _gtk_tree_view_find_node (selection->tree_view,
919 _gtk_tree_view_find_node (selection->tree_view,
926 g_return_val_if_fail (start_node != NULL, FALSE);
927 g_return_val_if_fail (end_node != NULL, FALSE);
931 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, TRUE);
933 if (start_node == end_node)
936 if (start_node->children)
938 start_tree = start_node->children;
939 start_node = start_tree->root;
940 while (start_node->left != start_tree->nil)
941 start_node = start_node->left;
945 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
946 if (start_tree == NULL)
948 /* we just ran out of tree. That means someone passed in bogus values.
960 * gtk_tree_selection_select_range:
961 * @selection: A #GtkTreeSelection.
962 * @start_path: The initial node of the range.
963 * @end_path: The final node of the range.
965 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
968 gtk_tree_selection_select_range (GtkTreeSelection *selection,
969 GtkTreePath *start_path,
970 GtkTreePath *end_path)
972 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
973 g_return_if_fail (selection->tree_view != NULL);
975 if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
976 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
979 /* Called internally by gtktreeview.c It handles actually selecting the tree.
982 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
986 GdkModifierType state)
990 GtkTreePath *anchor_path = NULL;
992 if (selection->type == GTK_SELECTION_NONE)
995 if (selection->tree_view->priv->anchor)
996 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
998 if (selection->type == GTK_SELECTION_SINGLE ||
999 selection->type == GTK_SELECTION_BROWSE)
1001 /* Did we try to select the same node again? */
1002 if (selection->type == GTK_SELECTION_SINGLE &&
1003 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1005 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1007 dirty = gtk_tree_selection_real_unselect_all (selection);
1014 /* We only want to select the new node if we can unselect the old one,
1015 * and we can select the new one. */
1016 if (selection->user_func)
1018 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1019 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1020 selection->user_data))
1028 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1029 * unselect the new one
1032 dirty = gtk_tree_selection_real_unselect_all (selection);
1034 /* if dirty is TRUE at this point, we successfully unselected the
1035 * old one, and can then select the new one */
1038 if (selection->tree_view->priv->anchor)
1039 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1040 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1042 selection->tree_view->priv->anchor =
1043 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1049 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1052 selection->tree_view->priv->anchor =
1053 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1058 else if (selection->type == GTK_SELECTION_MULTIPLE)
1060 if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
1062 if (selection->tree_view->priv->anchor)
1063 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1065 selection->tree_view->priv->anchor =
1066 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1067 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1069 else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
1071 gtk_tree_selection_select_range (selection,
1075 else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1077 flags = node->flags;
1078 if (selection->tree_view->priv->anchor)
1079 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1081 selection->tree_view->priv->anchor =
1082 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1084 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1085 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1087 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1089 else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
1091 dirty = gtk_tree_selection_real_unselect_all (selection);
1092 dirty |= gtk_tree_selection_real_select_range (selection,
1098 dirty = gtk_tree_selection_real_unselect_all (selection);
1100 if (selection->tree_view->priv->anchor)
1101 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1103 selection->tree_view->priv->anchor =
1104 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1106 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1111 gtk_tree_path_free (anchor_path);
1114 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
1117 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1121 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1126 gboolean selected = FALSE;
1127 GtkTreePath *path = NULL;
1131 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1133 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1134 if (selection->user_func)
1136 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1137 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1138 selection->user_data))
1143 gtk_tree_path_free (path);
1146 if (selected == TRUE)
1148 node->flags ^= GTK_RBNODE_IS_SELECTED;
1150 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);