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"
31 * SECTION:gtktreeselection
32 * @Short_description: The selection object for GtkTreeView
33 * @Title: GtkTreeSelection
34 * @See_also: #GtkTreeView, #GtkTreeViewColumn, #GtkTreeDnd, #GtkTreeMode,
35 * #GtkTreeSortable, #GtkTreeModelSort, #GtkListStore, #GtkTreeStore,
36 * #GtkCellRenderer, #GtkCellEditable, #GtkCellRendererPixbuf,
37 * #GtkCellRendererText, #GtkCellRendererToggle
39 * The #GtkTreeSelection object is a helper object to manage the selection
40 * for a #GtkTreeView widget. The #GtkTreeSelection object is
41 * automatically created when a new #GtkTreeView widget is created, and
42 * cannot exist independentally of this widget. The primary reason the
43 * #GtkTreeSelection objects exists is for cleanliness of code and API.
44 * That is, there is no conceptual reason all these functions could not be
45 * methods on the #GtkTreeView widget instead of a separate function.
47 * The #GtkTreeSelection object is gotten from a #GtkTreeView by calling
48 * gtk_tree_view_get_selection(). It can be manipulated to check the
49 * selection status of the tree, as well as select and deselect individual
50 * rows. Selection is done completely view side. As a result, multiple
51 * views of the same model can have completely different selections.
52 * Additionally, you cannot change the selection of a row on the model that
53 * is not currently displayed by the view without expanding its parents
56 * One of the important things to remember when monitoring the selection of
57 * a view is that the #GtkTreeSelection::changed signal is mostly a hint.
58 * That is,it may only emit one signal when a range of rows is selected.
59 * Additionally, it may on occasion emit a #GtkTreeSelection::changed signal
60 * when nothing has happened (mostly as a result of programmers calling
61 * select_row on an already selected row).
64 struct _GtkTreeSelectionPrivate
66 GtkTreeView *tree_view;
67 GtkSelectionMode type;
68 GtkTreeSelectionFunc user_func;
70 GDestroyNotify destroy;
73 static void gtk_tree_selection_finalize (GObject *object);
74 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
75 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
76 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
80 static void gtk_tree_selection_set_property (GObject *object,
84 static void gtk_tree_selection_get_property (GObject *object,
102 static GParamSpec *properties[N_PROPERTIES];
103 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
105 G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
108 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
110 GObjectClass *object_class;
112 object_class = (GObjectClass*) class;
114 object_class->finalize = gtk_tree_selection_finalize;
115 object_class->set_property = gtk_tree_selection_set_property;
116 object_class->get_property = gtk_tree_selection_get_property;
117 class->changed = NULL;
122 * GtkTreeSelection:mode:
125 * See gtk_tree_selection_set_mode() for more information on this property.
129 properties[PROP_MODE] = g_param_spec_enum ("mode",
131 P_("Selection mode"),
132 GTK_TYPE_SELECTION_MODE,
133 GTK_SELECTION_SINGLE,
135 G_PARAM_STATIC_STRINGS);
137 /* Install all properties */
138 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
143 * GtkTreeSelection::changed:
144 * @treeselection: the object which received the signal.
146 * Emitted whenever the selection has (possibly) changed. Please note that
147 * this signal is mostly a hint. It may only be emitted once when a range
148 * of rows are selected, and it may occasionally be emitted when nothing
151 tree_selection_signals[CHANGED] =
152 g_signal_new (I_("changed"),
153 G_OBJECT_CLASS_TYPE (object_class),
155 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
157 _gtk_marshal_VOID__VOID,
160 g_type_class_add_private (class, sizeof (GtkTreeSelectionPrivate));
164 gtk_tree_selection_init (GtkTreeSelection *selection)
166 GtkTreeSelectionPrivate *priv;
168 selection->priv = G_TYPE_INSTANCE_GET_PRIVATE (selection,
169 GTK_TYPE_TREE_SELECTION,
170 GtkTreeSelectionPrivate);
171 priv = selection->priv;
173 priv->type = GTK_SELECTION_SINGLE;
177 gtk_tree_selection_finalize (GObject *object)
179 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
180 GtkTreeSelectionPrivate *priv = selection->priv;
183 priv->destroy (priv->user_data);
185 /* chain parent_class' handler */
186 G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
190 gtk_tree_selection_set_property (GObject *object,
195 g_return_if_fail (GTK_IS_TREE_SELECTION (object));
200 gtk_tree_selection_set_mode (GTK_TREE_SELECTION (object), g_value_get_enum (value));
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 gtk_tree_selection_get_property (GObject *object,
214 g_return_if_fail (GTK_IS_TREE_SELECTION (object));
219 g_value_set_enum (value, gtk_tree_selection_get_mode (GTK_TREE_SELECTION (object)));
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 * _gtk_tree_selection_new:
230 * Creates a new #GtkTreeSelection object. This function should not be invoked,
231 * as each #GtkTreeView will create its own #GtkTreeSelection.
233 * Return value: A newly created #GtkTreeSelection object.
236 _gtk_tree_selection_new (void)
238 GtkTreeSelection *selection;
240 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
246 * _gtk_tree_selection_new_with_tree_view:
247 * @tree_view: The #GtkTreeView.
249 * Creates a new #GtkTreeSelection object. This function should not be invoked,
250 * as each #GtkTreeView will create its own #GtkTreeSelection.
252 * Return value: A newly created #GtkTreeSelection object.
255 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
257 GtkTreeSelection *selection;
259 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
261 selection = _gtk_tree_selection_new ();
262 _gtk_tree_selection_set_tree_view (selection, tree_view);
268 * _gtk_tree_selection_set_tree_view:
269 * @selection: A #GtkTreeSelection.
270 * @tree_view: The #GtkTreeView.
272 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
273 * it is used internally by #GtkTreeView.
276 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
277 GtkTreeView *tree_view)
279 GtkTreeSelectionPrivate *priv;
281 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
282 if (tree_view != NULL)
283 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
285 priv = selection->priv;
287 priv->tree_view = tree_view;
291 * gtk_tree_selection_set_mode:
292 * @selection: A #GtkTreeSelection.
293 * @type: The selection mode
295 * Sets the selection mode of the @selection. If the previous type was
296 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
297 * previously selected.
300 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
301 GtkSelectionMode type)
303 GtkTreeSelectionPrivate *priv;
304 GtkTreeSelectionFunc tmp_func;
306 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
308 priv = selection->priv;
310 if (priv->type == type)
313 if (type == GTK_SELECTION_NONE)
315 /* We do this so that we unconditionally unset all rows
317 tmp_func = priv->user_func;
318 priv->user_func = NULL;
319 gtk_tree_selection_unselect_all (selection);
320 priv->user_func = tmp_func;
322 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
324 else if (type == GTK_SELECTION_SINGLE ||
325 type == GTK_SELECTION_BROWSE)
327 GtkRBTree *tree = NULL;
328 GtkRBNode *node = NULL;
329 gint selected = FALSE;
330 GtkTreePath *anchor_path = NULL;
332 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
336 _gtk_tree_view_find_node (priv->tree_view,
341 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
345 /* We do this so that we unconditionally unset all rows
347 tmp_func = priv->user_func;
348 priv->user_func = NULL;
349 gtk_tree_selection_unselect_all (selection);
350 priv->user_func = tmp_func;
352 if (node && selected)
353 _gtk_tree_selection_internal_select_node (selection,
360 gtk_tree_path_free (anchor_path);
365 g_object_notify_by_pspec (G_OBJECT (selection), properties[PROP_MODE]);
369 * gtk_tree_selection_get_mode:
370 * @selection: a #GtkTreeSelection
372 * Gets the selection mode for @selection. See
373 * gtk_tree_selection_set_mode().
375 * Return value: the current selection mode
378 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
380 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
382 return selection->priv->type;
386 * gtk_tree_selection_set_select_function:
387 * @selection: A #GtkTreeSelection.
388 * @func: The selection function. May be %NULL
389 * @data: The selection function's data. May be %NULL
390 * @destroy: The destroy function for user data. May be %NULL
392 * Sets the selection function.
394 * If set, this function is called before any node is selected or unselected,
395 * giving some control over which nodes are selected. The select function
396 * should return %TRUE if the state of the node may be toggled, and %FALSE
397 * if the state of the node should be left unchanged.
400 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
401 GtkTreeSelectionFunc func,
403 GDestroyNotify destroy)
405 GtkTreeSelectionPrivate *priv;
407 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
409 priv = selection->priv;
412 priv->destroy (priv->user_data);
414 priv->user_func = func;
415 priv->user_data = data;
416 priv->destroy = destroy;
420 * gtk_tree_selection_get_select_function: (skip)
421 * @selection: A #GtkTreeSelection.
423 * Returns the current selection function.
425 * Return value: The function.
430 gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
432 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
434 return selection->priv->user_func;
438 * gtk_tree_selection_get_user_data: (skip)
439 * @selection: A #GtkTreeSelection.
441 * Returns the user data for the selection function.
443 * Return value: The user data.
446 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
448 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
450 return selection->priv->user_data;
454 * gtk_tree_selection_get_tree_view:
455 * @selection: A #GtkTreeSelection
457 * Returns the tree view associated with @selection.
459 * Return value: (transfer none): A #GtkTreeView
462 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
464 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
466 return selection->priv->tree_view;
470 * gtk_tree_selection_get_selected:
471 * @selection: A #GtkTreeSelection.
472 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or NULL.
473 * @iter: (out) (allow-none): The #GtkTreeIter, or NULL.
475 * Sets @iter to the currently selected node if @selection is set to
476 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
477 * just want to test if @selection has any selected nodes. @model is filled
478 * with the current model as a convenience. This function will not work if you
479 * use @selection is #GTK_SELECTION_MULTIPLE.
481 * Return value: TRUE, if there is a selected node.
484 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
485 GtkTreeModel **model,
488 GtkTreeSelectionPrivate *priv;
491 GtkTreePath *anchor_path;
495 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
497 priv = selection->priv;
499 g_return_val_if_fail (priv->type != GTK_SELECTION_MULTIPLE, FALSE);
500 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
504 memset (iter, 0, sizeof (GtkTreeIter));
507 *model = gtk_tree_view_get_model (priv->tree_view);
509 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
511 if (anchor_path == NULL)
516 found_node = !_gtk_tree_view_find_node (priv->tree_view,
521 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
523 /* we only want to return the anchor if it exists in the rbtree and
529 retval = gtk_tree_model_get_iter (gtk_tree_view_get_model (priv->tree_view),
535 /* We don't want to return the anchor if it isn't actually selected.
540 gtk_tree_path_free (anchor_path);
546 * gtk_tree_selection_get_selected_rows:
547 * @selection: A #GtkTreeSelection.
548 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or %NULL.
550 * Creates a list of path of all selected rows. Additionally, if you are
551 * planning on modifying the model after calling this function, you may
552 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
553 * To do this, you can use gtk_tree_row_reference_new().
555 * To free the return value, use:
557 * g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
560 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
565 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
566 GtkTreeModel **model)
568 GtkTreeSelectionPrivate *priv;
570 GtkRBTree *tree = NULL;
571 GtkRBNode *node = NULL;
574 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
576 priv = selection->priv;
578 g_return_val_if_fail (priv->tree_view != NULL, NULL);
581 *model = gtk_tree_view_get_model (priv->tree_view);
583 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
585 if (tree == NULL || tree->root == NULL)
588 if (priv->type == GTK_SELECTION_NONE)
590 else if (priv->type != GTK_SELECTION_MULTIPLE)
594 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
598 path = gtk_tree_model_get_path (gtk_tree_view_get_model (priv->tree_view), &iter);
599 list = g_list_append (list, path);
607 node = _gtk_rbtree_first (tree);
608 path = gtk_tree_path_new_first ();
612 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
613 list = g_list_prepend (list, gtk_tree_path_copy (path));
617 tree = node->children;
618 node = _gtk_rbtree_first (tree);
620 gtk_tree_path_append_index (path, 0);
624 gboolean done = FALSE;
628 node = _gtk_rbtree_next (tree, node);
632 gtk_tree_path_next (path);
636 node = tree->parent_node;
637 tree = tree->parent_tree;
641 gtk_tree_path_free (path);
646 gtk_tree_path_up (path);
654 gtk_tree_path_free (path);
657 return g_list_reverse (list);
661 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
665 gint *count = (gint *)data;
667 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
671 _gtk_rbtree_traverse (node->children, node->children->root,
673 gtk_tree_selection_count_selected_rows_helper, data);
677 * gtk_tree_selection_count_selected_rows:
678 * @selection: A #GtkTreeSelection.
680 * Returns the number of rows that have been selected in @tree.
682 * Return value: The number of rows selected.
687 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
689 GtkTreeSelectionPrivate *priv;
693 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
695 priv = selection->priv;
697 g_return_val_if_fail (priv->tree_view != NULL, 0);
699 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
701 if (tree == NULL || tree->root == NULL)
704 if (priv->type == GTK_SELECTION_SINGLE ||
705 priv->type == GTK_SELECTION_BROWSE)
707 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
713 _gtk_rbtree_traverse (tree, tree->root,
715 gtk_tree_selection_count_selected_rows_helper,
721 /* gtk_tree_selection_selected_foreach helper */
723 model_changed (gpointer data)
725 gboolean *stop = (gboolean *)data;
731 * gtk_tree_selection_selected_foreach:
732 * @selection: A #GtkTreeSelection.
733 * @func: (scope call): The function to call for each selected node.
734 * @data: user data to pass to the function.
736 * Calls a function for each selected node. Note that you cannot modify
737 * the tree or selection from within this function. As a result,
738 * gtk_tree_selection_get_selected_rows() might be more useful.
741 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
742 GtkTreeSelectionForeachFunc func,
745 GtkTreeSelectionPrivate *priv;
752 gulong inserted_id, deleted_id, reordered_id, changed_id;
753 gboolean stop = FALSE;
755 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
757 priv = selection->priv;
759 g_return_if_fail (priv->tree_view != NULL);
761 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
763 if (func == NULL || tree == NULL || tree->root == NULL)
766 model = gtk_tree_view_get_model (priv->tree_view);
768 if (priv->type == GTK_SELECTION_SINGLE ||
769 priv->type == GTK_SELECTION_BROWSE)
771 path = _gtk_tree_view_get_anchor_path (priv->tree_view);
775 gtk_tree_model_get_iter (model, &iter, path);
776 (* func) (model, path, &iter, data);
777 gtk_tree_path_free (path);
782 node = _gtk_rbtree_first (tree);
784 g_object_ref (model);
786 /* connect to signals to monitor changes in treemodel */
787 inserted_id = g_signal_connect_swapped (model, "row-inserted",
788 G_CALLBACK (model_changed),
790 deleted_id = g_signal_connect_swapped (model, "row-deleted",
791 G_CALLBACK (model_changed),
793 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
794 G_CALLBACK (model_changed),
796 changed_id = g_signal_connect_swapped (priv->tree_view, "notify::model",
797 G_CALLBACK (model_changed),
800 /* find the node internally */
801 path = gtk_tree_path_new_first ();
805 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
807 gtk_tree_model_get_iter (model, &iter, path);
808 (* func) (model, path, &iter, data);
816 tree = node->children;
817 node = _gtk_rbtree_first (tree);
819 gtk_tree_path_append_index (path, 0);
823 gboolean done = FALSE;
827 node = _gtk_rbtree_next (tree, node);
831 gtk_tree_path_next (path);
835 node = tree->parent_node;
836 tree = tree->parent_tree;
840 /* we've run out of tree */
841 /* We're done with this function */
846 gtk_tree_path_up (path);
856 gtk_tree_path_free (path);
858 g_signal_handler_disconnect (model, inserted_id);
859 g_signal_handler_disconnect (model, deleted_id);
860 g_signal_handler_disconnect (model, reordered_id);
861 g_signal_handler_disconnect (priv->tree_view, changed_id);
862 g_object_unref (model);
864 /* check if we have to spew a scary message */
866 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
867 "This function is for observing the selections of the tree only. If\n"
868 "you are trying to get all selected items from the tree, try using\n"
869 "gtk_tree_selection_get_selected_rows instead.\n");
873 * gtk_tree_selection_select_path:
874 * @selection: A #GtkTreeSelection.
875 * @path: The #GtkTreePath to be selected.
877 * Select the row at @path.
880 gtk_tree_selection_select_path (GtkTreeSelection *selection,
883 GtkTreeSelectionPrivate *priv;
887 GtkTreeSelectMode mode = 0;
889 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
891 priv = selection->priv;
893 g_return_if_fail (priv->tree_view != NULL);
894 g_return_if_fail (path != NULL);
896 ret = _gtk_tree_view_find_node (priv->tree_view,
901 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
905 if (priv->type == GTK_SELECTION_MULTIPLE)
906 mode = GTK_TREE_SELECT_MODE_TOGGLE;
908 _gtk_tree_selection_internal_select_node (selection,
917 * gtk_tree_selection_unselect_path:
918 * @selection: A #GtkTreeSelection.
919 * @path: The #GtkTreePath to be unselected.
921 * Unselects the row at @path.
924 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
927 GtkTreeSelectionPrivate *priv;
932 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
934 priv = selection->priv;
936 g_return_if_fail (priv->tree_view != NULL);
937 g_return_if_fail (path != NULL);
939 ret = _gtk_tree_view_find_node (priv->tree_view,
944 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
948 _gtk_tree_selection_internal_select_node (selection,
952 GTK_TREE_SELECT_MODE_TOGGLE,
957 * gtk_tree_selection_select_iter:
958 * @selection: A #GtkTreeSelection.
959 * @iter: The #GtkTreeIter to be selected.
961 * Selects the specified iterator.
964 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
967 GtkTreeSelectionPrivate *priv;
971 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
973 priv = selection->priv;
975 g_return_if_fail (priv->tree_view != NULL);
977 model = gtk_tree_view_get_model (priv->tree_view);
978 g_return_if_fail (model != NULL);
979 g_return_if_fail (iter != NULL);
981 path = gtk_tree_model_get_path (model, iter);
986 gtk_tree_selection_select_path (selection, path);
987 gtk_tree_path_free (path);
992 * gtk_tree_selection_unselect_iter:
993 * @selection: A #GtkTreeSelection.
994 * @iter: The #GtkTreeIter to be unselected.
996 * Unselects the specified iterator.
999 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
1002 GtkTreeSelectionPrivate *priv;
1004 GtkTreeModel *model;
1006 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1008 priv = selection->priv;
1010 g_return_if_fail (priv->tree_view != NULL);
1012 model = gtk_tree_view_get_model (priv->tree_view);
1013 g_return_if_fail (model != NULL);
1014 g_return_if_fail (iter != NULL);
1016 path = gtk_tree_model_get_path (model, iter);
1021 gtk_tree_selection_unselect_path (selection, path);
1022 gtk_tree_path_free (path);
1026 * gtk_tree_selection_path_is_selected:
1027 * @selection: A #GtkTreeSelection.
1028 * @path: A #GtkTreePath to check selection on.
1030 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
1031 * does not point to a valid location, %FALSE is returned
1033 * Return value: %TRUE if @path is selected.
1036 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
1039 GtkTreeSelectionPrivate *priv;
1044 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1046 priv = selection->priv;
1048 g_return_val_if_fail (path != NULL, FALSE);
1049 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1051 if (gtk_tree_view_get_model (priv->tree_view) == NULL)
1054 ret = _gtk_tree_view_find_node (priv->tree_view,
1059 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
1067 * gtk_tree_selection_iter_is_selected:
1068 * @selection: A #GtkTreeSelection
1069 * @iter: A valid #GtkTreeIter
1071 * Returns %TRUE if the row at @iter is currently selected.
1073 * Return value: %TRUE, if @iter is selected
1076 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
1079 GtkTreeSelectionPrivate *priv;
1081 GtkTreeModel *model;
1084 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1086 priv = selection->priv;
1088 g_return_val_if_fail (iter != NULL, FALSE);
1089 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1091 model = gtk_tree_view_get_model (priv->tree_view);
1092 g_return_val_if_fail (model != NULL, FALSE);
1094 path = gtk_tree_model_get_path (model, iter);
1098 retval = gtk_tree_selection_path_is_selected (selection, path);
1099 gtk_tree_path_free (path);
1105 /* Wish I was in python, right now... */
1107 GtkTreeSelection *selection;
1112 select_all_helper (GtkRBTree *tree,
1116 struct _TempTuple *tuple = data;
1119 _gtk_rbtree_traverse (node->children,
1120 node->children->root,
1124 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1126 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
1131 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
1132 * can use it in other places without fear of the signal being emitted.
1135 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
1137 GtkTreeSelectionPrivate *priv = selection->priv;
1138 struct _TempTuple *tuple;
1141 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1146 /* Mark all nodes selected */
1147 tuple = g_new (struct _TempTuple, 1);
1148 tuple->selection = selection;
1149 tuple->dirty = FALSE;
1151 _gtk_rbtree_traverse (tree, tree->root,
1165 * gtk_tree_selection_select_all:
1166 * @selection: A #GtkTreeSelection.
1168 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1172 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1174 GtkTreeSelectionPrivate *priv;
1176 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1178 priv = selection->priv;
1180 g_return_if_fail (priv->tree_view != NULL);
1182 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1183 gtk_tree_view_get_model (priv->tree_view) == NULL)
1186 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1188 if (gtk_tree_selection_real_select_all (selection))
1189 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1193 unselect_all_helper (GtkRBTree *tree,
1197 struct _TempTuple *tuple = data;
1200 _gtk_rbtree_traverse (node->children,
1201 node->children->root,
1203 unselect_all_helper,
1205 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1207 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1212 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1214 GtkTreeSelectionPrivate *priv = selection->priv;
1215 struct _TempTuple *tuple;
1217 if (priv->type == GTK_SELECTION_SINGLE ||
1218 priv->type == GTK_SELECTION_BROWSE)
1220 GtkRBTree *tree = NULL;
1221 GtkRBNode *node = NULL;
1222 GtkTreePath *anchor_path;
1224 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1226 if (anchor_path == NULL)
1229 _gtk_tree_view_find_node (priv->tree_view,
1234 gtk_tree_path_free (anchor_path);
1239 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1241 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1243 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1253 tuple = g_new (struct _TempTuple, 1);
1254 tuple->selection = selection;
1255 tuple->dirty = FALSE;
1257 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1258 _gtk_rbtree_traverse (tree, tree->root,
1260 unselect_all_helper,
1274 * gtk_tree_selection_unselect_all:
1275 * @selection: A #GtkTreeSelection.
1277 * Unselects all the nodes.
1280 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1282 GtkTreeSelectionPrivate *priv;
1284 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1286 priv = selection->priv;
1288 g_return_if_fail (priv->tree_view != NULL);
1290 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1291 gtk_tree_view_get_model (priv->tree_view) == NULL)
1294 if (gtk_tree_selection_real_unselect_all (selection))
1295 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1305 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1307 GtkTreePath *start_path,
1308 GtkTreePath *end_path)
1310 GtkTreeSelectionPrivate *priv = selection->priv;
1311 GtkRBNode *start_node, *end_node;
1312 GtkRBTree *start_tree, *end_tree;
1313 GtkTreePath *anchor_path = NULL;
1314 gboolean dirty = FALSE;
1316 switch (gtk_tree_path_compare (start_path, end_path))
1319 _gtk_tree_view_find_node (priv->tree_view,
1323 _gtk_tree_view_find_node (priv->tree_view,
1327 anchor_path = start_path;
1330 _gtk_tree_view_find_node (priv->tree_view,
1334 end_tree = start_tree;
1335 end_node = start_node;
1336 anchor_path = start_path;
1339 _gtk_tree_view_find_node (priv->tree_view,
1343 _gtk_tree_view_find_node (priv->tree_view,
1347 anchor_path = start_path;
1351 g_return_val_if_fail (start_node != NULL, FALSE);
1352 g_return_val_if_fail (end_node != NULL, FALSE);
1355 _gtk_tree_view_set_anchor_path (priv->tree_view, anchor_path);
1359 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1361 if (start_node == end_node)
1364 if (start_node->children)
1366 start_tree = start_node->children;
1367 start_node = _gtk_rbtree_first (start_tree);
1371 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1372 if (start_tree == NULL)
1374 /* we just ran out of tree. That means someone passed in bogus values.
1386 * gtk_tree_selection_select_range:
1387 * @selection: A #GtkTreeSelection.
1388 * @start_path: The initial node of the range.
1389 * @end_path: The final node of the range.
1391 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1392 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1395 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1396 GtkTreePath *start_path,
1397 GtkTreePath *end_path)
1399 GtkTreeSelectionPrivate *priv;
1401 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1403 priv = selection->priv;
1405 g_return_if_fail (priv->tree_view != NULL);
1406 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1407 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1409 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1410 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1414 * gtk_tree_selection_unselect_range:
1415 * @selection: A #GtkTreeSelection.
1416 * @start_path: The initial node of the range.
1417 * @end_path: The initial node of the range.
1419 * Unselects a range of nodes, determined by @start_path and @end_path
1425 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1426 GtkTreePath *start_path,
1427 GtkTreePath *end_path)
1429 GtkTreeSelectionPrivate *priv;
1431 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1433 priv = selection->priv;
1435 g_return_if_fail (priv->tree_view != NULL);
1436 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1438 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1439 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1443 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1447 GtkTreeSelectionPrivate *priv = selection->priv;
1449 GtkTreeModel *model;
1450 GtkTreeViewRowSeparatorFunc separator_func;
1451 gpointer separator_data;
1452 gboolean sensitive = FALSE;
1454 model = gtk_tree_view_get_model (priv->tree_view);
1456 _gtk_tree_view_get_row_separator_func (priv->tree_view,
1457 &separator_func, &separator_data);
1459 if (!gtk_tree_model_get_iter (model, &iter, path))
1462 if (!sensitive && separator_func)
1464 /* never allow separators to be selected */
1465 if ((* separator_func) (model, &iter, separator_data))
1469 if (priv->user_func)
1470 return (*priv->user_func) (selection, model, path,
1471 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1478 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1482 * docs about the 'override_browse_mode', we set this flag when we want to
1483 * unset select the node and override the select browse mode behaviour (that is
1484 * 'one node should *always* be selected').
1487 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1491 GtkTreeSelectMode mode,
1492 gboolean override_browse_mode)
1494 GtkTreeSelectionPrivate *priv = selection->priv;
1497 GtkTreePath *anchor_path = NULL;
1499 if (priv->type == GTK_SELECTION_NONE)
1502 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1504 if (priv->type == GTK_SELECTION_SINGLE ||
1505 priv->type == GTK_SELECTION_BROWSE)
1508 if (priv->type == GTK_SELECTION_BROWSE && override_browse_mode)
1510 dirty = gtk_tree_selection_real_unselect_all (selection);
1512 /* Did we try to select the same node again? */
1513 else if (priv->type == GTK_SELECTION_SINGLE &&
1514 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1516 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1518 dirty = gtk_tree_selection_real_unselect_all (selection);
1525 /* We only want to select the new node if we can unselect the old one,
1526 * and we can select the new one. */
1527 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1529 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1530 * unselect the new one
1533 dirty = gtk_tree_selection_real_unselect_all (selection);
1535 /* if dirty is TRUE at this point, we successfully unselected the
1536 * old one, and can then select the new one */
1540 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1542 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1543 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1548 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1552 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1557 else if (priv->type == GTK_SELECTION_MULTIPLE)
1559 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1560 && (anchor_path == NULL))
1562 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1564 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1566 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1568 gtk_tree_selection_select_range (selection,
1572 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1574 flags = node->flags;
1576 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1578 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1579 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1581 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1583 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1585 dirty = gtk_tree_selection_real_unselect_all (selection);
1586 dirty |= gtk_tree_selection_real_modify_range (selection,
1593 dirty = gtk_tree_selection_real_unselect_all (selection);
1595 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1597 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1602 gtk_tree_path_free (anchor_path);
1605 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1610 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1612 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1615 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1619 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1624 GtkTreeSelectionPrivate *priv = selection->priv;
1625 gboolean toggle = FALSE;
1626 GtkTreePath *path = NULL;
1630 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1632 path = _gtk_tree_path_new_from_rbtree (tree, node);
1633 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1634 gtk_tree_path_free (path);
1639 node->flags ^= GTK_RBNODE_IS_SELECTED;
1641 _gtk_tree_view_queue_draw_node (priv->tree_view, tree, node, NULL);