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, see <http://www.gnu.org/licenses/>.
20 #include "gtktreeselection.h"
21 #include "gtktreeprivate.h"
22 #include "gtkrbtree.h"
23 #include "gtkmarshalers.h"
25 #include "gtktypebuiltins.h"
26 #include "a11y/gtktreeviewaccessibleprivate.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).
63 struct _GtkTreeSelectionPrivate
65 GtkTreeView *tree_view;
66 GtkSelectionMode type;
67 GtkTreeSelectionFunc user_func;
69 GDestroyNotify destroy;
72 static void gtk_tree_selection_finalize (GObject *object);
73 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
74 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
75 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
79 static void gtk_tree_selection_set_property (GObject *object,
83 static void gtk_tree_selection_get_property (GObject *object,
101 static GParamSpec *properties[N_PROPERTIES];
102 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
104 G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
107 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
109 GObjectClass *object_class;
111 object_class = (GObjectClass*) class;
113 object_class->finalize = gtk_tree_selection_finalize;
114 object_class->set_property = gtk_tree_selection_set_property;
115 object_class->get_property = gtk_tree_selection_get_property;
116 class->changed = NULL;
121 * GtkTreeSelection:mode:
124 * See gtk_tree_selection_set_mode() for more information on this property.
128 properties[PROP_MODE] = g_param_spec_enum ("mode",
130 P_("Selection mode"),
131 GTK_TYPE_SELECTION_MODE,
132 GTK_SELECTION_SINGLE,
134 G_PARAM_STATIC_STRINGS);
136 /* Install all properties */
137 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
142 * GtkTreeSelection::changed:
143 * @treeselection: the object which received the signal.
145 * Emitted whenever the selection has (possibly) changed. Please note that
146 * this signal is mostly a hint. It may only be emitted once when a range
147 * of rows are selected, and it may occasionally be emitted when nothing
150 tree_selection_signals[CHANGED] =
151 g_signal_new (I_("changed"),
152 G_OBJECT_CLASS_TYPE (object_class),
154 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
156 _gtk_marshal_VOID__VOID,
159 g_type_class_add_private (class, sizeof (GtkTreeSelectionPrivate));
163 gtk_tree_selection_init (GtkTreeSelection *selection)
165 GtkTreeSelectionPrivate *priv;
167 selection->priv = G_TYPE_INSTANCE_GET_PRIVATE (selection,
168 GTK_TYPE_TREE_SELECTION,
169 GtkTreeSelectionPrivate);
170 priv = selection->priv;
172 priv->type = GTK_SELECTION_SINGLE;
176 gtk_tree_selection_finalize (GObject *object)
178 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
179 GtkTreeSelectionPrivate *priv = selection->priv;
182 priv->destroy (priv->user_data);
184 /* chain parent_class' handler */
185 G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
189 gtk_tree_selection_set_property (GObject *object,
194 g_return_if_fail (GTK_IS_TREE_SELECTION (object));
199 gtk_tree_selection_set_mode (GTK_TREE_SELECTION (object), g_value_get_enum (value));
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 gtk_tree_selection_get_property (GObject *object,
213 g_return_if_fail (GTK_IS_TREE_SELECTION (object));
218 g_value_set_enum (value, gtk_tree_selection_get_mode (GTK_TREE_SELECTION (object)));
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 * _gtk_tree_selection_new:
229 * Creates a new #GtkTreeSelection object. This function should not be invoked,
230 * as each #GtkTreeView will create its own #GtkTreeSelection.
232 * Return value: A newly created #GtkTreeSelection object.
235 _gtk_tree_selection_new (void)
237 GtkTreeSelection *selection;
239 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
245 * _gtk_tree_selection_new_with_tree_view:
246 * @tree_view: The #GtkTreeView.
248 * Creates a new #GtkTreeSelection object. This function should not be invoked,
249 * as each #GtkTreeView will create its own #GtkTreeSelection.
251 * Return value: A newly created #GtkTreeSelection object.
254 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
256 GtkTreeSelection *selection;
258 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
260 selection = _gtk_tree_selection_new ();
261 _gtk_tree_selection_set_tree_view (selection, tree_view);
267 * _gtk_tree_selection_set_tree_view:
268 * @selection: A #GtkTreeSelection.
269 * @tree_view: The #GtkTreeView.
271 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
272 * it is used internally by #GtkTreeView.
275 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
276 GtkTreeView *tree_view)
278 GtkTreeSelectionPrivate *priv;
280 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
281 if (tree_view != NULL)
282 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
284 priv = selection->priv;
286 priv->tree_view = tree_view;
290 * gtk_tree_selection_set_mode:
291 * @selection: A #GtkTreeSelection.
292 * @type: The selection mode
294 * Sets the selection mode of the @selection. If the previous type was
295 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
296 * previously selected.
299 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
300 GtkSelectionMode type)
302 GtkTreeSelectionPrivate *priv;
303 GtkTreeSelectionFunc tmp_func;
305 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
307 priv = selection->priv;
309 if (priv->type == type)
312 if (type == GTK_SELECTION_NONE)
314 /* We do this so that we unconditionally unset all rows
316 tmp_func = priv->user_func;
317 priv->user_func = NULL;
318 gtk_tree_selection_unselect_all (selection);
319 priv->user_func = tmp_func;
321 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
323 else if (type == GTK_SELECTION_SINGLE ||
324 type == GTK_SELECTION_BROWSE)
326 GtkRBTree *tree = NULL;
327 GtkRBNode *node = NULL;
328 gint selected = FALSE;
329 GtkTreePath *anchor_path = NULL;
331 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
335 _gtk_tree_view_find_node (priv->tree_view,
340 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
344 /* We do this so that we unconditionally unset all rows
346 tmp_func = priv->user_func;
347 priv->user_func = NULL;
348 gtk_tree_selection_unselect_all (selection);
349 priv->user_func = tmp_func;
351 if (node && selected)
352 _gtk_tree_selection_internal_select_node (selection,
359 gtk_tree_path_free (anchor_path);
364 g_object_notify_by_pspec (G_OBJECT (selection), properties[PROP_MODE]);
368 * gtk_tree_selection_get_mode:
369 * @selection: a #GtkTreeSelection
371 * Gets the selection mode for @selection. See
372 * gtk_tree_selection_set_mode().
374 * Return value: the current selection mode
377 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
379 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
381 return selection->priv->type;
385 * gtk_tree_selection_set_select_function:
386 * @selection: A #GtkTreeSelection.
387 * @func: The selection function. May be %NULL
388 * @data: The selection function's data. May be %NULL
389 * @destroy: The destroy function for user data. May be %NULL
391 * Sets the selection function.
393 * If set, this function is called before any node is selected or unselected,
394 * giving some control over which nodes are selected. The select function
395 * should return %TRUE if the state of the node may be toggled, and %FALSE
396 * if the state of the node should be left unchanged.
399 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
400 GtkTreeSelectionFunc func,
402 GDestroyNotify destroy)
404 GtkTreeSelectionPrivate *priv;
406 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
408 priv = selection->priv;
411 priv->destroy (priv->user_data);
413 priv->user_func = func;
414 priv->user_data = data;
415 priv->destroy = destroy;
419 * gtk_tree_selection_get_select_function: (skip)
420 * @selection: A #GtkTreeSelection.
422 * Returns the current selection function.
424 * Return value: The function.
429 gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
431 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
433 return selection->priv->user_func;
437 * gtk_tree_selection_get_user_data: (skip)
438 * @selection: A #GtkTreeSelection.
440 * Returns the user data for the selection function.
442 * Return value: The user data.
445 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
447 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
449 return selection->priv->user_data;
453 * gtk_tree_selection_get_tree_view:
454 * @selection: A #GtkTreeSelection
456 * Returns the tree view associated with @selection.
458 * Return value: (transfer none): A #GtkTreeView
461 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
463 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
465 return selection->priv->tree_view;
469 * gtk_tree_selection_get_selected:
470 * @selection: A #GtkTreeSelection.
471 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or NULL.
472 * @iter: (out) (allow-none): The #GtkTreeIter, or NULL.
474 * Sets @iter to the currently selected node if @selection is set to
475 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
476 * just want to test if @selection has any selected nodes. @model is filled
477 * with the current model as a convenience. This function will not work if you
478 * use @selection is #GTK_SELECTION_MULTIPLE.
480 * Return value: TRUE, if there is a selected node.
483 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
484 GtkTreeModel **model,
487 GtkTreeSelectionPrivate *priv;
490 GtkTreePath *anchor_path;
494 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
496 priv = selection->priv;
498 g_return_val_if_fail (priv->type != GTK_SELECTION_MULTIPLE, FALSE);
499 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
503 memset (iter, 0, sizeof (GtkTreeIter));
506 *model = gtk_tree_view_get_model (priv->tree_view);
508 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
510 if (anchor_path == NULL)
515 found_node = !_gtk_tree_view_find_node (priv->tree_view,
520 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
522 /* we only want to return the anchor if it exists in the rbtree and
528 retval = gtk_tree_model_get_iter (gtk_tree_view_get_model (priv->tree_view),
534 /* We don't want to return the anchor if it isn't actually selected.
539 gtk_tree_path_free (anchor_path);
545 * gtk_tree_selection_get_selected_rows:
546 * @selection: A #GtkTreeSelection.
547 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or %NULL.
549 * Creates a list of path of all selected rows. Additionally, if you are
550 * planning on modifying the model after calling this function, you may
551 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
552 * To do this, you can use gtk_tree_row_reference_new().
554 * To free the return value, use:
556 * g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
559 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
564 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
565 GtkTreeModel **model)
567 GtkTreeSelectionPrivate *priv;
569 GtkRBTree *tree = NULL;
570 GtkRBNode *node = NULL;
573 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
575 priv = selection->priv;
577 g_return_val_if_fail (priv->tree_view != NULL, NULL);
580 *model = gtk_tree_view_get_model (priv->tree_view);
582 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
584 if (tree == NULL || tree->root == NULL)
587 if (priv->type == GTK_SELECTION_NONE)
589 else if (priv->type != GTK_SELECTION_MULTIPLE)
593 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
597 path = gtk_tree_model_get_path (gtk_tree_view_get_model (priv->tree_view), &iter);
598 list = g_list_append (list, path);
606 node = _gtk_rbtree_first (tree);
607 path = gtk_tree_path_new_first ();
611 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
612 list = g_list_prepend (list, gtk_tree_path_copy (path));
616 tree = node->children;
617 node = _gtk_rbtree_first (tree);
619 gtk_tree_path_append_index (path, 0);
623 gboolean done = FALSE;
627 node = _gtk_rbtree_next (tree, node);
631 gtk_tree_path_next (path);
635 node = tree->parent_node;
636 tree = tree->parent_tree;
640 gtk_tree_path_free (path);
645 gtk_tree_path_up (path);
653 gtk_tree_path_free (path);
656 return g_list_reverse (list);
660 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
664 gint *count = (gint *)data;
666 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
670 _gtk_rbtree_traverse (node->children, node->children->root,
672 gtk_tree_selection_count_selected_rows_helper, data);
676 * gtk_tree_selection_count_selected_rows:
677 * @selection: A #GtkTreeSelection.
679 * Returns the number of rows that have been selected in @tree.
681 * Return value: The number of rows selected.
686 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
688 GtkTreeSelectionPrivate *priv;
692 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
694 priv = selection->priv;
696 g_return_val_if_fail (priv->tree_view != NULL, 0);
698 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
700 if (tree == NULL || tree->root == NULL)
703 if (priv->type == GTK_SELECTION_SINGLE ||
704 priv->type == GTK_SELECTION_BROWSE)
706 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
712 _gtk_rbtree_traverse (tree, tree->root,
714 gtk_tree_selection_count_selected_rows_helper,
720 /* gtk_tree_selection_selected_foreach helper */
722 model_changed (gpointer data)
724 gboolean *stop = (gboolean *)data;
730 * gtk_tree_selection_selected_foreach:
731 * @selection: A #GtkTreeSelection.
732 * @func: (scope call): The function to call for each selected node.
733 * @data: user data to pass to the function.
735 * Calls a function for each selected node. Note that you cannot modify
736 * the tree or selection from within this function. As a result,
737 * gtk_tree_selection_get_selected_rows() might be more useful.
740 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
741 GtkTreeSelectionForeachFunc func,
744 GtkTreeSelectionPrivate *priv;
751 gulong inserted_id, deleted_id, reordered_id, changed_id;
752 gboolean stop = FALSE;
754 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
756 priv = selection->priv;
758 g_return_if_fail (priv->tree_view != NULL);
760 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
762 if (func == NULL || tree == NULL || tree->root == NULL)
765 model = gtk_tree_view_get_model (priv->tree_view);
767 if (priv->type == GTK_SELECTION_SINGLE ||
768 priv->type == GTK_SELECTION_BROWSE)
770 path = _gtk_tree_view_get_anchor_path (priv->tree_view);
774 gtk_tree_model_get_iter (model, &iter, path);
775 (* func) (model, path, &iter, data);
776 gtk_tree_path_free (path);
781 node = _gtk_rbtree_first (tree);
783 g_object_ref (model);
785 /* connect to signals to monitor changes in treemodel */
786 inserted_id = g_signal_connect_swapped (model, "row-inserted",
787 G_CALLBACK (model_changed),
789 deleted_id = g_signal_connect_swapped (model, "row-deleted",
790 G_CALLBACK (model_changed),
792 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
793 G_CALLBACK (model_changed),
795 changed_id = g_signal_connect_swapped (priv->tree_view, "notify::model",
796 G_CALLBACK (model_changed),
799 /* find the node internally */
800 path = gtk_tree_path_new_first ();
804 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
806 gtk_tree_model_get_iter (model, &iter, path);
807 (* func) (model, path, &iter, data);
815 tree = node->children;
816 node = _gtk_rbtree_first (tree);
818 gtk_tree_path_append_index (path, 0);
822 gboolean done = FALSE;
826 node = _gtk_rbtree_next (tree, node);
830 gtk_tree_path_next (path);
834 node = tree->parent_node;
835 tree = tree->parent_tree;
839 /* we've run out of tree */
840 /* We're done with this function */
845 gtk_tree_path_up (path);
855 gtk_tree_path_free (path);
857 g_signal_handler_disconnect (model, inserted_id);
858 g_signal_handler_disconnect (model, deleted_id);
859 g_signal_handler_disconnect (model, reordered_id);
860 g_signal_handler_disconnect (priv->tree_view, changed_id);
861 g_object_unref (model);
863 /* check if we have to spew a scary message */
865 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
866 "This function is for observing the selections of the tree only. If\n"
867 "you are trying to get all selected items from the tree, try using\n"
868 "gtk_tree_selection_get_selected_rows instead.\n");
872 * gtk_tree_selection_select_path:
873 * @selection: A #GtkTreeSelection.
874 * @path: The #GtkTreePath to be selected.
876 * Select the row at @path.
879 gtk_tree_selection_select_path (GtkTreeSelection *selection,
882 GtkTreeSelectionPrivate *priv;
886 GtkTreeSelectMode mode = 0;
888 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
890 priv = selection->priv;
892 g_return_if_fail (priv->tree_view != NULL);
893 g_return_if_fail (path != NULL);
895 ret = _gtk_tree_view_find_node (priv->tree_view,
900 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
904 if (priv->type == GTK_SELECTION_MULTIPLE)
905 mode = GTK_TREE_SELECT_MODE_TOGGLE;
907 _gtk_tree_selection_internal_select_node (selection,
916 * gtk_tree_selection_unselect_path:
917 * @selection: A #GtkTreeSelection.
918 * @path: The #GtkTreePath to be unselected.
920 * Unselects the row at @path.
923 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
926 GtkTreeSelectionPrivate *priv;
931 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
933 priv = selection->priv;
935 g_return_if_fail (priv->tree_view != NULL);
936 g_return_if_fail (path != NULL);
938 ret = _gtk_tree_view_find_node (priv->tree_view,
943 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
947 _gtk_tree_selection_internal_select_node (selection,
951 GTK_TREE_SELECT_MODE_TOGGLE,
956 * gtk_tree_selection_select_iter:
957 * @selection: A #GtkTreeSelection.
958 * @iter: The #GtkTreeIter to be selected.
960 * Selects the specified iterator.
963 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
966 GtkTreeSelectionPrivate *priv;
970 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
972 priv = selection->priv;
974 g_return_if_fail (priv->tree_view != NULL);
976 model = gtk_tree_view_get_model (priv->tree_view);
977 g_return_if_fail (model != NULL);
978 g_return_if_fail (iter != NULL);
980 path = gtk_tree_model_get_path (model, iter);
985 gtk_tree_selection_select_path (selection, path);
986 gtk_tree_path_free (path);
991 * gtk_tree_selection_unselect_iter:
992 * @selection: A #GtkTreeSelection.
993 * @iter: The #GtkTreeIter to be unselected.
995 * Unselects the specified iterator.
998 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
1001 GtkTreeSelectionPrivate *priv;
1003 GtkTreeModel *model;
1005 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1007 priv = selection->priv;
1009 g_return_if_fail (priv->tree_view != NULL);
1011 model = gtk_tree_view_get_model (priv->tree_view);
1012 g_return_if_fail (model != NULL);
1013 g_return_if_fail (iter != NULL);
1015 path = gtk_tree_model_get_path (model, iter);
1020 gtk_tree_selection_unselect_path (selection, path);
1021 gtk_tree_path_free (path);
1025 * gtk_tree_selection_path_is_selected:
1026 * @selection: A #GtkTreeSelection.
1027 * @path: A #GtkTreePath to check selection on.
1029 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
1030 * does not point to a valid location, %FALSE is returned
1032 * Return value: %TRUE if @path is selected.
1035 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
1038 GtkTreeSelectionPrivate *priv;
1043 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1045 priv = selection->priv;
1047 g_return_val_if_fail (path != NULL, FALSE);
1048 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1050 if (gtk_tree_view_get_model (priv->tree_view) == NULL)
1053 ret = _gtk_tree_view_find_node (priv->tree_view,
1058 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
1066 * gtk_tree_selection_iter_is_selected:
1067 * @selection: A #GtkTreeSelection
1068 * @iter: A valid #GtkTreeIter
1070 * Returns %TRUE if the row at @iter is currently selected.
1072 * Return value: %TRUE, if @iter is selected
1075 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
1078 GtkTreeSelectionPrivate *priv;
1080 GtkTreeModel *model;
1083 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1085 priv = selection->priv;
1087 g_return_val_if_fail (iter != NULL, FALSE);
1088 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1090 model = gtk_tree_view_get_model (priv->tree_view);
1091 g_return_val_if_fail (model != NULL, FALSE);
1093 path = gtk_tree_model_get_path (model, iter);
1097 retval = gtk_tree_selection_path_is_selected (selection, path);
1098 gtk_tree_path_free (path);
1104 /* Wish I was in python, right now... */
1106 GtkTreeSelection *selection;
1111 select_all_helper (GtkRBTree *tree,
1115 struct _TempTuple *tuple = data;
1118 _gtk_rbtree_traverse (node->children,
1119 node->children->root,
1123 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1125 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
1130 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
1131 * can use it in other places without fear of the signal being emitted.
1134 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
1136 GtkTreeSelectionPrivate *priv = selection->priv;
1137 struct _TempTuple *tuple;
1140 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1145 /* Mark all nodes selected */
1146 tuple = g_new (struct _TempTuple, 1);
1147 tuple->selection = selection;
1148 tuple->dirty = FALSE;
1150 _gtk_rbtree_traverse (tree, tree->root,
1164 * gtk_tree_selection_select_all:
1165 * @selection: A #GtkTreeSelection.
1167 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1171 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1173 GtkTreeSelectionPrivate *priv;
1175 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1177 priv = selection->priv;
1179 g_return_if_fail (priv->tree_view != NULL);
1181 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1182 gtk_tree_view_get_model (priv->tree_view) == NULL)
1185 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1187 if (gtk_tree_selection_real_select_all (selection))
1188 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1192 unselect_all_helper (GtkRBTree *tree,
1196 struct _TempTuple *tuple = data;
1199 _gtk_rbtree_traverse (node->children,
1200 node->children->root,
1202 unselect_all_helper,
1204 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1206 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1211 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1213 GtkTreeSelectionPrivate *priv = selection->priv;
1214 struct _TempTuple *tuple;
1216 if (priv->type == GTK_SELECTION_SINGLE ||
1217 priv->type == GTK_SELECTION_BROWSE)
1219 GtkRBTree *tree = NULL;
1220 GtkRBNode *node = NULL;
1221 GtkTreePath *anchor_path;
1223 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1225 if (anchor_path == NULL)
1228 _gtk_tree_view_find_node (priv->tree_view,
1233 gtk_tree_path_free (anchor_path);
1238 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1240 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1242 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1252 tuple = g_new (struct _TempTuple, 1);
1253 tuple->selection = selection;
1254 tuple->dirty = FALSE;
1256 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1257 _gtk_rbtree_traverse (tree, tree->root,
1259 unselect_all_helper,
1273 * gtk_tree_selection_unselect_all:
1274 * @selection: A #GtkTreeSelection.
1276 * Unselects all the nodes.
1279 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1281 GtkTreeSelectionPrivate *priv;
1283 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1285 priv = selection->priv;
1287 g_return_if_fail (priv->tree_view != NULL);
1289 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1290 gtk_tree_view_get_model (priv->tree_view) == NULL)
1293 if (gtk_tree_selection_real_unselect_all (selection))
1294 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1304 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1306 GtkTreePath *start_path,
1307 GtkTreePath *end_path)
1309 GtkTreeSelectionPrivate *priv = selection->priv;
1310 GtkRBNode *start_node, *end_node;
1311 GtkRBTree *start_tree, *end_tree;
1312 GtkTreePath *anchor_path = NULL;
1313 gboolean dirty = FALSE;
1315 switch (gtk_tree_path_compare (start_path, end_path))
1318 _gtk_tree_view_find_node (priv->tree_view,
1322 _gtk_tree_view_find_node (priv->tree_view,
1326 anchor_path = start_path;
1329 _gtk_tree_view_find_node (priv->tree_view,
1333 end_tree = start_tree;
1334 end_node = start_node;
1335 anchor_path = start_path;
1338 _gtk_tree_view_find_node (priv->tree_view,
1342 _gtk_tree_view_find_node (priv->tree_view,
1346 anchor_path = start_path;
1350 g_return_val_if_fail (start_node != NULL, FALSE);
1351 g_return_val_if_fail (end_node != NULL, FALSE);
1354 _gtk_tree_view_set_anchor_path (priv->tree_view, anchor_path);
1358 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1360 if (start_node == end_node)
1363 if (start_node->children)
1365 start_tree = start_node->children;
1366 start_node = _gtk_rbtree_first (start_tree);
1370 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1371 if (start_tree == NULL)
1373 /* we just ran out of tree. That means someone passed in bogus values.
1385 * gtk_tree_selection_select_range:
1386 * @selection: A #GtkTreeSelection.
1387 * @start_path: The initial node of the range.
1388 * @end_path: The final node of the range.
1390 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1391 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1394 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1395 GtkTreePath *start_path,
1396 GtkTreePath *end_path)
1398 GtkTreeSelectionPrivate *priv;
1400 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1402 priv = selection->priv;
1404 g_return_if_fail (priv->tree_view != NULL);
1405 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1406 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1408 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1409 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1413 * gtk_tree_selection_unselect_range:
1414 * @selection: A #GtkTreeSelection.
1415 * @start_path: The initial node of the range.
1416 * @end_path: The initial node of the range.
1418 * Unselects a range of nodes, determined by @start_path and @end_path
1424 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1425 GtkTreePath *start_path,
1426 GtkTreePath *end_path)
1428 GtkTreeSelectionPrivate *priv;
1430 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1432 priv = selection->priv;
1434 g_return_if_fail (priv->tree_view != NULL);
1435 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1437 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1438 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1442 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1446 GtkTreeSelectionPrivate *priv = selection->priv;
1448 GtkTreeModel *model;
1449 GtkTreeViewRowSeparatorFunc separator_func;
1450 gpointer separator_data;
1451 gboolean sensitive = FALSE;
1453 model = gtk_tree_view_get_model (priv->tree_view);
1455 _gtk_tree_view_get_row_separator_func (priv->tree_view,
1456 &separator_func, &separator_data);
1458 if (!gtk_tree_model_get_iter (model, &iter, path))
1461 if (!sensitive && separator_func)
1463 /* never allow separators to be selected */
1464 if ((* separator_func) (model, &iter, separator_data))
1468 if (priv->user_func)
1469 return (*priv->user_func) (selection, model, path,
1470 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1477 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1481 * docs about the 'override_browse_mode', we set this flag when we want to
1482 * unset select the node and override the select browse mode behaviour (that is
1483 * 'one node should *always* be selected').
1486 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1490 GtkTreeSelectMode mode,
1491 gboolean override_browse_mode)
1493 GtkTreeSelectionPrivate *priv = selection->priv;
1496 GtkTreePath *anchor_path = NULL;
1498 if (priv->type == GTK_SELECTION_NONE)
1501 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1503 if (priv->type == GTK_SELECTION_SINGLE ||
1504 priv->type == GTK_SELECTION_BROWSE)
1507 if (priv->type == GTK_SELECTION_BROWSE && override_browse_mode)
1509 dirty = gtk_tree_selection_real_unselect_all (selection);
1511 /* Did we try to select the same node again? */
1512 else if (priv->type == GTK_SELECTION_SINGLE &&
1513 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1515 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1517 dirty = gtk_tree_selection_real_unselect_all (selection);
1524 /* We only want to select the new node if we can unselect the old one,
1525 * and we can select the new one. */
1526 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1528 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1529 * unselect the new one
1532 dirty = gtk_tree_selection_real_unselect_all (selection);
1534 /* if dirty is TRUE at this point, we successfully unselected the
1535 * old one, and can then select the new one */
1539 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1541 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1542 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1547 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1551 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1556 else if (priv->type == GTK_SELECTION_MULTIPLE)
1558 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1559 && (anchor_path == NULL))
1561 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1563 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1565 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1567 gtk_tree_selection_select_range (selection,
1571 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1573 flags = node->flags;
1575 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1577 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1578 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1580 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1582 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1584 dirty = gtk_tree_selection_real_unselect_all (selection);
1585 dirty |= gtk_tree_selection_real_modify_range (selection,
1592 dirty = gtk_tree_selection_real_unselect_all (selection);
1594 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1596 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1601 gtk_tree_path_free (anchor_path);
1604 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1609 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1611 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1614 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1618 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1623 GtkTreeSelectionPrivate *priv = selection->priv;
1624 gboolean toggle = FALSE;
1625 GtkTreePath *path = NULL;
1629 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1631 path = _gtk_tree_path_new_from_rbtree (tree, node);
1632 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1633 gtk_tree_path_free (path);
1638 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1640 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_IS_SELECTED);
1641 _gtk_tree_view_accessible_add_state (priv->tree_view, tree, node, GTK_CELL_RENDERER_SELECTED);
1645 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_IS_SELECTED);
1646 _gtk_tree_view_accessible_remove_state (priv->tree_view, tree, node, GTK_CELL_RENDERER_SELECTED);
1649 _gtk_tree_view_queue_draw_node (priv->tree_view, tree, node, NULL);