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_view_set_anchor_path (selection->tree_view, NULL);
219 else if (type == GTK_SELECTION_SINGLE ||
220 type == GTK_SELECTION_BROWSE)
222 GtkRBTree *tree = NULL;
223 GtkRBNode *node = NULL;
224 gint selected = FALSE;
225 GtkTreePath *anchor_path = NULL;
227 anchor_path = _gtk_tree_view_get_anchor_path (selection->tree_view);
231 _gtk_tree_view_find_node (selection->tree_view,
236 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
240 /* We do this so that we unconditionally unset all rows
242 tmp_func = selection->user_func;
243 selection->user_func = NULL;
244 gtk_tree_selection_unselect_all (selection);
245 selection->user_func = tmp_func;
247 if (node && selected)
248 _gtk_tree_selection_internal_select_node (selection,
255 gtk_tree_path_free (anchor_path);
258 selection->type = type;
262 * gtk_tree_selection_get_mode:
263 * @selection: a #GtkTreeSelection
265 * Gets the selection mode for @selection. See
266 * gtk_tree_selection_set_mode().
268 * Return value: the current selection mode
271 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
273 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
275 return selection->type;
279 * gtk_tree_selection_set_select_function:
280 * @selection: A #GtkTreeSelection.
281 * @func: The selection function. May be %NULL
282 * @data: The selection function's data. May be %NULL
283 * @destroy: The destroy function for user data. May be %NULL
285 * Sets the selection function.
287 * If set, this function is called before any node is selected or unselected,
288 * giving some control over which nodes are selected. The select function
289 * should return %TRUE if the state of the node may be toggled, and %FALSE
290 * if the state of the node should be left unchanged.
293 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
294 GtkTreeSelectionFunc func,
296 GDestroyNotify destroy)
298 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
300 if (selection->destroy)
301 selection->destroy (selection->user_data);
303 selection->user_func = func;
304 selection->user_data = data;
305 selection->destroy = destroy;
309 * gtk_tree_selection_get_select_function: (skip)
310 * @selection: A #GtkTreeSelection.
312 * Returns the current selection function.
314 * Return value: The function.
319 gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
321 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
323 return selection->user_func;
327 * gtk_tree_selection_get_user_data: (skip)
328 * @selection: A #GtkTreeSelection.
330 * Returns the user data for the selection function.
332 * Return value: The user data.
335 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
337 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
339 return selection->user_data;
343 * gtk_tree_selection_get_tree_view:
344 * @selection: A #GtkTreeSelection
346 * Returns the tree view associated with @selection.
348 * Return value: (transfer none): A #GtkTreeView
351 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
353 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
355 return selection->tree_view;
359 * gtk_tree_selection_get_selected:
360 * @selection: A #GtkTreeSelection.
361 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or NULL.
362 * @iter: (out) (allow-none): The #GtkTreeIter, or NULL.
364 * Sets @iter to the currently selected node if @selection is set to
365 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
366 * just want to test if @selection has any selected nodes. @model is filled
367 * with the current model as a convenience. This function will not work if you
368 * use @selection is #GTK_SELECTION_MULTIPLE.
370 * Return value: TRUE, if there is a selected node.
373 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
374 GtkTreeModel **model,
379 GtkTreePath *anchor_path;
383 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
384 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
385 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
389 memset (iter, 0, sizeof (GtkTreeIter));
392 *model = gtk_tree_view_get_model (selection->tree_view);
394 anchor_path = _gtk_tree_view_get_anchor_path (selection->tree_view);
396 if (anchor_path == NULL)
401 found_node = !_gtk_tree_view_find_node (selection->tree_view,
406 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
408 /* we only want to return the anchor if it exists in the rbtree and
414 retval = gtk_tree_model_get_iter (gtk_tree_view_get_model (selection->tree_view),
420 /* We don't want to return the anchor if it isn't actually selected.
425 gtk_tree_path_free (anchor_path);
431 * gtk_tree_selection_get_selected_rows:
432 * @selection: A #GtkTreeSelection.
433 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or %NULL.
435 * Creates a list of path of all selected rows. Additionally, if you are
436 * planning on modifying the model after calling this function, you may
437 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
438 * To do this, you can use gtk_tree_row_reference_new().
440 * To free the return value, use:
442 * g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
443 * g_list_free (list);
446 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
451 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
452 GtkTreeModel **model)
455 GtkRBTree *tree = NULL;
456 GtkRBNode *node = NULL;
459 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
460 g_return_val_if_fail (selection->tree_view != NULL, NULL);
463 *model = gtk_tree_view_get_model (selection->tree_view);
465 tree = _gtk_tree_view_get_rbtree (selection->tree_view);
467 if (tree == NULL || tree->root == NULL)
470 if (selection->type == GTK_SELECTION_NONE)
472 else if (selection->type != GTK_SELECTION_MULTIPLE)
476 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
480 path = gtk_tree_model_get_path (gtk_tree_view_get_model (selection->tree_view), &iter);
481 list = g_list_append (list, path);
491 while (node->left != tree->nil)
493 path = gtk_tree_path_new_first ();
497 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
498 list = g_list_prepend (list, gtk_tree_path_copy (path));
502 tree = node->children;
505 while (node->left != tree->nil)
508 gtk_tree_path_append_index (path, 0);
512 gboolean done = FALSE;
516 node = _gtk_rbtree_next (tree, node);
520 gtk_tree_path_next (path);
524 node = tree->parent_node;
525 tree = tree->parent_tree;
529 gtk_tree_path_free (path);
534 gtk_tree_path_up (path);
542 gtk_tree_path_free (path);
545 return g_list_reverse (list);
549 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
553 gint *count = (gint *)data;
555 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
559 _gtk_rbtree_traverse (node->children, node->children->root,
561 gtk_tree_selection_count_selected_rows_helper, data);
565 * gtk_tree_selection_count_selected_rows:
566 * @selection: A #GtkTreeSelection.
568 * Returns the number of rows that have been selected in @tree.
570 * Return value: The number of rows selected.
575 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
580 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
581 g_return_val_if_fail (selection->tree_view != NULL, 0);
583 tree = _gtk_tree_view_get_rbtree (selection->tree_view);
585 if (tree == NULL || tree->root == NULL)
588 if (selection->type == GTK_SELECTION_SINGLE ||
589 selection->type == GTK_SELECTION_BROWSE)
591 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
597 _gtk_rbtree_traverse (tree, tree->root,
599 gtk_tree_selection_count_selected_rows_helper,
605 /* gtk_tree_selection_selected_foreach helper */
607 model_changed (gpointer data)
609 gboolean *stop = (gboolean *)data;
615 * gtk_tree_selection_selected_foreach:
616 * @selection: A #GtkTreeSelection.
617 * @func: (scope call): The function to call for each selected node.
618 * @data: user data to pass to the function.
620 * Calls a function for each selected node. Note that you cannot modify
621 * the tree or selection from within this function. As a result,
622 * gtk_tree_selection_get_selected_rows() might be more useful.
625 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
626 GtkTreeSelectionForeachFunc func,
635 gulong inserted_id, deleted_id, reordered_id, changed_id;
636 gboolean stop = FALSE;
638 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
639 g_return_if_fail (selection->tree_view != NULL);
641 tree = _gtk_tree_view_get_rbtree (selection->tree_view);
643 if (func == NULL || tree == NULL || tree->root == NULL)
646 model = gtk_tree_view_get_model (selection->tree_view);
648 if (selection->type == GTK_SELECTION_SINGLE ||
649 selection->type == GTK_SELECTION_BROWSE)
651 path = _gtk_tree_view_get_anchor_path (selection->tree_view);
655 gtk_tree_model_get_iter (model, &iter, path);
656 (* func) (model, path, &iter, data);
657 gtk_tree_path_free (path);
664 while (node->left != tree->nil)
667 g_object_ref (model);
669 /* connect to signals to monitor changes in treemodel */
670 inserted_id = g_signal_connect_swapped (model, "row-inserted",
671 G_CALLBACK (model_changed),
673 deleted_id = g_signal_connect_swapped (model, "row-deleted",
674 G_CALLBACK (model_changed),
676 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
677 G_CALLBACK (model_changed),
679 changed_id = g_signal_connect_swapped (selection->tree_view, "notify::model",
680 G_CALLBACK (model_changed),
683 /* find the node internally */
684 path = gtk_tree_path_new_first ();
688 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
690 gtk_tree_model_get_iter (model, &iter, path);
691 (* func) (model, path, &iter, data);
699 tree = node->children;
702 while (node->left != tree->nil)
705 gtk_tree_path_append_index (path, 0);
709 gboolean done = FALSE;
713 node = _gtk_rbtree_next (tree, node);
717 gtk_tree_path_next (path);
721 node = tree->parent_node;
722 tree = tree->parent_tree;
726 /* we've run out of tree */
727 /* We're done with this function */
732 gtk_tree_path_up (path);
742 gtk_tree_path_free (path);
744 g_signal_handler_disconnect (model, inserted_id);
745 g_signal_handler_disconnect (model, deleted_id);
746 g_signal_handler_disconnect (model, reordered_id);
747 g_signal_handler_disconnect (selection->tree_view, changed_id);
748 g_object_unref (model);
750 /* check if we have to spew a scary message */
752 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
753 "This function is for observing the selections of the tree only. If\n"
754 "you are trying to get all selected items from the tree, try using\n"
755 "gtk_tree_selection_get_selected_rows instead.\n");
759 * gtk_tree_selection_select_path:
760 * @selection: A #GtkTreeSelection.
761 * @path: The #GtkTreePath to be selected.
763 * Select the row at @path.
766 gtk_tree_selection_select_path (GtkTreeSelection *selection,
772 GtkTreeSelectMode mode = 0;
774 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
775 g_return_if_fail (selection->tree_view != NULL);
776 g_return_if_fail (path != NULL);
778 ret = _gtk_tree_view_find_node (selection->tree_view,
783 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
787 if (selection->type == GTK_SELECTION_MULTIPLE)
788 mode = GTK_TREE_SELECT_MODE_TOGGLE;
790 _gtk_tree_selection_internal_select_node (selection,
799 * gtk_tree_selection_unselect_path:
800 * @selection: A #GtkTreeSelection.
801 * @path: The #GtkTreePath to be unselected.
803 * Unselects the row at @path.
806 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
813 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
814 g_return_if_fail (selection->tree_view != NULL);
815 g_return_if_fail (path != NULL);
817 ret = _gtk_tree_view_find_node (selection->tree_view,
822 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
826 _gtk_tree_selection_internal_select_node (selection,
830 GTK_TREE_SELECT_MODE_TOGGLE,
835 * gtk_tree_selection_select_iter:
836 * @selection: A #GtkTreeSelection.
837 * @iter: The #GtkTreeIter to be selected.
839 * Selects the specified iterator.
842 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
848 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
849 g_return_if_fail (selection->tree_view != NULL);
851 model = gtk_tree_view_get_model (selection->tree_view);
852 g_return_if_fail (model != NULL);
853 g_return_if_fail (iter != NULL);
855 path = gtk_tree_model_get_path (model, iter);
860 gtk_tree_selection_select_path (selection, path);
861 gtk_tree_path_free (path);
866 * gtk_tree_selection_unselect_iter:
867 * @selection: A #GtkTreeSelection.
868 * @iter: The #GtkTreeIter to be unselected.
870 * Unselects the specified iterator.
873 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
879 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
880 g_return_if_fail (selection->tree_view != NULL);
882 model = gtk_tree_view_get_model (selection->tree_view);
883 g_return_if_fail (model != NULL);
884 g_return_if_fail (iter != NULL);
886 path = gtk_tree_model_get_path (model, iter);
891 gtk_tree_selection_unselect_path (selection, path);
892 gtk_tree_path_free (path);
896 * gtk_tree_selection_path_is_selected:
897 * @selection: A #GtkTreeSelection.
898 * @path: A #GtkTreePath to check selection on.
900 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
901 * does not point to a valid location, %FALSE is returned
903 * Return value: %TRUE if @path is selected.
906 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
913 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
914 g_return_val_if_fail (path != NULL, FALSE);
915 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
917 if (gtk_tree_view_get_model (selection->tree_view) == NULL)
920 ret = _gtk_tree_view_find_node (selection->tree_view,
925 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
933 * gtk_tree_selection_iter_is_selected:
934 * @selection: A #GtkTreeSelection
935 * @iter: A valid #GtkTreeIter
937 * Returns %TRUE if the row at @iter is currently selected.
939 * Return value: %TRUE, if @iter is selected
942 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);
953 model = gtk_tree_view_get_model (selection->tree_view);
954 g_return_val_if_fail (model != NULL, FALSE);
956 path = gtk_tree_model_get_path (model, iter);
960 retval = gtk_tree_selection_path_is_selected (selection, path);
961 gtk_tree_path_free (path);
967 /* Wish I was in python, right now... */
969 GtkTreeSelection *selection;
974 select_all_helper (GtkRBTree *tree,
978 struct _TempTuple *tuple = data;
981 _gtk_rbtree_traverse (node->children,
982 node->children->root,
986 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
988 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
993 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
994 * can use it in other places without fear of the signal being emitted.
997 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
999 struct _TempTuple *tuple;
1002 tree = _gtk_tree_view_get_rbtree (selection->tree_view);
1007 /* Mark all nodes selected */
1008 tuple = g_new (struct _TempTuple, 1);
1009 tuple->selection = selection;
1010 tuple->dirty = FALSE;
1012 _gtk_rbtree_traverse (tree, tree->root,
1026 * gtk_tree_selection_select_all:
1027 * @selection: A #GtkTreeSelection.
1029 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1033 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1035 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1036 g_return_if_fail (selection->tree_view != NULL);
1038 if (_gtk_tree_view_get_rbtree (selection->tree_view) == NULL ||
1039 gtk_tree_view_get_model (selection->tree_view) == NULL)
1042 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1044 if (gtk_tree_selection_real_select_all (selection))
1045 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1049 unselect_all_helper (GtkRBTree *tree,
1053 struct _TempTuple *tuple = data;
1056 _gtk_rbtree_traverse (node->children,
1057 node->children->root,
1059 unselect_all_helper,
1061 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1063 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1068 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1070 struct _TempTuple *tuple;
1072 if (selection->type == GTK_SELECTION_SINGLE ||
1073 selection->type == GTK_SELECTION_BROWSE)
1075 GtkRBTree *tree = NULL;
1076 GtkRBNode *node = NULL;
1077 GtkTreePath *anchor_path;
1079 anchor_path = _gtk_tree_view_get_anchor_path (selection->tree_view);
1081 if (anchor_path == NULL)
1084 _gtk_tree_view_find_node (selection->tree_view,
1089 gtk_tree_path_free (anchor_path);
1094 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1096 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1098 _gtk_tree_view_set_anchor_path (selection->tree_view, NULL);
1108 tuple = g_new (struct _TempTuple, 1);
1109 tuple->selection = selection;
1110 tuple->dirty = FALSE;
1112 tree = _gtk_tree_view_get_rbtree (selection->tree_view);
1113 _gtk_rbtree_traverse (tree, tree->root,
1115 unselect_all_helper,
1129 * gtk_tree_selection_unselect_all:
1130 * @selection: A #GtkTreeSelection.
1132 * Unselects all the nodes.
1135 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1137 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1138 g_return_if_fail (selection->tree_view != NULL);
1140 if (_gtk_tree_view_get_rbtree (selection->tree_view) == NULL ||
1141 gtk_tree_view_get_model (selection->tree_view) == NULL)
1144 if (gtk_tree_selection_real_unselect_all (selection))
1145 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1155 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1157 GtkTreePath *start_path,
1158 GtkTreePath *end_path)
1160 GtkRBNode *start_node, *end_node;
1161 GtkRBTree *start_tree, *end_tree;
1162 GtkTreePath *anchor_path = NULL;
1163 gboolean dirty = FALSE;
1165 switch (gtk_tree_path_compare (start_path, end_path))
1168 _gtk_tree_view_find_node (selection->tree_view,
1172 _gtk_tree_view_find_node (selection->tree_view,
1176 anchor_path = start_path;
1179 _gtk_tree_view_find_node (selection->tree_view,
1183 end_tree = start_tree;
1184 end_node = start_node;
1185 anchor_path = start_path;
1188 _gtk_tree_view_find_node (selection->tree_view,
1192 _gtk_tree_view_find_node (selection->tree_view,
1196 anchor_path = start_path;
1200 g_return_val_if_fail (start_node != NULL, FALSE);
1201 g_return_val_if_fail (end_node != NULL, FALSE);
1204 _gtk_tree_view_set_anchor_path (selection->tree_view, anchor_path);
1208 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1210 if (start_node == end_node)
1213 if (start_node->children)
1215 start_tree = start_node->children;
1216 start_node = start_tree->root;
1217 while (start_node->left != start_tree->nil)
1218 start_node = start_node->left;
1222 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1223 if (start_tree == NULL)
1225 /* we just ran out of tree. That means someone passed in bogus values.
1237 * gtk_tree_selection_select_range:
1238 * @selection: A #GtkTreeSelection.
1239 * @start_path: The initial node of the range.
1240 * @end_path: The final node of the range.
1242 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1243 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1246 gtk_tree_selection_select_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);
1252 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1253 g_return_if_fail (gtk_tree_view_get_model (selection->tree_view) != NULL);
1255 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1256 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1260 * gtk_tree_selection_unselect_range:
1261 * @selection: A #GtkTreeSelection.
1262 * @start_path: The initial node of the range.
1263 * @end_path: The initial node of the range.
1265 * Unselects a range of nodes, determined by @start_path and @end_path
1271 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1272 GtkTreePath *start_path,
1273 GtkTreePath *end_path)
1275 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1276 g_return_if_fail (selection->tree_view != NULL);
1277 g_return_if_fail (gtk_tree_view_get_model (selection->tree_view) != NULL);
1279 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1280 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1284 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1289 GtkTreeModel *model;
1290 GtkTreeViewRowSeparatorFunc separator_func;
1291 gpointer separator_data;
1292 gboolean sensitive = FALSE;
1294 model = gtk_tree_view_get_model (selection->tree_view);
1296 _gtk_tree_view_get_row_separator_func (selection->tree_view,
1297 &separator_func, &separator_data);
1299 if (!gtk_tree_model_get_iter (model, &iter, path))
1302 if (!sensitive && separator_func)
1304 /* never allow separators to be selected */
1305 if ((* separator_func) (model, &iter, separator_data))
1309 if (selection->user_func)
1310 return (*selection->user_func) (selection, model, path,
1311 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1312 selection->user_data);
1318 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1322 * docs about the 'override_browse_mode', we set this flag when we want to
1323 * unset select the node and override the select browse mode behaviour (that is
1324 * 'one node should *always* be selected').
1327 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1331 GtkTreeSelectMode mode,
1332 gboolean override_browse_mode)
1336 GtkTreePath *anchor_path = NULL;
1338 if (selection->type == GTK_SELECTION_NONE)
1341 anchor_path = _gtk_tree_view_get_anchor_path (selection->tree_view);
1343 if (selection->type == GTK_SELECTION_SINGLE ||
1344 selection->type == GTK_SELECTION_BROWSE)
1347 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1349 dirty = gtk_tree_selection_real_unselect_all (selection);
1351 /* Did we try to select the same node again? */
1352 else if (selection->type == GTK_SELECTION_SINGLE &&
1353 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1355 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1357 dirty = gtk_tree_selection_real_unselect_all (selection);
1364 /* We only want to select the new node if we can unselect the old one,
1365 * and we can select the new one. */
1366 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1368 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1369 * unselect the new one
1372 dirty = gtk_tree_selection_real_unselect_all (selection);
1374 /* if dirty is TRUE at this point, we successfully unselected the
1375 * old one, and can then select the new one */
1379 _gtk_tree_view_set_anchor_path (selection->tree_view, NULL);
1381 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1382 _gtk_tree_view_set_anchor_path (selection->tree_view, path);
1387 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1391 _gtk_tree_view_set_anchor_path (selection->tree_view, path);
1396 else if (selection->type == GTK_SELECTION_MULTIPLE)
1398 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1399 && (anchor_path == NULL))
1401 _gtk_tree_view_set_anchor_path (selection->tree_view, path);
1403 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1405 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1407 gtk_tree_selection_select_range (selection,
1411 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1413 flags = node->flags;
1415 _gtk_tree_view_set_anchor_path (selection->tree_view, path);
1417 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1418 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1420 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1422 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1424 dirty = gtk_tree_selection_real_unselect_all (selection);
1425 dirty |= gtk_tree_selection_real_modify_range (selection,
1432 dirty = gtk_tree_selection_real_unselect_all (selection);
1434 _gtk_tree_view_set_anchor_path (selection->tree_view, path);
1436 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1441 gtk_tree_path_free (anchor_path);
1444 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1449 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1451 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1454 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1458 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1463 gboolean toggle = FALSE;
1464 GtkTreePath *path = NULL;
1468 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1470 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1471 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1472 gtk_tree_path_free (path);
1477 node->flags ^= GTK_RBNODE_IS_SELECTED;
1479 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);