[units] Add more conversion functions

A GValue containing a ClutterUnit should be transformable into a
GValue holding an integer, a floating point value or a fixed point
value.

This means adding more transformation functions when registering
the ClutterUnit GType.
This commit is contained in:
Emmanuele Bassi 2009-03-09 17:35:08 +00:00
parent 6bee140e74
commit d1f3190f5b
2 changed files with 85 additions and 11 deletions

View File

@ -160,7 +160,7 @@ clutter_units_pt (gdouble pt)
* @em: em to convert * @em: em to convert
* *
* Converts a value in em to #ClutterUnit<!-- -->s at the * Converts a value in em to #ClutterUnit<!-- -->s at the
* current DPI. * current DPI
* *
* Return value: the value in units * Return value: the value in units
* *
@ -176,6 +176,42 @@ clutter_units_em (gdouble em)
return em * _clutter_backend_get_units_per_em (backend); return em * _clutter_backend_get_units_per_em (backend);
} }
/**
* clutter_units_pixels:
* @px: pixels to convert
*
* Converts a value in pixels to #ClutterUnit<!-- -->s
*
* Return value: the value in units
*
* Since: 1.0
*/
ClutterUnit
clutter_units_pixels (gint px)
{
return CLUTTER_UNITS_FROM_INT (px);
}
/**
* clutter_units_to_pixels:
* @units: units to convert
*
* Converts a value in #ClutterUnit<!-- -->s to pixels
*
* Return value: the value in pixels
*
* Since: 1.0
*/
gint
clutter_units_to_pixels (ClutterUnit units)
{
return CLUTTER_UNITS_TO_INT (units);
}
/*
* GValue and GParamSpec integration
*/
static GTypeInfo _info = { static GTypeInfo _info = {
0, 0,
NULL, NULL,
@ -246,6 +282,36 @@ clutter_value_transform_int_unit (const GValue *src,
dest->data[0].v_float = CLUTTER_UNITS_FROM_INT (src->data[0].v_int); dest->data[0].v_float = CLUTTER_UNITS_FROM_INT (src->data[0].v_int);
} }
static void
clutter_value_transform_unit_float (const GValue *src,
GValue *dest)
{
dest->data[0].v_float = CLUTTER_UNITS_TO_FLOAT (src->data[0].v_float);
}
static void
clutter_value_transform_float_unit (const GValue *src,
GValue *dest)
{
dest->data[0].v_float = CLUTTER_UNITS_FROM_FLOAT (src->data[0].v_float);
}
#if 0
static void
clutter_value_transform_unit_fixed (const GValue *src,
GValue *dest)
{
dest->data[0].v_int = CLUTTER_UNITS_TO_FIXED (src->data[0].v_float);
}
static void
clutter_value_transform_fixed_unit (const GValue *src,
GValue *dest)
{
dest->data[0].v_float = CLUTTER_UNITS_FROM_FIXED (src->data[0].v_int);
}
#endif
static const GTypeValueTable _clutter_unit_value_table = { static const GTypeValueTable _clutter_unit_value_table = {
clutter_value_init_unit, clutter_value_init_unit,
NULL, NULL,
@ -274,6 +340,11 @@ clutter_unit_get_type (void)
clutter_value_transform_unit_int); clutter_value_transform_unit_int);
g_value_register_transform_func (G_TYPE_INT, _clutter_unit_type, g_value_register_transform_func (G_TYPE_INT, _clutter_unit_type,
clutter_value_transform_int_unit); clutter_value_transform_int_unit);
g_value_register_transform_func (_clutter_unit_type, G_TYPE_FLOAT,
clutter_value_transform_unit_float);
g_value_register_transform_func (G_TYPE_FLOAT, _clutter_unit_type,
clutter_value_transform_float_unit);
} }
return _clutter_unit_type; return _clutter_unit_type;

View File

@ -39,8 +39,8 @@ G_BEGIN_DECLS
/** /**
* ClutterUnit: * ClutterUnit:
* *
* Device independent unit used by Clutter. The value held can be transformed * Device independent unit used by Clutter. The value held can be
* into other units, likes pixels. * transformed into other units, likes pixels.
* *
* Since: 0.4 * Since: 0.4
*/ */
@ -52,8 +52,8 @@ typedef float ClutterUnit;
#define CLUTTER_UNITS_FROM_FLOAT(x) (x) #define CLUTTER_UNITS_FROM_FLOAT(x) (x)
#define CLUTTER_UNITS_TO_FLOAT(x) (x) #define CLUTTER_UNITS_TO_FLOAT(x) (x)
#define CLUTTER_UNITS_FROM_FIXED(x) (x) #define CLUTTER_UNITS_FROM_FIXED(x) (COGL_FIXED_TO_FLOAT (x))
#define CLUTTER_UNITS_TO_FIXED(x) (x) #define CLUTTER_UNITS_TO_FIXED(x) (COGL_FIXED_FROM_FLOAT (x))
/** /**
* CLUTTER_UNITS_FORMAT: * CLUTTER_UNITS_FORMAT:
@ -79,7 +79,7 @@ typedef float ClutterUnit;
* *
* Since: 0.6 * Since: 0.6
*/ */
#define CLUTTER_UNITS_FROM_DEVICE(x) CLUTTER_UNITS_FROM_INT ((x)) #define CLUTTER_UNITS_FROM_DEVICE(x) (clutter_units_pixels ((x)))
/** /**
* CLUTTER_UNITS_TO_DEVICE: * CLUTTER_UNITS_TO_DEVICE:
@ -89,7 +89,7 @@ typedef float ClutterUnit;
* *
* Since: 0.6 * Since: 0.6
*/ */
#define CLUTTER_UNITS_TO_DEVICE(x) CLUTTER_UNITS_TO_INT ((x)) #define CLUTTER_UNITS_TO_DEVICE(x) (clutter_units_to_pixels ((x)))
/** /**
* CLUTTER_UNITS_FROM_PANGO_UNIT: * CLUTTER_UNITS_FROM_PANGO_UNIT:
@ -99,7 +99,7 @@ typedef float ClutterUnit;
* *
* Since: 0.6 * Since: 0.6
*/ */
#define CLUTTER_UNITS_FROM_PANGO_UNIT(x) ((float)((x) / 1024)) #define CLUTTER_UNITS_FROM_PANGO_UNIT(x) ((float)((x) / 1024.0))
/** /**
* CLUTTER_UNITS_TO_PANGO_UNIT: * CLUTTER_UNITS_TO_PANGO_UNIT:
@ -144,6 +144,9 @@ typedef float ClutterUnit;
ClutterUnit clutter_units_mm (gdouble mm); ClutterUnit clutter_units_mm (gdouble mm);
ClutterUnit clutter_units_pt (gdouble pt); ClutterUnit clutter_units_pt (gdouble pt);
ClutterUnit clutter_units_em (gdouble em); ClutterUnit clutter_units_em (gdouble em);
ClutterUnit clutter_units_pixels (gint px);
gint clutter_units_to_pixels (ClutterUnit units);
#define CLUTTER_TYPE_UNIT (clutter_unit_get_type ()) #define CLUTTER_TYPE_UNIT (clutter_unit_get_type ())
#define CLUTTER_TYPE_PARAM_UNIT (clutter_param_unit_get_type ()) #define CLUTTER_TYPE_PARAM_UNIT (clutter_param_unit_get_type ())