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 /* We do this so that we unconditionally unset all rows
198 tmp_func = selection->user_func;
199 selection->user_func = NULL;
200 gtk_tree_selection_unselect_all (selection);
201 selection->user_func = tmp_func;
203 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
204 selection->tree_view->priv->anchor = NULL;
206 else if (type == GTK_SELECTION_SINGLE ||
207 type == GTK_SELECTION_BROWSE)
209 GtkRBTree *tree = NULL;
210 GtkRBNode *node = NULL;
211 gint selected = FALSE;
212 GtkTreePath *anchor_path = NULL;
214 if (selection->tree_view->priv->anchor)
216 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
220 _gtk_tree_view_find_node (selection->tree_view,
225 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
230 /* We do this so that we unconditionally unset all rows
232 tmp_func = selection->user_func;
233 selection->user_func = NULL;
234 gtk_tree_selection_unselect_all (selection);
235 selection->user_func = tmp_func;
237 if (node && selected)
238 _gtk_tree_selection_internal_select_node (selection,
245 gtk_tree_path_free (anchor_path);
248 selection->type = type;
252 * gtk_tree_selection_get_mode:
253 * @selection: a #GtkTreeSelection
255 * Gets the selection mode for @selection. See
256 * gtk_tree_selection_set_mode().
258 * Return value: the current selection mode
261 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
263 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
265 return selection->type;
269 * gtk_tree_selection_set_select_function:
270 * @selection: A #GtkTreeSelection.
271 * @func: The selection function.
272 * @data: The selection function's data.
273 * @destroy: The destroy function for user data. May be NULL.
275 * Sets the selection function. If set, this function is called before any node
276 * is selected or unselected, giving some control over which nodes are selected.
277 * The select function should return %TRUE if the state of the node may be toggled,
278 * and %FALSE if the state of the node should be left unchanged.
281 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
282 GtkTreeSelectionFunc func,
284 GtkDestroyNotify destroy)
286 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
287 g_return_if_fail (func != NULL);
289 if (selection->destroy)
291 GtkDestroyNotify d = selection->destroy;
293 selection->destroy = NULL;
294 d (selection->user_data);
297 selection->user_func = func;
298 selection->user_data = data;
299 selection->destroy = destroy;
303 * gtk_tree_selection_get_user_data:
304 * @selection: A #GtkTreeSelection.
306 * Returns the user data for the selection function.
308 * Return value: The user data.
311 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
313 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
315 return selection->user_data;
319 * gtk_tree_selection_get_tree_view:
320 * @selection: A #GtkTreeSelection
322 * Returns the tree view associated with @selection.
324 * Return value: A #GtkTreeView
327 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
329 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
331 return selection->tree_view;
335 * gtk_tree_selection_get_selected:
336 * @selection: A #GtkTreeSelection.
337 * @model: A pointer set to the #GtkTreeModel, or NULL.
338 * @iter: The #GtkTreeIter, or NULL.
340 * Sets @iter to the currently selected node if @selection is set to
341 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
342 * just want to test if @selection has any selected nodes. @model is filled
343 * with the current model as a convenience. This function will not work if you
344 * use @selection is #GTK_SELECTION_MULTIPLE.
346 * Return value: TRUE, if there is a selected node.
349 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
350 GtkTreeModel **model,
355 GtkTreePath *anchor_path;
358 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
359 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
360 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
361 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
364 *model = selection->tree_view->priv->model;
366 if (selection->tree_view->priv->anchor == NULL)
369 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
371 if (anchor_path == NULL)
376 gtk_tree_path_free (anchor_path);
382 if (!_gtk_tree_view_find_node (selection->tree_view,
386 ! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
388 /* We don't want to return the anchor if it isn't actually selected.
394 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
399 gtk_tree_path_free (anchor_path);
405 * gtk_tree_selection_selected_foreach:
406 * @selection: A #GtkTreeSelection.
407 * @func: The function to call for each selected node.
408 * @data: user data to pass to the function.
410 * Calls a function for each selected node.
413 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
414 GtkTreeSelectionForeachFunc func,
422 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
423 g_return_if_fail (selection->tree_view != NULL);
424 g_return_if_fail (selection->tree_view->priv->model != NULL);
427 selection->tree_view->priv->tree == NULL ||
428 selection->tree_view->priv->tree->root == NULL)
431 if (selection->type == GTK_SELECTION_SINGLE ||
432 selection->type == GTK_SELECTION_BROWSE)
434 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
436 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
437 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
438 (* func) (selection->tree_view->priv->model, path, &iter, data);
439 gtk_tree_path_free (path);
444 tree = selection->tree_view->priv->tree;
445 node = selection->tree_view->priv->tree->root;
447 while (node->left != tree->nil)
450 /* find the node internally */
451 path = gtk_tree_path_new_first ();
452 gtk_tree_model_get_iter (selection->tree_view->priv->model,
457 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
458 (* func) (selection->tree_view->priv->model, path, &iter, data);
464 tree = node->children;
466 while (node->left != tree->nil)
469 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
470 gtk_tree_path_append_index (path, 0);
472 TREE_VIEW_INTERNAL_ASSERT_VOID (has_child);
476 gboolean done = FALSE;
479 node = _gtk_rbtree_next (tree, node);
484 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
486 gtk_tree_path_next (path);
489 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
494 GtkTreeIter tmp_iter = iter;
496 node = tree->parent_node;
497 tree = tree->parent_tree;
500 gtk_tree_path_free (path);
501 /* we've run out of tree */
502 /* We're done with this function */
505 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
506 gtk_tree_path_up (path);
508 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
518 * gtk_tree_selection_select_path:
519 * @selection: A #GtkTreeSelection.
520 * @path: The #GtkTreePath to be selected.
522 * Select the row at @path.
525 gtk_tree_selection_select_path (GtkTreeSelection *selection,
530 GdkModifierType state = 0;
533 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
534 g_return_if_fail (selection->tree_view != NULL);
535 g_return_if_fail (path != NULL);
537 ret = _gtk_tree_view_find_node (selection->tree_view,
542 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
546 if (selection->type == GTK_SELECTION_MULTIPLE)
547 state = GDK_CONTROL_MASK;
549 _gtk_tree_selection_internal_select_node (selection,
558 * gtk_tree_selection_unselect_path:
559 * @selection: A #GtkTreeSelection.
560 * @path: The #GtkTreePath to be unselected.
562 * Unselects the row at @path.
565 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
572 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
573 g_return_if_fail (selection->tree_view != NULL);
574 g_return_if_fail (path != NULL);
576 ret = _gtk_tree_view_find_node (selection->tree_view,
581 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
585 _gtk_tree_selection_internal_select_node (selection,
594 * gtk_tree_selection_select_iter:
595 * @selection: A #GtkTreeSelection.
596 * @iter: The #GtkTreeIter to be selected.
598 * Selects the specified iterator.
601 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
606 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
607 g_return_if_fail (selection->tree_view != NULL);
608 g_return_if_fail (selection->tree_view->priv->model != NULL);
609 g_return_if_fail (iter != NULL);
611 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
617 gtk_tree_selection_select_path (selection, path);
618 gtk_tree_path_free (path);
623 * gtk_tree_selection_unselect_iter:
624 * @selection: A #GtkTreeSelection.
625 * @iter: The #GtkTreeIter to be unselected.
627 * Unselects the specified iterator.
630 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
635 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
636 g_return_if_fail (selection->tree_view != NULL);
637 g_return_if_fail (selection->tree_view->priv->model != NULL);
638 g_return_if_fail (iter != NULL);
640 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
646 gtk_tree_selection_unselect_path (selection, path);
647 gtk_tree_path_free (path);
651 * gtk_tree_selection_path_is_selected:
652 * @selection: A #GtkTreeSelection.
653 * @path: A #GtkTreePath to check selection on.
655 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
656 * does not point to a valid location, %FALSE is returned
658 * Return value: %TRUE if @path is selected.
661 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
668 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
669 g_return_val_if_fail (path != NULL, FALSE);
670 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
671 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
673 ret = _gtk_tree_view_find_node (selection->tree_view,
678 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
686 * gtk_tree_selection_iter_is_selected:
687 * @selection: A #GtkTreeSelection
688 * @iter: A valid #GtkTreeIter
690 * Returns %TRUE if the row pointed to by @path is currently selected.
692 * Return value: %TRUE, if @iter is selected
695 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
701 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
702 g_return_val_if_fail (iter != NULL, FALSE);
703 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
704 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
706 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
710 retval = gtk_tree_selection_path_is_selected (selection, path);
711 gtk_tree_path_free (path);
717 /* Wish I was in python, right now... */
719 GtkTreeSelection *selection;
724 select_all_helper (GtkRBTree *tree,
728 struct _TempTuple *tuple = data;
731 _gtk_rbtree_traverse (node->children,
732 node->children->root,
736 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
738 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
743 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
744 * can use it in other places without fear of the signal being emitted.
747 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
749 struct _TempTuple *tuple;
751 if (selection->tree_view->priv->tree == NULL)
754 /* Mark all nodes selected */
755 tuple = g_new (struct _TempTuple, 1);
756 tuple->selection = selection;
757 tuple->dirty = FALSE;
759 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
760 selection->tree_view->priv->tree->root,
774 * gtk_tree_selection_select_all:
775 * @selection: A #GtkTreeSelection.
777 * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
781 gtk_tree_selection_select_all (GtkTreeSelection *selection)
783 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
784 g_return_if_fail (selection->tree_view != NULL);
785 if (selection->tree_view->priv->tree == NULL)
787 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
789 if (gtk_tree_selection_real_select_all (selection))
790 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
794 unselect_all_helper (GtkRBTree *tree,
798 struct _TempTuple *tuple = data;
801 _gtk_rbtree_traverse (node->children,
802 node->children->root,
806 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
808 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
813 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
815 struct _TempTuple *tuple;
817 if (selection->type == GTK_SELECTION_SINGLE ||
818 selection->type == GTK_SELECTION_BROWSE)
820 GtkRBTree *tree = NULL;
821 GtkRBNode *node = NULL;
822 GtkTreePath *anchor_path;
824 if (selection->tree_view->priv->anchor == NULL)
827 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
829 if (anchor_path == NULL)
832 _gtk_tree_view_find_node (selection->tree_view,
837 gtk_tree_path_free (anchor_path);
842 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
844 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
846 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
847 selection->tree_view->priv->anchor = NULL;
855 tuple = g_new (struct _TempTuple, 1);
856 tuple->selection = selection;
857 tuple->dirty = FALSE;
859 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
860 selection->tree_view->priv->tree->root,
876 * gtk_tree_selection_unselect_all:
877 * @selection: A #GtkTreeSelection.
879 * Unselects all the nodes.
882 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
884 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
885 g_return_if_fail (selection->tree_view != NULL);
886 if (selection->tree_view->priv->tree == NULL)
889 if (selection->tree_view->priv->tree == NULL)
892 if (gtk_tree_selection_real_unselect_all (selection))
893 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
897 gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
898 GtkTreePath *start_path,
899 GtkTreePath *end_path)
901 GtkRBNode *start_node, *end_node;
902 GtkRBTree *start_tree, *end_tree;
903 gboolean dirty = FALSE;
905 switch (gtk_tree_path_compare (start_path, end_path))
908 _gtk_tree_view_find_node (selection->tree_view,
912 _gtk_tree_view_find_node (selection->tree_view,
918 _gtk_tree_view_find_node (selection->tree_view,
922 end_tree = start_tree;
923 end_node = start_node;
926 _gtk_tree_view_find_node (selection->tree_view,
930 _gtk_tree_view_find_node (selection->tree_view,
937 g_return_val_if_fail (start_node != NULL, FALSE);
938 g_return_val_if_fail (end_node != NULL, FALSE);
942 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, TRUE);
944 if (start_node == end_node)
947 if (start_node->children)
949 start_tree = start_node->children;
950 start_node = start_tree->root;
951 while (start_node->left != start_tree->nil)
952 start_node = start_node->left;
956 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
957 if (start_tree == NULL)
959 /* we just ran out of tree. That means someone passed in bogus values.
971 * gtk_tree_selection_select_range:
972 * @selection: A #GtkTreeSelection.
973 * @start_path: The initial node of the range.
974 * @end_path: The final node of the range.
976 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
979 gtk_tree_selection_select_range (GtkTreeSelection *selection,
980 GtkTreePath *start_path,
981 GtkTreePath *end_path)
983 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
984 g_return_if_fail (selection->tree_view != NULL);
986 if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
987 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
990 /* Called internally by gtktreeview.c It handles actually selecting the tree.
994 * docs about the 'override_browse_mode', we set this flag when we want to
995 * unset select the node and override the select browse mode behaviour (that is
996 * 'one node should *always* be selected').
999 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1003 GdkModifierType state,
1004 gboolean override_browse_mode)
1008 GtkTreePath *anchor_path = NULL;
1010 if (selection->type == GTK_SELECTION_NONE)
1013 if (selection->tree_view->priv->anchor)
1014 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1016 if (selection->type == GTK_SELECTION_SINGLE ||
1017 selection->type == GTK_SELECTION_BROWSE)
1020 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1022 dirty = gtk_tree_selection_real_unselect_all (selection);
1024 /* Did we try to select the same node again? */
1025 else if (selection->type == GTK_SELECTION_SINGLE &&
1026 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1028 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1030 dirty = gtk_tree_selection_real_unselect_all (selection);
1037 /* We only want to select the new node if we can unselect the old one,
1038 * and we can select the new one. */
1039 if (selection->user_func)
1041 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1042 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1043 selection->user_data))
1051 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1052 * unselect the new one
1055 dirty = gtk_tree_selection_real_unselect_all (selection);
1057 /* if dirty is TRUE at this point, we successfully unselected the
1058 * old one, and can then select the new one */
1061 if (selection->tree_view->priv->anchor)
1062 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1063 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1065 selection->tree_view->priv->anchor =
1066 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1072 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1075 selection->tree_view->priv->anchor =
1076 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1081 else if (selection->type == GTK_SELECTION_MULTIPLE)
1083 if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
1085 if (selection->tree_view->priv->anchor)
1086 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1088 selection->tree_view->priv->anchor =
1089 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1090 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1092 else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
1094 gtk_tree_selection_select_range (selection,
1098 else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1100 flags = node->flags;
1101 if (selection->tree_view->priv->anchor)
1102 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1104 selection->tree_view->priv->anchor =
1105 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1107 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1108 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1110 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1112 else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
1114 dirty = gtk_tree_selection_real_unselect_all (selection);
1115 dirty |= gtk_tree_selection_real_select_range (selection,
1121 dirty = gtk_tree_selection_real_unselect_all (selection);
1123 if (selection->tree_view->priv->anchor)
1124 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1126 selection->tree_view->priv->anchor =
1127 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1129 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1134 gtk_tree_path_free (anchor_path);
1137 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
1140 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1144 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1149 gboolean selected = FALSE;
1150 GtkTreePath *path = NULL;
1154 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1156 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1157 if (selection->user_func)
1159 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1160 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1161 selection->user_data))
1166 gtk_tree_path_free (path);
1169 if (selected == TRUE)
1171 node->flags ^= GTK_RBNODE_IS_SELECTED;
1173 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);