2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtktreeselection.h"
23 #include "gtktreeprivate.h"
24 #include "gtkrbtree.h"
25 #include "gtkmarshalers.h"
30 * SECTION:gtktreeselection
31 * @Short_description: The selection object for GtkTreeView
32 * @Title: GtkTreeSelection
33 * @See_also: #GtkTreeView, #GtkTreeViewColumn, #GtkTreeDnd, #GtkTreeMode,
34 * #GtkTreeSortable, #GtkTreeModelSort, #GtkListStore, #GtkTreeStore,
35 * #GtkCellRenderer, #GtkCellEditable, #GtkCellRendererPixbuf,
36 * #GtkCellRendererText, #GtkCellRendererToggle
38 * The #GtkTreeSelection object is a helper object to manage the selection
39 * for a #GtkTreeView widget. The #GtkTreeSelection object is
40 * automatically created when a new #GtkTreeView widget is created, and
41 * cannot exist independentally of this widget. The primary reason the
42 * #GtkTreeSelection objects exists is for cleanliness of code and API.
43 * That is, there is no conceptual reason all these functions could not be
44 * methods on the #GtkTreeView widget instead of a separate function.
46 * The #GtkTreeSelection object is gotten from a #GtkTreeView by calling
47 * gtk_tree_view_get_selection(). It can be manipulated to check the
48 * selection status of the tree, as well as select and deselect individual
49 * rows. Selection is done completely view side. As a result, multiple
50 * views of the same model can have completely different selections.
51 * Additionally, you cannot change the selection of a row on the model that
52 * is not currently displayed by the view without expanding its parents
55 * One of the important things to remember when monitoring the selection of
56 * a view is that the #GtkTreeSelection::changed signal is mostly a hint.
57 * That is,it may only emit one signal when a range of rows is selected.
58 * Additionally, it may on occasion emit a #GtkTreeSelection::changed signal
59 * when nothing has happened (mostly as a result of programmers calling
60 * select_row on an already selected row).
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,
86 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
88 G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
91 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
93 GObjectClass *object_class;
95 object_class = (GObjectClass*) class;
97 object_class->finalize = gtk_tree_selection_finalize;
98 class->changed = NULL;
101 * GtkTreeSelection::changed:
102 * @treeselection: the object which received the signal.
104 * Emitted whenever the selection has (possibly) changed. Please note that
105 * this signal is mostly a hint. It may only be emitted once when a range
106 * of rows are selected, and it may occasionally be emitted when nothing
109 tree_selection_signals[CHANGED] =
110 g_signal_new (I_("changed"),
111 G_OBJECT_CLASS_TYPE (object_class),
113 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
115 _gtk_marshal_VOID__VOID,
118 g_type_class_add_private (class, sizeof (GtkTreeSelectionPrivate));
122 gtk_tree_selection_init (GtkTreeSelection *selection)
124 GtkTreeSelectionPrivate *priv;
126 selection->priv = G_TYPE_INSTANCE_GET_PRIVATE (selection,
127 GTK_TYPE_TREE_SELECTION,
128 GtkTreeSelectionPrivate);
129 priv = selection->priv;
131 priv->type = GTK_SELECTION_SINGLE;
135 gtk_tree_selection_finalize (GObject *object)
137 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
138 GtkTreeSelectionPrivate *priv = selection->priv;
141 priv->destroy (priv->user_data);
143 /* chain parent_class' handler */
144 G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
148 * _gtk_tree_selection_new:
150 * Creates a new #GtkTreeSelection object. This function should not be invoked,
151 * as each #GtkTreeView will create its own #GtkTreeSelection.
153 * Return value: A newly created #GtkTreeSelection object.
156 _gtk_tree_selection_new (void)
158 GtkTreeSelection *selection;
160 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
166 * _gtk_tree_selection_new_with_tree_view:
167 * @tree_view: The #GtkTreeView.
169 * Creates a new #GtkTreeSelection object. This function should not be invoked,
170 * as each #GtkTreeView will create its own #GtkTreeSelection.
172 * Return value: A newly created #GtkTreeSelection object.
175 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
177 GtkTreeSelection *selection;
179 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
181 selection = _gtk_tree_selection_new ();
182 _gtk_tree_selection_set_tree_view (selection, tree_view);
188 * _gtk_tree_selection_set_tree_view:
189 * @selection: A #GtkTreeSelection.
190 * @tree_view: The #GtkTreeView.
192 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
193 * it is used internally by #GtkTreeView.
196 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
197 GtkTreeView *tree_view)
199 GtkTreeSelectionPrivate *priv;
201 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
202 if (tree_view != NULL)
203 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
205 priv = selection->priv;
207 priv->tree_view = tree_view;
211 * gtk_tree_selection_set_mode:
212 * @selection: A #GtkTreeSelection.
213 * @type: The selection mode
215 * Sets the selection mode of the @selection. If the previous type was
216 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
217 * previously selected.
220 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
221 GtkSelectionMode type)
223 GtkTreeSelectionPrivate *priv;
224 GtkTreeSelectionFunc tmp_func;
226 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
228 priv = selection->priv;
230 if (priv->type == type)
233 if (type == GTK_SELECTION_NONE)
235 /* We do this so that we unconditionally unset all rows
237 tmp_func = priv->user_func;
238 priv->user_func = NULL;
239 gtk_tree_selection_unselect_all (selection);
240 priv->user_func = tmp_func;
242 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
244 else if (type == GTK_SELECTION_SINGLE ||
245 type == GTK_SELECTION_BROWSE)
247 GtkRBTree *tree = NULL;
248 GtkRBNode *node = NULL;
249 gint selected = FALSE;
250 GtkTreePath *anchor_path = NULL;
252 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
256 _gtk_tree_view_find_node (priv->tree_view,
261 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
265 /* We do this so that we unconditionally unset all rows
267 tmp_func = priv->user_func;
268 priv->user_func = NULL;
269 gtk_tree_selection_unselect_all (selection);
270 priv->user_func = tmp_func;
272 if (node && selected)
273 _gtk_tree_selection_internal_select_node (selection,
280 gtk_tree_path_free (anchor_path);
287 * gtk_tree_selection_get_mode:
288 * @selection: a #GtkTreeSelection
290 * Gets the selection mode for @selection. See
291 * gtk_tree_selection_set_mode().
293 * Return value: the current selection mode
296 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
298 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
300 return selection->priv->type;
304 * gtk_tree_selection_set_select_function:
305 * @selection: A #GtkTreeSelection.
306 * @func: The selection function. May be %NULL
307 * @data: The selection function's data. May be %NULL
308 * @destroy: The destroy function for user data. May be %NULL
310 * Sets the selection function.
312 * If set, this function is called before any node is selected or unselected,
313 * giving some control over which nodes are selected. The select function
314 * should return %TRUE if the state of the node may be toggled, and %FALSE
315 * if the state of the node should be left unchanged.
318 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
319 GtkTreeSelectionFunc func,
321 GDestroyNotify destroy)
323 GtkTreeSelectionPrivate *priv;
325 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
327 priv = selection->priv;
330 priv->destroy (priv->user_data);
332 priv->user_func = func;
333 priv->user_data = data;
334 priv->destroy = destroy;
338 * gtk_tree_selection_get_select_function: (skip)
339 * @selection: A #GtkTreeSelection.
341 * Returns the current selection function.
343 * Return value: The function.
348 gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
350 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
352 return selection->priv->user_func;
356 * gtk_tree_selection_get_user_data: (skip)
357 * @selection: A #GtkTreeSelection.
359 * Returns the user data for the selection function.
361 * Return value: The user data.
364 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
366 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
368 return selection->priv->user_data;
372 * gtk_tree_selection_get_tree_view:
373 * @selection: A #GtkTreeSelection
375 * Returns the tree view associated with @selection.
377 * Return value: (transfer none): A #GtkTreeView
380 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
382 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
384 return selection->priv->tree_view;
388 * gtk_tree_selection_get_selected:
389 * @selection: A #GtkTreeSelection.
390 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or NULL.
391 * @iter: (out) (allow-none): The #GtkTreeIter, or NULL.
393 * Sets @iter to the currently selected node if @selection is set to
394 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
395 * just want to test if @selection has any selected nodes. @model is filled
396 * with the current model as a convenience. This function will not work if you
397 * use @selection is #GTK_SELECTION_MULTIPLE.
399 * Return value: TRUE, if there is a selected node.
402 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
403 GtkTreeModel **model,
406 GtkTreeSelectionPrivate *priv;
409 GtkTreePath *anchor_path;
413 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
415 priv = selection->priv;
417 g_return_val_if_fail (priv->type != GTK_SELECTION_MULTIPLE, FALSE);
418 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
422 memset (iter, 0, sizeof (GtkTreeIter));
425 *model = gtk_tree_view_get_model (priv->tree_view);
427 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
429 if (anchor_path == NULL)
434 found_node = !_gtk_tree_view_find_node (priv->tree_view,
439 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
441 /* we only want to return the anchor if it exists in the rbtree and
447 retval = gtk_tree_model_get_iter (gtk_tree_view_get_model (priv->tree_view),
453 /* We don't want to return the anchor if it isn't actually selected.
458 gtk_tree_path_free (anchor_path);
464 * gtk_tree_selection_get_selected_rows:
465 * @selection: A #GtkTreeSelection.
466 * @model: (out) (allow-none) (transfer none): A pointer to set to the #GtkTreeModel, or %NULL.
468 * Creates a list of path of all selected rows. Additionally, if you are
469 * planning on modifying the model after calling this function, you may
470 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
471 * To do this, you can use gtk_tree_row_reference_new().
473 * To free the return value, use:
475 * g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
476 * g_list_free (list);
479 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
484 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
485 GtkTreeModel **model)
487 GtkTreeSelectionPrivate *priv;
489 GtkRBTree *tree = NULL;
490 GtkRBNode *node = NULL;
493 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
495 priv = selection->priv;
497 g_return_val_if_fail (priv->tree_view != NULL, NULL);
500 *model = gtk_tree_view_get_model (priv->tree_view);
502 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
504 if (tree == NULL || tree->root == NULL)
507 if (priv->type == GTK_SELECTION_NONE)
509 else if (priv->type != GTK_SELECTION_MULTIPLE)
513 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
517 path = gtk_tree_model_get_path (gtk_tree_view_get_model (priv->tree_view), &iter);
518 list = g_list_append (list, path);
528 while (node->left != tree->nil)
530 path = gtk_tree_path_new_first ();
534 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
535 list = g_list_prepend (list, gtk_tree_path_copy (path));
539 tree = node->children;
542 while (node->left != tree->nil)
545 gtk_tree_path_append_index (path, 0);
549 gboolean done = FALSE;
553 node = _gtk_rbtree_next (tree, node);
557 gtk_tree_path_next (path);
561 node = tree->parent_node;
562 tree = tree->parent_tree;
566 gtk_tree_path_free (path);
571 gtk_tree_path_up (path);
579 gtk_tree_path_free (path);
582 return g_list_reverse (list);
586 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
590 gint *count = (gint *)data;
592 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
596 _gtk_rbtree_traverse (node->children, node->children->root,
598 gtk_tree_selection_count_selected_rows_helper, data);
602 * gtk_tree_selection_count_selected_rows:
603 * @selection: A #GtkTreeSelection.
605 * Returns the number of rows that have been selected in @tree.
607 * Return value: The number of rows selected.
612 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
614 GtkTreeSelectionPrivate *priv;
618 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
620 priv = selection->priv;
622 g_return_val_if_fail (priv->tree_view != NULL, 0);
624 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
626 if (tree == NULL || tree->root == NULL)
629 if (priv->type == GTK_SELECTION_SINGLE ||
630 priv->type == GTK_SELECTION_BROWSE)
632 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
638 _gtk_rbtree_traverse (tree, tree->root,
640 gtk_tree_selection_count_selected_rows_helper,
646 /* gtk_tree_selection_selected_foreach helper */
648 model_changed (gpointer data)
650 gboolean *stop = (gboolean *)data;
656 * gtk_tree_selection_selected_foreach:
657 * @selection: A #GtkTreeSelection.
658 * @func: (scope call): The function to call for each selected node.
659 * @data: user data to pass to the function.
661 * Calls a function for each selected node. Note that you cannot modify
662 * the tree or selection from within this function. As a result,
663 * gtk_tree_selection_get_selected_rows() might be more useful.
666 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
667 GtkTreeSelectionForeachFunc func,
670 GtkTreeSelectionPrivate *priv;
677 gulong inserted_id, deleted_id, reordered_id, changed_id;
678 gboolean stop = FALSE;
680 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
682 priv = selection->priv;
684 g_return_if_fail (priv->tree_view != NULL);
686 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
688 if (func == NULL || tree == NULL || tree->root == NULL)
691 model = gtk_tree_view_get_model (priv->tree_view);
693 if (priv->type == GTK_SELECTION_SINGLE ||
694 priv->type == GTK_SELECTION_BROWSE)
696 path = _gtk_tree_view_get_anchor_path (priv->tree_view);
700 gtk_tree_model_get_iter (model, &iter, path);
701 (* func) (model, path, &iter, data);
702 gtk_tree_path_free (path);
709 while (node->left != tree->nil)
712 g_object_ref (model);
714 /* connect to signals to monitor changes in treemodel */
715 inserted_id = g_signal_connect_swapped (model, "row-inserted",
716 G_CALLBACK (model_changed),
718 deleted_id = g_signal_connect_swapped (model, "row-deleted",
719 G_CALLBACK (model_changed),
721 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
722 G_CALLBACK (model_changed),
724 changed_id = g_signal_connect_swapped (priv->tree_view, "notify::model",
725 G_CALLBACK (model_changed),
728 /* find the node internally */
729 path = gtk_tree_path_new_first ();
733 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
735 gtk_tree_model_get_iter (model, &iter, path);
736 (* func) (model, path, &iter, data);
744 tree = node->children;
747 while (node->left != tree->nil)
750 gtk_tree_path_append_index (path, 0);
754 gboolean done = FALSE;
758 node = _gtk_rbtree_next (tree, node);
762 gtk_tree_path_next (path);
766 node = tree->parent_node;
767 tree = tree->parent_tree;
771 /* we've run out of tree */
772 /* We're done with this function */
777 gtk_tree_path_up (path);
787 gtk_tree_path_free (path);
789 g_signal_handler_disconnect (model, inserted_id);
790 g_signal_handler_disconnect (model, deleted_id);
791 g_signal_handler_disconnect (model, reordered_id);
792 g_signal_handler_disconnect (priv->tree_view, changed_id);
793 g_object_unref (model);
795 /* check if we have to spew a scary message */
797 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
798 "This function is for observing the selections of the tree only. If\n"
799 "you are trying to get all selected items from the tree, try using\n"
800 "gtk_tree_selection_get_selected_rows instead.\n");
804 * gtk_tree_selection_select_path:
805 * @selection: A #GtkTreeSelection.
806 * @path: The #GtkTreePath to be selected.
808 * Select the row at @path.
811 gtk_tree_selection_select_path (GtkTreeSelection *selection,
814 GtkTreeSelectionPrivate *priv;
818 GtkTreeSelectMode mode = 0;
820 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
822 priv = selection->priv;
824 g_return_if_fail (priv->tree_view != NULL);
825 g_return_if_fail (path != NULL);
827 ret = _gtk_tree_view_find_node (priv->tree_view,
832 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
836 if (priv->type == GTK_SELECTION_MULTIPLE)
837 mode = GTK_TREE_SELECT_MODE_TOGGLE;
839 _gtk_tree_selection_internal_select_node (selection,
848 * gtk_tree_selection_unselect_path:
849 * @selection: A #GtkTreeSelection.
850 * @path: The #GtkTreePath to be unselected.
852 * Unselects the row at @path.
855 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
858 GtkTreeSelectionPrivate *priv;
863 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
865 priv = selection->priv;
867 g_return_if_fail (priv->tree_view != NULL);
868 g_return_if_fail (path != NULL);
870 ret = _gtk_tree_view_find_node (priv->tree_view,
875 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
879 _gtk_tree_selection_internal_select_node (selection,
883 GTK_TREE_SELECT_MODE_TOGGLE,
888 * gtk_tree_selection_select_iter:
889 * @selection: A #GtkTreeSelection.
890 * @iter: The #GtkTreeIter to be selected.
892 * Selects the specified iterator.
895 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
898 GtkTreeSelectionPrivate *priv;
902 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
904 priv = selection->priv;
906 g_return_if_fail (priv->tree_view != NULL);
908 model = gtk_tree_view_get_model (priv->tree_view);
909 g_return_if_fail (model != NULL);
910 g_return_if_fail (iter != NULL);
912 path = gtk_tree_model_get_path (model, iter);
917 gtk_tree_selection_select_path (selection, path);
918 gtk_tree_path_free (path);
923 * gtk_tree_selection_unselect_iter:
924 * @selection: A #GtkTreeSelection.
925 * @iter: The #GtkTreeIter to be unselected.
927 * Unselects the specified iterator.
930 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
933 GtkTreeSelectionPrivate *priv;
937 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
939 priv = selection->priv;
941 g_return_if_fail (priv->tree_view != NULL);
943 model = gtk_tree_view_get_model (priv->tree_view);
944 g_return_if_fail (model != NULL);
945 g_return_if_fail (iter != NULL);
947 path = gtk_tree_model_get_path (model, iter);
952 gtk_tree_selection_unselect_path (selection, path);
953 gtk_tree_path_free (path);
957 * gtk_tree_selection_path_is_selected:
958 * @selection: A #GtkTreeSelection.
959 * @path: A #GtkTreePath to check selection on.
961 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
962 * does not point to a valid location, %FALSE is returned
964 * Return value: %TRUE if @path is selected.
967 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
970 GtkTreeSelectionPrivate *priv;
975 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
977 priv = selection->priv;
979 g_return_val_if_fail (path != NULL, FALSE);
980 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
982 if (gtk_tree_view_get_model (priv->tree_view) == NULL)
985 ret = _gtk_tree_view_find_node (priv->tree_view,
990 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
998 * gtk_tree_selection_iter_is_selected:
999 * @selection: A #GtkTreeSelection
1000 * @iter: A valid #GtkTreeIter
1002 * Returns %TRUE if the row at @iter is currently selected.
1004 * Return value: %TRUE, if @iter is selected
1007 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
1010 GtkTreeSelectionPrivate *priv;
1012 GtkTreeModel *model;
1015 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
1017 priv = selection->priv;
1019 g_return_val_if_fail (iter != NULL, FALSE);
1020 g_return_val_if_fail (priv->tree_view != NULL, FALSE);
1022 model = gtk_tree_view_get_model (priv->tree_view);
1023 g_return_val_if_fail (model != NULL, FALSE);
1025 path = gtk_tree_model_get_path (model, iter);
1029 retval = gtk_tree_selection_path_is_selected (selection, path);
1030 gtk_tree_path_free (path);
1036 /* Wish I was in python, right now... */
1038 GtkTreeSelection *selection;
1043 select_all_helper (GtkRBTree *tree,
1047 struct _TempTuple *tuple = data;
1050 _gtk_rbtree_traverse (node->children,
1051 node->children->root,
1055 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1057 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
1062 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
1063 * can use it in other places without fear of the signal being emitted.
1066 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
1068 GtkTreeSelectionPrivate *priv = selection->priv;
1069 struct _TempTuple *tuple;
1072 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1077 /* Mark all nodes selected */
1078 tuple = g_new (struct _TempTuple, 1);
1079 tuple->selection = selection;
1080 tuple->dirty = FALSE;
1082 _gtk_rbtree_traverse (tree, tree->root,
1096 * gtk_tree_selection_select_all:
1097 * @selection: A #GtkTreeSelection.
1099 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1103 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1105 GtkTreeSelectionPrivate *priv;
1107 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1109 priv = selection->priv;
1111 g_return_if_fail (priv->tree_view != NULL);
1113 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1114 gtk_tree_view_get_model (priv->tree_view) == NULL)
1117 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1119 if (gtk_tree_selection_real_select_all (selection))
1120 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1124 unselect_all_helper (GtkRBTree *tree,
1128 struct _TempTuple *tuple = data;
1131 _gtk_rbtree_traverse (node->children,
1132 node->children->root,
1134 unselect_all_helper,
1136 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1138 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1143 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1145 GtkTreeSelectionPrivate *priv = selection->priv;
1146 struct _TempTuple *tuple;
1148 if (priv->type == GTK_SELECTION_SINGLE ||
1149 priv->type == GTK_SELECTION_BROWSE)
1151 GtkRBTree *tree = NULL;
1152 GtkRBNode *node = NULL;
1153 GtkTreePath *anchor_path;
1155 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1157 if (anchor_path == NULL)
1160 _gtk_tree_view_find_node (priv->tree_view,
1165 gtk_tree_path_free (anchor_path);
1170 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1172 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1174 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1184 tuple = g_new (struct _TempTuple, 1);
1185 tuple->selection = selection;
1186 tuple->dirty = FALSE;
1188 tree = _gtk_tree_view_get_rbtree (priv->tree_view);
1189 _gtk_rbtree_traverse (tree, tree->root,
1191 unselect_all_helper,
1205 * gtk_tree_selection_unselect_all:
1206 * @selection: A #GtkTreeSelection.
1208 * Unselects all the nodes.
1211 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1213 GtkTreeSelectionPrivate *priv;
1215 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1217 priv = selection->priv;
1219 g_return_if_fail (priv->tree_view != NULL);
1221 if (_gtk_tree_view_get_rbtree (priv->tree_view) == NULL ||
1222 gtk_tree_view_get_model (priv->tree_view) == NULL)
1225 if (gtk_tree_selection_real_unselect_all (selection))
1226 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1236 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1238 GtkTreePath *start_path,
1239 GtkTreePath *end_path)
1241 GtkTreeSelectionPrivate *priv = selection->priv;
1242 GtkRBNode *start_node, *end_node;
1243 GtkRBTree *start_tree, *end_tree;
1244 GtkTreePath *anchor_path = NULL;
1245 gboolean dirty = FALSE;
1247 switch (gtk_tree_path_compare (start_path, end_path))
1250 _gtk_tree_view_find_node (priv->tree_view,
1254 _gtk_tree_view_find_node (priv->tree_view,
1258 anchor_path = start_path;
1261 _gtk_tree_view_find_node (priv->tree_view,
1265 end_tree = start_tree;
1266 end_node = start_node;
1267 anchor_path = start_path;
1270 _gtk_tree_view_find_node (priv->tree_view,
1274 _gtk_tree_view_find_node (priv->tree_view,
1278 anchor_path = start_path;
1282 g_return_val_if_fail (start_node != NULL, FALSE);
1283 g_return_val_if_fail (end_node != NULL, FALSE);
1286 _gtk_tree_view_set_anchor_path (priv->tree_view, anchor_path);
1290 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1292 if (start_node == end_node)
1295 if (start_node->children)
1297 start_tree = start_node->children;
1298 start_node = start_tree->root;
1299 while (start_node->left != start_tree->nil)
1300 start_node = start_node->left;
1304 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1305 if (start_tree == NULL)
1307 /* we just ran out of tree. That means someone passed in bogus values.
1319 * gtk_tree_selection_select_range:
1320 * @selection: A #GtkTreeSelection.
1321 * @start_path: The initial node of the range.
1322 * @end_path: The final node of the range.
1324 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1325 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1328 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1329 GtkTreePath *start_path,
1330 GtkTreePath *end_path)
1332 GtkTreeSelectionPrivate *priv;
1334 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1336 priv = selection->priv;
1338 g_return_if_fail (priv->tree_view != NULL);
1339 g_return_if_fail (priv->type == GTK_SELECTION_MULTIPLE);
1340 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1342 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1343 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1347 * gtk_tree_selection_unselect_range:
1348 * @selection: A #GtkTreeSelection.
1349 * @start_path: The initial node of the range.
1350 * @end_path: The initial node of the range.
1352 * Unselects a range of nodes, determined by @start_path and @end_path
1358 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1359 GtkTreePath *start_path,
1360 GtkTreePath *end_path)
1362 GtkTreeSelectionPrivate *priv;
1364 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1366 priv = selection->priv;
1368 g_return_if_fail (priv->tree_view != NULL);
1369 g_return_if_fail (gtk_tree_view_get_model (priv->tree_view) != NULL);
1371 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1372 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1376 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1380 GtkTreeSelectionPrivate *priv = selection->priv;
1382 GtkTreeModel *model;
1383 GtkTreeViewRowSeparatorFunc separator_func;
1384 gpointer separator_data;
1385 gboolean sensitive = FALSE;
1387 model = gtk_tree_view_get_model (priv->tree_view);
1389 _gtk_tree_view_get_row_separator_func (priv->tree_view,
1390 &separator_func, &separator_data);
1392 if (!gtk_tree_model_get_iter (model, &iter, path))
1395 if (!sensitive && separator_func)
1397 /* never allow separators to be selected */
1398 if ((* separator_func) (model, &iter, separator_data))
1402 if (priv->user_func)
1403 return (*priv->user_func) (selection, model, path,
1404 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1411 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1415 * docs about the 'override_browse_mode', we set this flag when we want to
1416 * unset select the node and override the select browse mode behaviour (that is
1417 * 'one node should *always* be selected').
1420 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1424 GtkTreeSelectMode mode,
1425 gboolean override_browse_mode)
1427 GtkTreeSelectionPrivate *priv = selection->priv;
1430 GtkTreePath *anchor_path = NULL;
1432 if (priv->type == GTK_SELECTION_NONE)
1435 anchor_path = _gtk_tree_view_get_anchor_path (priv->tree_view);
1437 if (priv->type == GTK_SELECTION_SINGLE ||
1438 priv->type == GTK_SELECTION_BROWSE)
1441 if (priv->type == GTK_SELECTION_BROWSE && override_browse_mode)
1443 dirty = gtk_tree_selection_real_unselect_all (selection);
1445 /* Did we try to select the same node again? */
1446 else if (priv->type == GTK_SELECTION_SINGLE &&
1447 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1449 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1451 dirty = gtk_tree_selection_real_unselect_all (selection);
1458 /* We only want to select the new node if we can unselect the old one,
1459 * and we can select the new one. */
1460 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1462 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1463 * unselect the new one
1466 dirty = gtk_tree_selection_real_unselect_all (selection);
1468 /* if dirty is TRUE at this point, we successfully unselected the
1469 * old one, and can then select the new one */
1473 _gtk_tree_view_set_anchor_path (priv->tree_view, NULL);
1475 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1476 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1481 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1485 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1490 else if (priv->type == GTK_SELECTION_MULTIPLE)
1492 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1493 && (anchor_path == NULL))
1495 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1497 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1499 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1501 gtk_tree_selection_select_range (selection,
1505 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1507 flags = node->flags;
1509 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1511 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1512 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1514 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1516 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1518 dirty = gtk_tree_selection_real_unselect_all (selection);
1519 dirty |= gtk_tree_selection_real_modify_range (selection,
1526 dirty = gtk_tree_selection_real_unselect_all (selection);
1528 _gtk_tree_view_set_anchor_path (priv->tree_view, path);
1530 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1535 gtk_tree_path_free (anchor_path);
1538 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1543 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1545 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1548 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1552 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1557 GtkTreeSelectionPrivate *priv = selection->priv;
1558 gboolean toggle = FALSE;
1559 GtkTreePath *path = NULL;
1563 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1565 path = _gtk_tree_view_find_path (priv->tree_view, tree, node);
1566 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1567 gtk_tree_path_free (path);
1572 node->flags ^= GTK_RBNODE_IS_SELECTED;
1574 _gtk_tree_view_queue_draw_node (priv->tree_view, tree, node, NULL);