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.
20 #include "gtktreeselection.h"
21 #include "gtktreeprivate.h"
22 #include "gtkrbtree.h"
23 #include "gtkmarshalers.h"
25 static void gtk_tree_selection_init (GtkTreeSelection *selection);
26 static void gtk_tree_selection_class_init (GtkTreeSelectionClass *class);
28 static void gtk_tree_selection_finalize (GObject *object);
29 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
30 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
31 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
42 static GObjectClass *parent_class = NULL;
43 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
46 gtk_tree_selection_get_type (void)
48 static GType selection_type = 0;
52 static const GTypeInfo selection_info =
54 sizeof (GtkTreeSelectionClass),
56 NULL, /* base_finalize */
57 (GClassInitFunc) gtk_tree_selection_class_init,
58 NULL, /* class_finalize */
59 NULL, /* class_data */
60 sizeof (GtkTreeSelection),
62 (GInstanceInitFunc) gtk_tree_selection_init
66 g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection",
70 return selection_type;
74 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
76 GObjectClass *object_class;
78 object_class = (GObjectClass*) class;
79 parent_class = g_type_class_peek_parent (class);
81 object_class->finalize = gtk_tree_selection_finalize;
82 class->changed = NULL;
84 tree_selection_signals[CHANGED] =
85 g_signal_new ("changed",
86 G_OBJECT_CLASS_TYPE (object_class),
88 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
90 _gtk_marshal_VOID__VOID,
95 gtk_tree_selection_init (GtkTreeSelection *selection)
97 selection->type = GTK_SELECTION_SINGLE;
101 gtk_tree_selection_finalize (GObject *object)
103 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
105 if (selection->destroy)
107 GtkDestroyNotify d = selection->destroy;
109 selection->destroy = NULL;
110 d (selection->user_data);
113 /* chain parent_class' handler */
114 G_OBJECT_CLASS (parent_class)->finalize (object);
118 * _gtk_tree_selection_new:
120 * Creates a new #GtkTreeSelection object. This function should not be invoked,
121 * as each #GtkTreeView will create its own #GtkTreeSelection.
123 * Return value: A newly created #GtkTreeSelection object.
126 _gtk_tree_selection_new (void)
128 GtkTreeSelection *selection;
130 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
136 * _gtk_tree_selection_new_with_tree_view:
137 * @tree_view: The #GtkTreeView.
139 * Creates a new #GtkTreeSelection object. This function should not be invoked,
140 * as each #GtkTreeView will create its own #GtkTreeSelection.
142 * Return value: A newly created #GtkTreeSelection object.
145 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
147 GtkTreeSelection *selection;
149 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
151 selection = _gtk_tree_selection_new ();
152 _gtk_tree_selection_set_tree_view (selection, tree_view);
158 * _gtk_tree_selection_set_tree_view:
159 * @selection: A #GtkTreeSelection.
160 * @tree_view: The #GtkTreeView.
162 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
163 * it is used internally by #GtkTreeView.
166 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
167 GtkTreeView *tree_view)
169 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
170 if (tree_view != NULL)
171 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
173 selection->tree_view = tree_view;
177 * gtk_tree_selection_set_mode:
178 * @selection: A #GtkTreeSelection.
179 * @type: The selection mode
181 * Sets the selection mode of the @selection. If the previous type was
182 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
183 * previously selected.
186 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
187 GtkSelectionMode type)
189 GtkTreeSelectionFunc tmp_func;
190 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
192 if (selection->type == type)
196 if (type == GTK_SELECTION_NONE)
198 /* We do this so that we unconditionally unset all rows
200 tmp_func = selection->user_func;
201 selection->user_func = NULL;
202 gtk_tree_selection_unselect_all (selection);
203 selection->user_func = tmp_func;
205 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
206 selection->tree_view->priv->anchor = NULL;
208 else if (type == GTK_SELECTION_SINGLE ||
209 type == GTK_SELECTION_BROWSE)
211 GtkRBTree *tree = NULL;
212 GtkRBNode *node = NULL;
213 gint selected = FALSE;
214 GtkTreePath *anchor_path = NULL;
216 if (selection->tree_view->priv->anchor)
218 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
222 _gtk_tree_view_find_node (selection->tree_view,
227 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
232 /* We do this so that we unconditionally unset all rows
234 tmp_func = selection->user_func;
235 selection->user_func = NULL;
236 gtk_tree_selection_unselect_all (selection);
237 selection->user_func = tmp_func;
239 if (node && selected)
240 _gtk_tree_selection_internal_select_node (selection,
247 gtk_tree_path_free (anchor_path);
250 selection->type = type;
254 * gtk_tree_selection_get_mode:
255 * @selection: a #GtkTreeSelection
257 * Gets the selection mode for @selection. See
258 * gtk_tree_selection_set_mode().
260 * Return value: the current selection mode
263 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
265 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
267 return selection->type;
271 * gtk_tree_selection_set_select_function:
272 * @selection: A #GtkTreeSelection.
273 * @func: The selection function.
274 * @data: The selection function's data.
275 * @destroy: The destroy function for user data. May be NULL.
277 * Sets the selection function. If set, this function is called before any node
278 * is selected or unselected, giving some control over which nodes are selected.
279 * The select function should return %TRUE if the state of the node may be toggled,
280 * and %FALSE if the state of the node should be left unchanged.
283 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
284 GtkTreeSelectionFunc func,
286 GtkDestroyNotify destroy)
288 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
289 g_return_if_fail (func != NULL);
291 if (selection->destroy)
293 GtkDestroyNotify d = selection->destroy;
295 selection->destroy = NULL;
296 d (selection->user_data);
299 selection->user_func = func;
300 selection->user_data = data;
301 selection->destroy = destroy;
305 * gtk_tree_selection_get_user_data:
306 * @selection: A #GtkTreeSelection.
308 * Returns the user data for the selection function.
310 * Return value: The user data.
313 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
315 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
317 return selection->user_data;
321 * gtk_tree_selection_get_tree_view:
322 * @selection: A #GtkTreeSelection
324 * Returns the tree view associated with @selection.
326 * Return value: A #GtkTreeView
329 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
331 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
333 return selection->tree_view;
337 * gtk_tree_selection_get_selected:
338 * @selection: A #GtkTreeSelection.
339 * @model: A pointer to set to the #GtkTreeModel, or NULL.
340 * @iter: The #GtkTreeIter, or NULL.
342 * Sets @iter to the currently selected node if @selection is set to
343 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
344 * just want to test if @selection has any selected nodes. @model is filled
345 * with the current model as a convenience. This function will not work if you
346 * use @selection is #GTK_SELECTION_MULTIPLE.
348 * Return value: TRUE, if there is a selected node.
351 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
352 GtkTreeModel **model,
357 GtkTreePath *anchor_path;
361 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
362 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
363 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
364 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
367 *model = selection->tree_view->priv->model;
369 if (selection->tree_view->priv->anchor == NULL)
372 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
374 if (anchor_path == NULL)
379 found_node = !_gtk_tree_view_find_node (selection->tree_view,
384 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
386 /* we only want to return the anchor if it exists in the rbtree and
392 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
398 /* We don't want to return the anchor if it isn't actually selected.
403 gtk_tree_path_free (anchor_path);
409 * gtk_tree_selection_get_selected_rows:
410 * @selection: A #GtkTreeSelection.
411 * @model: A pointer to set to the #GtkTreeModel, or NULL.
413 * Creates a list of path of all selected rows. Additionally, if you are
414 * planning on modifying the model after calling this function, you may
415 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
416 * To do this, you can use gtk_tree_row_reference_new_proxy().
418 * To free the return value, use:
419 * <informalexample><programlisting>
420 * g_list_foreach (list, gtk_tree_path_free, NULL);
421 * g_list_free (list);
422 * </programlisting></informalexample>
424 * Return value: A #GList containing a #GtkTreePath for each selected row.
429 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
430 GtkTreeModel **model)
433 GtkRBTree *tree = NULL;
434 GtkRBNode *node = NULL;
437 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
438 g_return_val_if_fail (selection->tree_view != NULL, NULL);
439 g_return_val_if_fail (selection->tree_view->priv->model != NULL, NULL);
441 if (selection->tree_view->priv->tree == NULL ||
442 selection->tree_view->priv->tree->root == NULL)
446 *model = selection->tree_view->priv->model;
448 if (selection->type == GTK_SELECTION_NONE)
450 else if (selection->type != GTK_SELECTION_MULTIPLE)
454 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
458 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
459 list = g_list_append (list, path);
467 tree = selection->tree_view->priv->tree;
468 node = selection->tree_view->priv->tree->root;
470 while (node->left != tree->nil)
472 path = gtk_tree_path_new_first ();
476 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
477 list = g_list_prepend (list, gtk_tree_path_copy (path));
481 tree = node->children;
484 while (node->left != tree->nil)
487 gtk_tree_path_append_index (path, 0);
491 gboolean done = FALSE;
495 node = _gtk_rbtree_next (tree, node);
499 gtk_tree_path_next (path);
503 node = tree->parent_node;
504 tree = tree->parent_tree;
508 gtk_tree_path_free (path);
513 gtk_tree_path_up (path);
521 gtk_tree_path_free (path);
524 return g_list_reverse (list);
528 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
532 gint *count = (gint *)data;
534 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
538 _gtk_rbtree_traverse (node->children, node->children->root,
540 gtk_tree_selection_count_selected_rows_helper, data);
544 * gtk_tree_selection_count_selected_rows:
545 * @selection: A #GtkTreeSelection.
547 * Returns the number of rows that have been selected in @tree.
549 * Return value: The number of rows selected.
554 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
560 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
561 g_return_val_if_fail (selection->tree_view != NULL, 0);
562 g_return_val_if_fail (selection->tree_view->priv->model != NULL, 0);
564 if (selection->tree_view->priv->tree == NULL ||
565 selection->tree_view->priv->tree->root == NULL)
568 if (selection->type == GTK_SELECTION_SINGLE ||
569 selection->type == GTK_SELECTION_BROWSE)
571 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
577 tree = selection->tree_view->priv->tree;
578 node = selection->tree_view->priv->tree->root;
580 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
581 selection->tree_view->priv->tree->root,
583 gtk_tree_selection_count_selected_rows_helper,
589 /* gtk_tree_selection_selected_foreach helper */
591 model_changed (gpointer data)
593 gboolean *stop = (gboolean *)data;
599 * gtk_tree_selection_selected_foreach:
600 * @selection: A #GtkTreeSelection.
601 * @func: The function to call for each selected node.
602 * @data: user data to pass to the function.
604 * Calls a function for each selected node. Note that you cannot modify
605 * the tree or selection from within this function. As a result,
606 * gtk_tree_selection_get_selected_rows() might be more useful.
609 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
610 GtkTreeSelectionForeachFunc func,
618 guint inserted_id, deleted_id, reordered_id;
619 gboolean stop = FALSE, has_next = TRUE, has_parent = TRUE;
621 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
622 g_return_if_fail (selection->tree_view != NULL);
623 g_return_if_fail (selection->tree_view->priv->model != NULL);
626 selection->tree_view->priv->tree == NULL ||
627 selection->tree_view->priv->tree->root == NULL)
630 if (selection->type == GTK_SELECTION_SINGLE ||
631 selection->type == GTK_SELECTION_BROWSE)
633 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
635 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
636 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
637 (* func) (selection->tree_view->priv->model, path, &iter, data);
638 gtk_tree_path_free (path);
643 tree = selection->tree_view->priv->tree;
644 node = selection->tree_view->priv->tree->root;
646 while (node->left != tree->nil)
649 /* connect to signals to monitor changes in treemodel */
650 inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
652 G_CALLBACK (model_changed),
654 deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
656 G_CALLBACK (model_changed),
658 reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
660 G_CALLBACK (model_changed),
663 /* find the node internally */
664 path = gtk_tree_path_new_first ();
665 gtk_tree_model_get_iter (selection->tree_view->priv->model,
670 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
671 (* func) (selection->tree_view->priv->model, path, &iter, data);
681 tree = node->children;
683 while (node->left != tree->nil)
686 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
687 gtk_tree_path_append_index (path, 0);
689 /* we do the sanity check at the bottom of this function */
695 gboolean done = FALSE;
698 node = _gtk_rbtree_next (tree, node);
703 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
705 gtk_tree_path_next (path);
707 /* we do the sanity check at the bottom of this function */
714 GtkTreeIter tmp_iter = iter;
716 node = tree->parent_node;
717 tree = tree->parent_tree;
720 /* we've run out of tree */
721 /* We're done with this function */
726 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
727 gtk_tree_path_up (path);
729 /* we do the sanity check at the bottom of this function */
741 gtk_tree_path_free (path);
743 g_signal_handler_disconnect (selection->tree_view->priv->model,
745 g_signal_handler_disconnect (selection->tree_view->priv->model,
747 g_signal_handler_disconnect (selection->tree_view->priv->model,
750 /* check if we have to spew a scary message */
752 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
754 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
757 ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
758 "This function is for observing the selections of the tree only. If\n"
759 "you are trying to get all selected items from the tree, try using\n"
760 "gtk_tree_selection_get_selected_rows instead.\n");
764 * gtk_tree_selection_select_path:
765 * @selection: A #GtkTreeSelection.
766 * @path: The #GtkTreePath to be selected.
768 * Select the row at @path.
771 gtk_tree_selection_select_path (GtkTreeSelection *selection,
777 GtkTreeSelectMode mode = 0;
779 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
780 g_return_if_fail (selection->tree_view != NULL);
781 g_return_if_fail (path != NULL);
783 ret = _gtk_tree_view_find_node (selection->tree_view,
788 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
792 if (selection->type == GTK_SELECTION_MULTIPLE)
793 mode = GTK_TREE_SELECT_MODE_TOGGLE;
795 _gtk_tree_selection_internal_select_node (selection,
804 * gtk_tree_selection_unselect_path:
805 * @selection: A #GtkTreeSelection.
806 * @path: The #GtkTreePath to be unselected.
808 * Unselects the row at @path.
811 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
818 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
819 g_return_if_fail (selection->tree_view != NULL);
820 g_return_if_fail (path != NULL);
822 ret = _gtk_tree_view_find_node (selection->tree_view,
827 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
831 _gtk_tree_selection_internal_select_node (selection,
835 GTK_TREE_SELECT_MODE_TOGGLE,
840 * gtk_tree_selection_select_iter:
841 * @selection: A #GtkTreeSelection.
842 * @iter: The #GtkTreeIter to be selected.
844 * Selects the specified iterator.
847 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
852 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
853 g_return_if_fail (selection->tree_view != NULL);
854 g_return_if_fail (selection->tree_view->priv->model != NULL);
855 g_return_if_fail (iter != NULL);
857 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
863 gtk_tree_selection_select_path (selection, path);
864 gtk_tree_path_free (path);
869 * gtk_tree_selection_unselect_iter:
870 * @selection: A #GtkTreeSelection.
871 * @iter: The #GtkTreeIter to be unselected.
873 * Unselects the specified iterator.
876 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
881 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
882 g_return_if_fail (selection->tree_view != NULL);
883 g_return_if_fail (selection->tree_view->priv->model != NULL);
884 g_return_if_fail (iter != NULL);
886 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
892 gtk_tree_selection_unselect_path (selection, path);
893 gtk_tree_path_free (path);
897 * gtk_tree_selection_path_is_selected:
898 * @selection: A #GtkTreeSelection.
899 * @path: A #GtkTreePath to check selection on.
901 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
902 * does not point to a valid location, %FALSE is returned
904 * Return value: %TRUE if @path is selected.
907 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
914 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
915 g_return_val_if_fail (path != 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 ret = _gtk_tree_view_find_node (selection->tree_view,
924 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
932 * gtk_tree_selection_iter_is_selected:
933 * @selection: A #GtkTreeSelection
934 * @iter: A valid #GtkTreeIter
936 * Returns %TRUE if the row at @iter is currently selected.
938 * Return value: %TRUE, if @iter is selected
941 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
947 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
948 g_return_val_if_fail (iter != NULL, FALSE);
949 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
950 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
952 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
956 retval = gtk_tree_selection_path_is_selected (selection, path);
957 gtk_tree_path_free (path);
963 /* Wish I was in python, right now... */
965 GtkTreeSelection *selection;
970 select_all_helper (GtkRBTree *tree,
974 struct _TempTuple *tuple = data;
977 _gtk_rbtree_traverse (node->children,
978 node->children->root,
982 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
984 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
989 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
990 * can use it in other places without fear of the signal being emitted.
993 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
995 struct _TempTuple *tuple;
997 if (selection->tree_view->priv->tree == NULL)
1000 /* Mark all nodes selected */
1001 tuple = g_new (struct _TempTuple, 1);
1002 tuple->selection = selection;
1003 tuple->dirty = FALSE;
1005 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1006 selection->tree_view->priv->tree->root,
1020 * gtk_tree_selection_select_all:
1021 * @selection: A #GtkTreeSelection.
1023 * Selects all the nodes. @selection is must be set to #GTK_SELECTION_MULTIPLE
1027 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1029 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1030 g_return_if_fail (selection->tree_view != NULL);
1031 if (selection->tree_view->priv->tree == NULL)
1033 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1035 if (gtk_tree_selection_real_select_all (selection))
1036 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1040 unselect_all_helper (GtkRBTree *tree,
1044 struct _TempTuple *tuple = data;
1047 _gtk_rbtree_traverse (node->children,
1048 node->children->root,
1050 unselect_all_helper,
1052 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1054 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1059 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1061 struct _TempTuple *tuple;
1063 if (selection->type == GTK_SELECTION_SINGLE ||
1064 selection->type == GTK_SELECTION_BROWSE)
1066 GtkRBTree *tree = NULL;
1067 GtkRBNode *node = NULL;
1068 GtkTreePath *anchor_path;
1070 if (selection->tree_view->priv->anchor == NULL)
1073 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1075 if (anchor_path == NULL)
1078 _gtk_tree_view_find_node (selection->tree_view,
1083 gtk_tree_path_free (anchor_path);
1088 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1090 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1092 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1093 selection->tree_view->priv->anchor = NULL;
1101 tuple = g_new (struct _TempTuple, 1);
1102 tuple->selection = selection;
1103 tuple->dirty = FALSE;
1105 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1106 selection->tree_view->priv->tree->root,
1108 unselect_all_helper,
1122 * gtk_tree_selection_unselect_all:
1123 * @selection: A #GtkTreeSelection.
1125 * Unselects all the nodes.
1128 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1130 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1131 g_return_if_fail (selection->tree_view != NULL);
1132 if (selection->tree_view->priv->tree == NULL)
1135 if (selection->tree_view->priv->tree == NULL)
1138 if (gtk_tree_selection_real_unselect_all (selection))
1139 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1149 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1151 GtkTreePath *start_path,
1152 GtkTreePath *end_path)
1154 GtkRBNode *start_node, *end_node;
1155 GtkRBTree *start_tree, *end_tree;
1156 GtkTreePath *anchor_path = NULL;
1157 gboolean dirty = FALSE;
1159 switch (gtk_tree_path_compare (start_path, end_path))
1162 _gtk_tree_view_find_node (selection->tree_view,
1166 _gtk_tree_view_find_node (selection->tree_view,
1170 anchor_path = start_path;
1173 _gtk_tree_view_find_node (selection->tree_view,
1177 end_tree = start_tree;
1178 end_node = start_node;
1179 anchor_path = start_path;
1182 _gtk_tree_view_find_node (selection->tree_view,
1186 _gtk_tree_view_find_node (selection->tree_view,
1190 anchor_path = start_path;
1194 g_return_val_if_fail (start_node != NULL, FALSE);
1195 g_return_val_if_fail (end_node != NULL, FALSE);
1199 if (selection->tree_view->priv->anchor)
1200 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1202 selection->tree_view->priv->anchor =
1203 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1204 selection->tree_view->priv->model,
1210 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1212 if (start_node == end_node)
1215 if (start_node->children)
1217 start_tree = start_node->children;
1218 start_node = start_tree->root;
1219 while (start_node->left != start_tree->nil)
1220 start_node = start_node->left;
1224 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1225 if (start_tree == NULL)
1227 /* we just ran out of tree. That means someone passed in bogus values.
1239 * gtk_tree_selection_select_range:
1240 * @selection: A #GtkTreeSelection.
1241 * @start_path: The initial node of the range.
1242 * @end_path: The final node of the range.
1244 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1247 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1248 GtkTreePath *start_path,
1249 GtkTreePath *end_path)
1251 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1252 g_return_if_fail (selection->tree_view != NULL);
1254 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1255 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1259 * gtk_tree_selection_unselect_range:
1260 * @selection: A #GtkTreeSelection.
1261 * @start_path: The initial node of the range.
1262 * @end_path: The initial node of the range.
1264 * Unselects a range of nodes, determined by @start_path and @end_path
1270 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1271 GtkTreePath *start_path,
1272 GtkTreePath *end_path)
1274 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1275 g_return_if_fail (selection->tree_view != NULL);
1277 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1278 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
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 if (selection->user_func)
1332 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1333 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1334 selection->user_data))
1342 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1343 * unselect the new one
1346 dirty = gtk_tree_selection_real_unselect_all (selection);
1348 /* if dirty is TRUE at this point, we successfully unselected the
1349 * old one, and can then select the new one */
1352 if (selection->tree_view->priv->anchor)
1354 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1355 selection->tree_view->priv->anchor = NULL;
1358 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1360 selection->tree_view->priv->anchor =
1361 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1367 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1370 if (selection->tree_view->priv->anchor)
1371 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1373 selection->tree_view->priv->anchor =
1374 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1379 else if (selection->type == GTK_SELECTION_MULTIPLE)
1381 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1382 && (anchor_path == NULL))
1384 if (selection->tree_view->priv->anchor)
1385 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1387 selection->tree_view->priv->anchor =
1388 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1389 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1391 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1393 gtk_tree_selection_select_range (selection,
1397 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1399 flags = node->flags;
1400 if (selection->tree_view->priv->anchor)
1401 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1403 selection->tree_view->priv->anchor =
1404 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1406 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1407 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1409 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1411 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1413 dirty = gtk_tree_selection_real_unselect_all (selection);
1414 dirty |= gtk_tree_selection_real_modify_range (selection,
1421 dirty = gtk_tree_selection_real_unselect_all (selection);
1423 if (selection->tree_view->priv->anchor)
1424 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1426 selection->tree_view->priv->anchor =
1427 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1429 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1434 gtk_tree_path_free (anchor_path);
1437 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1440 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1444 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1449 gboolean selected = FALSE;
1450 GtkTreePath *path = NULL;
1454 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1456 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1457 if (selection->user_func)
1459 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
1460 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1461 selection->user_data))
1466 gtk_tree_path_free (path);
1469 if (selected == TRUE)
1471 node->flags ^= GTK_RBNODE_IS_SELECTED;
1473 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);