From 876dc22633a62c3eb451d9f491d97cddadf6d3c0 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 4 Jun 2009 12:15:15 +0100 Subject: [PATCH] [units] Validate units against the ParamSpec When declaring a property using ClutterParamSpecUnits we pass a default type to limit the type of units we accept as valid values for the property. This means that we need to add the unit type check as part of the validation process. --- clutter/clutter-units.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/clutter/clutter-units.c b/clutter/clutter-units.c index 2488a109a..97b2d9a05 100644 --- a/clutter/clutter-units.c +++ b/clutter/clutter-units.c @@ -447,6 +447,29 @@ clutter_units_from_string (ClutterUnits *units, return TRUE; } +static const gchar * +clutter_unit_type_name (ClutterUnitType unit_type) +{ + switch (unit_type) + { + case CLUTTER_UNIT_MM: + return "mm"; + + case CLUTTER_UNIT_POINT: + return "pt"; + + case CLUTTER_UNIT_EM: + return "em"; + + case CLUTTER_UNIT_PIXEL: + return "px"; + } + + g_warning ("Invalid unit type %d", (int) unit_type); + + return ""; +} + /** * clutter_units_to_string: * @units: a #ClutterUnits @@ -653,10 +676,25 @@ param_units_validate (GParamSpec *pspec, { ClutterParamSpecUnits *uspec = CLUTTER_PARAM_SPEC_UNITS (pspec); ClutterUnits *units = value->data[0].v_pointer; + ClutterUnitType otype = units->unit_type; gfloat oval = units->value; g_assert (CLUTTER_IS_PARAM_SPEC_UNITS (pspec)); + if (otype != uspec->default_type) + { + gchar *str = clutter_units_to_string (units); + + g_warning ("The units value of '%s' does not have the same unit " + "type as declared by the ClutterParamSpecUnits of '%s'", + str, + clutter_unit_type_name (otype)); + + g_free (str); + + return FALSE; + } + units->value = CLAMP (units->value, uspec->minimum, uspec->maximum);