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.
23 #include "gtktreeselection.h"
24 #include "gtktreeprivate.h"
25 #include "gtkrbtree.h"
26 #include "gtkmarshalers.h"
28 static void gtk_tree_selection_init (GtkTreeSelection *selection);
29 static void gtk_tree_selection_class_init (GtkTreeSelectionClass *class);
31 static void gtk_tree_selection_finalize (GObject *object);
32 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
33 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
34 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
45 static GObjectClass *parent_class = NULL;
46 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
49 gtk_tree_selection_get_type (void)
51 static GType selection_type = 0;
55 static const GTypeInfo selection_info =
57 sizeof (GtkTreeSelectionClass),
59 NULL, /* base_finalize */
60 (GClassInitFunc) gtk_tree_selection_class_init,
61 NULL, /* class_finalize */
62 NULL, /* class_data */
63 sizeof (GtkTreeSelection),
65 (GInstanceInitFunc) gtk_tree_selection_init
69 g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection",
73 return selection_type;
77 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
79 GObjectClass *object_class;
81 object_class = (GObjectClass*) class;
82 parent_class = g_type_class_peek_parent (class);
84 object_class->finalize = gtk_tree_selection_finalize;
85 class->changed = NULL;
87 tree_selection_signals[CHANGED] =
88 g_signal_new ("changed",
89 G_OBJECT_CLASS_TYPE (object_class),
91 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
93 _gtk_marshal_VOID__VOID,
98 gtk_tree_selection_init (GtkTreeSelection *selection)
100 selection->type = GTK_SELECTION_SINGLE;
104 gtk_tree_selection_finalize (GObject *object)
106 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
108 if (selection->destroy)
110 GtkDestroyNotify d = selection->destroy;
112 selection->destroy = NULL;
113 d (selection->user_data);
116 /* chain parent_class' handler */
117 G_OBJECT_CLASS (parent_class)->finalize (object);
121 * _gtk_tree_selection_new:
123 * Creates a new #GtkTreeSelection object. This function should not be invoked,
124 * as each #GtkTreeView will create its own #GtkTreeSelection.
126 * Return value: A newly created #GtkTreeSelection object.
129 _gtk_tree_selection_new (void)
131 GtkTreeSelection *selection;
133 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
139 * _gtk_tree_selection_new_with_tree_view:
140 * @tree_view: The #GtkTreeView.
142 * Creates a new #GtkTreeSelection object. This function should not be invoked,
143 * as each #GtkTreeView will create its own #GtkTreeSelection.
145 * Return value: A newly created #GtkTreeSelection object.
148 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
150 GtkTreeSelection *selection;
152 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
154 selection = _gtk_tree_selection_new ();
155 _gtk_tree_selection_set_tree_view (selection, tree_view);
161 * _gtk_tree_selection_set_tree_view:
162 * @selection: A #GtkTreeSelection.
163 * @tree_view: The #GtkTreeView.
165 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
166 * it is used internally by #GtkTreeView.
169 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
170 GtkTreeView *tree_view)
172 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
173 if (tree_view != NULL)
174 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
176 selection->tree_view = tree_view;
180 * gtk_tree_selection_set_mode:
181 * @selection: A #GtkTreeSelection.
182 * @type: The selection mode
184 * Sets the selection mode of the @selection. If the previous type was
185 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
186 * previously selected.
189 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
190 GtkSelectionMode type)
192 GtkTreeSelectionFunc tmp_func;
193 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
195 if (selection->type == type)
199 if (type == GTK_SELECTION_NONE)
201 /* We do this so that we unconditionally unset all rows
203 tmp_func = selection->user_func;
204 selection->user_func = NULL;
205 gtk_tree_selection_unselect_all (selection);
206 selection->user_func = tmp_func;
208 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
209 selection->tree_view->priv->anchor = NULL;
211 else if (type == GTK_SELECTION_SINGLE ||
212 type == GTK_SELECTION_BROWSE)
214 GtkRBTree *tree = NULL;
215 GtkRBNode *node = NULL;
216 gint selected = FALSE;
217 GtkTreePath *anchor_path = NULL;
219 if (selection->tree_view->priv->anchor)
221 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
225 _gtk_tree_view_find_node (selection->tree_view,
230 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
235 /* We do this so that we unconditionally unset all rows
237 tmp_func = selection->user_func;
238 selection->user_func = NULL;
239 gtk_tree_selection_unselect_all (selection);
240 selection->user_func = tmp_func;
242 if (node && selected)
243 _gtk_tree_selection_internal_select_node (selection,
250 gtk_tree_path_free (anchor_path);
253 selection->type = type;
257 * gtk_tree_selection_get_mode:
258 * @selection: a #GtkTreeSelection
260 * Gets the selection mode for @selection. See
261 * gtk_tree_selection_set_mode().
263 * Return value: the current selection mode
266 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
268 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
270 return selection->type;
274 * gtk_tree_selection_set_select_function:
275 * @selection: A #GtkTreeSelection.
276 * @func: The selection function.
277 * @data: The selection function's data.
278 * @destroy: The destroy function for user data. May be NULL.
280 * Sets the selection function. If set, this function is called before any node
281 * is selected or unselected, giving some control over which nodes are selected.
282 * The select function should return %TRUE if the state of the node may be toggled,
283 * and %FALSE if the state of the node should be left unchanged.
286 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
287 GtkTreeSelectionFunc func,
289 GtkDestroyNotify destroy)
291 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
292 g_return_if_fail (func != NULL);
294 if (selection->destroy)
296 GtkDestroyNotify d = selection->destroy;
298 selection->destroy = NULL;
299 d (selection->user_data);
302 selection->user_func = func;
303 selection->user_data = data;
304 selection->destroy = destroy;
308 * gtk_tree_selection_get_user_data:
309 * @selection: A #GtkTreeSelection.
311 * Returns the user data for the selection function.
313 * Return value: The user data.
316 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
318 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
320 return selection->user_data;
324 * gtk_tree_selection_get_tree_view:
325 * @selection: A #GtkTreeSelection
327 * Returns the tree view associated with @selection.
329 * Return value: A #GtkTreeView
332 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
334 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
336 return selection->tree_view;
340 * gtk_tree_selection_get_selected:
341 * @selection: A #GtkTreeSelection.
342 * @model: A pointer to set to the #GtkTreeModel, or NULL.
343 * @iter: The #GtkTreeIter, or NULL.
345 * Sets @iter to the currently selected node if @selection is set to
346 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
347 * just want to test if @selection has any selected nodes. @model is filled
348 * with the current model as a convenience. This function will not work if you
349 * use @selection is #GTK_SELECTION_MULTIPLE.
351 * Return value: TRUE, if there is a selected node.
354 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
355 GtkTreeModel **model,
360 GtkTreePath *anchor_path;
364 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
365 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
366 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
370 memset (iter, 0, sizeof (GtkTreeIter));
373 *model = selection->tree_view->priv->model;
375 if (selection->tree_view->priv->anchor == NULL)
378 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
380 if (anchor_path == NULL)
385 found_node = !_gtk_tree_view_find_node (selection->tree_view,
390 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
392 /* we only want to return the anchor if it exists in the rbtree and
398 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
404 /* We don't want to return the anchor if it isn't actually selected.
409 gtk_tree_path_free (anchor_path);
415 * gtk_tree_selection_get_selected_rows:
416 * @selection: A #GtkTreeSelection.
417 * @model: A pointer to set to the #GtkTreeModel, or NULL.
419 * Creates a list of path of all selected rows. Additionally, if you are
420 * planning on modifying the model after calling this function, you may
421 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
422 * To do this, you can use gtk_tree_row_reference_new().
424 * To free the return value, use:
425 * <informalexample><programlisting>
426 * g_list_foreach (list, gtk_tree_path_free, NULL);
427 * g_list_free (list);
428 * </programlisting></informalexample>
430 * Return value: A #GList containing a #GtkTreePath for each selected row.
435 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
436 GtkTreeModel **model)
439 GtkRBTree *tree = NULL;
440 GtkRBNode *node = NULL;
443 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
444 g_return_val_if_fail (selection->tree_view != NULL, NULL);
446 if (selection->tree_view->priv->tree == NULL ||
447 selection->tree_view->priv->tree->root == NULL)
451 *model = selection->tree_view->priv->model;
453 if (selection->type == GTK_SELECTION_NONE)
455 else if (selection->type != GTK_SELECTION_MULTIPLE)
459 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
463 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
464 list = g_list_append (list, path);
472 tree = selection->tree_view->priv->tree;
473 node = selection->tree_view->priv->tree->root;
475 while (node->left != tree->nil)
477 path = gtk_tree_path_new_first ();
481 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
482 list = g_list_prepend (list, gtk_tree_path_copy (path));
486 tree = node->children;
489 while (node->left != tree->nil)
492 gtk_tree_path_append_index (path, 0);
496 gboolean done = FALSE;
500 node = _gtk_rbtree_next (tree, node);
504 gtk_tree_path_next (path);
508 node = tree->parent_node;
509 tree = tree->parent_tree;
513 gtk_tree_path_free (path);
518 gtk_tree_path_up (path);
526 gtk_tree_path_free (path);
529 return g_list_reverse (list);
533 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
537 gint *count = (gint *)data;
539 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
543 _gtk_rbtree_traverse (node->children, node->children->root,
545 gtk_tree_selection_count_selected_rows_helper, data);
549 * gtk_tree_selection_count_selected_rows:
550 * @selection: A #GtkTreeSelection.
552 * Returns the number of rows that have been selected in @tree.
554 * Return value: The number of rows selected.
559 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
565 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
566 g_return_val_if_fail (selection->tree_view != NULL, 0);
568 if (selection->tree_view->priv->tree == NULL ||
569 selection->tree_view->priv->tree->root == NULL)
572 if (selection->type == GTK_SELECTION_SINGLE ||
573 selection->type == GTK_SELECTION_BROWSE)
575 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
581 tree = selection->tree_view->priv->tree;
582 node = selection->tree_view->priv->tree->root;
584 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
585 selection->tree_view->priv->tree->root,
587 gtk_tree_selection_count_selected_rows_helper,
593 /* gtk_tree_selection_selected_foreach helper */
595 model_changed (gpointer data)
597 gboolean *stop = (gboolean *)data;
603 * gtk_tree_selection_selected_foreach:
604 * @selection: A #GtkTreeSelection.
605 * @func: The function to call for each selected node.
606 * @data: user data to pass to the function.
608 * Calls a function for each selected node. Note that you cannot modify
609 * the tree or selection from within this function. As a result,
610 * gtk_tree_selection_get_selected_rows() might be more useful.
613 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
614 GtkTreeSelectionForeachFunc func,
622 guint inserted_id, deleted_id, reordered_id;
623 gboolean stop = FALSE, has_next = TRUE, has_parent = TRUE;
625 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
626 g_return_if_fail (selection->tree_view != NULL);
629 selection->tree_view->priv->tree == NULL ||
630 selection->tree_view->priv->tree->root == NULL)
633 if (selection->type == GTK_SELECTION_SINGLE ||
634 selection->type == GTK_SELECTION_BROWSE)
636 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
638 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
639 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
640 (* func) (selection->tree_view->priv->model, path, &iter, data);
641 gtk_tree_path_free (path);
646 tree = selection->tree_view->priv->tree;
647 node = selection->tree_view->priv->tree->root;
649 while (node->left != tree->nil)
652 /* connect to signals to monitor changes in treemodel */
653 inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
655 G_CALLBACK (model_changed),
657 deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
659 G_CALLBACK (model_changed),
661 reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
663 G_CALLBACK (model_changed),
666 /* find the node internally */
667 path = gtk_tree_path_new_first ();
668 gtk_tree_model_get_iter (selection->tree_view->priv->model,
673 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
674 (* func) (selection->tree_view->priv->model, path, &iter, data);
684 tree = node->children;
686 while (node->left != tree->nil)
689 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
690 gtk_tree_path_append_index (path, 0);
692 /* we do the sanity check at the bottom of this function */
698 gboolean done = FALSE;
701 node = _gtk_rbtree_next (tree, node);
706 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
708 gtk_tree_path_next (path);
710 /* we do the sanity check at the bottom of this function */
717 GtkTreeIter tmp_iter = iter;
719 node = tree->parent_node;
720 tree = tree->parent_tree;
723 /* we've run out of tree */
724 /* We're done with this function */
729 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
730 gtk_tree_path_up (path);
732 /* we do the sanity check at the bottom of this function */
744 gtk_tree_path_free (path);
746 g_signal_handler_disconnect (selection->tree_view->priv->model,
748 g_signal_handler_disconnect (selection->tree_view->priv->model,
750 g_signal_handler_disconnect (selection->tree_view->priv->model,
753 /* check if we have to spew a scary message */
755 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
757 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
760 ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
761 "This function is for observing the selections of the tree only. If\n"
762 "you are trying to get all selected items from the tree, try using\n"
763 "gtk_tree_selection_get_selected_rows instead.\n");
767 * gtk_tree_selection_select_path:
768 * @selection: A #GtkTreeSelection.
769 * @path: The #GtkTreePath to be selected.
771 * Select the row at @path.
774 gtk_tree_selection_select_path (GtkTreeSelection *selection,
780 GtkTreeSelectMode mode = 0;
782 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
783 g_return_if_fail (selection->tree_view != NULL);
784 g_return_if_fail (path != NULL);
786 ret = _gtk_tree_view_find_node (selection->tree_view,
791 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
795 if (selection->type == GTK_SELECTION_MULTIPLE)
796 mode = GTK_TREE_SELECT_MODE_TOGGLE;
798 _gtk_tree_selection_internal_select_node (selection,
807 * gtk_tree_selection_unselect_path:
808 * @selection: A #GtkTreeSelection.
809 * @path: The #GtkTreePath to be unselected.
811 * Unselects the row at @path.
814 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
821 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
822 g_return_if_fail (selection->tree_view != NULL);
823 g_return_if_fail (path != NULL);
825 ret = _gtk_tree_view_find_node (selection->tree_view,
830 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
834 _gtk_tree_selection_internal_select_node (selection,
838 GTK_TREE_SELECT_MODE_TOGGLE,
843 * gtk_tree_selection_select_iter:
844 * @selection: A #GtkTreeSelection.
845 * @iter: The #GtkTreeIter to be selected.
847 * Selects the specified iterator.
850 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
855 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
856 g_return_if_fail (selection->tree_view != NULL);
857 g_return_if_fail (selection->tree_view->priv->model != NULL);
858 g_return_if_fail (iter != NULL);
860 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
866 gtk_tree_selection_select_path (selection, path);
867 gtk_tree_path_free (path);
872 * gtk_tree_selection_unselect_iter:
873 * @selection: A #GtkTreeSelection.
874 * @iter: The #GtkTreeIter to be unselected.
876 * Unselects the specified iterator.
879 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
884 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
885 g_return_if_fail (selection->tree_view != NULL);
886 g_return_if_fail (selection->tree_view->priv->model != NULL);
887 g_return_if_fail (iter != NULL);
889 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
895 gtk_tree_selection_unselect_path (selection, path);
896 gtk_tree_path_free (path);
900 * gtk_tree_selection_path_is_selected:
901 * @selection: A #GtkTreeSelection.
902 * @path: A #GtkTreePath to check selection on.
904 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
905 * does not point to a valid location, %FALSE is returned
907 * Return value: %TRUE if @path is selected.
910 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
917 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
918 g_return_val_if_fail (path != NULL, FALSE);
919 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
921 if (selection->tree_view->priv->model == NULL)
924 ret = _gtk_tree_view_find_node (selection->tree_view,
929 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
937 * gtk_tree_selection_iter_is_selected:
938 * @selection: A #GtkTreeSelection
939 * @iter: A valid #GtkTreeIter
941 * Returns %TRUE if the row at @iter is currently selected.
943 * Return value: %TRUE, if @iter is selected
946 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
952 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
953 g_return_val_if_fail (iter != NULL, FALSE);
954 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
955 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
957 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
961 retval = gtk_tree_selection_path_is_selected (selection, path);
962 gtk_tree_path_free (path);
968 /* Wish I was in python, right now... */
970 GtkTreeSelection *selection;
975 select_all_helper (GtkRBTree *tree,
979 struct _TempTuple *tuple = data;
982 _gtk_rbtree_traverse (node->children,
983 node->children->root,
987 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
989 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
994 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
995 * can use it in other places without fear of the signal being emitted.
998 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
1000 struct _TempTuple *tuple;
1002 if (selection->tree_view->priv->tree == NULL)
1005 /* Mark all nodes selected */
1006 tuple = g_new (struct _TempTuple, 1);
1007 tuple->selection = selection;
1008 tuple->dirty = FALSE;
1010 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1011 selection->tree_view->priv->tree->root,
1025 * gtk_tree_selection_select_all:
1026 * @selection: A #GtkTreeSelection.
1028 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1032 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1034 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1035 g_return_if_fail (selection->tree_view != NULL);
1037 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1040 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1042 if (gtk_tree_selection_real_select_all (selection))
1043 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1047 unselect_all_helper (GtkRBTree *tree,
1051 struct _TempTuple *tuple = data;
1054 _gtk_rbtree_traverse (node->children,
1055 node->children->root,
1057 unselect_all_helper,
1059 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1061 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1066 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1068 struct _TempTuple *tuple;
1070 if (selection->type == GTK_SELECTION_SINGLE ||
1071 selection->type == GTK_SELECTION_BROWSE)
1073 GtkRBTree *tree = NULL;
1074 GtkRBNode *node = NULL;
1075 GtkTreePath *anchor_path;
1077 if (selection->tree_view->priv->anchor == NULL)
1080 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1082 if (anchor_path == NULL)
1085 _gtk_tree_view_find_node (selection->tree_view,
1090 gtk_tree_path_free (anchor_path);
1095 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1097 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1099 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1100 selection->tree_view->priv->anchor = NULL;
1108 tuple = g_new (struct _TempTuple, 1);
1109 tuple->selection = selection;
1110 tuple->dirty = FALSE;
1112 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1113 selection->tree_view->priv->tree->root,
1115 unselect_all_helper,
1129 * gtk_tree_selection_unselect_all:
1130 * @selection: A #GtkTreeSelection.
1132 * Unselects all the nodes.
1135 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1137 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1138 g_return_if_fail (selection->tree_view != NULL);
1140 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1143 if (gtk_tree_selection_real_unselect_all (selection))
1144 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1154 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1156 GtkTreePath *start_path,
1157 GtkTreePath *end_path)
1159 GtkRBNode *start_node, *end_node;
1160 GtkRBTree *start_tree, *end_tree;
1161 GtkTreePath *anchor_path = NULL;
1162 gboolean dirty = FALSE;
1164 switch (gtk_tree_path_compare (start_path, end_path))
1167 _gtk_tree_view_find_node (selection->tree_view,
1171 _gtk_tree_view_find_node (selection->tree_view,
1175 anchor_path = start_path;
1178 _gtk_tree_view_find_node (selection->tree_view,
1182 end_tree = start_tree;
1183 end_node = start_node;
1184 anchor_path = start_path;
1187 _gtk_tree_view_find_node (selection->tree_view,
1191 _gtk_tree_view_find_node (selection->tree_view,
1195 anchor_path = start_path;
1199 g_return_val_if_fail (start_node != NULL, FALSE);
1200 g_return_val_if_fail (end_node != NULL, FALSE);
1204 if (selection->tree_view->priv->anchor)
1205 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1207 selection->tree_view->priv->anchor =
1208 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1209 selection->tree_view->priv->model,
1215 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1217 if (start_node == end_node)
1220 if (start_node->children)
1222 start_tree = start_node->children;
1223 start_node = start_tree->root;
1224 while (start_node->left != start_tree->nil)
1225 start_node = start_node->left;
1229 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1230 if (start_tree == NULL)
1232 /* we just ran out of tree. That means someone passed in bogus values.
1244 * gtk_tree_selection_select_range:
1245 * @selection: A #GtkTreeSelection.
1246 * @start_path: The initial node of the range.
1247 * @end_path: The final node of the range.
1249 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1250 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1253 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1254 GtkTreePath *start_path,
1255 GtkTreePath *end_path)
1257 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1258 g_return_if_fail (selection->tree_view != NULL);
1259 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1260 g_return_if_fail (selection->tree_view->priv->model != NULL);
1262 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1263 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1267 * gtk_tree_selection_unselect_range:
1268 * @selection: A #GtkTreeSelection.
1269 * @start_path: The initial node of the range.
1270 * @end_path: The initial node of the range.
1272 * Unselects a range of nodes, determined by @start_path and @end_path
1278 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1279 GtkTreePath *start_path,
1280 GtkTreePath *end_path)
1282 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1283 g_return_if_fail (selection->tree_view != NULL);
1284 g_return_if_fail (selection->tree_view->priv->model != NULL);
1286 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1287 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1291 tree_column_is_sensitive (GtkTreeViewColumn *column,
1292 GtkTreeModel *model,
1295 GList *cells, *list;
1299 gtk_tree_view_column_cell_set_cell_data (column, model,
1300 iter, FALSE, FALSE);
1302 cells = gtk_tree_view_column_get_cell_renderers (column);
1307 g_object_get (G_OBJECT (list->data),
1308 "sensitive", &sensitive,
1309 "visible", &visible,
1312 if (visible && sensitive)
1317 g_list_free (cells);
1323 row_is_selectable (GtkTreeSelection *selection,
1329 gboolean sensitive = FALSE;
1331 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1334 if (!sensitive && selection->tree_view->priv->row_separator_func)
1336 /* never allow separators to be selected */
1337 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1339 selection->tree_view->priv->row_separator_data))
1343 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
1345 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
1347 if (!column->visible)
1350 sensitive = tree_column_is_sensitive (column, selection->tree_view->priv->model, &iter);
1356 if (selection->user_func)
1357 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1358 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1359 selection->user_data);
1365 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1369 * docs about the 'override_browse_mode', we set this flag when we want to
1370 * unset select the node and override the select browse mode behaviour (that is
1371 * 'one node should *always* be selected').
1374 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1378 GtkTreeSelectMode mode,
1379 gboolean override_browse_mode)
1383 GtkTreePath *anchor_path = NULL;
1385 if (selection->type == GTK_SELECTION_NONE)
1388 if (selection->tree_view->priv->anchor)
1389 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1391 if (selection->type == GTK_SELECTION_SINGLE ||
1392 selection->type == GTK_SELECTION_BROWSE)
1395 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1397 dirty = gtk_tree_selection_real_unselect_all (selection);
1399 /* Did we try to select the same node again? */
1400 else if (selection->type == GTK_SELECTION_SINGLE &&
1401 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1403 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1405 dirty = gtk_tree_selection_real_unselect_all (selection);
1412 /* We only want to select the new node if we can unselect the old one,
1413 * and we can select the new one. */
1414 dirty = row_is_selectable (selection, node, path);
1416 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1417 * unselect the new one
1420 dirty = gtk_tree_selection_real_unselect_all (selection);
1422 /* if dirty is TRUE at this point, we successfully unselected the
1423 * old one, and can then select the new one */
1426 if (selection->tree_view->priv->anchor)
1428 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1429 selection->tree_view->priv->anchor = NULL;
1432 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1434 selection->tree_view->priv->anchor =
1435 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1441 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1444 if (selection->tree_view->priv->anchor)
1445 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1447 selection->tree_view->priv->anchor =
1448 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1453 else if (selection->type == GTK_SELECTION_MULTIPLE)
1455 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1456 && (anchor_path == NULL))
1458 if (selection->tree_view->priv->anchor)
1459 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1461 selection->tree_view->priv->anchor =
1462 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1463 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1465 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1467 gtk_tree_selection_select_range (selection,
1471 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1473 flags = node->flags;
1474 if (selection->tree_view->priv->anchor)
1475 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1477 selection->tree_view->priv->anchor =
1478 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1480 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1481 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1483 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1485 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1487 dirty = gtk_tree_selection_real_unselect_all (selection);
1488 dirty |= gtk_tree_selection_real_modify_range (selection,
1495 dirty = gtk_tree_selection_real_unselect_all (selection);
1497 if (selection->tree_view->priv->anchor)
1498 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1500 selection->tree_view->priv->anchor =
1501 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1503 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1508 gtk_tree_path_free (anchor_path);
1511 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1514 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1518 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1523 gboolean selected = FALSE;
1524 GtkTreePath *path = NULL;
1528 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1530 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1531 selected = row_is_selectable (selection, node, path);
1532 gtk_tree_path_free (path);
1535 if (selected == TRUE)
1537 node->flags ^= GTK_RBNODE_IS_SELECTED;
1539 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);