]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktreeselection.c
Mark abstract types as G_TYPE_FLAG_ABSTRACT. (#72383)
[~andy/gtk] / gtk / gtktreeselection.c
index 3105c8646998bc1ae7c47876de6a7c773f8efb8a..88df17bcbac6f92491885b6db4e304ee757c548a 100644 (file)
@@ -20,6 +20,7 @@
 #include "gtktreeselection.h"
 #include "gtktreeprivate.h"
 #include "gtkrbtree.h"
+#include "gtkmarshalers.h"
 #include "gtksignal.h"
 
 static void gtk_tree_selection_init              (GtkTreeSelection      *selection);
@@ -84,7 +85,7 @@ gtk_tree_selection_class_init (GtkTreeSelectionClass *class)
                    GTK_RUN_FIRST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTreeSelectionClass, changed),
-                   gtk_marshal_VOID__VOID,
+                   _gtk_marshal_VOID__VOID,
                    GTK_TYPE_NONE, 0);
 }
 
@@ -97,8 +98,15 @@ gtk_tree_selection_init (GtkTreeSelection *selection)
 static void
 gtk_tree_selection_finalize (GObject *object)
 {
-  if (GTK_TREE_SELECTION (object)->destroy)
-    (* GTK_TREE_SELECTION (object)->destroy) (GTK_TREE_SELECTION (object)->user_data);
+  GtkTreeSelection *selection = GTK_TREE_SELECTION (object);
+
+  if (selection->destroy)
+    {
+      GtkDestroyNotify d = selection->destroy;
+
+      selection->destroy = NULL;
+      d (selection->user_data);
+    }
 
   /* chain parent_class' handler */
   G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -169,29 +177,42 @@ _gtk_tree_selection_set_tree_view (GtkTreeSelection *selection,
  * @type: The selection mode
  *
  * Sets the selection mode of the @selection.  If the previous type was
- * #GTK_SELECTION_MULTIPLE and, then the anchor is kept selected, if it was
+ * #GTK_SELECTION_MULTIPLE, then the anchor is kept selected, if it was
  * previously selected.
  **/
 void
 gtk_tree_selection_set_mode (GtkTreeSelection *selection,
                             GtkSelectionMode  type)
 {
+  GtkTreeSelectionFunc tmp_func;
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
 
   if (selection->type == type)
     return;
 
-  if (type == GTK_SELECTION_SINGLE ||
-      type == GTK_SELECTION_BROWSE)
+  
+  if (type == GTK_SELECTION_NONE)
+    {
+      /* We do this so that we unconditionally unset all rows
+       */
+      tmp_func = selection->user_func;
+      selection->user_func = NULL;
+      gtk_tree_selection_unselect_all (selection);
+      selection->user_func = tmp_func;
+
+      gtk_tree_row_reference_free (selection->tree_view->priv->anchor);
+      selection->tree_view->priv->anchor = NULL;
+    }
+  else if (type == GTK_SELECTION_SINGLE ||
+          type == GTK_SELECTION_BROWSE)
     {
       GtkRBTree *tree = NULL;
       GtkRBNode *node = NULL;
       gint selected = FALSE;
+      GtkTreePath *anchor_path = NULL;
 
       if (selection->tree_view->priv->anchor)
        {
-          GtkTreePath *anchor_path;
-
           anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
 
           if (anchor_path)
@@ -203,19 +224,27 @@ gtk_tree_selection_set_mode (GtkTreeSelection *selection,
 
               if (node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
                 selected = TRUE;
-
-              gtk_tree_path_free (anchor_path);
             }
        }
-      /* FIXME: if user_func is set, then it needs to unconditionally unselect
-       * all.
+
+      /* We do this so that we unconditionally unset all rows
        */
+      tmp_func = selection->user_func;
+      selection->user_func = NULL;
       gtk_tree_selection_unselect_all (selection);
+      selection->user_func = tmp_func;
 
-      /* FIXME are we properly emitting the selection_changed signal here? */
       if (node && selected)
-       GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_IS_SELECTED);
+       _gtk_tree_selection_internal_select_node (selection,
+                                                 node,
+                                                 tree,
+                                                 anchor_path,
+                                                 0,
+                                                 FALSE);
+      if (anchor_path)
+       gtk_tree_path_free (anchor_path);
     }
+
   selection->type = type;
 }
 
@@ -245,6 +274,8 @@ gtk_tree_selection_get_mode (GtkTreeSelection *selection)
  *
  * Sets the selection function.  If set, this function is called before any node
  * is selected or unselected, giving some control over which nodes are selected.
+ * The select function should return %TRUE if the state of the node may be toggled,
+ * and %FALSE if the state of the node should be left unchanged.
  **/
 void
 gtk_tree_selection_set_select_function (GtkTreeSelection     *selection,
@@ -255,6 +286,14 @@ gtk_tree_selection_set_select_function (GtkTreeSelection     *selection,
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (func != NULL);
 
+  if (selection->destroy)
+    {
+      GtkDestroyNotify d = selection->destroy;
+
+      selection->destroy = NULL;
+      d (selection->user_data);
+    }
+
   selection->user_func = func;
   selection->user_data = data;
   selection->destroy = destroy;
@@ -295,7 +334,7 @@ gtk_tree_selection_get_tree_view (GtkTreeSelection *selection)
 /**
  * gtk_tree_selection_get_selected:
  * @selection: A #GtkTreeSelection.
- * @model: A pointer set to the #GtkTreeModel, or NULL.
+ * @model: A pointer to set to the #GtkTreeModel, or NULL.
  * @iter: The #GtkTreeIter, or NULL.
  *
  * Sets @iter to the currently selected node if @selection is set to
@@ -315,6 +354,7 @@ gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
   GtkRBNode *node;
   GtkTreePath *anchor_path;
   gboolean retval;
+  gboolean found_node;
 
   g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
   g_return_val_if_fail (selection->type != GTK_SELECTION_MULTIPLE, FALSE);
@@ -332,29 +372,30 @@ gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
   if (anchor_path == NULL)
     return FALSE;
 
-  if (iter == NULL)
-    {
-      gtk_tree_path_free (anchor_path);
-      return TRUE;
-    }
-
   retval = FALSE;
 
-  if (!_gtk_tree_view_find_node (selection->tree_view,
-                                 anchor_path,
-                                 &tree,
-                                 &node) &&
-      ! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+  found_node = !_gtk_tree_view_find_node (selection->tree_view,
+                                          anchor_path,
+                                          &tree,
+                                          &node);
+
+  if (found_node && GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
     {
-      /* We don't want to return the anchor if it isn't actually selected.
+      /* we only want to return the anchor if it exists in the rbtree and
+       * is selected.
        */
-      retval = FALSE;
+      if (iter == NULL)
+       retval = TRUE;
+      else
+        retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
+                                          iter,
+                                          anchor_path);
     }
   else
     {
-      retval = gtk_tree_model_get_iter (selection->tree_view->priv->model,
-                                        iter,
-                                        anchor_path);
+      /* We don't want to return the anchor if it isn't actually selected.
+       */
+      retval = FALSE;
     }
 
   gtk_tree_path_free (anchor_path);
@@ -362,13 +403,190 @@ gtk_tree_selection_get_selected (GtkTreeSelection  *selection,
   return retval;
 }
 
+/**
+ * gtk_tree_selection_get_selected_rows:
+ * @selection: A #GtkTreeSelection.
+ * @model: A pointer to set to the #GtkTreeModel, or NULL.
+ *
+ * Creates a list of path of all selected rows. Additionally, if you are
+ * planning on modifying the model after calling this function, you may
+ * want to convert the returned list into a list of #GtkTreeRowReferences.
+ * To do this, you can use gtk_tree_row_reference_new_proxy().
+ *
+ * To free the return value, use:
+ * <informalexample><programlisting>
+ * g_list_foreach (list, gtk_tree_path_free, NULL);
+ * g_list_free (list);
+ * </programlisting></informalexample>
+ *
+ * Return value: A #GList containing a #GtkTreePath for each selected row.
+ **/
+GList *
+gtk_tree_selection_get_selected_rows (GtkTreeSelection   *selection,
+                                      GtkTreeModel      **model)
+{
+  GList *list = NULL;
+  GtkRBTree *tree = NULL;
+  GtkRBNode *node = NULL;
+  GtkTreePath *path;
+
+  g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), NULL);
+  g_return_val_if_fail (selection->tree_view != NULL, NULL);
+  g_return_val_if_fail (selection->tree_view->priv->model != NULL, NULL);
+
+  if (selection->tree_view->priv->tree == NULL ||
+      selection->tree_view->priv->tree->root == NULL)
+    return NULL;
+
+  if (model)
+    *model = selection->tree_view->priv->model;
+
+  if (selection->type == GTK_SELECTION_NONE)
+    return NULL;
+  else if (selection->type != GTK_SELECTION_MULTIPLE)
+    {
+      GtkTreeIter iter;
+
+      if (gtk_tree_selection_get_selected (selection, NULL, &iter))
+        {
+         GtkTreePath *path;
+
+         path = gtk_tree_model_get_path (*model, &iter);
+         list = g_list_append (list, path);
+
+         return list;
+       }
+
+      return NULL;
+    }
+
+  tree = selection->tree_view->priv->tree;
+  node = selection->tree_view->priv->tree->root;
+
+  while (node->left != tree->nil)
+    node = node->left;
+  path = gtk_tree_path_new_first ();
+
+  do
+    {
+      if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+       list = g_list_append (list, gtk_tree_path_copy (path));
+
+      if (node->children)
+        {
+         tree = node->children;
+         node = tree->root;
+
+         while (node->left != tree->nil)
+           node = node->left;
+
+         gtk_tree_path_append_index (path, 0);
+       }
+      else
+        {
+         gboolean done = FALSE;
+
+         do
+           {
+             node = _gtk_rbtree_next (tree, node);
+             if (node != NULL)
+               {
+                 done = TRUE;
+                 gtk_tree_path_next (path);
+               }
+             else
+               {
+                 node = tree->parent_node;
+                 tree = tree->parent_tree;
+
+                 if (!tree)
+                   {
+                     gtk_tree_path_free (path);
+                     return list;
+                   }
+
+                 gtk_tree_path_up (path);
+               }
+           }
+         while (!done);
+       }
+    }
+  while (TRUE);
+
+  gtk_tree_path_free (path);
+
+  return list;
+}
+
+static void
+gtk_tree_selection_count_selected_rows_helper (GtkRBTree *tree,
+                                              GtkRBNode *node,
+                                              gpointer   data)
+{
+  gint *count = (gint *)data;
+
+  if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+    (*count)++;
+
+  if (node->children)
+    _gtk_rbtree_traverse (node->children, node->children->root,
+                         G_PRE_ORDER,
+                         gtk_tree_selection_count_selected_rows_helper, data);
+}
+
+/**
+ * gtk_tree_selection_count_selected_rows:
+ * @selection: A #GtkTreeSelection.
+ *
+ * Returns the number of rows that have been selected in @tree.
+ *
+ * Return value: The number of rows selected.
+ **/
+gint
+gtk_tree_selection_count_selected_rows (GtkTreeSelection *selection)
+{
+  gint count = 0;
+  GtkRBTree *tree;
+  GtkRBNode *node;
+
+  g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), 0);
+  g_return_val_if_fail (selection->tree_view != NULL, 0);
+  g_return_val_if_fail (selection->tree_view->priv->model != NULL, 0);
+
+  if (selection->tree_view->priv->tree == NULL ||
+      selection->tree_view->priv->tree->root == NULL)
+    return 0;
+
+  if (selection->type == GTK_SELECTION_SINGLE ||
+      selection->type == GTK_SELECTION_BROWSE)
+    {
+      if (gtk_tree_selection_get_selected (selection, NULL, NULL))
+       return 1;
+      else
+       return 0;
+    }
+
+  tree = selection->tree_view->priv->tree;
+  node = selection->tree_view->priv->tree->root;
+
+  _gtk_rbtree_traverse (selection->tree_view->priv->tree,
+                        selection->tree_view->priv->tree->root,
+                       G_PRE_ORDER,
+                       gtk_tree_selection_count_selected_rows_helper,
+                       &count);
+
+  return count;
+}
+
 /**
  * gtk_tree_selection_selected_foreach:
  * @selection: A #GtkTreeSelection.
  * @func: The function to call for each selected node.
  * @data: user data to pass to the function.
  *
- * Calls a function for each selected node.
+ * Calls a function for each selected node. Note that you cannot modify
+ * the tree or selection from within this function. As a result,
+ * gtk_tree_selection_get_selected_rows() might be more useful.
  **/
 void
 gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
@@ -409,7 +627,7 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
     node = node->left;
 
   /* find the node internally */
-  path = gtk_tree_path_new_root ();
+  path = gtk_tree_path_new_first ();
   gtk_tree_model_get_iter (selection->tree_view->priv->model,
                           &iter, path);
 
@@ -489,17 +707,19 @@ gtk_tree_selection_select_path (GtkTreeSelection *selection,
   GtkRBNode *node;
   GtkRBTree *tree;
   GdkModifierType state = 0;
+  gboolean ret;
 
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
   g_return_if_fail (path != NULL);
 
-  _gtk_tree_view_find_node (selection->tree_view,
-                           path,
-                           &tree,
-                           &node);
+  ret = _gtk_tree_view_find_node (selection->tree_view,
+                                 path,
+                                 &tree,
+                                 &node);
 
-  if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+  if (node == NULL || GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
+      ret == TRUE)
     return;
 
   if (selection->type == GTK_SELECTION_MULTIPLE)
@@ -509,7 +729,8 @@ gtk_tree_selection_select_path (GtkTreeSelection *selection,
                                            node,
                                            tree,
                                            path,
-                                           state);
+                                           state,
+                                           FALSE);
 }
 
 /**
@@ -525,24 +746,27 @@ gtk_tree_selection_unselect_path (GtkTreeSelection *selection,
 {
   GtkRBNode *node;
   GtkRBTree *tree;
+  gboolean ret;
 
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
   g_return_if_fail (path != NULL);
 
-  _gtk_tree_view_find_node (selection->tree_view,
-                           path,
-                           &tree,
-                           &node);
+  ret = _gtk_tree_view_find_node (selection->tree_view,
+                                 path,
+                                 &tree,
+                                 &node);
 
-  if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED))
+  if (node == NULL || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
+      ret == TRUE)
     return;
 
   _gtk_tree_selection_internal_select_node (selection,
                                            node,
                                            tree,
                                            path,
-                                           GDK_CONTROL_MASK);
+                                           GDK_CONTROL_MASK,
+                                           TRUE);
 }
 
 /**
@@ -598,10 +822,77 @@ gtk_tree_selection_unselect_iter (GtkTreeSelection *selection,
   if (path == NULL)
     return;
 
-  gtk_tree_selection_select_path (selection, path);
+  gtk_tree_selection_unselect_path (selection, path);
   gtk_tree_path_free (path);
 }
 
+/**
+ * gtk_tree_selection_path_is_selected:
+ * @selection: A #GtkTreeSelection.
+ * @path: A #GtkTreePath to check selection on.
+ * 
+ * Returns %TRUE if the row pointed to by @path is currently selected.  If @path
+ * does not point to a valid location, %FALSE is returned
+ * 
+ * Return value: %TRUE if @path is selected.
+ **/
+gboolean
+gtk_tree_selection_path_is_selected (GtkTreeSelection *selection,
+                                    GtkTreePath      *path)
+{
+  GtkRBNode *node;
+  GtkRBTree *tree;
+  gboolean ret;
+
+  g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
+  g_return_val_if_fail (path != NULL, FALSE);
+  g_return_val_if_fail (selection->tree_view != NULL, FALSE);
+  g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
+
+  ret = _gtk_tree_view_find_node (selection->tree_view,
+                                 path,
+                                 &tree,
+                                 &node);
+
+  if ((node == NULL) || !GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) ||
+      ret == TRUE)
+    return FALSE;
+
+  return TRUE;
+}
+
+/**
+ * gtk_tree_selection_iter_is_selected:
+ * @selection: A #GtkTreeSelection
+ * @iter: A valid #GtkTreeIter
+ * 
+ * Returns %TRUE if the row at @iter is currently selected.
+ * 
+ * Return value: %TRUE, if @iter is selected
+ **/
+gboolean
+gtk_tree_selection_iter_is_selected (GtkTreeSelection *selection,
+                                    GtkTreeIter      *iter)
+{
+  GtkTreePath *path;
+  gboolean retval;
+
+  g_return_val_if_fail (GTK_IS_TREE_SELECTION (selection), FALSE);
+  g_return_val_if_fail (iter != NULL, FALSE);
+  g_return_val_if_fail (selection->tree_view != NULL, FALSE);
+  g_return_val_if_fail (selection->tree_view->priv->model != NULL, FALSE);
+
+  path = gtk_tree_model_get_path (selection->tree_view->priv->model, iter);
+  if (path == NULL)
+    return FALSE;
+
+  retval = gtk_tree_selection_path_is_selected (selection, path);
+  gtk_tree_path_free (path);
+
+  return retval;
+}
+
+
 /* Wish I was in python, right now... */
 struct _TempTuple {
   GtkTreeSelection *selection;
@@ -672,7 +963,7 @@ gtk_tree_selection_select_all (GtkTreeSelection *selection)
   g_return_if_fail (selection->tree_view != NULL);
   if (selection->tree_view->priv->tree == NULL)
     return;
-  g_return_if_fail (selection->type != GTK_SELECTION_MULTIPLE);
+  g_return_if_fail (selection->type == GTK_SELECTION_MULTIPLE);
 
   if (gtk_tree_selection_real_select_all (selection))
     g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
@@ -781,8 +1072,15 @@ gtk_tree_selection_unselect_all (GtkTreeSelection *selection)
     g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
 }
 
+enum
+{
+  RANGE_SELECT,
+  RANGE_UNSELECT
+};
+
 static gint
-gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
+gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
+                                      gint              mode,
                                      GtkTreePath      *start_path,
                                      GtkTreePath      *end_path)
 {
@@ -827,7 +1125,7 @@ gtk_tree_selection_real_select_range (GtkTreeSelection *selection,
 
   do
     {
-      dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, TRUE);
+      dirty |= gtk_tree_selection_real_select_node (selection, start_tree, start_node, (mode == RANGE_SELECT)?TRUE:FALSE);
 
       if (start_node == end_node)
        break;
@@ -871,23 +1169,53 @@ gtk_tree_selection_select_range (GtkTreeSelection *selection,
   g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
   g_return_if_fail (selection->tree_view != NULL);
 
-  if (gtk_tree_selection_real_select_range (selection, start_path, end_path))
+  if (gtk_tree_selection_real_modify_range (selection, RANGE_SELECT, start_path, end_path))
+    g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
+}
+
+/**
+ * gtk_tree_selection_unselect_range:
+ * @selection: A #GtkTreeSelection.
+ * @start_path: The initial node of the range.
+ * @end_path: The initial node of the range.
+ *
+ * Unselects a range of nodes, determined by @start_path and @end_path
+ * inclusive.
+ **/
+void
+gtk_tree_selection_unselect_range (GtkTreeSelection *selection,
+                                   GtkTreePath      *start_path,
+                                  GtkTreePath      *end_path)
+{
+  g_return_if_fail (GTK_IS_TREE_SELECTION (selection));
+  g_return_if_fail (selection->tree_view != NULL);
+
+  if (gtk_tree_selection_real_modify_range (selection, RANGE_UNSELECT, start_path, end_path))
     g_signal_emit (G_OBJECT (selection), tree_selection_signals[CHANGED], 0);
 }
 
 /* Called internally by gtktreeview.c It handles actually selecting the tree.
  */
+
+/*
+ * docs about the 'override_browse_mode', we set this flag when we want to
+ * unset select the node and override the select browse mode behaviour (that is
+ * 'one node should *always* be selected').
+ */
 void
 _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
                                          GtkRBNode        *node,
                                          GtkRBTree        *tree,
                                          GtkTreePath      *path,
-                                         GdkModifierType   state)
+                                         GdkModifierType   state,
+                                         gboolean          override_browse_mode)
 {
   gint flags;
   gint dirty = FALSE;
   GtkTreePath *anchor_path = NULL;
 
+  if (selection->type == GTK_SELECTION_NONE)
+    return;
 
   if (selection->tree_view->priv->anchor)
     anchor_path = gtk_tree_row_reference_get_path (selection->tree_view->priv->anchor);
@@ -895,9 +1223,14 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
   if (selection->type == GTK_SELECTION_SINGLE ||
       selection->type == GTK_SELECTION_BROWSE)
     {
+      /* just unselect */
+      if (selection->type == GTK_SELECTION_BROWSE && override_browse_mode)
+        {
+         dirty = gtk_tree_selection_real_unselect_all (selection);
+       }
       /* Did we try to select the same node again? */
-      if (selection->type == GTK_SELECTION_SINGLE &&
-         anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
+      else if (selection->type == GTK_SELECTION_SINGLE &&
+              anchor_path && gtk_tree_path_compare (path, anchor_path) == 0)
        {
          if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
            {
@@ -906,12 +1239,30 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
        }
       else
        {
-         /* FIXME: We only want to select the new node if we can unselect the
-          * old one, and we can select the new one.  We are currently
-          * unselecting the old one first, then trying the new one. */
          if (anchor_path)
            {
-             dirty = gtk_tree_selection_real_unselect_all (selection);
+             /* We only want to select the new node if we can unselect the old one,
+              * and we can select the new one. */
+             if (selection->user_func)
+               {
+                 if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
+                                              GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
+                                              selection->user_data))
+                   dirty = TRUE;
+               }
+             else
+               {
+                 dirty = TRUE;
+               }
+
+             /* if dirty is FALSE, we weren't able to select the new one, otherwise, we try to
+              * unselect the new one
+              */
+             if (dirty)
+               dirty = gtk_tree_selection_real_unselect_all (selection);
+
+             /* if dirty is TRUE at this point, we successfully unselected the
+              * old one, and can then select the new one */
              if (dirty)
                {
                  if (selection->tree_view->priv->anchor)
@@ -968,7 +1319,8 @@ _gtk_tree_selection_internal_select_node (GtkTreeSelection *selection,
       else if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
        {
          dirty = gtk_tree_selection_real_unselect_all (selection);
-         dirty |= gtk_tree_selection_real_select_range (selection,
+         dirty |= gtk_tree_selection_real_modify_range (selection,
+                                                         RANGE_SELECT,
                                                         anchor_path,
                                                         path);
        }
@@ -1005,12 +1357,16 @@ gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
   gboolean selected = FALSE;
   GtkTreePath *path = NULL;
 
+  select = !! select;
+
   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED) != select)
     {
       path = _gtk_tree_view_find_path (selection->tree_view, tree, node);
       if (selection->user_func)
        {
-         if ((*selection->user_func) (selection, selection->tree_view->priv->model, path, selection->user_data))
+         if ((*selection->user_func) (selection, selection->tree_view->priv->model, path,
+                                       GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_SELECTED),
+                                       selection->user_data))
            selected = TRUE;
        }
       else
@@ -1022,8 +1378,8 @@ gtk_tree_selection_real_select_node (GtkTreeSelection *selection,
     {
       node->flags ^= GTK_RBNODE_IS_SELECTED;
 
-      /* FIXME: just draw the one node*/
-      gtk_widget_queue_draw (GTK_WIDGET (selection->tree_view));
+      _gtk_tree_view_queue_draw_node (selection->tree_view, tree, node, NULL);
+      
       return TRUE;
     }