2000-05-31 21:24:14 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
2003-11-16 06:53:28 +08:00
|
|
|
* gimpunitcache.c
|
2000-05-31 21:24:14 +08:00
|
|
|
* Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2001-05-21 21:58:46 +08:00
|
|
|
#include "libgimpbase/gimpbasetypes.h"
|
|
|
|
|
|
|
|
#include "libgimpbase/gimpunit.h"
|
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
#include "gimpunitcache.h"
|
2000-05-31 21:24:14 +08:00
|
|
|
#include "gimpunit_pdb.h"
|
|
|
|
|
|
|
|
#include "libgimp-intl.h"
|
|
|
|
|
|
|
|
/* internal structures */
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2001-12-31 09:22:47 +08:00
|
|
|
gdouble factor;
|
|
|
|
gint digits;
|
|
|
|
const gchar *identifier;
|
|
|
|
const gchar *symbol;
|
|
|
|
const gchar *abbreviation;
|
|
|
|
const gchar *singular;
|
|
|
|
const gchar *plural;
|
2000-05-31 21:24:14 +08:00
|
|
|
} GimpUnitDef;
|
|
|
|
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
static GimpUnitDef * gimp_unit_defs = NULL;
|
|
|
|
static GimpUnit gimp_units_initialized = 0;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
|
|
|
/* not a unit at all but kept here to have the strings in one place
|
|
|
|
*/
|
|
|
|
static GimpUnitDef gimp_unit_percent =
|
|
|
|
{
|
2001-12-31 09:22:47 +08:00
|
|
|
0.0, 0, "percent", "%", "%", N_("percent"), N_("percent")
|
2000-05-31 21:24:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
static void gimp_unit_def_init (GimpUnitDef *unit_def,
|
|
|
|
GimpUnit unit);
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_unit_init (GimpUnit unit)
|
|
|
|
{
|
|
|
|
gint i, n;
|
|
|
|
|
|
|
|
if (unit < gimp_units_initialized)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
n = _gimp_unit_get_number_of_units ();
|
|
|
|
|
|
|
|
if (unit >= n)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gimp_unit_defs = g_renew (GimpUnitDef, gimp_unit_defs, n);
|
|
|
|
|
|
|
|
for (i = gimp_units_initialized; i < n; i++)
|
|
|
|
{
|
|
|
|
gimp_unit_def_init (&gimp_unit_defs[i], i);
|
|
|
|
}
|
|
|
|
|
|
|
|
gimp_units_initialized = n;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_unit_def_init (GimpUnitDef *unit_def,
|
|
|
|
GimpUnit unit)
|
|
|
|
{
|
|
|
|
unit_def->factor = _gimp_unit_get_factor (unit);
|
|
|
|
unit_def->digits = _gimp_unit_get_digits (unit);
|
|
|
|
unit_def->identifier = _gimp_unit_get_identifier (unit);
|
|
|
|
unit_def->symbol = _gimp_unit_get_symbol (unit);
|
|
|
|
unit_def->abbreviation = _gimp_unit_get_abbreviation (unit);
|
|
|
|
unit_def->singular = _gimp_unit_get_singular (unit);
|
2003-11-16 06:53:28 +08:00
|
|
|
unit_def->plural = _gimp_unit_get_plural (unit);
|
2001-12-31 09:22:47 +08:00
|
|
|
}
|
|
|
|
|
2000-05-31 21:24:14 +08:00
|
|
|
gint
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_number_of_units (void)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
|
|
|
return _gimp_unit_get_number_of_units ();
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_number_of_built_in_units (void)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
|
|
|
return GIMP_UNIT_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
GimpUnit
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_new (gchar *identifier,
|
|
|
|
gdouble factor,
|
|
|
|
gint digits,
|
|
|
|
gchar *symbol,
|
|
|
|
gchar *abbreviation,
|
|
|
|
gchar *singular,
|
|
|
|
gchar *plural)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
|
|
|
return _gimp_unit_new (identifier,
|
|
|
|
factor,
|
|
|
|
digits,
|
|
|
|
symbol,
|
|
|
|
abbreviation,
|
|
|
|
singular,
|
|
|
|
plural);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_deletion_flag (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, TRUE);
|
|
|
|
|
|
|
|
if (unit < GIMP_UNIT_END)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return _gimp_unit_get_deletion_flag (unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_set_deletion_flag (GimpUnit unit,
|
|
|
|
gboolean deletion_flag)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (unit >= GIMP_UNIT_PIXEL);
|
|
|
|
|
|
|
|
if (unit < GIMP_UNIT_END)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_gimp_unit_set_deletion_flag (unit,
|
|
|
|
deletion_flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
gdouble
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_factor (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 1.0);
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
if (unit == GIMP_UNIT_PERCENT)
|
|
|
|
return gimp_unit_percent.factor;
|
|
|
|
|
|
|
|
if (!gimp_unit_init (unit))
|
|
|
|
return 1.0;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
return gimp_unit_defs[unit].factor;
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_digits (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 0);
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
if (unit == GIMP_UNIT_PERCENT)
|
|
|
|
return gimp_unit_percent.digits;
|
|
|
|
|
|
|
|
if (!gimp_unit_init (unit))
|
|
|
|
return 0;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
return gimp_unit_defs[unit].digits;
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
2003-11-16 06:53:28 +08:00
|
|
|
const gchar *
|
|
|
|
_gimp_unit_cache_get_identifier (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
2001-12-31 09:22:47 +08:00
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
|
|
|
if (unit == GIMP_UNIT_PERCENT)
|
2001-12-31 09:22:47 +08:00
|
|
|
return gimp_unit_percent.identifier;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
if (!gimp_unit_init (unit))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gimp_unit_defs[unit].identifier;
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_symbol (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
2001-12-31 09:22:47 +08:00
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
|
|
|
if (unit == GIMP_UNIT_PERCENT)
|
2001-12-31 09:22:47 +08:00
|
|
|
return gimp_unit_percent.symbol;
|
|
|
|
|
|
|
|
if (!gimp_unit_init (unit))
|
|
|
|
return NULL;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
return gimp_unit_defs[unit].symbol;
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_abbreviation (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
2001-12-31 09:22:47 +08:00
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
|
|
|
if (unit == GIMP_UNIT_PERCENT)
|
2001-12-31 09:22:47 +08:00
|
|
|
return gimp_unit_percent.abbreviation;
|
|
|
|
|
|
|
|
if (!gimp_unit_init (unit))
|
|
|
|
return NULL;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
return gimp_unit_defs[unit].abbreviation;
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_singular (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
2001-12-31 09:22:47 +08:00
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
|
|
|
if (unit == GIMP_UNIT_PERCENT)
|
2001-12-31 09:22:47 +08:00
|
|
|
return gettext (gimp_unit_percent.singular);
|
|
|
|
|
|
|
|
if (!gimp_unit_init (unit))
|
|
|
|
return NULL;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
return gettext (gimp_unit_defs[unit].singular);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
const gchar *
|
2003-11-16 06:53:28 +08:00
|
|
|
_gimp_unit_cache_get_plural (GimpUnit unit)
|
2000-05-31 21:24:14 +08:00
|
|
|
{
|
2001-12-31 09:22:47 +08:00
|
|
|
g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
|
2000-05-31 21:24:14 +08:00
|
|
|
|
|
|
|
if (unit == GIMP_UNIT_PERCENT)
|
2001-12-31 09:22:47 +08:00
|
|
|
return gettext (gimp_unit_percent.plural);
|
|
|
|
|
|
|
|
if (!gimp_unit_init (unit))
|
|
|
|
return NULL;
|
2000-05-31 21:24:14 +08:00
|
|
|
|
2001-12-31 09:22:47 +08:00
|
|
|
return gettext (gimp_unit_defs[unit].plural);
|
2000-05-31 21:24:14 +08:00
|
|
|
}
|