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,
244 gtk_tree_path_free (anchor_path);
247 selection->type = type;
251 * gtk_tree_selection_get_mode:
252 * @selection: a #GtkTreeSelection
254 * Gets the selection mode for @selection. See
255 * gtk_tree_selection_set_mode().
257 * Return value: the current selection mode
260 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
262 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
264 return selection->type;
268 * gtk_tree_selection_set_select_function:
269 * @selection: A #GtkTreeSelection.
270 * @func: The selection function.
271 * @data: The selection function's data.
272 * @destroy: The destroy function for user data. May be NULL.
274 * Sets the selection function. If set, this function is called before any node
275 * is selected or unselected, giving some control over which nodes are selected.
276 * The select function should return %TRUE if the state of the node may be toggled,
277 * and %FALSE if the state of the node should be left unchanged.
280 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
281 GtkTreeSelectionFunc func,
283 GtkDestroyNotify destroy)
285 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
286 g_return_if_fail (func != NULL);
288 if (selection->destroy)
290 GtkDestroyNotify d = selection->destroy;
292 selection->destroy = NULL;
293 d (selection->user_data);
296 selection->user_func = func;
297 selection->user_data = data;
298 selection->destroy = destroy;
302 * gtk_tree_selection_get_user_data:
303 * @selection: A #GtkTreeSelection.
305 * Returns the user data for the selection function.
307 * Return value: The user data.
310 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
312 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
314 return selection->user_data;
318 * gtk_tree_selection_get_tree_view:
319 * @selection: A #GtkTreeSelection
321 * Returns the tree view associated with @selection.
323 * Return value: A #GtkTreeView
326 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
328 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
330 return selection->tree_view;
334 * gtk_tree_selection_get_selected:
335 * @selection: A #GtkTreeSelection.
336 * @model: A pointer set to the #GtkTreeModel, or NULL.
337 * @iter: The #GtkTreeIter, or NULL.
339 * Sets @iter to the currently selected node if @selection is set to
340 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
341 * just want to test if @selection has any selected nodes. @model is filled
342 * with the current model as a convenience. This function will not work if you
343 * use @selection is #GTK_SELECTION_MULTIPLE.
345 * Return value: TRUE, if there is a selected node.
348 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
349 GtkTreeModel **model,
354 GtkTreePath *anchor_path;
357 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
358 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
359 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
360 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
363 *model = selection->tree_view->priv->model;
365 if (selection->tree_view->priv->anchor == NULL)
368 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
370 if (anchor_path == NULL)
375 gtk_tree_path_free (anchor_path);
381 if (!_gtk_tree_view_find_node (selection->tree_view,
385 ! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
387 /* We don't want to return the anchor if it isn't actually selected.
393 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
398 gtk_tree_path_free (anchor_path);
404 * gtk_tree_selection_selected_foreach:
405 * @selection: A #GtkTreeSelection.
406 * @func: The function to call for each selected node.
407 * @data: user data to pass to the function.
409 * Calls a function for each selected node.
412 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
413 GtkTreeSelectionForeachFunc func,
421 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
422 g_return_if_fail (selection->tree_view != NULL);
423 g_return_if_fail (selection->tree_view->priv->model != NULL);
426 selection->tree_view->priv->tree == NULL ||
427 selection->tree_view->priv->tree->root == NULL)
430 if (selection->type == GTK_SELECTION_SINGLE ||
431 selection->type == GTK_SELECTION_BROWSE)
433 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
435 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
436 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
437 (* func) (selection->tree_view->priv->model, path, &iter, data);
438 gtk_tree_path_free (path);
443 tree = selection->tree_view->priv->tree;
444 node = selection->tree_view->priv->tree->root;
446 while (node->left != tree->nil)
449 /* find the node internally */
450 path = gtk_tree_path_new_first ();
451 gtk_tree_model_get_iter (selection->tree_view->priv->model,
456 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
457 (* func) (selection->tree_view->priv->model, path, &iter, data);
463 tree = node->children;
465 while (node->left != tree->nil)
468 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
469 gtk_tree_path_append_index (path, 0);
471 TREE_VIEW_INTERNAL_ASSERT_VOID (has_child);
475 gboolean done = FALSE;
478 node = _gtk_rbtree_next (tree, node);
483 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
485 gtk_tree_path_next (path);
488 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
493 GtkTreeIter tmp_iter = iter;
495 node = tree->parent_node;
496 tree = tree->parent_tree;
499 gtk_tree_path_free (path);
500 /* we've run out of tree */
501 /* We're done with this function */
504 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
505 gtk_tree_path_up (path);
507 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
517 * gtk_tree_selection_select_path:
518 * @selection: A #GtkTreeSelection.
519 * @path: The #GtkTreePath to be selected.
521 * Select the row at @path.
524 gtk_tree_selection_select_path (GtkTreeSelection *selection,
529 GdkModifierType state = 0;
532 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
533 g_return_if_fail (selection->tree_view != NULL);
534 g_return_if_fail (path != NULL);
536 ret = _gtk_tree_view_find_node (selection->tree_view,
541 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
545 if (selection->type == GTK_SELECTION_MULTIPLE)
546 state = GDK_CONTROL_MASK;
548 _gtk_tree_selection_internal_select_node (selection,
556 * gtk_tree_selection_unselect_path:
557 * @selection: A #GtkTreeSelection.
558 * @path: The #GtkTreePath to be unselected.
560 * Unselects the row at @path.
563 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
570 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
571 g_return_if_fail (selection->tree_view != NULL);
572 g_return_if_fail (path != NULL);
574 ret = _gtk_tree_view_find_node (selection->tree_view,
579 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
583 _gtk_tree_selection_internal_select_node (selection,
591 * gtk_tree_selection_select_iter:
592 * @selection: A #GtkTreeSelection.
593 * @iter: The #GtkTreeIter to be selected.
595 * Selects the specified iterator.
598 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
603 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
604 g_return_if_fail (selection->tree_view != NULL);
605 g_return_if_fail (selection->tree_view->priv->model != NULL);
606 g_return_if_fail (iter != NULL);
608 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
614 gtk_tree_selection_select_path (selection, path);
615 gtk_tree_path_free (path);
620 * gtk_tree_selection_unselect_iter:
621 * @selection: A #GtkTreeSelection.
622 * @iter: The #GtkTreeIter to be unselected.
624 * Unselects the specified iterator.
627 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
632 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
633 g_return_if_fail (selection->tree_view != NULL);
634 g_return_if_fail (selection->tree_view->priv->model != NULL);
635 g_return_if_fail (iter != NULL);
637 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
643 gtk_tree_selection_unselect_path (selection, path);
644 gtk_tree_path_free (path);
648 * gtk_tree_selection_path_is_selected:
649 * @selection: A #GtkTreeSelection.
650 * @path: A #GtkTreePath to check selection on.
652 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
653 * does not point to a valid location, %FALSE is returned
655 * Return value: %TRUE if @path is selected.
658 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
665 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
666 g_return_val_if_fail (path != NULL, FALSE);
667 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
668 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
670 ret = _gtk_tree_view_find_node (selection->tree_view,
675 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
683 * gtk_tree_selection_iter_is_selected:
684 * @selection: A #GtkTreeSelection
685 * @iter: A valid #GtkTreeIter
687 * Returns %TRUE if the row pointed to by @path is currently selected.
689 * Return value: %TRUE, if @iter is selected
692 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
698 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
699 g_return_val_if_fail (iter != NULL, FALSE);
700 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
701 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
703 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
707 retval = gtk_tree_selection_path_is_selected (selection, path);
708 gtk_tree_path_free (path);
714 /* Wish I was in python, right now... */
716 GtkTreeSelection *selection;
721 select_all_helper (GtkRBTree *tree,
725 struct _TempTuple *tuple = data;
728 _gtk_rbtree_traverse (node->children,
729 node->children->root,
733 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
735 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
740 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
741 * can use it in other places without fear of the signal being emitted.
744 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
746 struct _TempTuple *tuple;
748 if (selection->tree_view->priv->tree == NULL)
751 /* Mark all nodes selected */
752 tuple = g_new (struct _TempTuple, 1);
753 tuple->selection = selection;
754 tuple->dirty = FALSE;
756 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
757 selection->tree_view->priv->tree->root,
771 * gtk_tree_selection_select_all:
772 * @selection: A #GtkTreeSelection.
774 * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
778 gtk_tree_selection_select_all (GtkTreeSelection *selection)
780 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
781 g_return_if_fail (selection->tree_view != NULL);
782 if (selection->tree_view->priv->tree == NULL)
784 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
786 if (gtk_tree_selection_real_select_all (selection))
787 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
791 unselect_all_helper (GtkRBTree *tree,
795 struct _TempTuple *tuple = data;
798 _gtk_rbtree_traverse (node->children,
799 node->children->root,
803 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
805 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
810 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
812 struct _TempTuple *tuple;
814 if (selection->type == GTK_SELECTION_SINGLE ||
815 selection->type == GTK_SELECTION_BROWSE)
817 GtkRBTree *tree = NULL;
818 GtkRBNode *node = NULL;
819 GtkTreePath *anchor_path;
821 if (selection->tree_view->priv->anchor == NULL)
824 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
826 if (anchor_path == NULL)
829 _gtk_tree_view_find_node (selection->tree_view,
834 gtk_tree_path_free (anchor_path);
839 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
841 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
843 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
844 selection->tree_view->priv->anchor = NULL;
852 tuple = g_new (struct _TempTuple, 1);
853 tuple->selection = selection;
854 tuple->dirty = FALSE;
856 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
857 selection->tree_view->priv->tree->root,
873 * gtk_tree_selection_unselect_all:
874 * @selection: A #GtkTreeSelection.
876 * Unselects all the nodes.
879 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
881 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
882 g_return_if_fail (selection->tree_view != NULL);
883 if (selection->tree_view->priv->tree == NULL)
886 if (selection->tree_view->priv->tree == NULL)
889 if (gtk_tree_selection_real_unselect_all (selection))
890 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
894 gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
895 GtkTreePath *start_path,
896 GtkTreePath *end_path)
898 GtkRBNode *start_node, *end_node;
899 GtkRBTree *start_tree, *end_tree;
900 gboolean dirty = FALSE;
902 switch (gtk_tree_path_compare (start_path, end_path))
905 _gtk_tree_view_find_node (selection->tree_view,
909 _gtk_tree_view_find_node (selection->tree_view,
915 _gtk_tree_view_find_node (selection->tree_view,
919 end_tree = start_tree;
920 end_node = start_node;
923 _gtk_tree_view_find_node (selection->tree_view,
927 _gtk_tree_view_find_node (selection->tree_view,
934 g_return_val_if_fail (start_node != NULL, FALSE);
935 g_return_val_if_fail (end_node != NULL, FALSE);
939 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, TRUE);
941 if (start_node == end_node)
944 if (start_node->children)
946 start_tree = start_node->children;
947 start_node = start_tree->root;
948 while (start_node->left != start_tree->nil)
949 start_node = start_node->left;
953 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
954 if (start_tree == NULL)
956 /* we just ran out of tree. That means someone passed in bogus values.
968 * gtk_tree_selection_select_range:
969 * @selection: A #GtkTreeSelection.
970 * @start_path: The initial node of the range.
971 * @end_path: The final node of the range.
973 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
976 gtk_tree_selection_select_range (GtkTreeSelection *selection,
977 GtkTreePath *start_path,
978 GtkTreePath *end_path)
980 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
981 g_return_if_fail (selection->tree_view != NULL);
983 if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
984 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
987 /* Called internally by gtktreeview.c It handles actually selecting the tree.
990 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
994 GdkModifierType state)
998 GtkTreePath *anchor_path = NULL;
1000 if (selection->type == GTK_SELECTION_NONE)
1003 if (selection->tree_view->priv->anchor)
1004 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1006 if (selection->type == GTK_SELECTION_SINGLE ||
1007 selection->type == GTK_SELECTION_BROWSE)
1009 /* Did we try to select the same node again? */
1010 if (selection->type == GTK_SELECTION_SINGLE &&
1011 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1013 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1015 dirty = gtk_tree_selection_real_unselect_all (selection);
1022 /* We only want to select the new node if we can unselect the old one,
1023 * and we can select the new one. */
1024 if (selection->user_func)
1026 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1027 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1028 selection->user_data))
1036 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1037 * unselect the new one
1040 dirty = gtk_tree_selection_real_unselect_all (selection);
1042 /* if dirty is TRUE at this point, we successfully unselected the
1043 * old one, and can then select the new one */
1046 if (selection->tree_view->priv->anchor)
1047 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1048 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1050 selection->tree_view->priv->anchor =
1051 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1057 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1060 selection->tree_view->priv->anchor =
1061 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1066 else if (selection->type == GTK_SELECTION_MULTIPLE)
1068 if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
1070 if (selection->tree_view->priv->anchor)
1071 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1073 selection->tree_view->priv->anchor =
1074 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1075 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1077 else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
1079 gtk_tree_selection_select_range (selection,
1083 else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1085 flags = node->flags;
1086 if (selection->tree_view->priv->anchor)
1087 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1089 selection->tree_view->priv->anchor =
1090 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1092 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1093 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1095 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1097 else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
1099 dirty = gtk_tree_selection_real_unselect_all (selection);
1100 dirty |= gtk_tree_selection_real_select_range (selection,
1106 dirty = gtk_tree_selection_real_unselect_all (selection);
1108 if (selection->tree_view->priv->anchor)
1109 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1111 selection->tree_view->priv->anchor =
1112 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1114 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1119 gtk_tree_path_free (anchor_path);
1122 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
1125 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1129 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1134 gboolean selected = FALSE;
1135 GtkTreePath *path = NULL;
1139 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1141 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1142 if (selection->user_func)
1144 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1145 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1146 selection->user_data))
1151 gtk_tree_path_free (path);
1154 if (selected == TRUE)
1156 node->flags ^= GTK_RBNODE_IS_SELECTED;
1158 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);