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"
29 static void gtk_tree_selection_init (GtkTreeSelection *selection);
30 static void gtk_tree_selection_class_init (GtkTreeSelectionClass *class);
32 static void gtk_tree_selection_finalize (GObject *object);
33 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
34 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
35 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
46 static GObjectClass *parent_class = NULL;
47 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
50 gtk_tree_selection_get_type (void)
52 static GType selection_type = 0;
56 static const GTypeInfo selection_info =
58 sizeof (GtkTreeSelectionClass),
60 NULL, /* base_finalize */
61 (GClassInitFunc) gtk_tree_selection_class_init,
62 NULL, /* class_finalize */
63 NULL, /* class_data */
64 sizeof (GtkTreeSelection),
66 (GInstanceInitFunc) gtk_tree_selection_init
70 g_type_register_static (G_TYPE_OBJECT, I_("GtkTreeSelection"),
74 return selection_type;
78 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
80 GObjectClass *object_class;
82 object_class = (GObjectClass*) class;
83 parent_class = g_type_class_peek_parent (class);
85 object_class->finalize = gtk_tree_selection_finalize;
86 class->changed = NULL;
88 tree_selection_signals[CHANGED] =
89 g_signal_new (I_("changed"),
90 G_OBJECT_CLASS_TYPE (object_class),
92 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
94 _gtk_marshal_VOID__VOID,
99 gtk_tree_selection_init (GtkTreeSelection *selection)
101 selection->type = GTK_SELECTION_SINGLE;
105 gtk_tree_selection_finalize (GObject *object)
107 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
109 if (selection->destroy)
111 GtkDestroyNotify d = selection->destroy;
113 selection->destroy = NULL;
114 d (selection->user_data);
117 /* chain parent_class' handler */
118 G_OBJECT_CLASS (parent_class)->finalize (object);
122 * _gtk_tree_selection_new:
124 * Creates a new #GtkTreeSelection object. This function should not be invoked,
125 * as each #GtkTreeView will create its own #GtkTreeSelection.
127 * Return value: A newly created #GtkTreeSelection object.
130 _gtk_tree_selection_new (void)
132 GtkTreeSelection *selection;
134 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
140 * _gtk_tree_selection_new_with_tree_view:
141 * @tree_view: The #GtkTreeView.
143 * Creates a new #GtkTreeSelection object. This function should not be invoked,
144 * as each #GtkTreeView will create its own #GtkTreeSelection.
146 * Return value: A newly created #GtkTreeSelection object.
149 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
151 GtkTreeSelection *selection;
153 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
155 selection = _gtk_tree_selection_new ();
156 _gtk_tree_selection_set_tree_view (selection, tree_view);
162 * _gtk_tree_selection_set_tree_view:
163 * @selection: A #GtkTreeSelection.
164 * @tree_view: The #GtkTreeView.
166 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
167 * it is used internally by #GtkTreeView.
170 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
171 GtkTreeView *tree_view)
173 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
174 if (tree_view != NULL)
175 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
177 selection->tree_view = tree_view;
181 * gtk_tree_selection_set_mode:
182 * @selection: A #GtkTreeSelection.
183 * @type: The selection mode
185 * Sets the selection mode of the @selection. If the previous type was
186 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
187 * previously selected.
190 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
191 GtkSelectionMode type)
193 GtkTreeSelectionFunc tmp_func;
194 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
196 if (selection->type == type)
200 if (type == GTK_SELECTION_NONE)
202 /* We do this so that we unconditionally unset all rows
204 tmp_func = selection->user_func;
205 selection->user_func = NULL;
206 gtk_tree_selection_unselect_all (selection);
207 selection->user_func = tmp_func;
209 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
210 selection->tree_view->priv->anchor = NULL;
212 else if (type == GTK_SELECTION_SINGLE ||
213 type == GTK_SELECTION_BROWSE)
215 GtkRBTree *tree = NULL;
216 GtkRBNode *node = NULL;
217 gint selected = FALSE;
218 GtkTreePath *anchor_path = NULL;
220 if (selection->tree_view->priv->anchor)
222 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
226 _gtk_tree_view_find_node (selection->tree_view,
231 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
236 /* We do this so that we unconditionally unset all rows
238 tmp_func = selection->user_func;
239 selection->user_func = NULL;
240 gtk_tree_selection_unselect_all (selection);
241 selection->user_func = tmp_func;
243 if (node && selected)
244 _gtk_tree_selection_internal_select_node (selection,
251 gtk_tree_path_free (anchor_path);
254 selection->type = type;
258 * gtk_tree_selection_get_mode:
259 * @selection: a #GtkTreeSelection
261 * Gets the selection mode for @selection. See
262 * gtk_tree_selection_set_mode().
264 * Return value: the current selection mode
267 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
269 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
271 return selection->type;
275 * gtk_tree_selection_set_select_function:
276 * @selection: A #GtkTreeSelection.
277 * @func: The selection function.
278 * @data: The selection function's data.
279 * @destroy: The destroy function for user data. May be NULL.
281 * Sets the selection function. If set, this function is called before any node
282 * is selected or unselected, giving some control over which nodes are selected.
283 * The select function should return %TRUE if the state of the node may be toggled,
284 * and %FALSE if the state of the node should be left unchanged.
287 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
288 GtkTreeSelectionFunc func,
290 GtkDestroyNotify destroy)
292 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
293 g_return_if_fail (func != NULL);
295 if (selection->destroy)
297 GtkDestroyNotify d = selection->destroy;
299 selection->destroy = NULL;
300 d (selection->user_data);
303 selection->user_func = func;
304 selection->user_data = data;
305 selection->destroy = destroy;
309 * gtk_tree_selection_get_user_data:
310 * @selection: A #GtkTreeSelection.
312 * Returns the user data for the selection function.
314 * Return value: The user data.
317 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
319 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
321 return selection->user_data;
325 * gtk_tree_selection_get_tree_view:
326 * @selection: A #GtkTreeSelection
328 * Returns the tree view associated with @selection.
330 * Return value: A #GtkTreeView
333 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
335 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
337 return selection->tree_view;
341 * gtk_tree_selection_get_selected:
342 * @selection: A #GtkTreeSelection.
343 * @model: A pointer to set to the #GtkTreeModel, or NULL.
344 * @iter: The #GtkTreeIter, or NULL.
346 * Sets @iter to the currently selected node if @selection is set to
347 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
348 * just want to test if @selection has any selected nodes. @model is filled
349 * with the current model as a convenience. This function will not work if you
350 * use @selection is #GTK_SELECTION_MULTIPLE.
352 * Return value: TRUE, if there is a selected node.
355 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
356 GtkTreeModel **model,
361 GtkTreePath *anchor_path;
365 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
366 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
367 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
371 memset (iter, 0, sizeof (GtkTreeIter));
374 *model = selection->tree_view->priv->model;
376 if (selection->tree_view->priv->anchor == NULL)
379 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
381 if (anchor_path == NULL)
386 found_node = !_gtk_tree_view_find_node (selection->tree_view,
391 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
393 /* we only want to return the anchor if it exists in the rbtree and
399 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
405 /* We don't want to return the anchor if it isn't actually selected.
410 gtk_tree_path_free (anchor_path);
416 * gtk_tree_selection_get_selected_rows:
417 * @selection: A #GtkTreeSelection.
418 * @model: A pointer to set to the #GtkTreeModel, or NULL.
420 * Creates a list of path of all selected rows. Additionally, if you are
421 * planning on modifying the model after calling this function, you may
422 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
423 * To do this, you can use gtk_tree_row_reference_new().
425 * To free the return value, use:
426 * <informalexample><programlisting>
427 * g_list_foreach (list, gtk_tree_path_free, NULL);
428 * g_list_free (list);
429 * </programlisting></informalexample>
431 * Return value: A #GList containing a #GtkTreePath for each selected row.
436 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
437 GtkTreeModel **model)
440 GtkRBTree *tree = NULL;
441 GtkRBNode *node = NULL;
444 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
445 g_return_val_if_fail (selection->tree_view != NULL, NULL);
447 if (selection->tree_view->priv->tree == NULL ||
448 selection->tree_view->priv->tree->root == NULL)
452 *model = selection->tree_view->priv->model;
454 if (selection->type == GTK_SELECTION_NONE)
456 else if (selection->type != GTK_SELECTION_MULTIPLE)
460 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
464 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
465 list = g_list_append (list, path);
473 tree = selection->tree_view->priv->tree;
474 node = selection->tree_view->priv->tree->root;
476 while (node->left != tree->nil)
478 path = gtk_tree_path_new_first ();
482 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
483 list = g_list_prepend (list, gtk_tree_path_copy (path));
487 tree = node->children;
490 while (node->left != tree->nil)
493 gtk_tree_path_append_index (path, 0);
497 gboolean done = FALSE;
501 node = _gtk_rbtree_next (tree, node);
505 gtk_tree_path_next (path);
509 node = tree->parent_node;
510 tree = tree->parent_tree;
514 gtk_tree_path_free (path);
519 gtk_tree_path_up (path);
527 gtk_tree_path_free (path);
530 return g_list_reverse (list);
534 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
538 gint *count = (gint *)data;
540 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
544 _gtk_rbtree_traverse (node->children, node->children->root,
546 gtk_tree_selection_count_selected_rows_helper, data);
550 * gtk_tree_selection_count_selected_rows:
551 * @selection: A #GtkTreeSelection.
553 * Returns the number of rows that have been selected in @tree.
555 * Return value: The number of rows selected.
560 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
564 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
565 g_return_val_if_fail (selection->tree_view != NULL, 0);
567 if (selection->tree_view->priv->tree == NULL ||
568 selection->tree_view->priv->tree->root == NULL)
571 if (selection->type == GTK_SELECTION_SINGLE ||
572 selection->type == GTK_SELECTION_BROWSE)
574 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
580 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
581 selection->tree_view->priv->tree->root,
583 gtk_tree_selection_count_selected_rows_helper,
589 /* gtk_tree_selection_selected_foreach helper */
591 model_changed (gpointer data)
593 gboolean *stop = (gboolean *)data;
599 * gtk_tree_selection_selected_foreach:
600 * @selection: A #GtkTreeSelection.
601 * @func: The function to call for each selected node.
602 * @data: user data to pass to the function.
604 * Calls a function for each selected node. Note that you cannot modify
605 * the tree or selection from within this function. As a result,
606 * gtk_tree_selection_get_selected_rows() might be more useful.
609 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
610 GtkTreeSelectionForeachFunc func,
618 guint inserted_id, deleted_id, reordered_id;
619 gboolean stop = FALSE;
621 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
622 g_return_if_fail (selection->tree_view != NULL);
625 selection->tree_view->priv->tree == NULL ||
626 selection->tree_view->priv->tree->root == NULL)
629 if (selection->type == GTK_SELECTION_SINGLE ||
630 selection->type == GTK_SELECTION_BROWSE)
632 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
634 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
635 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
636 (* func) (selection->tree_view->priv->model, path, &iter, data);
637 gtk_tree_path_free (path);
642 tree = selection->tree_view->priv->tree;
643 node = selection->tree_view->priv->tree->root;
645 while (node->left != tree->nil)
648 /* connect to signals to monitor changes in treemodel */
649 inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
651 G_CALLBACK (model_changed),
653 deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
655 G_CALLBACK (model_changed),
657 reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
659 G_CALLBACK (model_changed),
662 /* find the node internally */
663 path = gtk_tree_path_new_first ();
667 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
669 gtk_tree_model_get_iter (selection->tree_view->priv->model,
671 (* func) (selection->tree_view->priv->model, path, &iter, data);
679 tree = node->children;
682 while (node->left != tree->nil)
685 gtk_tree_path_append_index (path, 0);
689 gboolean done = FALSE;
693 node = _gtk_rbtree_next (tree, node);
697 gtk_tree_path_next (path);
701 node = tree->parent_node;
702 tree = tree->parent_tree;
706 /* we've run out of tree */
707 /* We're done with this function */
712 gtk_tree_path_up (path);
722 gtk_tree_path_free (path);
724 g_signal_handler_disconnect (selection->tree_view->priv->model,
726 g_signal_handler_disconnect (selection->tree_view->priv->model,
728 g_signal_handler_disconnect (selection->tree_view->priv->model,
731 /* check if we have to spew a scary message */
734 ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
735 "This function is for observing the selections of the tree only. If\n"
736 "you are trying to get all selected items from the tree, try using\n"
737 "gtk_tree_selection_get_selected_rows instead.\n");
741 * gtk_tree_selection_select_path:
742 * @selection: A #GtkTreeSelection.
743 * @path: The #GtkTreePath to be selected.
745 * Select the row at @path.
748 gtk_tree_selection_select_path (GtkTreeSelection *selection,
754 GtkTreeSelectMode mode = 0;
756 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
757 g_return_if_fail (selection->tree_view != NULL);
758 g_return_if_fail (path != NULL);
760 ret = _gtk_tree_view_find_node (selection->tree_view,
765 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
769 if (selection->type == GTK_SELECTION_MULTIPLE)
770 mode = GTK_TREE_SELECT_MODE_TOGGLE;
772 _gtk_tree_selection_internal_select_node (selection,
781 * gtk_tree_selection_unselect_path:
782 * @selection: A #GtkTreeSelection.
783 * @path: The #GtkTreePath to be unselected.
785 * Unselects the row at @path.
788 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
795 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
796 g_return_if_fail (selection->tree_view != NULL);
797 g_return_if_fail (path != NULL);
799 ret = _gtk_tree_view_find_node (selection->tree_view,
804 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
808 _gtk_tree_selection_internal_select_node (selection,
812 GTK_TREE_SELECT_MODE_TOGGLE,
817 * gtk_tree_selection_select_iter:
818 * @selection: A #GtkTreeSelection.
819 * @iter: The #GtkTreeIter to be selected.
821 * Selects the specified iterator.
824 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
829 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
830 g_return_if_fail (selection->tree_view != NULL);
831 g_return_if_fail (selection->tree_view->priv->model != NULL);
832 g_return_if_fail (iter != NULL);
834 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
840 gtk_tree_selection_select_path (selection, path);
841 gtk_tree_path_free (path);
846 * gtk_tree_selection_unselect_iter:
847 * @selection: A #GtkTreeSelection.
848 * @iter: The #GtkTreeIter to be unselected.
850 * Unselects the specified iterator.
853 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
858 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
859 g_return_if_fail (selection->tree_view != NULL);
860 g_return_if_fail (selection->tree_view->priv->model != NULL);
861 g_return_if_fail (iter != NULL);
863 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
869 gtk_tree_selection_unselect_path (selection, path);
870 gtk_tree_path_free (path);
874 * gtk_tree_selection_path_is_selected:
875 * @selection: A #GtkTreeSelection.
876 * @path: A #GtkTreePath to check selection on.
878 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
879 * does not point to a valid location, %FALSE is returned
881 * Return value: %TRUE if @path is selected.
884 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
891 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
892 g_return_val_if_fail (path != NULL, FALSE);
893 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
895 if (selection->tree_view->priv->model == NULL)
898 ret = _gtk_tree_view_find_node (selection->tree_view,
903 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
911 * gtk_tree_selection_iter_is_selected:
912 * @selection: A #GtkTreeSelection
913 * @iter: A valid #GtkTreeIter
915 * Returns %TRUE if the row at @iter is currently selected.
917 * Return value: %TRUE, if @iter is selected
920 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
926 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
927 g_return_val_if_fail (iter != NULL, FALSE);
928 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
929 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
931 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
935 retval = gtk_tree_selection_path_is_selected (selection, path);
936 gtk_tree_path_free (path);
942 /* Wish I was in python, right now... */
944 GtkTreeSelection *selection;
949 select_all_helper (GtkRBTree *tree,
953 struct _TempTuple *tuple = data;
956 _gtk_rbtree_traverse (node->children,
957 node->children->root,
961 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
963 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
968 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
969 * can use it in other places without fear of the signal being emitted.
972 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
974 struct _TempTuple *tuple;
976 if (selection->tree_view->priv->tree == NULL)
979 /* Mark all nodes selected */
980 tuple = g_new (struct _TempTuple, 1);
981 tuple->selection = selection;
982 tuple->dirty = FALSE;
984 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
985 selection->tree_view->priv->tree->root,
999 * gtk_tree_selection_select_all:
1000 * @selection: A #GtkTreeSelection.
1002 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1006 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1008 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1009 g_return_if_fail (selection->tree_view != NULL);
1011 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1014 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1016 if (gtk_tree_selection_real_select_all (selection))
1017 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1021 unselect_all_helper (GtkRBTree *tree,
1025 struct _TempTuple *tuple = data;
1028 _gtk_rbtree_traverse (node->children,
1029 node->children->root,
1031 unselect_all_helper,
1033 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1035 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1040 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1042 struct _TempTuple *tuple;
1044 if (selection->type == GTK_SELECTION_SINGLE ||
1045 selection->type == GTK_SELECTION_BROWSE)
1047 GtkRBTree *tree = NULL;
1048 GtkRBNode *node = NULL;
1049 GtkTreePath *anchor_path;
1051 if (selection->tree_view->priv->anchor == NULL)
1054 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1056 if (anchor_path == NULL)
1059 _gtk_tree_view_find_node (selection->tree_view,
1064 gtk_tree_path_free (anchor_path);
1069 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1071 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1073 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1074 selection->tree_view->priv->anchor = NULL;
1082 tuple = g_new (struct _TempTuple, 1);
1083 tuple->selection = selection;
1084 tuple->dirty = FALSE;
1086 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1087 selection->tree_view->priv->tree->root,
1089 unselect_all_helper,
1103 * gtk_tree_selection_unselect_all:
1104 * @selection: A #GtkTreeSelection.
1106 * Unselects all the nodes.
1109 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1111 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1112 g_return_if_fail (selection->tree_view != NULL);
1114 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1117 if (gtk_tree_selection_real_unselect_all (selection))
1118 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1128 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1130 GtkTreePath *start_path,
1131 GtkTreePath *end_path)
1133 GtkRBNode *start_node, *end_node;
1134 GtkRBTree *start_tree, *end_tree;
1135 GtkTreePath *anchor_path = NULL;
1136 gboolean dirty = FALSE;
1138 switch (gtk_tree_path_compare (start_path, end_path))
1141 _gtk_tree_view_find_node (selection->tree_view,
1145 _gtk_tree_view_find_node (selection->tree_view,
1149 anchor_path = start_path;
1152 _gtk_tree_view_find_node (selection->tree_view,
1156 end_tree = start_tree;
1157 end_node = start_node;
1158 anchor_path = start_path;
1161 _gtk_tree_view_find_node (selection->tree_view,
1165 _gtk_tree_view_find_node (selection->tree_view,
1169 anchor_path = start_path;
1173 g_return_val_if_fail (start_node != NULL, FALSE);
1174 g_return_val_if_fail (end_node != NULL, FALSE);
1178 if (selection->tree_view->priv->anchor)
1179 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1181 selection->tree_view->priv->anchor =
1182 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1183 selection->tree_view->priv->model,
1189 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1191 if (start_node == end_node)
1194 if (start_node->children)
1196 start_tree = start_node->children;
1197 start_node = start_tree->root;
1198 while (start_node->left != start_tree->nil)
1199 start_node = start_node->left;
1203 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1204 if (start_tree == NULL)
1206 /* we just ran out of tree. That means someone passed in bogus values.
1218 * gtk_tree_selection_select_range:
1219 * @selection: A #GtkTreeSelection.
1220 * @start_path: The initial node of the range.
1221 * @end_path: The final node of the range.
1223 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1224 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1227 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1228 GtkTreePath *start_path,
1229 GtkTreePath *end_path)
1231 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1232 g_return_if_fail (selection->tree_view != NULL);
1233 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1234 g_return_if_fail (selection->tree_view->priv->model != NULL);
1236 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1237 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1241 * gtk_tree_selection_unselect_range:
1242 * @selection: A #GtkTreeSelection.
1243 * @start_path: The initial node of the range.
1244 * @end_path: The initial node of the range.
1246 * Unselects a range of nodes, determined by @start_path and @end_path
1252 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1253 GtkTreePath *start_path,
1254 GtkTreePath *end_path)
1256 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1257 g_return_if_fail (selection->tree_view != NULL);
1258 g_return_if_fail (selection->tree_view->priv->model != NULL);
1260 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1261 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1265 tree_column_is_sensitive (GtkTreeViewColumn *column,
1266 GtkTreeModel *model,
1269 GList *cells, *list;
1273 gtk_tree_view_column_cell_set_cell_data (column, model,
1274 iter, FALSE, FALSE);
1276 cells = gtk_tree_view_column_get_cell_renderers (column);
1281 g_object_get (list->data,
1282 "sensitive", &sensitive,
1283 "visible", &visible,
1286 if (visible && sensitive)
1291 g_list_free (cells);
1297 row_is_selectable (GtkTreeSelection *selection,
1303 gboolean sensitive = FALSE;
1305 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1308 if (!sensitive && selection->tree_view->priv->row_separator_func)
1310 /* never allow separators to be selected */
1311 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1313 selection->tree_view->priv->row_separator_data))
1317 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
1319 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
1321 if (!column->visible)
1324 sensitive = tree_column_is_sensitive (column, selection->tree_view->priv->model, &iter);
1330 if (selection->user_func)
1331 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1332 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1333 selection->user_data);
1339 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1343 * docs about the 'override_browse_mode', we set this flag when we want to
1344 * unset select the node and override the select browse mode behaviour (that is
1345 * 'one node should *always* be selected').
1348 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1352 GtkTreeSelectMode mode,
1353 gboolean override_browse_mode)
1357 GtkTreePath *anchor_path = NULL;
1359 if (selection->type == GTK_SELECTION_NONE)
1362 if (selection->tree_view->priv->anchor)
1363 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1365 if (selection->type == GTK_SELECTION_SINGLE ||
1366 selection->type == GTK_SELECTION_BROWSE)
1369 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1371 dirty = gtk_tree_selection_real_unselect_all (selection);
1373 /* Did we try to select the same node again? */
1374 else if (selection->type == GTK_SELECTION_SINGLE &&
1375 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1377 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1379 dirty = gtk_tree_selection_real_unselect_all (selection);
1386 /* We only want to select the new node if we can unselect the old one,
1387 * and we can select the new one. */
1388 dirty = row_is_selectable (selection, node, path);
1390 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1391 * unselect the new one
1394 dirty = gtk_tree_selection_real_unselect_all (selection);
1396 /* if dirty is TRUE at this point, we successfully unselected the
1397 * old one, and can then select the new one */
1400 if (selection->tree_view->priv->anchor)
1402 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1403 selection->tree_view->priv->anchor = NULL;
1406 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1408 selection->tree_view->priv->anchor =
1409 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1415 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1418 if (selection->tree_view->priv->anchor)
1419 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1421 selection->tree_view->priv->anchor =
1422 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1427 else if (selection->type == GTK_SELECTION_MULTIPLE)
1429 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1430 && (anchor_path == NULL))
1432 if (selection->tree_view->priv->anchor)
1433 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1435 selection->tree_view->priv->anchor =
1436 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1437 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1439 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1441 gtk_tree_selection_select_range (selection,
1445 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1447 flags = node->flags;
1448 if (selection->tree_view->priv->anchor)
1449 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1451 selection->tree_view->priv->anchor =
1452 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1454 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1455 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1457 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1459 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1461 dirty = gtk_tree_selection_real_unselect_all (selection);
1462 dirty |= gtk_tree_selection_real_modify_range (selection,
1469 dirty = gtk_tree_selection_real_unselect_all (selection);
1471 if (selection->tree_view->priv->anchor)
1472 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1474 selection->tree_view->priv->anchor =
1475 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1477 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1482 gtk_tree_path_free (anchor_path);
1485 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1488 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1492 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1497 gboolean selected = FALSE;
1498 GtkTreePath *path = NULL;
1502 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1504 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1505 selected = row_is_selectable (selection, node, path);
1506 gtk_tree_path_free (path);
1509 if (selected == TRUE)
1511 node->flags ^= GTK_RBNODE_IS_SELECTED;
1513 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);
1521 #define __GTK_TREE_SELECTION_C__
1522 #include "gtkaliasdef.c"