2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtktreeselection.h"
23 #include "gtktreeprivate.h"
24 #include "gtkrbtree.h"
25 #include "gtkmarshalers.h"
29 static void gtk_tree_selection_finalize (GObject *object);
30 static gint gtk_tree_selection_real_select_all (GtkTreeSelection *selection);
31 static gint gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection);
32 static gint gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
43 static guint tree_selection_signals [LAST_SIGNAL] = { 0 };
45 G_DEFINE_TYPE (GtkTreeSelection, gtk_tree_selection, G_TYPE_OBJECT)
48 gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
50 GObjectClass *object_class;
52 object_class = (GObjectClass*) class;
54 object_class->finalize = gtk_tree_selection_finalize;
55 class->changed = NULL;
57 tree_selection_signals[CHANGED] =
58 g_signal_new (I_("changed"),
59 G_OBJECT_CLASS_TYPE (object_class),
61 G_STRUCT_OFFSET (GtkTreeSelectionClass, changed),
63 _gtk_marshal_VOID__VOID,
68 gtk_tree_selection_init (GtkTreeSelection *selection)
70 selection->type = GTK_SELECTION_SINGLE;
74 gtk_tree_selection_finalize (GObject *object)
76 GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
78 if (selection->destroy)
80 GtkDestroyNotify d = selection->destroy;
82 selection->destroy = NULL;
83 d (selection->user_data);
86 /* chain parent_class' handler */
87 G_OBJECT_CLASS (gtk_tree_selection_parent_class)->finalize (object);
91 * _gtk_tree_selection_new:
93 * Creates a new #GtkTreeSelection object. This function should not be invoked,
94 * as each #GtkTreeView will create its own #GtkTreeSelection.
96 * Return value: A newly created #GtkTreeSelection object.
99 _gtk_tree_selection_new (void)
101 GtkTreeSelection *selection;
103 selection = g_object_new (GTK_TYPE_TREE_SELECTION, NULL);
109 * _gtk_tree_selection_new_with_tree_view:
110 * @tree_view: The #GtkTreeView.
112 * Creates a new #GtkTreeSelection object. This function should not be invoked,
113 * as each #GtkTreeView will create its own #GtkTreeSelection.
115 * Return value: A newly created #GtkTreeSelection object.
118 _gtk_tree_selection_new_with_tree_view (GtkTreeView *tree_view)
120 GtkTreeSelection *selection;
122 g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), NULL);
124 selection = _gtk_tree_selection_new ();
125 _gtk_tree_selection_set_tree_view (selection, tree_view);
131 * _gtk_tree_selection_set_tree_view:
132 * @selection: A #GtkTreeSelection.
133 * @tree_view: The #GtkTreeView.
135 * Sets the #GtkTreeView of @selection. This function should not be invoked, as
136 * it is used internally by #GtkTreeView.
139 _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
140 GtkTreeView *tree_view)
142 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
143 if (tree_view != NULL)
144 g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
146 selection->tree_view = tree_view;
150 * gtk_tree_selection_set_mode:
151 * @selection: A #GtkTreeSelection.
152 * @type: The selection mode
154 * Sets the selection mode of the @selection. If the previous type was
155 * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
156 * previously selected.
159 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
160 GtkSelectionMode type)
162 GtkTreeSelectionFunc tmp_func;
163 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
165 if (selection->type == type)
169 if (type == GTK_SELECTION_NONE)
171 /* We do this so that we unconditionally unset all rows
173 tmp_func = selection->user_func;
174 selection->user_func = NULL;
175 gtk_tree_selection_unselect_all (selection);
176 selection->user_func = tmp_func;
178 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
179 selection->tree_view->priv->anchor = NULL;
181 else if (type == GTK_SELECTION_SINGLE ||
182 type == GTK_SELECTION_BROWSE)
184 GtkRBTree *tree = NULL;
185 GtkRBNode *node = NULL;
186 gint selected = FALSE;
187 GtkTreePath *anchor_path = NULL;
189 if (selection->tree_view->priv->anchor)
191 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
195 _gtk_tree_view_find_node (selection->tree_view,
200 if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
205 /* We do this so that we unconditionally unset all rows
207 tmp_func = selection->user_func;
208 selection->user_func = NULL;
209 gtk_tree_selection_unselect_all (selection);
210 selection->user_func = tmp_func;
212 if (node && selected)
213 _gtk_tree_selection_internal_select_node (selection,
220 gtk_tree_path_free (anchor_path);
223 selection->type = type;
227 * gtk_tree_selection_get_mode:
228 * @selection: a #GtkTreeSelection
230 * Gets the selection mode for @selection. See
231 * gtk_tree_selection_set_mode().
233 * Return value: the current selection mode
236 gtk_tree_selection_get_mode (GtkTreeSelection *selection)
238 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), GTK_SELECTION_SINGLE);
240 return selection->type;
244 * gtk_tree_selection_set_select_function:
245 * @selection: A #GtkTreeSelection.
246 * @func: The selection function.
247 * @data: The selection function's data.
248 * @destroy: The destroy function for user data. May be NULL.
250 * Sets the selection function. If set, this function is called before any node
251 * is selected or unselected, giving some control over which nodes are selected.
252 * The select function should return %TRUE if the state of the node may be toggled,
253 * and %FALSE if the state of the node should be left unchanged.
256 gtk_tree_selection_set_select_function (GtkTreeSelection *selection,
257 GtkTreeSelectionFunc func,
259 GtkDestroyNotify destroy)
261 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
262 g_return_if_fail (func != NULL);
264 if (selection->destroy)
266 GtkDestroyNotify d = selection->destroy;
268 selection->destroy = NULL;
269 d (selection->user_data);
272 selection->user_func = func;
273 selection->user_data = data;
274 selection->destroy = destroy;
278 * gtk_tree_selection_get_user_data:
279 * @selection: A #GtkTreeSelection.
281 * Returns the user data for the selection function.
283 * Return value: The user data.
286 gtk_tree_selection_get_user_data (GtkTreeSelection *selection)
288 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
290 return selection->user_data;
294 * gtk_tree_selection_get_tree_view:
295 * @selection: A #GtkTreeSelection
297 * Returns the tree view associated with @selection.
299 * Return value: A #GtkTreeView
302 gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
304 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
306 return selection->tree_view;
310 * gtk_tree_selection_get_selected:
311 * @selection: A #GtkTreeSelection.
312 * @model: A pointer to set to the #GtkTreeModel, or NULL.
313 * @iter: The #GtkTreeIter, or NULL.
315 * Sets @iter to the currently selected node if @selection is set to
316 * #GTK_SELECTION_SINGLE or #GTK_SELECTION_BROWSE. @iter may be NULL if you
317 * just want to test if @selection has any selected nodes. @model is filled
318 * with the current model as a convenience. This function will not work if you
319 * use @selection is #GTK_SELECTION_MULTIPLE.
321 * Return value: TRUE, if there is a selected node.
324 gtk_tree_selection_get_selected (GtkTreeSelection *selection,
325 GtkTreeModel **model,
330 GtkTreePath *anchor_path;
334 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
335 g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
336 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
340 memset (iter, 0, sizeof (GtkTreeIter));
343 *model = selection->tree_view->priv->model;
345 if (selection->tree_view->priv->anchor == NULL)
348 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
350 if (anchor_path == NULL)
355 found_node = !_gtk_tree_view_find_node (selection->tree_view,
360 if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
362 /* we only want to return the anchor if it exists in the rbtree and
368 retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
374 /* We don't want to return the anchor if it isn't actually selected.
379 gtk_tree_path_free (anchor_path);
385 * gtk_tree_selection_get_selected_rows:
386 * @selection: A #GtkTreeSelection.
387 * @model: A pointer to set to the #GtkTreeModel, or NULL.
389 * Creates a list of path of all selected rows. Additionally, if you are
390 * planning on modifying the model after calling this function, you may
391 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
392 * To do this, you can use gtk_tree_row_reference_new().
394 * To free the return value, use:
396 * g_list_foreach (list, gtk_tree_path_free, NULL);
397 * g_list_free (list);
400 * Return value: A #GList containing a #GtkTreePath for each selected row.
405 gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection,
406 GtkTreeModel **model)
409 GtkRBTree *tree = NULL;
410 GtkRBNode *node = NULL;
413 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
414 g_return_val_if_fail (selection->tree_view != NULL, NULL);
417 *model = selection->tree_view->priv->model;
419 if (selection->tree_view->priv->tree == NULL ||
420 selection->tree_view->priv->tree->root == NULL)
423 if (selection->type == GTK_SELECTION_NONE)
425 else if (selection->type != GTK_SELECTION_MULTIPLE)
429 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
433 path = gtk_tree_model_get_path (selection->tree_view->priv->model, &iter);
434 list = g_list_append (list, path);
442 tree = selection->tree_view->priv->tree;
443 node = selection->tree_view->priv->tree->root;
445 while (node->left != tree->nil)
447 path = gtk_tree_path_new_first ();
451 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
452 list = g_list_prepend (list, gtk_tree_path_copy (path));
456 tree = node->children;
459 while (node->left != tree->nil)
462 gtk_tree_path_append_index (path, 0);
466 gboolean done = FALSE;
470 node = _gtk_rbtree_next (tree, node);
474 gtk_tree_path_next (path);
478 node = tree->parent_node;
479 tree = tree->parent_tree;
483 gtk_tree_path_free (path);
488 gtk_tree_path_up (path);
496 gtk_tree_path_free (path);
499 return g_list_reverse (list);
503 gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
507 gint *count = (gint *)data;
509 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
513 _gtk_rbtree_traverse (node->children, node->children->root,
515 gtk_tree_selection_count_selected_rows_helper, data);
519 * gtk_tree_selection_count_selected_rows:
520 * @selection: A #GtkTreeSelection.
522 * Returns the number of rows that have been selected in @tree.
524 * Return value: The number of rows selected.
529 gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
533 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
534 g_return_val_if_fail (selection->tree_view != NULL, 0);
536 if (selection->tree_view->priv->tree == NULL ||
537 selection->tree_view->priv->tree->root == NULL)
540 if (selection->type == GTK_SELECTION_SINGLE ||
541 selection->type == GTK_SELECTION_BROWSE)
543 if (gtk_tree_selection_get_selected (selection, NULL, NULL))
549 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
550 selection->tree_view->priv->tree->root,
552 gtk_tree_selection_count_selected_rows_helper,
558 /* gtk_tree_selection_selected_foreach helper */
560 model_changed (gpointer data)
562 gboolean *stop = (gboolean *)data;
568 * gtk_tree_selection_selected_foreach:
569 * @selection: A #GtkTreeSelection.
570 * @func: The function to call for each selected node.
571 * @data: user data to pass to the function.
573 * Calls a function for each selected node. Note that you cannot modify
574 * the tree or selection from within this function. As a result,
575 * gtk_tree_selection_get_selected_rows() might be more useful.
578 gtk_tree_selection_selected_foreach (GtkTreeSelection *selection,
579 GtkTreeSelectionForeachFunc func,
588 gulong inserted_id, deleted_id, reordered_id, changed_id;
589 gboolean stop = FALSE;
591 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
592 g_return_if_fail (selection->tree_view != NULL);
595 selection->tree_view->priv->tree == NULL ||
596 selection->tree_view->priv->tree->root == NULL)
599 if (selection->type == GTK_SELECTION_SINGLE ||
600 selection->type == GTK_SELECTION_BROWSE)
602 if (gtk_tree_row_reference_valid (selection->tree_view->priv->anchor))
604 path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
605 gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path);
606 (* func) (selection->tree_view->priv->model, path, &iter, data);
607 gtk_tree_path_free (path);
612 tree = selection->tree_view->priv->tree;
613 node = selection->tree_view->priv->tree->root;
615 while (node->left != tree->nil)
618 model = selection->tree_view->priv->model;
619 g_object_ref (model);
621 /* connect to signals to monitor changes in treemodel */
622 inserted_id = g_signal_connect_swapped (model, "row_inserted",
623 G_CALLBACK (model_changed),
625 deleted_id = g_signal_connect_swapped (model, "row_deleted",
626 G_CALLBACK (model_changed),
628 reordered_id = g_signal_connect_swapped (model, "rows_reordered",
629 G_CALLBACK (model_changed),
631 changed_id = g_signal_connect_swapped (selection->tree_view, "notify::model",
632 G_CALLBACK (model_changed),
635 /* find the node internally */
636 path = gtk_tree_path_new_first ();
640 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
642 gtk_tree_model_get_iter (model, &iter, path);
643 (* func) (model, path, &iter, data);
651 tree = node->children;
654 while (node->left != tree->nil)
657 gtk_tree_path_append_index (path, 0);
661 gboolean done = FALSE;
665 node = _gtk_rbtree_next (tree, node);
669 gtk_tree_path_next (path);
673 node = tree->parent_node;
674 tree = tree->parent_tree;
678 /* we've run out of tree */
679 /* We're done with this function */
684 gtk_tree_path_up (path);
694 gtk_tree_path_free (path);
696 g_signal_handler_disconnect (model, inserted_id);
697 g_signal_handler_disconnect (model, deleted_id);
698 g_signal_handler_disconnect (model, reordered_id);
699 g_signal_handler_disconnect (selection->tree_view, changed_id);
700 g_object_unref (model);
702 /* check if we have to spew a scary message */
704 g_warning ("The model has been modified from within gtk_tree_selection_selected_foreach.\n"
705 "This function is for observing the selections of the tree only. If\n"
706 "you are trying to get all selected items from the tree, try using\n"
707 "gtk_tree_selection_get_selected_rows instead.\n");
711 * gtk_tree_selection_select_path:
712 * @selection: A #GtkTreeSelection.
713 * @path: The #GtkTreePath to be selected.
715 * Select the row at @path.
718 gtk_tree_selection_select_path (GtkTreeSelection *selection,
724 GtkTreeSelectMode mode = 0;
726 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
727 g_return_if_fail (selection->tree_view != NULL);
728 g_return_if_fail (path != NULL);
730 ret = _gtk_tree_view_find_node (selection->tree_view,
735 if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
739 if (selection->type == GTK_SELECTION_MULTIPLE)
740 mode = GTK_TREE_SELECT_MODE_TOGGLE;
742 _gtk_tree_selection_internal_select_node (selection,
751 * gtk_tree_selection_unselect_path:
752 * @selection: A #GtkTreeSelection.
753 * @path: The #GtkTreePath to be unselected.
755 * Unselects the row at @path.
758 gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
765 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
766 g_return_if_fail (selection->tree_view != NULL);
767 g_return_if_fail (path != NULL);
769 ret = _gtk_tree_view_find_node (selection->tree_view,
774 if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
778 _gtk_tree_selection_internal_select_node (selection,
782 GTK_TREE_SELECT_MODE_TOGGLE,
787 * gtk_tree_selection_select_iter:
788 * @selection: A #GtkTreeSelection.
789 * @iter: The #GtkTreeIter to be selected.
791 * Selects the specified iterator.
794 gtk_tree_selection_select_iter (GtkTreeSelection *selection,
799 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
800 g_return_if_fail (selection->tree_view != NULL);
801 g_return_if_fail (selection->tree_view->priv->model != NULL);
802 g_return_if_fail (iter != NULL);
804 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
810 gtk_tree_selection_select_path (selection, path);
811 gtk_tree_path_free (path);
816 * gtk_tree_selection_unselect_iter:
817 * @selection: A #GtkTreeSelection.
818 * @iter: The #GtkTreeIter to be unselected.
820 * Unselects the specified iterator.
823 gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
828 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
829 g_return_if_fail (selection->tree_view != NULL);
830 g_return_if_fail (selection->tree_view->priv->model != NULL);
831 g_return_if_fail (iter != NULL);
833 path = gtk_tree_model_get_path (selection->tree_view->priv->model,
839 gtk_tree_selection_unselect_path (selection, path);
840 gtk_tree_path_free (path);
844 * gtk_tree_selection_path_is_selected:
845 * @selection: A #GtkTreeSelection.
846 * @path: A #GtkTreePath to check selection on.
848 * Returns %TRUE if the row pointed to by @path is currently selected. If @path
849 * does not point to a valid location, %FALSE is returned
851 * Return value: %TRUE if @path is selected.
854 gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
861 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
862 g_return_val_if_fail (path != NULL, FALSE);
863 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
865 if (selection->tree_view->priv->model == NULL)
868 ret = _gtk_tree_view_find_node (selection->tree_view,
873 if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
881 * gtk_tree_selection_iter_is_selected:
882 * @selection: A #GtkTreeSelection
883 * @iter: A valid #GtkTreeIter
885 * Returns %TRUE if the row at @iter is currently selected.
887 * Return value: %TRUE, if @iter is selected
890 gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
896 g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
897 g_return_val_if_fail (iter != NULL, FALSE);
898 g_return_val_if_fail (selection->tree_view != NULL, FALSE);
899 g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
901 path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
905 retval = gtk_tree_selection_path_is_selected (selection, path);
906 gtk_tree_path_free (path);
912 /* Wish I was in python, right now... */
914 GtkTreeSelection *selection;
919 select_all_helper (GtkRBTree *tree,
923 struct _TempTuple *tuple = data;
926 _gtk_rbtree_traverse (node->children,
927 node->children->root,
931 if (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
933 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, TRUE) || tuple->dirty;
938 /* We have a real_{un,}select_all function that doesn't emit the signal, so we
939 * can use it in other places without fear of the signal being emitted.
942 gtk_tree_selection_real_select_all (GtkTreeSelection *selection)
944 struct _TempTuple *tuple;
946 if (selection->tree_view->priv->tree == NULL)
949 /* Mark all nodes selected */
950 tuple = g_new (struct _TempTuple, 1);
951 tuple->selection = selection;
952 tuple->dirty = FALSE;
954 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
955 selection->tree_view->priv->tree->root,
969 * gtk_tree_selection_select_all:
970 * @selection: A #GtkTreeSelection.
972 * Selects all the nodes. @selection must be set to #GTK_SELECTION_MULTIPLE
976 gtk_tree_selection_select_all (GtkTreeSelection *selection)
978 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
979 g_return_if_fail (selection->tree_view != NULL);
981 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
984 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
986 if (gtk_tree_selection_real_select_all (selection))
987 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
991 unselect_all_helper (GtkRBTree *tree,
995 struct _TempTuple *tuple = data;
998 _gtk_rbtree_traverse (node->children,
999 node->children->root,
1001 unselect_all_helper,
1003 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1005 tuple->dirty = gtk_tree_selection_real_select_node (tuple->selection, tree, node, FALSE) || tuple->dirty;
1010 gtk_tree_selection_real_unselect_all (GtkTreeSelection *selection)
1012 struct _TempTuple *tuple;
1014 if (selection->type == GTK_SELECTION_SINGLE ||
1015 selection->type == GTK_SELECTION_BROWSE)
1017 GtkRBTree *tree = NULL;
1018 GtkRBNode *node = NULL;
1019 GtkTreePath *anchor_path;
1021 if (selection->tree_view->priv->anchor == NULL)
1024 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1026 if (anchor_path == NULL)
1029 _gtk_tree_view_find_node (selection->tree_view,
1034 gtk_tree_path_free (anchor_path);
1039 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
1041 if (gtk_tree_selection_real_select_node (selection, tree, node, FALSE))
1043 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1044 selection->tree_view->priv->anchor = NULL;
1052 tuple = g_new (struct _TempTuple, 1);
1053 tuple->selection = selection;
1054 tuple->dirty = FALSE;
1056 _gtk_rbtree_traverse (selection->tree_view->priv->tree,
1057 selection->tree_view->priv->tree->root,
1059 unselect_all_helper,
1073 * gtk_tree_selection_unselect_all:
1074 * @selection: A #GtkTreeSelection.
1076 * Unselects all the nodes.
1079 gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
1081 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1082 g_return_if_fail (selection->tree_view != NULL);
1084 if (selection->tree_view->priv->tree == NULL || selection->tree_view->priv->model == NULL)
1087 if (gtk_tree_selection_real_unselect_all (selection))
1088 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1098 gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
1100 GtkTreePath *start_path,
1101 GtkTreePath *end_path)
1103 GtkRBNode *start_node, *end_node;
1104 GtkRBTree *start_tree, *end_tree;
1105 GtkTreePath *anchor_path = NULL;
1106 gboolean dirty = FALSE;
1108 switch (gtk_tree_path_compare (start_path, end_path))
1111 _gtk_tree_view_find_node (selection->tree_view,
1115 _gtk_tree_view_find_node (selection->tree_view,
1119 anchor_path = start_path;
1122 _gtk_tree_view_find_node (selection->tree_view,
1126 end_tree = start_tree;
1127 end_node = start_node;
1128 anchor_path = start_path;
1131 _gtk_tree_view_find_node (selection->tree_view,
1135 _gtk_tree_view_find_node (selection->tree_view,
1139 anchor_path = start_path;
1143 g_return_val_if_fail (start_node != NULL, FALSE);
1144 g_return_val_if_fail (end_node != NULL, FALSE);
1148 if (selection->tree_view->priv->anchor)
1149 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1151 selection->tree_view->priv->anchor =
1152 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view),
1153 selection->tree_view->priv->model,
1159 dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
1161 if (start_node == end_node)
1164 if (start_node->children)
1166 start_tree = start_node->children;
1167 start_node = start_tree->root;
1168 while (start_node->left != start_tree->nil)
1169 start_node = start_node->left;
1173 _gtk_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
1174 if (start_tree == NULL)
1176 /* we just ran out of tree. That means someone passed in bogus values.
1188 * gtk_tree_selection_select_range:
1189 * @selection: A #GtkTreeSelection.
1190 * @start_path: The initial node of the range.
1191 * @end_path: The final node of the range.
1193 * Selects a range of nodes, determined by @start_path and @end_path inclusive.
1194 * @selection must be set to #GTK_SELECTION_MULTIPLE mode.
1197 gtk_tree_selection_select_range (GtkTreeSelection *selection,
1198 GtkTreePath *start_path,
1199 GtkTreePath *end_path)
1201 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1202 g_return_if_fail (selection->tree_view != NULL);
1203 g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
1204 g_return_if_fail (selection->tree_view->priv->model != NULL);
1206 if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
1207 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1211 * gtk_tree_selection_unselect_range:
1212 * @selection: A #GtkTreeSelection.
1213 * @start_path: The initial node of the range.
1214 * @end_path: The initial node of the range.
1216 * Unselects a range of nodes, determined by @start_path and @end_path
1222 gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
1223 GtkTreePath *start_path,
1224 GtkTreePath *end_path)
1226 g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
1227 g_return_if_fail (selection->tree_view != NULL);
1228 g_return_if_fail (selection->tree_view->priv->model != NULL);
1230 if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
1231 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1235 _gtk_tree_selection_row_is_selectable (GtkTreeSelection *selection,
1240 gboolean sensitive = FALSE;
1242 if (!gtk_tree_model_get_iter (selection->tree_view->priv->model, &iter, path))
1245 if (!sensitive && selection->tree_view->priv->row_separator_func)
1247 /* never allow separators to be selected */
1248 if ((* selection->tree_view->priv->row_separator_func) (selection->tree_view->priv->model,
1250 selection->tree_view->priv->row_separator_data))
1254 if (selection->user_func)
1255 return (*selection->user_func) (selection, selection->tree_view->priv->model, path,
1256 GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
1257 selection->user_data);
1263 /* Called internally by gtktreeview.c It handles actually selecting the tree.
1267 * docs about the 'override_browse_mode', we set this flag when we want to
1268 * unset select the node and override the select browse mode behaviour (that is
1269 * 'one node should *always* be selected').
1272 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
1276 GtkTreeSelectMode mode,
1277 gboolean override_browse_mode)
1281 GtkTreePath *anchor_path = NULL;
1283 if (selection->type == GTK_SELECTION_NONE)
1286 if (selection->tree_view->priv->anchor)
1287 anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
1289 if (selection->type == GTK_SELECTION_SINGLE ||
1290 selection->type == GTK_SELECTION_BROWSE)
1293 if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
1295 dirty = gtk_tree_selection_real_unselect_all (selection);
1297 /* Did we try to select the same node again? */
1298 else if (selection->type == GTK_SELECTION_SINGLE &&
1299 anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
1301 if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1303 dirty = gtk_tree_selection_real_unselect_all (selection);
1310 /* We only want to select the new node if we can unselect the old one,
1311 * and we can select the new one. */
1312 dirty = _gtk_tree_selection_row_is_selectable (selection, node, path);
1314 /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
1315 * unselect the new one
1318 dirty = gtk_tree_selection_real_unselect_all (selection);
1320 /* if dirty is TRUE at this point, we successfully unselected the
1321 * old one, and can then select the new one */
1324 if (selection->tree_view->priv->anchor)
1326 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1327 selection->tree_view->priv->anchor = NULL;
1330 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1332 selection->tree_view->priv->anchor =
1333 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1339 if (gtk_tree_selection_real_select_node (selection, tree, node, TRUE))
1342 if (selection->tree_view->priv->anchor)
1343 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1345 selection->tree_view->priv->anchor =
1346 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1351 else if (selection->type == GTK_SELECTION_MULTIPLE)
1353 if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND
1354 && (anchor_path == NULL))
1356 if (selection->tree_view->priv->anchor)
1357 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1359 selection->tree_view->priv->anchor =
1360 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1361 dirty = gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1363 else if ((mode & (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE)) == (GTK_TREE_SELECT_MODE_EXTEND | GTK_TREE_SELECT_MODE_TOGGLE))
1365 gtk_tree_selection_select_range (selection,
1369 else if ((mode & GTK_TREE_SELECT_MODE_TOGGLE) == GTK_TREE_SELECT_MODE_TOGGLE)
1371 flags = node->flags;
1372 if (selection->tree_view->priv->anchor)
1373 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1375 selection->tree_view->priv->anchor =
1376 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1378 if ((flags & GTK_RBNODE_IS_SELECTED) == GTK_RBNODE_IS_SELECTED)
1379 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, FALSE);
1381 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1383 else if ((mode & GTK_TREE_SELECT_MODE_EXTEND) == GTK_TREE_SELECT_MODE_EXTEND)
1385 dirty = gtk_tree_selection_real_unselect_all (selection);
1386 dirty |= gtk_tree_selection_real_modify_range (selection,
1393 dirty = gtk_tree_selection_real_unselect_all (selection);
1395 if (selection->tree_view->priv->anchor)
1396 gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
1398 selection->tree_view->priv->anchor =
1399 gtk_tree_row_reference_new_proxy (G_OBJECT (selection->tree_view), selection->tree_view->priv->model, path);
1401 dirty |= gtk_tree_selection_real_select_node (selection, tree, node, TRUE);
1406 gtk_tree_path_free (anchor_path);
1409 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1414 _gtk_tree_selection_emit_changed (GtkTreeSelection *selection)
1416 g_signal_emit (selection, tree_selection_signals[CHANGED], 0);
1419 /* NOTE: Any {un,}selection ever done _MUST_ be done through this function!
1423 gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
1428 gboolean toggle = FALSE;
1429 GtkTreePath *path = NULL;
1433 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
1435 path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
1436 toggle = _gtk_tree_selection_row_is_selectable (selection, node, path);
1437 gtk_tree_path_free (path);
1442 node->flags ^= GTK_RBNODE_IS_SELECTED;
1444 _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);
1452 #define __GTK_TREE_SELECTION_C__
1453 #include "gtkaliasdef.c"