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_foreach (list, (GFunc) gtk_tree_path_free, NULL);
558 * g_list_free (list);
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);
610 while (node->left != tree->nil)
612 path = gtk_tree_path_new_first ();
616 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
617 list = g_list_prepend (list, gtk_tree_path_copy (path));
621 tree = node->children;
624 while (node->left != tree->nil)
627 gtk_tree_path_append_index (path, 0);
631 gboolean done = FALSE;
635 node = _gtk_rbtree_next (tree, node);
639 gtk_tree_path_next (path);
643 node = tree->parent_node;
644 tree = tree->parent_tree;
648 gtk_tree_path_free (path);
653 gtk_tree_path_up (path);
661 gtk_tree_path_free (path);
664 return g_list_reverse (list);
668 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
672 gint *count = (gint *)data;
674 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
678 _gtk_rbtree_traverse (node->children, node->children->root,
680 gtk_tree_selection_count_selected_rows_helper, data);
684 * gtk_tree_selection_count_selected_rows:
685 * @selection: A #GtkTreeSelection.
687 * Returns the number of rows that have been selected in @tree.
689 * Return value: The number of rows selected.
694 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
696 GtkTreeSelectionPrivate *priv;
700 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
702 priv = selection->priv;
704 g_return_val_if_fail (priv->tree_view != NULL, 0);
706 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
708 if (tree == NULL || tree->root == NULL)
711 if (priv->type == GTK_SELECTION_SINGLE ||
712 priv->type == GTK_SELECTION_BROWSE)
714 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
720 _gtk_rbtree_traverse (tree, tree->root,
722 gtk_tree_selection_count_selected_rows_helper,
728 /* gtk_tree_selection_selected_foreach helper */
730 model_changed (gpointer data)
732 gboolean *stop = (gboolean *)data;
738 * gtk_tree_selection_selected_foreach:
739 * @selection: A #GtkTreeSelection.
740 * @func: (scope call): The function to call for each selected node.
741 * @data: user data to pass to the function.
743 * Calls a function for each selected node. Note that you cannot modify
744 * the tree or selection from within this function. As a result,
745 * gtk_tree_selection_get_selected_rows() might be more useful.
748 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
749 GtkTreeSelectionForeachFunc func,
752 GtkTreeSelectionPrivate *priv;
759 gulong inserted_id, deleted_id, reordered_id, changed_id;
760 gboolean stop = FALSE;
762 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
764 priv = selection->priv;
766 g_return_if_fail (priv->tree_view != NULL);
768 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
770 if (func == NULL || tree == NULL || tree->root == NULL)
773 model = gtk_tree_view_get_model (priv->tree_view);
775 if (priv->type == GTK_SELECTION_SINGLE ||
776 priv->type == GTK_SELECTION_BROWSE)
778 path = _gtk_tree_view_get_anchor_path (priv->tree_view);
782 gtk_tree_model_get_iter (model, &iter, path);
783 (* func) (model, path, &iter, data);
784 gtk_tree_path_free (path);
791 while (node->left != tree->nil)
794 g_object_ref (model);
796 /* connect to signals to monitor changes in treemodel */
797 inserted_id = g_signal_connect_swapped (model, "row-inserted",
798 G_CALLBACK (model_changed),
800 deleted_id = g_signal_connect_swapped (model, "row-deleted",
801 G_CALLBACK (model_changed),
803 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
804 G_CALLBACK (model_changed),
806 changed_id = g_signal_connect_swapped (priv->tree_view, "notify::model",
807 G_CALLBACK (model_changed),
810 /* find the node internally */
811 path = gtk_tree_path_new_first ();
815 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
817 gtk_tree_model_get_iter (model, &iter, path);
818 (* func) (model, path, &iter, data);
826 tree = node->children;
829 while (node->left != tree->nil)
832 gtk_tree_path_append_index (path, 0);
836 gboolean done = FALSE;
840 node = _gtk_rbtree_next (tree, node);
844 gtk_tree_path_next (path);
848 node = tree->parent_node;
849 tree = tree->parent_tree;
853 /* we've run out of tree */
854 /* We're done with this function */
859 gtk_tree_path_up (path);
869 gtk_tree_path_free (path);
871 g_signal_handler_disconnect (model, inserted_id);
872 g_signal_handler_disconnect (model, deleted_id);
873 g_signal_handler_disconnect (model, reordered_id);
874 g_signal_handler_disconnect (priv->tree_view, changed_id);
875 g_object_unref (model);
877 /* check if we have to spew a scary message */
879 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
880 "This function is for observing the selections of the tree only. If\n"
881 "you are trying to get all selected items from the tree, try using\n"
882 "gtk_tree_selection_get_selected_rows instead.\n");
886 * gtk_tree_selection_select_path:
887 * @selection: A #GtkTreeSelection.
888 * @path: The #GtkTreePath to be selected.
890 * Select the row at @path.
893 gtk_tree_selection_select_path (GtkTreeSelection *selection,
896 GtkTreeSelectionPrivate *priv;
900 GtkTreeSelectMode mode = 0;
902 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
904 priv = selection->priv;
906 g_return_if_fail (priv->tree_view != NULL);
907 g_return_if_fail (path != NULL);
909 ret = _gtk_tree_view_find_node (priv->tree_view,
914 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
918 if (priv->type == GTK_SELECTION_MULTIPLE)
919 mode = GTK_TREE_SELECT_MODE_TOGGLE;
921 _gtk_tree_selection_internal_select_node (selection,
930 * gtk_tree_selection_unselect_path:
931 * @selection: A #GtkTreeSelection.
932 * @path: The #GtkTreePath to be unselected.
934 * Unselects the row at @path.
937 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
940 GtkTreeSelectionPrivate *priv;
945 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
947 priv = selection->priv;
949 g_return_if_fail (priv->tree_view != NULL);
950 g_return_if_fail (path != NULL);
952 ret = _gtk_tree_view_find_node (priv->tree_view,
957 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
961 _gtk_tree_selection_internal_select_node (selection,
965 GTK_TREE_SELECT_MODE_TOGGLE,
970 * gtk_tree_selection_select_iter:
971 * @selection: A #GtkTreeSelection.
972 * @iter: The #GtkTreeIter to be selected.
974 * Selects the specified iterator.
977 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
980 GtkTreeSelectionPrivate *priv;
984 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
986 priv = selection->priv;
988 g_return_if_fail (priv->tree_view != NULL);
990 model = gtk_tree_view_get_model (priv->tree_view);
991 g_return_if_fail (model != NULL);
992 g_return_if_fail (iter != NULL);
994 path = gtk_tree_model_get_path (model, iter);
999 gtk_tree_selection_select_path (selection, path);
1000 gtk_tree_path_free (path);
1005 * gtk_tree_selection_unselect_iter:
1006 * @selection: A #GtkTreeSelection.
1007 * @iter: The #GtkTreeIter to be unselected.
1009 * Unselects the specified iterator.
1012 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
1015 GtkTreeSelectionPrivate *priv;
1017 GtkTreeModel *model;
1019 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1021 priv = selection->priv;
1023 g_return_if_fail (priv->tree_view != NULL);
1025 model = gtk_tree_view_get_model (priv->tree_view);
1026 g_return_if_fail (model != NULL);
1027 g_return_if_fail (iter != NULL);
1029 path = gtk_tree_model_get_path (model, iter);
1034 gtk_tree_selection_unselect_path (selection, path);
1035 gtk_tree_path_free (path);
1039 * gtk_tree_selection_path_is_selected:
1040 * @selection: A #GtkTreeSelection.
1041 * @path: A #GtkTreePath to check selection on.
1043 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
1044 * does not point to a valid location, %FALSE is returned
1046 * Return value: %TRUE if @path is selected.
1049 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
1052 GtkTreeSelectionPrivate *priv;
1057 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1059 priv = selection->priv;
1061 g_return_val_if_fail (path != NULL, FALSE);
1062 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1064 if (gtk_tree_view_get_model (priv->tree_view) == NULL)
1067 ret = _gtk_tree_view_find_node (priv->tree_view,
1072 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
1080 * gtk_tree_selection_iter_is_selected:
1081 * @selection: A #GtkTreeSelection
1082 * @iter: A valid #GtkTreeIter
1084 * Returns %TRUE if the row at @iter is currently selected.
1086 * Return value: %TRUE, if @iter is selected
1089 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
1092 GtkTreeSelectionPrivate *priv;
1094 GtkTreeModel *model;
1097 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1099 priv = selection->priv;
1101 g_return_val_if_fail (iter != NULL, FALSE);
1102 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1104 model = gtk_tree_view_get_model (priv->tree_view);
1105 g_return_val_if_fail (model != NULL, FALSE);
1107 path = gtk_tree_model_get_path (model, iter);
1111 retval = gtk_tree_selection_path_is_selected (selection, path);
1112 gtk_tree_path_free (path);
1118 /* Wish I was in python, right now... */
1120 GtkTreeSelection *selection;
1125 select_all_helper (GtkRBTree *tree,
1129 struct _TempTuple *tuple = data;
1132 _gtk_rbtree_traverse (node->children,
1133 node->children->root,
1137 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1139 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
1144 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
1145 * can use it in other places without fear of the signal being emitted.
1148 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
1150 GtkTreeSelectionPrivate *priv = selection->priv;
1151 struct _TempTuple *tuple;
1154 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1159 /* Mark all nodes selected */
1160 tuple = g_new (struct _TempTuple, 1);
1161 tuple->selection = selection;
1162 tuple->dirty = FALSE;
1164 _gtk_rbtree_traverse (tree, tree->root,
1178 * gtk_tree_selection_select_all:
1179 * @selection: A #GtkTreeSelection.
1181 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1185 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1187 GtkTreeSelectionPrivate *priv;
1189 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1191 priv = selection->priv;
1193 g_return_if_fail (priv->tree_view != NULL);
1195 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1196 gtk_tree_view_get_model (priv->tree_view) == NULL)
1199 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1201 if (gtk_tree_selection_real_select_all (selection))
1202 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1206 unselect_all_helper (GtkRBTree *tree,
1210 struct _TempTuple *tuple = data;
1213 _gtk_rbtree_traverse (node->children,
1214 node->children->root,
1216 unselect_all_helper,
1218 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1220 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1225 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1227 GtkTreeSelectionPrivate *priv = selection->priv;
1228 struct _TempTuple *tuple;
1230 if (priv->type == GTK_SELECTION_SINGLE ||
1231 priv->type == GTK_SELECTION_BROWSE)
1233 GtkRBTree *tree = NULL;
1234 GtkRBNode *node = NULL;
1235 GtkTreePath *anchor_path;
1237 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1239 if (anchor_path == NULL)
1242 _gtk_tree_view_find_node (priv->tree_view,
1247 gtk_tree_path_free (anchor_path);
1252 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1254 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1256 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1266 tuple = g_new (struct _TempTuple, 1);
1267 tuple->selection = selection;
1268 tuple->dirty = FALSE;
1270 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1271 _gtk_rbtree_traverse (tree, tree->root,
1273 unselect_all_helper,
1287 * gtk_tree_selection_unselect_all:
1288 * @selection: A #GtkTreeSelection.
1290 * Unselects all the nodes.
1293 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1295 GtkTreeSelectionPrivate *priv;
1297 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1299 priv = selection->priv;
1301 g_return_if_fail (priv->tree_view != NULL);
1303 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1304 gtk_tree_view_get_model (priv->tree_view) == NULL)
1307 if (gtk_tree_selection_real_unselect_all (selection))
1308 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1318 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1320 GtkTreePath *start_path,
1321 GtkTreePath *end_path)
1323 GtkTreeSelectionPrivate *priv = selection->priv;
1324 GtkRBNode *start_node, *end_node;
1325 GtkRBTree *start_tree, *end_tree;
1326 GtkTreePath *anchor_path = NULL;
1327 gboolean dirty = FALSE;
1329 switch (gtk_tree_path_compare (start_path, end_path))
1332 _gtk_tree_view_find_node (priv->tree_view,
1336 _gtk_tree_view_find_node (priv->tree_view,
1340 anchor_path = start_path;
1343 _gtk_tree_view_find_node (priv->tree_view,
1347 end_tree = start_tree;
1348 end_node = start_node;
1349 anchor_path = start_path;
1352 _gtk_tree_view_find_node (priv->tree_view,
1356 _gtk_tree_view_find_node (priv->tree_view,
1360 anchor_path = start_path;
1364 g_return_val_if_fail (start_node != NULL, FALSE);
1365 g_return_val_if_fail (end_node != NULL, FALSE);
1368 _gtk_tree_view_set_anchor_path (priv->tree_view, anchor_path);
1372 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1374 if (start_node == end_node)
1377 if (start_node->children)
1379 start_tree = start_node->children;
1380 start_node = start_tree->root;
1381 while (start_node->left != start_tree->nil)
1382 start_node = start_node->left;
1386 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1387 if (start_tree == NULL)
1389 /* we just ran out of tree. That means someone passed in bogus values.
1401 * gtk_tree_selection_select_range:
1402 * @selection: A #GtkTreeSelection.
1403 * @start_path: The initial node of the range.
1404 * @end_path: The final node of the range.
1406 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1407 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1410 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1411 GtkTreePath *start_path,
1412 GtkTreePath *end_path)
1414 GtkTreeSelectionPrivate *priv;
1416 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1418 priv = selection->priv;
1420 g_return_if_fail (priv->tree_view != NULL);
1421 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1422 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1424 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1425 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1429 * gtk_tree_selection_unselect_range:
1430 * @selection: A #GtkTreeSelection.
1431 * @start_path: The initial node of the range.
1432 * @end_path: The initial node of the range.
1434 * Unselects a range of nodes, determined by @start_path and @end_path
1440 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1441 GtkTreePath *start_path,
1442 GtkTreePath *end_path)
1444 GtkTreeSelectionPrivate *priv;
1446 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1448 priv = selection->priv;
1450 g_return_if_fail (priv->tree_view != NULL);
1451 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1453 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1454 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1458 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1462 GtkTreeSelectionPrivate *priv = selection->priv;
1464 GtkTreeModel *model;
1465 GtkTreeViewRowSeparatorFunc separator_func;
1466 gpointer separator_data;
1467 gboolean sensitive = FALSE;
1469 model = gtk_tree_view_get_model (priv->tree_view);
1471 _gtk_tree_view_get_row_separator_func (priv->tree_view,
1472 &separator_func, &separator_data);
1474 if (!gtk_tree_model_get_iter (model, &iter, path))
1477 if (!sensitive && separator_func)
1479 /* never allow separators to be selected */
1480 if ((* separator_func) (model, &iter, separator_data))
1484 if (priv->user_func)
1485 return (*priv->user_func) (selection, model, path,
1486 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1493 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1497 * docs about the 'override_browse_mode', we set this flag when we want to
1498 * unset select the node and override the select browse mode behaviour (that is
1499 * 'one node should *always* be selected').
1502 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1506 GtkTreeSelectMode mode,
1507 gboolean override_browse_mode)
1509 GtkTreeSelectionPrivate *priv = selection->priv;
1512 GtkTreePath *anchor_path = NULL;
1514 if (priv->type == GTK_SELECTION_NONE)
1517 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1519 if (priv->type == GTK_SELECTION_SINGLE ||
1520 priv->type == GTK_SELECTION_BROWSE)
1523 if (priv->type == GTK_SELECTION_BROWSE && override_browse_mode)
1525 dirty = gtk_tree_selection_real_unselect_all (selection);
1527 /* Did we try to select the same node again? */
1528 else if (priv->type == GTK_SELECTION_SINGLE &&
1529 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1531 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1533 dirty = gtk_tree_selection_real_unselect_all (selection);
1540 /* We only want to select the new node if we can unselect the old one,
1541 * and we can select the new one. */
1542 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1544 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1545 * unselect the new one
1548 dirty = gtk_tree_selection_real_unselect_all (selection);
1550 /* if dirty is TRUE at this point, we successfully unselected the
1551 * old one, and can then select the new one */
1555 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1557 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1558 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1563 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1567 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1572 else if (priv->type == GTK_SELECTION_MULTIPLE)
1574 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1575 && (anchor_path == NULL))
1577 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1579 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1581 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1583 gtk_tree_selection_select_range (selection,
1587 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1589 flags = node->flags;
1591 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1593 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1594 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1596 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1598 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1600 dirty = gtk_tree_selection_real_unselect_all (selection);
1601 dirty |= gtk_tree_selection_real_modify_range (selection,
1608 dirty = gtk_tree_selection_real_unselect_all (selection);
1610 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1612 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1617 gtk_tree_path_free (anchor_path);
1620 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1625 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1627 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1630 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1634 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1639 GtkTreeSelectionPrivate *priv = selection->priv;
1640 gboolean toggle = FALSE;
1641 GtkTreePath *path = NULL;
1645 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1647 path = _gtk_tree_view_find_path (priv->tree_view, tree, node);
1648 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1649 gtk_tree_path_free (path);
1654 node->flags ^= GTK_RBNODE_IS_SELECTED;
1656 _gtk_tree_view_queue_draw_node (priv->tree_view, tree, node, NULL);