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;
531 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
532 g_return_if_fail (selection->tree_view != NULL);
533 g_return_if_fail (path != NULL);
535 _gtk_tree_view_find_node (selection->tree_view,
540 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
543 if (selection->type == GTK_SELECTION_MULTIPLE)
544 state = GDK_CONTROL_MASK;
546 _gtk_tree_selection_internal_select_node (selection,
554 * gtk_tree_selection_unselect_path:
555 * @selection: A #GtkTreeSelection.
556 * @path: The #GtkTreePath to be unselected.
558 * Unselects the row at @path.
561 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
567 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
568 g_return_if_fail (selection->tree_view != NULL);
569 g_return_if_fail (path != NULL);
571 _gtk_tree_view_find_node (selection->tree_view,
576 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
579 _gtk_tree_selection_internal_select_node (selection,
587 * gtk_tree_selection_select_iter:
588 * @selection: A #GtkTreeSelection.
589 * @iter: The #GtkTreeIter to be selected.
591 * Selects the specified iterator.
594 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
599 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
600 g_return_if_fail (selection->tree_view != NULL);
601 g_return_if_fail (selection->tree_view->priv->model != NULL);
602 g_return_if_fail (iter != NULL);
604 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
610 gtk_tree_selection_select_path (selection, path);
611 gtk_tree_path_free (path);
616 * gtk_tree_selection_unselect_iter:
617 * @selection: A #GtkTreeSelection.
618 * @iter: The #GtkTreeIter to be unselected.
620 * Unselects the specified iterator.
623 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
628 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
629 g_return_if_fail (selection->tree_view != NULL);
630 g_return_if_fail (selection->tree_view->priv->model != NULL);
631 g_return_if_fail (iter != NULL);
633 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
639 gtk_tree_selection_unselect_path (selection, path);
640 gtk_tree_path_free (path);
644 * gtk_tree_selection_path_is_selected:
645 * @selection: A #GtkTreeSelection.
646 * @path: A #GtkTreePath to check selection on.
648 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
649 * does not point to a valid location, %FALSE is returned
651 * Return value: %TRUE if @path is selected.
654 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
660 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
661 g_return_val_if_fail (path != NULL, FALSE);
662 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
663 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
665 _gtk_tree_view_find_node (selection->tree_view,
670 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
677 * gtk_tree_selection_iter_is_selected:
678 * @selection: A #GtkTreeSelection
679 * @iter: A valid #GtkTreeIter
681 * Returns %TRUE if the row pointed to by @path is currently selected.
683 * Return value: %TRUE, if @iter is selected
686 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
692 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
693 g_return_val_if_fail (iter != NULL, FALSE);
694 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
695 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
697 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
701 retval = gtk_tree_selection_path_is_selected (selection, path);
702 gtk_tree_path_free (path);
708 /* Wish I was in python, right now... */
710 GtkTreeSelection *selection;
715 select_all_helper (GtkRBTree *tree,
719 struct _TempTuple *tuple = data;
722 _gtk_rbtree_traverse (node->children,
723 node->children->root,
727 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
729 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
734 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
735 * can use it in other places without fear of the signal being emitted.
738 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
740 struct _TempTuple *tuple;
742 if (selection->tree_view->priv->tree == NULL)
745 /* Mark all nodes selected */
746 tuple = g_new (struct _TempTuple, 1);
747 tuple->selection = selection;
748 tuple->dirty = FALSE;
750 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
751 selection->tree_view->priv->tree->root,
765 * gtk_tree_selection_select_all:
766 * @selection: A #GtkTreeSelection.
768 * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
772 gtk_tree_selection_select_all (GtkTreeSelection *selection)
774 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
775 g_return_if_fail (selection->tree_view != NULL);
776 if (selection->tree_view->priv->tree == NULL)
778 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
780 if (gtk_tree_selection_real_select_all (selection))
781 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
785 unselect_all_helper (GtkRBTree *tree,
789 struct _TempTuple *tuple = data;
792 _gtk_rbtree_traverse (node->children,
793 node->children->root,
797 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
799 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
804 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
806 struct _TempTuple *tuple;
808 if (selection->type == GTK_SELECTION_SINGLE ||
809 selection->type == GTK_SELECTION_BROWSE)
811 GtkRBTree *tree = NULL;
812 GtkRBNode *node = NULL;
813 GtkTreePath *anchor_path;
815 if (selection->tree_view->priv->anchor == NULL)
818 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
820 if (anchor_path == NULL)
823 _gtk_tree_view_find_node (selection->tree_view,
828 gtk_tree_path_free (anchor_path);
833 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
835 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
837 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
838 selection->tree_view->priv->anchor = NULL;
846 tuple = g_new (struct _TempTuple, 1);
847 tuple->selection = selection;
848 tuple->dirty = FALSE;
850 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
851 selection->tree_view->priv->tree->root,
867 * gtk_tree_selection_unselect_all:
868 * @selection: A #GtkTreeSelection.
870 * Unselects all the nodes.
873 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
875 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
876 g_return_if_fail (selection->tree_view != NULL);
877 if (selection->tree_view->priv->tree == NULL)
880 if (selection->tree_view->priv->tree == NULL)
883 if (gtk_tree_selection_real_unselect_all (selection))
884 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
888 gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
889 GtkTreePath *start_path,
890 GtkTreePath *end_path)
892 GtkRBNode *start_node, *end_node;
893 GtkRBTree *start_tree, *end_tree;
894 gboolean dirty = FALSE;
896 switch (gtk_tree_path_compare (start_path, end_path))
899 _gtk_tree_view_find_node (selection->tree_view,
903 _gtk_tree_view_find_node (selection->tree_view,
909 _gtk_tree_view_find_node (selection->tree_view,
913 end_tree = start_tree;
914 end_node = start_node;
917 _gtk_tree_view_find_node (selection->tree_view,
921 _gtk_tree_view_find_node (selection->tree_view,
928 g_return_val_if_fail (start_node != NULL, FALSE);
929 g_return_val_if_fail (end_node != NULL, FALSE);
933 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, TRUE);
935 if (start_node == end_node)
938 if (start_node->children)
940 start_tree = start_node->children;
941 start_node = start_tree->root;
942 while (start_node->left != start_tree->nil)
943 start_node = start_node->left;
947 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
948 if (start_tree == NULL)
950 /* we just ran out of tree. That means someone passed in bogus values.
962 * gtk_tree_selection_select_range:
963 * @selection: A #GtkTreeSelection.
964 * @start_path: The initial node of the range.
965 * @end_path: The final node of the range.
967 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
970 gtk_tree_selection_select_range (GtkTreeSelection *selection,
971 GtkTreePath *start_path,
972 GtkTreePath *end_path)
974 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
975 g_return_if_fail (selection->tree_view != NULL);
977 if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
978 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
981 /* Called internally by gtktreeview.c It handles actually selecting the tree.
984 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
988 GdkModifierType state)
992 GtkTreePath *anchor_path = NULL;
994 if (selection->type == GTK_SELECTION_NONE)
997 if (selection->tree_view->priv->anchor)
998 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1000 if (selection->type == GTK_SELECTION_SINGLE ||
1001 selection->type == GTK_SELECTION_BROWSE)
1003 /* Did we try to select the same node again? */
1004 if (selection->type == GTK_SELECTION_SINGLE &&
1005 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1007 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1009 dirty = gtk_tree_selection_real_unselect_all (selection);
1016 /* We only want to select the new node if we can unselect the old one,
1017 * and we can select the new one. */
1018 if (selection->user_func)
1020 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1021 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1022 selection->user_data))
1030 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1031 * unselect the new one
1034 dirty = gtk_tree_selection_real_unselect_all (selection);
1036 /* if dirty is TRUE at this point, we successfully unselected the
1037 * old one, and can then select the new one */
1040 if (selection->tree_view->priv->anchor)
1041 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1042 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1044 selection->tree_view->priv->anchor =
1045 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1051 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1054 selection->tree_view->priv->anchor =
1055 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1060 else if (selection->type == GTK_SELECTION_MULTIPLE)
1062 if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
1064 if (selection->tree_view->priv->anchor)
1065 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1067 selection->tree_view->priv->anchor =
1068 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1069 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1071 else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
1073 gtk_tree_selection_select_range (selection,
1077 else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1079 flags = node->flags;
1080 if (selection->tree_view->priv->anchor)
1081 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1083 selection->tree_view->priv->anchor =
1084 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1086 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1087 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1089 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1091 else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
1093 dirty = gtk_tree_selection_real_unselect_all (selection);
1094 dirty |= gtk_tree_selection_real_select_range (selection,
1100 dirty = gtk_tree_selection_real_unselect_all (selection);
1102 if (selection->tree_view->priv->anchor)
1103 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1105 selection->tree_view->priv->anchor =
1106 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1108 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1113 gtk_tree_path_free (anchor_path);
1116 g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
1119 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1123 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1128 gboolean selected = FALSE;
1129 GtkTreePath *path = NULL;
1133 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1135 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1136 if (selection->user_func)
1138 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1139 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1140 selection->user_data))
1145 gtk_tree_path_free (path);
1148 if (selected == TRUE)
1150 node->flags ^= GTK_RBNODE_IS_SELECTED;
1152 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);