gimpunitcache.c 8.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpunit.c
 * 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>

27 28 29 30
#include "libgimpbase/gimpbasetypes.h"

#include "libgimpbase/gimpunit.h"

31 32 33 34 35 36 37 38
#include "gimpunit_pdb.h"

#include "libgimp-intl.h"

/*  internal structures  */

typedef struct
{
39 40 41 42 43 44 45
  gdouble      factor;
  gint         digits;
  const gchar *identifier;
  const gchar *symbol;
  const gchar *abbreviation;
  const gchar *singular;
  const gchar *plural;
46 47 48
} GimpUnitDef;


49 50
static GimpUnitDef * gimp_unit_defs         = NULL;
static GimpUnit      gimp_units_initialized = 0;
51 52 53 54 55

/*  not a unit at all but kept here to have the strings in one place
 */
static GimpUnitDef gimp_unit_percent =
{
56
  0.0, 0, "percent", "%", "%",  N_("percent"), N_("percent")
57 58 59
};


60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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);
  unit_def->plural       = _gimp_unit_get_plural (unit);  
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/**
 * gimp_unit_get_number_of_units:
 *
 * Returns the number of units which are known to the #GimpUnit system.
 *
 * Returns: The number of defined units.
 */
gint
gimp_unit_get_number_of_units (void)
{
  return _gimp_unit_get_number_of_units ();
}

/**
 * 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.
 */
gint
gimp_unit_get_number_of_built_in_units (void)
{
  return GIMP_UNIT_END;
}

/**
 * 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
143
 * set to %TRUE. You will have to set it to %FALSE with
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
 * gimp_unit_set_deletion_flag() to make the unit definition persistent.
 *
 * Returns: The ID of the new unit.
 */
GimpUnit
gimp_unit_new (gchar   *identifier,
	       gdouble  factor,
	       gint     digits,
	       gchar   *symbol,
	       gchar   *abbreviation,
	       gchar   *singular,
	       gchar   *plural)
{
  return _gimp_unit_new (identifier,
			 factor,
			 digits,
			 symbol,
			 abbreviation,
			 singular,
			 plural);
}

/**
 * gimp_unit_get_deletion_flag:
 * @unit: The unit you want to know the @deletion_flag of.
 *
 * Returns: The unit's @deletion_flag.
 */
gboolean
gimp_unit_get_deletion_flag (GimpUnit unit)
{
  g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, TRUE);

  if (unit < GIMP_UNIT_END)
    return FALSE;

  return _gimp_unit_get_deletion_flag (unit);
}

/**
 * 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
189
 * %TRUE when GIMP exits, this unit will not be saved in the uses's
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
 * "unitrc" file.
 *
 * Trying to change the @deletion_flag of a built-in unit will be silently
 * ignored.
 */
void
gimp_unit_set_deletion_flag (GimpUnit unit,
			     gboolean deletion_flag)
{
  g_return_if_fail (unit >= GIMP_UNIT_PIXEL);

  if (unit < GIMP_UNIT_END)
    return;

  _gimp_unit_set_deletion_flag (unit,
				deletion_flag);
}

/**
 * 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.
 */
gdouble
gimp_unit_get_factor (GimpUnit unit)
{
  g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 1.0);

225 226 227 228 229
  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.factor;

  if (!gimp_unit_init (unit))
    return 1.0;
230

231
  return gimp_unit_defs[unit].factor;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
}

/**
 * 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.
 */
gint
gimp_unit_get_digits (GimpUnit unit)
{
  g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 0);

250 251 252 253 254
  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.digits;

  if (!gimp_unit_init (unit))
    return 0;
255

256
  return gimp_unit_defs[unit].digits;
257 258 259 260 261 262
}

/**
 * gimp_unit_get_identifier:
 * @unit: The unit you want to know the identifier of.
 *
263
 * This is an unstranslated string and must not be changed or freed.
264 265 266
 *
 * Returns: The unit's identifier.
 */
267
const gchar * 
268 269
gimp_unit_get_identifier (GimpUnit unit)
{
270
  g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
271 272

  if (unit == GIMP_UNIT_PERCENT)
273
    return gimp_unit_percent.identifier;
274

275 276 277 278
  if (!gimp_unit_init (unit))
    return NULL;

  return gimp_unit_defs[unit].identifier;
279 280 281 282 283 284 285 286
}

/**
 * gimp_unit_get_symbol:
 * @unit: The unit you want to know the symbol of.
 *
 * This is e.g. "''" for UNIT_INCH.
 *
287
 * NOTE: This string must not be changed or freed.
288 289 290
 *
 * Returns: The unit's symbol.
 */
291
const gchar *
292 293
gimp_unit_get_symbol (GimpUnit unit)
{
294
  g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
295 296

  if (unit == GIMP_UNIT_PERCENT)
297 298 299 300
    return gimp_unit_percent.symbol;

  if (!gimp_unit_init (unit))
    return NULL;
301

302
  return gimp_unit_defs[unit].symbol;
303 304 305 306 307 308 309 310 311
}

/**
 * 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.
 *
312
 * NOTE: This string must not be changed or freed.
313 314 315
 *
 * Returns: The unit's abbreviation.
 */
316
const gchar *
317 318
gimp_unit_get_abbreviation (GimpUnit unit)
{
319
  g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
320 321

  if (unit == GIMP_UNIT_PERCENT)
322 323 324 325
    return gimp_unit_percent.abbreviation;

  if (!gimp_unit_init (unit))
    return NULL;
326

327
  return gimp_unit_defs[unit].abbreviation;
328 329 330 331 332 333 334 335 336
}

/**
 * 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.
 *
337
 * NOTE: This string must not be changed or freed.
338 339 340
 *
 * Returns: The unit's singular form.
 */
341
const gchar *
342 343
gimp_unit_get_singular (GimpUnit unit)
{
344
  g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
345 346

  if (unit == GIMP_UNIT_PERCENT)
347 348 349 350
    return gettext (gimp_unit_percent.singular);

  if (!gimp_unit_init (unit))
    return NULL;
351

352
  return gettext (gimp_unit_defs[unit].singular);
353 354 355 356 357 358 359 360 361
}

/**
 * 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.
 *
362
 * NOTE: This string must not be changed or freed.
363 364 365 366
 *
 * Returns: The unit's plural form.
 *
 */
367
const gchar *
368 369
gimp_unit_get_plural (GimpUnit unit)
{
370
  g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, NULL);
371 372

  if (unit == GIMP_UNIT_PERCENT)
373 374 375 376
    return gettext (gimp_unit_percent.plural);

  if (!gimp_unit_init (unit))
    return NULL;
377

378
  return gettext (gimp_unit_defs[unit].plural);
379
}