gtk2/gtk/gtkcssvalue.c
Benjamin Otte 1454ba15ba css: Huge refactoring to avoid computing wrong values
Here's the shortest description of the bug I can come up with:
When computing values, we have 3 kinds of dependencies:
(1) other properties ("currentColor" or em values)
(2) inherited properties ("inherit")
(3) generic things from the theme (@keyframes or @define-color)
Previously, we passed the GtkStyleContext as an argument, because it
provided these 3 things using:
(1) _gtk_style_context_peek_property()
(2) _gtk_style_context_peek_property(gtk_style_context_get_parent())
(3) context->priv->cascade

However, this makes it impossible to lookup values other than the ones
accessible via _gtk_style_context_peek_property(). And this is exactly
what we are doing in gtk_style_context_update_cache(). So when the cache
updates encountered case (1), they were looking up the values from the
wrong style data.

So this large patch essentially does nothing but replace the
context argument in all compute functions with new arguments for the 3
cases above:
(1) values
(2) parent_values
(3) provider

We apparently have a lot of computing code.
2012-09-28 18:27:49 +02:00

184 lines
5.0 KiB
C

/* GTK - The GIMP Toolkit
* Copyright (C) 2011 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkcssvalueprivate.h"
#include "gtkcsscomputedvaluesprivate.h"
#include "gtkstyleproviderprivate.h"
struct _GtkCssValue {
GTK_CSS_VALUE_BASE
};
G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref)
GtkCssValue *
_gtk_css_value_alloc (const GtkCssValueClass *klass,
gsize size)
{
GtkCssValue *value;
value = g_slice_alloc0 (size);
value->class = klass;
value->ref_count = 1;
return value;
}
GtkCssValue *
_gtk_css_value_ref (GtkCssValue *value)
{
g_return_val_if_fail (value != NULL, NULL);
g_atomic_int_add (&value->ref_count, 1);
return value;
}
void
_gtk_css_value_unref (GtkCssValue *value)
{
if (value == NULL)
return;
if (!g_atomic_int_dec_and_test (&value->ref_count))
return;
value->class->free (value);
}
/**
* _gtk_css_value_compute:
* @value: the value to compute from
* @property_id: the ID of the property to compute
* @provider: Style provider for looking up extra information
* @values: values to compute for
* @parent_values: parent values to use for inherited values
* @dependencies: (out) (allow-none): Set to the dependencies of the
* computed values that indicate when this value needs to be
* recomputed and how.
*
* Converts the specified @value into the computed value for the CSS
* property given by @property_id using the information in @context.
* This step is explained in detail in
* <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
* the CSS documentation</ulink>.
*
* Returns: the computed value
**/
GtkCssValue *
_gtk_css_value_compute (GtkCssValue *value,
guint property_id,
GtkStyleProviderPrivate *provider,
GtkCssComputedValues *values,
GtkCssComputedValues *parent_values,
GtkCssDependencies *dependencies)
{
GtkCssDependencies fallback;
g_return_val_if_fail (value != NULL, NULL);
g_return_val_if_fail (GTK_IS_STYLE_PROVIDER_PRIVATE (provider), NULL);
g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
g_return_val_if_fail (parent_values == NULL || GTK_IS_CSS_COMPUTED_VALUES (parent_values), NULL);
if (dependencies == NULL)
dependencies = &fallback;
*dependencies = 0;
return value->class->compute (value, property_id, provider, values, parent_values, dependencies);
}
gboolean
_gtk_css_value_equal (const GtkCssValue *value1,
const GtkCssValue *value2)
{
g_return_val_if_fail (value1 != NULL, FALSE);
g_return_val_if_fail (value2 != NULL, FALSE);
if (value1 == value2)
return TRUE;
if (value1->class != value2->class)
return FALSE;
return value1->class->equal (value1, value2);
}
gboolean
_gtk_css_value_equal0 (const GtkCssValue *value1,
const GtkCssValue *value2)
{
/* Inclues both values being NULL */
if (value1 == value2)
return TRUE;
if (value1 == NULL || value2 == NULL)
return FALSE;
return _gtk_css_value_equal (value1, value2);
}
GtkCssValue *
_gtk_css_value_transition (GtkCssValue *start,
GtkCssValue *end,
guint property_id,
double progress)
{
g_return_val_if_fail (start != NULL, FALSE);
g_return_val_if_fail (end != NULL, FALSE);
if (start->class != end->class)
return NULL;
return start->class->transition (start, end, property_id, progress);
}
char *
_gtk_css_value_to_string (const GtkCssValue *value)
{
GString *string;
g_return_val_if_fail (value != NULL, NULL);
string = g_string_new (NULL);
_gtk_css_value_print (value, string);
return g_string_free (string, FALSE);
}
/**
* _gtk_css_value_print:
* @value: the value to print
* @string: the string to print to
*
* Prints @value to the given @string in CSS format. The @value must be a
* valid specified value as parsed using the parse functions or as assigned
* via _gtk_style_property_assign().
**/
void
_gtk_css_value_print (const GtkCssValue *value,
GString *string)
{
g_return_if_fail (value != NULL);
g_return_if_fail (string != NULL);
value->class->print (value, string);
}