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"
27 #include "gtktypebuiltins.h"
28 #include "a11y/gtktreeviewaccessible.h"
32 * SECTION:gtktreeselection
33 * @Short_description: The selection object for GtkTreeView
34 * @Title: GtkTreeSelection
35 * @See_also: #GtkTreeView, #GtkTreeViewColumn, #GtkTreeDnd, #GtkTreeMode,
36 * #GtkTreeSortable, #GtkTreeModelSort, #GtkListStore, #GtkTreeStore,
37 * #GtkCellRenderer, #GtkCellEditable, #GtkCellRendererPixbuf,
38 * #GtkCellRendererText, #GtkCellRendererToggle
40 * The #GtkTreeSelection object is a helper object to manage the selection
41 * for a #GtkTreeView widget. The #GtkTreeSelection object is
42 * automatically created when a new #GtkTreeView widget is created, and
43 * cannot exist independentally of this widget. The primary reason the
44 * #GtkTreeSelection objects exists is for cleanliness of code and API.
45 * That is, there is no conceptual reason all these functions could not be
46 * methods on the #GtkTreeView widget instead of a separate function.
48 * The #GtkTreeSelection object is gotten from a #GtkTreeView by calling
49 * gtk_tree_view_get_selection(). It can be manipulated to check the
50 * selection status of the tree, as well as select and deselect individual
51 * rows. Selection is done completely view side. As a result, multiple
52 * views of the same model can have completely different selections.
53 * Additionally, you cannot change the selection of a row on the model that
54 * is not currently displayed by the view without expanding its parents
57 * One of the important things to remember when monitoring the selection of
58 * a view is that the #GtkTreeSelection::changed signal is mostly a hint.
59 * That is,it may only emit one signal when a range of rows is selected.
60 * Additionally, it may on occasion emit a #GtkTreeSelection::changed signal
61 * when nothing has happened (mostly as a result of programmers calling
62 * select_row on an already selected row).
65 struct _GtkTreeSelectionPrivate
67 GtkTreeView *tree_view;
68 GtkSelectionMode type;
69 GtkTreeSelectionFunc user_func;
71 GDestroyNotify destroy;
74 static void gtk_tree_selection_finalize (GObject *object);
75 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
76 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
77 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
81 static void gtk_tree_selection_set_property (GObject *object,
85 static void gtk_tree_selection_get_property (GObject *object,
103 static GParamSpec *properties[N_PROPERTIES];
104 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
106 G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
109 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
111 GObjectClass *object_class;
113 object_class = (GObjectClass*) class;
115 object_class->finalize = gtk_tree_selection_finalize;
116 object_class->set_property = gtk_tree_selection_set_property;
117 object_class->get_property = gtk_tree_selection_get_property;
118 class->changed = NULL;
123 * GtkTreeSelection:mode:
126 * See gtk_tree_selection_set_mode() for more information on this property.
130 properties[PROP_MODE] = g_param_spec_enum ("mode",
132 P_("Selection mode"),
133 GTK_TYPE_SELECTION_MODE,
134 GTK_SELECTION_SINGLE,
136 G_PARAM_STATIC_STRINGS);
138 /* Install all properties */
139 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
144 * GtkTreeSelection::changed:
145 * @treeselection: the object which received the signal.
147 * Emitted whenever the selection has (possibly) changed. Please note that
148 * this signal is mostly a hint. It may only be emitted once when a range
149 * of rows are selected, and it may occasionally be emitted when nothing
152 tree_selection_signals[CHANGED] =
153 g_signal_new (I_("changed"),
154 G_OBJECT_CLASS_TYPE (object_class),
156 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
158 _gtk_marshal_VOID__VOID,
161 g_type_class_add_private (class, sizeof (GtkTreeSelectionPrivate));
165 gtk_tree_selection_init (GtkTreeSelection *selection)
167 GtkTreeSelectionPrivate *priv;
169 selection->priv = G_TYPE_INSTANCE_GET_PRIVATE (selection,
170 GTK_TYPE_TREE_SELECTION,
171 GtkTreeSelectionPrivate);
172 priv = selection->priv;
174 priv->type = GTK_SELECTION_SINGLE;
178 gtk_tree_selection_finalize (GObject *object)
180 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
181 GtkTreeSelectionPrivate *priv = selection->priv;
184 priv->destroy (priv->user_data);
186 /* chain parent_class' handler */
187 G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
191 gtk_tree_selection_set_property (GObject *object,
196 g_return_if_fail (GTK_IS_TREE_SELECTION (object));
201 gtk_tree_selection_set_mode (GTK_TREE_SELECTION (object), g_value_get_enum (value));
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 gtk_tree_selection_get_property (GObject *object,
215 g_return_if_fail (GTK_IS_TREE_SELECTION (object));
220 g_value_set_enum (value, gtk_tree_selection_get_mode (GTK_TREE_SELECTION (object)));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 * _gtk_tree_selection_new:
231 * Creates a new #GtkTreeSelection object. This function should not be invoked,
232 * as each #GtkTreeView will create its own #GtkTreeSelection.
234 * Return value: A newly created #GtkTreeSelection object.
237 _gtk_tree_selection_new (void)
239 GtkTreeSelection *selection;
241 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
247 * _gtk_tree_selection_new_with_tree_view:
248 * @tree_view: The #GtkTreeView.
250 * Creates a new #GtkTreeSelection object. This function should not be invoked,
251 * as each #GtkTreeView will create its own #GtkTreeSelection.
253 * Return value: A newly created #GtkTreeSelection object.
256 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
258 GtkTreeSelection *selection;
260 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
262 selection = _gtk_tree_selection_new ();
263 _gtk_tree_selection_set_tree_view (selection, tree_view);
269 * _gtk_tree_selection_set_tree_view:
270 * @selection: A #GtkTreeSelection.
271 * @tree_view: The #GtkTreeView.
273 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
274 * it is used internally by #GtkTreeView.
277 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
278 GtkTreeView *tree_view)
280 GtkTreeSelectionPrivate *priv;
282 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
283 if (tree_view != NULL)
284 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
286 priv = selection->priv;
288 priv->tree_view = tree_view;
292 * gtk_tree_selection_set_mode:
293 * @selection: A #GtkTreeSelection.
294 * @type: The selection mode
296 * Sets the selection mode of the @selection. If the previous type was
297 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
298 * previously selected.
301 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
302 GtkSelectionMode type)
304 GtkTreeSelectionPrivate *priv;
305 GtkTreeSelectionFunc tmp_func;
307 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
309 priv = selection->priv;
311 if (priv->type == type)
314 if (type == GTK_SELECTION_NONE)
316 /* We do this so that we unconditionally unset all rows
318 tmp_func = priv->user_func;
319 priv->user_func = NULL;
320 gtk_tree_selection_unselect_all (selection);
321 priv->user_func = tmp_func;
323 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
325 else if (type == GTK_SELECTION_SINGLE ||
326 type == GTK_SELECTION_BROWSE)
328 GtkRBTree *tree = NULL;
329 GtkRBNode *node = NULL;
330 gint selected = FALSE;
331 GtkTreePath *anchor_path = NULL;
333 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
337 _gtk_tree_view_find_node (priv->tree_view,
342 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
346 /* We do this so that we unconditionally unset all rows
348 tmp_func = priv->user_func;
349 priv->user_func = NULL;
350 gtk_tree_selection_unselect_all (selection);
351 priv->user_func = tmp_func;
353 if (node && selected)
354 _gtk_tree_selection_internal_select_node (selection,
361 gtk_tree_path_free (anchor_path);
366 g_object_notify_by_pspec (G_OBJECT (selection), properties[PROP_MODE]);
370 * gtk_tree_selection_get_mode:
371 * @selection: a #GtkTreeSelection
373 * Gets the selection mode for @selection. See
374 * gtk_tree_selection_set_mode().
376 * Return value: the current selection mode
379 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
381 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
383 return selection->priv->type;
387 * gtk_tree_selection_set_select_function:
388 * @selection: A #GtkTreeSelection.
389 * @func: The selection function. May be %NULL
390 * @data: The selection function's data. May be %NULL
391 * @destroy: The destroy function for user data. May be %NULL
393 * Sets the selection function.
395 * If set, this function is called before any node is selected or unselected,
396 * giving some control over which nodes are selected. The select function
397 * should return %TRUE if the state of the node may be toggled, and %FALSE
398 * if the state of the node should be left unchanged.
401 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
402 GtkTreeSelectionFunc func,
404 GDestroyNotify destroy)
406 GtkTreeSelectionPrivate *priv;
408 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
410 priv = selection->priv;
413 priv->destroy (priv->user_data);
415 priv->user_func = func;
416 priv->user_data = data;
417 priv->destroy = destroy;
421 * gtk_tree_selection_get_select_function: (skip)
422 * @selection: A #GtkTreeSelection.
424 * Returns the current selection function.
426 * Return value: The function.
431 gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
433 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
435 return selection->priv->user_func;
439 * gtk_tree_selection_get_user_data: (skip)
440 * @selection: A #GtkTreeSelection.
442 * Returns the user data for the selection function.
444 * Return value: The user data.
447 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
449 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
451 return selection->priv->user_data;
455 * gtk_tree_selection_get_tree_view:
456 * @selection: A #GtkTreeSelection
458 * Returns the tree view associated with @selection.
460 * Return value: (transfer none): A #GtkTreeView
463 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
465 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
467 return selection->priv->tree_view;
471 * gtk_tree_selection_get_selected:
472 * @selection: A #GtkTreeSelection.
473 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or NULL.
474 * @iter: (out) (allow-none): The #GtkTreeIter, or NULL.
476 * Sets @iter to the currently selected node if @selection is set to
477 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
478 * just want to test if @selection has any selected nodes. @model is filled
479 * with the current model as a convenience. This function will not work if you
480 * use @selection is #GTK_SELECTION_MULTIPLE.
482 * Return value: TRUE, if there is a selected node.
485 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
486 GtkTreeModel **model,
489 GtkTreeSelectionPrivate *priv;
492 GtkTreePath *anchor_path;
496 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
498 priv = selection->priv;
500 g_return_val_if_fail (priv->type != GTK_SELECTION_MULTIPLE, FALSE);
501 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
505 memset (iter, 0, sizeof (GtkTreeIter));
508 *model = gtk_tree_view_get_model (priv->tree_view);
510 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
512 if (anchor_path == NULL)
517 found_node = !_gtk_tree_view_find_node (priv->tree_view,
522 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
524 /* we only want to return the anchor if it exists in the rbtree and
530 retval = gtk_tree_model_get_iter (gtk_tree_view_get_model (priv->tree_view),
536 /* We don't want to return the anchor if it isn't actually selected.
541 gtk_tree_path_free (anchor_path);
547 * gtk_tree_selection_get_selected_rows:
548 * @selection: A #GtkTreeSelection.
549 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or %NULL.
551 * Creates a list of path of all selected rows. Additionally, if you are
552 * planning on modifying the model after calling this function, you may
553 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
554 * To do this, you can use gtk_tree_row_reference_new().
556 * To free the return value, use:
558 * g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
561 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
566 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
567 GtkTreeModel **model)
569 GtkTreeSelectionPrivate *priv;
571 GtkRBTree *tree = NULL;
572 GtkRBNode *node = NULL;
575 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
577 priv = selection->priv;
579 g_return_val_if_fail (priv->tree_view != NULL, NULL);
582 *model = gtk_tree_view_get_model (priv->tree_view);
584 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
586 if (tree == NULL || tree->root == NULL)
589 if (priv->type == GTK_SELECTION_NONE)
591 else if (priv->type != GTK_SELECTION_MULTIPLE)
595 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
599 path = gtk_tree_model_get_path (gtk_tree_view_get_model (priv->tree_view), &iter);
600 list = g_list_append (list, path);
608 node = _gtk_rbtree_first (tree);
609 path = gtk_tree_path_new_first ();
613 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
614 list = g_list_prepend (list, gtk_tree_path_copy (path));
618 tree = node->children;
619 node = _gtk_rbtree_first (tree);
621 gtk_tree_path_append_index (path, 0);
625 gboolean done = FALSE;
629 node = _gtk_rbtree_next (tree, node);
633 gtk_tree_path_next (path);
637 node = tree->parent_node;
638 tree = tree->parent_tree;
642 gtk_tree_path_free (path);
647 gtk_tree_path_up (path);
655 gtk_tree_path_free (path);
658 return g_list_reverse (list);
662 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
666 gint *count = (gint *)data;
668 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
672 _gtk_rbtree_traverse (node->children, node->children->root,
674 gtk_tree_selection_count_selected_rows_helper, data);
678 * gtk_tree_selection_count_selected_rows:
679 * @selection: A #GtkTreeSelection.
681 * Returns the number of rows that have been selected in @tree.
683 * Return value: The number of rows selected.
688 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
690 GtkTreeSelectionPrivate *priv;
694 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
696 priv = selection->priv;
698 g_return_val_if_fail (priv->tree_view != NULL, 0);
700 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
702 if (tree == NULL || tree->root == NULL)
705 if (priv->type == GTK_SELECTION_SINGLE ||
706 priv->type == GTK_SELECTION_BROWSE)
708 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
714 _gtk_rbtree_traverse (tree, tree->root,
716 gtk_tree_selection_count_selected_rows_helper,
722 /* gtk_tree_selection_selected_foreach helper */
724 model_changed (gpointer data)
726 gboolean *stop = (gboolean *)data;
732 * gtk_tree_selection_selected_foreach:
733 * @selection: A #GtkTreeSelection.
734 * @func: (scope call): The function to call for each selected node.
735 * @data: user data to pass to the function.
737 * Calls a function for each selected node. Note that you cannot modify
738 * the tree or selection from within this function. As a result,
739 * gtk_tree_selection_get_selected_rows() might be more useful.
742 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
743 GtkTreeSelectionForeachFunc func,
746 GtkTreeSelectionPrivate *priv;
753 gulong inserted_id, deleted_id, reordered_id, changed_id;
754 gboolean stop = FALSE;
756 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
758 priv = selection->priv;
760 g_return_if_fail (priv->tree_view != NULL);
762 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
764 if (func == NULL || tree == NULL || tree->root == NULL)
767 model = gtk_tree_view_get_model (priv->tree_view);
769 if (priv->type == GTK_SELECTION_SINGLE ||
770 priv->type == GTK_SELECTION_BROWSE)
772 path = _gtk_tree_view_get_anchor_path (priv->tree_view);
776 gtk_tree_model_get_iter (model, &iter, path);
777 (* func) (model, path, &iter, data);
778 gtk_tree_path_free (path);
783 node = _gtk_rbtree_first (tree);
785 g_object_ref (model);
787 /* connect to signals to monitor changes in treemodel */
788 inserted_id = g_signal_connect_swapped (model, "row-inserted",
789 G_CALLBACK (model_changed),
791 deleted_id = g_signal_connect_swapped (model, "row-deleted",
792 G_CALLBACK (model_changed),
794 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
795 G_CALLBACK (model_changed),
797 changed_id = g_signal_connect_swapped (priv->tree_view, "notify::model",
798 G_CALLBACK (model_changed),
801 /* find the node internally */
802 path = gtk_tree_path_new_first ();
806 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
808 gtk_tree_model_get_iter (model, &iter, path);
809 (* func) (model, path, &iter, data);
817 tree = node->children;
818 node = _gtk_rbtree_first (tree);
820 gtk_tree_path_append_index (path, 0);
824 gboolean done = FALSE;
828 node = _gtk_rbtree_next (tree, node);
832 gtk_tree_path_next (path);
836 node = tree->parent_node;
837 tree = tree->parent_tree;
841 /* we've run out of tree */
842 /* We're done with this function */
847 gtk_tree_path_up (path);
857 gtk_tree_path_free (path);
859 g_signal_handler_disconnect (model, inserted_id);
860 g_signal_handler_disconnect (model, deleted_id);
861 g_signal_handler_disconnect (model, reordered_id);
862 g_signal_handler_disconnect (priv->tree_view, changed_id);
863 g_object_unref (model);
865 /* check if we have to spew a scary message */
867 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
868 "This function is for observing the selections of the tree only. If\n"
869 "you are trying to get all selected items from the tree, try using\n"
870 "gtk_tree_selection_get_selected_rows instead.\n");
874 * gtk_tree_selection_select_path:
875 * @selection: A #GtkTreeSelection.
876 * @path: The #GtkTreePath to be selected.
878 * Select the row at @path.
881 gtk_tree_selection_select_path (GtkTreeSelection *selection,
884 GtkTreeSelectionPrivate *priv;
888 GtkTreeSelectMode mode = 0;
890 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
892 priv = selection->priv;
894 g_return_if_fail (priv->tree_view != NULL);
895 g_return_if_fail (path != NULL);
897 ret = _gtk_tree_view_find_node (priv->tree_view,
902 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
906 if (priv->type == GTK_SELECTION_MULTIPLE)
907 mode = GTK_TREE_SELECT_MODE_TOGGLE;
909 _gtk_tree_selection_internal_select_node (selection,
918 * gtk_tree_selection_unselect_path:
919 * @selection: A #GtkTreeSelection.
920 * @path: The #GtkTreePath to be unselected.
922 * Unselects the row at @path.
925 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
928 GtkTreeSelectionPrivate *priv;
933 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
935 priv = selection->priv;
937 g_return_if_fail (priv->tree_view != NULL);
938 g_return_if_fail (path != NULL);
940 ret = _gtk_tree_view_find_node (priv->tree_view,
945 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
949 _gtk_tree_selection_internal_select_node (selection,
953 GTK_TREE_SELECT_MODE_TOGGLE,
958 * gtk_tree_selection_select_iter:
959 * @selection: A #GtkTreeSelection.
960 * @iter: The #GtkTreeIter to be selected.
962 * Selects the specified iterator.
965 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
968 GtkTreeSelectionPrivate *priv;
972 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
974 priv = selection->priv;
976 g_return_if_fail (priv->tree_view != NULL);
978 model = gtk_tree_view_get_model (priv->tree_view);
979 g_return_if_fail (model != NULL);
980 g_return_if_fail (iter != NULL);
982 path = gtk_tree_model_get_path (model, iter);
987 gtk_tree_selection_select_path (selection, path);
988 gtk_tree_path_free (path);
993 * gtk_tree_selection_unselect_iter:
994 * @selection: A #GtkTreeSelection.
995 * @iter: The #GtkTreeIter to be unselected.
997 * Unselects the specified iterator.
1000 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
1003 GtkTreeSelectionPrivate *priv;
1005 GtkTreeModel *model;
1007 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1009 priv = selection->priv;
1011 g_return_if_fail (priv->tree_view != NULL);
1013 model = gtk_tree_view_get_model (priv->tree_view);
1014 g_return_if_fail (model != NULL);
1015 g_return_if_fail (iter != NULL);
1017 path = gtk_tree_model_get_path (model, iter);
1022 gtk_tree_selection_unselect_path (selection, path);
1023 gtk_tree_path_free (path);
1027 * gtk_tree_selection_path_is_selected:
1028 * @selection: A #GtkTreeSelection.
1029 * @path: A #GtkTreePath to check selection on.
1031 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
1032 * does not point to a valid location, %FALSE is returned
1034 * Return value: %TRUE if @path is selected.
1037 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
1040 GtkTreeSelectionPrivate *priv;
1045 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1047 priv = selection->priv;
1049 g_return_val_if_fail (path != NULL, FALSE);
1050 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1052 if (gtk_tree_view_get_model (priv->tree_view) == NULL)
1055 ret = _gtk_tree_view_find_node (priv->tree_view,
1060 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
1068 * gtk_tree_selection_iter_is_selected:
1069 * @selection: A #GtkTreeSelection
1070 * @iter: A valid #GtkTreeIter
1072 * Returns %TRUE if the row at @iter is currently selected.
1074 * Return value: %TRUE, if @iter is selected
1077 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
1080 GtkTreeSelectionPrivate *priv;
1082 GtkTreeModel *model;
1085 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1087 priv = selection->priv;
1089 g_return_val_if_fail (iter != NULL, FALSE);
1090 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1092 model = gtk_tree_view_get_model (priv->tree_view);
1093 g_return_val_if_fail (model != NULL, FALSE);
1095 path = gtk_tree_model_get_path (model, iter);
1099 retval = gtk_tree_selection_path_is_selected (selection, path);
1100 gtk_tree_path_free (path);
1106 /* Wish I was in python, right now... */
1108 GtkTreeSelection *selection;
1113 select_all_helper (GtkRBTree *tree,
1117 struct _TempTuple *tuple = data;
1120 _gtk_rbtree_traverse (node->children,
1121 node->children->root,
1125 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1127 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
1132 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
1133 * can use it in other places without fear of the signal being emitted.
1136 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
1138 GtkTreeSelectionPrivate *priv = selection->priv;
1139 struct _TempTuple *tuple;
1142 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1147 /* Mark all nodes selected */
1148 tuple = g_new (struct _TempTuple, 1);
1149 tuple->selection = selection;
1150 tuple->dirty = FALSE;
1152 _gtk_rbtree_traverse (tree, tree->root,
1166 * gtk_tree_selection_select_all:
1167 * @selection: A #GtkTreeSelection.
1169 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1173 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1175 GtkTreeSelectionPrivate *priv;
1177 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1179 priv = selection->priv;
1181 g_return_if_fail (priv->tree_view != NULL);
1183 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1184 gtk_tree_view_get_model (priv->tree_view) == NULL)
1187 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1189 if (gtk_tree_selection_real_select_all (selection))
1190 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1194 unselect_all_helper (GtkRBTree *tree,
1198 struct _TempTuple *tuple = data;
1201 _gtk_rbtree_traverse (node->children,
1202 node->children->root,
1204 unselect_all_helper,
1206 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1208 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1213 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1215 GtkTreeSelectionPrivate *priv = selection->priv;
1216 struct _TempTuple *tuple;
1218 if (priv->type == GTK_SELECTION_SINGLE ||
1219 priv->type == GTK_SELECTION_BROWSE)
1221 GtkRBTree *tree = NULL;
1222 GtkRBNode *node = NULL;
1223 GtkTreePath *anchor_path;
1225 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1227 if (anchor_path == NULL)
1230 _gtk_tree_view_find_node (priv->tree_view,
1235 gtk_tree_path_free (anchor_path);
1240 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1242 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1244 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1254 tuple = g_new (struct _TempTuple, 1);
1255 tuple->selection = selection;
1256 tuple->dirty = FALSE;
1258 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1259 _gtk_rbtree_traverse (tree, tree->root,
1261 unselect_all_helper,
1275 * gtk_tree_selection_unselect_all:
1276 * @selection: A #GtkTreeSelection.
1278 * Unselects all the nodes.
1281 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1283 GtkTreeSelectionPrivate *priv;
1285 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1287 priv = selection->priv;
1289 g_return_if_fail (priv->tree_view != NULL);
1291 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1292 gtk_tree_view_get_model (priv->tree_view) == NULL)
1295 if (gtk_tree_selection_real_unselect_all (selection))
1296 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1306 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1308 GtkTreePath *start_path,
1309 GtkTreePath *end_path)
1311 GtkTreeSelectionPrivate *priv = selection->priv;
1312 GtkRBNode *start_node, *end_node;
1313 GtkRBTree *start_tree, *end_tree;
1314 GtkTreePath *anchor_path = NULL;
1315 gboolean dirty = FALSE;
1317 switch (gtk_tree_path_compare (start_path, end_path))
1320 _gtk_tree_view_find_node (priv->tree_view,
1324 _gtk_tree_view_find_node (priv->tree_view,
1328 anchor_path = start_path;
1331 _gtk_tree_view_find_node (priv->tree_view,
1335 end_tree = start_tree;
1336 end_node = start_node;
1337 anchor_path = start_path;
1340 _gtk_tree_view_find_node (priv->tree_view,
1344 _gtk_tree_view_find_node (priv->tree_view,
1348 anchor_path = start_path;
1352 g_return_val_if_fail (start_node != NULL, FALSE);
1353 g_return_val_if_fail (end_node != NULL, FALSE);
1356 _gtk_tree_view_set_anchor_path (priv->tree_view, anchor_path);
1360 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1362 if (start_node == end_node)
1365 if (start_node->children)
1367 start_tree = start_node->children;
1368 start_node = _gtk_rbtree_first (start_tree);
1372 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1373 if (start_tree == NULL)
1375 /* we just ran out of tree. That means someone passed in bogus values.
1387 * gtk_tree_selection_select_range:
1388 * @selection: A #GtkTreeSelection.
1389 * @start_path: The initial node of the range.
1390 * @end_path: The final node of the range.
1392 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1393 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1396 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1397 GtkTreePath *start_path,
1398 GtkTreePath *end_path)
1400 GtkTreeSelectionPrivate *priv;
1402 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1404 priv = selection->priv;
1406 g_return_if_fail (priv->tree_view != NULL);
1407 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1408 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1410 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1411 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1415 * gtk_tree_selection_unselect_range:
1416 * @selection: A #GtkTreeSelection.
1417 * @start_path: The initial node of the range.
1418 * @end_path: The initial node of the range.
1420 * Unselects a range of nodes, determined by @start_path and @end_path
1426 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1427 GtkTreePath *start_path,
1428 GtkTreePath *end_path)
1430 GtkTreeSelectionPrivate *priv;
1432 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1434 priv = selection->priv;
1436 g_return_if_fail (priv->tree_view != NULL);
1437 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1439 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1440 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1444 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1448 GtkTreeSelectionPrivate *priv = selection->priv;
1450 GtkTreeModel *model;
1451 GtkTreeViewRowSeparatorFunc separator_func;
1452 gpointer separator_data;
1453 gboolean sensitive = FALSE;
1455 model = gtk_tree_view_get_model (priv->tree_view);
1457 _gtk_tree_view_get_row_separator_func (priv->tree_view,
1458 &separator_func, &separator_data);
1460 if (!gtk_tree_model_get_iter (model, &iter, path))
1463 if (!sensitive && separator_func)
1465 /* never allow separators to be selected */
1466 if ((* separator_func) (model, &iter, separator_data))
1470 if (priv->user_func)
1471 return (*priv->user_func) (selection, model, path,
1472 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1479 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1483 * docs about the 'override_browse_mode', we set this flag when we want to
1484 * unset select the node and override the select browse mode behaviour (that is
1485 * 'one node should *always* be selected').
1488 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1492 GtkTreeSelectMode mode,
1493 gboolean override_browse_mode)
1495 GtkTreeSelectionPrivate *priv = selection->priv;
1498 GtkTreePath *anchor_path = NULL;
1500 if (priv->type == GTK_SELECTION_NONE)
1503 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1505 if (priv->type == GTK_SELECTION_SINGLE ||
1506 priv->type == GTK_SELECTION_BROWSE)
1509 if (priv->type == GTK_SELECTION_BROWSE && override_browse_mode)
1511 dirty = gtk_tree_selection_real_unselect_all (selection);
1513 /* Did we try to select the same node again? */
1514 else if (priv->type == GTK_SELECTION_SINGLE &&
1515 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1517 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1519 dirty = gtk_tree_selection_real_unselect_all (selection);
1526 /* We only want to select the new node if we can unselect the old one,
1527 * and we can select the new one. */
1528 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1530 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1531 * unselect the new one
1534 dirty = gtk_tree_selection_real_unselect_all (selection);
1536 /* if dirty is TRUE at this point, we successfully unselected the
1537 * old one, and can then select the new one */
1541 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1543 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1544 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1549 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1553 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1558 else if (priv->type == GTK_SELECTION_MULTIPLE)
1560 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1561 && (anchor_path == NULL))
1563 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1565 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1567 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1569 gtk_tree_selection_select_range (selection,
1573 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1575 flags = node->flags;
1577 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1579 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1580 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1582 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1584 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1586 dirty = gtk_tree_selection_real_unselect_all (selection);
1587 dirty |= gtk_tree_selection_real_modify_range (selection,
1594 dirty = gtk_tree_selection_real_unselect_all (selection);
1596 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1598 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1603 gtk_tree_path_free (anchor_path);
1606 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1611 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1613 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1616 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1620 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1625 GtkTreeSelectionPrivate *priv = selection->priv;
1626 gboolean toggle = FALSE;
1627 GtkTreePath *path = NULL;
1631 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1633 path = _gtk_tree_path_new_from_rbtree (tree, node);
1634 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1635 gtk_tree_path_free (path);
1640 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1642 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_IS_SELECTED);
1643 _gtk_tree_view_accessible_add_state (priv->tree_view, tree, node, GTK_CELL_RENDERER_SELECTED);
1647 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_IS_SELECTED);
1648 _gtk_tree_view_accessible_remove_state (priv->tree_view, tree, node, GTK_CELL_RENDERER_SELECTED);
1651 _gtk_tree_view_queue_draw_node (priv->tree_view, tree, node, NULL);