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.
22 #include "gtktreeselection.h"
23 #include "gtktreeprivate.h"
24 #include "gtkrbtree.h"
25 #include "gtkmarshalers.h"
30 * SECTION:gtktreeselection
31 * @Short_description: The selection object for GtkTreeView
32 * @Title: GtkTreeSelection
33 * @See_also: #GtkTreeView, #GtkTreeViewColumn, #GtkTreeDnd, #GtkTreeMode,
34 * #GtkTreeSortable, #GtkTreeModelSort, #GtkListStore, #GtkTreeStore,
35 * #GtkCellRenderer, #GtkCellEditable, #GtkCellRendererPixbuf,
36 * #GtkCellRendererText, #GtkCellRendererToggle
38 * The #GtkTreeSelection object is a helper object to manage the selection
39 * for a #GtkTreeView widget. The #GtkTreeSelection object is
40 * automatically created when a new #GtkTreeView widget is created, and
41 * cannot exist independentally of this widget. The primary reason the
42 * #GtkTreeSelection objects exists is for cleanliness of code and API.
43 * That is, there is no conceptual reason all these functions could not be
44 * methods on the #GtkTreeView widget instead of a separate function.
46 * The #GtkTreeSelection object is gotten from a #GtkTreeView by calling
47 * gtk_tree_view_get_selection(). It can be manipulated to check the
48 * selection status of the tree, as well as select and deselect individual
49 * rows. Selection is done completely view side. As a result, multiple
50 * views of the same model can have completely different selections.
51 * Additionally, you cannot change the selection of a row on the model that
52 * is not currently displayed by the view without expanding its parents
55 * One of the important things to remember when monitoring the selection of
56 * a view is that the #GtkTreeSelection::changed signal is mostly a hint.
57 * That is,it may only emit one signal when a range of rows is selected.
58 * Additionally, it may on occasion emit a #GtkTreeSelection::changed signal
59 * when nothing has happened (mostly as a result of programmers calling
60 * select_row on an already selected row).
64 static void gtk_tree_selection_finalize (GObject *object);
65 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
66 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
67 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
78 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
80 G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
83 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
85 GObjectClass *object_class;
87 object_class = (GObjectClass*) class;
89 object_class->finalize = gtk_tree_selection_finalize;
90 class->changed = NULL;
93 * GtkTreeSelection::changed:
94 * @treeselection: the object which received the signal.
96 * Emitted whenever the selection has (possibly) changed. Please note that
97 * this signal is mostly a hint. It may only be emitted once when a range
98 * of rows are selected, and it may occasionally be emitted when nothing
101 tree_selection_signals[CHANGED] =
102 g_signal_new (I_("changed"),
103 G_OBJECT_CLASS_TYPE (object_class),
105 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
107 _gtk_marshal_VOID__VOID,
112 gtk_tree_selection_init (GtkTreeSelection *selection)
114 selection->type = GTK_SELECTION_SINGLE;
118 gtk_tree_selection_finalize (GObject *object)
120 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
122 if (selection->destroy)
123 selection->destroy (selection->user_data);
125 /* chain parent_class' handler */
126 G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
130 * _gtk_tree_selection_new:
132 * Creates a new #GtkTreeSelection object. This function should not be invoked,
133 * as each #GtkTreeView will create its own #GtkTreeSelection.
135 * Return value: A newly created #GtkTreeSelection object.
138 _gtk_tree_selection_new (void)
140 GtkTreeSelection *selection;
142 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
148 * _gtk_tree_selection_new_with_tree_view:
149 * @tree_view: The #GtkTreeView.
151 * Creates a new #GtkTreeSelection object. This function should not be invoked,
152 * as each #GtkTreeView will create its own #GtkTreeSelection.
154 * Return value: A newly created #GtkTreeSelection object.
157 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
159 GtkTreeSelection *selection;
161 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
163 selection = _gtk_tree_selection_new ();
164 _gtk_tree_selection_set_tree_view (selection, tree_view);
170 * _gtk_tree_selection_set_tree_view:
171 * @selection: A #GtkTreeSelection.
172 * @tree_view: The #GtkTreeView.
174 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
175 * it is used internally by #GtkTreeView.
178 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
179 GtkTreeView *tree_view)
181 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
182 if (tree_view != NULL)
183 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
185 selection->tree_view = tree_view;
189 * gtk_tree_selection_set_mode:
190 * @selection: A #GtkTreeSelection.
191 * @type: The selection mode
193 * Sets the selection mode of the @selection. If the previous type was
194 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
195 * previously selected.
198 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
199 GtkSelectionMode type)
201 GtkTreeSelectionFunc tmp_func;
202 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
204 if (selection->type == type)
208 if (type == GTK_SELECTION_NONE)
210 /* We do this so that we unconditionally unset all rows
212 tmp_func = selection->user_func;
213 selection->user_func = NULL;
214 gtk_tree_selection_unselect_all (selection);
215 selection->user_func = tmp_func;
217 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
218 selection->tree_view->priv->anchor = NULL;
220 else if (type == GTK_SELECTION_SINGLE ||
221 type == GTK_SELECTION_BROWSE)
223 GtkRBTree *tree = NULL;
224 GtkRBNode *node = NULL;
225 gint selected = FALSE;
226 GtkTreePath *anchor_path = NULL;
228 if (selection->tree_view->priv->anchor)
230 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
234 _gtk_tree_view_find_node (selection->tree_view,
239 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
244 /* We do this so that we unconditionally unset all rows
246 tmp_func = selection->user_func;
247 selection->user_func = NULL;
248 gtk_tree_selection_unselect_all (selection);
249 selection->user_func = tmp_func;
251 if (node && selected)
252 _gtk_tree_selection_internal_select_node (selection,
259 gtk_tree_path_free (anchor_path);
262 selection->type = type;
266 * gtk_tree_selection_get_mode:
267 * @selection: a #GtkTreeSelection
269 * Gets the selection mode for @selection. See
270 * gtk_tree_selection_set_mode().
272 * Return value: the current selection mode
275 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
277 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
279 return selection->type;
283 * gtk_tree_selection_set_select_function:
284 * @selection: A #GtkTreeSelection.
285 * @func: The selection function. May be %NULL
286 * @data: The selection function's data. May be %NULL
287 * @destroy: The destroy function for user data. May be %NULL
289 * Sets the selection function.
291 * If set, this function is called before any node is selected or unselected,
292 * giving some control over which nodes are selected. The select function
293 * should return %TRUE if the state of the node may be toggled, and %FALSE
294 * if the state of the node should be left unchanged.
297 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
298 GtkTreeSelectionFunc func,
300 GDestroyNotify destroy)
302 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
304 if (selection->destroy)
305 selection->destroy (selection->user_data);
307 selection->user_func = func;
308 selection->user_data = data;
309 selection->destroy = destroy;
313 * gtk_tree_selection_get_select_function: (skip)
314 * @selection: A #GtkTreeSelection.
316 * Returns the current selection function.
318 * Return value: The function.
323 gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
325 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
327 return selection->user_func;
331 * gtk_tree_selection_get_user_data: (skip)
332 * @selection: A #GtkTreeSelection.
334 * Returns the user data for the selection function.
336 * Return value: The user data.
339 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
341 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
343 return selection->user_data;
347 * gtk_tree_selection_get_tree_view:
348 * @selection: A #GtkTreeSelection
350 * Returns the tree view associated with @selection.
352 * Return value: (transfer none): A #GtkTreeView
355 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
357 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
359 return selection->tree_view;
363 * gtk_tree_selection_get_selected:
364 * @selection: A #GtkTreeSelection.
365 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or NULL.
366 * @iter: (out) (allow-none): The #GtkTreeIter, or NULL.
368 * Sets @iter to the currently selected node if @selection is set to
369 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
370 * just want to test if @selection has any selected nodes. @model is filled
371 * with the current model as a convenience. This function will not work if you
372 * use @selection is #GTK_SELECTION_MULTIPLE.
374 * Return value: TRUE, if there is a selected node.
377 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
378 GtkTreeModel **model,
383 GtkTreePath *anchor_path;
387 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
388 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
389 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
393 memset (iter, 0, sizeof (GtkTreeIter));
396 *model = selection->tree_view->priv->model;
398 if (selection->tree_view->priv->anchor == NULL)
401 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
403 if (anchor_path == NULL)
408 found_node = !_gtk_tree_view_find_node (selection->tree_view,
413 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
415 /* we only want to return the anchor if it exists in the rbtree and
421 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
427 /* We don't want to return the anchor if it isn't actually selected.
432 gtk_tree_path_free (anchor_path);
438 * gtk_tree_selection_get_selected_rows:
439 * @selection: A #GtkTreeSelection.
440 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or %NULL.
442 * Creates a list of path of all selected rows. Additionally, if you are
443 * planning on modifying the model after calling this function, you may
444 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
445 * To do this, you can use gtk_tree_row_reference_new().
447 * To free the return value, use:
449 * g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
450 * g_list_free (list);
453 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
458 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
459 GtkTreeModel **model)
462 GtkRBTree *tree = NULL;
463 GtkRBNode *node = NULL;
466 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
467 g_return_val_if_fail (selection->tree_view != NULL, NULL);
470 *model = selection->tree_view->priv->model;
472 if (selection->tree_view->priv->tree == NULL ||
473 selection->tree_view->priv->tree->root == NULL)
476 if (selection->type == GTK_SELECTION_NONE)
478 else if (selection->type != GTK_SELECTION_MULTIPLE)
482 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
486 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
487 list = g_list_append (list, path);
495 tree = selection->tree_view->priv->tree;
496 node = selection->tree_view->priv->tree->root;
498 while (node->left != tree->nil)
500 path = gtk_tree_path_new_first ();
504 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
505 list = g_list_prepend (list, gtk_tree_path_copy (path));
509 tree = node->children;
512 while (node->left != tree->nil)
515 gtk_tree_path_append_index (path, 0);
519 gboolean done = FALSE;
523 node = _gtk_rbtree_next (tree, node);
527 gtk_tree_path_next (path);
531 node = tree->parent_node;
532 tree = tree->parent_tree;
536 gtk_tree_path_free (path);
541 gtk_tree_path_up (path);
549 gtk_tree_path_free (path);
552 return g_list_reverse (list);
556 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
560 gint *count = (gint *)data;
562 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
566 _gtk_rbtree_traverse (node->children, node->children->root,
568 gtk_tree_selection_count_selected_rows_helper, data);
572 * gtk_tree_selection_count_selected_rows:
573 * @selection: A #GtkTreeSelection.
575 * Returns the number of rows that have been selected in @tree.
577 * Return value: The number of rows selected.
582 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
586 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
587 g_return_val_if_fail (selection->tree_view != NULL, 0);
589 if (selection->tree_view->priv->tree == NULL ||
590 selection->tree_view->priv->tree->root == NULL)
593 if (selection->type == GTK_SELECTION_SINGLE ||
594 selection->type == GTK_SELECTION_BROWSE)
596 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
602 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
603 selection->tree_view->priv->tree->root,
605 gtk_tree_selection_count_selected_rows_helper,
611 /* gtk_tree_selection_selected_foreach helper */
613 model_changed (gpointer data)
615 gboolean *stop = (gboolean *)data;
621 * gtk_tree_selection_selected_foreach:
622 * @selection: A #GtkTreeSelection.
623 * @func: (scope call): The function to call for each selected node.
624 * @data: user data to pass to the function.
626 * Calls a function for each selected node. Note that you cannot modify
627 * the tree or selection from within this function. As a result,
628 * gtk_tree_selection_get_selected_rows() might be more useful.
631 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
632 GtkTreeSelectionForeachFunc func,
641 gulong inserted_id, deleted_id, reordered_id, changed_id;
642 gboolean stop = FALSE;
644 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
645 g_return_if_fail (selection->tree_view != NULL);
648 selection->tree_view->priv->tree == NULL ||
649 selection->tree_view->priv->tree->root == NULL)
652 if (selection->type == GTK_SELECTION_SINGLE ||
653 selection->type == GTK_SELECTION_BROWSE)
655 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
657 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
658 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
659 (* func) (selection->tree_view->priv->model, path, &iter, data);
660 gtk_tree_path_free (path);
665 tree = selection->tree_view->priv->tree;
666 node = selection->tree_view->priv->tree->root;
668 while (node->left != tree->nil)
671 model = selection->tree_view->priv->model;
672 g_object_ref (model);
674 /* connect to signals to monitor changes in treemodel */
675 inserted_id = g_signal_connect_swapped (model, "row-inserted",
676 G_CALLBACK (model_changed),
678 deleted_id = g_signal_connect_swapped (model, "row-deleted",
679 G_CALLBACK (model_changed),
681 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
682 G_CALLBACK (model_changed),
684 changed_id = g_signal_connect_swapped (selection->tree_view, "notify::model",
685 G_CALLBACK (model_changed),
688 /* find the node internally */
689 path = gtk_tree_path_new_first ();
693 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
695 gtk_tree_model_get_iter (model, &iter, path);
696 (* func) (model, path, &iter, data);
704 tree = node->children;
707 while (node->left != tree->nil)
710 gtk_tree_path_append_index (path, 0);
714 gboolean done = FALSE;
718 node = _gtk_rbtree_next (tree, node);
722 gtk_tree_path_next (path);
726 node = tree->parent_node;
727 tree = tree->parent_tree;
731 /* we've run out of tree */
732 /* We're done with this function */
737 gtk_tree_path_up (path);
747 gtk_tree_path_free (path);
749 g_signal_handler_disconnect (model, inserted_id);
750 g_signal_handler_disconnect (model, deleted_id);
751 g_signal_handler_disconnect (model, reordered_id);
752 g_signal_handler_disconnect (selection->tree_view, changed_id);
753 g_object_unref (model);
755 /* check if we have to spew a scary message */
757 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
758 "This function is for observing the selections of the tree only. If\n"
759 "you are trying to get all selected items from the tree, try using\n"
760 "gtk_tree_selection_get_selected_rows instead.\n");
764 * gtk_tree_selection_select_path:
765 * @selection: A #GtkTreeSelection.
766 * @path: The #GtkTreePath to be selected.
768 * Select the row at @path.
771 gtk_tree_selection_select_path (GtkTreeSelection *selection,
777 GtkTreeSelectMode mode = 0;
779 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
780 g_return_if_fail (selection->tree_view != NULL);
781 g_return_if_fail (path != NULL);
783 ret = _gtk_tree_view_find_node (selection->tree_view,
788 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
792 if (selection->type == GTK_SELECTION_MULTIPLE)
793 mode = GTK_TREE_SELECT_MODE_TOGGLE;
795 _gtk_tree_selection_internal_select_node (selection,
804 * gtk_tree_selection_unselect_path:
805 * @selection: A #GtkTreeSelection.
806 * @path: The #GtkTreePath to be unselected.
808 * Unselects the row at @path.
811 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
818 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
819 g_return_if_fail (selection->tree_view != NULL);
820 g_return_if_fail (path != NULL);
822 ret = _gtk_tree_view_find_node (selection->tree_view,
827 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
831 _gtk_tree_selection_internal_select_node (selection,
835 GTK_TREE_SELECT_MODE_TOGGLE,
840 * gtk_tree_selection_select_iter:
841 * @selection: A #GtkTreeSelection.
842 * @iter: The #GtkTreeIter to be selected.
844 * Selects the specified iterator.
847 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
852 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
853 g_return_if_fail (selection->tree_view != NULL);
854 g_return_if_fail (selection->tree_view->priv->model != NULL);
855 g_return_if_fail (iter != NULL);
857 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
863 gtk_tree_selection_select_path (selection, path);
864 gtk_tree_path_free (path);
869 * gtk_tree_selection_unselect_iter:
870 * @selection: A #GtkTreeSelection.
871 * @iter: The #GtkTreeIter to be unselected.
873 * Unselects the specified iterator.
876 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
881 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
882 g_return_if_fail (selection->tree_view != NULL);
883 g_return_if_fail (selection->tree_view->priv->model != NULL);
884 g_return_if_fail (iter != NULL);
886 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
892 gtk_tree_selection_unselect_path (selection, path);
893 gtk_tree_path_free (path);
897 * gtk_tree_selection_path_is_selected:
898 * @selection: A #GtkTreeSelection.
899 * @path: A #GtkTreePath to check selection on.
901 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
902 * does not point to a valid location, %FALSE is returned
904 * Return value: %TRUE if @path is selected.
907 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
914 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
915 g_return_val_if_fail (path != NULL, FALSE);
916 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
918 if (selection->tree_view->priv->model == NULL)
921 ret = _gtk_tree_view_find_node (selection->tree_view,
926 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
934 * gtk_tree_selection_iter_is_selected:
935 * @selection: A #GtkTreeSelection
936 * @iter: A valid #GtkTreeIter
938 * Returns %TRUE if the row at @iter is currently selected.
940 * Return value: %TRUE, if @iter is selected
943 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
949 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
950 g_return_val_if_fail (iter != NULL, FALSE);
951 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
952 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
954 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
958 retval = gtk_tree_selection_path_is_selected (selection, path);
959 gtk_tree_path_free (path);
965 /* Wish I was in python, right now... */
967 GtkTreeSelection *selection;
972 select_all_helper (GtkRBTree *tree,
976 struct _TempTuple *tuple = data;
979 _gtk_rbtree_traverse (node->children,
980 node->children->root,
984 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
986 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
991 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
992 * can use it in other places without fear of the signal being emitted.
995 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
997 struct _TempTuple *tuple;
999 if (selection->tree_view->priv->tree == NULL)
1002 /* Mark all nodes selected */
1003 tuple = g_new (struct _TempTuple, 1);
1004 tuple->selection = selection;
1005 tuple->dirty = FALSE;
1007 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1008 selection->tree_view->priv->tree->root,
1022 * gtk_tree_selection_select_all:
1023 * @selection: A #GtkTreeSelection.
1025 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1029 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1031 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1032 g_return_if_fail (selection->tree_view != NULL);
1034 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1037 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1039 if (gtk_tree_selection_real_select_all (selection))
1040 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1044 unselect_all_helper (GtkRBTree *tree,
1048 struct _TempTuple *tuple = data;
1051 _gtk_rbtree_traverse (node->children,
1052 node->children->root,
1054 unselect_all_helper,
1056 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1058 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1063 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1065 struct _TempTuple *tuple;
1067 if (selection->type == GTK_SELECTION_SINGLE ||
1068 selection->type == GTK_SELECTION_BROWSE)
1070 GtkRBTree *tree = NULL;
1071 GtkRBNode *node = NULL;
1072 GtkTreePath *anchor_path;
1074 if (selection->tree_view->priv->anchor == NULL)
1077 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1079 if (anchor_path == NULL)
1082 _gtk_tree_view_find_node (selection->tree_view,
1087 gtk_tree_path_free (anchor_path);
1092 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1094 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1096 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1097 selection->tree_view->priv->anchor = NULL;
1105 tuple = g_new (struct _TempTuple, 1);
1106 tuple->selection = selection;
1107 tuple->dirty = FALSE;
1109 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1110 selection->tree_view->priv->tree->root,
1112 unselect_all_helper,
1126 * gtk_tree_selection_unselect_all:
1127 * @selection: A #GtkTreeSelection.
1129 * Unselects all the nodes.
1132 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1134 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1135 g_return_if_fail (selection->tree_view != NULL);
1137 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1140 if (gtk_tree_selection_real_unselect_all (selection))
1141 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1151 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1153 GtkTreePath *start_path,
1154 GtkTreePath *end_path)
1156 GtkRBNode *start_node, *end_node;
1157 GtkRBTree *start_tree, *end_tree;
1158 GtkTreePath *anchor_path = NULL;
1159 gboolean dirty = FALSE;
1161 switch (gtk_tree_path_compare (start_path, end_path))
1164 _gtk_tree_view_find_node (selection->tree_view,
1168 _gtk_tree_view_find_node (selection->tree_view,
1172 anchor_path = start_path;
1175 _gtk_tree_view_find_node (selection->tree_view,
1179 end_tree = start_tree;
1180 end_node = start_node;
1181 anchor_path = start_path;
1184 _gtk_tree_view_find_node (selection->tree_view,
1188 _gtk_tree_view_find_node (selection->tree_view,
1192 anchor_path = start_path;
1196 g_return_val_if_fail (start_node != NULL, FALSE);
1197 g_return_val_if_fail (end_node != NULL, FALSE);
1201 if (selection->tree_view->priv->anchor)
1202 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1204 selection->tree_view->priv->anchor =
1205 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1206 selection->tree_view->priv->model,
1212 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1214 if (start_node == end_node)
1217 if (start_node->children)
1219 start_tree = start_node->children;
1220 start_node = start_tree->root;
1221 while (start_node->left != start_tree->nil)
1222 start_node = start_node->left;
1226 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1227 if (start_tree == NULL)
1229 /* we just ran out of tree. That means someone passed in bogus values.
1241 * gtk_tree_selection_select_range:
1242 * @selection: A #GtkTreeSelection.
1243 * @start_path: The initial node of the range.
1244 * @end_path: The final node of the range.
1246 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1247 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1250 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1251 GtkTreePath *start_path,
1252 GtkTreePath *end_path)
1254 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1255 g_return_if_fail (selection->tree_view != NULL);
1256 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1257 g_return_if_fail (selection->tree_view->priv->model != NULL);
1259 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1260 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1264 * gtk_tree_selection_unselect_range:
1265 * @selection: A #GtkTreeSelection.
1266 * @start_path: The initial node of the range.
1267 * @end_path: The initial node of the range.
1269 * Unselects a range of nodes, determined by @start_path and @end_path
1275 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1276 GtkTreePath *start_path,
1277 GtkTreePath *end_path)
1279 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1280 g_return_if_fail (selection->tree_view != NULL);
1281 g_return_if_fail (selection->tree_view->priv->model != NULL);
1283 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1284 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1288 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1293 gboolean sensitive = FALSE;
1295 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1298 if (!sensitive && selection->tree_view->priv->row_separator_func)
1300 /* never allow separators to be selected */
1301 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1303 selection->tree_view->priv->row_separator_data))
1307 if (selection->user_func)
1308 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1309 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1310 selection->user_data);
1316 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1320 * docs about the 'override_browse_mode', we set this flag when we want to
1321 * unset select the node and override the select browse mode behaviour (that is
1322 * 'one node should *always* be selected').
1325 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1329 GtkTreeSelectMode mode,
1330 gboolean override_browse_mode)
1334 GtkTreePath *anchor_path = NULL;
1336 if (selection->type == GTK_SELECTION_NONE)
1339 if (selection->tree_view->priv->anchor)
1340 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1342 if (selection->type == GTK_SELECTION_SINGLE ||
1343 selection->type == GTK_SELECTION_BROWSE)
1346 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1348 dirty = gtk_tree_selection_real_unselect_all (selection);
1350 /* Did we try to select the same node again? */
1351 else if (selection->type == GTK_SELECTION_SINGLE &&
1352 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1354 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1356 dirty = gtk_tree_selection_real_unselect_all (selection);
1363 /* We only want to select the new node if we can unselect the old one,
1364 * and we can select the new one. */
1365 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1367 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1368 * unselect the new one
1371 dirty = gtk_tree_selection_real_unselect_all (selection);
1373 /* if dirty is TRUE at this point, we successfully unselected the
1374 * old one, and can then select the new one */
1377 if (selection->tree_view->priv->anchor)
1379 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1380 selection->tree_view->priv->anchor = NULL;
1383 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1385 selection->tree_view->priv->anchor =
1386 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1392 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1395 if (selection->tree_view->priv->anchor)
1396 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1398 selection->tree_view->priv->anchor =
1399 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1404 else if (selection->type == GTK_SELECTION_MULTIPLE)
1406 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1407 && (anchor_path == NULL))
1409 if (selection->tree_view->priv->anchor)
1410 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1412 selection->tree_view->priv->anchor =
1413 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1414 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1416 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1418 gtk_tree_selection_select_range (selection,
1422 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1424 flags = node->flags;
1425 if (selection->tree_view->priv->anchor)
1426 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1428 selection->tree_view->priv->anchor =
1429 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1431 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1432 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1434 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1436 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1438 dirty = gtk_tree_selection_real_unselect_all (selection);
1439 dirty |= gtk_tree_selection_real_modify_range (selection,
1446 dirty = gtk_tree_selection_real_unselect_all (selection);
1448 if (selection->tree_view->priv->anchor)
1449 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1451 selection->tree_view->priv->anchor =
1452 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1454 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1459 gtk_tree_path_free (anchor_path);
1462 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1467 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1469 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1472 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1476 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1481 gboolean toggle = FALSE;
1482 GtkTreePath *path = NULL;
1486 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1488 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1489 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1490 gtk_tree_path_free (path);
1495 node->flags ^= GTK_RBNODE_IS_SELECTED;
1497 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);