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"
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;
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 ();
671 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
673 gtk_tree_model_get_iter (selection->tree_view->priv->model,
675 (* func) (selection->tree_view->priv->model, path, &iter, data);
683 tree = node->children;
686 while (node->left != tree->nil)
689 gtk_tree_path_append_index (path, 0);
693 gboolean done = FALSE;
697 node = _gtk_rbtree_next (tree, node);
701 gtk_tree_path_next (path);
705 node = tree->parent_node;
706 tree = tree->parent_tree;
710 /* we've run out of tree */
711 /* We're done with this function */
716 gtk_tree_path_up (path);
726 gtk_tree_path_free (path);
728 g_signal_handler_disconnect (selection->tree_view->priv->model,
730 g_signal_handler_disconnect (selection->tree_view->priv->model,
732 g_signal_handler_disconnect (selection->tree_view->priv->model,
735 /* check if we have to spew a scary message */
738 ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
739 "This function is for observing the selections of the tree only. If\n"
740 "you are trying to get all selected items from the tree, try using\n"
741 "gtk_tree_selection_get_selected_rows instead.\n");
745 * gtk_tree_selection_select_path:
746 * @selection: A #GtkTreeSelection.
747 * @path: The #GtkTreePath to be selected.
749 * Select the row at @path.
752 gtk_tree_selection_select_path (GtkTreeSelection *selection,
758 GtkTreeSelectMode mode = 0;
760 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
761 g_return_if_fail (selection->tree_view != NULL);
762 g_return_if_fail (path != NULL);
764 ret = _gtk_tree_view_find_node (selection->tree_view,
769 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
773 if (selection->type == GTK_SELECTION_MULTIPLE)
774 mode = GTK_TREE_SELECT_MODE_TOGGLE;
776 _gtk_tree_selection_internal_select_node (selection,
785 * gtk_tree_selection_unselect_path:
786 * @selection: A #GtkTreeSelection.
787 * @path: The #GtkTreePath to be unselected.
789 * Unselects the row at @path.
792 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
799 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
800 g_return_if_fail (selection->tree_view != NULL);
801 g_return_if_fail (path != NULL);
803 ret = _gtk_tree_view_find_node (selection->tree_view,
808 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
812 _gtk_tree_selection_internal_select_node (selection,
816 GTK_TREE_SELECT_MODE_TOGGLE,
821 * gtk_tree_selection_select_iter:
822 * @selection: A #GtkTreeSelection.
823 * @iter: The #GtkTreeIter to be selected.
825 * Selects the specified iterator.
828 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
833 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
834 g_return_if_fail (selection->tree_view != NULL);
835 g_return_if_fail (selection->tree_view->priv->model != NULL);
836 g_return_if_fail (iter != NULL);
838 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
844 gtk_tree_selection_select_path (selection, path);
845 gtk_tree_path_free (path);
850 * gtk_tree_selection_unselect_iter:
851 * @selection: A #GtkTreeSelection.
852 * @iter: The #GtkTreeIter to be unselected.
854 * Unselects the specified iterator.
857 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
862 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
863 g_return_if_fail (selection->tree_view != NULL);
864 g_return_if_fail (selection->tree_view->priv->model != NULL);
865 g_return_if_fail (iter != NULL);
867 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
873 gtk_tree_selection_unselect_path (selection, path);
874 gtk_tree_path_free (path);
878 * gtk_tree_selection_path_is_selected:
879 * @selection: A #GtkTreeSelection.
880 * @path: A #GtkTreePath to check selection on.
882 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
883 * does not point to a valid location, %FALSE is returned
885 * Return value: %TRUE if @path is selected.
888 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
895 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
896 g_return_val_if_fail (path != NULL, FALSE);
897 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
899 if (selection->tree_view->priv->model == NULL)
902 ret = _gtk_tree_view_find_node (selection->tree_view,
907 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
915 * gtk_tree_selection_iter_is_selected:
916 * @selection: A #GtkTreeSelection
917 * @iter: A valid #GtkTreeIter
919 * Returns %TRUE if the row at @iter is currently selected.
921 * Return value: %TRUE, if @iter is selected
924 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
930 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
931 g_return_val_if_fail (iter != NULL, FALSE);
932 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
933 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
935 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
939 retval = gtk_tree_selection_path_is_selected (selection, path);
940 gtk_tree_path_free (path);
946 /* Wish I was in python, right now... */
948 GtkTreeSelection *selection;
953 select_all_helper (GtkRBTree *tree,
957 struct _TempTuple *tuple = data;
960 _gtk_rbtree_traverse (node->children,
961 node->children->root,
965 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
967 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
972 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
973 * can use it in other places without fear of the signal being emitted.
976 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
978 struct _TempTuple *tuple;
980 if (selection->tree_view->priv->tree == NULL)
983 /* Mark all nodes selected */
984 tuple = g_new (struct _TempTuple, 1);
985 tuple->selection = selection;
986 tuple->dirty = FALSE;
988 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
989 selection->tree_view->priv->tree->root,
1003 * gtk_tree_selection_select_all:
1004 * @selection: A #GtkTreeSelection.
1006 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1010 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1012 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1013 g_return_if_fail (selection->tree_view != NULL);
1015 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1018 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1020 if (gtk_tree_selection_real_select_all (selection))
1021 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1025 unselect_all_helper (GtkRBTree *tree,
1029 struct _TempTuple *tuple = data;
1032 _gtk_rbtree_traverse (node->children,
1033 node->children->root,
1035 unselect_all_helper,
1037 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1039 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1044 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1046 struct _TempTuple *tuple;
1048 if (selection->type == GTK_SELECTION_SINGLE ||
1049 selection->type == GTK_SELECTION_BROWSE)
1051 GtkRBTree *tree = NULL;
1052 GtkRBNode *node = NULL;
1053 GtkTreePath *anchor_path;
1055 if (selection->tree_view->priv->anchor == NULL)
1058 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1060 if (anchor_path == NULL)
1063 _gtk_tree_view_find_node (selection->tree_view,
1068 gtk_tree_path_free (anchor_path);
1073 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1075 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1077 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1078 selection->tree_view->priv->anchor = NULL;
1086 tuple = g_new (struct _TempTuple, 1);
1087 tuple->selection = selection;
1088 tuple->dirty = FALSE;
1090 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1091 selection->tree_view->priv->tree->root,
1093 unselect_all_helper,
1107 * gtk_tree_selection_unselect_all:
1108 * @selection: A #GtkTreeSelection.
1110 * Unselects all the nodes.
1113 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1115 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1116 g_return_if_fail (selection->tree_view != NULL);
1118 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1121 if (gtk_tree_selection_real_unselect_all (selection))
1122 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1132 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1134 GtkTreePath *start_path,
1135 GtkTreePath *end_path)
1137 GtkRBNode *start_node, *end_node;
1138 GtkRBTree *start_tree, *end_tree;
1139 GtkTreePath *anchor_path = NULL;
1140 gboolean dirty = FALSE;
1142 switch (gtk_tree_path_compare (start_path, end_path))
1145 _gtk_tree_view_find_node (selection->tree_view,
1149 _gtk_tree_view_find_node (selection->tree_view,
1153 anchor_path = start_path;
1156 _gtk_tree_view_find_node (selection->tree_view,
1160 end_tree = start_tree;
1161 end_node = start_node;
1162 anchor_path = start_path;
1165 _gtk_tree_view_find_node (selection->tree_view,
1169 _gtk_tree_view_find_node (selection->tree_view,
1173 anchor_path = start_path;
1177 g_return_val_if_fail (start_node != NULL, FALSE);
1178 g_return_val_if_fail (end_node != NULL, FALSE);
1182 if (selection->tree_view->priv->anchor)
1183 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1185 selection->tree_view->priv->anchor =
1186 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1187 selection->tree_view->priv->model,
1193 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1195 if (start_node == end_node)
1198 if (start_node->children)
1200 start_tree = start_node->children;
1201 start_node = start_tree->root;
1202 while (start_node->left != start_tree->nil)
1203 start_node = start_node->left;
1207 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1208 if (start_tree == NULL)
1210 /* we just ran out of tree. That means someone passed in bogus values.
1222 * gtk_tree_selection_select_range:
1223 * @selection: A #GtkTreeSelection.
1224 * @start_path: The initial node of the range.
1225 * @end_path: The final node of the range.
1227 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1228 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1231 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1232 GtkTreePath *start_path,
1233 GtkTreePath *end_path)
1235 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1236 g_return_if_fail (selection->tree_view != NULL);
1237 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1238 g_return_if_fail (selection->tree_view->priv->model != NULL);
1240 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1241 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1245 * gtk_tree_selection_unselect_range:
1246 * @selection: A #GtkTreeSelection.
1247 * @start_path: The initial node of the range.
1248 * @end_path: The initial node of the range.
1250 * Unselects a range of nodes, determined by @start_path and @end_path
1256 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1257 GtkTreePath *start_path,
1258 GtkTreePath *end_path)
1260 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1261 g_return_if_fail (selection->tree_view != NULL);
1262 g_return_if_fail (selection->tree_view->priv->model != NULL);
1264 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1265 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1269 tree_column_is_sensitive (GtkTreeViewColumn *column,
1270 GtkTreeModel *model,
1273 GList *cells, *list;
1277 gtk_tree_view_column_cell_set_cell_data (column, model,
1278 iter, FALSE, FALSE);
1280 cells = gtk_tree_view_column_get_cell_renderers (column);
1285 g_object_get (list->data,
1286 "sensitive", &sensitive,
1287 "visible", &visible,
1290 if (visible && sensitive)
1295 g_list_free (cells);
1301 row_is_selectable (GtkTreeSelection *selection,
1307 gboolean sensitive = FALSE;
1309 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1312 if (!sensitive && selection->tree_view->priv->row_separator_func)
1314 /* never allow separators to be selected */
1315 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1317 selection->tree_view->priv->row_separator_data))
1321 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
1323 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
1325 if (!column->visible)
1328 sensitive = tree_column_is_sensitive (column, selection->tree_view->priv->model, &iter);
1334 if (selection->user_func)
1335 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1336 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1337 selection->user_data);
1343 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1347 * docs about the 'override_browse_mode', we set this flag when we want to
1348 * unset select the node and override the select browse mode behaviour (that is
1349 * 'one node should *always* be selected').
1352 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1356 GtkTreeSelectMode mode,
1357 gboolean override_browse_mode)
1361 GtkTreePath *anchor_path = NULL;
1363 if (selection->type == GTK_SELECTION_NONE)
1366 if (selection->tree_view->priv->anchor)
1367 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1369 if (selection->type == GTK_SELECTION_SINGLE ||
1370 selection->type == GTK_SELECTION_BROWSE)
1373 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1375 dirty = gtk_tree_selection_real_unselect_all (selection);
1377 /* Did we try to select the same node again? */
1378 else if (selection->type == GTK_SELECTION_SINGLE &&
1379 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1381 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1383 dirty = gtk_tree_selection_real_unselect_all (selection);
1390 /* We only want to select the new node if we can unselect the old one,
1391 * and we can select the new one. */
1392 dirty = row_is_selectable (selection, node, path);
1394 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1395 * unselect the new one
1398 dirty = gtk_tree_selection_real_unselect_all (selection);
1400 /* if dirty is TRUE at this point, we successfully unselected the
1401 * old one, and can then select the new one */
1404 if (selection->tree_view->priv->anchor)
1406 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1407 selection->tree_view->priv->anchor = NULL;
1410 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1412 selection->tree_view->priv->anchor =
1413 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1419 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1422 if (selection->tree_view->priv->anchor)
1423 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1425 selection->tree_view->priv->anchor =
1426 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1431 else if (selection->type == GTK_SELECTION_MULTIPLE)
1433 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1434 && (anchor_path == NULL))
1436 if (selection->tree_view->priv->anchor)
1437 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1439 selection->tree_view->priv->anchor =
1440 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1441 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1443 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1445 gtk_tree_selection_select_range (selection,
1449 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1451 flags = node->flags;
1452 if (selection->tree_view->priv->anchor)
1453 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1455 selection->tree_view->priv->anchor =
1456 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1458 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1459 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1461 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1463 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1465 dirty = gtk_tree_selection_real_unselect_all (selection);
1466 dirty |= gtk_tree_selection_real_modify_range (selection,
1473 dirty = gtk_tree_selection_real_unselect_all (selection);
1475 if (selection->tree_view->priv->anchor)
1476 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1478 selection->tree_view->priv->anchor =
1479 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1481 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1486 gtk_tree_path_free (anchor_path);
1489 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1492 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1496 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1501 gboolean selected = FALSE;
1502 GtkTreePath *path = NULL;
1506 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1508 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1509 selected = row_is_selectable (selection, node, path);
1510 gtk_tree_path_free (path);
1513 if (selected == TRUE)
1515 node->flags ^= GTK_RBNODE_IS_SELECTED;
1517 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);
1525 #define __GTK_TREE_SELECTION_C__
1526 #include "gtkaliasdef.c"