#include "config.h"
-#include "gtkstyleproperties.h"
+#include "gtkstylepropertiesprivate.h"
#include <stdlib.h>
#include <gobject/gvaluecollector.h>
#include <cairo-gobject.h>
-#include "gtktypebuiltins.h"
#include "gtkstyleprovider.h"
#include "gtksymboliccolor.h"
-#include "gtkprivate.h"
#include "gtkthemingengine.h"
#include "gtkanimationdescription.h"
-#include "gtkborder.h"
#include "gtkgradient.h"
-#include "gtk9slice.h"
+#include "gtkshadowprivate.h"
+#include "gtkcsstypesprivate.h"
+
+#include "gtkstylepropertyprivate.h"
#include "gtkintl.h"
/**
typedef struct GtkStylePropertiesPrivate GtkStylePropertiesPrivate;
typedef struct PropertyData PropertyData;
-typedef struct PropertyNode PropertyNode;
typedef struct ValueData ValueData;
-struct PropertyNode
-{
- GQuark property_quark;
- GParamSpec *pspec;
- GtkStylePropertyParser parse_func;
-};
-
struct ValueData
{
GtkStateFlags state;
GHashTable *properties;
};
-static GArray *properties = NULL;
-
static void gtk_style_properties_provider_init (GtkStyleProviderIface *iface);
static void gtk_style_properties_finalize (GObject *object);
object_class->finalize = gtk_style_properties_finalize;
- /* Initialize default property set */
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("color",
- "Foreground color",
- "Foreground color",
- GDK_TYPE_RGBA, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("background-color",
- "Background color",
- "Background color",
- GDK_TYPE_RGBA, 0));
-
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("font",
- "Font Description",
- "Font Description",
- PANGO_TYPE_FONT_DESCRIPTION, 0));
-
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("margin",
- "Margin",
- "Margin",
- GTK_TYPE_BORDER, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("padding",
- "Padding",
- "Padding",
- GTK_TYPE_BORDER, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("border-width",
- "Border width",
- "Border width, in pixels",
- GTK_TYPE_BORDER, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_int ("border-radius",
- "Border radius",
- "Border radius, in pixels",
- 0, G_MAXINT, 0, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_enum ("border-style",
- "Border style",
- "Border style",
- GTK_TYPE_BORDER_STYLE,
- GTK_BORDER_STYLE_NONE, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("border-color",
- "Border color",
- "Border color",
- GDK_TYPE_RGBA, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("background-image",
- "Background Image",
- "Background Image",
- CAIRO_GOBJECT_TYPE_PATTERN, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("border-image",
- "Border Image",
- "Border Image",
- GTK_TYPE_9SLICE, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_object ("engine",
- "Theming Engine",
- "Theming Engine",
- GTK_TYPE_THEMING_ENGINE, 0));
- gtk_style_properties_register_property (NULL,
- g_param_spec_boxed ("transition",
- "Transition animation description",
- "Transition animation description",
- GTK_TYPE_ANIMATION_DESCRIPTION, 0));
-
g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
}
}
static void
-property_data_free (PropertyData *data)
+property_data_remove_values (PropertyData *data)
{
guint i;
g_value_unset (&value_data->value);
}
+ if (data->values->len > 0)
+ g_array_remove_range (data->values, 0, data->values->len);
+}
+
+static void
+property_data_free (PropertyData *data)
+{
+ property_data_remove_values (data);
g_array_free (data->values, TRUE);
g_slice_free (PropertyData, data);
}
iface->get_style = gtk_style_properties_get_style;
}
-static int
-compare_property (gconstpointer p1,
- gconstpointer p2)
-{
- PropertyNode *key = (PropertyNode *) p1;
- PropertyNode *node = (PropertyNode *) p2;
-
- if (key->property_quark > node->property_quark)
- return 1;
- else if (key->property_quark < node->property_quark)
- return -1;
-
- return 0;
-}
-
-static PropertyNode *
-property_node_lookup (GQuark quark)
-{
- PropertyNode key = { 0 };
-
- if (!quark)
- return NULL;
-
- if (!properties)
- return NULL;
-
- key.property_quark = quark;
-
- return bsearch (&key, properties->data, properties->len,
- sizeof (PropertyNode), compare_property);
-}
-
/* Property registration functions */
/**
- * gtk_style_properties_register_property:
+ * gtk_style_properties_register_property: (skip)
* @parse_func: parsing function to use, or %NULL
* @pspec: the #GParamSpec for the new property
*
gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
GParamSpec *pspec)
{
- PropertyNode *node, new = { 0 };
- GQuark quark;
- gint i;
-
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
- if (G_UNLIKELY (!properties))
- properties = g_array_new (FALSE, TRUE, sizeof (PropertyNode));
-
- quark = g_quark_from_string (pspec->name);
-
- if ((node = property_node_lookup (quark)) != NULL)
- {
- g_warning ("Property \"%s\" was already registered with type %s",
- pspec->name, g_type_name (node->pspec->value_type));
- return;
- }
-
- new.property_quark = quark;
- new.pspec = pspec;
-
- if (parse_func)
- new.parse_func = parse_func;
-
- for (i = 0; i < properties->len; i++)
- {
- node = &g_array_index (properties, PropertyNode, i);
-
- if (node->property_quark > quark)
- break;
- }
-
- g_array_insert_val (properties, i, new);
+ _gtk_style_property_register (pspec,
+ 0,
+ parse_func,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL);
}
/**
- * gtk_style_properties_lookup_property:
+ * gtk_style_properties_lookup_property: (skip)
* @property_name: property name to look up
* @parse_func: (out): return location for the parse function
- * @pspec: (out): return location for the #GParamSpec
+ * @pspec: (out) (transfer none): return location for the #GParamSpec
*
* Returns %TRUE if a property has been registered, if @pspec or
* @parse_func are not %NULL, the #GParamSpec and parsing function
GtkStylePropertyParser *parse_func,
GParamSpec **pspec)
{
- PropertyNode *node;
- GtkStylePropertiesClass *klass;
+ const GtkStyleProperty *node;
gboolean found = FALSE;
- GQuark quark;
- gint i;
g_return_val_if_fail (property_name != NULL, FALSE);
- klass = g_type_class_ref (GTK_TYPE_STYLE_PROPERTIES);
- quark = g_quark_try_string (property_name);
-
- if (quark == 0)
- {
- g_type_class_unref (klass);
- return FALSE;
- }
+ node = _gtk_style_property_lookup (property_name);
- for (i = 0; i < properties->len; i++)
+ if (node)
{
- node = &g_array_index (properties, PropertyNode, i);
-
- if (node->property_quark == quark)
- {
- if (pspec)
- *pspec = node->pspec;
+ if (pspec)
+ *pspec = node->pspec;
- if (parse_func)
- *parse_func = node->parse_func;
+ if (parse_func)
+ *parse_func = node->property_parse_func;
- found = TRUE;
- break;
- }
- else if (node->property_quark > quark)
- break;
+ found = TRUE;
}
- g_type_class_unref (klass);
-
return found;
}
* Returns the symbolic color that is mapped
* to @name.
*
- * Returns: The mapped color
+ * Returns: (transfer none): The mapped color
*
* Since: 3.0
**/
return g_hash_table_lookup (priv->color_map, name);
}
-/**
- * gtk_style_properties_set_property:
- * @props: a #GtkStyleProperties
- * @property: styling property to set
- * @state: state to set the value for
- * @value: new value for the property
- *
- * Sets a styling property in @props.
- *
- * Since: 3.0
- **/
void
-gtk_style_properties_set_property (GtkStyleProperties *props,
- const gchar *property,
- GtkStateFlags state,
- const GValue *value)
+_gtk_style_properties_set_property_by_property (GtkStyleProperties *props,
+ const GtkStyleProperty *style_prop,
+ GtkStateFlags state,
+ const GValue *value)
{
GtkStylePropertiesPrivate *priv;
- PropertyNode *node;
PropertyData *prop;
GType value_type;
GValue *val;
- g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
- g_return_if_fail (property != NULL);
- g_return_if_fail (value != NULL);
-
value_type = G_VALUE_TYPE (value);
- node = property_node_lookup (g_quark_try_string (property));
- if (!node)
- {
- g_warning ("Style property \"%s\" is not registered", property);
- return;
- }
-
- if (node->pspec->value_type == GDK_TYPE_RGBA ||
- node->pspec->value_type == GDK_TYPE_COLOR)
+ if (style_prop->pspec->value_type == GDK_TYPE_RGBA ||
+ style_prop->pspec->value_type == GDK_TYPE_COLOR)
{
/* Allow GtkSymbolicColor as well */
g_return_if_fail (value_type == GDK_TYPE_RGBA ||
value_type == GDK_TYPE_COLOR ||
value_type == GTK_TYPE_SYMBOLIC_COLOR);
}
- else if (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
+ else if (style_prop->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
{
/* Allow GtkGradient as a substitute */
g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
value_type == GTK_TYPE_GRADIENT);
}
+ else if (style_prop->pspec->value_type == G_TYPE_INT)
+ {
+ g_return_if_fail (value_type == G_TYPE_INT ||
+ value_type == GTK_TYPE_CSS_BORDER_RADIUS);
+ }
else
- g_return_if_fail (node->pspec->value_type == value_type);
+ g_return_if_fail (style_prop->pspec->value_type == value_type);
+
+ if (_gtk_style_property_is_shorthand (style_prop))
+ {
+ GParameter *parameters;
+ guint i, n_parameters;
+
+ parameters = _gtk_style_property_unpack (style_prop, value, &n_parameters);
+
+ for (i = 0; i < n_parameters; i++)
+ {
+ gtk_style_properties_set_property (props,
+ parameters[i].name,
+ state,
+ ¶meters[i].value);
+ g_value_unset (¶meters[i].value);
+ }
+ g_free (parameters);
+ return;
+ }
priv = props->priv;
- prop = g_hash_table_lookup (priv->properties,
- GINT_TO_POINTER (node->property_quark));
+ prop = g_hash_table_lookup (priv->properties, style_prop);
if (!prop)
{
prop = property_data_new ();
- g_hash_table_insert (priv->properties,
- GINT_TO_POINTER (node->property_quark),
- prop);
+ g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
}
val = property_data_get_value (prop, state);
}
g_value_copy (value, val);
+ if (style_prop->pspec->value_type == value_type)
+ g_param_value_validate (style_prop->pspec, val);
+}
+
+/**
+ * gtk_style_properties_set_property:
+ * @props: a #GtkStyleProperties
+ * @property: styling property to set
+ * @state: state to set the value for
+ * @value: new value for the property
+ *
+ * Sets a styling property in @props.
+ *
+ * Since: 3.0
+ **/
+void
+gtk_style_properties_set_property (GtkStyleProperties *props,
+ const gchar *property,
+ GtkStateFlags state,
+ const GValue *value)
+{
+ const GtkStyleProperty *node;
+
+ g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
+ g_return_if_fail (property != NULL);
+ g_return_if_fail (value != NULL);
+
+ node = _gtk_style_property_lookup (property);
+
+ if (!node)
+ {
+ g_warning ("Style property \"%s\" is not registered", property);
+ return;
+ }
+
+ _gtk_style_properties_set_property_by_property (props,
+ node,
+ state,
+ value);
}
/**
GtkStateFlags state,
va_list args)
{
- GtkStylePropertiesPrivate *priv;
const gchar *property_name;
g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
- priv = props->priv;
property_name = va_arg (args, const gchar *);
while (property_name)
{
- PropertyNode *node;
- PropertyData *prop;
+ const GtkStyleProperty *node;
gchar *error = NULL;
- GValue *val;
+ GValue val = { 0 };
- node = property_node_lookup (g_quark_try_string (property_name));
+ node = _gtk_style_property_lookup (property_name);
if (!node)
{
break;
}
- prop = g_hash_table_lookup (priv->properties,
- GINT_TO_POINTER (node->property_quark));
-
- if (!prop)
- {
- prop = property_data_new ();
- g_hash_table_insert (priv->properties,
- GINT_TO_POINTER (node->property_quark),
- prop);
- }
-
- val = property_data_get_value (prop, state);
-
- if (G_IS_VALUE (val))
- g_value_unset (val);
-
- g_value_init (val, node->pspec->value_type);
- G_VALUE_COLLECT (val, args, 0, &error);
-
+ G_VALUE_COLLECT_INIT (&val, node->pspec->value_type,
+ args, 0, &error);
if (error)
{
g_warning ("Could not set style property \"%s\": %s", property_name, error);
- g_value_unset (val);
+ g_value_unset (&val);
g_free (error);
break;
}
+ _gtk_style_properties_set_property_by_property (props, node, state, &val);
+ g_value_unset (&val);
+
property_name = va_arg (args, const gchar *);
}
}
va_end (args);
}
-static gboolean
-resolve_color (GtkStyleProperties *props,
- GValue *value)
+/* NB: Will return NULL for shorthands */
+const GValue *
+_gtk_style_properties_peek_property (GtkStyleProperties *props,
+ const gchar *prop_name,
+ GtkStateFlags state,
+ const GtkStyleProperty **property)
{
- GdkRGBA color;
-
- /* Resolve symbolic color to GdkRGBA */
- if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
- return FALSE;
-
- /* Store it back, this is where GdkRGBA caching happens */
- g_value_unset (value);
- g_value_init (value, GDK_TYPE_RGBA);
- g_value_set_boxed (value, &color);
-
- return TRUE;
-}
-
-static gboolean
-resolve_color_rgb (GtkStyleProperties *props,
- GValue *value)
-{
- GdkColor color = { 0 };
- GdkRGBA rgba;
-
- if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
- return FALSE;
-
- color.red = rgba.red * 65535. + 0.5;
- color.green = rgba.green * 65535. + 0.5;
- color.blue = rgba.blue * 65535. + 0.5;
-
- g_value_unset (value);
- g_value_init (value, GDK_TYPE_COLOR);
- g_value_set_boxed (value, &color);
+ GtkStylePropertiesPrivate *priv;
+ const GtkStyleProperty *node;
+ PropertyData *prop;
+ GValue *val;
- return TRUE;
-}
+ g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
+ g_return_val_if_fail (prop_name != NULL, NULL);
-static gboolean
-resolve_gradient (GtkStyleProperties *props,
- GValue *value)
-{
- cairo_pattern_t *gradient;
+ node = _gtk_style_property_lookup (prop_name);
+ if (property)
+ *property = node;
- if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
- return FALSE;
+ if (!node)
+ {
+ g_warning ("Style property \"%s\" is not registered", prop_name);
+ return NULL;
+ }
- /* Store it back, this is where cairo_pattern_t caching happens */
- g_value_unset (value);
- g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
- g_value_take_boxed (value, gradient);
+ priv = props->priv;
+ prop = g_hash_table_lookup (priv->properties, node);
- return TRUE;
-}
+ if (!prop)
+ return NULL;
-static void
-lookup_default_value (PropertyNode *node,
- GValue *value)
-{
- g_value_init (value, node->pspec->value_type);
+ val = property_data_match_state (prop, state);
+ if (val == NULL)
+ return NULL;
+
+ _gtk_style_property_resolve (node, props, val);
- if (node->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
- g_value_set_object (value, gtk_theming_engine_load (NULL));
- else
- g_param_value_set_default (node->pspec, value);
+ return val;
}
/**
GtkStateFlags state,
GValue *value)
{
- GtkStylePropertiesPrivate *priv;
- PropertyNode *node;
- PropertyData *prop;
- GValue *val;
+ const GtkStyleProperty *node;
+ const GValue *val;
g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
g_return_val_if_fail (property != NULL, FALSE);
g_return_val_if_fail (value != NULL, FALSE);
- node = property_node_lookup (g_quark_try_string (property));
+ val = _gtk_style_properties_peek_property (props, property, state, &node);
if (!node)
- {
- g_warning ("Style property \"%s\" is not registered", property);
- return FALSE;
- }
-
- priv = props->priv;
- prop = g_hash_table_lookup (priv->properties,
- GINT_TO_POINTER (node->property_quark));
-
- if (!prop)
return FALSE;
g_value_init (value, node->pspec->value_type);
- val = property_data_match_state (prop, state);
-
- if (val && G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
- {
- if (node->pspec->value_type == GDK_TYPE_RGBA)
- {
- if (!resolve_color (props, val))
- return FALSE;
- }
- else if (node->pspec->value_type == GDK_TYPE_COLOR)
- {
- if (!resolve_color_rgb (props, val))
- return FALSE;
- }
- else
- return FALSE;
- }
- else if (val && G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
- {
- g_return_val_if_fail (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN, FALSE);
-
- if (!resolve_gradient (props, val))
- return FALSE;
- }
-
if (val)
- {
- g_param_value_validate (node->pspec, val);
- g_value_copy (val, value);
- }
+ g_value_copy (val, value);
+ else if (_gtk_style_property_is_shorthand (node))
+ _gtk_style_property_pack (node, props, state, value);
else
- {
- GValue default_value = { 0 };
-
- lookup_default_value (node, &default_value);
- g_value_copy (&default_value, value);
- g_value_unset (&default_value);
- }
+ _gtk_style_property_default_value (node, props, value);
return TRUE;
}
GtkStateFlags state,
va_list args)
{
- GtkStylePropertiesPrivate *priv;
const gchar *property_name;
g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
- priv = props->priv;
property_name = va_arg (args, const gchar *);
while (property_name)
{
- PropertyNode *node;
- PropertyData *prop;
+ const GtkStyleProperty *node;
gchar *error = NULL;
- GValue *val = NULL;
-
- node = property_node_lookup (g_quark_try_string (property_name));
+ const GValue *val;
+ val = _gtk_style_properties_peek_property (props, property_name, state, &node);
if (!node)
- {
- g_warning ("Style property \"%s\" is not registered", property_name);
- break;
- }
-
- prop = g_hash_table_lookup (priv->properties,
- GINT_TO_POINTER (node->property_quark));
-
- if (prop)
- val = property_data_match_state (prop, state);
+ break;
- if (val && G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
+ if (val)
{
- gboolean resolved;
-
- if (node->pspec->value_type == GDK_TYPE_RGBA)
- resolved = resolve_color (props, val);
- else if (node->pspec->value_type == GDK_TYPE_COLOR)
- resolved = resolve_color_rgb (props, val);
- else
- resolved = FALSE;
-
- if (!resolved)
- val = NULL;
+ G_VALUE_LCOPY (val, args, 0, &error);
}
- else if (val && G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
+ else if (_gtk_style_property_is_shorthand (node))
{
- g_return_if_fail (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
+ GValue packed = { 0 };
- if (!resolve_gradient (props, val))
- val = NULL;
- }
-
- if (val)
- {
- g_param_value_validate (node->pspec, val);
- G_VALUE_LCOPY (val, args, 0, &error);
+ g_value_init (&packed, node->pspec->value_type);
+ _gtk_style_property_pack (node, props, state, &packed);
+ G_VALUE_LCOPY (&packed, args, 0, &error);
+ g_value_unset (&packed);
}
else
{
GValue default_value = { 0 };
- lookup_default_value (node, &default_value);
+ g_value_init (&default_value, node->pspec->value_type);
+ _gtk_style_property_default_value (node, props, &default_value);
G_VALUE_LCOPY (&default_value, args, 0, &error);
g_value_unset (&default_value);
}
GtkStateFlags state)
{
GtkStylePropertiesPrivate *priv;
- PropertyNode *node;
+ const GtkStyleProperty *node;
PropertyData *prop;
guint pos;
g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
g_return_if_fail (property != NULL);
- node = property_node_lookup (g_quark_try_string (property));
+ node = _gtk_style_property_lookup (property);
if (!node)
{
}
priv = props->priv;
- prop = g_hash_table_lookup (priv->properties,
- GINT_TO_POINTER (node->property_quark));
+ prop = g_hash_table_lookup (priv->properties, node);
if (!prop)
return;
GValue *value;
data = &g_array_index (prop_to_merge->values, ValueData, i);
+
+ if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
+ G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
+ {
+ /* Let normal state override all states
+ * previously set in the original set
+ */
+ property_data_remove_values (prop);
+ }
+
value = property_data_get_value (prop, data->state);
if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
pango_font_description_merge (font_desc, font_desc_to_merge, replace);
}
+ else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
+ G_IS_VALUE (value))
+ {
+ GPtrArray *array, *array_to_merge;
+ gint i;
+
+ /* Append the array, mainly thought
+ * for the gtk-key-bindings property
+ */
+ array = g_value_get_boxed (value);
+ array_to_merge = g_value_get_boxed (&data->value);
+
+ for (i = 0; i < array_to_merge->len; i++)
+ g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
+ }
else if (replace || !G_IS_VALUE (value))
{
if (!G_IS_VALUE (value))