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"
25 static void gtk_tree_selection_init (GtkTreeSelection *selection);
26 static void gtk_tree_selection_class_init (GtkTreeSelectionClass *class);
28 static void gtk_tree_selection_finalize (GObject *object);
29 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
30 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
31 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
42 static GObjectClass *parent_class = NULL;
43 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
46 gtk_tree_selection_get_type (void)
48 static GType selection_type = 0;
52 static const GTypeInfo selection_info =
54 sizeof (GtkTreeSelectionClass),
56 NULL, /* base_finalize */
57 (GClassInitFunc) gtk_tree_selection_class_init,
58 NULL, /* class_finalize */
59 NULL, /* class_data */
60 sizeof (GtkTreeSelection),
62 (GInstanceInitFunc) gtk_tree_selection_init
66 g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection",
70 return selection_type;
74 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
76 GObjectClass *object_class;
78 object_class = (GObjectClass*) class;
79 parent_class = g_type_class_peek_parent (class);
81 object_class->finalize = gtk_tree_selection_finalize;
82 class->changed = NULL;
84 tree_selection_signals[CHANGED] =
85 g_signal_new ("changed",
86 G_OBJECT_CLASS_TYPE (object_class),
88 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
90 _gtk_marshal_VOID__VOID,
95 gtk_tree_selection_init (GtkTreeSelection *selection)
97 selection->type = GTK_SELECTION_SINGLE;
101 gtk_tree_selection_finalize (GObject *object)
103 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
105 if (selection->destroy)
107 GtkDestroyNotify d = selection->destroy;
109 selection->destroy = NULL;
110 d (selection->user_data);
113 /* chain parent_class' handler */
114 G_OBJECT_CLASS (parent_class)->finalize (object);
118 * _gtk_tree_selection_new:
120 * Creates a new #GtkTreeSelection object. This function should not be invoked,
121 * as each #GtkTreeView will create it's own #GtkTreeSelection.
123 * Return value: A newly created #GtkTreeSelection object.
126 _gtk_tree_selection_new (void)
128 GtkTreeSelection *selection;
130 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
136 * _gtk_tree_selection_new_with_tree_view:
137 * @tree_view: The #GtkTreeView.
139 * Creates a new #GtkTreeSelection object. This function should not be invoked,
140 * as each #GtkTreeView will create it's own #GtkTreeSelection.
142 * Return value: A newly created #GtkTreeSelection object.
145 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
147 GtkTreeSelection *selection;
149 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
151 selection = _gtk_tree_selection_new ();
152 _gtk_tree_selection_set_tree_view (selection, tree_view);
158 * _gtk_tree_selection_set_tree_view:
159 * @selection: A #GtkTreeSelection.
160 * @tree_view: The #GtkTreeView.
162 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
163 * it is used internally by #GtkTreeView.
166 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
167 GtkTreeView *tree_view)
169 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
170 if (tree_view != NULL)
171 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
173 selection->tree_view = tree_view;
177 * gtk_tree_selection_set_mode:
178 * @selection: A #GtkTreeSelection.
179 * @type: The selection mode
181 * Sets the selection mode of the @selection. If the previous type was
182 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
183 * previously selected.
186 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
187 GtkSelectionMode type)
189 GtkTreeSelectionFunc tmp_func;
190 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
192 if (selection->type == type)
196 if (type == GTK_SELECTION_NONE)
198 /* We do this so that we unconditionally unset all rows
200 tmp_func = selection->user_func;
201 selection->user_func = NULL;
202 gtk_tree_selection_unselect_all (selection);
203 selection->user_func = tmp_func;
205 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
206 selection->tree_view->priv->anchor = NULL;
208 else if (type == GTK_SELECTION_SINGLE ||
209 type == GTK_SELECTION_BROWSE)
211 GtkRBTree *tree = NULL;
212 GtkRBNode *node = NULL;
213 gint selected = FALSE;
214 GtkTreePath *anchor_path = NULL;
216 if (selection->tree_view->priv->anchor)
218 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
222 _gtk_tree_view_find_node (selection->tree_view,
227 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
232 /* We do this so that we unconditionally unset all rows
234 tmp_func = selection->user_func;
235 selection->user_func = NULL;
236 gtk_tree_selection_unselect_all (selection);
237 selection->user_func = tmp_func;
239 if (node && selected)
240 _gtk_tree_selection_internal_select_node (selection,
247 gtk_tree_path_free (anchor_path);
250 selection->type = type;
254 * gtk_tree_selection_get_mode:
255 * @selection: a #GtkTreeSelection
257 * Gets the selection mode for @selection. See
258 * gtk_tree_selection_set_mode().
260 * Return value: the current selection mode
263 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
265 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
267 return selection->type;
271 * gtk_tree_selection_set_select_function:
272 * @selection: A #GtkTreeSelection.
273 * @func: The selection function.
274 * @data: The selection function's data.
275 * @destroy: The destroy function for user data. May be NULL.
277 * Sets the selection function. If set, this function is called before any node
278 * is selected or unselected, giving some control over which nodes are selected.
279 * The select function should return %TRUE if the state of the node may be toggled,
280 * and %FALSE if the state of the node should be left unchanged.
283 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
284 GtkTreeSelectionFunc func,
286 GtkDestroyNotify destroy)
288 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
289 g_return_if_fail (func != NULL);
291 if (selection->destroy)
293 GtkDestroyNotify d = selection->destroy;
295 selection->destroy = NULL;
296 d (selection->user_data);
299 selection->user_func = func;
300 selection->user_data = data;
301 selection->destroy = destroy;
305 * gtk_tree_selection_get_user_data:
306 * @selection: A #GtkTreeSelection.
308 * Returns the user data for the selection function.
310 * Return value: The user data.
313 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
315 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
317 return selection->user_data;
321 * gtk_tree_selection_get_tree_view:
322 * @selection: A #GtkTreeSelection
324 * Returns the tree view associated with @selection.
326 * Return value: A #GtkTreeView
329 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
331 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
333 return selection->tree_view;
337 * gtk_tree_selection_get_selected:
338 * @selection: A #GtkTreeSelection.
339 * @model: A pointer to set to the #GtkTreeModel, or NULL.
340 * @iter: The #GtkTreeIter, or NULL.
342 * Sets @iter to the currently selected node if @selection is set to
343 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
344 * just want to test if @selection has any selected nodes. @model is filled
345 * with the current model as a convenience. This function will not work if you
346 * use @selection is #GTK_SELECTION_MULTIPLE.
348 * Return value: TRUE, if there is a selected node.
351 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
352 GtkTreeModel **model,
357 GtkTreePath *anchor_path;
361 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
362 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
363 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
364 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
367 *model = selection->tree_view->priv->model;
369 if (selection->tree_view->priv->anchor == NULL)
372 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
374 if (anchor_path == NULL)
379 found_node = !_gtk_tree_view_find_node (selection->tree_view,
384 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
386 /* we only want to return the anchor if it exists in the rbtree and
392 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
398 /* We don't want to return the anchor if it isn't actually selected.
403 gtk_tree_path_free (anchor_path);
409 * gtk_tree_selection_get_selected_rows:
410 * @selection: A #GtkTreeSelection.
411 * @model: A pointer to set to the #GtkTreeModel, or NULL.
413 * Creates a list of path of all selected rows. Additionally, if you are
414 * planning on modifying the model after calling this function, you may
415 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
416 * To do this, you can use gtk_tree_row_reference_new_proxy().
418 * To free the return value, use:
419 * <informalexample><programlisting>
420 * g_list_foreach (list, gtk_tree_path_free, NULL);
421 * g_list_free (list);
422 * </programlisting></informalexample>
424 * Return value: A #GList containing a #GtkTreePath for each selected row.
427 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
428 GtkTreeModel **model)
431 GtkRBTree *tree = NULL;
432 GtkRBNode *node = NULL;
435 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
436 g_return_val_if_fail (selection->tree_view != NULL, NULL);
437 g_return_val_if_fail (selection->tree_view->priv->model != NULL, NULL);
439 if (selection->tree_view->priv->tree == NULL ||
440 selection->tree_view->priv->tree->root == NULL)
444 *model = selection->tree_view->priv->model;
446 if (selection->type == GTK_SELECTION_NONE)
448 else if (selection->type != GTK_SELECTION_MULTIPLE)
452 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
456 path = gtk_tree_model_get_path (*model, &iter);
457 list = g_list_append (list, path);
465 tree = selection->tree_view->priv->tree;
466 node = selection->tree_view->priv->tree->root;
468 while (node->left != tree->nil)
470 path = gtk_tree_path_new_first ();
474 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
475 list = g_list_append (list, gtk_tree_path_copy (path));
479 tree = node->children;
482 while (node->left != tree->nil)
485 gtk_tree_path_append_index (path, 0);
489 gboolean done = FALSE;
493 node = _gtk_rbtree_next (tree, node);
497 gtk_tree_path_next (path);
501 node = tree->parent_node;
502 tree = tree->parent_tree;
506 gtk_tree_path_free (path);
510 gtk_tree_path_up (path);
518 gtk_tree_path_free (path);
524 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
528 gint *count = (gint *)data;
530 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
534 _gtk_rbtree_traverse (node->children, node->children->root,
536 gtk_tree_selection_count_selected_rows_helper, data);
540 * gtk_tree_selection_count_selected_rows:
541 * @selection: A #GtkTreeSelection.
543 * Returns the number of rows that have been selected in @tree.
545 * Return value: The number of rows selected.
548 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
554 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
555 g_return_val_if_fail (selection->tree_view != NULL, 0);
556 g_return_val_if_fail (selection->tree_view->priv->model != NULL, 0);
558 if (selection->tree_view->priv->tree == NULL ||
559 selection->tree_view->priv->tree->root == NULL)
562 if (selection->type == GTK_SELECTION_SINGLE ||
563 selection->type == GTK_SELECTION_BROWSE)
565 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
571 tree = selection->tree_view->priv->tree;
572 node = selection->tree_view->priv->tree->root;
574 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
575 selection->tree_view->priv->tree->root,
577 gtk_tree_selection_count_selected_rows_helper,
583 /* gtk_tree_selection_selected_foreach helper */
585 model_changed (gpointer data)
587 gboolean *stop = (gboolean *)data;
593 * gtk_tree_selection_selected_foreach:
594 * @selection: A #GtkTreeSelection.
595 * @func: The function to call for each selected node.
596 * @data: user data to pass to the function.
598 * Calls a function for each selected node. Note that you cannot modify
599 * the tree or selection from within this function. As a result,
600 * gtk_tree_selection_get_selected_rows() might be more useful.
603 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
604 GtkTreeSelectionForeachFunc func,
612 guint inserted_id, deleted_id, reordered_id;
613 gboolean stop = FALSE, has_next = FALSE, has_parent = FALSE;
615 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
616 g_return_if_fail (selection->tree_view != NULL);
617 g_return_if_fail (selection->tree_view->priv->model != NULL);
620 selection->tree_view->priv->tree == NULL ||
621 selection->tree_view->priv->tree->root == NULL)
624 if (selection->type == GTK_SELECTION_SINGLE ||
625 selection->type == GTK_SELECTION_BROWSE)
627 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
629 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
630 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
631 (* func) (selection->tree_view->priv->model, path, &iter, data);
632 gtk_tree_path_free (path);
637 tree = selection->tree_view->priv->tree;
638 node = selection->tree_view->priv->tree->root;
640 while (node->left != tree->nil)
643 /* connect to signals to monitor changes in treemodel */
644 inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
646 G_CALLBACK (model_changed),
648 deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
650 G_CALLBACK (model_changed),
652 reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
654 G_CALLBACK (model_changed),
657 /* find the node internally */
658 path = gtk_tree_path_new_first ();
659 gtk_tree_model_get_iter (selection->tree_view->priv->model,
664 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
665 (* func) (selection->tree_view->priv->model, path, &iter, data);
675 tree = node->children;
677 while (node->left != tree->nil)
680 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
681 gtk_tree_path_append_index (path, 0);
683 /* we do the sanity check at the bottom of this function */
689 gboolean done = FALSE;
692 node = _gtk_rbtree_next (tree, node);
697 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
699 gtk_tree_path_next (path);
702 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
707 GtkTreeIter tmp_iter = iter;
709 node = tree->parent_node;
710 tree = tree->parent_tree;
713 /* we've run out of tree */
714 /* We're done with this function */
719 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
720 gtk_tree_path_up (path);
722 /* we do the sanity check at the bottom of this function */
734 gtk_tree_path_free (path);
736 g_signal_handler_disconnect (selection->tree_view->priv->model,
738 g_signal_handler_disconnect (selection->tree_view->priv->model,
740 g_signal_handler_disconnect (selection->tree_view->priv->model,
743 /* check if we have to spew a scary message */
745 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
747 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
750 ("The model has been modified from within gtk_tree_selection_foreach.\n"
751 "This function is for observing the selections of the tree only. If\n"
752 "you are trying to get all selected items from the tree, try using\n"
753 "gtk_tree_selection_get_selected_rows instead.\n");
757 * gtk_tree_selection_select_path:
758 * @selection: A #GtkTreeSelection.
759 * @path: The #GtkTreePath to be selected.
761 * Select the row at @path.
764 gtk_tree_selection_select_path (GtkTreeSelection *selection,
769 GdkModifierType state = 0;
772 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
773 g_return_if_fail (selection->tree_view != NULL);
774 g_return_if_fail (path != NULL);
776 ret = _gtk_tree_view_find_node (selection->tree_view,
781 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
785 if (selection->type == GTK_SELECTION_MULTIPLE)
786 state = GDK_CONTROL_MASK;
788 _gtk_tree_selection_internal_select_node (selection,
797 * gtk_tree_selection_unselect_path:
798 * @selection: A #GtkTreeSelection.
799 * @path: The #GtkTreePath to be unselected.
801 * Unselects the row at @path.
804 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
811 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
812 g_return_if_fail (selection->tree_view != NULL);
813 g_return_if_fail (path != NULL);
815 ret = _gtk_tree_view_find_node (selection->tree_view,
820 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
824 _gtk_tree_selection_internal_select_node (selection,
833 * gtk_tree_selection_select_iter:
834 * @selection: A #GtkTreeSelection.
835 * @iter: The #GtkTreeIter to be selected.
837 * Selects the specified iterator.
840 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
845 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
846 g_return_if_fail (selection->tree_view != NULL);
847 g_return_if_fail (selection->tree_view->priv->model != NULL);
848 g_return_if_fail (iter != NULL);
850 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
856 gtk_tree_selection_select_path (selection, path);
857 gtk_tree_path_free (path);
862 * gtk_tree_selection_unselect_iter:
863 * @selection: A #GtkTreeSelection.
864 * @iter: The #GtkTreeIter to be unselected.
866 * Unselects the specified iterator.
869 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
874 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
875 g_return_if_fail (selection->tree_view != NULL);
876 g_return_if_fail (selection->tree_view->priv->model != NULL);
877 g_return_if_fail (iter != NULL);
879 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
885 gtk_tree_selection_unselect_path (selection, path);
886 gtk_tree_path_free (path);
890 * gtk_tree_selection_path_is_selected:
891 * @selection: A #GtkTreeSelection.
892 * @path: A #GtkTreePath to check selection on.
894 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
895 * does not point to a valid location, %FALSE is returned
897 * Return value: %TRUE if @path is selected.
900 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
907 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
908 g_return_val_if_fail (path != NULL, FALSE);
909 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
910 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
912 ret = _gtk_tree_view_find_node (selection->tree_view,
917 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
925 * gtk_tree_selection_iter_is_selected:
926 * @selection: A #GtkTreeSelection
927 * @iter: A valid #GtkTreeIter
929 * Returns %TRUE if the row at @iter is currently selected.
931 * Return value: %TRUE, if @iter is selected
934 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
940 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
941 g_return_val_if_fail (iter != NULL, FALSE);
942 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
943 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
945 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
949 retval = gtk_tree_selection_path_is_selected (selection, path);
950 gtk_tree_path_free (path);
956 /* Wish I was in python, right now... */
958 GtkTreeSelection *selection;
963 select_all_helper (GtkRBTree *tree,
967 struct _TempTuple *tuple = data;
970 _gtk_rbtree_traverse (node->children,
971 node->children->root,
975 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
977 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
982 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
983 * can use it in other places without fear of the signal being emitted.
986 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
988 struct _TempTuple *tuple;
990 if (selection->tree_view->priv->tree == NULL)
993 /* Mark all nodes selected */
994 tuple = g_new (struct _TempTuple, 1);
995 tuple->selection = selection;
996 tuple->dirty = FALSE;
998 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
999 selection->tree_view->priv->tree->root,
1013 * gtk_tree_selection_select_all:
1014 * @selection: A #GtkTreeSelection.
1016 * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
1020 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1022 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1023 g_return_if_fail (selection->tree_view != NULL);
1024 if (selection->tree_view->priv->tree == NULL)
1026 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1028 if (gtk_tree_selection_real_select_all (selection))
1029 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1033 unselect_all_helper (GtkRBTree *tree,
1037 struct _TempTuple *tuple = data;
1040 _gtk_rbtree_traverse (node->children,
1041 node->children->root,
1043 unselect_all_helper,
1045 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1047 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1052 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1054 struct _TempTuple *tuple;
1056 if (selection->type == GTK_SELECTION_SINGLE ||
1057 selection->type == GTK_SELECTION_BROWSE)
1059 GtkRBTree *tree = NULL;
1060 GtkRBNode *node = NULL;
1061 GtkTreePath *anchor_path;
1063 if (selection->tree_view->priv->anchor == NULL)
1066 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1068 if (anchor_path == NULL)
1071 _gtk_tree_view_find_node (selection->tree_view,
1076 gtk_tree_path_free (anchor_path);
1081 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1083 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1085 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1086 selection->tree_view->priv->anchor = NULL;
1094 tuple = g_new (struct _TempTuple, 1);
1095 tuple->selection = selection;
1096 tuple->dirty = FALSE;
1098 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1099 selection->tree_view->priv->tree->root,
1101 unselect_all_helper,
1115 * gtk_tree_selection_unselect_all:
1116 * @selection: A #GtkTreeSelection.
1118 * Unselects all the nodes.
1121 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1123 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1124 g_return_if_fail (selection->tree_view != NULL);
1125 if (selection->tree_view->priv->tree == NULL)
1128 if (selection->tree_view->priv->tree == NULL)
1131 if (gtk_tree_selection_real_unselect_all (selection))
1132 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1142 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1144 GtkTreePath *start_path,
1145 GtkTreePath *end_path)
1147 GtkRBNode *start_node, *end_node;
1148 GtkRBTree *start_tree, *end_tree;
1149 gboolean dirty = FALSE;
1151 switch (gtk_tree_path_compare (start_path, end_path))
1154 _gtk_tree_view_find_node (selection->tree_view,
1158 _gtk_tree_view_find_node (selection->tree_view,
1164 _gtk_tree_view_find_node (selection->tree_view,
1168 end_tree = start_tree;
1169 end_node = start_node;
1172 _gtk_tree_view_find_node (selection->tree_view,
1176 _gtk_tree_view_find_node (selection->tree_view,
1183 g_return_val_if_fail (start_node != NULL, FALSE);
1184 g_return_val_if_fail (end_node != NULL, FALSE);
1188 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1190 if (start_node == end_node)
1193 if (start_node->children)
1195 start_tree = start_node->children;
1196 start_node = start_tree->root;
1197 while (start_node->left != start_tree->nil)
1198 start_node = start_node->left;
1202 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1203 if (start_tree == NULL)
1205 /* we just ran out of tree. That means someone passed in bogus values.
1217 * gtk_tree_selection_select_range:
1218 * @selection: A #GtkTreeSelection.
1219 * @start_path: The initial node of the range.
1220 * @end_path: The final node of the range.
1222 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1225 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1226 GtkTreePath *start_path,
1227 GtkTreePath *end_path)
1229 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1230 g_return_if_fail (selection->tree_view != NULL);
1232 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1233 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1237 * gtk_tree_selection_unselect_range:
1238 * @selection: A #GtkTreeSelection.
1239 * @start_path: The initial node of the range.
1240 * @end_path: The initial node of the range.
1242 * Unselects a range of nodes, determined by @start_path and @end_path
1246 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1247 GtkTreePath *start_path,
1248 GtkTreePath *end_path)
1250 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1251 g_return_if_fail (selection->tree_view != NULL);
1253 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1254 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1257 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1261 * docs about the 'override_browse_mode', we set this flag when we want to
1262 * unset select the node and override the select browse mode behaviour (that is
1263 * 'one node should *always* be selected').
1266 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1270 GdkModifierType state,
1271 gboolean override_browse_mode)
1275 GtkTreePath *anchor_path = NULL;
1277 if (selection->type == GTK_SELECTION_NONE)
1280 if (selection->tree_view->priv->anchor)
1281 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1283 if (selection->type == GTK_SELECTION_SINGLE ||
1284 selection->type == GTK_SELECTION_BROWSE)
1287 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1289 dirty = gtk_tree_selection_real_unselect_all (selection);
1291 /* Did we try to select the same node again? */
1292 else if (selection->type == GTK_SELECTION_SINGLE &&
1293 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1295 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1297 dirty = gtk_tree_selection_real_unselect_all (selection);
1304 /* We only want to select the new node if we can unselect the old one,
1305 * and we can select the new one. */
1306 if (selection->user_func)
1308 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1309 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1310 selection->user_data))
1318 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1319 * unselect the new one
1322 dirty = gtk_tree_selection_real_unselect_all (selection);
1324 /* if dirty is TRUE at this point, we successfully unselected the
1325 * old one, and can then select the new one */
1328 if (selection->tree_view->priv->anchor)
1329 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1330 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1332 selection->tree_view->priv->anchor =
1333 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1339 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1342 if (selection->tree_view->priv->anchor)
1343 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1345 selection->tree_view->priv->anchor =
1346 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1351 else if (selection->type == GTK_SELECTION_MULTIPLE)
1353 if (((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) && (anchor_path == NULL))
1355 if (selection->tree_view->priv->anchor)
1356 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1358 selection->tree_view->priv->anchor =
1359 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1360 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1362 else if ((state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_SHIFT_MASK|GDK_CONTROL_MASK))
1364 gtk_tree_selection_select_range (selection,
1368 else if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1370 flags = node->flags;
1371 if (selection->tree_view->priv->anchor)
1372 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1374 selection->tree_view->priv->anchor =
1375 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1377 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1378 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1380 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1382 else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
1384 dirty = gtk_tree_selection_real_unselect_all (selection);
1385 dirty |= gtk_tree_selection_real_modify_range (selection,
1392 dirty = gtk_tree_selection_real_unselect_all (selection);
1394 if (selection->tree_view->priv->anchor)
1395 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1397 selection->tree_view->priv->anchor =
1398 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1400 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1405 gtk_tree_path_free (anchor_path);
1408 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1411 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1415 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1420 gboolean selected = FALSE;
1421 GtkTreePath *path = NULL;
1425 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1427 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1428 if (selection->user_func)
1430 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1431 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1432 selection->user_data))
1437 gtk_tree_path_free (path);
1440 if (selected == TRUE)
1442 node->flags ^= GTK_RBNODE_IS_SELECTED;
1444 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);