2008-04-17 05:13:12 -04:00
|
|
|
/* -*- mode:C; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
|
|
/*
|
|
|
|
* Clutter.
|
|
|
|
*
|
|
|
|
* An OpenGL based 'interactive canvas' library.
|
|
|
|
*
|
2008-05-09 06:58:26 -04:00
|
|
|
* Authored By: Tomas Frydrych <tf@openedhand.com>
|
|
|
|
* Emmanuele Bassi <ebassi@openedhand.com>
|
2008-04-17 05:13:12 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 OpenedHand
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2010-03-01 07:56:10 -05:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
2008-04-17 05:13:12 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:clutter-units
|
2009-06-03 06:12:09 -04:00
|
|
|
* @short_description: A logical distance unit
|
2008-04-17 05:13:12 -04:00
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* #ClutterUnits is a structure holding a logical distance value along with
|
|
|
|
* its type, expressed as a value of the #ClutterUnitType enumeration. It is
|
|
|
|
* possible to use #ClutterUnits to store a position or a size in units
|
|
|
|
* different than pixels, and convert them whenever needed (for instance
|
|
|
|
* inside the #ClutterActor::allocate() virtual function, or inside the
|
|
|
|
* #ClutterActor::get_preferred_width() and #ClutterActor::get_preferred_height()
|
|
|
|
* virtual functions.
|
2008-04-17 05:13:12 -04:00
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* In order to register a #ClutterUnits property, the #ClutterParamSpecUnits
|
2008-05-09 06:58:26 -04:00
|
|
|
* #GParamSpec sub-class should be used:
|
|
|
|
*
|
|
|
|
* |[
|
|
|
|
* GParamSpec *pspec;
|
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* pspec = clutter_param_spec_units ("active-width",
|
|
|
|
* "Width",
|
|
|
|
* "Width of the active area, in millimeters",
|
|
|
|
* CLUTTER_UNIT_MM,
|
|
|
|
* 0.0, 12.0,
|
|
|
|
* 12.0,
|
|
|
|
* G_PARAM_READWRITE);
|
2008-05-09 06:58:26 -04:00
|
|
|
* g_object_class_install_property (gobject_class, PROP_WIDTH, pspec);
|
|
|
|
* ]|
|
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* A #GValue holding units can be manipulated using clutter_value_set_units()
|
|
|
|
* and clutter_value_get_units(). #GValue<!-- -->s containing a #ClutterUnits
|
|
|
|
* value can also be transformed to #GValue<!-- -->s initialized with
|
|
|
|
* %G_TYPE_INT, %G_TYPE_FLOAT and %G_TYPE_STRING through implicit conversion
|
|
|
|
* and using g_value_transform().
|
2008-05-09 06:58:26 -04:00
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* #ClutterUnits is available since Clutter 1.0
|
2008-04-17 05:13:12 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2010-10-21 07:16:05 -04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2008-04-17 05:13:12 -04:00
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
#include <gobject/gvaluecollector.h>
|
|
|
|
|
2010-10-21 06:49:37 -04:00
|
|
|
#include "clutter-backend-private.h"
|
2009-10-12 11:28:10 -04:00
|
|
|
#include "clutter-interval.h"
|
2010-10-21 06:49:37 -04:00
|
|
|
#include "clutter-private.h"
|
|
|
|
#include "clutter-units.h"
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-01-22 07:11:25 -05:00
|
|
|
#define DPI_FALLBACK (96.0)
|
|
|
|
|
|
|
|
#define FLOAT_EPSILON (1e-30)
|
2009-01-21 12:10:36 -05:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
static gfloat
|
|
|
|
units_mm_to_pixels (gfloat mm)
|
2009-01-21 12:10:36 -05:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-06 12:47:34 -04:00
|
|
|
static gfloat
|
|
|
|
units_cm_to_pixels (gfloat cm)
|
|
|
|
{
|
|
|
|
return units_mm_to_pixels (cm * 10);
|
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
static gfloat
|
|
|
|
units_pt_to_pixels (gfloat pt)
|
2009-01-21 12:10:36 -05:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
static gfloat
|
|
|
|
units_em_to_pixels (const gchar *font_name,
|
|
|
|
gfloat em)
|
2009-05-07 09:16:01 -04:00
|
|
|
{
|
|
|
|
ClutterBackend *backend = clutter_get_default_backend ();
|
|
|
|
|
|
|
|
if (font_name == NULL || *font_name == '\0')
|
|
|
|
return em * _clutter_backend_get_units_per_em (backend, NULL);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PangoFontDescription *font_desc;
|
|
|
|
gfloat res;
|
|
|
|
|
|
|
|
font_desc = pango_font_description_from_string (font_name);
|
|
|
|
if (font_desc == NULL)
|
|
|
|
res = -1.0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = em * _clutter_backend_get_units_per_em (backend, font_desc);
|
|
|
|
|
|
|
|
pango_font_description_free (font_desc);
|
|
|
|
}
|
2009-01-21 12:35:47 -05:00
|
|
|
|
2009-05-07 09:16:01 -04:00
|
|
|
return res;
|
|
|
|
}
|
2009-01-21 12:35:47 -05:00
|
|
|
}
|
|
|
|
|
2009-03-09 13:35:08 -04:00
|
|
|
/**
|
2009-07-28 08:43:52 -04:00
|
|
|
* clutter_units_from_mm:
|
2010-09-08 12:04:21 -04:00
|
|
|
* @units: (out caller-allocates): a #ClutterUnits
|
2009-06-03 06:12:09 -04:00
|
|
|
* @mm: millimeters
|
2009-03-09 13:35:08 -04:00
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* Stores a value in millimiters inside @units
|
2009-03-09 13:35:08 -04:00
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
void
|
2009-07-28 08:43:52 -04:00
|
|
|
clutter_units_from_mm (ClutterUnits *units,
|
|
|
|
gfloat mm)
|
2009-06-03 06:12:09 -04:00
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_if_fail (units != NULL);
|
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
units->unit_type = CLUTTER_UNIT_MM;
|
|
|
|
units->value = mm;
|
|
|
|
units->pixels = units_mm_to_pixels (mm);
|
|
|
|
units->pixels_set = TRUE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-06-03 06:12:09 -04:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:47:34 -04:00
|
|
|
/**
|
|
|
|
* clutter_units_from_cm:
|
2010-09-08 12:04:21 -04:00
|
|
|
* @units: (out caller-allocates): a #ClutterUnits
|
2009-10-06 12:47:34 -04:00
|
|
|
* @cm: centimeters
|
|
|
|
*
|
|
|
|
* Stores a value in centimeters inside @units
|
|
|
|
*
|
|
|
|
* Since: 1.2
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clutter_units_from_cm (ClutterUnits *units,
|
|
|
|
gfloat cm)
|
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
|
|
|
|
2009-10-06 12:47:34 -04:00
|
|
|
g_return_if_fail (units != NULL);
|
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
|
2009-10-06 12:47:34 -04:00
|
|
|
units->unit_type = CLUTTER_UNIT_CM;
|
|
|
|
units->value = cm;
|
|
|
|
units->pixels = units_cm_to_pixels (cm);
|
|
|
|
units->pixels_set = TRUE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-10-06 12:47:34 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
2009-07-28 08:43:52 -04:00
|
|
|
* clutter_units_from_pt:
|
2010-09-08 12:04:21 -04:00
|
|
|
* @units: (out caller-allocates): a #ClutterUnits
|
2009-06-03 06:12:09 -04:00
|
|
|
* @pt: typographic points
|
|
|
|
*
|
|
|
|
* Stores a value in typographic points inside @units
|
2009-03-09 13:35:08 -04:00
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
2009-06-03 06:12:09 -04:00
|
|
|
void
|
2009-07-28 08:43:52 -04:00
|
|
|
clutter_units_from_pt (ClutterUnits *units,
|
|
|
|
gfloat pt)
|
2009-03-09 13:35:08 -04:00
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_if_fail (units != NULL);
|
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
units->unit_type = CLUTTER_UNIT_POINT;
|
|
|
|
units->value = pt;
|
|
|
|
units->pixels = units_pt_to_pixels (pt);
|
|
|
|
units->pixels_set = TRUE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-03-09 13:35:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-28 08:43:52 -04:00
|
|
|
* clutter_units_from_em:
|
2010-09-08 12:04:21 -04:00
|
|
|
* @units: (out caller-allocates): a #ClutterUnits
|
2009-06-03 06:12:09 -04:00
|
|
|
* @em: em
|
2009-03-09 13:35:08 -04:00
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* Stores a value in em inside @units, using the default font
|
|
|
|
* name as returned by clutter_backend_get_font_name()
|
2009-03-09 13:35:08 -04:00
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
void
|
2009-07-28 08:43:52 -04:00
|
|
|
clutter_units_from_em (ClutterUnits *units,
|
|
|
|
gfloat em)
|
2009-06-03 06:12:09 -04:00
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_if_fail (units != NULL);
|
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
units->unit_type = CLUTTER_UNIT_EM;
|
|
|
|
units->value = em;
|
|
|
|
units->pixels = units_em_to_pixels (NULL, em);
|
|
|
|
units->pixels_set = TRUE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-06-03 06:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-28 08:43:52 -04:00
|
|
|
* clutter_units_from_em_for_font:
|
2010-09-08 12:04:21 -04:00
|
|
|
* @units: (out caller-allocates): a #ClutterUnits
|
|
|
|
* @font_name: (allow-none): the font name and size
|
2009-06-03 06:12:09 -04:00
|
|
|
* @em: em
|
|
|
|
*
|
|
|
|
* Stores a value in em inside @units using @font_name
|
2009-03-09 13:35:08 -04:00
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
2009-06-03 06:12:09 -04:00
|
|
|
void
|
2009-07-28 08:43:52 -04:00
|
|
|
clutter_units_from_em_for_font (ClutterUnits *units,
|
|
|
|
const gchar *font_name,
|
|
|
|
gfloat em)
|
2009-03-09 13:35:08 -04:00
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_if_fail (units != NULL);
|
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
units->unit_type = CLUTTER_UNIT_EM;
|
|
|
|
units->value = em;
|
|
|
|
units->pixels = units_em_to_pixels (font_name, em);
|
|
|
|
units->pixels_set = TRUE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-03-09 13:35:08 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
2009-07-28 08:43:52 -04:00
|
|
|
* clutter_units_from_pixels:
|
2010-09-08 12:04:21 -04:00
|
|
|
* @units: (out caller-allocates): a #ClutterUnits
|
2009-06-03 06:12:09 -04:00
|
|
|
* @px: pixels
|
|
|
|
*
|
|
|
|
* Stores a value in pixels inside @units
|
|
|
|
*
|
|
|
|
* Since: 1.0
|
2009-03-09 13:35:08 -04:00
|
|
|
*/
|
2009-06-03 06:12:09 -04:00
|
|
|
void
|
2009-07-28 08:43:52 -04:00
|
|
|
clutter_units_from_pixels (ClutterUnits *units,
|
|
|
|
gint px)
|
2009-06-03 06:12:09 -04:00
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_if_fail (units != NULL);
|
2009-03-09 13:35:08 -04:00
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
units->unit_type = CLUTTER_UNIT_PIXEL;
|
|
|
|
units->value = px;
|
|
|
|
units->pixels = px;
|
|
|
|
units->pixels_set = TRUE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-06-03 06:12:09 -04:00
|
|
|
}
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
|
|
|
* clutter_units_get_unit_type:
|
|
|
|
* @units: a #ClutterUnits
|
|
|
|
*
|
|
|
|
* Retrieves the unit type of the value stored inside @units
|
|
|
|
*
|
|
|
|
* Return value: a unit type
|
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
ClutterUnitType
|
|
|
|
clutter_units_get_unit_type (const ClutterUnits *units)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_val_if_fail (units != NULL, CLUTTER_UNIT_PIXEL);
|
|
|
|
|
|
|
|
return units->unit_type;
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
|
|
|
* clutter_units_get_unit_value:
|
|
|
|
* @units: a #ClutterUnits
|
|
|
|
*
|
|
|
|
* Retrieves the value stored inside @units
|
|
|
|
*
|
|
|
|
* Return value: the value stored inside a #ClutterUnits
|
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
gfloat
|
|
|
|
clutter_units_get_unit_value (const ClutterUnits *units)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_val_if_fail (units != NULL, 0.0);
|
|
|
|
|
|
|
|
return units->value;
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
|
|
|
* clutter_units_copy:
|
|
|
|
* @units: the #ClutterUnits to copy
|
|
|
|
*
|
|
|
|
* Copies @units
|
|
|
|
*
|
2010-09-08 12:04:21 -04:00
|
|
|
* Return value: (transfer full): the newly created copy of a
|
|
|
|
* #ClutterUnits structure. Use clutter_units_free() to free
|
|
|
|
* the allocated resources
|
2009-06-03 06:12:09 -04:00
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
ClutterUnits *
|
|
|
|
clutter_units_copy (const ClutterUnits *units)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
if (units != NULL)
|
|
|
|
return g_slice_dup (ClutterUnits, units);
|
2008-04-17 05:13:12 -04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
|
|
|
* clutter_units_free:
|
|
|
|
* @units: the #ClutterUnits to free
|
|
|
|
*
|
|
|
|
* Frees the resources allocated by @units
|
|
|
|
*
|
|
|
|
* You should only call this function on a #ClutterUnits
|
|
|
|
* created using clutter_units_copy()
|
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clutter_units_free (ClutterUnits *units)
|
|
|
|
{
|
|
|
|
if (units != NULL)
|
|
|
|
g_slice_free (ClutterUnits, units);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clutter_units_to_pixels:
|
|
|
|
* @units: units to convert
|
|
|
|
*
|
2009-06-09 09:47:36 -04:00
|
|
|
* Converts a value in #ClutterUnits to pixels
|
2009-06-03 06:12:09 -04:00
|
|
|
*
|
|
|
|
* Return value: the value in pixels
|
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
gfloat
|
|
|
|
clutter_units_to_pixels (ClutterUnits *units)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_val_if_fail (units != NULL, 0.0);
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
/* if the backend settings changed we evict the cached value */
|
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
if (units->serial != _clutter_backend_get_units_serial (backend))
|
|
|
|
units->pixels_set = FALSE;
|
|
|
|
|
|
|
|
if (units->pixels_set)
|
|
|
|
return units->pixels;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
switch (units->unit_type)
|
|
|
|
{
|
|
|
|
case CLUTTER_UNIT_MM:
|
|
|
|
units->pixels = units_mm_to_pixels (units->value);
|
|
|
|
break;
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-10-06 12:47:34 -04:00
|
|
|
case CLUTTER_UNIT_CM:
|
|
|
|
units->pixels = units_cm_to_pixels (units->value);
|
|
|
|
break;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
case CLUTTER_UNIT_POINT:
|
|
|
|
units->pixels = units_pt_to_pixels (units->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLUTTER_UNIT_EM:
|
|
|
|
units->pixels = units_em_to_pixels (NULL, units->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLUTTER_UNIT_PIXEL:
|
|
|
|
units->pixels = units->value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
units->pixels_set = TRUE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-06-03 06:12:09 -04:00
|
|
|
|
|
|
|
return units->pixels;
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
|
|
|
* clutter_units_from_string:
|
2010-09-08 12:04:21 -04:00
|
|
|
* @units: (out caller-allocates): a #ClutterUnits
|
2009-06-03 06:12:09 -04:00
|
|
|
* @str: the string to convert
|
|
|
|
*
|
|
|
|
* Parses a value and updates @units with it
|
|
|
|
*
|
|
|
|
* A #ClutterUnits expressed in string should match:
|
|
|
|
*
|
|
|
|
* |[
|
2009-10-06 07:02:15 -04:00
|
|
|
* units: wsp* unit-value wsp* unit-name? wsp*
|
|
|
|
* unit-value: number
|
2009-10-06 12:47:34 -04:00
|
|
|
* unit-name: 'px' | 'pt' | 'mm' | 'em' | 'cm'
|
2009-10-06 07:02:15 -04:00
|
|
|
* number: digit+
|
|
|
|
* | digit* sep digit+
|
|
|
|
* sep: '.' | ','
|
|
|
|
* digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
|
|
|
|
* wsp: (#0x20 | #0x9 | #0xA | #0xB | #0xC | #0xD)+
|
2009-06-03 06:12:09 -04:00
|
|
|
* ]|
|
|
|
|
*
|
|
|
|
* For instance, these are valid strings:
|
|
|
|
*
|
|
|
|
* |[
|
|
|
|
* 10 px
|
2009-07-20 15:59:18 -04:00
|
|
|
* 5.1 em
|
2009-06-03 06:12:09 -04:00
|
|
|
* 24 pt
|
|
|
|
* 12.6 mm
|
2009-10-06 07:02:15 -04:00
|
|
|
* .3 cm
|
2009-06-03 06:12:09 -04:00
|
|
|
* ]|
|
|
|
|
*
|
2009-07-20 15:59:18 -04:00
|
|
|
* While these are not:
|
|
|
|
*
|
|
|
|
* |[
|
|
|
|
* 42 cats
|
|
|
|
* omg!1!ponies
|
|
|
|
* ]|
|
|
|
|
*
|
2010-06-30 09:58:47 -04:00
|
|
|
* <note><para>If no unit is specified, pixels are assumed.</para></note>
|
2009-07-20 15:59:18 -04:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if the string was successfully parsed,
|
|
|
|
* and %FALSE otherwise
|
2009-06-03 06:12:09 -04:00
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
clutter_units_from_string (ClutterUnits *units,
|
|
|
|
const gchar *str)
|
|
|
|
{
|
2009-10-16 10:25:37 -04:00
|
|
|
ClutterBackend *backend;
|
2009-06-03 06:12:09 -04:00
|
|
|
ClutterUnitType unit_type;
|
|
|
|
gfloat value;
|
|
|
|
|
|
|
|
g_return_val_if_fail (units != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (str != NULL, FALSE);
|
|
|
|
|
2009-10-06 07:02:15 -04:00
|
|
|
/* strip leading space */
|
2009-06-03 06:12:09 -04:00
|
|
|
while (g_ascii_isspace (*str))
|
|
|
|
str++;
|
|
|
|
|
|
|
|
if (*str == '\0')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* integer part */
|
|
|
|
value = (gfloat) strtoul (str, (char **) &str, 10);
|
|
|
|
|
|
|
|
if (*str == '.' || *str == ',')
|
|
|
|
{
|
2009-10-06 07:02:15 -04:00
|
|
|
gfloat divisor = 0.1;
|
|
|
|
|
|
|
|
/* 5.cm is not a valid number */
|
|
|
|
if (!g_ascii_isdigit (*++str))
|
|
|
|
return FALSE;
|
2009-06-03 06:12:09 -04:00
|
|
|
|
2009-10-06 07:02:15 -04:00
|
|
|
while (g_ascii_isdigit (*str))
|
2009-06-03 06:12:09 -04:00
|
|
|
{
|
2009-10-06 07:02:15 -04:00
|
|
|
value += (*str - '0') * divisor;
|
|
|
|
divisor *= 0.1;
|
|
|
|
str++;
|
2009-06-03 06:12:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-06 07:02:15 -04:00
|
|
|
while (g_ascii_isspace (*str))
|
|
|
|
str++;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/* assume pixels by default, if no unit is specified */
|
2009-10-03 09:08:33 -04:00
|
|
|
if (*str == '\0')
|
2009-06-03 06:12:09 -04:00
|
|
|
unit_type = CLUTTER_UNIT_PIXEL;
|
2009-10-06 07:02:15 -04:00
|
|
|
else if (strncmp (str, "em", 2) == 0)
|
2009-06-03 06:12:09 -04:00
|
|
|
{
|
2009-10-06 07:02:15 -04:00
|
|
|
unit_type = CLUTTER_UNIT_EM;
|
|
|
|
str += 2;
|
2009-06-03 06:12:09 -04:00
|
|
|
}
|
2009-10-06 07:02:15 -04:00
|
|
|
else if (strncmp (str, "mm", 2) == 0)
|
|
|
|
{
|
|
|
|
unit_type = CLUTTER_UNIT_MM;
|
|
|
|
str += 2;
|
|
|
|
}
|
2009-10-06 12:47:34 -04:00
|
|
|
else if (strncmp (str, "cm", 2) == 0)
|
|
|
|
{
|
|
|
|
unit_type = CLUTTER_UNIT_CM;
|
|
|
|
str += 2;
|
|
|
|
}
|
2009-10-06 07:02:15 -04:00
|
|
|
else if (strncmp (str, "pt", 2) == 0)
|
|
|
|
{
|
|
|
|
unit_type = CLUTTER_UNIT_POINT;
|
|
|
|
str += 2;
|
|
|
|
}
|
|
|
|
else if (strncmp (str, "px", 2) == 0)
|
|
|
|
{
|
|
|
|
unit_type = CLUTTER_UNIT_PIXEL;
|
|
|
|
str += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* ensure the unit is only followed by white space */
|
|
|
|
while (g_ascii_isspace (*str))
|
|
|
|
str++;
|
|
|
|
if (*str != '\0')
|
|
|
|
return FALSE;
|
2009-06-03 06:12:09 -04:00
|
|
|
|
2009-10-16 10:25:37 -04:00
|
|
|
backend = clutter_get_default_backend ();
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
units->unit_type = unit_type;
|
|
|
|
units->value = value;
|
|
|
|
units->pixels_set = FALSE;
|
2009-10-16 10:25:37 -04:00
|
|
|
units->serial = _clutter_backend_get_units_serial (backend);
|
2009-06-03 06:12:09 -04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-06-04 07:15:15 -04:00
|
|
|
static const gchar *
|
|
|
|
clutter_unit_type_name (ClutterUnitType unit_type)
|
|
|
|
{
|
|
|
|
switch (unit_type)
|
|
|
|
{
|
|
|
|
case CLUTTER_UNIT_MM:
|
|
|
|
return "mm";
|
|
|
|
|
2009-10-06 12:47:34 -04:00
|
|
|
case CLUTTER_UNIT_CM:
|
|
|
|
return "cm";
|
|
|
|
|
2009-06-04 07:15:15 -04:00
|
|
|
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>";
|
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/**
|
|
|
|
* clutter_units_to_string:
|
|
|
|
* @units: a #ClutterUnits
|
|
|
|
*
|
|
|
|
* Converts @units into a string
|
|
|
|
*
|
|
|
|
* See clutter_units_from_string() for the units syntax and for
|
2009-07-20 15:59:18 -04:00
|
|
|
* examples of output
|
|
|
|
*
|
|
|
|
* <note>Fractional values are truncated to the second decimal
|
2009-10-06 12:47:34 -04:00
|
|
|
* position for em, mm and cm, and to the first decimal position for
|
2009-07-20 15:59:18 -04:00
|
|
|
* typographic points. Pixels are integers.</note>
|
2009-06-03 06:12:09 -04:00
|
|
|
*
|
|
|
|
* Return value: a newly allocated string containing the encoded
|
|
|
|
* #ClutterUnits value. Use g_free() to free the string
|
|
|
|
*
|
|
|
|
* Since: 1.0
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
clutter_units_to_string (const ClutterUnits *units)
|
2008-04-17 10:08:26 -04:00
|
|
|
{
|
2009-06-06 09:37:41 -04:00
|
|
|
const gchar *unit_name = NULL;
|
[units] Do not be locale-dependant on string conversion
When dumping a ClutterUnits structure to a string we are using a bare
g_strdup_printf(), which unfortunately is locale dependant. So, for
instance, a type of CLUTTER_UNIT_EM and a value of 42 are stringified
as:
C: 42.00 em
en_GB 42.00 em
it_IT 42,00 em
fr_FR 42,00 em
This would not be a problem -- clutter_units_from_string() allows both
'.' and ',' as fractionary part delimiters. The test suite, on the
other hand, does not know that, and it checks for exact matches with
the C locale.
Calling setlocale(LC_ALL,"C") at the beginning of the conformance test
suite is not a good idea, because it would prevent external testing; and
it's a lame cop out from doing exactly what we have to do -- pick a format
and stick with it.
Like other platforms, languages and frameworks before us, we opt to
be less liberal in what we create; so, we choose to always stringify
ClutterUnits with fractionary parts using '.' as the delimiter.
Fixes bug:
http://bugzilla.openedhand.com/show_bug.cgi?id=1763
2009-08-14 10:59:29 -04:00
|
|
|
const gchar *fmt = NULL;
|
|
|
|
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
|
2009-06-03 06:12:09 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail (units != NULL, NULL);
|
|
|
|
|
|
|
|
switch (units->unit_type)
|
|
|
|
{
|
[units] Do not be locale-dependant on string conversion
When dumping a ClutterUnits structure to a string we are using a bare
g_strdup_printf(), which unfortunately is locale dependant. So, for
instance, a type of CLUTTER_UNIT_EM and a value of 42 are stringified
as:
C: 42.00 em
en_GB 42.00 em
it_IT 42,00 em
fr_FR 42,00 em
This would not be a problem -- clutter_units_from_string() allows both
'.' and ',' as fractionary part delimiters. The test suite, on the
other hand, does not know that, and it checks for exact matches with
the C locale.
Calling setlocale(LC_ALL,"C") at the beginning of the conformance test
suite is not a good idea, because it would prevent external testing; and
it's a lame cop out from doing exactly what we have to do -- pick a format
and stick with it.
Like other platforms, languages and frameworks before us, we opt to
be less liberal in what we create; so, we choose to always stringify
ClutterUnits with fractionary parts using '.' as the delimiter.
Fixes bug:
http://bugzilla.openedhand.com/show_bug.cgi?id=1763
2009-08-14 10:59:29 -04:00
|
|
|
/* special case: there is no such thing as "half a pixel", so
|
|
|
|
* we round up to the nearest integer using C default
|
|
|
|
*/
|
|
|
|
case CLUTTER_UNIT_PIXEL:
|
|
|
|
return g_strdup_printf ("%d px", (int) units->value);
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
case CLUTTER_UNIT_MM:
|
|
|
|
unit_name = "mm";
|
[units] Do not be locale-dependant on string conversion
When dumping a ClutterUnits structure to a string we are using a bare
g_strdup_printf(), which unfortunately is locale dependant. So, for
instance, a type of CLUTTER_UNIT_EM and a value of 42 are stringified
as:
C: 42.00 em
en_GB 42.00 em
it_IT 42,00 em
fr_FR 42,00 em
This would not be a problem -- clutter_units_from_string() allows both
'.' and ',' as fractionary part delimiters. The test suite, on the
other hand, does not know that, and it checks for exact matches with
the C locale.
Calling setlocale(LC_ALL,"C") at the beginning of the conformance test
suite is not a good idea, because it would prevent external testing; and
it's a lame cop out from doing exactly what we have to do -- pick a format
and stick with it.
Like other platforms, languages and frameworks before us, we opt to
be less liberal in what we create; so, we choose to always stringify
ClutterUnits with fractionary parts using '.' as the delimiter.
Fixes bug:
http://bugzilla.openedhand.com/show_bug.cgi?id=1763
2009-08-14 10:59:29 -04:00
|
|
|
fmt = "%.2f";
|
2009-06-03 06:12:09 -04:00
|
|
|
break;
|
|
|
|
|
2009-10-06 12:47:34 -04:00
|
|
|
case CLUTTER_UNIT_CM:
|
|
|
|
unit_name = "cm";
|
|
|
|
fmt = "%.2f";
|
|
|
|
break;
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
case CLUTTER_UNIT_POINT:
|
|
|
|
unit_name = "pt";
|
[units] Do not be locale-dependant on string conversion
When dumping a ClutterUnits structure to a string we are using a bare
g_strdup_printf(), which unfortunately is locale dependant. So, for
instance, a type of CLUTTER_UNIT_EM and a value of 42 are stringified
as:
C: 42.00 em
en_GB 42.00 em
it_IT 42,00 em
fr_FR 42,00 em
This would not be a problem -- clutter_units_from_string() allows both
'.' and ',' as fractionary part delimiters. The test suite, on the
other hand, does not know that, and it checks for exact matches with
the C locale.
Calling setlocale(LC_ALL,"C") at the beginning of the conformance test
suite is not a good idea, because it would prevent external testing; and
it's a lame cop out from doing exactly what we have to do -- pick a format
and stick with it.
Like other platforms, languages and frameworks before us, we opt to
be less liberal in what we create; so, we choose to always stringify
ClutterUnits with fractionary parts using '.' as the delimiter.
Fixes bug:
http://bugzilla.openedhand.com/show_bug.cgi?id=1763
2009-08-14 10:59:29 -04:00
|
|
|
fmt = "%.1f";
|
2009-06-03 06:12:09 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CLUTTER_UNIT_EM:
|
|
|
|
unit_name = "em";
|
[units] Do not be locale-dependant on string conversion
When dumping a ClutterUnits structure to a string we are using a bare
g_strdup_printf(), which unfortunately is locale dependant. So, for
instance, a type of CLUTTER_UNIT_EM and a value of 42 are stringified
as:
C: 42.00 em
en_GB 42.00 em
it_IT 42,00 em
fr_FR 42,00 em
This would not be a problem -- clutter_units_from_string() allows both
'.' and ',' as fractionary part delimiters. The test suite, on the
other hand, does not know that, and it checks for exact matches with
the C locale.
Calling setlocale(LC_ALL,"C") at the beginning of the conformance test
suite is not a good idea, because it would prevent external testing; and
it's a lame cop out from doing exactly what we have to do -- pick a format
and stick with it.
Like other platforms, languages and frameworks before us, we opt to
be less liberal in what we create; so, we choose to always stringify
ClutterUnits with fractionary parts using '.' as the delimiter.
Fixes bug:
http://bugzilla.openedhand.com/show_bug.cgi?id=1763
2009-08-14 10:59:29 -04:00
|
|
|
fmt = "%.2f";
|
2009-06-03 06:12:09 -04:00
|
|
|
break;
|
2009-06-06 09:37:41 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2009-06-03 06:12:09 -04:00
|
|
|
}
|
|
|
|
|
[units] Do not be locale-dependant on string conversion
When dumping a ClutterUnits structure to a string we are using a bare
g_strdup_printf(), which unfortunately is locale dependant. So, for
instance, a type of CLUTTER_UNIT_EM and a value of 42 are stringified
as:
C: 42.00 em
en_GB 42.00 em
it_IT 42,00 em
fr_FR 42,00 em
This would not be a problem -- clutter_units_from_string() allows both
'.' and ',' as fractionary part delimiters. The test suite, on the
other hand, does not know that, and it checks for exact matches with
the C locale.
Calling setlocale(LC_ALL,"C") at the beginning of the conformance test
suite is not a good idea, because it would prevent external testing; and
it's a lame cop out from doing exactly what we have to do -- pick a format
and stick with it.
Like other platforms, languages and frameworks before us, we opt to
be less liberal in what we create; so, we choose to always stringify
ClutterUnits with fractionary parts using '.' as the delimiter.
Fixes bug:
http://bugzilla.openedhand.com/show_bug.cgi?id=1763
2009-08-14 10:59:29 -04:00
|
|
|
g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, units->value);
|
|
|
|
|
|
|
|
return g_strconcat (buf, " ", unit_name, NULL);
|
2008-05-09 06:58:26 -04:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:28:10 -04:00
|
|
|
/*
|
|
|
|
* ClutterInterval integration
|
|
|
|
*/
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
clutter_units_progress (const GValue *a,
|
|
|
|
const GValue *b,
|
|
|
|
gdouble progress,
|
|
|
|
GValue *retval)
|
|
|
|
{
|
|
|
|
ClutterUnits *a_units = (ClutterUnits *) clutter_value_get_units (a);
|
|
|
|
ClutterUnits *b_units = (ClutterUnits *) clutter_value_get_units (b);
|
|
|
|
ClutterUnits res;
|
|
|
|
gfloat a_px, b_px, value;
|
|
|
|
|
|
|
|
a_px = clutter_units_to_pixels (a_units);
|
|
|
|
b_px = clutter_units_to_pixels (b_units);
|
|
|
|
value = progress * (b_px - a_px) + a_px;
|
|
|
|
|
|
|
|
clutter_units_from_pixels (&res, value);
|
|
|
|
clutter_value_set_units (retval, &res);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/*
|
|
|
|
* GValue and GParamSpec integration
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* units to integer */
|
2008-05-09 06:58:26 -04:00
|
|
|
static void
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_value_transform_units_int (const GValue *src,
|
|
|
|
GValue *dest)
|
2008-05-09 06:58:26 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
dest->data[0].v_int = clutter_units_to_pixels (src->data[0].v_pointer);
|
2008-04-17 10:08:26 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/* integer to units */
|
2009-03-09 13:35:08 -04:00
|
|
|
static void
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_value_transform_int_units (const GValue *src,
|
|
|
|
GValue *dest)
|
2009-03-09 13:35:08 -04:00
|
|
|
{
|
2009-07-28 08:43:52 -04:00
|
|
|
clutter_units_from_pixels (dest->data[0].v_pointer, src->data[0].v_int);
|
2009-03-09 13:35:08 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/* units to float */
|
2009-03-09 13:35:08 -04:00
|
|
|
static void
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_value_transform_units_float (const GValue *src,
|
|
|
|
GValue *dest)
|
2009-03-09 13:35:08 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
dest->data[0].v_float = clutter_units_to_pixels (src->data[0].v_pointer);
|
2009-03-09 13:35:08 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/* float to units */
|
2009-03-09 13:35:08 -04:00
|
|
|
static void
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_value_transform_float_units (const GValue *src,
|
|
|
|
GValue *dest)
|
2009-03-09 13:35:08 -04:00
|
|
|
{
|
2009-07-28 08:43:52 -04:00
|
|
|
clutter_units_from_pixels (dest->data[0].v_pointer, src->data[0].v_float);
|
2009-03-09 13:35:08 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/* units to string */
|
2009-03-09 13:35:08 -04:00
|
|
|
static void
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_value_transform_units_string (const GValue *src,
|
|
|
|
GValue *dest)
|
2009-03-09 13:35:08 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
gchar *string = clutter_units_to_string (src->data[0].v_pointer);
|
|
|
|
|
|
|
|
g_value_take_string (dest, string);
|
2009-03-09 13:35:08 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
/* string to units */
|
|
|
|
static void
|
|
|
|
clutter_value_transform_string_units (const GValue *src,
|
|
|
|
GValue *dest)
|
|
|
|
{
|
|
|
|
ClutterUnits units = { CLUTTER_UNIT_PIXEL, 0.0f };
|
|
|
|
|
|
|
|
clutter_units_from_string (&units, g_value_get_string (src));
|
|
|
|
|
|
|
|
clutter_value_set_units (dest, &units);
|
|
|
|
}
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2010-10-08 10:21:57 -04:00
|
|
|
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterUnits, clutter_units,
|
|
|
|
clutter_units_copy,
|
|
|
|
clutter_units_free,
|
|
|
|
CLUTTER_REGISTER_VALUE_TRANSFORM_TO (G_TYPE_INT, clutter_value_transform_units_int)
|
|
|
|
CLUTTER_REGISTER_VALUE_TRANSFORM_TO (G_TYPE_FLOAT, clutter_value_transform_units_float)
|
|
|
|
CLUTTER_REGISTER_VALUE_TRANSFORM_TO (G_TYPE_STRING, clutter_value_transform_units_string)
|
|
|
|
CLUTTER_REGISTER_VALUE_TRANSFORM_FROM (G_TYPE_INT, clutter_value_transform_int_units)
|
|
|
|
CLUTTER_REGISTER_VALUE_TRANSFORM_FROM (G_TYPE_FLOAT, clutter_value_transform_float_units)
|
|
|
|
CLUTTER_REGISTER_VALUE_TRANSFORM_FROM (G_TYPE_STRING, clutter_value_transform_string_units)
|
|
|
|
CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_units_progress));
|
2008-04-17 05:13:12 -04:00
|
|
|
|
|
|
|
/**
|
2009-06-03 06:12:09 -04:00
|
|
|
* clutter_value_set_units:
|
2010-12-09 08:59:08 -05:00
|
|
|
* @value: a #GValue initialized to %CLUTTER_TYPE_UNITS
|
2008-04-17 05:13:12 -04:00
|
|
|
* @units: the units to set
|
|
|
|
*
|
|
|
|
* Sets @value to @units
|
|
|
|
*
|
|
|
|
* Since: 0.8
|
|
|
|
*/
|
|
|
|
void
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_value_set_units (GValue *value,
|
|
|
|
const ClutterUnits *units)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_if_fail (CLUTTER_VALUE_HOLDS_UNITS (value));
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
value->data[0].v_pointer = clutter_units_copy (units);
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-03 06:12:09 -04:00
|
|
|
* clutter_value_get_units:
|
2010-12-09 08:59:08 -05:00
|
|
|
* @value: a #GValue initialized to %CLUTTER_TYPE_UNITS
|
2008-04-17 05:13:12 -04:00
|
|
|
*
|
2010-11-18 09:14:37 -05:00
|
|
|
* Gets the #ClutterUnits contained in @value.
|
2008-04-17 05:13:12 -04:00
|
|
|
*
|
|
|
|
* Return value: the units inside the passed #GValue
|
|
|
|
*
|
|
|
|
* Since: 0.8
|
|
|
|
*/
|
Eliminate G_CONST_RETURN
The G_CONST_RETURN define in GLib is, and has always been, a bit fuzzy.
We always used it to conform to the platform, at least for public-facing
API.
At first I assumed it has something to do with brain-damaged compilers
or with weird platforms where const was not really supported; sadly,
it's something much, much worse: it's a define that can be toggled at
compile-time to remove const from the signature of public API. This is a
truly terrifying feature that I assume was added in the past century,
and whose inception clearly had something to do with massive doses of
absynthe and opium — because any other explanation would make the
existence of such a feature even worse than assuming drugs had anything
to do with it.
Anyway, and pleasing the gods, this dubious feature is being
removed/deprecated in GLib; see bug:
https://bugzilla.gnome.org/show_bug.cgi?id=644611
Before deprecation, though, we should just remove its usage from the
whole API. We should especially remove its usage from Cally's internals,
since there it never made sense in the first place.
2011-06-07 10:49:20 -04:00
|
|
|
const ClutterUnits *
|
2009-10-03 07:15:13 -04:00
|
|
|
clutter_value_get_units (const GValue *value)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
g_return_val_if_fail (CLUTTER_VALUE_HOLDS_UNITS (value), NULL);
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
return value->data[0].v_pointer;
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-06-03 06:12:09 -04:00
|
|
|
param_units_init (GParamSpec *pspec)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
ClutterParamSpecUnits *uspec = CLUTTER_PARAM_SPEC_UNITS (pspec);
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
uspec->minimum = -G_MAXFLOAT;
|
|
|
|
uspec->maximum = G_MAXFLOAT;
|
|
|
|
uspec->default_value = 0.0f;
|
|
|
|
uspec->default_type = CLUTTER_UNIT_PIXEL;
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-06-03 06:12:09 -04:00
|
|
|
param_units_set_default (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
ClutterParamSpecUnits *uspec = CLUTTER_PARAM_SPEC_UNITS (pspec);
|
|
|
|
ClutterUnits units;
|
|
|
|
|
|
|
|
units.unit_type = uspec->default_type;
|
|
|
|
units.value = uspec->default_value;
|
|
|
|
units.pixels_set = FALSE;
|
|
|
|
|
|
|
|
clutter_value_set_units (value, &units);
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2009-06-03 06:12:09 -04:00
|
|
|
param_units_validate (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
ClutterParamSpecUnits *uspec = CLUTTER_PARAM_SPEC_UNITS (pspec);
|
|
|
|
ClutterUnits *units = value->data[0].v_pointer;
|
2009-06-04 07:15:15 -04:00
|
|
|
ClutterUnitType otype = units->unit_type;
|
2009-06-03 06:12:09 -04:00
|
|
|
gfloat oval = units->value;
|
2008-05-09 06:58:26 -04:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
g_assert (CLUTTER_IS_PARAM_SPEC_UNITS (pspec));
|
2008-05-09 06:58:26 -04:00
|
|
|
|
2009-06-04 07:15:15 -04:00
|
|
|
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,
|
2009-10-12 12:05:05 -04:00
|
|
|
clutter_unit_type_name (uspec->default_type));
|
2009-06-04 07:15:15 -04:00
|
|
|
|
|
|
|
g_free (str);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
units->value = CLAMP (units->value,
|
|
|
|
uspec->minimum,
|
|
|
|
uspec->maximum);
|
2008-04-17 05:13:12 -04:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
return units->value != oval;
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2009-06-03 06:12:09 -04:00
|
|
|
param_units_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
ClutterUnits *units1 = value1->data[0].v_pointer;
|
|
|
|
ClutterUnits *units2 = value2->data[0].v_pointer;
|
|
|
|
gfloat v1, v2;
|
|
|
|
|
|
|
|
if (units1->unit_type == units2->unit_type)
|
|
|
|
{
|
|
|
|
v1 = units1->value;
|
|
|
|
v2 = units2->value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v1 = clutter_units_to_pixels (units1);
|
|
|
|
v2 = clutter_units_to_pixels (units2);
|
|
|
|
}
|
2009-01-22 07:11:25 -05:00
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
if (v1 < v2)
|
|
|
|
return - (v2 - v1 > FLOAT_EPSILON);
|
2008-04-17 05:13:12 -04:00
|
|
|
else
|
2009-06-03 06:12:09 -04:00
|
|
|
return v1 - v2 > FLOAT_EPSILON;
|
2008-04-17 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_param_units_get_type (void)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
|
|
|
static GType pspec_type = 0;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (pspec_type == 0))
|
|
|
|
{
|
|
|
|
const GParamSpecTypeInfo pspec_info = {
|
2009-06-03 06:12:09 -04:00
|
|
|
sizeof (ClutterParamSpecUnits),
|
2008-04-17 05:13:12 -04:00
|
|
|
16,
|
2009-06-03 06:12:09 -04:00
|
|
|
param_units_init,
|
|
|
|
CLUTTER_TYPE_UNITS,
|
2008-04-17 05:13:12 -04:00
|
|
|
NULL,
|
2009-06-03 06:12:09 -04:00
|
|
|
param_units_set_default,
|
|
|
|
param_units_validate,
|
|
|
|
param_units_values_cmp,
|
2008-04-17 05:13:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
pspec_type = g_param_type_register_static (I_("ClutterParamSpecUnit"),
|
|
|
|
&pspec_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pspec_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-09-08 12:04:21 -04:00
|
|
|
* clutter_param_spec_units: (skip)
|
2008-04-17 05:13:12 -04:00
|
|
|
* @name: name of the property
|
|
|
|
* @nick: short name
|
|
|
|
* @blurb: description (can be translatable)
|
2009-06-03 06:12:09 -04:00
|
|
|
* @default_type: the default type for the #ClutterUnits
|
2008-04-17 05:13:12 -04:00
|
|
|
* @minimum: lower boundary
|
|
|
|
* @maximum: higher boundary
|
|
|
|
* @default_value: default value
|
|
|
|
* @flags: flags for the param spec
|
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* Creates a #GParamSpec for properties using #ClutterUnits.
|
2008-04-17 05:13:12 -04:00
|
|
|
*
|
|
|
|
* Return value: the newly created #GParamSpec
|
|
|
|
*
|
2009-06-03 06:12:09 -04:00
|
|
|
* Since: 1.0
|
2008-04-17 05:13:12 -04:00
|
|
|
*/
|
|
|
|
GParamSpec *
|
2009-06-03 06:12:09 -04:00
|
|
|
clutter_param_spec_units (const gchar *name,
|
|
|
|
const gchar *nick,
|
|
|
|
const gchar *blurb,
|
|
|
|
ClutterUnitType default_type,
|
|
|
|
gfloat minimum,
|
|
|
|
gfloat maximum,
|
|
|
|
gfloat default_value,
|
|
|
|
GParamFlags flags)
|
2008-04-17 05:13:12 -04:00
|
|
|
{
|
2009-06-03 06:12:09 -04:00
|
|
|
ClutterParamSpecUnits *uspec;
|
2008-04-17 05:13:12 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail (default_value >= minimum && default_value <= maximum,
|
|
|
|
NULL);
|
|
|
|
|
2009-06-03 06:12:09 -04:00
|
|
|
uspec = g_param_spec_internal (CLUTTER_TYPE_PARAM_UNITS,
|
2008-04-17 05:13:12 -04:00
|
|
|
name, nick, blurb,
|
|
|
|
flags);
|
2009-06-03 06:12:09 -04:00
|
|
|
|
|
|
|
uspec->default_type = default_type;
|
2008-04-17 05:13:12 -04:00
|
|
|
uspec->minimum = minimum;
|
|
|
|
uspec->maximum = maximum;
|
|
|
|
uspec->default_value = default_value;
|
|
|
|
|
|
|
|
return G_PARAM_SPEC (uspec);
|
|
|
|
}
|