[gtk+] Factor out find_elt_with_offset



commit f064d07bdae5a29a46270201d43ffd5884a49c2f
Author: Kristian Rietveld <kris gtk org>
Date:   Sat May 14 14:09:51 2011 +0200

    Factor out find_elt_with_offset
    
    Suggested by Xavier Claessens / bug 621076.

 gtk/gtktreemodelfilter.c |  157 +++++++++++++++++++++++++++-------------------
 1 files changed, 92 insertions(+), 65 deletions(-)
---
diff --git a/gtk/gtktreemodelfilter.c b/gtk/gtktreemodelfilter.c
index a55e5fe..b97156d 100644
--- a/gtk/gtktreemodelfilter.c
+++ b/gtk/gtktreemodelfilter.c
@@ -1298,6 +1298,50 @@ bsearch_elt_with_offset (GArray *array,
   return NULL;
 }
 
+/* Path is relative to the child model (this is on search on elt offset)
+ * but with the virtual root already removed if necesssary.
+ */
+static gboolean
+find_elt_with_offset (GtkTreeModelFilter *filter,
+                      GtkTreePath        *path,
+                      FilterLevel       **level_,
+                      FilterElt         **elt_)
+{
+  int i = 0;
+  FilterLevel *level;
+  FilterLevel *parent_level = NULL;
+  FilterElt *elt = NULL;
+
+  level = FILTER_LEVEL (filter->priv->root);
+
+  while (i < gtk_tree_path_get_depth (path))
+    {
+      int j;
+
+      if (!level)
+        return FALSE;
+
+      elt = bsearch_elt_with_offset (level->array,
+                                     gtk_tree_path_get_indices (path)[i],
+                                     &j);
+
+      if (!elt)
+        return FALSE;
+
+      parent_level = level;
+      level = elt->children;
+      i++;
+    }
+
+  if (level_)
+    *level_ = parent_level;
+
+  if (elt_)
+    *elt_ = elt;
+
+  return TRUE;
+}
+
 /* TreeModel signals */
 static void
 gtk_tree_model_filter_row_changed (GtkTreeModel *c_model,
@@ -1489,9 +1533,9 @@ gtk_tree_model_filter_row_inserted (GtkTreeModel *c_model,
 
   GtkTreeIter real_c_iter;
 
-  FilterElt *elt;
-  FilterLevel *level;
-  FilterLevel *parent_level;
+  FilterElt *elt = NULL;
+  FilterLevel *level = NULL;
+  FilterLevel *parent_level = NULL;
 
   gint i = 0, offset;
 
@@ -1555,8 +1599,6 @@ gtk_tree_model_filter_row_inserted (GtkTreeModel *c_model,
         goto done;
     }
 
-  parent_level = level = FILTER_LEVEL (filter->priv->root);
-
   /* subtract virtual root if necessary */
   if (filter->priv->virtual_root)
     {
@@ -1571,55 +1613,51 @@ gtk_tree_model_filter_row_inserted (GtkTreeModel *c_model,
 
   if (gtk_tree_path_get_depth (real_path) - 1 >= 1)
     {
-      /* find the parent level */
-      while (i < gtk_tree_path_get_depth (real_path) - 1)
-        {
-          gint j;
+      gboolean found = FALSE;
+      GtkTreePath *parent = gtk_tree_path_copy (real_path);
+      gtk_tree_path_up (parent);
 
-          if (!level)
-            /* we don't cover this signal */
-            goto done;
+      found = find_elt_with_offset (filter, parent, &parent_level, &elt);
 
-          elt = bsearch_elt_with_offset (level->array,
-                                         gtk_tree_path_get_indices (real_path)[i],
-                                         &j);
+      gtk_tree_path_free (parent);
 
-          if (!elt)
-            /* parent is probably being filtered out */
-            goto done;
+      if (!found)
+        /* Parent is not in the cache and probably being filtered out */
+        goto done;
 
-          if (!elt->children)
-            {
-              GtkTreePath *tmppath;
-              GtkTreeIter  tmpiter;
+      level = elt->children;
+    }
+  else
+    level = FILTER_LEVEL (filter->priv->root);
 
-              tmpiter.stamp = filter->priv->stamp;
-              tmpiter.user_data = level;
-              tmpiter.user_data2 = elt;
+  if (!level)
+    {
+      if (elt && elt->visible)
+        {
+          /* The level in which the new node should be inserted does not
+           * exist, but the parent, elt, does.  If elt is visible, emit
+           * row-has-child-toggled.
+           */
+          GtkTreePath *tmppath;
+          GtkTreeIter  tmpiter;
 
-              tmppath = gtk_tree_model_get_path (GTK_TREE_MODEL (data),
-                                                 &tmpiter);
+          tmpiter.stamp = filter->priv->stamp;
+          tmpiter.user_data = parent_level;
+          tmpiter.user_data2 = elt;
 
-              if (tmppath)
-                {
-                  gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (data),
-                                                        tmppath, &tmpiter);
-                  gtk_tree_path_free (tmppath);
-                }
+          tmppath = gtk_tree_model_get_path (GTK_TREE_MODEL (filter),
+                                             &tmpiter);
 
-              /* not covering this signal */
-              goto done;
+          if (tmppath)
+            {
+              gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (filter),
+                                                    tmppath, &tmpiter);
+              gtk_tree_path_free (tmppath);
             }
-
-          level = elt->children;
-          parent_level = level;
-          i++;
         }
+      goto done;
     }
 
-  if (!parent_level)
-    goto done;
-
   /* let's try to insert the value */
   offset = gtk_tree_path_get_indices (real_path)[gtk_tree_path_get_depth (real_path) - 1];
 
@@ -1881,35 +1919,24 @@ gtk_tree_model_filter_row_deleted_invisible_node (GtkTreeModelFilter *filter,
   else
     real_path = gtk_tree_path_copy (c_path);
 
-  i = 0;
   if (gtk_tree_path_get_depth (real_path) - 1 >= 1)
     {
-      /* find the level where the deletion occurred */
-      while (i < gtk_tree_path_get_depth (real_path) - 1)
-        {
-          gint j;
+      gboolean found = FALSE;
+      GtkTreePath *parent = gtk_tree_path_copy (real_path);
+      gtk_tree_path_up (parent);
 
-          if (!level)
-            {
-              /* we don't cover this */
-              gtk_tree_path_free (real_path);
-              return;
-            }
+      found = find_elt_with_offset (filter, parent, &level, &elt);
 
-          elt = bsearch_elt_with_offset (level->array,
-                                         gtk_tree_path_get_indices (real_path)[i],
-                                         &j);
-
-          if (!elt || !elt->children)
-            {
-              /* parent is filtered out, so no level */
-              gtk_tree_path_free (real_path);
-              return;
-            }
+      gtk_tree_path_free (parent);
 
-          level = elt->children;
-          i++;
+      if (!found)
+        {
+          /* parent is filtered out, so no level */
+          gtk_tree_path_free (real_path);
+          return;
         }
+
+      level = elt->children;
     }
 
   offset = gtk_tree_path_get_indices (real_path)[gtk_tree_path_get_depth (real_path) - 1];



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]