diff --git a/clutter/clutter-actor.c b/clutter/clutter-actor.c index e0903fe38..be3603759 100644 --- a/clutter/clutter-actor.c +++ b/clutter/clutter-actor.c @@ -6524,6 +6524,8 @@ parse_units (ClutterActor *self, if (end[0] == '%' && end[1] == '\0') { + ClutterActor *stage; + if (CLUTTER_PRIVATE_FLAGS (self) & CLUTTER_ACTOR_IS_TOPLEVEL) { g_warning ("Unable to set percentage of %s on a top-level " @@ -6536,12 +6538,20 @@ parse_units (ClutterActor *self, goto out; } + stage = clutter_actor_get_stage (self); + if (stage == NULL) + stage = clutter_stage_get_default (); + if (dimension == PARSE_X || dimension == PARSE_WIDTH || dimension == PARSE_ANCHOR_X) - retval = CLUTTER_UNITS_FROM_STAGE_WIDTH_PERCENTAGE (val); + { + retval = clutter_actor_get_widthu (stage) * val; + } else - retval = CLUTTER_UNITS_FROM_STAGE_HEIGHT_PERCENTAGE (val); + { + retval = clutter_actor_get_heightu (stage) * val; + } goto out; } @@ -6556,7 +6566,12 @@ parse_units (ClutterActor *self, } else if (G_VALUE_HOLDS (&value, G_TYPE_DOUBLE)) { - gint val; + ClutterActor *stage; + gdouble val; + + stage = clutter_actor_get_stage (self); + if (stage == NULL) + stage = clutter_stage_get_default (); if (CLUTTER_PRIVATE_FLAGS (self) & CLUTTER_ACTOR_IS_TOPLEVEL) { @@ -6569,14 +6584,18 @@ parse_units (ClutterActor *self, goto out; } - val = CLAMP (g_value_get_double (&value) * 100, 0, 100); + val = g_value_get_double (&value); if (dimension == PARSE_X || dimension == PARSE_WIDTH || dimension == PARSE_ANCHOR_X) - retval = CLUTTER_UNITS_FROM_STAGE_WIDTH_PERCENTAGE (val); + { + retval = clutter_actor_get_widthu (stage) * val; + } else - retval = CLUTTER_UNITS_FROM_STAGE_HEIGHT_PERCENTAGE (val); + { + retval = clutter_actor_get_heightu (stage) * val; + } } else { diff --git a/clutter/clutter-units.c b/clutter/clutter-units.c index 97dce9790..cdcb8390b 100644 --- a/clutter/clutter-units.c +++ b/clutter/clutter-units.c @@ -101,6 +101,58 @@ #include "clutter-units.h" #include "clutter-private.h" +#define DPI_FALLBACK 96.0 + +/** + * clutter_units_mm: + * @mm: millimeters to convert + * + * Converts a value in millimeters to #ClutterUnits at + * the current DPI. + * + * Return value: the value in units + * + * Since: 1.0 + */ +ClutterUnit +clutter_units_mm (gdouble mm) +{ + ClutterBackend *backend; + gdouble dpi; + + backend = clutter_get_default_backend (); + dpi = clutter_backend_get_resolution (backend); + if (dpi < 0) + dpi = DPI_FALLBACK; + + return mm * dpi / 25.4; +} + +/** + * clutter_units_pt: + * @pt: typographic points to convert + * + * Converts a value in typographic points to #ClutterUnits + * at the current DPI. + * + * Return value: the value in units + * + * Since: 1.0 + */ +ClutterUnit +clutter_units_pt (gdouble pt) +{ + ClutterBackend *backend; + gdouble dpi; + + backend = clutter_get_default_backend (); + dpi = clutter_backend_get_resolution (backend); + if (dpi < 0) + dpi = DPI_FALLBACK; + + return pt * dpi / 72.0; +} + static GTypeInfo _info = { 0, NULL, diff --git a/clutter/clutter-units.h b/clutter/clutter-units.h index 1084095d9..8eb24d102 100644 --- a/clutter/clutter-units.h +++ b/clutter/clutter-units.h @@ -4,9 +4,11 @@ * * An OpenGL based 'interactive canvas' library. * - * Authored By Tomas Frydrych + * Authored By: Tomas Frydrych + * Emmanuele Bassu * - * Copyright (C) 2007 OpenedHand + * Copyright (C) 2007, 2008 OpenedHand + * Copyright (C) 2009 Intel Corp. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -44,12 +46,6 @@ G_BEGIN_DECLS */ typedef float ClutterUnit; -/* - * Currently CLUTTER_UNIT maps directly onto ClutterFixed. Nevertheless, the - * _FROM_FIXED and _TO_FIXED macros should always be used in case that we - * decide to change this relationship in the future. - */ - #define CLUTTER_UNITS_FROM_INT(x) ((float)(x)) #define CLUTTER_UNITS_TO_INT(x) ((int)(x)) @@ -59,6 +55,20 @@ typedef float ClutterUnit; #define CLUTTER_UNITS_FROM_FIXED(x) (x) #define CLUTTER_UNITS_TO_FIXED(x) (x) +/** + * CLUTTER_UNITS_FORMAT: + * + * Format string that should be used for scanning and printing units. + * It is a string literal, but it does not include the percent sign to + * allow precision and length modifiers between the percent sign and + * the format: + * + * |[ + * g_print ("%" CLUTTER_UNITS_FORMAT, units); + * ]| + * + * Since: 1.0 + */ #define CLUTTER_UNITS_FORMAT "f" /** @@ -81,9 +91,6 @@ typedef float ClutterUnit; */ #define CLUTTER_UNITS_TO_DEVICE(x) CLUTTER_UNITS_TO_INT ((x)) -#define CLUTTER_UNITS_TMP_FROM_DEVICE(x) (x) -#define CLUTTER_UNITS_TMP_TO_DEVICE(x) (x) - /** * CLUTTER_UNITS_FROM_PANGO_UNIT: * @x: value in Pango units @@ -92,7 +99,7 @@ typedef float ClutterUnit; * * Since: 0.6 */ -#define CLUTTER_UNITS_FROM_PANGO_UNIT(x) ((float)(x / 1024)) +#define CLUTTER_UNITS_FROM_PANGO_UNIT(x) ((float)((x) / 1024)) /** * CLUTTER_UNITS_TO_PANGO_UNIT: @@ -102,19 +109,7 @@ typedef float ClutterUnit; * * Since: 0.6 */ -#define CLUTTER_UNITS_TO_PANGO_UNIT(x) ((int)(x * 1024)) - -#define CLUTTER_UNITS_FROM_STAGE_WIDTH_PERCENTAGE(x) \ - ((clutter_actor_get_widthu (clutter_stage_get_default ()) * x) / 100) - -#define CLUTTER_UNITS_FROM_STAGE_HEIGHT_PERCENTAGE(x) \ - ((clutter_actor_get_heightu (clutter_stage_get_default ()) * x) / 100) - -#define CLUTTER_UNITS_FROM_PARENT_WIDTH_PERCENTAGE(a, x) \ - ((clutter_actor_get_widthu (clutter_actor_get_parent (a)) * x) / 100) - -#define CLUTTER_UNITS_FROM_PARENT_HEIGHT_PERCENTAGE(a, x) \ - ((clutter_actor_get_heightu (clutter_actor_get_parent (a)) * x) / 100) +#define CLUTTER_UNITS_TO_PANGO_UNIT(x) ((int)((x) * 1024)) /** * CLUTTER_UNITS_FROM_MM: @@ -124,10 +119,7 @@ typedef float ClutterUnit; * * Since: 0.6 */ -#define CLUTTER_UNITS_FROM_MM(x) \ - (CLUTTER_UNITS_FROM_FLOAT ((((x) * clutter_stage_get_resolution ((ClutterStage *) clutter_stage_get_default ())) / 25.4))) - -#define CLUTTER_UNITS_FROM_MMX(x) CLUTTER_UNITS_FROM_MM +#define CLUTTER_UNITS_FROM_MM(x) (clutter_units_mm (x)) /** * CLUTTER_UNITS_FROM_POINTS: @@ -137,8 +129,10 @@ typedef float ClutterUnit; * * Since: 0.6 */ -#define CLUTTER_UNITS_FROM_POINTS(x) \ - CLUTTER_UNITS_FROM_FLOAT ((((x) * clutter_stage_get_resolution ((ClutterStage *) clutter_stage_get_default ())) / 72.0)) +#define CLUTTER_UNITS_FROM_POINTS(x) (clutter_units_pt (x)) + +ClutterUnit clutter_units_mm (gdouble mm); +ClutterUnit clutter_units_pt (gdouble pt); #define CLUTTER_TYPE_UNIT (clutter_unit_get_type ()) #define CLUTTER_TYPE_PARAM_UNIT (clutter_param_unit_get_type ()) diff --git a/doc/reference/clutter/clutter-sections.txt b/doc/reference/clutter/clutter-sections.txt index f6fb673c5..abef50c16 100644 --- a/doc/reference/clutter/clutter-sections.txt +++ b/doc/reference/clutter/clutter-sections.txt @@ -29,25 +29,20 @@ clutter_media_get_type Unit conversion ClutterUnit CLUTTER_UNITS_FORMAT -CLUTTER_UNITS_FROM_DEVICE -CLUTTER_UNITS_TO_DEVICE -CLUTTER_UNITS_FROM_FIXED -CLUTTER_UNITS_TO_FIXED CLUTTER_UNITS_FROM_FLOAT CLUTTER_UNITS_TO_FLOAT CLUTTER_UNITS_FROM_INT CLUTTER_UNITS_TO_INT +CLUTTER_UNITS_FROM_DEVICE +CLUTTER_UNITS_TO_DEVICE +CLUTTER_UNITS_FROM_FIXED +CLUTTER_UNITS_TO_FIXED CLUTTER_UNITS_FROM_PANGO_UNIT CLUTTER_UNITS_TO_PANGO_UNIT -CLUTTER_UNITS_TMP_FROM_DEVICE -CLUTTER_UNITS_TMP_TO_DEVICE -CLUTTER_UNITS_FROM_STAGE_WIDTH_PERCENTAGE -CLUTTER_UNITS_FROM_STAGE_HEIGHT_PERCENTAGE -CLUTTER_UNITS_FROM_PARENT_WIDTH_PERCENTAGE -CLUTTER_UNITS_FROM_PARENT_HEIGHT_PERCENTAGE CLUTTER_UNITS_FROM_MM -CLUTTER_UNITS_FROM_MMX CLUTTER_UNITS_FROM_POINTS +clutter_units_mm +clutter_units_pt CLUTTER_MAXUNIT