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.
21 #include "gtktreeselection.h"
22 #include "gtktreeprivate.h"
23 #include "gtkrbtree.h"
24 #include "gtkmarshalers.h"
26 static void gtk_tree_selection_init (GtkTreeSelection *selection);
27 static void gtk_tree_selection_class_init (GtkTreeSelectionClass *class);
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 GObjectClass *parent_class = NULL;
44 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
47 gtk_tree_selection_get_type (void)
49 static GType selection_type = 0;
53 static const GTypeInfo selection_info =
55 sizeof (GtkTreeSelectionClass),
57 NULL, /* base_finalize */
58 (GClassInitFunc) gtk_tree_selection_class_init,
59 NULL, /* class_finalize */
60 NULL, /* class_data */
61 sizeof (GtkTreeSelection),
63 (GInstanceInitFunc) gtk_tree_selection_init
67 g_type_register_static (G_TYPE_OBJECT, "GtkTreeSelection",
71 return selection_type;
75 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
77 GObjectClass *object_class;
79 object_class = (GObjectClass*) class;
80 parent_class = g_type_class_peek_parent (class);
82 object_class->finalize = gtk_tree_selection_finalize;
83 class->changed = NULL;
85 tree_selection_signals[CHANGED] =
86 g_signal_new ("changed",
87 G_OBJECT_CLASS_TYPE (object_class),
89 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
91 _gtk_marshal_VOID__VOID,
96 gtk_tree_selection_init (GtkTreeSelection *selection)
98 selection->type = GTK_SELECTION_SINGLE;
102 gtk_tree_selection_finalize (GObject *object)
104 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
106 if (selection->destroy)
108 GtkDestroyNotify d = selection->destroy;
110 selection->destroy = NULL;
111 d (selection->user_data);
114 /* chain parent_class' handler */
115 G_OBJECT_CLASS (parent_class)->finalize (object);
119 * _gtk_tree_selection_new:
121 * Creates a new #GtkTreeSelection object. This function should not be invoked,
122 * as each #GtkTreeView will create its own #GtkTreeSelection.
124 * Return value: A newly created #GtkTreeSelection object.
127 _gtk_tree_selection_new (void)
129 GtkTreeSelection *selection;
131 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
137 * _gtk_tree_selection_new_with_tree_view:
138 * @tree_view: The #GtkTreeView.
140 * Creates a new #GtkTreeSelection object. This function should not be invoked,
141 * as each #GtkTreeView will create its own #GtkTreeSelection.
143 * Return value: A newly created #GtkTreeSelection object.
146 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
148 GtkTreeSelection *selection;
150 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
152 selection = _gtk_tree_selection_new ();
153 _gtk_tree_selection_set_tree_view (selection, tree_view);
159 * _gtk_tree_selection_set_tree_view:
160 * @selection: A #GtkTreeSelection.
161 * @tree_view: The #GtkTreeView.
163 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
164 * it is used internally by #GtkTreeView.
167 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
168 GtkTreeView *tree_view)
170 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
171 if (tree_view != NULL)
172 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
174 selection->tree_view = tree_view;
178 * gtk_tree_selection_set_mode:
179 * @selection: A #GtkTreeSelection.
180 * @type: The selection mode
182 * Sets the selection mode of the @selection. If the previous type was
183 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
184 * previously selected.
187 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
188 GtkSelectionMode type)
190 GtkTreeSelectionFunc tmp_func;
191 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
193 if (selection->type == type)
197 if (type == GTK_SELECTION_NONE)
199 /* We do this so that we unconditionally unset all rows
201 tmp_func = selection->user_func;
202 selection->user_func = NULL;
203 gtk_tree_selection_unselect_all (selection);
204 selection->user_func = tmp_func;
206 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
207 selection->tree_view->priv->anchor = NULL;
209 else if (type == GTK_SELECTION_SINGLE ||
210 type == GTK_SELECTION_BROWSE)
212 GtkRBTree *tree = NULL;
213 GtkRBNode *node = NULL;
214 gint selected = FALSE;
215 GtkTreePath *anchor_path = NULL;
217 if (selection->tree_view->priv->anchor)
219 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
223 _gtk_tree_view_find_node (selection->tree_view,
228 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
233 /* We do this so that we unconditionally unset all rows
235 tmp_func = selection->user_func;
236 selection->user_func = NULL;
237 gtk_tree_selection_unselect_all (selection);
238 selection->user_func = tmp_func;
240 if (node && selected)
241 _gtk_tree_selection_internal_select_node (selection,
248 gtk_tree_path_free (anchor_path);
251 selection->type = type;
255 * gtk_tree_selection_get_mode:
256 * @selection: a #GtkTreeSelection
258 * Gets the selection mode for @selection. See
259 * gtk_tree_selection_set_mode().
261 * Return value: the current selection mode
264 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
266 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
268 return selection->type;
272 * gtk_tree_selection_set_select_function:
273 * @selection: A #GtkTreeSelection.
274 * @func: The selection function.
275 * @data: The selection function's data.
276 * @destroy: The destroy function for user data. May be NULL.
278 * Sets the selection function. If set, this function is called before any node
279 * is selected or unselected, giving some control over which nodes are selected.
280 * The select function should return %TRUE if the state of the node may be toggled,
281 * and %FALSE if the state of the node should be left unchanged.
284 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
285 GtkTreeSelectionFunc func,
287 GtkDestroyNotify destroy)
289 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
290 g_return_if_fail (func != NULL);
292 if (selection->destroy)
294 GtkDestroyNotify d = selection->destroy;
296 selection->destroy = NULL;
297 d (selection->user_data);
300 selection->user_func = func;
301 selection->user_data = data;
302 selection->destroy = destroy;
306 * gtk_tree_selection_get_user_data:
307 * @selection: A #GtkTreeSelection.
309 * Returns the user data for the selection function.
311 * Return value: The user data.
314 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
316 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
318 return selection->user_data;
322 * gtk_tree_selection_get_tree_view:
323 * @selection: A #GtkTreeSelection
325 * Returns the tree view associated with @selection.
327 * Return value: A #GtkTreeView
330 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
332 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
334 return selection->tree_view;
338 * gtk_tree_selection_get_selected:
339 * @selection: A #GtkTreeSelection.
340 * @model: A pointer to set to the #GtkTreeModel, or NULL.
341 * @iter: The #GtkTreeIter, or NULL.
343 * Sets @iter to the currently selected node if @selection is set to
344 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
345 * just want to test if @selection has any selected nodes. @model is filled
346 * with the current model as a convenience. This function will not work if you
347 * use @selection is #GTK_SELECTION_MULTIPLE.
349 * Return value: TRUE, if there is a selected node.
352 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
353 GtkTreeModel **model,
358 GtkTreePath *anchor_path;
362 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
363 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
364 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
365 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
368 *model = selection->tree_view->priv->model;
370 if (selection->tree_view->priv->anchor == NULL)
373 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
375 if (anchor_path == NULL)
380 found_node = !_gtk_tree_view_find_node (selection->tree_view,
385 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
387 /* we only want to return the anchor if it exists in the rbtree and
393 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
399 /* We don't want to return the anchor if it isn't actually selected.
404 gtk_tree_path_free (anchor_path);
410 * gtk_tree_selection_get_selected_rows:
411 * @selection: A #GtkTreeSelection.
412 * @model: A pointer to set to the #GtkTreeModel, or NULL.
414 * Creates a list of path of all selected rows. Additionally, if you are
415 * planning on modifying the model after calling this function, you may
416 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
417 * To do this, you can use gtk_tree_row_reference_new().
419 * To free the return value, use:
420 * <informalexample><programlisting>
421 * g_list_foreach (list, gtk_tree_path_free, NULL);
422 * g_list_free (list);
423 * </programlisting></informalexample>
425 * Return value: A #GList containing a #GtkTreePath for each selected row.
430 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
431 GtkTreeModel **model)
434 GtkRBTree *tree = NULL;
435 GtkRBNode *node = NULL;
438 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
439 g_return_val_if_fail (selection->tree_view != NULL, NULL);
440 g_return_val_if_fail (selection->tree_view->priv->model != NULL, NULL);
442 if (selection->tree_view->priv->tree == NULL ||
443 selection->tree_view->priv->tree->root == NULL)
447 *model = selection->tree_view->priv->model;
449 if (selection->type == GTK_SELECTION_NONE)
451 else if (selection->type != GTK_SELECTION_MULTIPLE)
455 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
459 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
460 list = g_list_append (list, path);
468 tree = selection->tree_view->priv->tree;
469 node = selection->tree_view->priv->tree->root;
471 while (node->left != tree->nil)
473 path = gtk_tree_path_new_first ();
477 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
478 list = g_list_prepend (list, gtk_tree_path_copy (path));
482 tree = node->children;
485 while (node->left != tree->nil)
488 gtk_tree_path_append_index (path, 0);
492 gboolean done = FALSE;
496 node = _gtk_rbtree_next (tree, node);
500 gtk_tree_path_next (path);
504 node = tree->parent_node;
505 tree = tree->parent_tree;
509 gtk_tree_path_free (path);
514 gtk_tree_path_up (path);
522 gtk_tree_path_free (path);
525 return g_list_reverse (list);
529 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
533 gint *count = (gint *)data;
535 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
539 _gtk_rbtree_traverse (node->children, node->children->root,
541 gtk_tree_selection_count_selected_rows_helper, data);
545 * gtk_tree_selection_count_selected_rows:
546 * @selection: A #GtkTreeSelection.
548 * Returns the number of rows that have been selected in @tree.
550 * Return value: The number of rows selected.
555 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
561 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
562 g_return_val_if_fail (selection->tree_view != NULL, 0);
563 g_return_val_if_fail (selection->tree_view->priv->model != NULL, 0);
565 if (selection->tree_view->priv->tree == NULL ||
566 selection->tree_view->priv->tree->root == NULL)
569 if (selection->type == GTK_SELECTION_SINGLE ||
570 selection->type == GTK_SELECTION_BROWSE)
572 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
578 tree = selection->tree_view->priv->tree;
579 node = selection->tree_view->priv->tree->root;
581 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
582 selection->tree_view->priv->tree->root,
584 gtk_tree_selection_count_selected_rows_helper,
590 /* gtk_tree_selection_selected_foreach helper */
592 model_changed (gpointer data)
594 gboolean *stop = (gboolean *)data;
600 * gtk_tree_selection_selected_foreach:
601 * @selection: A #GtkTreeSelection.
602 * @func: The function to call for each selected node.
603 * @data: user data to pass to the function.
605 * Calls a function for each selected node. Note that you cannot modify
606 * the tree or selection from within this function. As a result,
607 * gtk_tree_selection_get_selected_rows() might be more useful.
610 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
611 GtkTreeSelectionForeachFunc func,
619 guint inserted_id, deleted_id, reordered_id;
620 gboolean stop = FALSE, has_next = TRUE, has_parent = TRUE;
622 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
623 g_return_if_fail (selection->tree_view != NULL);
624 g_return_if_fail (selection->tree_view->priv->model != NULL);
627 selection->tree_view->priv->tree == NULL ||
628 selection->tree_view->priv->tree->root == NULL)
631 if (selection->type == GTK_SELECTION_SINGLE ||
632 selection->type == GTK_SELECTION_BROWSE)
634 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
636 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
637 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
638 (* func) (selection->tree_view->priv->model, path, &iter, data);
639 gtk_tree_path_free (path);
644 tree = selection->tree_view->priv->tree;
645 node = selection->tree_view->priv->tree->root;
647 while (node->left != tree->nil)
650 /* connect to signals to monitor changes in treemodel */
651 inserted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
653 G_CALLBACK (model_changed),
655 deleted_id = g_signal_connect_swapped (selection->tree_view->priv->model,
657 G_CALLBACK (model_changed),
659 reordered_id = g_signal_connect_swapped (selection->tree_view->priv->model,
661 G_CALLBACK (model_changed),
664 /* find the node internally */
665 path = gtk_tree_path_new_first ();
666 gtk_tree_model_get_iter (selection->tree_view->priv->model,
671 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
672 (* func) (selection->tree_view->priv->model, path, &iter, data);
682 tree = node->children;
684 while (node->left != tree->nil)
687 has_child = gtk_tree_model_iter_children (selection->tree_view->priv->model, &iter, &tmp);
688 gtk_tree_path_append_index (path, 0);
690 /* we do the sanity check at the bottom of this function */
696 gboolean done = FALSE;
699 node = _gtk_rbtree_next (tree, node);
704 has_next = gtk_tree_model_iter_next (selection->tree_view->priv->model, &iter);
706 gtk_tree_path_next (path);
708 /* we do the sanity check at the bottom of this function */
715 GtkTreeIter tmp_iter = iter;
717 node = tree->parent_node;
718 tree = tree->parent_tree;
721 /* we've run out of tree */
722 /* We're done with this function */
727 has_parent = gtk_tree_model_iter_parent (selection->tree_view->priv->model, &iter, &tmp_iter);
728 gtk_tree_path_up (path);
730 /* we do the sanity check at the bottom of this function */
742 gtk_tree_path_free (path);
744 g_signal_handler_disconnect (selection->tree_view->priv->model,
746 g_signal_handler_disconnect (selection->tree_view->priv->model,
748 g_signal_handler_disconnect (selection->tree_view->priv->model,
751 /* check if we have to spew a scary message */
753 TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
755 TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
758 ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
759 "This function is for observing the selections of the tree only. If\n"
760 "you are trying to get all selected items from the tree, try using\n"
761 "gtk_tree_selection_get_selected_rows instead.\n");
765 * gtk_tree_selection_select_path:
766 * @selection: A #GtkTreeSelection.
767 * @path: The #GtkTreePath to be selected.
769 * Select the row at @path.
772 gtk_tree_selection_select_path (GtkTreeSelection *selection,
778 GtkTreeSelectMode mode = 0;
780 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
781 g_return_if_fail (selection->tree_view != NULL);
782 g_return_if_fail (path != NULL);
784 ret = _gtk_tree_view_find_node (selection->tree_view,
789 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
793 if (selection->type == GTK_SELECTION_MULTIPLE)
794 mode = GTK_TREE_SELECT_MODE_TOGGLE;
796 _gtk_tree_selection_internal_select_node (selection,
805 * gtk_tree_selection_unselect_path:
806 * @selection: A #GtkTreeSelection.
807 * @path: The #GtkTreePath to be unselected.
809 * Unselects the row at @path.
812 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
819 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
820 g_return_if_fail (selection->tree_view != NULL);
821 g_return_if_fail (path != NULL);
823 ret = _gtk_tree_view_find_node (selection->tree_view,
828 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
832 _gtk_tree_selection_internal_select_node (selection,
836 GTK_TREE_SELECT_MODE_TOGGLE,
841 * gtk_tree_selection_select_iter:
842 * @selection: A #GtkTreeSelection.
843 * @iter: The #GtkTreeIter to be selected.
845 * Selects the specified iterator.
848 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
853 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
854 g_return_if_fail (selection->tree_view != NULL);
855 g_return_if_fail (selection->tree_view->priv->model != NULL);
856 g_return_if_fail (iter != NULL);
858 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
864 gtk_tree_selection_select_path (selection, path);
865 gtk_tree_path_free (path);
870 * gtk_tree_selection_unselect_iter:
871 * @selection: A #GtkTreeSelection.
872 * @iter: The #GtkTreeIter to be unselected.
874 * Unselects the specified iterator.
877 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
882 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
883 g_return_if_fail (selection->tree_view != NULL);
884 g_return_if_fail (selection->tree_view->priv->model != NULL);
885 g_return_if_fail (iter != NULL);
887 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
893 gtk_tree_selection_unselect_path (selection, path);
894 gtk_tree_path_free (path);
898 * gtk_tree_selection_path_is_selected:
899 * @selection: A #GtkTreeSelection.
900 * @path: A #GtkTreePath to check selection on.
902 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
903 * does not point to a valid location, %FALSE is returned
905 * Return value: %TRUE if @path is selected.
908 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
915 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
916 g_return_val_if_fail (path != NULL, FALSE);
917 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
918 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
920 ret = _gtk_tree_view_find_node (selection->tree_view,
925 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
933 * gtk_tree_selection_iter_is_selected:
934 * @selection: A #GtkTreeSelection
935 * @iter: A valid #GtkTreeIter
937 * Returns %TRUE if the row at @iter is currently selected.
939 * Return value: %TRUE, if @iter is selected
942 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
948 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
949 g_return_val_if_fail (iter != NULL, FALSE);
950 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
951 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
953 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
957 retval = gtk_tree_selection_path_is_selected (selection, path);
958 gtk_tree_path_free (path);
964 /* Wish I was in python, right now... */
966 GtkTreeSelection *selection;
971 select_all_helper (GtkRBTree *tree,
975 struct _TempTuple *tuple = data;
978 _gtk_rbtree_traverse (node->children,
979 node->children->root,
983 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
985 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
990 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
991 * can use it in other places without fear of the signal being emitted.
994 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
996 struct _TempTuple *tuple;
998 if (selection->tree_view->priv->tree == NULL)
1001 /* Mark all nodes selected */
1002 tuple = g_new (struct _TempTuple, 1);
1003 tuple->selection = selection;
1004 tuple->dirty = FALSE;
1006 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1007 selection->tree_view->priv->tree->root,
1021 * gtk_tree_selection_select_all:
1022 * @selection: A #GtkTreeSelection.
1024 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
1028 gtk_tree_selection_select_all (GtkTreeSelection *selection)
1030 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1031 g_return_if_fail (selection->tree_view != NULL);
1032 if (selection->tree_view->priv->tree == NULL)
1034 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1036 if (gtk_tree_selection_real_select_all (selection))
1037 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1041 unselect_all_helper (GtkRBTree *tree,
1045 struct _TempTuple *tuple = data;
1048 _gtk_rbtree_traverse (node->children,
1049 node->children->root,
1051 unselect_all_helper,
1053 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1055 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1060 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1062 struct _TempTuple *tuple;
1064 if (selection->type == GTK_SELECTION_SINGLE ||
1065 selection->type == GTK_SELECTION_BROWSE)
1067 GtkRBTree *tree = NULL;
1068 GtkRBNode *node = NULL;
1069 GtkTreePath *anchor_path;
1071 if (selection->tree_view->priv->anchor == NULL)
1074 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1076 if (anchor_path == NULL)
1079 _gtk_tree_view_find_node (selection->tree_view,
1084 gtk_tree_path_free (anchor_path);
1089 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1091 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1093 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1094 selection->tree_view->priv->anchor = NULL;
1102 tuple = g_new (struct _TempTuple, 1);
1103 tuple->selection = selection;
1104 tuple->dirty = FALSE;
1106 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1107 selection->tree_view->priv->tree->root,
1109 unselect_all_helper,
1123 * gtk_tree_selection_unselect_all:
1124 * @selection: A #GtkTreeSelection.
1126 * Unselects all the nodes.
1129 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1131 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1132 g_return_if_fail (selection->tree_view != NULL);
1133 if (selection->tree_view->priv->tree == NULL)
1136 if (selection->tree_view->priv->tree == NULL)
1139 if (gtk_tree_selection_real_unselect_all (selection))
1140 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1150 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1152 GtkTreePath *start_path,
1153 GtkTreePath *end_path)
1155 GtkRBNode *start_node, *end_node;
1156 GtkRBTree *start_tree, *end_tree;
1157 GtkTreePath *anchor_path = NULL;
1158 gboolean dirty = FALSE;
1160 switch (gtk_tree_path_compare (start_path, end_path))
1163 _gtk_tree_view_find_node (selection->tree_view,
1167 _gtk_tree_view_find_node (selection->tree_view,
1171 anchor_path = start_path;
1174 _gtk_tree_view_find_node (selection->tree_view,
1178 end_tree = start_tree;
1179 end_node = start_node;
1180 anchor_path = start_path;
1183 _gtk_tree_view_find_node (selection->tree_view,
1187 _gtk_tree_view_find_node (selection->tree_view,
1191 anchor_path = start_path;
1195 g_return_val_if_fail (start_node != NULL, FALSE);
1196 g_return_val_if_fail (end_node != NULL, FALSE);
1200 if (selection->tree_view->priv->anchor)
1201 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1203 selection->tree_view->priv->anchor =
1204 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1205 selection->tree_view->priv->model,
1211 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1213 if (start_node == end_node)
1216 if (start_node->children)
1218 start_tree = start_node->children;
1219 start_node = start_tree->root;
1220 while (start_node->left != start_tree->nil)
1221 start_node = start_node->left;
1225 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1226 if (start_tree == NULL)
1228 /* we just ran out of tree. That means someone passed in bogus values.
1240 * gtk_tree_selection_select_range:
1241 * @selection: A #GtkTreeSelection.
1242 * @start_path: The initial node of the range.
1243 * @end_path: The final node of the range.
1245 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1246 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1249 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1250 GtkTreePath *start_path,
1251 GtkTreePath *end_path)
1253 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1254 g_return_if_fail (selection->tree_view != NULL);
1255 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1257 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1258 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1262 * gtk_tree_selection_unselect_range:
1263 * @selection: A #GtkTreeSelection.
1264 * @start_path: The initial node of the range.
1265 * @end_path: The initial node of the range.
1267 * Unselects a range of nodes, determined by @start_path and @end_path
1273 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1274 GtkTreePath *start_path,
1275 GtkTreePath *end_path)
1277 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1278 g_return_if_fail (selection->tree_view != NULL);
1280 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1281 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1285 tree_column_is_sensitive (GtkTreeViewColumn *column,
1286 GtkTreeModel *model,
1289 GList *cells, *list;
1293 gtk_tree_view_column_cell_set_cell_data (column, model,
1294 iter, FALSE, FALSE);
1296 cells = gtk_tree_view_column_get_cell_renderers (column);
1301 g_object_get (G_OBJECT (list->data),
1302 "sensitive", &sensitive,
1303 "visible", &visible,
1306 if (visible && sensitive)
1311 g_list_free (cells);
1317 row_is_selectable (GtkTreeSelection *selection,
1323 gboolean sensitive = FALSE;
1325 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1328 if (!sensitive && selection->tree_view->priv->row_separator_func)
1330 /* never allow separators to be selected */
1331 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1333 selection->tree_view->priv->row_separator_data))
1337 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
1339 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
1341 if (!column->visible)
1344 sensitive = tree_column_is_sensitive (column, selection->tree_view->priv->model, &iter);
1350 if (selection->user_func)
1351 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1352 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1353 selection->user_data);
1359 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1363 * docs about the 'override_browse_mode', we set this flag when we want to
1364 * unset select the node and override the select browse mode behaviour (that is
1365 * 'one node should *always* be selected').
1368 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1372 GtkTreeSelectMode mode,
1373 gboolean override_browse_mode)
1377 GtkTreePath *anchor_path = NULL;
1379 if (selection->type == GTK_SELECTION_NONE)
1382 if (selection->tree_view->priv->anchor)
1383 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1385 if (selection->type == GTK_SELECTION_SINGLE ||
1386 selection->type == GTK_SELECTION_BROWSE)
1389 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1391 dirty = gtk_tree_selection_real_unselect_all (selection);
1393 /* Did we try to select the same node again? */
1394 else if (selection->type == GTK_SELECTION_SINGLE &&
1395 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1397 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1399 dirty = gtk_tree_selection_real_unselect_all (selection);
1406 /* We only want to select the new node if we can unselect the old one,
1407 * and we can select the new one. */
1408 dirty = row_is_selectable (selection, node, path);
1410 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1411 * unselect the new one
1414 dirty = gtk_tree_selection_real_unselect_all (selection);
1416 /* if dirty is TRUE at this point, we successfully unselected the
1417 * old one, and can then select the new one */
1420 if (selection->tree_view->priv->anchor)
1422 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1423 selection->tree_view->priv->anchor = NULL;
1426 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1428 selection->tree_view->priv->anchor =
1429 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1435 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1438 if (selection->tree_view->priv->anchor)
1439 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1441 selection->tree_view->priv->anchor =
1442 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1447 else if (selection->type == GTK_SELECTION_MULTIPLE)
1449 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1450 && (anchor_path == NULL))
1452 if (selection->tree_view->priv->anchor)
1453 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1455 selection->tree_view->priv->anchor =
1456 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1457 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1459 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1461 gtk_tree_selection_select_range (selection,
1465 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1467 flags = node->flags;
1468 if (selection->tree_view->priv->anchor)
1469 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1471 selection->tree_view->priv->anchor =
1472 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1474 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1475 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1477 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1479 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1481 dirty = gtk_tree_selection_real_unselect_all (selection);
1482 dirty |= gtk_tree_selection_real_modify_range (selection,
1489 dirty = gtk_tree_selection_real_unselect_all (selection);
1491 if (selection->tree_view->priv->anchor)
1492 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1494 selection->tree_view->priv->anchor =
1495 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1497 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1502 gtk_tree_path_free (anchor_path);
1505 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1508 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1512 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1517 gboolean selected = FALSE;
1518 GtkTreePath *path = NULL;
1522 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1524 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1525 selected = row_is_selectable (selection, node, path);
1526 gtk_tree_path_free (path);
1529 if (selected == TRUE)
1531 node->flags ^= GTK_RBNODE_IS_SELECTED;
1533 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);