diff --git a/ChangeLog b/ChangeLog index 8bc7f346d..ff6bb5a07 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2008-10-17 Thomas Wood + + reviewed by: Emmanuele Bassi + + * clutter/clutter-color.[ch]: Add GParamSpec and GValue integration + for ClutterColor. With ClutterParamSpecColor it is possible to define + color properties; with the GValue integration it's possible to + automatically transform strings into colors and vice versa. + + (clutter_color_free): Allow NULL parameter to match other boxed + types destructors. + + (clutter_color_new): Add a constructor for the ClutterColor boxed + type, mostly for bindings. + 2008-10-17 Emmanuele Bassi * clutter/clutter-color.c (clutter_color_parse): Add checks diff --git a/clutter/clutter-color.c b/clutter/clutter-color.c index 1cca736a3..3e307c485 100644 --- a/clutter/clutter-color.c +++ b/clutter/clutter-color.c @@ -35,6 +35,7 @@ #endif #include +#include #include "clutter-main.h" #include "clutter-color.h" @@ -615,19 +616,299 @@ clutter_color_copy (const ClutterColor *color) void clutter_color_free (ClutterColor *color) { - g_return_if_fail (color != NULL); + if (G_LIKELY (color)) + g_slice_free (ClutterColor, color); +} - g_slice_free (ClutterColor, color); +/** + * clutter_color_new: + * @red: red component of the color, between 0 and 255 + * @green: green component of the color, between 0 and 255 + * @blue: blue component of the color, between 0 and 255 + * @alpha: alpha component of the color, between 0 and 255 + * + * Creates a new #ClutterColor with the given values. + * + * Return value: the newly allocated color. Use clutter_color_free() + * when done + * + * Since: 1.0 + */ +ClutterColor * +clutter_color_new (guint8 red, + guint8 green, + guint8 blue, + guint8 alpha) +{ + ClutterColor *color; + + color = g_slice_new (ClutterColor); + + color->red = CLAMP (red, 0, 255); + color->green = CLAMP (green, 0, 255); + color->blue = CLAMP (blue, 0, 255); + color->alpha = CLAMP (alpha, 0, 255); + + return color; +} + +static void +clutter_value_transform_color_string (const GValue *src, + GValue *dest) +{ + gchar *string = clutter_color_to_string (src->data[0].v_pointer); + + g_value_take_string (dest, string); +} + +static void +clutter_value_transform_string_color (const GValue *src, + GValue *dest) +{ + ClutterColor color = { 0, }; + + clutter_color_parse (g_value_get_string (src), &color); + + clutter_value_set_color (dest, &color); } GType clutter_color_get_type (void) { - static GType our_type = 0; + static GType _clutter_color_type = 0; - if (!our_type) - our_type = g_boxed_type_register_static (I_("ClutterColor"), - (GBoxedCopyFunc) clutter_color_copy, - (GBoxedFreeFunc) clutter_color_free); - return our_type; + if (G_UNLIKELY (_clutter_color_type == 0)) + { + _clutter_color_type = + g_boxed_type_register_static (I_("ClutterColor"), + (GBoxedCopyFunc) clutter_color_copy, + (GBoxedFreeFunc) clutter_color_free); + + g_value_register_transform_func (_clutter_color_type, G_TYPE_STRING, + clutter_value_transform_color_string); + g_value_register_transform_func (G_TYPE_STRING, _clutter_color_type, + clutter_value_transform_string_color); + } + + return _clutter_color_type; +} + +static void +clutter_value_init_color (GValue *value) +{ + value->data[0].v_pointer = NULL; +} + +static void +clutter_value_free_color (GValue *value) +{ + if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)) + clutter_color_free (value->data[0].v_pointer); +} + +static void +clutter_value_copy_color (const GValue *src, + GValue *dest) +{ + dest->data[0].v_pointer = clutter_color_copy (src->data[0].v_pointer); +} + +static gpointer +clutter_value_peek_color (const GValue *value) +{ + return value->data[0].v_pointer; +} + +static gchar * +clutter_value_collect_color (GValue *value, + guint n_collect_values, + GTypeCValue *collect_values, + guint collect_flags) +{ + if (!collect_values[0].v_pointer) + value->data[0].v_pointer = NULL; + else + { + if (collect_flags & G_VALUE_NOCOPY_CONTENTS) + { + value->data[0].v_pointer = collect_values[0].v_pointer; + value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS; + } + else + { + value->data[0].v_pointer = + clutter_color_copy (collect_values[0].v_pointer); + } + } + + return NULL; +} + +static gchar * +clutter_value_lcopy_color (const GValue *value, + guint n_collect_values, + GTypeCValue *collect_values, + guint collect_flags) +{ + ClutterColor **color_p = collect_values[0].v_pointer; + + if (!color_p) + return g_strdup_printf ("value location for `%s' passed as NULL", + G_VALUE_TYPE_NAME (value)); + + if (!value->data[0].v_pointer) + *color_p = NULL; + else + { + if (collect_flags & G_VALUE_NOCOPY_CONTENTS) + *color_p = value->data[0].v_pointer; + else + *color_p = clutter_color_copy (value->data[0].v_pointer); + } + + return NULL; +} + +/** + * clutter_value_set_color: + * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR + * @color: the color to set + * + * Sets @value to @color. + * + * Since: 1.0 + */ +void +clutter_value_set_color (GValue *value, + ClutterColor *color) +{ + g_return_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value)); + + value->data[0].v_pointer = clutter_color_copy (color); +} + +/** + * clutter_value_get_color: + * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR + * + * Gets the #ClutterColor contained in @value. + * + * Return value: the colors inside the passed #GValue + * + * Since: 1.0 + */ +const ClutterColor * +clutter_value_get_color (const GValue *value) +{ + g_return_val_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value), NULL); + + return value->data[0].v_pointer; +} + +static void +param_color_init (GParamSpec *pspec) +{ + ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec); + + cspec->default_value = NULL; +} + +static void +param_color_finalize (GParamSpec *pspec) +{ + ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec); + + clutter_color_free (cspec->default_value); +} + +static void +param_color_set_default (GParamSpec *pspec, + GValue *value) +{ + value->data[0].v_pointer = CLUTTER_PARAM_SPEC_COLOR (pspec)->default_value; +} + +static gint +param_color_values_cmp (GParamSpec *pspec, + const GValue *value1, + const GValue *value2) +{ + guint32 color1, color2; + + color1 = clutter_color_to_pixel (value1->data[0].v_pointer); + color2 = clutter_color_to_pixel (value2->data[0].v_pointer); + + if (color1 < color2) + return -1; + else if (color1 == color2) + return 0; + else + return 1; +} + +static const GTypeValueTable _clutter_color_value_table = { + clutter_value_init_color, + clutter_value_free_color, + clutter_value_copy_color, + clutter_value_peek_color, + "p", + clutter_value_collect_color, + "p", + clutter_value_lcopy_color +}; + +GType +clutter_param_color_get_type (void) +{ + static GType pspec_type = 0; + + if (G_UNLIKELY (pspec_type == 0)) + { + const GParamSpecTypeInfo pspec_info = { + sizeof (ClutterParamSpecColor), + 16, + param_color_init, + CLUTTER_TYPE_COLOR, + param_color_finalize, + param_color_set_default, + NULL, + param_color_values_cmp, + }; + + pspec_type = g_param_type_register_static (I_("ClutterParamSpecColor"), + &pspec_info); + } + + return pspec_type; +} + +/** + * clutter_param_spec_color: + * @name: name of the property + * @nick: short name + * @blurb: description (can be translatable) + * @default_value: default value + * @flags: flags for the param spec + * + * Creates a #GParamSpec for properties using #ClutterColor. + * + * Return value: the newly created #GParamSpec + * + * Since: 1.0 + */ +GParamSpec * +clutter_param_spec_color (const gchar *name, + const gchar *nick, + const gchar *blurb, + const ClutterColor *default_value, + GParamFlags flags) +{ + ClutterParamSpecColor *cspec; + + cspec = g_param_spec_internal (CLUTTER_TYPE_PARAM_COLOR, + name, nick, blurb, flags); + + cspec->default_value = clutter_color_copy (default_value); + + return G_PARAM_SPEC (cspec); } diff --git a/clutter/clutter-color.h b/clutter/clutter-color.h index 83bab6b15..f2b14987f 100644 --- a/clutter/clutter-color.h +++ b/clutter/clutter-color.h @@ -103,6 +103,50 @@ guint32 clutter_color_to_pixel (const ClutterColor *src); void clutter_color_from_pixel (ClutterColor *dest, guint32 pixel); + + + +#define CLUTTER_TYPE_PARAM_COLOR (clutter_param_color_get_type ()) +#define CLUTTER_PARAM_SPEC_COLOR(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), CLUTTER_TYPE_PARAM_COLOR, ClutterParamSpecColor)) +#define CLUTTER_IS_PARAM_SPEC_COLOR(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), CLUTTER_TYPE_PARAM_COLOR)) +#define CLUTTER_VALUE_HOLDS_COLOR(x) (G_VALUE_HOLDS ((x), CLUTTER_TYPE_COLOR)) + +/** + * CLUTTER_VALUE_HOLDS_COLOR: + * @x: a #GValue + * + * Evaluates to %TRUE if @x holds a #ClutterColor. + * + * Since: 1.0 + */ + +typedef struct _ClutterParamSpecColor ClutterParamSpecColor; + +/** + * ClutterParamSpecColor: + * @default_value: default color value + */ +struct _ClutterParamSpecColor +{ + /*< private >*/ + GParamSpec parent_instance; + + /*< public >*/ + ClutterColor *default_value; +}; + +GType clutter_param_color_get_type (void) G_GNUC_CONST; + +void clutter_value_set_color (GValue *value, + ClutterColor *color); +const ClutterColor *clutter_value_get_color (const GValue *value); + +GParamSpec *clutter_param_spec_color (const gchar *name, + const gchar *nick, + const gchar *blurb, + const ClutterColor *default_value, + GParamFlags flags); + G_END_DECLS -#endif +#endif /* _HAVE_CLUTTER_COLOR_H */ diff --git a/doc/reference/ChangeLog b/doc/reference/ChangeLog index 2ee577fb6..45f9f67bc 100644 --- a/doc/reference/ChangeLog +++ b/doc/reference/ChangeLog @@ -1,3 +1,8 @@ +2008-10-17 Emmanuele Bassi + + * clutter/clutter-sections.txt: Add the new ClutterColor + symbols. + 2008-09-25 Emmanuele Bassi * clutter/clutter-sections.txt: Add diff --git a/doc/reference/clutter/clutter-sections.txt b/doc/reference/clutter/clutter-sections.txt index f6b1f9a96..5953c621d 100644 --- a/doc/reference/clutter/clutter-sections.txt +++ b/doc/reference/clutter/clutter-sections.txt @@ -972,12 +972,17 @@ clutter_param_fixed_get_type clutter-color Colors ClutterColor +clutter_color_new clutter_color_copy clutter_color_free + + clutter_color_parse clutter_color_from_hls clutter_color_from_hlsx clutter_color_from_pixel + + clutter_color_add clutter_color_subtract clutter_color_equal @@ -985,14 +990,29 @@ clutter_color_lighten clutter_color_darken clutter_color_shade clutter_color_shadex + + clutter_color_to_hls clutter_color_to_hlsx clutter_color_to_pixel clutter_color_to_string + + +ClutterParamSpecColor +clutter_param_spec_color +CLUTTER_VALUE_HOLDS_COLOR +clutter_value_set_color +clutter_value_get_color + CLUTTER_TYPE_COLOR +CLUTTER_TYPE_PARAM_COLOR +CLUTTER_PARAM_SPEC_COLOR +CLUTTER_IS_PARAM_SPEC_COLOR + clutter_color_get_type +clutter_param_color_get_type