2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtktreeselection.h"
23 #include "gtktreeprivate.h"
24 #include "gtkrbtree.h"
25 #include "gtkmarshalers.h"
29 static void gtk_tree_selection_init (GtkTreeSelection *selection);
30 static void gtk_tree_selection_class_init (GtkTreeSelectionClass *class);
32 static void gtk_tree_selection_finalize (GObject *object);
33 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
34 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
35 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
46 static GObjectClass *parent_class = NULL;
47 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
50 gtk_tree_selection_get_type (void)
52 static GType selection_type = 0;
56 static const GTypeInfo selection_info =
58 sizeof (GtkTreeSelectionClass),
60 NULL, /* base_finalize */
61 (GClassInitFunc) gtk_tree_selection_class_init,
62 NULL, /* class_finalize */
63 NULL, /* class_data */
64 sizeof (GtkTreeSelection),
66 (GInstanceInitFunc) gtk_tree_selection_init
70 g_type_register_static (G_TYPE_OBJECT, I_("GtkTreeSelection"),
74 return selection_type;
78 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
80 GObjectClass *object_class;
82 object_class = (GObjectClass*) class;
83 parent_class = g_type_class_peek_parent (class);
85 object_class->finalize = gtk_tree_selection_finalize;
86 class->changed = NULL;
88 tree_selection_signals[CHANGED] =
89 g_signal_new (I_("changed"),
90 G_OBJECT_CLASS_TYPE (object_class),
92 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
94 _gtk_marshal_VOID__VOID,
99 gtk_tree_selection_init (GtkTreeSelection *selection)
101 selection->type = GTK_SELECTION_SINGLE;
105 gtk_tree_selection_finalize (GObject *object)
107 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
109 if (selection->destroy)
111 GtkDestroyNotify d = selection->destroy;
113 selection->destroy = NULL;
114 d (selection->user_data);
117 /* chain parent_class' handler */
118 G_OBJECT_CLASS (parent_class)->finalize (object);
122 * _gtk_tree_selection_new:
124 * Creates a new #GtkTreeSelection object. This function should not be invoked,
125 * as each #GtkTreeView will create its own #GtkTreeSelection.
127 * Return value: A newly created #GtkTreeSelection object.
130 _gtk_tree_selection_new (void)
132 GtkTreeSelection *selection;
134 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
140 * _gtk_tree_selection_new_with_tree_view:
141 * @tree_view: The #GtkTreeView.
143 * Creates a new #GtkTreeSelection object. This function should not be invoked,
144 * as each #GtkTreeView will create its own #GtkTreeSelection.
146 * Return value: A newly created #GtkTreeSelection object.
149 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
151 GtkTreeSelection *selection;
153 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
155 selection = _gtk_tree_selection_new ();
156 _gtk_tree_selection_set_tree_view (selection, tree_view);
162 * _gtk_tree_selection_set_tree_view:
163 * @selection: A #GtkTreeSelection.
164 * @tree_view: The #GtkTreeView.
166 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
167 * it is used internally by #GtkTreeView.
170 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
171 GtkTreeView *tree_view)
173 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
174 if (tree_view != NULL)
175 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
177 selection->tree_view = tree_view;
181 * gtk_tree_selection_set_mode:
182 * @selection: A #GtkTreeSelection.
183 * @type: The selection mode
185 * Sets the selection mode of the @selection. If the previous type was
186 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
187 * previously selected.
190 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
191 GtkSelectionMode type)
193 GtkTreeSelectionFunc tmp_func;
194 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
196 if (selection->type == type)
200 if (type == GTK_SELECTION_NONE)
202 /* We do this so that we unconditionally unset all rows
204 tmp_func = selection->user_func;
205 selection->user_func = NULL;
206 gtk_tree_selection_unselect_all (selection);
207 selection->user_func = tmp_func;
209 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
210 selection->tree_view->priv->anchor = NULL;
212 else if (type == GTK_SELECTION_SINGLE ||
213 type == GTK_SELECTION_BROWSE)
215 GtkRBTree *tree = NULL;
216 GtkRBNode *node = NULL;
217 gint selected = FALSE;
218 GtkTreePath *anchor_path = NULL;
220 if (selection->tree_view->priv->anchor)
222 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
226 _gtk_tree_view_find_node (selection->tree_view,
231 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
236 /* We do this so that we unconditionally unset all rows
238 tmp_func = selection->user_func;
239 selection->user_func = NULL;
240 gtk_tree_selection_unselect_all (selection);
241 selection->user_func = tmp_func;
243 if (node && selected)
244 _gtk_tree_selection_internal_select_node (selection,
251 gtk_tree_path_free (anchor_path);
254 selection->type = type;
258 * gtk_tree_selection_get_mode:
259 * @selection: a #GtkTreeSelection
261 * Gets the selection mode for @selection. See
262 * gtk_tree_selection_set_mode().
264 * Return value: the current selection mode
267 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
269 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
271 return selection->type;
275 * gtk_tree_selection_set_select_function:
276 * @selection: A #GtkTreeSelection.
277 * @func: The selection function.
278 * @data: The selection function's data.
279 * @destroy: The destroy function for user data. May be NULL.
281 * Sets the selection function. If set, this function is called before any node
282 * is selected or unselected, giving some control over which nodes are selected.
283 * The select function should return %TRUE if the state of the node may be toggled,
284 * and %FALSE if the state of the node should be left unchanged.
287 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
288 GtkTreeSelectionFunc func,
290 GtkDestroyNotify destroy)
292 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
293 g_return_if_fail (func != NULL);
295 if (selection->destroy)
297 GtkDestroyNotify d = selection->destroy;
299 selection->destroy = NULL;
300 d (selection->user_data);
303 selection->user_func = func;
304 selection->user_data = data;
305 selection->destroy = destroy;
309 * gtk_tree_selection_get_user_data:
310 * @selection: A #GtkTreeSelection.
312 * Returns the user data for the selection function.
314 * Return value: The user data.
317 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
319 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
321 return selection->user_data;
325 * gtk_tree_selection_get_tree_view:
326 * @selection: A #GtkTreeSelection
328 * Returns the tree view associated with @selection.
330 * Return value: A #GtkTreeView
333 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
335 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
337 return selection->tree_view;
341 * gtk_tree_selection_get_selected:
342 * @selection: A #GtkTreeSelection.
343 * @model: A pointer to set to the #GtkTreeModel, or NULL.
344 * @iter: The #GtkTreeIter, or NULL.
346 * Sets @iter to the currently selected node if @selection is set to
347 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
348 * just want to test if @selection has any selected nodes. @model is filled
349 * with the current model as a convenience. This function will not work if you
350 * use @selection is #GTK_SELECTION_MULTIPLE.
352 * Return value: TRUE, if there is a selected node.
355 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
356 GtkTreeModel **model,
361 GtkTreePath *anchor_path;
365 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
366 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
367 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
371 memset (iter, 0, sizeof (GtkTreeIter));
374 *model = selection->tree_view->priv->model;
376 if (selection->tree_view->priv->anchor == NULL)
379 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
381 if (anchor_path == NULL)
386 found_node = !_gtk_tree_view_find_node (selection->tree_view,
391 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
393 /* we only want to return the anchor if it exists in the rbtree and
399 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
405 /* We don't want to return the anchor if it isn't actually selected.
410 gtk_tree_path_free (anchor_path);
416 * gtk_tree_selection_get_selected_rows:
417 * @selection: A #GtkTreeSelection.
418 * @model: A pointer to set to the #GtkTreeModel, or NULL.
420 * Creates a list of path of all selected rows. Additionally, if you are
421 * planning on modifying the model after calling this function, you may
422 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
423 * To do this, you can use gtk_tree_row_reference_new().
425 * To free the return value, use:
426 * <informalexample><programlisting>
427 * g_list_foreach (list, gtk_tree_path_free, NULL);
428 * g_list_free (list);
429 * </programlisting></informalexample>
431 * Return value: A #GList containing a #GtkTreePath for each selected row.
436 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
437 GtkTreeModel **model)
440 GtkRBTree *tree = NULL;
441 GtkRBNode *node = NULL;
444 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
445 g_return_val_if_fail (selection->tree_view != NULL, NULL);
447 if (selection->tree_view->priv->tree == NULL ||
448 selection->tree_view->priv->tree->root == NULL)
452 *model = selection->tree_view->priv->model;
454 if (selection->type == GTK_SELECTION_NONE)
456 else if (selection->type != GTK_SELECTION_MULTIPLE)
460 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
464 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
465 list = g_list_append (list, path);
473 tree = selection->tree_view->priv->tree;
474 node = selection->tree_view->priv->tree->root;
476 while (node->left != tree->nil)
478 path = gtk_tree_path_new_first ();
482 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
483 list = g_list_prepend (list, gtk_tree_path_copy (path));
487 tree = node->children;
490 while (node->left != tree->nil)
493 gtk_tree_path_append_index (path, 0);
497 gboolean done = FALSE;
501 node = _gtk_rbtree_next (tree, node);
505 gtk_tree_path_next (path);
509 node = tree->parent_node;
510 tree = tree->parent_tree;
514 gtk_tree_path_free (path);
519 gtk_tree_path_up (path);
527 gtk_tree_path_free (path);
530 return g_list_reverse (list);
534 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
538 gint *count = (gint *)data;
540 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
544 _gtk_rbtree_traverse (node->children, node->children->root,
546 gtk_tree_selection_count_selected_rows_helper, data);
550 * gtk_tree_selection_count_selected_rows:
551 * @selection: A #GtkTreeSelection.
553 * Returns the number of rows that have been selected in @tree.
555 * Return value: The number of rows selected.
560 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
564 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
565 g_return_val_if_fail (selection->tree_view != NULL, 0);
567 if (selection->tree_view->priv->tree == NULL ||
568 selection->tree_view->priv->tree->root == NULL)
571 if (selection->type == GTK_SELECTION_SINGLE ||
572 selection->type == GTK_SELECTION_BROWSE)
574 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
580 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
581 selection->tree_view->priv->tree->root,
583 gtk_tree_selection_count_selected_rows_helper,
589 /* gtk_tree_selection_selected_foreach helper */
591 model_changed (gpointer data)
593 gboolean *stop = (gboolean *)data;
599 * gtk_tree_selection_selected_foreach:
600 * @selection: A #GtkTreeSelection.
601 * @func: The function to call for each selected node.
602 * @data: user data to pass to the function.
604 * Calls a function for each selected node. Note that you cannot modify
605 * the tree or selection from within this function. As a result,
606 * gtk_tree_selection_get_selected_rows() might be more useful.
609 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
610 GtkTreeSelectionForeachFunc func,
619 gulong inserted_id, deleted_id, reordered_id, changed_id;
620 gboolean stop = FALSE;
622 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
623 g_return_if_fail (selection->tree_view != 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 model = selection->tree_view->priv->model;
650 g_object_ref (model);
652 /* connect to signals to monitor changes in treemodel */
653 inserted_id = g_signal_connect_swapped (model, "row_inserted",
654 G_CALLBACK (model_changed),
656 deleted_id = g_signal_connect_swapped (model, "row_deleted",
657 G_CALLBACK (model_changed),
659 reordered_id = g_signal_connect_swapped (model, "rows_reordered",
660 G_CALLBACK (model_changed),
662 changed_id = g_signal_connect_swapped (selection->tree_view, "notify::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 (model, &iter, path);
674 (* func) (model, path, &iter, data);
682 tree = node->children;
685 while (node->left != tree->nil)
688 gtk_tree_path_append_index (path, 0);
692 gboolean done = FALSE;
696 node = _gtk_rbtree_next (tree, node);
700 gtk_tree_path_next (path);
704 node = tree->parent_node;
705 tree = tree->parent_tree;
709 /* we've run out of tree */
710 /* We're done with this function */
715 gtk_tree_path_up (path);
725 gtk_tree_path_free (path);
727 g_signal_handler_disconnect (model, inserted_id);
728 g_signal_handler_disconnect (model, deleted_id);
729 g_signal_handler_disconnect (model, reordered_id);
730 g_signal_handler_disconnect (selection->tree_view, changed_id);
731 g_object_unref (model);
733 /* check if we have to spew a scary message */
735 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
736 "This function is for observing the selections of the tree only. If\n"
737 "you are trying to get all selected items from the tree, try using\n"
738 "gtk_tree_selection_get_selected_rows instead.\n");
742 * gtk_tree_selection_select_path:
743 * @selection: A #GtkTreeSelection.
744 * @path: The #GtkTreePath to be selected.
746 * Select the row at @path.
749 gtk_tree_selection_select_path (GtkTreeSelection *selection,
755 GtkTreeSelectMode mode = 0;
757 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
758 g_return_if_fail (selection->tree_view != NULL);
759 g_return_if_fail (path != NULL);
761 ret = _gtk_tree_view_find_node (selection->tree_view,
766 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
770 if (selection->type == GTK_SELECTION_MULTIPLE)
771 mode = GTK_TREE_SELECT_MODE_TOGGLE;
773 _gtk_tree_selection_internal_select_node (selection,
782 * gtk_tree_selection_unselect_path:
783 * @selection: A #GtkTreeSelection.
784 * @path: The #GtkTreePath to be unselected.
786 * Unselects the row at @path.
789 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
796 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
797 g_return_if_fail (selection->tree_view != NULL);
798 g_return_if_fail (path != NULL);
800 ret = _gtk_tree_view_find_node (selection->tree_view,
805 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
809 _gtk_tree_selection_internal_select_node (selection,
813 GTK_TREE_SELECT_MODE_TOGGLE,
818 * gtk_tree_selection_select_iter:
819 * @selection: A #GtkTreeSelection.
820 * @iter: The #GtkTreeIter to be selected.
822 * Selects the specified iterator.
825 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
830 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
831 g_return_if_fail (selection->tree_view != NULL);
832 g_return_if_fail (selection->tree_view->priv->model != NULL);
833 g_return_if_fail (iter != NULL);
835 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
841 gtk_tree_selection_select_path (selection, path);
842 gtk_tree_path_free (path);
847 * gtk_tree_selection_unselect_iter:
848 * @selection: A #GtkTreeSelection.
849 * @iter: The #GtkTreeIter to be unselected.
851 * Unselects the specified iterator.
854 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
859 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
860 g_return_if_fail (selection->tree_view != NULL);
861 g_return_if_fail (selection->tree_view->priv->model != NULL);
862 g_return_if_fail (iter != NULL);
864 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
870 gtk_tree_selection_unselect_path (selection, path);
871 gtk_tree_path_free (path);
875 * gtk_tree_selection_path_is_selected:
876 * @selection: A #GtkTreeSelection.
877 * @path: A #GtkTreePath to check selection on.
879 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
880 * does not point to a valid location, %FALSE is returned
882 * Return value: %TRUE if @path is selected.
885 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
892 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
893 g_return_val_if_fail (path != NULL, FALSE);
894 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
896 if (selection->tree_view->priv->model == NULL)
899 ret = _gtk_tree_view_find_node (selection->tree_view,
904 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
912 * gtk_tree_selection_iter_is_selected:
913 * @selection: A #GtkTreeSelection
914 * @iter: A valid #GtkTreeIter
916 * Returns %TRUE if the row at @iter is currently selected.
918 * Return value: %TRUE, if @iter is selected
921 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
927 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
928 g_return_val_if_fail (iter != NULL, FALSE);
929 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
930 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
932 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
936 retval = gtk_tree_selection_path_is_selected (selection, path);
937 gtk_tree_path_free (path);
943 /* Wish I was in python, right now... */
945 GtkTreeSelection *selection;
950 select_all_helper (GtkRBTree *tree,
954 struct _TempTuple *tuple = data;
957 _gtk_rbtree_traverse (node->children,
958 node->children->root,
962 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
964 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
969 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
970 * can use it in other places without fear of the signal being emitted.
973 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
975 struct _TempTuple *tuple;
977 if (selection->tree_view->priv->tree == NULL)
980 /* Mark all nodes selected */
981 tuple = g_new (struct _TempTuple, 1);
982 tuple->selection = selection;
983 tuple->dirty = FALSE;
985 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
986 selection->tree_view->priv->tree->root,
1000 * gtk_tree_selection_select_all:
1001 * @selection: A #GtkTreeSelection.
1003 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1007 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1009 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1010 g_return_if_fail (selection->tree_view != NULL);
1012 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1015 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1017 if (gtk_tree_selection_real_select_all (selection))
1018 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1022 unselect_all_helper (GtkRBTree *tree,
1026 struct _TempTuple *tuple = data;
1029 _gtk_rbtree_traverse (node->children,
1030 node->children->root,
1032 unselect_all_helper,
1034 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1036 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1041 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1043 struct _TempTuple *tuple;
1045 if (selection->type == GTK_SELECTION_SINGLE ||
1046 selection->type == GTK_SELECTION_BROWSE)
1048 GtkRBTree *tree = NULL;
1049 GtkRBNode *node = NULL;
1050 GtkTreePath *anchor_path;
1052 if (selection->tree_view->priv->anchor == NULL)
1055 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1057 if (anchor_path == NULL)
1060 _gtk_tree_view_find_node (selection->tree_view,
1065 gtk_tree_path_free (anchor_path);
1070 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1072 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1074 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1075 selection->tree_view->priv->anchor = NULL;
1083 tuple = g_new (struct _TempTuple, 1);
1084 tuple->selection = selection;
1085 tuple->dirty = FALSE;
1087 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1088 selection->tree_view->priv->tree->root,
1090 unselect_all_helper,
1104 * gtk_tree_selection_unselect_all:
1105 * @selection: A #GtkTreeSelection.
1107 * Unselects all the nodes.
1110 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1112 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1113 g_return_if_fail (selection->tree_view != NULL);
1115 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1118 if (gtk_tree_selection_real_unselect_all (selection))
1119 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1129 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1131 GtkTreePath *start_path,
1132 GtkTreePath *end_path)
1134 GtkRBNode *start_node, *end_node;
1135 GtkRBTree *start_tree, *end_tree;
1136 GtkTreePath *anchor_path = NULL;
1137 gboolean dirty = FALSE;
1139 switch (gtk_tree_path_compare (start_path, end_path))
1142 _gtk_tree_view_find_node (selection->tree_view,
1146 _gtk_tree_view_find_node (selection->tree_view,
1150 anchor_path = start_path;
1153 _gtk_tree_view_find_node (selection->tree_view,
1157 end_tree = start_tree;
1158 end_node = start_node;
1159 anchor_path = start_path;
1162 _gtk_tree_view_find_node (selection->tree_view,
1166 _gtk_tree_view_find_node (selection->tree_view,
1170 anchor_path = start_path;
1174 g_return_val_if_fail (start_node != NULL, FALSE);
1175 g_return_val_if_fail (end_node != NULL, FALSE);
1179 if (selection->tree_view->priv->anchor)
1180 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1182 selection->tree_view->priv->anchor =
1183 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1184 selection->tree_view->priv->model,
1190 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1192 if (start_node == end_node)
1195 if (start_node->children)
1197 start_tree = start_node->children;
1198 start_node = start_tree->root;
1199 while (start_node->left != start_tree->nil)
1200 start_node = start_node->left;
1204 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1205 if (start_tree == NULL)
1207 /* we just ran out of tree. That means someone passed in bogus values.
1219 * gtk_tree_selection_select_range:
1220 * @selection: A #GtkTreeSelection.
1221 * @start_path: The initial node of the range.
1222 * @end_path: The final node of the range.
1224 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1225 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1228 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1229 GtkTreePath *start_path,
1230 GtkTreePath *end_path)
1232 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1233 g_return_if_fail (selection->tree_view != NULL);
1234 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1235 g_return_if_fail (selection->tree_view->priv->model != NULL);
1237 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1238 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1242 * gtk_tree_selection_unselect_range:
1243 * @selection: A #GtkTreeSelection.
1244 * @start_path: The initial node of the range.
1245 * @end_path: The initial node of the range.
1247 * Unselects a range of nodes, determined by @start_path and @end_path
1253 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1254 GtkTreePath *start_path,
1255 GtkTreePath *end_path)
1257 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1258 g_return_if_fail (selection->tree_view != NULL);
1259 g_return_if_fail (selection->tree_view->priv->model != NULL);
1261 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1262 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1266 tree_column_is_sensitive (GtkTreeViewColumn *column,
1267 GtkTreeModel *model,
1270 GList *cells, *list;
1274 gtk_tree_view_column_cell_set_cell_data (column, model,
1275 iter, FALSE, FALSE);
1277 cells = gtk_tree_view_column_get_cell_renderers (column);
1282 g_object_get (list->data,
1283 "sensitive", &sensitive,
1284 "visible", &visible,
1287 if (visible && sensitive)
1292 g_list_free (cells);
1298 row_is_selectable (GtkTreeSelection *selection,
1304 gboolean sensitive = FALSE;
1306 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1309 if (!sensitive && selection->tree_view->priv->row_separator_func)
1311 /* never allow separators to be selected */
1312 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1314 selection->tree_view->priv->row_separator_data))
1318 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
1320 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
1322 if (!column->visible)
1325 sensitive = tree_column_is_sensitive (column, selection->tree_view->priv->model, &iter);
1331 if (selection->user_func)
1332 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1333 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1334 selection->user_data);
1340 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1344 * docs about the 'override_browse_mode', we set this flag when we want to
1345 * unset select the node and override the select browse mode behaviour (that is
1346 * 'one node should *always* be selected').
1349 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1353 GtkTreeSelectMode mode,
1354 gboolean override_browse_mode)
1358 GtkTreePath *anchor_path = NULL;
1360 if (selection->type == GTK_SELECTION_NONE)
1363 if (selection->tree_view->priv->anchor)
1364 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1366 if (selection->type == GTK_SELECTION_SINGLE ||
1367 selection->type == GTK_SELECTION_BROWSE)
1370 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1372 dirty = gtk_tree_selection_real_unselect_all (selection);
1374 /* Did we try to select the same node again? */
1375 else if (selection->type == GTK_SELECTION_SINGLE &&
1376 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1378 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1380 dirty = gtk_tree_selection_real_unselect_all (selection);
1387 /* We only want to select the new node if we can unselect the old one,
1388 * and we can select the new one. */
1389 dirty = row_is_selectable (selection, node, path);
1391 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1392 * unselect the new one
1395 dirty = gtk_tree_selection_real_unselect_all (selection);
1397 /* if dirty is TRUE at this point, we successfully unselected the
1398 * old one, and can then select the new one */
1401 if (selection->tree_view->priv->anchor)
1403 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1404 selection->tree_view->priv->anchor = NULL;
1407 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1409 selection->tree_view->priv->anchor =
1410 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1416 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1419 if (selection->tree_view->priv->anchor)
1420 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1422 selection->tree_view->priv->anchor =
1423 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1428 else if (selection->type == GTK_SELECTION_MULTIPLE)
1430 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1431 && (anchor_path == NULL))
1433 if (selection->tree_view->priv->anchor)
1434 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1436 selection->tree_view->priv->anchor =
1437 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1438 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1440 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1442 gtk_tree_selection_select_range (selection,
1446 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1448 flags = node->flags;
1449 if (selection->tree_view->priv->anchor)
1450 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1452 selection->tree_view->priv->anchor =
1453 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1455 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1456 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1458 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1460 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1462 dirty = gtk_tree_selection_real_unselect_all (selection);
1463 dirty |= gtk_tree_selection_real_modify_range (selection,
1470 dirty = gtk_tree_selection_real_unselect_all (selection);
1472 if (selection->tree_view->priv->anchor)
1473 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1475 selection->tree_view->priv->anchor =
1476 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1478 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1483 gtk_tree_path_free (anchor_path);
1486 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1491 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1493 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1496 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1500 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1505 gboolean selected = FALSE;
1506 GtkTreePath *path = NULL;
1510 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1512 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1513 selected = row_is_selectable (selection, node, path);
1514 gtk_tree_path_free (path);
1517 if (selected == TRUE)
1519 node->flags ^= GTK_RBNODE_IS_SELECTED;
1521 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);
1529 #define __GTK_TREE_SELECTION_C__
1530 #include "gtkaliasdef.c"