]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktreemodelsort.c
don't bother inserting new rows in a level with a zero refcount and
[~andy/gtk] / gtk / gtktreemodelsort.c
index 61cb405f498cb82530480efd444c73e3b73bc747..ed1579421708433a6d068366729c1fdac6940993 100644 (file)
 #include <config.h>
 #include <string.h>
 
-#include "gtkalias.h"
 #include "gtktreemodelsort.h"
 #include "gtktreesortable.h"
 #include "gtktreestore.h"
 #include "gtktreedatalist.h"
 #include "gtkintl.h"
+#include "gtkprivate.h"
 #include "gtktreednd.h"
+#include "gtkalias.h"
 
 typedef struct _SortElt SortElt;
 typedef struct _SortLevel SortLevel;
@@ -100,7 +101,7 @@ enum {
 #define SORT_ELT(sort_elt) ((SortElt *)sort_elt)
 #define SORT_LEVEL(sort_level) ((SortLevel *)sort_level)
 
-#define GET_CHILD_ITER(tree_model_sort,child_iter,sort_iter) gtk_tree_model_sort_convert_iter_to_child_iter(GTK_TREE_MODEL_SORT (tree_model_sort), child_iter, sort_iter);
+#define GET_CHILD_ITER(tree_model_sort,ch_iter,so_iter) gtk_tree_model_sort_convert_iter_to_child_iter(GTK_TREE_MODEL_SORT (tree_model_sort), ch_iter, so_iter);
 
 #define NO_SORT_FUNC ((GtkTreeIterCompareFunc) 0x1)
 
@@ -330,7 +331,7 @@ gtk_tree_model_sort_class_init (GtkTreeModelSortClass *class)
                                                        P_("TreeModelSort Model"),
                                                        P_("The model for the TreeModelSort to sort"),
                                                        GTK_TYPE_TREE_MODEL,
-                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
@@ -374,7 +375,7 @@ gtk_tree_model_sort_drag_source_init (GtkTreeDragSourceIface *iface)
  * gtk_tree_model_sort_new_with_model:
  * @child_model: A #GtkTreeModel
  *
- * Creates a new #GtkTreeModel, with @child_model as the child_model.
+ * Creates a new #GtkTreeModel, with @child_model as the child model.
  *
  * Return value: A new #GtkTreeModel.
  */
@@ -493,6 +494,8 @@ gtk_tree_model_sort_row_changed (GtkTreeModel *s_model,
   level = iter.user_data;
   elt = iter.user_data2;
 
+  level->ref_count++;
+
   if (level->array->len < 2 ||
       (tree_model_sort->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID &&
        tree_model_sort->default_sort_func == NO_SORT_FUNC))
@@ -504,9 +507,11 @@ gtk_tree_model_sort_row_changed (GtkTreeModel *s_model,
 
       gtk_tree_path_free (path);
 
+      level->ref_count--;
+
       return;
     }
-
+  
   if (!GTK_TREE_MODEL_SORT_CACHE_CHILD_ITERS (tree_model_sort))
     {
       gtk_tree_model_get_iter (tree_model_sort->child_model,
@@ -609,6 +614,8 @@ gtk_tree_model_sort_row_changed (GtkTreeModel *s_model,
       g_free (new_order);
     }
 
+  level->ref_count--;
+
   /* emit row_changed signal (at new location) */
   gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
   gtk_tree_model_row_changed (GTK_TREE_MODEL (data), path, &iter);
@@ -721,6 +728,12 @@ gtk_tree_model_sort_row_inserted (GtkTreeModel          *s_model,
   if (!parent_level)
     goto done;
 
+  if (level->ref_count == 0 && level != tree_model_sort->root)
+    {
+      gtk_tree_model_sort_free_level (tree_model_sort, level);
+      goto done;
+    }
+
   if (!gtk_tree_model_sort_insert_value (tree_model_sort,
                                         parent_level,
                                         s_path,
@@ -809,12 +822,18 @@ gtk_tree_model_sort_row_deleted (GtkTreeModel *s_model,
 
   if (level->ref_count == 0)
     {
-      /* This will prune the level, so I can just emit the signal and not worry
-       * about cleaning this level up. */
+      /* This will prune the level, so I can just emit the signal and 
+       * not worry about cleaning this level up. 
+       * Careful, root level is not cleaned up in increment stamp.
+       */
       gtk_tree_model_sort_increment_stamp (tree_model_sort);
       gtk_tree_path_free (path);
       if (level == tree_model_sort->root)
-       tree_model_sort->root = NULL;
+       {
+         gtk_tree_model_sort_free_level (tree_model_sort, 
+                                         tree_model_sort->root);
+         tree_model_sort->root = NULL;
+       }
       return;
     }
 
@@ -1619,6 +1638,8 @@ gtk_tree_model_sort_sort_level (GtkTreeModelSort *tree_model_sort,
   if (level->array->len < 1 && !((SortElt *)level->array->data)->children)
     return;
 
+  level->ref_count++;
+
   /* Set up data */
   data.tree_model_sort = tree_model_sort;
   if (level->parent_elt)
@@ -1740,6 +1761,8 @@ gtk_tree_model_sort_sort_level (GtkTreeModelSort *tree_model_sort,
     }
 
   g_free (new_order);
+
+  level->ref_count--;
 }
 
 static void
@@ -1924,11 +1947,12 @@ gtk_tree_model_sort_elt_get_path (SortLevel *level,
 /**
  * gtk_tree_model_sort_set_model:
  * @tree_model_sort: The #GtkTreeModelSort.
- * @child_model: A #GtkTreeModel, or NULL.
+ * @child_model: A #GtkTreeModel, or %NULL.
  *
- * Sets the model of @tree_model_sort to be @model.  If @model is NULL, then the
- * old model is unset.  The sort function is unset as a result of this call.
- * The model will be in an unsorted state until a sort function is set.
+ * Sets the model of @tree_model_sort to be @model.  If @model is %NULL, 
+ * then the old model is unset.  The sort function is unset as a result 
+ * of this call. The model will be in an unsorted state until a sort 
+ * function is set.
  **/
 static void
 gtk_tree_model_sort_set_model (GtkTreeModelSort *tree_model_sort,
@@ -2090,8 +2114,8 @@ gtk_real_tree_model_sort_convert_child_path_to_path (GtkTreeModelSort *tree_mode
  * 
  * Converts @child_path to a path relative to @tree_model_sort.  That is,
  * @child_path points to a path in the child model.  The returned path will
- * point to the same row in the sorted model.  If @child_path isn't a valid path
- * on the child model, then %NULL is returned.
+ * point to the same row in the sorted model.  If @child_path isn't a valid 
+ * path on the child model, then %NULL is returned.
  * 
  * Return value: A newly allocated #GtkTreePath, or %NULL
  **/
@@ -2145,10 +2169,11 @@ gtk_tree_model_sort_convert_child_iter_to_iter (GtkTreeModelSort *tree_model_sor
  * @tree_model_sort: A #GtkTreeModelSort
  * @sorted_path: A #GtkTreePath to convert
  * 
- * Converts @sorted_path to a path on the child model of @tree_model_sort.  That
- * is, @sorted_path points to a location in @tree_model_sort.  The returned path
- * will point to the same location in the model not being sorted.  If @sorted_path 
- * does not point to a location in the child model, %NULL is returned.
+ * Converts @sorted_path to a path on the child model of @tree_model_sort.  
+ * That is, @sorted_path points to a location in @tree_model_sort.  The 
+ * returned path will point to the same location in the model not being 
+ * sorted.  If @sorted_path does not point to a location in the child model, 
+ * %NULL is returned.
  * 
  * Return value: A newly allocated #GtkTreePath, or %NULL
  **/
@@ -2313,7 +2338,7 @@ gtk_tree_model_sort_build_level (GtkTreeModelSort *tree_model_sort,
          if (gtk_tree_model_iter_next (tree_model_sort->child_model, &iter) == FALSE &&
              i < length - 1)
            {
-             g_warning ("There is a discrepency between the sort model and the child model.");
+             g_warning ("There is a discrepancy between the sort model and the child model.");
              return;
            }
        }
@@ -2480,8 +2505,9 @@ gtk_tree_model_sort_iter_is_valid_helper (GtkTreeIter *iter,
  * @tree_model_sort: A #GtkTreeModelSort.
  * @iter: A #GtkTreeIter.
  *
- * WARNING: This function is slow. Only use it for debugging and/or testing
- * purposes.
+ * <warning><para>
+ * This function is slow. Only use it for debugging and/or testing purposes.
+ * </para></warning>
  *
  * Checks if the given iter is a valid iter for this #GtkTreeModelSort.
  *
@@ -2502,3 +2528,6 @@ gtk_tree_model_sort_iter_is_valid (GtkTreeModelSort *tree_model_sort,
   return gtk_tree_model_sort_iter_is_valid_helper (iter,
                                                   tree_model_sort->root);
 }
+
+#define __GTK_TREE_MODEL_SORT_C__
+#include "gtkaliasdef.c"