2000-05-31 21:24:14 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpunit.c
|
2003-11-16 06:53:28 +08:00
|
|
|
* Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2000-05-31 21:24:14 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
|
|
|
* 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
|
2003-11-16 06:53:28 +08:00
|
|
|
* Lesser General Public License for more details.
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
2000-05-31 21:24:14 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2004-07-28 00:39:00 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
#include "gimpbasetypes.h"
|
2001-05-21 21:58:46 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
#include "gimpbase-private.h"
|
|
|
|
#include "gimpunit.h"
|
2001-05-21 21:58:46 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
|
2010-06-30 01:13:40 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimpunit
|
|
|
|
* @title: gimpunit
|
|
|
|
* @short_description: Provides a collection of predefined units and
|
|
|
|
* functions for creating user-defined units.
|
|
|
|
* @see_also: #GimpUnitMenu, #GimpSizeEntry.
|
|
|
|
*
|
|
|
|
* Provides a collection of predefined units and functions for
|
|
|
|
* creating user-defined units.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2004-07-28 00:39:00 +08:00
|
|
|
static void unit_to_string (const GValue *src_value,
|
|
|
|
GValue *dest_value);
|
|
|
|
static void string_to_unit (const GValue *src_value,
|
|
|
|
GValue *dest_value);
|
|
|
|
|
|
|
|
GType
|
|
|
|
gimp_unit_get_type (void)
|
|
|
|
{
|
|
|
|
static GType unit_type = 0;
|
|
|
|
|
2006-10-18 21:17:50 +08:00
|
|
|
if (! unit_type)
|
2004-07-28 00:39:00 +08:00
|
|
|
{
|
2006-10-18 21:17:50 +08:00
|
|
|
const GTypeInfo type_info = { 0, };
|
2004-07-28 00:39:00 +08:00
|
|
|
|
|
|
|
unit_type = g_type_register_static (G_TYPE_INT, "GimpUnit",
|
|
|
|
&type_info, 0);
|
|
|
|
|
|
|
|
g_value_register_transform_func (unit_type, G_TYPE_STRING,
|
|
|
|
unit_to_string);
|
|
|
|
g_value_register_transform_func (G_TYPE_STRING, unit_type,
|
|
|
|
string_to_unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return unit_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unit_to_string (const GValue *src_value,
|
|
|
|
GValue *dest_value)
|
|
|
|
{
|
|
|
|
GimpUnit unit = (GimpUnit) g_value_get_int (src_value);
|
|
|
|
|
|
|
|
g_value_set_string (dest_value, gimp_unit_get_identifier (unit));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
string_to_unit (const GValue *src_value,
|
|
|
|
GValue *dest_value)
|
|
|
|
{
|
|
|
|
const gchar *str;
|
|
|
|
gint num_units;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
str = g_value_get_string (src_value);
|
|
|
|
|
|
|
|
if (!str || !*str)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
num_units = gimp_unit_get_number_of_units ();
|
|
|
|
|
|
|
|
for (i = GIMP_UNIT_PIXEL; i < num_units; i++)
|
|
|
|
if (strcmp (str, gimp_unit_get_identifier (i)) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == num_units)
|
|
|
|
{
|
|
|
|
if (strcmp (str, gimp_unit_get_identifier (GIMP_UNIT_PERCENT)) == 0)
|
|
|
|
i = GIMP_UNIT_PERCENT;
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_value_set_int (dest_value, i);
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
g_warning ("Can't convert string to GimpUnit.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-31 21:24:14 +08:00
|
|
|
/**
|
|
|
|
* gimp_unit_get_number_of_units:
|
|
|
|
*
|
|
|
|
* Returns the number of units which are known to the #GimpUnit system.
|
|
|
|
*
|
|
|
|
* Returns: The number of defined units.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2000-05-31 21:24:14 +08:00
|
|
|
gint
|
|
|
|
gimp_unit_get_number_of_units (void)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_number_of_units != NULL,
|
|
|
|
GIMP_UNIT_END);
|
|
|
|
|
|
|
|
return _gimp_unit_vtable.unit_get_number_of_units ();
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_number_of_built_in_units:
|
|
|
|
*
|
|
|
|
* Returns the number of #GimpUnit's which are hardcoded in the unit system
|
|
|
|
* (UNIT_INCH, UNIT_MM, UNIT_POINT, UNIT_PICA and the two "pseudo unit"
|
|
|
|
* UNIT_PIXEL).
|
|
|
|
*
|
|
|
|
* Returns: The number of built-in units.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2000-05-31 21:24:14 +08:00
|
|
|
gint
|
|
|
|
gimp_unit_get_number_of_built_in_units (void)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_number_of_built_in_units
|
|
|
|
!= NULL, GIMP_UNIT_END);
|
|
|
|
|
|
|
|
return _gimp_unit_vtable.unit_get_number_of_built_in_units ();
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_new:
|
|
|
|
* @identifier: The unit's identifier string.
|
|
|
|
* @factor: The unit's factor (how many units are in one inch).
|
|
|
|
* @digits: The unit's suggested number of digits (see gimp_unit_get_digits()).
|
|
|
|
* @symbol: The symbol of the unit (e.g. "''" for inch).
|
|
|
|
* @abbreviation: The abbreviation of the unit.
|
|
|
|
* @singular: The singular form of the unit.
|
|
|
|
* @plural: The plural form of the unit.
|
|
|
|
*
|
|
|
|
* Returns the integer ID of the new #GimpUnit.
|
|
|
|
*
|
|
|
|
* Note that a new unit is always created with it's deletion flag
|
2003-02-07 05:17:12 +08:00
|
|
|
* set to %TRUE. You will have to set it to %FALSE with
|
2000-05-31 21:24:14 +08:00
|
|
|
* gimp_unit_set_deletion_flag() to make the unit definition persistent.
|
|
|
|
*
|
|
|
|
* Returns: The ID of the new unit.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2000-05-31 21:24:14 +08:00
|
|
|
GimpUnit
|
|
|
|
gimp_unit_new (gchar *identifier,
|
2003-11-16 06:53:28 +08:00
|
|
|
gdouble factor,
|
|
|
|
gint digits,
|
|
|
|
gchar *symbol,
|
|
|
|
gchar *abbreviation,
|
|
|
|
gchar *singular,
|
|
|
|
gchar *plural)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_new != NULL, GIMP_UNIT_INCH);
|
|
|
|
|
|
|
|
return _gimp_unit_vtable.unit_new (identifier, factor, digits,
|
|
|
|
symbol, abbreviation, singular, plural);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_deletion_flag:
|
|
|
|
* @unit: The unit you want to know the @deletion_flag of.
|
|
|
|
*
|
|
|
|
* Returns: The unit's @deletion_flag.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2000-05-31 21:24:14 +08:00
|
|
|
gboolean
|
|
|
|
gimp_unit_get_deletion_flag (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_deletion_flag != NULL, FALSE);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_deletion_flag (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_set_deletion_flag:
|
|
|
|
* @unit: The unit you want to set the @deletion_flag for.
|
|
|
|
* @deletion_flag: The new deletion_flag.
|
|
|
|
*
|
|
|
|
* Sets a #GimpUnit's @deletion_flag. If the @deletion_flag of a unit is
|
2003-11-16 07:23:42 +08:00
|
|
|
* %TRUE when GIMP exits, this unit will not be saved in the users's
|
2000-05-31 21:24:14 +08:00
|
|
|
* "unitrc" file.
|
|
|
|
*
|
|
|
|
* Trying to change the @deletion_flag of a built-in unit will be silently
|
|
|
|
* ignored.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2000-05-31 21:24:14 +08:00
|
|
|
void
|
|
|
|
gimp_unit_set_deletion_flag (GimpUnit unit,
|
2003-11-16 06:53:28 +08:00
|
|
|
gboolean deletion_flag)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_if_fail (_gimp_unit_vtable.unit_set_deletion_flag != NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_vtable.unit_set_deletion_flag (unit, deletion_flag);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_factor:
|
|
|
|
* @unit: The unit you want to know the factor of.
|
|
|
|
*
|
|
|
|
* A #GimpUnit's @factor is defined to be:
|
|
|
|
*
|
|
|
|
* distance_in_units == (@factor * distance_in_inches)
|
|
|
|
*
|
|
|
|
* Returns 0 for @unit == GIMP_UNIT_PIXEL.
|
|
|
|
*
|
|
|
|
* Returns: The unit's factor.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2000-05-31 21:24:14 +08:00
|
|
|
gdouble
|
|
|
|
gimp_unit_get_factor (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_factor != NULL, 1.0);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_factor (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_digits:
|
|
|
|
* @unit: The unit you want to know the digits.
|
|
|
|
*
|
|
|
|
* Returns the number of digits an entry field should provide to get
|
|
|
|
* approximately the same accuracy as an inch input field with two digits.
|
|
|
|
*
|
|
|
|
* Returns 0 for @unit == GIMP_UNIT_PIXEL.
|
|
|
|
*
|
|
|
|
* Returns: The suggested number of digits.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2000-05-31 21:24:14 +08:00
|
|
|
gint
|
|
|
|
gimp_unit_get_digits (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_digits != NULL, 2);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_digits (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_identifier:
|
|
|
|
* @unit: The unit you want to know the identifier of.
|
|
|
|
*
|
2001-12-31 09:22:47 +08:00
|
|
|
* This is an unstranslated string and must not be changed or freed.
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
|
|
|
* Returns: The unit's identifier.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2003-11-16 06:53:28 +08:00
|
|
|
const gchar *
|
2000-05-31 21:24:14 +08:00
|
|
|
gimp_unit_get_identifier (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_identifier != NULL, NULL);
|
2001-12-31 09:22:47 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_identifier (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_symbol:
|
|
|
|
* @unit: The unit you want to know the symbol of.
|
|
|
|
*
|
|
|
|
* This is e.g. "''" for UNIT_INCH.
|
|
|
|
*
|
2001-12-31 09:22:47 +08:00
|
|
|
* NOTE: This string must not be changed or freed.
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
|
|
|
* Returns: The unit's symbol.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2000-05-31 21:24:14 +08:00
|
|
|
gimp_unit_get_symbol (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_symbol != NULL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_symbol (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_abbreviation:
|
|
|
|
* @unit: The unit you want to know the abbreviation of.
|
|
|
|
*
|
|
|
|
* For built-in units, this function returns the translated abbreviation
|
|
|
|
* of the unit.
|
|
|
|
*
|
2001-12-31 09:22:47 +08:00
|
|
|
* NOTE: This string must not be changed or freed.
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
|
|
|
* Returns: The unit's abbreviation.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2000-05-31 21:24:14 +08:00
|
|
|
gimp_unit_get_abbreviation (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_abbreviation != NULL, NULL);
|
2001-12-31 09:22:47 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_abbreviation (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_singular:
|
|
|
|
* @unit: The unit you want to know the singular form of.
|
|
|
|
*
|
|
|
|
* For built-in units, this function returns the translated singular form
|
|
|
|
* of the unit's name.
|
|
|
|
*
|
2001-12-31 09:22:47 +08:00
|
|
|
* NOTE: This string must not be changed or freed.
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
|
|
|
* Returns: The unit's singular form.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2000-05-31 21:24:14 +08:00
|
|
|
gimp_unit_get_singular (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_singular != NULL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_singular (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_unit_get_plural:
|
|
|
|
* @unit: The unit you want to know the plural form of.
|
|
|
|
*
|
|
|
|
* For built-in units, this function returns the translated plural form
|
|
|
|
* of the unit's name.
|
|
|
|
*
|
2001-12-31 09:22:47 +08:00
|
|
|
* NOTE: This string must not be changed or freed.
|
2000-05-31 21:24:14 +08:00
|
|
|
*
|
|
|
|
* Returns: The unit's plural form.
|
2003-11-16 07:23:42 +08:00
|
|
|
**/
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2000-05-31 21:24:14 +08:00
|
|
|
gimp_unit_get_plural (GimpUnit unit)
|
|
|
|
{
|
2003-11-16 06:53:28 +08:00
|
|
|
g_return_val_if_fail (_gimp_unit_vtable.unit_get_plural != NULL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
return _gimp_unit_vtable.unit_get_plural (unit);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
2005-01-23 10:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GIMP_TYPE_PARAM_UNIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GIMP_PARAM_SPEC_UNIT(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_UNIT, GimpParamSpecUnit))
|
|
|
|
|
|
|
|
typedef struct _GimpParamSpecUnit GimpParamSpecUnit;
|
|
|
|
|
|
|
|
struct _GimpParamSpecUnit
|
|
|
|
{
|
|
|
|
GParamSpecInt parent_instance;
|
|
|
|
|
|
|
|
gboolean allow_percent;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gimp_param_unit_class_init (GParamSpecClass *class);
|
|
|
|
static gboolean gimp_param_unit_value_validate (GParamSpec *pspec,
|
|
|
|
GValue *value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_param_unit_get_type:
|
|
|
|
*
|
|
|
|
* Reveals the object type
|
|
|
|
*
|
|
|
|
* Returns: the #GType for a unit param object
|
|
|
|
*
|
|
|
|
* Since: GIMP 2.4
|
|
|
|
**/
|
|
|
|
GType
|
|
|
|
gimp_param_unit_get_type (void)
|
|
|
|
{
|
|
|
|
static GType spec_type = 0;
|
|
|
|
|
2006-10-18 21:17:50 +08:00
|
|
|
if (! spec_type)
|
2005-01-23 10:09:41 +08:00
|
|
|
{
|
2006-10-18 21:17:50 +08:00
|
|
|
const GTypeInfo type_info =
|
2005-01-23 10:09:41 +08:00
|
|
|
{
|
|
|
|
sizeof (GParamSpecClass),
|
|
|
|
NULL, NULL,
|
|
|
|
(GClassInitFunc) gimp_param_unit_class_init,
|
|
|
|
NULL, NULL,
|
|
|
|
sizeof (GimpParamSpecUnit),
|
|
|
|
0, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
spec_type = g_type_register_static (G_TYPE_PARAM_INT,
|
|
|
|
"GimpParamUnit",
|
|
|
|
&type_info, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_unit_class_init (GParamSpecClass *class)
|
|
|
|
{
|
|
|
|
class->value_type = GIMP_TYPE_UNIT;
|
|
|
|
class->value_validate = gimp_param_unit_value_validate;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_param_unit_value_validate (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
|
|
|
{
|
|
|
|
GParamSpecInt *ispec = G_PARAM_SPEC_INT (pspec);
|
|
|
|
GimpParamSpecUnit *uspec = GIMP_PARAM_SPEC_UNIT (pspec);
|
|
|
|
gint oval = value->data[0].v_int;
|
|
|
|
|
|
|
|
if (uspec->allow_percent && value->data[0].v_int == GIMP_UNIT_PERCENT)
|
|
|
|
{
|
|
|
|
value->data[0].v_int = value->data[0].v_int;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value->data[0].v_int = CLAMP (value->data[0].v_int,
|
|
|
|
ispec->minimum,
|
|
|
|
gimp_unit_get_number_of_units () - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->data[0].v_int != oval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_param_spec_unit:
|
|
|
|
* @name: Canonical name of the param
|
|
|
|
* @nick: Nickname of the param
|
|
|
|
* @blurb: Brief desciption of param.
|
|
|
|
* @allow_pixels: Whether "pixels" is an allowed unit.
|
|
|
|
* @allow_percent: Whether "perecent" is an allowed unit.
|
|
|
|
* @default_value: Unit to use if none is assigned.
|
|
|
|
* @flags: a combination of #GParamFlags
|
|
|
|
*
|
|
|
|
* Creates a param spec to hold a units param.
|
|
|
|
* See g_param_spec_internal() for more information.
|
|
|
|
*
|
|
|
|
* Returns: a newly allocated #GParamSpec instance
|
|
|
|
*
|
|
|
|
* Since: GIMP 2.4
|
|
|
|
**/
|
|
|
|
GParamSpec *
|
|
|
|
gimp_param_spec_unit (const gchar *name,
|
|
|
|
const gchar *nick,
|
|
|
|
const gchar *blurb,
|
|
|
|
gboolean allow_pixels,
|
|
|
|
gboolean allow_percent,
|
|
|
|
GimpUnit default_value,
|
|
|
|
GParamFlags flags)
|
|
|
|
{
|
|
|
|
GimpParamSpecUnit *pspec;
|
|
|
|
GParamSpecInt *ispec;
|
|
|
|
|
|
|
|
pspec = g_param_spec_internal (GIMP_TYPE_PARAM_UNIT,
|
|
|
|
name, nick, blurb, flags);
|
|
|
|
|
|
|
|
ispec = G_PARAM_SPEC_INT (pspec);
|
|
|
|
|
|
|
|
ispec->default_value = default_value;
|
|
|
|
ispec->minimum = allow_pixels ? GIMP_UNIT_PIXEL : GIMP_UNIT_INCH;
|
|
|
|
ispec->maximum = GIMP_UNIT_PERCENT - 1;
|
|
|
|
|
|
|
|
pspec->allow_percent = allow_percent;
|
|
|
|
|
|
|
|
return G_PARAM_SPEC (pspec);
|
|
|
|
}
|
|
|
|
|
2010-02-21 23:45:01 +08:00
|
|
|
/**
|
|
|
|
* gimp_pixels_to_units:
|
|
|
|
* @pixels: value in pixels
|
|
|
|
* @unit: unit to convert to
|
2010-02-24 17:53:10 +08:00
|
|
|
* @resolution: resloution in DPI
|
2010-02-21 23:45:01 +08:00
|
|
|
*
|
2010-06-30 01:13:40 +08:00
|
|
|
* Converts a @value specified in pixels to @unit.
|
|
|
|
*
|
|
|
|
* Returns: @pixels converted to units.
|
2010-02-21 23:45:01 +08:00
|
|
|
*
|
|
|
|
* Since: GIMP 2.8
|
|
|
|
**/
|
|
|
|
gdouble
|
|
|
|
gimp_pixels_to_units (gdouble pixels,
|
|
|
|
GimpUnit unit,
|
|
|
|
gdouble resolution)
|
|
|
|
{
|
|
|
|
if (unit == GIMP_UNIT_PIXEL)
|
|
|
|
return pixels;
|
|
|
|
|
|
|
|
return pixels * gimp_unit_get_factor (unit) / resolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-24 17:53:10 +08:00
|
|
|
* gimp_units_to_pixels:
|
2010-02-21 23:45:01 +08:00
|
|
|
* @value: value in units
|
2010-06-30 01:13:40 +08:00
|
|
|
* @unit: unit of @value
|
2010-02-24 17:53:10 +08:00
|
|
|
* @resolution: resloution in DPI
|
2010-02-21 23:45:01 +08:00
|
|
|
*
|
2010-06-30 01:13:40 +08:00
|
|
|
* Converts a @value specified in @unit to pixels.
|
|
|
|
*
|
|
|
|
* Returns: @value converted to pixels.
|
2010-02-21 23:45:01 +08:00
|
|
|
*
|
|
|
|
* Since: GIMP 2.8
|
|
|
|
**/
|
|
|
|
gdouble
|
|
|
|
gimp_units_to_pixels (gdouble value,
|
|
|
|
GimpUnit unit,
|
|
|
|
gdouble resolution)
|
|
|
|
{
|
|
|
|
if (unit == GIMP_UNIT_PIXEL)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
return value * resolution / gimp_unit_get_factor (unit);
|
|
|
|
}
|