]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcssimagelinear.c
Fix includes
[~andy/gtk] / gtk / gtkcssimagelinear.c
index 63f440dcb1bf4fcaaeafbd3f82d2a440e17e5ec0..0bf89e7d8b13e620b07c11b0bc39b978a806d0e4 100644 (file)
 
 #include <math.h>
 
+#include "gtkcsscolorvalueprivate.h"
+#include "gtkcssnumbervalueprivate.h"
+#include "gtkcssrgbavalueprivate.h"
 #include "gtkcssprovider.h"
-#include "gtkstylecontextprivate.h"
 
 G_DEFINE_TYPE (GtkCssImageLinear, _gtk_css_image_linear, GTK_TYPE_CSS_IMAGE)
 
@@ -38,33 +40,36 @@ gtk_css_image_linear_get_start_end (GtkCssImageLinear *linear,
   double pos;
   guint i;
       
-  stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, 0);
-  if (stop->offset.unit == GTK_CSS_NUMBER)
-    *start = 0;
-  else if (stop->offset.unit == GTK_CSS_PX)
-    *start = stop->offset.value / length;
-  else
-    *start = stop->offset.value / 100;
+  if (linear->repeating)
+    {
+      stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, 0);
+      if (stop->offset == NULL)
+        *start = 0;
+      else
+        *start = _gtk_css_number_value_get (stop->offset, length) / length;
 
-  *end = *start;
+      *end = *start;
 
-  for (i = 0; i < linear->stops->len; i++)
-    {
-      stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
-      
-      if (stop->offset.unit == GTK_CSS_NUMBER)
-        continue;
+      for (i = 0; i < linear->stops->len; i++)
+        {
+          stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
+          
+          if (stop->offset == NULL)
+            continue;
 
-      if (stop->offset.unit == GTK_CSS_PX)
-        pos = stop->offset.value / length;
-      else
-        pos = stop->offset.value / 100;
+          pos = _gtk_css_number_value_get (stop->offset, length) / length;
 
-      *end = MAX (pos, *end);
+          *end = MAX (pos, *end);
+        }
+      
+      if (stop->offset == NULL)
+        *end = MAX (*end, 1.0);
+    }
+  else
+    {
+      *start = 0;
+      *end = 1;
     }
-  
-  if (stop->offset.unit == GTK_CSS_NUMBER)
-    *end = MAX (*end, 1.0);
 }
 
 static void
@@ -136,11 +141,9 @@ gtk_css_image_linear_draw (GtkCssImage        *image,
   double offset;
   int i, last;
 
-  g_return_if_fail (linear->is_computed);
-
-  if (linear->angle.unit == GTK_CSS_NUMBER)
+  if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
     {
-      guint side = linear->angle.value;
+      guint side = _gtk_css_number_value_get (linear->angle, 100);
 
       if (side & (1 << GTK_CSS_RIGHT))
         x = width;
@@ -158,7 +161,7 @@ gtk_css_image_linear_draw (GtkCssImage        *image,
     }
   else
     {
-      gtk_css_image_linear_compute_start_point (linear->angle.value,
+      gtk_css_image_linear_compute_start_point (_gtk_css_number_value_get (linear->angle, 100),
                                                 width, height,
                                                 &x, &y);
     }
@@ -181,7 +184,7 @@ gtk_css_image_linear_draw (GtkCssImage        *image,
       
       stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
 
-      if (stop->offset.unit == GTK_CSS_NUMBER)
+      if (stop->offset == NULL)
         {
           if (i == 0)
             pos = 0.0;
@@ -190,25 +193,26 @@ gtk_css_image_linear_draw (GtkCssImage        *image,
           else
             continue;
         }
-      else if (stop->offset.unit == GTK_CSS_PX)
-        pos = stop->offset.value / length;
       else
-        pos = stop->offset.value / 100;
+        pos = _gtk_css_number_value_get (stop->offset, length) / length;
 
       pos = MAX (pos, offset);
       step = (pos - offset) / (i - last);
       for (last = last + 1; last <= i; last++)
         {
+          const GdkRGBA *rgba;
+
           stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last);
 
+          rgba = _gtk_css_rgba_value_get_rgba (stop->color);
           offset += step;
 
           cairo_pattern_add_color_stop_rgba (pattern,
                                              (offset - start) / (end - start),
-                                             stop->color.rgba.red,
-                                             stop->color.rgba.green,
-                                             stop->color.rgba.blue,
-                                             stop->color.rgba.alpha);
+                                             rgba->red,
+                                             rgba->green,
+                                             rgba->blue,
+                                             rgba->alpha);
         }
 
       offset = pos;
@@ -226,8 +230,7 @@ gtk_css_image_linear_draw (GtkCssImage        *image,
 
 static gboolean
 gtk_css_image_linear_parse (GtkCssImage  *image,
-                            GtkCssParser *parser,
-                            GFile        *base)
+                            GtkCssParser *parser)
 {
   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
   guint i;
@@ -294,7 +297,7 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
           return FALSE;
         }
 
-      _gtk_css_number_init (&linear->angle, side, GTK_CSS_NUMBER);
+      linear->angle = _gtk_css_number_value_new (side, GTK_CSS_NUMBER);
 
       if (!_gtk_css_parser_try (parser, ",", TRUE))
         {
@@ -304,9 +307,8 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
     }
   else if (_gtk_css_parser_has_number (parser))
     {
-      if (!_gtk_css_parser_read_number (parser,
-                                        &linear->angle,
-                                        GTK_CSS_PARSE_ANGLE))
+      linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
+      if (linear->angle == NULL)
         return FALSE;
 
       if (!_gtk_css_parser_try (parser, ",", TRUE))
@@ -316,27 +318,29 @@ gtk_css_image_linear_parse (GtkCssImage  *image,
         }
     }
   else
-    _gtk_css_number_init (&linear->angle, 1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
+    linear->angle = _gtk_css_number_value_new (1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
 
   do {
     GtkCssImageLinearColorStop stop;
 
-    stop.color.symbolic = _gtk_css_parser_read_symbolic_color (parser);
-    if (stop.color.symbolic == NULL)
+    stop.color = _gtk_css_color_value_parse (parser);
+    if (stop.color == NULL)
       return FALSE;
 
     if (_gtk_css_parser_has_number (parser))
       {
-        if (!_gtk_css_parser_read_number (parser,
-                                          &stop.offset,
-                                          GTK_CSS_PARSE_PERCENT
-                                          | GTK_CSS_PARSE_LENGTH))
-          return FALSE;
+        stop.offset = _gtk_css_number_value_parse (parser,
+                                                   GTK_CSS_PARSE_PERCENT
+                                                   | GTK_CSS_PARSE_LENGTH);
+        if (stop.offset == NULL)
+          {
+            _gtk_css_value_unref (stop.color);
+            return FALSE;
+          }
       }
     else
       {
-        /* use NUMBER to mark as unset number */
-        _gtk_css_number_init (&stop.offset, 0, GTK_CSS_NUMBER);
+        stop.offset = NULL;
       }
 
     g_array_append_val (linear->stops, stop);
@@ -359,17 +363,14 @@ gtk_css_image_linear_print (GtkCssImage *image,
   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
   guint i;
 
-  /* XXX: Do these need to be prinatable? */
-  g_return_if_fail (!linear->is_computed);
-
   if (linear->repeating)
     g_string_append (string, "repeating-linear-gradient(");
   else
     g_string_append (string, "linear-gradient(");
 
-  if (linear->angle.unit == GTK_CSS_NUMBER)
+  if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
     {
-      guint side = linear->angle.value;
+      guint side = _gtk_css_number_value_get (linear->angle, 100);
 
       if (side != (1 << GTK_CSS_BOTTOM))
         {
@@ -390,28 +391,25 @@ gtk_css_image_linear_print (GtkCssImage *image,
     }
   else
     {
-      _gtk_css_number_print (&linear->angle, string);
+      _gtk_css_value_print (linear->angle, string);
       g_string_append (string, ", ");
     }
 
   for (i = 0; i < linear->stops->len; i++)
     {
       GtkCssImageLinearColorStop *stop;
-      char *s;
       
       if (i > 0)
         g_string_append (string, ", ");
 
       stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
 
-      s = gtk_symbolic_color_to_string (stop->color.symbolic);
-      g_string_append (string, s);
-      g_free (s);
+      _gtk_css_value_print (stop->color, string);
 
-      if (stop->offset.unit != GTK_CSS_NUMBER)
+      if (stop->offset)
         {
           g_string_append (string, " ");
-          _gtk_css_number_print (&stop->offset, string);
+          _gtk_css_value_print (stop->offset, string);
         }
     }
   
@@ -419,44 +417,150 @@ gtk_css_image_linear_print (GtkCssImage *image,
 }
 
 static GtkCssImage *
-gtk_css_image_linear_compute (GtkCssImage     *image,
-                              GtkStyleContext *context)
+gtk_css_image_linear_compute (GtkCssImage             *image,
+                              guint                    property_id,
+                              GtkStyleProviderPrivate *provider,
+                              GtkCssComputedValues    *values,
+                              GtkCssComputedValues    *parent_values,
+                              GtkCssDependencies      *dependencies)
 {
   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
   GtkCssImageLinear *copy;
   guint i;
 
-  if (linear->is_computed)
-    return g_object_ref (linear);
-
   copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
-  copy->is_computed = TRUE;
   copy->repeating = linear->repeating;
 
-  _gtk_css_number_compute (&copy->angle, &linear->angle, context);
+  copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, values, parent_values, dependencies);
   
   g_array_set_size (copy->stops, linear->stops->len);
   for (i = 0; i < linear->stops->len; i++)
     {
       GtkCssImageLinearColorStop *stop, *scopy;
+      GtkCssDependencies child_deps;
 
       stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
       scopy = &g_array_index (copy->stops, GtkCssImageLinearColorStop, i);
               
-      if (!_gtk_style_context_resolve_color (context,
-                                             stop->color.symbolic,
-                                             &scopy->color.rgba))
+      scopy->color = _gtk_css_value_compute (stop->color, property_id, provider, values, parent_values, &child_deps);
+      *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
+      
+      if (stop->offset)
         {
-          static const GdkRGBA transparent = { 0, 0, 0, 0 };
-          scopy->color.rgba = transparent;
+          scopy->offset = _gtk_css_value_compute (stop->offset, property_id, provider, values, parent_values, &child_deps);
+          *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
+        }
+      else
+        {
+          scopy->offset = NULL;
         }
-      
-      _gtk_css_number_compute (&scopy->offset, &stop->offset, context);
     }
 
   return GTK_CSS_IMAGE (copy);
 }
 
+static GtkCssImage *
+gtk_css_image_linear_transition (GtkCssImage *start_image,
+                                 GtkCssImage *end_image,
+                                 guint        property_id,
+                                 double       progress)
+{
+  GtkCssImageLinear *start, *end, *result;
+  guint i;
+
+  start = GTK_CSS_IMAGE_LINEAR (start_image);
+
+  if (end_image == NULL)
+
+  if (!GTK_IS_CSS_IMAGE_LINEAR (end_image))
+    return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
+
+  end = GTK_CSS_IMAGE_LINEAR (end_image);
+
+  if ((start->repeating != end->repeating)
+      || (start->stops->len != end->stops->len))
+    return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
+
+  result = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
+  result->repeating = start->repeating;
+
+  result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress);
+  if (result->angle == NULL)
+    goto fail;
+  
+  for (i = 0; i < start->stops->len; i++)
+    {
+      GtkCssImageLinearColorStop stop, *start_stop, *end_stop;
+
+      start_stop = &g_array_index (start->stops, GtkCssImageLinearColorStop, i);
+      end_stop = &g_array_index (end->stops, GtkCssImageLinearColorStop, i);
+
+      if ((start_stop->offset != NULL) != (end_stop->offset != NULL))
+        goto fail;
+
+      if (start_stop->offset == NULL)
+        {
+          stop.offset = NULL;
+        }
+      else
+        {
+          stop.offset = _gtk_css_value_transition (start_stop->offset,
+                                                   end_stop->offset,
+                                                   property_id,
+                                                   progress);
+          if (stop.offset == NULL)
+            goto fail;
+        }
+
+      stop.color = _gtk_css_value_transition (start_stop->color,
+                                              end_stop->color,
+                                              property_id,
+                                              progress);
+      if (stop.color == NULL)
+        {
+          if (stop.offset)
+            _gtk_css_value_unref (stop.offset);
+          goto fail;
+        }
+
+      g_array_append_val (result->stops, stop);
+    }
+
+  return GTK_CSS_IMAGE (result);
+
+fail:
+  g_object_unref (result);
+  return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
+}
+
+static gboolean
+gtk_css_image_linear_equal (GtkCssImage *image1,
+                            GtkCssImage *image2)
+{
+  GtkCssImageLinear *linear1 = GTK_CSS_IMAGE_LINEAR (image1);
+  GtkCssImageLinear *linear2 = GTK_CSS_IMAGE_LINEAR (image2);
+  guint i;
+
+  if (linear1->repeating != linear2->repeating ||
+      !_gtk_css_value_equal (linear1->angle, linear2->angle) ||
+      linear1->stops->len != linear2->stops->len)
+    return FALSE;
+
+  for (i = 0; i < linear1->stops->len; i++)
+    {
+      GtkCssImageLinearColorStop *stop1, *stop2;
+
+      stop1 = &g_array_index (linear1->stops, GtkCssImageLinearColorStop, i);
+      stop2 = &g_array_index (linear2->stops, GtkCssImageLinearColorStop, i);
+
+      if (!_gtk_css_value_equal0 (stop1->offset, stop2->offset) ||
+          !_gtk_css_value_equal (stop1->color, stop2->color))
+        return FALSE;
+    }
+
+  return TRUE;
+}
+
 static void
 gtk_css_image_linear_dispose (GObject *object)
 {
@@ -464,23 +568,16 @@ gtk_css_image_linear_dispose (GObject *object)
 
   if (linear->stops)
     {
-      if (!linear->is_computed)
-        {
-          guint i;
-
-          for (i = 0; i < linear->stops->len; i++)
-            {
-              GtkCssImageLinearColorStop *stop;
-
-              stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
-              
-              gtk_symbolic_color_unref (stop->color.symbolic);
-            }
-        }
       g_array_free (linear->stops, TRUE);
       linear->stops = NULL;
     }
 
+  if (linear->angle)
+    {
+      _gtk_css_value_unref (linear->angle);
+      linear->angle = NULL;
+    }
+
   G_OBJECT_CLASS (_gtk_css_image_linear_parent_class)->dispose (object);
 }
 
@@ -494,15 +591,26 @@ _gtk_css_image_linear_class_init (GtkCssImageLinearClass *klass)
   image_class->parse = gtk_css_image_linear_parse;
   image_class->print = gtk_css_image_linear_print;
   image_class->compute = gtk_css_image_linear_compute;
+  image_class->equal = gtk_css_image_linear_equal;
+  image_class->transition = gtk_css_image_linear_transition;
 
   object_class->dispose = gtk_css_image_linear_dispose;
 }
 
+static void
+gtk_css_image_clear_color_stop (gpointer color_stop)
+{
+  GtkCssImageLinearColorStop *stop = color_stop;
+
+  _gtk_css_value_unref (stop->color);
+  if (stop->offset)
+    _gtk_css_value_unref (stop->offset);
+}
+
 static void
 _gtk_css_image_linear_init (GtkCssImageLinear *linear)
 {
   linear->stops = g_array_new (FALSE, FALSE, sizeof (GtkCssImageLinearColorStop));
-
-  _gtk_css_number_init (&linear->angle, 1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
+  g_array_set_clear_func (linear->stops, gtk_css_image_clear_color_stop);
 }