From 0ece7a5de3eae5f4d7e4d1623d191a0a0628e652 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 6 Mar 2012 14:16:08 +0100 Subject: [PATCH] css: Add GtkCssValue, an immutable refcounted css value --- gtk/Makefile.am | 2 + gtk/gtkcssvalue.c | 467 +++++++++++++++++++++++++++++++++++++++ gtk/gtkcssvalueprivate.h | 96 ++++++++ 3 files changed, 565 insertions(+) create mode 100644 gtk/gtkcssvalue.c create mode 100644 gtk/gtkcssvalueprivate.h diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 99ac63d358..41e42d5b28 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -438,6 +438,7 @@ gtk_private_h_sources = \ gtkcssshorthandpropertyprivate.h \ gtkcssstylefuncsprivate.h \ gtkcssstylepropertyprivate.h \ + gtkcssvalueprivate.h \ gtkcustompaperunixdialog.h \ gtkentryprivate.h \ gtkfilechooserdefault.h \ @@ -634,6 +635,7 @@ gtk_base_c_sources = \ gtkcssstylefuncs.c \ gtkcssstyleproperty.c \ gtkcssstylepropertyimpl.c \ + gtkcssvalue.c \ gtkcsstypes.c \ gtkdialog.c \ gtkdrawingarea.c \ diff --git a/gtk/gtkcssvalue.c b/gtk/gtkcssvalue.c new file mode 100644 index 0000000000..152297419b --- /dev/null +++ b/gtk/gtkcssvalue.c @@ -0,0 +1,467 @@ +/* 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 . + */ + +#include "config.h" + +#include "gtkcssvalueprivate.h" +#include "gtktypebuiltins.h" +#include "gtkgradient.h" +#include +#include "gtkprivatetypebuiltins.h" + +struct _GtkCssValue +{ + volatile gint ref_count; + GValue g_value; +}; + +G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref) + +static GtkCssValue * +_gtk_css_value_new (void) +{ + GtkCssValue *value; + + value = g_slice_new0 (GtkCssValue); + + value->ref_count = 1; + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_gvalue (const GValue *g_value) +{ + GtkCssValue *value; + + g_return_val_if_fail (g_value != NULL, NULL); + + value = _gtk_css_value_new (); + + g_value_init (&value->g_value, G_VALUE_TYPE (g_value)); + g_value_copy (g_value, &value->g_value); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_take_gvalue (GValue *g_value) +{ + GtkCssValue *value; + + g_return_val_if_fail (g_value != NULL, NULL); + + value = _gtk_css_value_new (); + value->g_value = *g_value; + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_int (gint val) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, G_TYPE_INT); + g_value_set_int (&value->g_value, val); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_take_string (char *string) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, G_TYPE_STRING); + g_value_take_string (&value->g_value, string); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_string (const char *string) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, G_TYPE_STRING); + g_value_set_string (&value->g_value, string); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_border (const GtkBorder *border) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GTK_TYPE_BORDER); + g_value_set_boxed (&value->g_value, border); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_take_pattern (cairo_pattern_t *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, CAIRO_GOBJECT_TYPE_PATTERN); + g_value_take_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_pattern (const cairo_pattern_t *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, CAIRO_GOBJECT_TYPE_PATTERN); + g_value_set_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_take_shadow (GtkShadow *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GTK_TYPE_SHADOW); + g_value_take_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_take_font_description (PangoFontDescription *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, PANGO_TYPE_FONT_DESCRIPTION); + g_value_take_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_take_image (GtkCssImage *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GTK_TYPE_CSS_IMAGE); + g_value_take_object (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_number (const GtkCssNumber *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GTK_TYPE_CSS_NUMBER); + g_value_set_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_rgba (const GdkRGBA *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GDK_TYPE_RGBA); + g_value_set_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_color (const GdkColor *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GDK_TYPE_COLOR); + g_value_set_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GTK_TYPE_CSS_BACKGROUND_SIZE); + g_value_set_boxed (&value->g_value, v); + + return value; +} + +GtkCssValue * +_gtk_css_value_new_take_symbolic_color (GtkSymbolicColor *v) +{ + GtkCssValue *value; + + value = _gtk_css_value_new (); + g_value_init (&value->g_value, GTK_TYPE_SYMBOLIC_COLOR); + g_value_take_boxed (&value->g_value, v); + + 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; + + g_value_unset (&value->g_value); + g_slice_free (GtkCssValue, value); +} + +GType +_gtk_css_value_get_content_type (GtkCssValue *value) +{ + return G_VALUE_TYPE (&value->g_value); +} + +gboolean +_gtk_css_value_holds (GtkCssValue *value, GType type) +{ + return G_VALUE_HOLDS (&value->g_value, type); +} + +void +_gtk_css_value_init_gvalue (GtkCssValue *value, + GValue *g_value) +{ + if (value != NULL) + { + g_value_init (g_value, G_VALUE_TYPE (&value->g_value)); + g_value_copy (&value->g_value, g_value); + } +} + +void +_gtk_css_value_to_gvalue (GtkCssValue *value, + GValue *g_value) +{ + if (G_VALUE_TYPE (&value->g_value) == G_VALUE_TYPE (g_value)) + g_value_copy (&value->g_value, g_value); + else if (g_value_type_transformable (G_VALUE_TYPE (&value->g_value), G_VALUE_TYPE (g_value))) + g_value_transform (&value->g_value, g_value); + else + g_warning ("can't convert css value of type `%s' as value of type `%s'", + G_VALUE_TYPE_NAME (&value->g_value), + G_VALUE_TYPE_NAME (g_value)); +} + +gboolean +_gtk_css_value_is_special (GtkCssValue *value) +{ + return _gtk_css_value_holds (value, GTK_TYPE_CSS_SPECIAL_VALUE); +} + +GtkCssSpecialValue +_gtk_css_value_get_special_kind (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_SPECIAL_VALUE), 0); + return g_value_get_enum (&value->g_value); +} + +GtkCssNumber * +_gtk_css_value_get_number (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_NUMBER), NULL); + return g_value_get_boxed (&value->g_value); +} + +GtkSymbolicColor * +_gtk_css_value_get_symbolic_color (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SYMBOLIC_COLOR), NULL); + return g_value_get_boxed (&value->g_value); +} + +int +_gtk_css_value_get_int (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_INT), 0); + return g_value_get_int (&value->g_value); +} + +double +_gtk_css_value_get_double (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_DOUBLE), 0); + return g_value_get_double (&value->g_value); +} + +const char * +_gtk_css_value_get_string (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRING), 0); + return g_value_get_string (&value->g_value); +} + +gpointer +_gtk_css_value_dup_object (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL); + return g_value_dup_object (&value->g_value); +} + +gpointer +_gtk_css_value_get_object (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_OBJECT), NULL); + return g_value_get_object (&value->g_value); +} + +gpointer +_gtk_css_value_get_boxed (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_BOXED), NULL); + return g_value_get_boxed (&value->g_value); +} + +const char ** +_gtk_css_value_get_strv (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, G_TYPE_STRV), NULL); + return g_value_get_boxed (&value->g_value); +} + +GtkCssImage * +_gtk_css_value_get_image (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_IMAGE), NULL); + return g_value_get_object (&value->g_value); +} + +GtkBorderStyle +_gtk_css_value_get_border_style (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_BORDER_STYLE), 0); + return g_value_get_enum (&value->g_value); +} + +GtkCssBackgroundSize * +_gtk_css_value_get_background_size (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_SIZE), NULL); + return g_value_get_boxed (&value->g_value); +} + +GtkCssBorderImageRepeat * +_gtk_css_value_get_border_image_repeat (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT), NULL); + return g_value_get_boxed (&value->g_value); +} + +GtkCssBorderCornerRadius * +_gtk_css_value_get_border_corner_radius (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS), NULL); + return g_value_get_boxed (&value->g_value); +} + +PangoFontDescription * +_gtk_css_value_get_font_description (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_FONT_DESCRIPTION), 0); + return g_value_get_boxed (&value->g_value); +} + +PangoStyle +_gtk_css_value_get_pango_style (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_STYLE), 0); + return g_value_get_enum (&value->g_value); +} + +PangoVariant +_gtk_css_value_get_pango_variant (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_VARIANT), 0); + return g_value_get_enum (&value->g_value); +} + +PangoWeight +_gtk_css_value_get_pango_weight (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, PANGO_TYPE_WEIGHT), 0); + return g_value_get_enum (&value->g_value); +} + +GdkRGBA * +_gtk_css_value_get_rgba (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GDK_TYPE_RGBA), NULL); + return g_value_get_boxed (&value->g_value); +} + +cairo_pattern_t * +_gtk_css_value_get_pattern (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, CAIRO_GOBJECT_TYPE_PATTERN), NULL); + return g_value_get_boxed (&value->g_value); +} + +GtkGradient * +_gtk_css_value_get_gradient (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_GRADIENT), NULL); + return g_value_get_boxed (&value->g_value); +} + +GtkShadow * +_gtk_css_value_get_shadow (GtkCssValue *value) +{ + g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_SHADOW), NULL); + return g_value_get_boxed (&value->g_value); +} diff --git a/gtk/gtkcssvalueprivate.h b/gtk/gtkcssvalueprivate.h new file mode 100644 index 0000000000..62f17a320f --- /dev/null +++ b/gtk/gtkcssvalueprivate.h @@ -0,0 +1,96 @@ +/* + * Copyright © 2012 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.1 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 . + * + * Authors: Alexander Larsson + */ + +#ifndef __GTK_CSS_VALUE_PRIVATE_H__ +#define __GTK_CSS_VALUE_PRIVATE_H__ + +#include +#include "gtkcsstypesprivate.h" +#include "gtksymboliccolor.h" +#include "gtkcssimageprivate.h" +#include "gtkshadowprivate.h" + +G_BEGIN_DECLS + +#define GTK_TYPE_CSS_VALUE (_gtk_css_value_get_type ()) +#define GTK_CSS_VALUE(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_CSS_VALUE, GtkCssValue)) +#define GTK_CSS_VALUE_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_CSS_VALUE, GtkCssValueClass)) +#define GTK_IS_CSS_VALUE(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_CSS_VALUE)) +#define GTK_IS_CSS_VALUE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_CSS_VALUE)) +#define GTK_CSS_VALUE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CSS_VALUE, GtkCssValueClass)) + +typedef struct _GtkCssValue GtkCssValue; + +/* A GtkCssValue is a refcounted immutable value type */ + +GType _gtk_css_value_get_type (void) G_GNUC_CONST; +GtkCssValue *_gtk_css_value_ref (GtkCssValue *value); +void _gtk_css_value_unref (GtkCssValue *value); +GType _gtk_css_value_get_content_type (GtkCssValue *value); +gboolean _gtk_css_value_holds (GtkCssValue *value, + GType type); +GtkCssValue *_gtk_css_value_new_from_gvalue (const GValue *g_value); +GtkCssValue *_gtk_css_value_new_take_gvalue (GValue *g_value); +GtkCssValue *_gtk_css_value_new_from_int (gint val); +GtkCssValue *_gtk_css_value_new_from_string (const char *string); +GtkCssValue *_gtk_css_value_new_take_string (char *string); +GtkCssValue *_gtk_css_value_new_take_font_description (PangoFontDescription *v); +GtkCssValue *_gtk_css_value_new_from_rgba (const GdkRGBA *v); +GtkCssValue *_gtk_css_value_new_from_color (const GdkColor *v); +GtkCssValue *_gtk_css_value_new_take_symbolic_color (GtkSymbolicColor *v); +GtkCssValue *_gtk_css_value_new_from_border (const GtkBorder *border); +GtkCssValue *_gtk_css_value_new_take_pattern (cairo_pattern_t *v); +GtkCssValue *_gtk_css_value_new_from_pattern (const cairo_pattern_t *v); +GtkCssValue *_gtk_css_value_new_take_shadow (GtkShadow *v); +GtkCssValue *_gtk_css_value_new_take_image (GtkCssImage *v); +GtkCssValue *_gtk_css_value_new_from_number (const GtkCssNumber *v); +GtkCssValue *_gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v); +void _gtk_css_value_to_gvalue (GtkCssValue *value, + GValue *g_value); +void _gtk_css_value_init_gvalue (GtkCssValue *value, + GValue *g_value); + +gboolean _gtk_css_value_is_special (GtkCssValue *value); +GtkCssSpecialValue _gtk_css_value_get_special_kind (GtkCssValue *value); +int _gtk_css_value_get_int (GtkCssValue *value); +double _gtk_css_value_get_double (GtkCssValue *value); +const char * _gtk_css_value_get_string (GtkCssValue *value); +gpointer _gtk_css_value_dup_object (GtkCssValue *value); +gpointer _gtk_css_value_get_object (GtkCssValue *value); +gpointer _gtk_css_value_get_boxed (GtkCssValue *value); +const char ** _gtk_css_value_get_strv (GtkCssValue *value); +GtkCssNumber *_gtk_css_value_get_number (GtkCssValue *value); +GtkSymbolicColor *_gtk_css_value_get_symbolic_color (GtkCssValue *value); +GtkCssImage *_gtk_css_value_get_image (GtkCssValue *value); +GtkBorderStyle _gtk_css_value_get_border_style (GtkCssValue *value); +GtkCssBackgroundSize *_gtk_css_value_get_background_size (GtkCssValue *value); +GtkCssBorderCornerRadius *_gtk_css_value_get_border_corner_radius (GtkCssValue *value); +GtkCssBorderImageRepeat *_gtk_css_value_get_border_image_repeat (GtkCssValue *value); +PangoFontDescription * _gtk_css_value_get_font_description (GtkCssValue *value); +PangoStyle _gtk_css_value_get_pango_style (GtkCssValue *value); +PangoVariant _gtk_css_value_get_pango_variant (GtkCssValue *value); +PangoWeight _gtk_css_value_get_pango_weight (GtkCssValue *value); +GdkRGBA *_gtk_css_value_get_rgba (GtkCssValue *value); +cairo_pattern_t *_gtk_css_value_get_pattern (GtkCssValue *value); +GtkGradient *_gtk_css_value_get_gradient (GtkCssValue *value); +GtkShadow *_gtk_css_value_get_shadow (GtkCssValue *value); + +G_END_DECLS + +#endif /* __GTK_CSS_VALUE_PRIVATE_H__ */