]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktreestore.c
Add a GtkPrintOperation::show-preview property
[~andy/gtk] / gtk / gtktreestore.c
index 38164bb5952ae752457b4e23c229b6f9f51b7360..03dd170a31919f3a3b29783eac7827fafe54d29e 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+#include <config.h>
+#include <string.h>
+#include <gobject/gvaluecollector.h>
 #include "gtktreemodel.h"
 #include "gtktreestore.h"
 #include "gtktreedatalist.h"
 #include "gtktreednd.h"
-#include <string.h>
-#include <gobject/gvaluecollector.h>
+#include "gtkintl.h"
+#include "gtkalias.h"
 
 #define G_NODE(node) ((GNode *)node)
-#define GTK_TREE_STORE_IS_SORTED(tree) (GTK_TREE_STORE (tree)->sort_column_id != -2)
-#define VALID_ITER(iter, tree_store) (iter!= NULL && iter->user_data != NULL && tree_store->stamp == iter->stamp)
+#define GTK_TREE_STORE_IS_SORTED(tree) (((GtkTreeStore*)(tree))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
+#define VALID_ITER(iter, tree_store) ((iter)!= NULL && (iter)->user_data != NULL && ((GtkTreeStore*)(tree_store))->stamp == (iter)->stamp)
 
-static void         gtk_tree_store_init            (GtkTreeStore      *tree_store);
-static void         gtk_tree_store_class_init      (GtkTreeStoreClass *tree_store_class);
 static void         gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
 static void         gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
 static void         gtk_tree_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
 static void         gtk_tree_store_sortable_init   (GtkTreeSortableIface   *iface);
 static void         gtk_tree_store_finalize        (GObject           *object);
-static guint        gtk_tree_store_get_flags       (GtkTreeModel      *tree_model);
+static GtkTreeModelFlags gtk_tree_store_get_flags  (GtkTreeModel      *tree_model);
 static gint         gtk_tree_store_get_n_columns   (GtkTreeModel      *tree_model);
 static GType        gtk_tree_store_get_column_type (GtkTreeModel      *tree_model,
                                                    gint               index);
@@ -72,8 +73,12 @@ static void gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
                                            gint          column,
                                            GType         type);
 
+static void gtk_tree_store_increment_stamp (GtkTreeStore  *tree_store);
+
 
 /* DND interfaces */
+static gboolean real_gtk_tree_store_row_draggable   (GtkTreeDragSource *drag_source,
+                                                  GtkTreePath       *path);
 static gboolean gtk_tree_store_drag_data_delete   (GtkTreeDragSource *drag_source,
                                                   GtkTreePath       *path);
 static gboolean gtk_tree_store_drag_data_get      (GtkTreeDragSource *drag_source,
@@ -91,7 +96,8 @@ static gboolean gtk_tree_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
 static void     gtk_tree_store_sort                    (GtkTreeStore           *tree_store);
 static void     gtk_tree_store_sort_iter_changed       (GtkTreeStore           *tree_store,
                                                        GtkTreeIter            *iter,
-                                                       gint                    column);
+                                                       gint                    column,
+                                                       gboolean                emit_signal);
 static gboolean gtk_tree_store_get_sort_column_id      (GtkTreeSortable        *sortable,
                                                        gint                   *sort_column_id,
                                                        GtkSortType            *order);
@@ -111,8 +117,10 @@ static gboolean gtk_tree_store_has_default_sort_func   (GtkTreeSortable        *
 
 static void     validate_gnode                         (GNode *node);
 
-
-static GObjectClass *parent_class = NULL;
+static void     gtk_tree_store_move                    (GtkTreeStore           *tree_store,
+                                                        GtkTreeIter            *iter,
+                                                        GtkTreeIter            *position,
+                                                        gboolean                before);
 
 
 static inline void
@@ -126,80 +134,21 @@ validate_tree (GtkTreeStore *tree_store)
     }
 }
 
-GtkType
-gtk_tree_store_get_type (void)
-{
-  static GType tree_store_type = 0;
-
-  if (!tree_store_type)
-    {
-      static const GTypeInfo tree_store_info =
-      {
-        sizeof (GtkTreeStoreClass),
-       NULL,           /* base_init */
-       NULL,           /* base_finalize */
-        (GClassInitFunc) gtk_tree_store_class_init,
-       NULL,           /* class_finalize */
-       NULL,           /* class_data */
-        sizeof (GtkTreeStore),
-       0,              /* n_preallocs */
-        (GInstanceInitFunc) gtk_tree_store_init
-      };
-
-      static const GInterfaceInfo tree_model_info =
-      {
-       (GInterfaceInitFunc) gtk_tree_store_tree_model_init,
-       NULL,
-       NULL
-      };
-
-      static const GInterfaceInfo drag_source_info =
-      {
-       (GInterfaceInitFunc) gtk_tree_store_drag_source_init,
-       NULL,
-       NULL
-      };
-
-      static const GInterfaceInfo drag_dest_info =
-      {
-       (GInterfaceInitFunc) gtk_tree_store_drag_dest_init,
-       NULL,
-       NULL
-      };
-
-      static const GInterfaceInfo sortable_info =
-      {
-       (GInterfaceInitFunc) gtk_tree_store_sortable_init,
-       NULL,
-       NULL
-      };
-
-      tree_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeStore", &tree_store_info, 0);
-
-      g_type_add_interface_static (tree_store_type,
-                                  GTK_TYPE_TREE_MODEL,
-                                  &tree_model_info);
-      g_type_add_interface_static (tree_store_type,
-                                  GTK_TYPE_TREE_DRAG_SOURCE,
-                                  &drag_source_info);
-      g_type_add_interface_static (tree_store_type,
-                                  GTK_TYPE_TREE_DRAG_DEST,
-                                  &drag_dest_info);
-      g_type_add_interface_static (tree_store_type,
-                                  GTK_TYPE_TREE_SORTABLE,
-                                  &sortable_info);
-
-    }
-
-  return tree_store_type;
-}
+G_DEFINE_TYPE_WITH_CODE (GtkTreeStore, gtk_tree_store, G_TYPE_OBJECT,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
+                                               gtk_tree_store_tree_model_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
+                                               gtk_tree_store_drag_source_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
+                                               gtk_tree_store_drag_dest_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
+                                               gtk_tree_store_sortable_init))
 
 static void
 gtk_tree_store_class_init (GtkTreeStoreClass *class)
 {
   GObjectClass *object_class;
 
-  parent_class = g_type_class_peek_parent (class);
   object_class = (GObjectClass *) class;
 
   object_class->finalize = gtk_tree_store_finalize;
@@ -225,6 +174,7 @@ gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
 static void
 gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
 {
+  iface->row_draggable = real_gtk_tree_store_row_draggable;
   iface->drag_data_delete = gtk_tree_store_drag_data_delete;
   iface->drag_data_get = gtk_tree_store_drag_data_get;
 }
@@ -269,7 +219,10 @@ gtk_tree_store_init (GtkTreeStore *tree_store)
  * @Varargs: all #GType types for the columns, from first to last
  *
  * Creates a new tree store as with @n_columns columns each of the types passed
- * in.  As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
+ * in.  Note that only types derived from standard GObject fundamental types 
+ * are supported. 
+ *
+ * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
  * GDK_TYPE_PIXBUF);</literal> will create a new #GtkTreeStore with three columns, of type
  * <type>int</type>, <type>string</type> and #GdkPixbuf respectively.
  *
@@ -285,7 +238,7 @@ gtk_tree_store_new (gint n_columns,
 
   g_return_val_if_fail (n_columns > 0, NULL);
 
-  retval = GTK_TREE_STORE (g_object_new (GTK_TYPE_TREE_STORE, NULL));
+  retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
   gtk_tree_store_set_n_columns (retval, n_columns);
 
   va_start (args, n_columns);
@@ -295,8 +248,9 @@ gtk_tree_store_new (gint n_columns,
       GType type = va_arg (args, GType);
       if (! _gtk_tree_data_list_check_type (type))
        {
-         g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (type));
-         g_object_unref (G_OBJECT (retval));
+         g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
+                    G_STRLOC, g_type_name (type));
+         g_object_unref (retval);
          return NULL;
        }
       gtk_tree_store_set_column_type (retval, i, type);
@@ -323,15 +277,16 @@ gtk_tree_store_newv (gint   n_columns,
 
   g_return_val_if_fail (n_columns > 0, NULL);
 
-  retval = GTK_TREE_STORE (g_object_new (GTK_TYPE_TREE_STORE, NULL));
+  retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
   gtk_tree_store_set_n_columns (retval, n_columns);
 
    for (i = 0; i < n_columns; i++)
     {
       if (! _gtk_tree_data_list_check_type (types[i]))
        {
-         g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (types[i]));
-         g_object_unref (G_OBJECT (retval));
+         g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
+                    G_STRLOC, g_type_name (types[i]));
+         g_object_unref (retval);
          return NULL;
        }
       gtk_tree_store_set_column_type (retval, i, types[i]);
@@ -380,8 +335,6 @@ gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
 {
   GType *new_columns;
 
-  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
-
   if (tree_store->n_columns == n_columns)
     return;
 
@@ -423,8 +376,6 @@ gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
                                gint          column,
                                GType         type)
 {
-  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
-  g_return_if_fail (column >=0 && column < tree_store->n_columns);
   if (!_gtk_tree_data_list_check_type (type))
     {
       g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (type));
@@ -433,10 +384,14 @@ gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
   tree_store->column_headers[column] = type;
 }
 
-static void
+static gboolean
 node_free (GNode *node, gpointer data)
 {
-  _gtk_tree_data_list_free (node->data, (GType*)data);
+  if (node->data)
+    _gtk_tree_data_list_free (node->data, (GType*)data);
+  node->data = NULL;
+
+  return FALSE;
 }
 
 static void
@@ -444,7 +399,9 @@ gtk_tree_store_finalize (GObject *object)
 {
   GtkTreeStore *tree_store = GTK_TREE_STORE (object);
 
-  g_node_children_foreach (tree_store->root, G_TRAVERSE_LEAFS, node_free, tree_store->column_headers);
+  g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
+                  node_free, tree_store->column_headers);
+  g_node_destroy (tree_store->root);
   _gtk_tree_data_list_header_free (tree_store->sort_list);
   g_free (tree_store->column_headers);
 
@@ -457,7 +414,8 @@ gtk_tree_store_finalize (GObject *object)
       tree_store->default_sort_data = NULL;
     }
 
-  (* parent_class->finalize) (object);
+  /* must chain up */
+  G_OBJECT_CLASS (gtk_tree_store_parent_class)->finalize (object);
 }
 
 /* fulfill the GtkTreeModel requirements */
@@ -467,11 +425,9 @@ gtk_tree_store_finalize (GObject *object)
  */
 
 
-static guint
+static GtkTreeModelFlags
 gtk_tree_store_get_flags (GtkTreeModel *tree_model)
 {
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);
-
   return GTK_TREE_MODEL_ITERS_PERSIST;
 }
 
@@ -480,8 +436,6 @@ gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
 {
   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);
-
   tree_store->columns_dirty = TRUE;
 
   return tree_store->n_columns;
@@ -493,9 +447,7 @@ gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
 {
   GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), G_TYPE_INVALID);
-  g_return_val_if_fail (index < GTK_TREE_STORE (tree_model)->n_columns &&
-                       index >= 0, G_TYPE_INVALID);
+  g_return_val_if_fail (index < tree_store->n_columns, G_TYPE_INVALID);
 
   tree_store->columns_dirty = TRUE;
 
@@ -512,8 +464,6 @@ gtk_tree_store_get_iter (GtkTreeModel *tree_model,
   gint *indices;
   gint depth, i;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
-
   tree_store->columns_dirty = TRUE;
 
   indices = gtk_tree_path_get_indices (path);
@@ -524,13 +474,13 @@ gtk_tree_store_get_iter (GtkTreeModel *tree_model,
   parent.stamp = tree_store->stamp;
   parent.user_data = tree_store->root;
 
-  if (! gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[0]))
+  if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[0]))
     return FALSE;
 
   for (i = 1; i < depth; i++)
     {
       parent = *iter;
-      if (! gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[i]))
+      if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[i]))
        return FALSE;
     }
 
@@ -541,26 +491,25 @@ static GtkTreePath *
 gtk_tree_store_get_path (GtkTreeModel *tree_model,
                         GtkTreeIter  *iter)
 {
+  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
   GtkTreePath *retval;
   GNode *tmp_node;
   gint i = 0;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), NULL);
-  g_return_val_if_fail (iter != NULL, NULL);
   g_return_val_if_fail (iter->user_data != NULL, NULL);
-  g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, NULL);
+  g_return_val_if_fail (iter->stamp == tree_store->stamp, NULL);
 
-  validate_tree ((GtkTreeStore*)tree_model);
+  validate_tree (tree_store);
 
   if (G_NODE (iter->user_data)->parent == NULL &&
-      G_NODE (iter->user_data) == GTK_TREE_STORE (tree_model)->root)
+      G_NODE (iter->user_data) == tree_store->root)
     return gtk_tree_path_new ();
   g_assert (G_NODE (iter->user_data)->parent != NULL);
 
-  if (G_NODE (iter->user_data)->parent == G_NODE (GTK_TREE_STORE (tree_model)->root))
+  if (G_NODE (iter->user_data)->parent == G_NODE (tree_store->root))
     {
       retval = gtk_tree_path_new ();
-      tmp_node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
+      tmp_node = G_NODE (tree_store->root)->children;
     }
   else
     {
@@ -568,8 +517,7 @@ gtk_tree_store_get_path (GtkTreeModel *tree_model,
 
       tmp_iter.user_data = G_NODE (iter->user_data)->parent;
 
-      retval = gtk_tree_store_get_path (tree_model,
-                                       &tmp_iter);
+      retval = gtk_tree_store_get_path (tree_model, &tmp_iter);
       tmp_node = G_NODE (iter->user_data)->parent->children;
     }
 
@@ -609,13 +557,12 @@ gtk_tree_store_get_value (GtkTreeModel *tree_model,
                          gint          column,
                          GValue       *value)
 {
+  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
   GtkTreeDataList *list;
   gint tmp_column = column;
 
-  g_return_if_fail (GTK_IS_TREE_STORE (tree_model));
-  g_return_if_fail (iter != NULL);
-  g_return_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp);
-  g_return_if_fail (column < GTK_TREE_STORE (tree_model)->n_columns);
+  g_return_if_fail (column < tree_store->n_columns);
+  g_return_if_fail (VALID_ITER (iter, tree_store));
 
   list = G_NODE (iter->user_data)->data;
 
@@ -625,13 +572,13 @@ gtk_tree_store_get_value (GtkTreeModel *tree_model,
   if (list)
     {
       _gtk_tree_data_list_node_to_value (list,
-                                        GTK_TREE_STORE (tree_model)->column_headers[column],
+                                        tree_store->column_headers[column],
                                         value);
     }
   else
     {
       /* We want to return an initialized but empty (default) value */
-      g_value_init (value, GTK_TREE_STORE (tree_model)->column_headers[column]);
+      g_value_init (value, tree_store->column_headers[column]);
     }
 }
 
@@ -639,7 +586,6 @@ static gboolean
 gtk_tree_store_iter_next (GtkTreeModel  *tree_model,
                          GtkTreeIter   *iter)
 {
-  g_return_val_if_fail (iter != NULL, FALSE);
   g_return_val_if_fail (iter->user_data != NULL, FALSE);
   g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
 
@@ -657,19 +603,20 @@ gtk_tree_store_iter_children (GtkTreeModel *tree_model,
                              GtkTreeIter  *iter,
                              GtkTreeIter  *parent)
 {
+  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
   GNode *children;
 
-  g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
-  g_return_val_if_fail (parent == NULL || parent->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
+  if (parent)
+    g_return_val_if_fail (VALID_ITER (parent, tree_store), FALSE);
 
   if (parent)
     children = G_NODE (parent->user_data)->children;
   else
-    children = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
+    children = G_NODE (tree_store->root)->children;
 
   if (children)
     {
-      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
+      iter->stamp = tree_store->stamp;
       iter->user_data = children;
       return TRUE;
     }
@@ -681,9 +628,8 @@ static gboolean
 gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
                               GtkTreeIter  *iter)
 {
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), FALSE);
-  g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
   g_return_val_if_fail (iter->user_data != NULL, FALSE);
+  g_return_val_if_fail (VALID_ITER (iter, tree_model), FALSE);
 
   return G_NODE (iter->user_data)->children != NULL;
 }
@@ -695,8 +641,7 @@ gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
   GNode *node;
   gint i = 0;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);
-  g_return_val_if_fail (iter == NULL || iter->user_data != NULL, FALSE);
+  g_return_val_if_fail (iter == NULL || iter->user_data != NULL, 0);
 
   if (iter == NULL)
     node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
@@ -718,14 +663,14 @@ gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
                               GtkTreeIter  *parent,
                               gint          n)
 {
+  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
   GNode *parent_node;
   GNode *child;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), FALSE);
   g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
 
   if (parent == NULL)
-    parent_node = GTK_TREE_STORE (tree_model)->root;
+    parent_node = tree_store->root;
   else
     parent_node = parent->user_data;
 
@@ -734,7 +679,7 @@ gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
   if (child)
     {
       iter->user_data = child;
-      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
+      iter->stamp = tree_store->stamp;
       return TRUE;
     }
   else
@@ -746,21 +691,20 @@ gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
                            GtkTreeIter  *iter,
                            GtkTreeIter  *child)
 {
+  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
   GNode *parent;
 
   g_return_val_if_fail (iter != NULL, FALSE);
-  g_return_val_if_fail (child != NULL, FALSE);
-  g_return_val_if_fail (child->user_data != NULL, FALSE);
-  g_return_val_if_fail (child->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
+  g_return_val_if_fail (VALID_ITER (child, tree_store), FALSE);
 
   parent = G_NODE (child->user_data)->parent;
 
   g_assert (parent != NULL);
 
-  if (parent != GTK_TREE_STORE (tree_model)->root)
+  if (parent != tree_store->root)
     {
       iter->user_data = parent;
-      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
+      iter->stamp = tree_store->stamp;
       return TRUE;
     }
   else
@@ -778,7 +722,7 @@ gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
 {
   GtkTreeDataList *list;
   GtkTreeDataList *prev;
-  GtkTreePath *path = NULL;
+  gint old_column = column;
   GValue real_value = {0, };
   gboolean converted = FALSE;
   gboolean retval = FALSE;
@@ -808,8 +752,6 @@ gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
 
   prev = list = G_NODE (iter->user_data)->data;
 
-  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
-
   while (list != NULL)
     {
       if (column == 0)
@@ -819,9 +761,10 @@ gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
          else
            _gtk_tree_data_list_value_to_node (list, value);
          retval = TRUE;
-         gtk_tree_path_free (path);
          if (converted)
            g_value_unset (&real_value);
+          if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
+            gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
          return retval;
        }
 
@@ -848,16 +791,19 @@ gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
       list->next = NULL;
       column --;
     }
+
   if (converted)
     _gtk_tree_data_list_value_to_node (list, &real_value);
   else
     _gtk_tree_data_list_value_to_node (list, value);
   
   retval = TRUE;
-  gtk_tree_path_free (path);
   if (converted)
     g_value_unset (&real_value);
 
+  if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
+    gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
+
   return retval;
 }
 
@@ -888,37 +834,17 @@ gtk_tree_store_set_value (GtkTreeStore *tree_store,
     {
       GtkTreePath *path;
 
-      path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), iter);
+      path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
       gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
       gtk_tree_path_free (path);
     }
 }
 
-/**
- * gtk_tree_store_set_valist:
- * @tree_store: A #GtkTreeStore
- * @iter: A valid #GtkTreeIter for the row being modified
- * @var_args: <type>va_list</type> of column/value pairs
- *
- * See gtk_tree_store_set(); this version takes a <type>va_list</type> for
- * use by language bindings.
- *
- **/
-void
-gtk_tree_store_set_valist (GtkTreeStore *tree_store,
-                           GtkTreeIter  *iter,
-                           va_list     var_args)
+static GtkTreeIterCompareFunc
+gtk_tree_store_get_compare_func (GtkTreeStore *tree_store)
 {
-  gint column;
-  gboolean emit_signal = FALSE;
-  gboolean maybe_need_sort = FALSE;
   GtkTreeIterCompareFunc func = NULL;
 
-  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
-  g_return_if_fail (VALID_ITER (iter, tree_store));
-
-  column = va_arg (var_args, gint);
-
   if (GTK_TREE_STORE_IS_SORTED (tree_store))
     {
       if (tree_store->sort_column_id != -1)
@@ -926,8 +852,8 @@ gtk_tree_store_set_valist (GtkTreeStore *tree_store,
          GtkTreeDataSortHeader *header;
          header = _gtk_tree_data_list_get_header (tree_store->sort_list,
                                                   tree_store->sort_column_id);
-         g_return_if_fail (header != NULL);
-         g_return_if_fail (header->func != NULL);
+         g_return_val_if_fail (header != NULL, NULL);
+         g_return_val_if_fail (header->func != NULL, NULL);
          func = header->func;
        }
       else
@@ -936,8 +862,24 @@ gtk_tree_store_set_valist (GtkTreeStore *tree_store,
        }
     }
 
-  if (func != gtk_tree_data_list_compare_func)
-    maybe_need_sort = TRUE;
+  return func;
+}
+
+static void
+gtk_tree_store_set_valist_internal (GtkTreeStore *tree_store,
+                                    GtkTreeIter  *iter,
+                                    gboolean     *emit_signal,
+                                    gboolean     *maybe_need_sort,
+                                    va_list       var_args)
+{
+  gint column;
+  GtkTreeIterCompareFunc func = NULL;
+
+  column = va_arg (var_args, gint);
+
+  func = gtk_tree_store_get_compare_func (tree_store);
+  if (func != _gtk_tree_data_list_compare_func)
+    *maybe_need_sort = TRUE;
 
   while (column != -1)
     {
@@ -963,29 +905,56 @@ gtk_tree_store_set_valist (GtkTreeStore *tree_store,
          break;
        }
 
-      emit_signal = gtk_tree_store_real_set_value (tree_store,
-                                                  iter,
-                                                  column,
-                                                  &value,
-                                                  FALSE) || emit_signal;
+      *emit_signal = gtk_tree_store_real_set_value (tree_store,
+                                                   iter,
+                                                   column,
+                                                   &value,
+                                                   FALSE) || *emit_signal;
 
-      if (func == gtk_tree_data_list_compare_func &&
+      if (func == _gtk_tree_data_list_compare_func &&
          column == tree_store->sort_column_id)
-       maybe_need_sort = TRUE;
+       *maybe_need_sort = TRUE;
 
       g_value_unset (&value);
 
       column = va_arg (var_args, gint);
     }
+}
+
+/**
+ * gtk_tree_store_set_valist:
+ * @tree_store: A #GtkTreeStore
+ * @iter: A valid #GtkTreeIter for the row being modified
+ * @var_args: <type>va_list</type> of column/value pairs
+ *
+ * See gtk_tree_store_set(); this version takes a <type>va_list</type> for
+ * use by language bindings.
+ *
+ **/
+void
+gtk_tree_store_set_valist (GtkTreeStore *tree_store,
+                           GtkTreeIter  *iter,
+                           va_list       var_args)
+{
+  gboolean emit_signal = FALSE;
+  gboolean maybe_need_sort = FALSE;
+
+  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+  g_return_if_fail (VALID_ITER (iter, tree_store));
+
+  gtk_tree_store_set_valist_internal (tree_store, iter,
+                                     &emit_signal,
+                                     &maybe_need_sort,
+                                     var_args);
 
   if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
-    gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id);
+    gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
 
   if (emit_signal)
     {
       GtkTreePath *path;
 
-      path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), iter);
+      path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
       gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
       gtk_tree_path_free (path);
     }
@@ -1011,9 +980,6 @@ gtk_tree_store_set (GtkTreeStore *tree_store,
 {
   va_list var_args;
 
-  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
-  g_return_if_fail (VALID_ITER (iter, tree_store));
-
   va_start (var_args, iter);
   gtk_tree_store_set_valist (tree_store, iter, var_args);
   va_end (var_args);
@@ -1027,8 +993,10 @@ gtk_tree_store_set (GtkTreeStore *tree_store,
  * Removes @iter from @tree_store.  After being removed, @iter is set to the
  * next valid row at that level, or invalidated if it previously pointed to the
  * last one.
+ *
+ * Return value: %TRUE if @iter is still valid, %FALSE if not.
  **/
-void
+gboolean
 gtk_tree_store_remove (GtkTreeStore *tree_store,
                       GtkTreeIter  *iter)
 {
@@ -1037,8 +1005,8 @@ gtk_tree_store_remove (GtkTreeStore *tree_store,
   GNode *parent;
   GNode *next_node;
 
-  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
-  g_return_if_fail (VALID_ITER (iter, tree_store));
+  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
+  g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
 
   parent = G_NODE (iter->user_data)->parent;
 
@@ -1046,8 +1014,8 @@ gtk_tree_store_remove (GtkTreeStore *tree_store,
   next_node = G_NODE (iter->user_data)->next;
 
   if (G_NODE (iter->user_data)->data)
-    _gtk_tree_data_list_free ((GtkTreeDataList *) G_NODE (iter->user_data)->data,
-                             tree_store->column_headers);
+    g_node_traverse (G_NODE (iter->user_data), G_POST_ORDER, G_TRAVERSE_ALL,
+                    -1, node_free, tree_store->column_headers);
 
   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
   g_node_destroy (G_NODE (iter->user_data));
@@ -1073,12 +1041,15 @@ gtk_tree_store_remove (GtkTreeStore *tree_store,
     {
       iter->stamp = tree_store->stamp;
       iter->user_data = next_node;
+      return TRUE;
     }
   else
     {
       iter->stamp = 0;
       iter->user_data = NULL;
     }
+
+  return FALSE;
 }
 
 /**
@@ -1105,8 +1076,10 @@ gtk_tree_store_insert (GtkTreeStore *tree_store,
 {
   GtkTreePath *path;
   GNode *parent_node;
+  GNode *new_node;
 
   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+  g_return_if_fail (iter != NULL);
   if (parent)
     g_return_if_fail (VALID_ITER (parent, tree_store));
 
@@ -1117,13 +1090,24 @@ gtk_tree_store_insert (GtkTreeStore *tree_store,
 
   tree_store->columns_dirty = TRUE;
 
+  new_node = g_node_new (NULL);
+
   iter->stamp = tree_store->stamp;
-  iter->user_data = g_node_new (NULL);
-  g_node_insert (parent_node, position, G_NODE (iter->user_data));
+  iter->user_data = new_node;
+  g_node_insert (parent_node, position, new_node);
 
   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
 
+  if (parent_node != tree_store->root)
+    {
+      if (new_node->prev == NULL && new_node->next == NULL)
+        {
+          gtk_tree_path_up (path);
+          gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
+        }
+    }
+
   gtk_tree_path_free (path);
 
   validate_tree ((GtkTreeStore*)tree_store);
@@ -1137,10 +1121,10 @@ gtk_tree_store_insert (GtkTreeStore *tree_store,
  * @sibling: A valid #GtkTreeIter, or %NULL
  *
  * Inserts a new row before @sibling.  If @sibling is %NULL, then the row will
- * be appended to the beginning of the @parent 's children.  If @parent and
- * @sibling are %NULL, then the row will be appended to the toplevel.  If both
- * @sibling and @parent are set, then @parent must be the parent of @sibling.
- * When @sibling is set, @parent is optional.
+ * be appended to @parent 's children.  If @parent and @sibling are %NULL, then
+ * the row will be appended to the toplevel.  If both @sibling and @parent are
+ * set, then @parent must be the parent of @sibling.  When @sibling is set,
+ * @parent is optional.
  *
  * @iter will be changed to point to this new row.  The row will be empty after
  * this function is called.  To fill in values, you need to call
@@ -1164,10 +1148,6 @@ gtk_tree_store_insert_before (GtkTreeStore *tree_store,
   if (sibling != NULL)
     g_return_if_fail (VALID_ITER (sibling, tree_store));
 
-  tree_store->columns_dirty = TRUE;
-
-  new_node = g_node_new (NULL);
-
   if (parent == NULL && sibling == NULL)
     parent_node = tree_store->root;
   else if (parent == NULL)
@@ -1180,6 +1160,10 @@ gtk_tree_store_insert_before (GtkTreeStore *tree_store,
       parent_node = G_NODE (parent->user_data);
     }
 
+  tree_store->columns_dirty = TRUE;
+
+  new_node = g_node_new (NULL);
+
   g_node_insert_before (parent_node,
                        sibling ? G_NODE (sibling->user_data) : NULL,
                         new_node);
@@ -1190,9 +1174,23 @@ gtk_tree_store_insert_before (GtkTreeStore *tree_store,
   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
 
+  if (parent_node != tree_store->root)
+    {
+      if (new_node->prev == NULL && new_node->next == NULL)
+        {
+          GtkTreeIter parent_iter;
+
+          parent_iter.stamp = tree_store->stamp;
+          parent_iter.user_data = parent_node;
+
+          gtk_tree_path_up (path);
+          gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
+        }
+    }
+
   gtk_tree_path_free (path);
 
-  validate_tree ((GtkTreeStore*)tree_store);
+  validate_tree (tree_store);
 }
 
 /**
@@ -1203,10 +1201,10 @@ gtk_tree_store_insert_before (GtkTreeStore *tree_store,
  * @sibling: A valid #GtkTreeIter, or %NULL
  *
  * Inserts a new row after @sibling.  If @sibling is %NULL, then the row will be
- * prepended to the beginning of the @parent 's children.  If @parent and
- * @sibling are %NULL, then the row will be prepended to the toplevel.  If both
- * @sibling and @parent are set, then @parent must be the parent of @sibling.
- * When @sibling is set, @parent is optional.
+ * prepended to @parent 's children.  If @parent and @sibling are %NULL, then
+ * the row will be prepended to the toplevel.  If both @sibling and @parent are
+ * set, then @parent must be the parent of @sibling.  When @sibling is set,
+ * @parent is optional.
  *
  * @iter will be changed to point to this new row.  The row will be empty after
  * this function is called.  To fill in values, you need to call
@@ -1230,10 +1228,6 @@ gtk_tree_store_insert_after (GtkTreeStore *tree_store,
   if (sibling != NULL)
     g_return_if_fail (VALID_ITER (sibling, tree_store));
 
-  tree_store->columns_dirty = TRUE;
-
-  new_node = g_node_new (NULL);
-
   if (parent == NULL && sibling == NULL)
     parent_node = tree_store->root;
   else if (parent == NULL)
@@ -1247,6 +1241,9 @@ gtk_tree_store_insert_after (GtkTreeStore *tree_store,
       parent_node = G_NODE (parent->user_data);
     }
 
+  tree_store->columns_dirty = TRUE;
+
+  new_node = g_node_new (NULL);
 
   g_node_insert_after (parent_node,
                       sibling ? G_NODE (sibling->user_data) : NULL,
@@ -1258,91 +1255,277 @@ gtk_tree_store_insert_after (GtkTreeStore *tree_store,
   path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
   gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
 
+  if (parent_node != tree_store->root)
+    {
+      if (new_node->prev == NULL && new_node->next == NULL)
+        {
+          GtkTreeIter parent_iter;
+
+          parent_iter.stamp = tree_store->stamp;
+          parent_iter.user_data = parent_node;
+
+          gtk_tree_path_up (path);
+          gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
+        }
+    }
+
   gtk_tree_path_free (path);
 
-  validate_tree ((GtkTreeStore*)tree_store);
+  validate_tree (tree_store);
 }
 
 /**
- * gtk_tree_store_prepend:
+ * gtk_tree_store_insert_with_values:
  * @tree_store: A #GtkTreeStore
- * @iter: An unset #GtkTreeIter to set to the prepended row
+ * @iter: An unset #GtkTreeIter to set the new row
  * @parent: A valid #GtkTreeIter, or %NULL
- * 
- * Prepends a new row to @tree_store.  If @parent is non-%NULL, then it will prepend
- * the new row before the first child of @parent, otherwise it will prepend a row
- * to the top level.  @iter will be changed to point to this new row.  The row
- * will be empty after this function is called.  To fill in values, you need to
- * call gtk_tree_store_set() or gtk_tree_store_set_value().
- **/
+ * @position: position to insert the new row
+ * @Varargs: pairs of column number and value, terminated with -1
+ *
+ * Creates a new row at @position.  @iter will be changed to point to this
+ * new row.  If @position is larger than the number of rows on the list, then
+ * the new row will be appended to the list.  The row will be filled with
+ * the values given to this function.
+ *
+ * Calling
+ * <literal>gtk_tree_store_insert_with_values (tree_store, iter, position, ...)</literal>
+ * has the same effect as calling
+ * <informalexample><programlisting>
+ * gtk_tree_store_insert (tree_store, iter, position);
+ * gtk_tree_store_set (tree_store, iter, ...);
+ * </programlisting></informalexample>
+ * with the different that the former will only emit a row_inserted signal,
+ * while the latter will emit row_inserted, row_changed and if the tree store
+ * is sorted, rows_reordered.  Since emitting the rows_reordered signal
+ * repeatedly can affect the performance of the program,
+ * gtk_tree_store_insert_with_values() should generally be preferred when
+ * inserting rows in a sorted tree store.
+ *
+ * Since: 2.10
+ */
 void
-gtk_tree_store_prepend (GtkTreeStore *tree_store,
-                       GtkTreeIter  *iter,
-                       GtkTreeIter  *parent)
+gtk_tree_store_insert_with_values (GtkTreeStore *tree_store,
+                                  GtkTreeIter  *iter,
+                                  GtkTreeIter  *parent,
+                                  gint          position,
+                                  ...)
 {
+  GtkTreePath *path;
   GNode *parent_node;
+  GNode *new_node;
+  va_list var_args;
+  gboolean changed = FALSE;
+  gboolean maybe_need_sort = FALSE;
 
   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
   g_return_if_fail (iter != NULL);
-  if (parent != NULL)
+  if (parent)
     g_return_if_fail (VALID_ITER (parent, tree_store));
 
+  if (parent)
+    parent_node = parent->user_data;
+  else
+    parent_node = tree_store->root;
+
   tree_store->columns_dirty = TRUE;
 
-  if (parent == NULL)
-    parent_node = tree_store->root;
-  else
-    parent_node = parent->user_data;
+  new_node = g_node_new (NULL);
 
-  if (parent_node->children == NULL)
-    {
-      GtkTreePath *path;
-      
-      iter->stamp = tree_store->stamp;
-      iter->user_data = g_node_new (NULL);
+  iter->stamp = tree_store->stamp;
+  iter->user_data = new_node;
+  g_node_insert (parent_node, position, new_node);
 
-      g_node_prepend (parent_node, G_NODE (iter->user_data));
+  va_start (var_args, position);
+  gtk_tree_store_set_valist_internal (tree_store, iter,
+                                     &changed, &maybe_need_sort,
+                                     var_args);
+  va_end (var_args);
 
-      path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
-      gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
+  if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
+    gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
 
-      if (parent_node != tree_store->root)
-       {
+  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
+  gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
+
+  if (parent_node != tree_store->root)
+    {
+      if (new_node->prev == NULL && new_node->next == NULL)
+        {
          gtk_tree_path_up (path);
          gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
        }
-      gtk_tree_path_free (path);
-    }
-  else
-    {
-      gtk_tree_store_insert_after (tree_store, iter, parent, NULL);
     }
 
-  validate_tree ((GtkTreeStore*)tree_store);
+  gtk_tree_path_free (path);
+
+  validate_tree ((GtkTreeStore *)tree_store);
 }
 
 /**
- * gtk_tree_store_append:
+ * gtk_tree_store_insert_with_valuesv:
  * @tree_store: A #GtkTreeStore
- * @iter: An unset #GtkTreeIter to set to the appended row
+ * @iter: An unset #GtkTreeIter to set the new row
  * @parent: A valid #GtkTreeIter, or %NULL
- * 
- * Appends a new row to @tree_store.  If @parent is non-%NULL, then it will append the
- * new row after the last child of @parent, otherwise it will append a row to
- * the top level.  @iter will be changed to point to this new row.  The row will
- * be empty after this function is called.  To fill in values, you need to call
- * gtk_tree_store_set() or gtk_tree_store_set_value().
- **/
+ * @position: position to insert the new row
+ * @columns: an array of column numbers
+ * @values: an array of GValues
+ * @n_values: the length of the @columns and @values arrays
+ *
+ * A variant of gtk_tree_store_insert_with_values() which takes
+ * the columns and values as two arrays, instead of varargs.  This
+ * function is mainly intended for language bindings.
+ *
+ * Since: 2.10
+ */
 void
-gtk_tree_store_append (GtkTreeStore *tree_store,
-                      GtkTreeIter  *iter,
-                      GtkTreeIter  *parent)
+gtk_tree_store_insert_with_valuesv (GtkTreeStore *tree_store,
+                                   GtkTreeIter  *iter,
+                                   GtkTreeIter  *parent,
+                                   gint          position,
+                                   gint         *columns,
+                                   GValue       *values,
+                                   gint          n_values)
 {
+  GtkTreePath *path;
   GNode *parent_node;
+  GNode *new_node;
+  gboolean changed = FALSE;
+  gboolean maybe_need_sort = FALSE;
+  GtkTreeIterCompareFunc func = NULL;
+  gint i;
 
   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
   g_return_if_fail (iter != NULL);
+  if (parent)
+    g_return_if_fail (VALID_ITER (parent, tree_store));
 
+  if (parent)
+    parent_node = parent->user_data;
+  else
+    parent_node = tree_store->root;
+
+  tree_store->columns_dirty = TRUE;
+
+  new_node = g_node_new (NULL);
+
+  iter->stamp = tree_store->stamp;
+  iter->user_data = new_node;
+  g_node_insert (parent_node, position, new_node);
+
+  func = gtk_tree_store_get_compare_func (tree_store);
+  if (func != _gtk_tree_data_list_compare_func)
+    maybe_need_sort = TRUE;
+
+  for (i = 0; i < n_values; i++)
+    {
+      changed = gtk_tree_store_real_set_value (tree_store, iter,
+                                              columns[i], &values[i],
+                                              FALSE) || changed;
+
+      if (func == _gtk_tree_data_list_compare_func &&
+         columns[i] == tree_store->sort_column_id)
+       maybe_need_sort = TRUE;
+    }
+
+  if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
+    gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
+
+  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
+  gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
+
+  if (parent_node != tree_store->root)
+    {
+      if (new_node->prev == NULL && new_node->next == NULL)
+        {
+         gtk_tree_path_up (path);
+         gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
+       }
+    }
+
+  gtk_tree_path_free (path);
+
+  validate_tree ((GtkTreeStore *)tree_store);
+}
+
+/**
+ * gtk_tree_store_prepend:
+ * @tree_store: A #GtkTreeStore
+ * @iter: An unset #GtkTreeIter to set to the prepended row
+ * @parent: A valid #GtkTreeIter, or %NULL
+ * 
+ * Prepends a new row to @tree_store.  If @parent is non-%NULL, then it will prepend
+ * the new row before the first child of @parent, otherwise it will prepend a row
+ * to the top level.  @iter will be changed to point to this new row.  The row
+ * will be empty after this function is called.  To fill in values, you need to
+ * call gtk_tree_store_set() or gtk_tree_store_set_value().
+ **/
+void
+gtk_tree_store_prepend (GtkTreeStore *tree_store,
+                       GtkTreeIter  *iter,
+                       GtkTreeIter  *parent)
+{
+  GNode *parent_node;
+
+  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+  g_return_if_fail (iter != NULL);
+  if (parent != NULL)
+    g_return_if_fail (VALID_ITER (parent, tree_store));
+
+  tree_store->columns_dirty = TRUE;
+
+  if (parent == NULL)
+    parent_node = tree_store->root;
+  else
+    parent_node = parent->user_data;
+
+  if (parent_node->children == NULL)
+    {
+      GtkTreePath *path;
+      
+      iter->stamp = tree_store->stamp;
+      iter->user_data = g_node_new (NULL);
+
+      g_node_prepend (parent_node, G_NODE (iter->user_data));
+
+      path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
+      gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
+
+      if (parent_node != tree_store->root)
+       {
+         gtk_tree_path_up (path);
+         gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
+       }
+      gtk_tree_path_free (path);
+    }
+  else
+    {
+      gtk_tree_store_insert_after (tree_store, iter, parent, NULL);
+    }
+
+  validate_tree (tree_store);
+}
+
+/**
+ * gtk_tree_store_append:
+ * @tree_store: A #GtkTreeStore
+ * @iter: An unset #GtkTreeIter to set to the appended row
+ * @parent: A valid #GtkTreeIter, or %NULL
+ * 
+ * Appends a new row to @tree_store.  If @parent is non-%NULL, then it will append the
+ * new row after the last child of @parent, otherwise it will append a row to
+ * the top level.  @iter will be changed to point to this new row.  The row will
+ * be empty after this function is called.  To fill in values, you need to call
+ * gtk_tree_store_set() or gtk_tree_store_set_value().
+ **/
+void
+gtk_tree_store_append (GtkTreeStore *tree_store,
+                      GtkTreeIter  *iter,
+                      GtkTreeIter  *parent)
+{
+  GNode *parent_node;
+
+  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+  g_return_if_fail (iter != NULL);
   if (parent != NULL)
     g_return_if_fail (VALID_ITER (parent, tree_store));
 
@@ -1377,7 +1560,7 @@ gtk_tree_store_append (GtkTreeStore *tree_store,
       gtk_tree_store_insert_before (tree_store, iter, parent, NULL);
     }
 
-  validate_tree ((GtkTreeStore*)tree_store);
+  validate_tree (tree_store);
 }
 
 /**
@@ -1427,7 +1610,7 @@ gtk_tree_store_iter_depth (GtkTreeStore *tree_store,
 
 /* simple ripoff from g_node_traverse_post_order */
 static gboolean
-gtk_tree_store_clear_traverse (GNode *node,
+gtk_tree_store_clear_traverse (GNode        *node,
                               GtkTreeStore *store)
 {
   GtkTreeIter iter;
@@ -1466,6 +1649,16 @@ gtk_tree_store_clear_traverse (GNode *node,
   return FALSE;
 }
 
+static void
+gtk_tree_store_increment_stamp (GtkTreeStore *tree_store)
+{
+  do
+    {
+      tree_store->stamp++;
+    }
+  while (tree_store->stamp == 0);
+}
+
 /**
  * gtk_tree_store_clear:
  * @tree_store: a #GtkTreeStore
@@ -1478,20 +1671,76 @@ gtk_tree_store_clear (GtkTreeStore *tree_store)
   g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
 
   gtk_tree_store_clear_traverse (tree_store->root, tree_store);
+  gtk_tree_store_increment_stamp (tree_store);
+}
+
+static gboolean
+gtk_tree_store_iter_is_valid_helper (GtkTreeIter *iter,
+                                    GNode       *first)
+{
+  GNode *node;
+
+  node = first;
+
+  do
+    {
+      if (node == iter->user_data)
+       return TRUE;
+
+      if (node->children)
+       if (gtk_tree_store_iter_is_valid_helper (iter, node->children))
+         return TRUE;
+
+      node = node->next;
+    }
+  while (node);
+
+  return FALSE;
+}
+
+/**
+ * gtk_tree_store_iter_is_valid:
+ * @tree_store: A #GtkTreeStore.
+ * @iter: A #GtkTreeIter.
+ *
+ * WARNING: This function is slow. Only use it for debugging and/or testing
+ * purposes.
+ *
+ * Checks if the given iter is a valid iter for this #GtkTreeStore.
+ *
+ * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
+ *
+ * Since: 2.2
+ **/
+gboolean
+gtk_tree_store_iter_is_valid (GtkTreeStore *tree_store,
+                              GtkTreeIter  *iter)
+{
+  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
+  g_return_val_if_fail (iter != NULL, FALSE);
+
+  if (!VALID_ITER (iter, tree_store))
+    return FALSE;
+
+  return gtk_tree_store_iter_is_valid_helper (iter, tree_store->root);
 }
 
 /* DND */
 
 
+static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
+                                                   GtkTreePath       *path)
+{
+  return TRUE;
+}
+               
 static gboolean
 gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
                                  GtkTreePath       *path)
 {
   GtkTreeIter iter;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (drag_source), FALSE);
-
-  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
+  if (gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_source),
                                &iter,
                                path))
     {
@@ -1510,8 +1759,6 @@ gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
                               GtkTreePath       *path,
                               GtkSelectionData  *selection_data)
 {
-  g_return_val_if_fail (GTK_IS_TREE_STORE (drag_source), FALSE);
-
   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
    * target, because the default handler does it for us, but
    * we do anyway for the convenience of someone maybe overriding the
@@ -1580,7 +1827,7 @@ recursive_node_copy (GtkTreeStore *tree_store,
 
   copy_node_data (tree_store, src_iter, dest_iter);
 
-  if (gtk_tree_model_iter_children (model,
+  if (gtk_tree_store_iter_children (model,
                                     &child,
                                     src_iter))
     {
@@ -1598,7 +1845,7 @@ recursive_node_copy (GtkTreeStore *tree_store,
 
           recursive_node_copy (tree_store, &child, &copy);
         }
-      while (gtk_tree_model_iter_next (model, &child));
+      while (gtk_tree_store_iter_next (model, &child));
     }
 }
 
@@ -1613,8 +1860,6 @@ gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
   GtkTreePath *src_path = NULL;
   gboolean retval = FALSE;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (drag_dest), FALSE);
-
   tree_model = GTK_TREE_MODEL (drag_dest);
   tree_store = GTK_TREE_STORE (drag_dest);
 
@@ -1630,7 +1875,7 @@ gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
       GtkTreeIter dest_iter;
       GtkTreePath *prev;
 
-      if (!gtk_tree_model_get_iter (src_model,
+      if (!gtk_tree_store_get_iter (src_model,
                                     &src_iter,
                                     src_path))
         {
@@ -1653,9 +1898,10 @@ gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
           /* Get the parent, NULL if parent is the root */
           dest_parent_p = NULL;
           parent = gtk_tree_path_copy (dest);
-          if (gtk_tree_path_up (parent))
+          if (gtk_tree_path_up (parent) &&
+             gtk_tree_path_get_depth (parent) > 0)
             {
-              gtk_tree_model_get_iter (tree_model,
+              gtk_tree_store_get_iter (tree_model,
                                        &dest_parent,
                                        parent);
               dest_parent_p = &dest_parent;
@@ -1663,7 +1909,7 @@ gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
           gtk_tree_path_free (parent);
           parent = NULL;
 
-          gtk_tree_store_prepend (GTK_TREE_STORE (tree_model),
+          gtk_tree_store_prepend (tree_store,
                                   &dest_iter,
                                   dest_parent_p);
 
@@ -1671,17 +1917,14 @@ gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
         }
       else
         {
-          if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
-                                       &dest_iter,
-                                       prev))
+          if (gtk_tree_store_get_iter (tree_model, &dest_iter, prev))
             {
               GtkTreeIter tmp_iter = dest_iter;
-              gtk_tree_store_insert_after (GTK_TREE_STORE (tree_model),
-                                           &dest_iter,
-                                           NULL,
+
+              gtk_tree_store_insert_after (tree_store, &dest_iter, NULL,
                                            &tmp_iter);
-              retval = TRUE;
 
+              retval = TRUE;
             }
         }
 
@@ -1724,6 +1967,10 @@ gtk_tree_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
   GtkTreePath *tmp = NULL;
   gboolean retval = FALSE;
   
+  /* don't accept drops if the tree has been sorted */
+  if (GTK_TREE_STORE_IS_SORTED (drag_dest))
+    return FALSE;
+
   if (!gtk_tree_get_row_drag_data (selection_data,
                                   &src_model,
                                   &src_path))
@@ -1747,7 +1994,7 @@ gtk_tree_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
        tmp = gtk_tree_path_copy (dest_path);
        gtk_tree_path_up (tmp);
        
-       if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_dest),
+       if (!gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_dest),
                                      &iter, tmp))
          goto out;
       }
@@ -1766,13 +2013,625 @@ gtk_tree_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
   return retval;
 }
 
-/* Sorting */
+/* Sorting and reordering */
 typedef struct _SortTuple
 {
   gint offset;
   GNode *node;
 } SortTuple;
 
+/* Reordering */
+static gint
+gtk_tree_store_reorder_func (gconstpointer a,
+                            gconstpointer b,
+                            gpointer      user_data)
+{
+  SortTuple *a_reorder;
+  SortTuple *b_reorder;
+
+  a_reorder = (SortTuple *)a;
+  b_reorder = (SortTuple *)b;
+
+  if (a_reorder->offset < b_reorder->offset)
+    return -1;
+  if (a_reorder->offset > b_reorder->offset)
+    return 1;
+
+  return 0;
+}
+
+/**
+ * gtk_tree_store_reorder:
+ * @tree_store: A #GtkTreeStore.
+ * @parent: A #GtkTreeIter.
+ * @new_order: an array of integers mapping the new position of each child
+ *      to its old position before the re-ordering,
+ *      i.e. @new_order<literal>[newpos] = oldpos</literal>.
+ *
+ * Reorders the children of @parent in @tree_store to follow the order
+ * indicated by @new_order. Note that this function only works with
+ * unsorted stores.
+ *
+ * Since: 2.2
+ **/
+void
+gtk_tree_store_reorder (GtkTreeStore *tree_store,
+                       GtkTreeIter  *parent,
+                       gint         *new_order)
+{
+  gint i, length = 0;
+  GNode *level, *node;
+  GtkTreePath *path;
+  SortTuple *sort_array;
+
+  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+  g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
+  g_return_if_fail (parent == NULL || VALID_ITER (parent, tree_store));
+  g_return_if_fail (new_order != NULL);
+
+  if (!parent)
+    level = G_NODE (tree_store->root)->children;
+  else
+    level = G_NODE (parent->user_data)->children;
+
+  /* count nodes */
+  node = level;
+  while (node)
+    {
+      length++;
+      node = node->next;
+    }
+
+  /* set up sortarray */
+  sort_array = g_new (SortTuple, length);
+
+  node = level;
+  for (i = 0; i < length; i++)
+    {
+      sort_array[new_order[i]].offset = i;
+      sort_array[i].node = node;
+
+      node = node->next;
+    }
+
+  g_qsort_with_data (sort_array,
+                    length,
+                    sizeof (SortTuple),
+                    gtk_tree_store_reorder_func,
+                    NULL);
+
+  /* fix up level */
+  for (i = 0; i < length - 1; i++)
+    {
+      sort_array[i].node->next = sort_array[i+1].node;
+      sort_array[i+1].node->prev = sort_array[i].node;
+    }
+
+  sort_array[length-1].node->next = NULL;
+  sort_array[0].node->prev = NULL;
+  if (parent)
+    G_NODE (parent->user_data)->children = sort_array[0].node;
+  else
+    G_NODE (tree_store->root)->children = sort_array[0].node;
+
+  /* emit signal */
+  if (parent)
+    path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), parent);
+  else
+    path = gtk_tree_path_new ();
+  gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path,
+                                parent, new_order);
+  gtk_tree_path_free (path);
+  g_free (sort_array);
+}
+
+/**
+ * gtk_tree_store_swap:
+ * @tree_store: A #GtkTreeStore.
+ * @a: A #GtkTreeIter.
+ * @b: Another #GtkTreeIter.
+ *
+ * Swaps @a and @b in the same level of @tree_store. Note that this function
+ * only works with unsorted stores.
+ *
+ * Since: 2.2
+ **/
+void
+gtk_tree_store_swap (GtkTreeStore *tree_store,
+                    GtkTreeIter  *a,
+                    GtkTreeIter  *b)
+{
+  GNode *tmp, *node_a, *node_b, *parent_node;
+  GNode *a_prev, *a_next, *b_prev, *b_next;
+  gint i, a_count, b_count, length, *order;
+  GtkTreePath *path_a, *path_b;
+  GtkTreeIter parent;
+
+  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+  g_return_if_fail (VALID_ITER (a, tree_store));
+  g_return_if_fail (VALID_ITER (b, tree_store));
+
+  node_a = G_NODE (a->user_data);
+  node_b = G_NODE (b->user_data);
+
+  /* basic sanity checking */
+  if (node_a == node_b)
+    return;
+
+  path_a = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), a);
+  path_b = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), b);
+
+  g_return_if_fail (path_a && path_b);
+
+  gtk_tree_path_up (path_a);
+  gtk_tree_path_up (path_b);
+
+  if (gtk_tree_path_get_depth (path_a) == 0
+      || gtk_tree_path_get_depth (path_b) == 0)
+    {
+      if (gtk_tree_path_get_depth (path_a) != gtk_tree_path_get_depth (path_b))
+        {
+          gtk_tree_path_free (path_a);
+          gtk_tree_path_free (path_b);
+                                                                                
+          g_warning ("Given children are not in the same level\n");
+          return;
+        }
+      parent_node = G_NODE (tree_store->root);
+    }
+  else
+    {
+      if (gtk_tree_path_compare (path_a, path_b))
+        {
+          gtk_tree_path_free (path_a);
+          gtk_tree_path_free (path_b);
+                                                                                
+          g_warning ("Given children don't have a common parent\n");
+          return;
+        }
+      gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &parent,
+                               path_a);
+      parent_node = G_NODE (parent.user_data);
+    }
+  gtk_tree_path_free (path_b);
+
+  /* old links which we have to keep around */
+  a_prev = node_a->prev;
+  a_next = node_a->next;
+
+  b_prev = node_b->prev;
+  b_next = node_b->next;
+
+  /* fix up links if the nodes are next to eachother */
+  if (a_prev == node_b)
+    a_prev = node_a;
+  if (a_next == node_b)
+    a_next = node_a;
+
+  if (b_prev == node_a)
+    b_prev = node_b;
+  if (b_next == node_a)
+    b_next = node_b;
+
+  /* counting nodes */
+  tmp = parent_node->children;
+  i = a_count = b_count = 0;
+  while (tmp)
+    {
+      if (tmp == node_a)
+       a_count = i;
+      if (tmp == node_b)
+       b_count = i;
+
+      tmp = tmp->next;
+      i++;
+    }
+  length = i;
+
+  /* hacking the tree */
+  if (!a_prev)
+    parent_node->children = node_b;
+  else
+    a_prev->next = node_b;
+
+  if (a_next)
+    a_next->prev = node_b;
+
+  if (!b_prev)
+    parent_node->children = node_a;
+  else
+    b_prev->next = node_a;
+
+  if (b_next)
+    b_next->prev = node_a;
+
+  node_a->prev = b_prev;
+  node_a->next = b_next;
+
+  node_b->prev = a_prev;
+  node_b->next = a_next;
+
+  /* emit signal */
+  order = g_new (gint, length);
+  for (i = 0; i < length; i++)
+    if (i == a_count)
+      order[i] = b_count;
+    else if (i == b_count)
+      order[i] = a_count;
+    else
+      order[i] = i;
+
+  gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path_a,
+                                parent_node == tree_store->root 
+                                ? NULL : &parent, order);
+  gtk_tree_path_free (path_a);
+  g_free (order);
+}
+
+/* WARNING: this function is *incredibly* fragile. Please smashtest after
+ * making changes here.
+ *     -Kris
+ */
+static void
+gtk_tree_store_move (GtkTreeStore *tree_store,
+                     GtkTreeIter  *iter,
+                    GtkTreeIter  *position,
+                    gboolean      before)
+{
+  GNode *parent, *node, *a, *b, *tmp, *tmp_a, *tmp_b;
+  gint old_pos, new_pos, length, i, *order;
+  GtkTreePath *path = NULL, *tmppath, *pos_path = NULL;
+  GtkTreeIter parent_iter, dst_a, dst_b;
+  gint depth = 0;
+  gboolean handle_b = TRUE;
+
+  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+  g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
+  g_return_if_fail (VALID_ITER (iter, tree_store));
+  if (position)
+    g_return_if_fail (VALID_ITER (position, tree_store));
+
+  a = b = NULL;
+
+  /* sanity checks */
+  if (position)
+    {
+      path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
+      pos_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
+                                         position);
+
+      /* if before:
+       *   moving the iter before path or "path + 1" doesn't make sense
+       * else
+       *   moving the iter before path or "path - 1" doesn't make sense
+       */
+      if (!gtk_tree_path_compare (path, pos_path))
+       goto free_paths_and_out;
+
+      if (before)
+        gtk_tree_path_next (path);
+      else
+        gtk_tree_path_prev (path);
+
+      if (!gtk_tree_path_compare (path, pos_path))
+       goto free_paths_and_out;
+
+      if (before)
+        gtk_tree_path_prev (path);
+      else
+        gtk_tree_path_next (path);
+
+      if (gtk_tree_path_get_depth (path) != gtk_tree_path_get_depth (pos_path))
+        {
+          g_warning ("Given children are not in the same level\n");
+
+         goto free_paths_and_out;
+        }
+
+      tmppath = gtk_tree_path_copy (pos_path);
+      gtk_tree_path_up (path);
+      gtk_tree_path_up (tmppath);
+
+      if (gtk_tree_path_get_depth (path) > 0 &&
+         gtk_tree_path_compare (path, tmppath))
+        {
+          g_warning ("Given children are not in the same level\n");
+
+          gtk_tree_path_free (tmppath);
+         goto free_paths_and_out;
+        }
+
+      gtk_tree_path_free (tmppath);
+    }
+
+  if (!path)
+    {
+      path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
+      gtk_tree_path_up (path);
+    }
+
+  depth = gtk_tree_path_get_depth (path);
+
+  if (depth)
+    {
+      gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), 
+                              &parent_iter, path);
+
+      parent = G_NODE (parent_iter.user_data);
+    }
+  else
+    parent = G_NODE (tree_store->root);
+
+  /* yes, I know that this can be done shorter, but I'm doing it this way
+   * so the code is also maintainable
+   */
+
+  if (before && position)
+    {
+      b = G_NODE (position->user_data);
+
+      if (gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path) - 1] > 0)
+        {
+          gtk_tree_path_prev (pos_path);
+          if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), 
+                                      &dst_a, pos_path))
+            a = G_NODE (dst_a.user_data);
+          else
+            a = NULL;
+          gtk_tree_path_next (pos_path);
+       }
+
+      /* if b is NULL, a is NULL too -- we are at the beginning of the list
+       * yes and we leak memory here ...
+       */
+      g_return_if_fail (b);
+    }
+  else if (before && !position)
+    {
+      /* move before without position is appending */
+      a = NULL;
+      b = NULL;
+    }
+  else /* !before */
+    {
+      if (position)
+        a = G_NODE (position->user_data);
+      else
+        a = NULL;
+
+      if (position)
+        {
+          gtk_tree_path_next (pos_path);
+          if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &dst_b, pos_path))
+             b = G_NODE (dst_b.user_data);
+          else
+             b = NULL;
+          gtk_tree_path_prev (pos_path);
+       }
+      else
+        {
+         /* move after without position is prepending */
+         if (depth)
+           gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
+                                         &parent_iter);
+         else
+           gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
+                                         NULL);
+
+         b = G_NODE (dst_b.user_data);
+       }
+
+      /* if a is NULL, b is NULL too -- we are at the end of the list
+       * yes and we leak memory here ...
+       */
+      if (position)
+        g_return_if_fail (a);
+    }
+
+  /* counting nodes */
+  tmp = parent->children;
+
+  length = old_pos = 0;
+  while (tmp)
+    {
+      if (tmp == iter->user_data)
+       old_pos = length;
+
+      tmp = tmp->next;
+      length++;
+    }
+
+  /* remove node from list */
+  node = G_NODE (iter->user_data);
+  tmp_a = node->prev;
+  tmp_b = node->next;
+
+  if (tmp_a)
+    tmp_a->next = tmp_b;
+  else
+    parent->children = tmp_b;
+
+  if (tmp_b)
+    tmp_b->prev = tmp_a;
+
+  /* and reinsert the node */
+  if (a)
+    {
+      tmp = a->next;
+
+      a->next = node;
+      node->next = tmp;
+      node->prev = a;
+    }
+  else if (!a && !before)
+    {
+      tmp = parent->children;
+
+      node->prev = NULL;
+      parent->children = node;
+
+      node->next = tmp;
+      if (tmp) 
+       tmp->prev = node;
+
+      handle_b = FALSE;
+    }
+  else if (!a && before)
+    {
+      if (!position)
+        {
+          node->parent = NULL;
+          node->next = node->prev = NULL;
+
+          /* before with sibling = NULL appends */
+          g_node_insert_before (parent, NULL, node);
+       }
+      else
+        {
+         node->parent = NULL;
+         node->next = node->prev = NULL;
+
+         /* after with sibling = NULL prepends */
+         g_node_insert_after (parent, NULL, node);
+       }
+
+      handle_b = FALSE;
+    }
+
+  if (handle_b)
+    {
+      if (b)
+        {
+          tmp = b->prev;
+
+          b->prev = node;
+          node->prev = tmp;
+          node->next = b;
+        }
+      else if (!(!a && before)) /* !a && before is completely handled above */
+        node->next = NULL;
+    }
+
+  /* emit signal */
+  if (position)
+    new_pos = gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path)-1];
+  else if (before)
+    {
+      if (depth)
+        new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
+                                                 &parent_iter) - 1;
+      else
+       new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
+                                                 NULL) - 1;
+    }
+  else
+    new_pos = 0;
+
+  if (new_pos > old_pos)
+    {
+      if (before && position)
+        new_pos--;
+    }
+  else
+    {
+      if (!before && position)
+        new_pos++;
+    }
+
+  order = g_new (gint, length);
+  if (new_pos > old_pos)
+    {
+      for (i = 0; i < length; i++)
+        if (i < old_pos)
+          order[i] = i;
+        else if (i >= old_pos && i < new_pos)
+          order[i] = i + 1;
+        else if (i == new_pos)
+          order[i] = old_pos;
+        else
+         order[i] = i;
+    }
+  else
+    {
+      for (i = 0; i < length; i++)
+        if (i == new_pos)
+         order[i] = old_pos;
+        else if (i > new_pos && i <= old_pos)
+         order[i] = i - 1;
+       else
+         order[i] = i;
+    }
+
+  if (depth)
+    {
+      tmppath = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), 
+                                        &parent_iter);
+      gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
+                                    tmppath, &parent_iter, order);
+    }
+  else
+    {
+      tmppath = gtk_tree_path_new ();
+      gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
+                                    tmppath, NULL, order);
+    }
+
+  gtk_tree_path_free (tmppath);
+  gtk_tree_path_free (path);
+  if (position)
+    gtk_tree_path_free (pos_path);
+  g_free (order);
+
+  return;
+
+free_paths_and_out:
+  gtk_tree_path_free (path);
+  gtk_tree_path_free (pos_path);
+}
+
+/**
+ * gtk_tree_store_move_before:
+ * @tree_store: A #GtkTreeStore.
+ * @iter: A #GtkTreeIter.
+ * @position: A #GtkTreeIter or %NULL.
+ *
+ * Moves @iter in @tree_store to the position before @position. @iter and
+ * @position should be in the same level. Note that this function only
+ * works with unsorted stores. If @position is %NULL, @iter will be
+ * moved to the end of the level.
+ *
+ * Since: 2.2
+ **/
+void
+gtk_tree_store_move_before (GtkTreeStore *tree_store,
+                            GtkTreeIter  *iter,
+                           GtkTreeIter  *position)
+{
+  gtk_tree_store_move (tree_store, iter, position, TRUE);
+}
+
+/**
+ * gtk_tree_store_move_after:
+ * @tree_store: A #GtkTreeStore.
+ * @iter: A #GtkTreeIter.
+ * @position: A #GtkTreeIter.
+ *
+ * Moves @iter in @tree_store to the position after @position. @iter and
+ * @position should be in the same level. Note that this function only
+ * works with unsorted stores. If @position is %NULL, @iter will be moved
+ * to the start of the level.
+ *
+ * Since: 2.2
+ **/
+void
+gtk_tree_store_move_after (GtkTreeStore *tree_store,
+                           GtkTreeIter  *iter,
+                          GtkTreeIter  *position)
+{
+  gtk_tree_store_move (tree_store, iter, position, FALSE);
+}
+
+/* Sorting */
 static gint
 gtk_tree_store_compare_func (gconstpointer a,
                             gconstpointer b,
@@ -1843,9 +2702,12 @@ gtk_tree_store_sort_helper (GtkTreeStore *tree_store,
 
   node = parent->children;
   if (node == NULL || node->next == NULL)
-    return;
+    {
+      if (recurse && node && node->children)
+        gtk_tree_store_sort_helper (tree_store, node, TRUE);
 
-  g_assert (GTK_TREE_STORE_IS_SORTED (tree_store));
+      return;
+    }
 
   list_length = 0;
   for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
@@ -1905,11 +2767,15 @@ gtk_tree_store_sort_helper (GtkTreeStore *tree_store,
 static void
 gtk_tree_store_sort (GtkTreeStore *tree_store)
 {
+  if (!GTK_TREE_STORE_IS_SORTED (tree_store))
+    return;
+
   if (tree_store->sort_column_id != -1)
     {
       GtkTreeDataSortHeader *header = NULL;
 
-      header = _gtk_tree_data_list_get_header (tree_store->sort_list, tree_store->sort_column_id);
+      header = _gtk_tree_data_list_get_header (tree_store->sort_list, 
+                                              tree_store->sort_column_id);
 
       /* We want to make sure that we have a function */
       g_return_if_fail (header != NULL);
@@ -1926,7 +2792,8 @@ gtk_tree_store_sort (GtkTreeStore *tree_store)
 static void
 gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
                                  GtkTreeIter  *iter,
-                                 gint          column)
+                                 gint          column,
+                                 gboolean      emit_signal)
 {
   GNode *prev = NULL;
   GNode *next = NULL;
@@ -1964,7 +2831,7 @@ gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
     }
 
   /* If it's the built in function, we don't sort. */
-  if (func == gtk_tree_data_list_compare_func &&
+  if (func == _gtk_tree_data_list_compare_func &&
       tree_store->sort_column_id != column)
     return;
 
@@ -1996,7 +2863,6 @@ gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
       cmp_b = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
     }
 
-
   if (tree_store->order == GTK_SORT_DESCENDING)
     {
       if (cmp_a < 0)
@@ -2025,6 +2891,7 @@ gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
     prev->next = next;
   else
     node->parent->children = next;
+
   if (next)
     next->prev = prev;
 
@@ -2055,6 +2922,7 @@ gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
 
   if ((!node->next) && (cmp_a > 0))
     {
+      new_location++;
       node->next = G_NODE (iter->user_data);
       node->next->prev = node;
     }
@@ -2068,9 +2936,13 @@ gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
   else
     {
       G_NODE (iter->user_data)->next = G_NODE (iter->user_data)->parent->children;
+      G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
       G_NODE (iter->user_data)->parent->children = G_NODE (iter->user_data);
     }
 
+  if (!emit_signal)
+    return;
+
   /* Emit the reordered signal. */
   length = g_node_n_children (node->parent);
   new_order = g_new (int, length);
@@ -2118,17 +2990,16 @@ gtk_tree_store_get_sort_column_id (GtkTreeSortable  *sortable,
 {
   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (sortable), FALSE);
-
-  if (tree_store->sort_column_id == -1)
-    return FALSE;
-
   if (sort_column_id)
     * sort_column_id = tree_store->sort_column_id;
   if (order)
     * order = tree_store->order;
-  return TRUE;
 
+  if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
+      tree_store->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
+    return FALSE;
+
+  return TRUE;
 }
 
 static void
@@ -2138,34 +3009,36 @@ gtk_tree_store_set_sort_column_id (GtkTreeSortable  *sortable,
 {
   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
 
-  g_return_if_fail (GTK_IS_TREE_STORE (sortable));
-
   
   if ((tree_store->sort_column_id == sort_column_id) &&
       (tree_store->order == order))
     return;
 
-  if (sort_column_id != -1)
+  if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
     {
-      GtkTreeDataSortHeader *header = NULL;
+      if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
+       {
+         GtkTreeDataSortHeader *header = NULL;
 
-      header = _gtk_tree_data_list_get_header (tree_store->sort_list, sort_column_id);
+         header = _gtk_tree_data_list_get_header (tree_store->sort_list, 
+                                                  sort_column_id);
 
-      /* We want to make sure that we have a function */
-      g_return_if_fail (header != NULL);
-      g_return_if_fail (header->func != NULL);
-    }
-  else
-    {
-      g_return_if_fail (tree_store->default_sort_func != NULL);
+         /* We want to make sure that we have a function */
+         g_return_if_fail (header != NULL);
+         g_return_if_fail (header->func != NULL);
+       }
+      else
+       {
+         g_return_if_fail (tree_store->default_sort_func != NULL);
+       }
     }
 
   tree_store->sort_column_id = sort_column_id;
   tree_store->order = order;
 
-  gtk_tree_store_sort (tree_store);
-
   gtk_tree_sortable_sort_column_changed (sortable);
+
+  gtk_tree_store_sort (tree_store);
 }
 
 static void
@@ -2176,37 +3049,13 @@ gtk_tree_store_set_sort_func (GtkTreeSortable        *sortable,
                              GtkDestroyNotify        destroy)
 {
   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
-  GtkTreeDataSortHeader *header = NULL;
-  GList *list;
-
-  g_return_if_fail (GTK_IS_TREE_STORE (sortable));
-  g_return_if_fail (func != NULL);
-
-  for (list = tree_store->sort_list; list; list = list->next)
-    {
-      header = (GtkTreeDataSortHeader*) list->data;
-      if (header->sort_column_id == sort_column_id)
-       break;
-    }
-
-  if (header == NULL)
-    {
-      header = g_new0 (GtkTreeDataSortHeader, 1);
-      header->sort_column_id = sort_column_id;
-      tree_store->sort_list = g_list_append (tree_store->sort_list, header);
-    }
-
-  if (header->destroy)
-    {
-      GtkDestroyNotify d = header->destroy;
 
-      header->destroy = NULL;
-      d (header->data);
-    }
+  tree_store->sort_list = _gtk_tree_data_list_set_header (tree_store->sort_list, 
+                                                         sort_column_id, 
+                                                         func, data, destroy);
 
-  header->func = func;
-  header->data = data;
-  header->destroy = destroy;
+  if (tree_store->sort_column_id == sort_column_id)
+    gtk_tree_store_sort (tree_store);
 }
 
 static void
@@ -2217,8 +3066,6 @@ gtk_tree_store_set_default_sort_func (GtkTreeSortable        *sortable,
 {
   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
 
-  g_return_if_fail (GTK_IS_TREE_STORE (sortable));
-
   if (tree_store->default_sort_destroy)
     {
       GtkDestroyNotify d = tree_store->default_sort_destroy;
@@ -2230,6 +3077,9 @@ gtk_tree_store_set_default_sort_func (GtkTreeSortable        *sortable,
   tree_store->default_sort_func = func;
   tree_store->default_sort_data = data;
   tree_store->default_sort_destroy = destroy;
+
+  if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
+    gtk_tree_store_sort (tree_store);
 }
 
 static gboolean
@@ -2237,8 +3087,6 @@ gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable)
 {
   GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
 
-  g_return_val_if_fail (GTK_IS_TREE_STORE (sortable), FALSE);
-
   return (tree_store->default_sort_func != NULL);
 }
 
@@ -2258,5 +3106,5 @@ validate_gnode (GNode* node)
     }
 }
 
-
-
+#define __GTK_TREE_STORE_C__
+#include "gtkaliasdef.c"