[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.
This commit is contained in:
Emmanuele Bassi 2009-06-04 12:15:15 +01:00
parent 5ebb59e6b0
commit 876dc22633

View File

@ -447,6 +447,29 @@ clutter_units_from_string (ClutterUnits *units,
return TRUE; 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 "<invalid>";
}
/** /**
* clutter_units_to_string: * clutter_units_to_string:
* @units: a #ClutterUnits * @units: a #ClutterUnits
@ -653,10 +676,25 @@ param_units_validate (GParamSpec *pspec,
{ {
ClutterParamSpecUnits *uspec = CLUTTER_PARAM_SPEC_UNITS (pspec); ClutterParamSpecUnits *uspec = CLUTTER_PARAM_SPEC_UNITS (pspec);
ClutterUnits *units = value->data[0].v_pointer; ClutterUnits *units = value->data[0].v_pointer;
ClutterUnitType otype = units->unit_type;
gfloat oval = units->value; gfloat oval = units->value;
g_assert (CLUTTER_IS_PARAM_SPEC_UNITS (pspec)); 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, units->value = CLAMP (units->value,
uspec->minimum, uspec->minimum,
uspec->maximum); uspec->maximum);