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;
359 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
360 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
361 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
362 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
365 *model = selection->tree_view->priv->model;
367 if (selection->tree_view->priv->anchor == NULL)
370 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
372 if (anchor_path == NULL)
377 found_node = !_gtk_tree_view_find_node (selection->tree_view,
382 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
384 /* we only want to return the anchor if it exists in the rbtree and
390 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
396 /* We don't want to return the anchor if it isn't actually selected.
401 gtk_tree_path_free (anchor_path);
407 * gtk_tree_selection_selected_foreach:
408 * @selection: A #GtkTreeSelection.
409 * @func: The function to call for each selected node.
410 * @data: user data to pass to the function.
412 * Calls a function for each selected node.
415 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
416 GtkTreeSelectionForeachFunc func,
424 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
425 g_return_if_fail (selection->tree_view != NULL);
426 g_return_if_fail (selection->tree_view->priv->model != NULL);
429 selection->tree_view->priv->tree == NULL ||
430 selection->tree_view->priv->tree->root == NULL)
433 if (selection->type == GTK_SELECTION_SINGLE ||
434 selection->type == GTK_SELECTION_BROWSE)
436 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
438 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
439 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
440 (* func) (selection->tree_view->priv->model, path, &iter, data);
441 gtk_tree_path_free (path);
446 tree = selection->tree_view->priv->tree;
447 node = selection->tree_view->priv->tree->root;
449 while (node->left != tree->nil)
452 /* find the node internally */
453 path = gtk_tree_path_new_first ();
454 gtk_tree_model_get_iter (selection->tree_view->priv->model,
459 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
460 (* func) (selection->tree_view->priv->model, path, &iter, data);
466 tree = node->children;
468 while (node->left != tree->nil)
471 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
472 gtk_tree_path_append_index (path, 0);
474 TREE_VIEW_INTERNAL_ASSERT_VOID (has_child);
478 gboolean done = FALSE;
481 node = _gtk_rbtree_next (tree, node);
486 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
488 gtk_tree_path_next (path);
491 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
496 GtkTreeIter tmp_iter = iter;
498 node = tree->parent_node;
499 tree = tree->parent_tree;
502 gtk_tree_path_free (path);
503 /* we've run out of tree */
504 /* We're done with this function */
507 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
508 gtk_tree_path_up (path);
510 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
520 * gtk_tree_selection_select_path:
521 * @selection: A #GtkTreeSelection.
522 * @path: The #GtkTreePath to be selected.
524 * Select the row at @path.
527 gtk_tree_selection_select_path (GtkTreeSelection *selection,
532 GdkModifierType state = 0;
535 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
536 g_return_if_fail (selection->tree_view != NULL);
537 g_return_if_fail (path != NULL);
539 ret = _gtk_tree_view_find_node (selection->tree_view,
544 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
548 if (selection->type == GTK_SELECTION_MULTIPLE)
549 state = GDK_CONTROL_MASK;
551 _gtk_tree_selection_internal_select_node (selection,
560 * gtk_tree_selection_unselect_path:
561 * @selection: A #GtkTreeSelection.
562 * @path: The #GtkTreePath to be unselected.
564 * Unselects the row at @path.
567 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
574 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
575 g_return_if_fail (selection->tree_view != NULL);
576 g_return_if_fail (path != NULL);
578 ret = _gtk_tree_view_find_node (selection->tree_view,
583 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
587 _gtk_tree_selection_internal_select_node (selection,
596 * gtk_tree_selection_select_iter:
597 * @selection: A #GtkTreeSelection.
598 * @iter: The #GtkTreeIter to be selected.
600 * Selects the specified iterator.
603 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
608 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
609 g_return_if_fail (selection->tree_view != NULL);
610 g_return_if_fail (selection->tree_view->priv->model != NULL);
611 g_return_if_fail (iter != NULL);
613 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
619 gtk_tree_selection_select_path (selection, path);
620 gtk_tree_path_free (path);
625 * gtk_tree_selection_unselect_iter:
626 * @selection: A #GtkTreeSelection.
627 * @iter: The #GtkTreeIter to be unselected.
629 * Unselects the specified iterator.
632 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
637 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
638 g_return_if_fail (selection->tree_view != NULL);
639 g_return_if_fail (selection->tree_view->priv->model != NULL);
640 g_return_if_fail (iter != NULL);
642 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
648 gtk_tree_selection_unselect_path (selection, path);
649 gtk_tree_path_free (path);
653 * gtk_tree_selection_path_is_selected:
654 * @selection: A #GtkTreeSelection.
655 * @path: A #GtkTreePath to check selection on.
657 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
658 * does not point to a valid location, %FALSE is returned
660 * Return value: %TRUE if @path is selected.
663 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
670 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
671 g_return_val_if_fail (path != NULL, FALSE);
672 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
673 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
675 ret = _gtk_tree_view_find_node (selection->tree_view,
680 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
688 * gtk_tree_selection_iter_is_selected:
689 * @selection: A #GtkTreeSelection
690 * @iter: A valid #GtkTreeIter
692 * Returns %TRUE if the row pointed to by @path is currently selected.
694 * Return value: %TRUE, if @iter is selected
697 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
703 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
704 g_return_val_if_fail (iter != NULL, FALSE);
705 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
706 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
708 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
712 retval = gtk_tree_selection_path_is_selected (selection, path);
713 gtk_tree_path_free (path);
719 /* Wish I was in python, right now... */
721 GtkTreeSelection *selection;
726 select_all_helper (GtkRBTree *tree,
730 struct _TempTuple *tuple = data;
733 _gtk_rbtree_traverse (node->children,
734 node->children->root,
738 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
740 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
745 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
746 * can use it in other places without fear of the signal being emitted.
749 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
751 struct _TempTuple *tuple;
753 if (selection->tree_view->priv->tree == NULL)
756 /* Mark all nodes selected */
757 tuple = g_new (struct _TempTuple, 1);
758 tuple->selection = selection;
759 tuple->dirty = FALSE;
761 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
762 selection->tree_view->priv->tree->root,
776 * gtk_tree_selection_select_all:
777 * @selection: A #GtkTreeSelection.
779 * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
783 gtk_tree_selection_select_all (GtkTreeSelection *selection)
785 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
786 g_return_if_fail (selection->tree_view != NULL);
787 if (selection->tree_view->priv->tree == NULL)
789 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
791 if (gtk_tree_selection_real_select_all (selection))
792 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
796 unselect_all_helper (GtkRBTree *tree,
800 struct _TempTuple *tuple = data;
803 _gtk_rbtree_traverse (node->children,
804 node->children->root,
808 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
810 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
815 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
817 struct _TempTuple *tuple;
819 if (selection->type == GTK_SELECTION_SINGLE ||
820 selection->type == GTK_SELECTION_BROWSE)
822 GtkRBTree *tree = NULL;
823 GtkRBNode *node = NULL;
824 GtkTreePath *anchor_path;
826 if (selection->tree_view->priv->anchor == NULL)
829 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
831 if (anchor_path == NULL)
834 _gtk_tree_view_find_node (selection->tree_view,
839 gtk_tree_path_free (anchor_path);
844 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
846 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
848 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
849 selection->tree_view->priv->anchor = NULL;
857 tuple = g_new (struct _TempTuple, 1);
858 tuple->selection = selection;
859 tuple->dirty = FALSE;
861 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
862 selection->tree_view->priv->tree->root,
878 * gtk_tree_selection_unselect_all:
879 * @selection: A #GtkTreeSelection.
881 * Unselects all the nodes.
884 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
886 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
887 g_return_if_fail (selection->tree_view != NULL);
888 if (selection->tree_view->priv->tree == NULL)
891 if (selection->tree_view->priv->tree == NULL)
894 if (gtk_tree_selection_real_unselect_all (selection))
895 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
899 gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
900 GtkTreePath *start_path,
901 GtkTreePath *end_path)
903 GtkRBNode *start_node, *end_node;
904 GtkRBTree *start_tree, *end_tree;
905 gboolean dirty = FALSE;
907 switch (gtk_tree_path_compare (start_path, end_path))
910 _gtk_tree_view_find_node (selection->tree_view,
914 _gtk_tree_view_find_node (selection->tree_view,
920 _gtk_tree_view_find_node (selection->tree_view,
924 end_tree = start_tree;
925 end_node = start_node;
928 _gtk_tree_view_find_node (selection->tree_view,
932 _gtk_tree_view_find_node (selection->tree_view,
939 g_return_val_if_fail (start_node != NULL, FALSE);
940 g_return_val_if_fail (end_node != NULL, FALSE);
944 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, TRUE);
946 if (start_node == end_node)
949 if (start_node->children)
951 start_tree = start_node->children;
952 start_node = start_tree->root;
953 while (start_node->left != start_tree->nil)
954 start_node = start_node->left;
958 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
959 if (start_tree == NULL)
961 /* we just ran out of tree. That means someone passed in bogus values.
973 * gtk_tree_selection_select_range:
974 * @selection: A #GtkTreeSelection.
975 * @start_path: The initial node of the range.
976 * @end_path: The final node of the range.
978 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
981 gtk_tree_selection_select_range (GtkTreeSelection *selection,
982 GtkTreePath *start_path,
983 GtkTreePath *end_path)
985 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
986 g_return_if_fail (selection->tree_view != NULL);
988 if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
989 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
992 /* Called internally by gtktreeview.c It handles actually selecting the tree.
996 * docs about the 'override_browse_mode', we set this flag when we want to
997 * unset select the node and override the select browse mode behaviour (that is
998 * 'one node should *always* be selected').
1001 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1005 GdkModifierType state,
1006 gboolean override_browse_mode)
1010 GtkTreePath *anchor_path = NULL;
1012 if (selection->type == GTK_SELECTION_NONE)
1015 if (selection->tree_view->priv->anchor)
1016 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1018 if (selection->type == GTK_SELECTION_SINGLE ||
1019 selection->type == GTK_SELECTION_BROWSE)
1022 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1024 dirty = gtk_tree_selection_real_unselect_all (selection);
1026 /* Did we try to select the same node again? */
1027 else if (selection->type == GTK_SELECTION_SINGLE &&
1028 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1030 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1032 dirty = gtk_tree_selection_real_unselect_all (selection);
1039 /* We only want to select the new node if we can unselect the old one,
1040 * and we can select the new one. */
1041 if (selection->user_func)
1043 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1044 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1045 selection->user_data))
1053 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1054 * unselect the new one
1057 dirty = gtk_tree_selection_real_unselect_all (selection);
1059 /* if dirty is TRUE at this point, we successfully unselected the
1060 * old one, and can then select the new one */
1063 if (selection->tree_view->priv->anchor)
1064 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1065 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1067 selection->tree_view->priv->anchor =
1068 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1074 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1077 selection->tree_view->priv->anchor =
1078 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1083 else if (selection->type == GTK_SELECTION_MULTIPLE)
1085 if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
1087 if (selection->tree_view->priv->anchor)
1088 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1090 selection->tree_view->priv->anchor =
1091 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1092 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1094 else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
1096 gtk_tree_selection_select_range (selection,
1100 else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1102 flags = node->flags;
1103 if (selection->tree_view->priv->anchor)
1104 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1106 selection->tree_view->priv->anchor =
1107 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1109 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1110 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1112 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1114 else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
1116 dirty = gtk_tree_selection_real_unselect_all (selection);
1117 dirty |= gtk_tree_selection_real_select_range (selection,
1123 dirty = gtk_tree_selection_real_unselect_all (selection);
1125 if (selection->tree_view->priv->anchor)
1126 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1128 selection->tree_view->priv->anchor =
1129 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1131 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1136 gtk_tree_path_free (anchor_path);
1139 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
1142 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1146 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1151 gboolean selected = FALSE;
1152 GtkTreePath *path = NULL;
1156 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1158 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1159 if (selection->user_func)
1161 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1162 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1163 selection->user_data))
1168 gtk_tree_path_free (path);
1171 if (selected == TRUE)
1173 node->flags ^= GTK_RBNODE_IS_SELECTED;
1175 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);