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_finalize (GObject *object);
30 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
31 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
32 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
43 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
45 G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
48 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
50 GObjectClass *object_class;
52 object_class = (GObjectClass*) class;
54 object_class->finalize = gtk_tree_selection_finalize;
55 class->changed = NULL;
57 tree_selection_signals[CHANGED] =
58 g_signal_new (I_("changed"),
59 G_OBJECT_CLASS_TYPE (object_class),
61 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
63 _gtk_marshal_VOID__VOID,
68 gtk_tree_selection_init (GtkTreeSelection *selection)
70 selection->type = GTK_SELECTION_SINGLE;
74 gtk_tree_selection_finalize (GObject *object)
76 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
78 if (selection->destroy)
80 GDestroyNotify d = selection->destroy;
82 selection->destroy = NULL;
83 d (selection->user_data);
86 /* chain parent_class' handler */
87 G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
91 * _gtk_tree_selection_new:
93 * Creates a new #GtkTreeSelection object. This function should not be invoked,
94 * as each #GtkTreeView will create its own #GtkTreeSelection.
96 * Return value: A newly created #GtkTreeSelection object.
99 _gtk_tree_selection_new (void)
101 GtkTreeSelection *selection;
103 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
109 * _gtk_tree_selection_new_with_tree_view:
110 * @tree_view: The #GtkTreeView.
112 * Creates a new #GtkTreeSelection object. This function should not be invoked,
113 * as each #GtkTreeView will create its own #GtkTreeSelection.
115 * Return value: A newly created #GtkTreeSelection object.
118 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
120 GtkTreeSelection *selection;
122 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
124 selection = _gtk_tree_selection_new ();
125 _gtk_tree_selection_set_tree_view (selection, tree_view);
131 * _gtk_tree_selection_set_tree_view:
132 * @selection: A #GtkTreeSelection.
133 * @tree_view: The #GtkTreeView.
135 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
136 * it is used internally by #GtkTreeView.
139 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
140 GtkTreeView *tree_view)
142 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
143 if (tree_view != NULL)
144 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
146 selection->tree_view = tree_view;
150 * gtk_tree_selection_set_mode:
151 * @selection: A #GtkTreeSelection.
152 * @type: The selection mode
154 * Sets the selection mode of the @selection. If the previous type was
155 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
156 * previously selected.
159 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
160 GtkSelectionMode type)
162 GtkTreeSelectionFunc tmp_func;
163 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
165 if (selection->type == type)
169 if (type == GTK_SELECTION_NONE)
171 /* We do this so that we unconditionally unset all rows
173 tmp_func = selection->user_func;
174 selection->user_func = NULL;
175 gtk_tree_selection_unselect_all (selection);
176 selection->user_func = tmp_func;
178 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
179 selection->tree_view->priv->anchor = NULL;
181 else if (type == GTK_SELECTION_SINGLE ||
182 type == GTK_SELECTION_BROWSE)
184 GtkRBTree *tree = NULL;
185 GtkRBNode *node = NULL;
186 gint selected = FALSE;
187 GtkTreePath *anchor_path = NULL;
189 if (selection->tree_view->priv->anchor)
191 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
195 _gtk_tree_view_find_node (selection->tree_view,
200 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
205 /* We do this so that we unconditionally unset all rows
207 tmp_func = selection->user_func;
208 selection->user_func = NULL;
209 gtk_tree_selection_unselect_all (selection);
210 selection->user_func = tmp_func;
212 if (node && selected)
213 _gtk_tree_selection_internal_select_node (selection,
220 gtk_tree_path_free (anchor_path);
223 selection->type = type;
227 * gtk_tree_selection_get_mode:
228 * @selection: a #GtkTreeSelection
230 * Gets the selection mode for @selection. See
231 * gtk_tree_selection_set_mode().
233 * Return value: the current selection mode
236 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
238 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
240 return selection->type;
244 * gtk_tree_selection_set_select_function:
245 * @selection: A #GtkTreeSelection.
246 * @func: The selection function.
247 * @data: The selection function's data.
248 * @destroy: The destroy function for user data. May be NULL.
250 * Sets the selection function. If set, this function is called before any node
251 * is selected or unselected, giving some control over which nodes are selected.
252 * The select function should return %TRUE if the state of the node may be toggled,
253 * and %FALSE if the state of the node should be left unchanged.
256 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
257 GtkTreeSelectionFunc func,
259 GDestroyNotify destroy)
261 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
262 g_return_if_fail (func != NULL);
264 if (selection->destroy)
266 GDestroyNotify d = selection->destroy;
268 selection->destroy = NULL;
269 d (selection->user_data);
272 selection->user_func = func;
273 selection->user_data = data;
274 selection->destroy = destroy;
278 * gtk_tree_selection_get_select_function:
279 * @selection: A #GtkTreeSelection.
281 * Returns the current selection function.
283 * Return value: The function.
288 gtk_tree_selection_get_select_function (GtkTreeSelection *selection)
290 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
292 return selection->user_func;
296 * gtk_tree_selection_get_user_data:
297 * @selection: A #GtkTreeSelection.
299 * Returns the user data for the selection function.
301 * Return value: The user data.
304 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
306 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
308 return selection->user_data;
312 * gtk_tree_selection_get_tree_view:
313 * @selection: A #GtkTreeSelection
315 * Returns the tree view associated with @selection.
317 * Return value: A #GtkTreeView
320 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
322 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
324 return selection->tree_view;
328 * gtk_tree_selection_get_selected:
329 * @selection: A #GtkTreeSelection.
330 * @model: (out) (allow-none): A pointer to set to the #GtkTreeModel, or NULL.
331 * @iter: (allow-none): The #GtkTreeIter, or NULL.
333 * Sets @iter to the currently selected node if @selection is set to
334 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
335 * just want to test if @selection has any selected nodes. @model is filled
336 * with the current model as a convenience. This function will not work if you
337 * use @selection is #GTK_SELECTION_MULTIPLE.
339 * Return value: TRUE, if there is a selected node.
342 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
343 GtkTreeModel **model,
348 GtkTreePath *anchor_path;
352 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
353 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
354 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
358 memset (iter, 0, sizeof (GtkTreeIter));
361 *model = selection->tree_view->priv->model;
363 if (selection->tree_view->priv->anchor == NULL)
366 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
368 if (anchor_path == NULL)
373 found_node = !_gtk_tree_view_find_node (selection->tree_view,
378 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
380 /* we only want to return the anchor if it exists in the rbtree and
386 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
392 /* We don't want to return the anchor if it isn't actually selected.
397 gtk_tree_path_free (anchor_path);
403 * gtk_tree_selection_get_selected_rows:
404 * @selection: A #GtkTreeSelection.
405 * @model: (allow-none): A pointer to set to the #GtkTreeModel, or NULL.
407 * Creates a list of path of all selected rows. Additionally, if you are
408 * planning on modifying the model after calling this function, you may
409 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
410 * To do this, you can use gtk_tree_row_reference_new().
412 * To free the return value, use:
414 * g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
415 * g_list_free (list);
418 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
423 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
424 GtkTreeModel **model)
427 GtkRBTree *tree = NULL;
428 GtkRBNode *node = NULL;
431 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
432 g_return_val_if_fail (selection->tree_view != NULL, NULL);
435 *model = selection->tree_view->priv->model;
437 if (selection->tree_view->priv->tree == NULL ||
438 selection->tree_view->priv->tree->root == NULL)
441 if (selection->type == GTK_SELECTION_NONE)
443 else if (selection->type != GTK_SELECTION_MULTIPLE)
447 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
451 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
452 list = g_list_append (list, path);
460 tree = selection->tree_view->priv->tree;
461 node = selection->tree_view->priv->tree->root;
463 while (node->left != tree->nil)
465 path = gtk_tree_path_new_first ();
469 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
470 list = g_list_prepend (list, gtk_tree_path_copy (path));
474 tree = node->children;
477 while (node->left != tree->nil)
480 gtk_tree_path_append_index (path, 0);
484 gboolean done = FALSE;
488 node = _gtk_rbtree_next (tree, node);
492 gtk_tree_path_next (path);
496 node = tree->parent_node;
497 tree = tree->parent_tree;
501 gtk_tree_path_free (path);
506 gtk_tree_path_up (path);
514 gtk_tree_path_free (path);
517 return g_list_reverse (list);
521 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
525 gint *count = (gint *)data;
527 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
531 _gtk_rbtree_traverse (node->children, node->children->root,
533 gtk_tree_selection_count_selected_rows_helper, data);
537 * gtk_tree_selection_count_selected_rows:
538 * @selection: A #GtkTreeSelection.
540 * Returns the number of rows that have been selected in @tree.
542 * Return value: The number of rows selected.
547 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
551 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
552 g_return_val_if_fail (selection->tree_view != NULL, 0);
554 if (selection->tree_view->priv->tree == NULL ||
555 selection->tree_view->priv->tree->root == NULL)
558 if (selection->type == GTK_SELECTION_SINGLE ||
559 selection->type == GTK_SELECTION_BROWSE)
561 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
567 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
568 selection->tree_view->priv->tree->root,
570 gtk_tree_selection_count_selected_rows_helper,
576 /* gtk_tree_selection_selected_foreach helper */
578 model_changed (gpointer data)
580 gboolean *stop = (gboolean *)data;
586 * gtk_tree_selection_selected_foreach:
587 * @selection: A #GtkTreeSelection.
588 * @func: The function to call for each selected node.
589 * @data: user data to pass to the function.
591 * Calls a function for each selected node. Note that you cannot modify
592 * the tree or selection from within this function. As a result,
593 * gtk_tree_selection_get_selected_rows() might be more useful.
596 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
597 GtkTreeSelectionForeachFunc func,
606 gulong inserted_id, deleted_id, reordered_id, changed_id;
607 gboolean stop = FALSE;
609 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
610 g_return_if_fail (selection->tree_view != NULL);
613 selection->tree_view->priv->tree == NULL ||
614 selection->tree_view->priv->tree->root == NULL)
617 if (selection->type == GTK_SELECTION_SINGLE ||
618 selection->type == GTK_SELECTION_BROWSE)
620 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
622 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
623 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
624 (* func) (selection->tree_view->priv->model, path, &iter, data);
625 gtk_tree_path_free (path);
630 tree = selection->tree_view->priv->tree;
631 node = selection->tree_view->priv->tree->root;
633 while (node->left != tree->nil)
636 model = selection->tree_view->priv->model;
637 g_object_ref (model);
639 /* connect to signals to monitor changes in treemodel */
640 inserted_id = g_signal_connect_swapped (model, "row-inserted",
641 G_CALLBACK (model_changed),
643 deleted_id = g_signal_connect_swapped (model, "row-deleted",
644 G_CALLBACK (model_changed),
646 reordered_id = g_signal_connect_swapped (model, "rows-reordered",
647 G_CALLBACK (model_changed),
649 changed_id = g_signal_connect_swapped (selection->tree_view, "notify::model",
650 G_CALLBACK (model_changed),
653 /* find the node internally */
654 path = gtk_tree_path_new_first ();
658 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
660 gtk_tree_model_get_iter (model, &iter, path);
661 (* func) (model, path, &iter, data);
669 tree = node->children;
672 while (node->left != tree->nil)
675 gtk_tree_path_append_index (path, 0);
679 gboolean done = FALSE;
683 node = _gtk_rbtree_next (tree, node);
687 gtk_tree_path_next (path);
691 node = tree->parent_node;
692 tree = tree->parent_tree;
696 /* we've run out of tree */
697 /* We're done with this function */
702 gtk_tree_path_up (path);
712 gtk_tree_path_free (path);
714 g_signal_handler_disconnect (model, inserted_id);
715 g_signal_handler_disconnect (model, deleted_id);
716 g_signal_handler_disconnect (model, reordered_id);
717 g_signal_handler_disconnect (selection->tree_view, changed_id);
718 g_object_unref (model);
720 /* check if we have to spew a scary message */
722 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
723 "This function is for observing the selections of the tree only. If\n"
724 "you are trying to get all selected items from the tree, try using\n"
725 "gtk_tree_selection_get_selected_rows instead.\n");
729 * gtk_tree_selection_select_path:
730 * @selection: A #GtkTreeSelection.
731 * @path: The #GtkTreePath to be selected.
733 * Select the row at @path.
736 gtk_tree_selection_select_path (GtkTreeSelection *selection,
742 GtkTreeSelectMode mode = 0;
744 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
745 g_return_if_fail (selection->tree_view != NULL);
746 g_return_if_fail (path != NULL);
748 ret = _gtk_tree_view_find_node (selection->tree_view,
753 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
757 if (selection->type == GTK_SELECTION_MULTIPLE)
758 mode = GTK_TREE_SELECT_MODE_TOGGLE;
760 _gtk_tree_selection_internal_select_node (selection,
769 * gtk_tree_selection_unselect_path:
770 * @selection: A #GtkTreeSelection.
771 * @path: The #GtkTreePath to be unselected.
773 * Unselects the row at @path.
776 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
783 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
784 g_return_if_fail (selection->tree_view != NULL);
785 g_return_if_fail (path != NULL);
787 ret = _gtk_tree_view_find_node (selection->tree_view,
792 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
796 _gtk_tree_selection_internal_select_node (selection,
800 GTK_TREE_SELECT_MODE_TOGGLE,
805 * gtk_tree_selection_select_iter:
806 * @selection: A #GtkTreeSelection.
807 * @iter: The #GtkTreeIter to be selected.
809 * Selects the specified iterator.
812 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
817 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
818 g_return_if_fail (selection->tree_view != NULL);
819 g_return_if_fail (selection->tree_view->priv->model != NULL);
820 g_return_if_fail (iter != NULL);
822 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
828 gtk_tree_selection_select_path (selection, path);
829 gtk_tree_path_free (path);
834 * gtk_tree_selection_unselect_iter:
835 * @selection: A #GtkTreeSelection.
836 * @iter: The #GtkTreeIter to be unselected.
838 * Unselects the specified iterator.
841 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
846 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
847 g_return_if_fail (selection->tree_view != NULL);
848 g_return_if_fail (selection->tree_view->priv->model != NULL);
849 g_return_if_fail (iter != NULL);
851 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
857 gtk_tree_selection_unselect_path (selection, path);
858 gtk_tree_path_free (path);
862 * gtk_tree_selection_path_is_selected:
863 * @selection: A #GtkTreeSelection.
864 * @path: A #GtkTreePath to check selection on.
866 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
867 * does not point to a valid location, %FALSE is returned
869 * Return value: %TRUE if @path is selected.
872 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
879 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
880 g_return_val_if_fail (path != NULL, FALSE);
881 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
883 if (selection->tree_view->priv->model == NULL)
886 ret = _gtk_tree_view_find_node (selection->tree_view,
891 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
899 * gtk_tree_selection_iter_is_selected:
900 * @selection: A #GtkTreeSelection
901 * @iter: A valid #GtkTreeIter
903 * Returns %TRUE if the row at @iter is currently selected.
905 * Return value: %TRUE, if @iter is selected
908 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
914 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
915 g_return_val_if_fail (iter != NULL, FALSE);
916 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
917 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
919 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
923 retval = gtk_tree_selection_path_is_selected (selection, path);
924 gtk_tree_path_free (path);
930 /* Wish I was in python, right now... */
932 GtkTreeSelection *selection;
937 select_all_helper (GtkRBTree *tree,
941 struct _TempTuple *tuple = data;
944 _gtk_rbtree_traverse (node->children,
945 node->children->root,
949 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
951 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
956 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
957 * can use it in other places without fear of the signal being emitted.
960 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
962 struct _TempTuple *tuple;
964 if (selection->tree_view->priv->tree == NULL)
967 /* Mark all nodes selected */
968 tuple = g_new (struct _TempTuple, 1);
969 tuple->selection = selection;
970 tuple->dirty = FALSE;
972 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
973 selection->tree_view->priv->tree->root,
987 * gtk_tree_selection_select_all:
988 * @selection: A #GtkTreeSelection.
990 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
994 gtk_tree_selection_select_all (GtkTreeSelection *selection)
996 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
997 g_return_if_fail (selection->tree_view != NULL);
999 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1002 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1004 if (gtk_tree_selection_real_select_all (selection))
1005 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1009 unselect_all_helper (GtkRBTree *tree,
1013 struct _TempTuple *tuple = data;
1016 _gtk_rbtree_traverse (node->children,
1017 node->children->root,
1019 unselect_all_helper,
1021 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1023 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1028 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1030 struct _TempTuple *tuple;
1032 if (selection->type == GTK_SELECTION_SINGLE ||
1033 selection->type == GTK_SELECTION_BROWSE)
1035 GtkRBTree *tree = NULL;
1036 GtkRBNode *node = NULL;
1037 GtkTreePath *anchor_path;
1039 if (selection->tree_view->priv->anchor == NULL)
1042 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1044 if (anchor_path == NULL)
1047 _gtk_tree_view_find_node (selection->tree_view,
1052 gtk_tree_path_free (anchor_path);
1057 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1059 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1061 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1062 selection->tree_view->priv->anchor = NULL;
1070 tuple = g_new (struct _TempTuple, 1);
1071 tuple->selection = selection;
1072 tuple->dirty = FALSE;
1074 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1075 selection->tree_view->priv->tree->root,
1077 unselect_all_helper,
1091 * gtk_tree_selection_unselect_all:
1092 * @selection: A #GtkTreeSelection.
1094 * Unselects all the nodes.
1097 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1099 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1100 g_return_if_fail (selection->tree_view != NULL);
1102 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1105 if (gtk_tree_selection_real_unselect_all (selection))
1106 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1116 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1118 GtkTreePath *start_path,
1119 GtkTreePath *end_path)
1121 GtkRBNode *start_node, *end_node;
1122 GtkRBTree *start_tree, *end_tree;
1123 GtkTreePath *anchor_path = NULL;
1124 gboolean dirty = FALSE;
1126 switch (gtk_tree_path_compare (start_path, end_path))
1129 _gtk_tree_view_find_node (selection->tree_view,
1133 _gtk_tree_view_find_node (selection->tree_view,
1137 anchor_path = start_path;
1140 _gtk_tree_view_find_node (selection->tree_view,
1144 end_tree = start_tree;
1145 end_node = start_node;
1146 anchor_path = start_path;
1149 _gtk_tree_view_find_node (selection->tree_view,
1153 _gtk_tree_view_find_node (selection->tree_view,
1157 anchor_path = start_path;
1161 g_return_val_if_fail (start_node != NULL, FALSE);
1162 g_return_val_if_fail (end_node != NULL, FALSE);
1166 if (selection->tree_view->priv->anchor)
1167 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1169 selection->tree_view->priv->anchor =
1170 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1171 selection->tree_view->priv->model,
1177 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1179 if (start_node == end_node)
1182 if (start_node->children)
1184 start_tree = start_node->children;
1185 start_node = start_tree->root;
1186 while (start_node->left != start_tree->nil)
1187 start_node = start_node->left;
1191 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1192 if (start_tree == NULL)
1194 /* we just ran out of tree. That means someone passed in bogus values.
1206 * gtk_tree_selection_select_range:
1207 * @selection: A #GtkTreeSelection.
1208 * @start_path: The initial node of the range.
1209 * @end_path: The final node of the range.
1211 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1212 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1215 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1216 GtkTreePath *start_path,
1217 GtkTreePath *end_path)
1219 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1220 g_return_if_fail (selection->tree_view != NULL);
1221 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1222 g_return_if_fail (selection->tree_view->priv->model != NULL);
1224 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1225 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1229 * gtk_tree_selection_unselect_range:
1230 * @selection: A #GtkTreeSelection.
1231 * @start_path: The initial node of the range.
1232 * @end_path: The initial node of the range.
1234 * Unselects a range of nodes, determined by @start_path and @end_path
1240 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1241 GtkTreePath *start_path,
1242 GtkTreePath *end_path)
1244 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1245 g_return_if_fail (selection->tree_view != NULL);
1246 g_return_if_fail (selection->tree_view->priv->model != NULL);
1248 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1249 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1253 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1258 gboolean sensitive = FALSE;
1260 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1263 if (!sensitive && selection->tree_view->priv->row_separator_func)
1265 /* never allow separators to be selected */
1266 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1268 selection->tree_view->priv->row_separator_data))
1272 if (selection->user_func)
1273 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1274 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1275 selection->user_data);
1281 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1285 * docs about the 'override_browse_mode', we set this flag when we want to
1286 * unset select the node and override the select browse mode behaviour (that is
1287 * 'one node should *always* be selected').
1290 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1294 GtkTreeSelectMode mode,
1295 gboolean override_browse_mode)
1299 GtkTreePath *anchor_path = NULL;
1301 if (selection->type == GTK_SELECTION_NONE)
1304 if (selection->tree_view->priv->anchor)
1305 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1307 if (selection->type == GTK_SELECTION_SINGLE ||
1308 selection->type == GTK_SELECTION_BROWSE)
1311 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1313 dirty = gtk_tree_selection_real_unselect_all (selection);
1315 /* Did we try to select the same node again? */
1316 else if (selection->type == GTK_SELECTION_SINGLE &&
1317 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1319 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1321 dirty = gtk_tree_selection_real_unselect_all (selection);
1328 /* We only want to select the new node if we can unselect the old one,
1329 * and we can select the new one. */
1330 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1332 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1333 * unselect the new one
1336 dirty = gtk_tree_selection_real_unselect_all (selection);
1338 /* if dirty is TRUE at this point, we successfully unselected the
1339 * old one, and can then select the new one */
1342 if (selection->tree_view->priv->anchor)
1344 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1345 selection->tree_view->priv->anchor = NULL;
1348 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1350 selection->tree_view->priv->anchor =
1351 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1357 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1360 if (selection->tree_view->priv->anchor)
1361 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1363 selection->tree_view->priv->anchor =
1364 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1369 else if (selection->type == GTK_SELECTION_MULTIPLE)
1371 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1372 && (anchor_path == NULL))
1374 if (selection->tree_view->priv->anchor)
1375 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1377 selection->tree_view->priv->anchor =
1378 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1379 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1381 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1383 gtk_tree_selection_select_range (selection,
1387 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1389 flags = node->flags;
1390 if (selection->tree_view->priv->anchor)
1391 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1393 selection->tree_view->priv->anchor =
1394 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1396 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1397 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1399 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1401 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1403 dirty = gtk_tree_selection_real_unselect_all (selection);
1404 dirty |= gtk_tree_selection_real_modify_range (selection,
1411 dirty = gtk_tree_selection_real_unselect_all (selection);
1413 if (selection->tree_view->priv->anchor)
1414 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1416 selection->tree_view->priv->anchor =
1417 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1419 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1424 gtk_tree_path_free (anchor_path);
1427 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1432 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1434 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1437 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1441 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1446 gboolean toggle = FALSE;
1447 GtkTreePath *path = NULL;
1451 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1453 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1454 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1455 gtk_tree_path_free (path);
1460 node->flags ^= GTK_RBNODE_IS_SELECTED;
1462 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);
1470 #define __GTK_TREE_SELECTION_C__
1471 #include "gtkaliasdef.c"