[gtk+/wip/cssstyle: 20/27] stylecontext: Refactor update_properties()



commit 1b2bbe151c8cc4be710f7ffd99eb639df8533141
Author: Benjamin Otte <otte redhat com>
Date:   Wed Dec 17 04:18:44 2014 +0100

    stylecontext: Refactor update_properties()
    
    It now always returns a new instance.

 gtk/gtkcssanimatedstyle.c        |    4 +-
 gtk/gtkcssanimatedstyleprivate.h |    1 +
 gtk/gtkcssstaticstyle.c          |   97 +++++++++++++++++++++++++++-----------
 gtk/gtkcssstaticstyleprivate.h   |    2 +
 gtk/gtkstylecontext.c            |   40 ++++++++++++----
 5 files changed, 106 insertions(+), 38 deletions(-)
---
diff --git a/gtk/gtkcssanimatedstyle.c b/gtk/gtkcssanimatedstyle.c
index b8c0d9a..a86621b 100644
--- a/gtk/gtkcssanimatedstyle.c
+++ b/gtk/gtkcssanimatedstyle.c
@@ -445,12 +445,14 @@ gtk_css_animated_style_new (GtkCssStyle             *base_style,
 
 GtkCssStyle *
 gtk_css_animated_style_new_advance (GtkCssAnimatedStyle *source,
+                                    GtkCssStyle         *base,
                                     gint64               timestamp)
 {
   GtkCssAnimatedStyle *result;
   GSList *l, *animations;
 
   gtk_internal_return_val_if_fail (GTK_IS_CSS_ANIMATED_STYLE (source), NULL);
+  gtk_internal_return_val_if_fail (GTK_IS_CSS_STYLE (base), NULL);
   
   animations = NULL;
   for (l = source->animations; l; l = l->next)
@@ -469,7 +471,7 @@ gtk_css_animated_style_new_advance (GtkCssAnimatedStyle *source,
 
   result = g_object_new (GTK_TYPE_CSS_ANIMATED_STYLE, NULL);
 
-  result->style = g_object_ref (source->style);
+  result->style = g_object_ref (base);
   result->current_time = timestamp;
   result->animations = animations;
 
diff --git a/gtk/gtkcssanimatedstyleprivate.h b/gtk/gtkcssanimatedstyleprivate.h
index 3bd49d7..c125ebb 100644
--- a/gtk/gtkcssanimatedstyleprivate.h
+++ b/gtk/gtkcssanimatedstyleprivate.h
@@ -59,6 +59,7 @@ GtkCssStyle *           gtk_css_animated_style_new              (GtkCssStyle
                                                                  int                     scale,
                                                                  GtkCssStyle            *previous_style);
 GtkCssStyle *           gtk_css_animated_style_new_advance      (GtkCssAnimatedStyle    *source,
+                                                                 GtkCssStyle            *base,
                                                                  gint64                  timestamp);
 
 void                    gtk_css_animated_style_set_animated_value(GtkCssAnimatedStyle   *style,
diff --git a/gtk/gtkcssstaticstyle.c b/gtk/gtkcssstaticstyle.c
index bcb37a0..82066cf 100644
--- a/gtk/gtkcssstaticstyle.c
+++ b/gtk/gtkcssstaticstyle.c
@@ -137,17 +137,81 @@ gtk_css_static_style_init (GtkCssStaticStyle *style)
   style->depends_on_font_size = _gtk_bitmask_new ();
 }
 
+static void
+maybe_unref_section (gpointer section)
+{
+  if (section)
+    gtk_css_section_unref (section);
+}
+
+static void
+gtk_css_static_style_set_value (GtkCssStaticStyle *style,
+                                guint              id,
+                                GtkCssValue       *value,
+                                GtkCssSection     *section)
+{
+  if (style->values == NULL)
+    style->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
+  if (id >= style->values->len)
+   g_ptr_array_set_size (style->values, id + 1);
+
+  if (g_ptr_array_index (style->values, id))
+    _gtk_css_value_unref (g_ptr_array_index (style->values, id));
+  g_ptr_array_index (style->values, id) = _gtk_css_value_ref (value);
+
+  if (style->sections && style->sections->len > id && g_ptr_array_index (style->sections, id))
+    {
+      gtk_css_section_unref (g_ptr_array_index (style->sections, id));
+      g_ptr_array_index (style->sections, id) = NULL;
+    }
+
+  if (section)
+    {
+      if (style->sections == NULL)
+        style->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
+      if (style->sections->len <= id)
+        g_ptr_array_set_size (style->sections, id + 1);
+
+      g_ptr_array_index (style->sections, id) = gtk_css_section_ref (section);
+    }
+}
+
 GtkCssStyle *
 gtk_css_static_style_new (void)
 {
   return g_object_new (GTK_TYPE_CSS_STATIC_STYLE, NULL);
 }
 
-static void
-maybe_unref_section (gpointer section)
+GtkCssStyle *
+gtk_css_static_style_copy (GtkCssStaticStyle *original,
+                           const GtkBitmask  *properties_to_not_copy)
 {
-  if (section)
-    gtk_css_section_unref (section);
+  GtkCssStaticStyle *copy;
+  guint i;
+
+  copy = g_object_new (GTK_TYPE_CSS_STATIC_STYLE, NULL);
+
+  copy->depends_on_parent = _gtk_bitmask_subtract (_gtk_bitmask_union (copy->depends_on_parent, 
original->depends_on_parent),
+                                                   properties_to_not_copy);
+  copy->equals_parent = _gtk_bitmask_subtract (_gtk_bitmask_union (copy->equals_parent, 
original->equals_parent),
+                                               properties_to_not_copy);
+  copy->depends_on_color = _gtk_bitmask_subtract (_gtk_bitmask_union (copy->depends_on_color, 
original->depends_on_color),
+                                                  properties_to_not_copy);
+  copy->depends_on_font_size = _gtk_bitmask_subtract (_gtk_bitmask_union (copy->depends_on_font_size, 
original->depends_on_font_size),
+                                                      properties_to_not_copy);
+
+  for (i = 0; i < original->values->len; i++)
+    {
+      if (_gtk_bitmask_get (properties_to_not_copy, i))
+        continue;
+
+      gtk_css_static_style_set_value (copy,
+                                      i,
+                                      gtk_css_static_style_get_value (GTK_CSS_STYLE (original), i),
+                                      gtk_css_static_style_get_section (GTK_CSS_STYLE (original), i));
+    }
+
+  return GTK_CSS_STYLE (copy);
 }
 
 void
@@ -185,14 +249,7 @@ gtk_css_static_style_compute_value (GtkCssStaticStyle       *style,
 
   value = _gtk_css_value_compute (specified, id, provider, scale, GTK_CSS_STYLE (style), parent_style, 
&dependencies);
 
-  if (style->values == NULL)
-    style->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
-  if (id >= style->values->len)
-   g_ptr_array_set_size (style->values, id + 1);
-
-  if (g_ptr_array_index (style->values, id))
-    _gtk_css_value_unref (g_ptr_array_index (style->values, id));
-  g_ptr_array_index (style->values, id) = _gtk_css_value_ref (value);
+  gtk_css_static_style_set_value (style, id, value, section);
 
   if (dependencies & (GTK_CSS_DEPENDS_ON_PARENT | GTK_CSS_EQUALS_PARENT))
     style->depends_on_parent = _gtk_bitmask_set (style->depends_on_parent, id, TRUE);
@@ -203,22 +260,6 @@ gtk_css_static_style_compute_value (GtkCssStaticStyle       *style,
   if (dependencies & (GTK_CSS_DEPENDS_ON_FONT_SIZE))
     style->depends_on_font_size = _gtk_bitmask_set (style->depends_on_font_size, id, TRUE);
 
-  if (style->sections && style->sections->len > id && g_ptr_array_index (style->sections, id))
-    {
-      gtk_css_section_unref (g_ptr_array_index (style->sections, id));
-      g_ptr_array_index (style->sections, id) = NULL;
-    }
-
-  if (section)
-    {
-      if (style->sections == NULL)
-        style->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
-      if (style->sections->len <= id)
-        g_ptr_array_set_size (style->sections, id + 1);
-
-      g_ptr_array_index (style->sections, id) = gtk_css_section_ref (section);
-    }
-
   _gtk_css_value_unref (value);
   _gtk_css_value_unref (specified);
 }
diff --git a/gtk/gtkcssstaticstyleprivate.h b/gtk/gtkcssstaticstyleprivate.h
index 390a911..a18127b 100644
--- a/gtk/gtkcssstaticstyleprivate.h
+++ b/gtk/gtkcssstaticstyleprivate.h
@@ -55,6 +55,8 @@ struct _GtkCssStaticStyleClass
 GType                   gtk_css_static_style_get_type           (void) G_GNUC_CONST;
 
 GtkCssStyle *           gtk_css_static_style_new                (void);
+GtkCssStyle *           gtk_css_static_style_copy               (GtkCssStaticStyle *original,
+                                                                 const GtkBitmask  *properties_to_not_copy);
 
 void                    gtk_css_static_style_compute_value      (GtkCssStaticStyle      *style,
                                                                  GtkStyleProviderPrivate*provider,
diff --git a/gtk/gtkstylecontext.c b/gtk/gtkstylecontext.c
index bdbb129..7dd3a78 100644
--- a/gtk/gtkstylecontext.c
+++ b/gtk/gtkstylecontext.c
@@ -669,7 +669,7 @@ create_query_path (GtkStyleContext             *context,
   return path;
 }
 
-static void
+static GtkCssStyle *
 update_properties (GtkStyleContext             *context,
                    GtkCssStyle                 *style,
                    const GtkCssNodeDeclaration *decl,
@@ -680,6 +680,7 @@ update_properties (GtkStyleContext             *context,
   GtkWidgetPath *path;
   GtkCssLookup *lookup;
   GtkBitmask *changes;
+  GtkCssStyle *result;
 
   priv = context->priv;
 
@@ -687,9 +688,10 @@ update_properties (GtkStyleContext             *context,
   if (_gtk_bitmask_is_empty (changes))
     {
       _gtk_bitmask_free (changes);
-      return;
+      return g_object_ref (style);
     }
 
+  result = gtk_css_static_style_copy (GTK_CSS_STATIC_STYLE (style), changes);
   path = create_query_path (context, decl);
   lookup = _gtk_css_lookup_new (changes);
 
@@ -702,12 +704,14 @@ update_properties (GtkStyleContext             *context,
   _gtk_css_lookup_resolve (lookup, 
                            GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
                           priv->scale,
-                           GTK_CSS_STATIC_STYLE (style),
+                           GTK_CSS_STATIC_STYLE (result),
                            priv->parent ? style_values_lookup (priv->parent) : NULL);
 
   _gtk_css_lookup_free (lookup);
   gtk_widget_path_free (path);
   _gtk_bitmask_free (changes);
+
+  return result;
 }
 
 static GtkCssStyle *
@@ -2686,7 +2690,9 @@ gtk_style_context_update_cache (GtkStyleContext  *context,
       const GtkCssNodeDeclaration *decl = key;
       GtkCssStyle *values = value;
 
-      update_properties (context, values, decl, parent_changes);
+      values = update_properties (context, values, decl, parent_changes);
+
+      g_hash_table_iter_replace (&iter, values);
     }
 
   gtk_style_context_clear_property_cache (context);
@@ -2835,18 +2841,34 @@ _gtk_style_context_validate (GtkStyleContext  *context,
 
       if (!_gtk_bitmask_is_empty (parent_changes))
         {
+          GtkCssStyle *new_values;
+
           if (GTK_IS_CSS_ANIMATED_STYLE (current))
-           update_properties (context, GTK_CSS_ANIMATED_STYLE (current)->style, info->decl, parent_changes);
+            {
+             GtkCssStyle *new_base;
+              
+              new_base = update_properties (context, GTK_CSS_ANIMATED_STYLE (current)->style, info->decl, 
parent_changes);
+              new_values = gtk_css_animated_style_new_advance (GTK_CSS_ANIMATED_STYLE (current),
+                                                               new_base,
+                                                               timestamp);
+              g_object_unref (new_base);
+            }
           else
-           update_properties (context, current, info->decl, parent_changes);
-        }
+            {
+             new_values = update_properties (context, current, info->decl, parent_changes);
+            }
 
-      if (change & GTK_CSS_CHANGE_ANIMATE &&
+          style_info_set_values (info, new_values);
+          g_object_unref (new_values);
+        }
+      else if (change & GTK_CSS_CHANGE_ANIMATE &&
           gtk_style_context_is_animating (context))
         {
           GtkCssStyle *new_values;
 
-          new_values = gtk_css_animated_style_new_advance (GTK_CSS_ANIMATED_STYLE (info->values), timestamp);
+          new_values = gtk_css_animated_style_new_advance (GTK_CSS_ANIMATED_STYLE (info->values),
+                                                           GTK_CSS_ANIMATED_STYLE (info->values)->style,
+                                                           timestamp);
           style_info_set_values (info, new_values);
           g_object_unref (new_values);
 


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