gimpunitcache.c 5.28 KB
Newer Older
1 2 3
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
4
 * gimpunitcache.c
5 6
 * Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * 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
18 19
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24
#include <gio/gio.h>
25

26
#include "libgimpbase/gimpbase.h"
27

28
#include "gimpunitcache.h"
29 30 31 32 33 34 35 36
#include "gimpunit_pdb.h"

#include "libgimp-intl.h"

/*  internal structures  */

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


47 48
static GimpUnitDef * gimp_unit_defs         = NULL;
static GimpUnit      gimp_units_initialized = 0;
49 50 51

/*  not a unit at all but kept here to have the strings in one place
 */
52
static const GimpUnitDef gimp_unit_percent =
53
{
54
  0.0, 0, "percent", "%", "%",  N_("percent"), N_("percent")
55 56 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
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);
97
  unit_def->plural       = _gimp_unit_get_plural (unit);
98 99
}

100
gint
101
_gimp_unit_cache_get_number_of_units (void)
102 103 104 105 106
{
  return _gimp_unit_get_number_of_units ();
}

gint
107
_gimp_unit_cache_get_number_of_built_in_units (void)
108 109 110 111 112
{
  return GIMP_UNIT_END;
}

GimpUnit
113 114 115 116 117 118 119
_gimp_unit_cache_new (gchar   *identifier,
                      gdouble  factor,
                      gint     digits,
                      gchar   *symbol,
                      gchar   *abbreviation,
                      gchar   *singular,
                      gchar   *plural)
120 121
{
  return _gimp_unit_new (identifier,
Sven Neumann's avatar
Sven Neumann committed
122 123 124 125 126 127
                         factor,
                         digits,
                         symbol,
                         abbreviation,
                         singular,
                         plural);
128 129 130
}

gboolean
131
_gimp_unit_cache_get_deletion_flag (GimpUnit unit)
132 133 134 135 136 137 138 139
{
  if (unit < GIMP_UNIT_END)
    return FALSE;

  return _gimp_unit_get_deletion_flag (unit);
}

void
140 141
_gimp_unit_cache_set_deletion_flag (GimpUnit unit,
                                    gboolean deletion_flag)
142 143 144 145 146
{
  if (unit < GIMP_UNIT_END)
    return;

  _gimp_unit_set_deletion_flag (unit,
Sven Neumann's avatar
Sven Neumann committed
147
                                deletion_flag);
148 149 150
}

gdouble
151
_gimp_unit_cache_get_factor (GimpUnit unit)
152 153 154
{
  g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 1.0);

155 156 157 158 159
  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.factor;

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

161
  return gimp_unit_defs[unit].factor;
162 163 164
}

gint
165
_gimp_unit_cache_get_digits (GimpUnit unit)
166 167 168
{
  g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 0);

169 170 171 172 173
  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.digits;

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

175
  return gimp_unit_defs[unit].digits;
176 177
}

178 179
const gchar *
_gimp_unit_cache_get_identifier (GimpUnit unit)
180 181
{
  if (unit == GIMP_UNIT_PERCENT)
182
    return gimp_unit_percent.identifier;
183

184 185 186 187
  if (!gimp_unit_init (unit))
    return NULL;

  return gimp_unit_defs[unit].identifier;
188 189
}

190
const gchar *
191
_gimp_unit_cache_get_symbol (GimpUnit unit)
192 193
{
  if (unit == GIMP_UNIT_PERCENT)
194 195 196 197
    return gimp_unit_percent.symbol;

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

199
  return gimp_unit_defs[unit].symbol;
200 201
}

202
const gchar *
203
_gimp_unit_cache_get_abbreviation (GimpUnit unit)
204 205
{
  if (unit == GIMP_UNIT_PERCENT)
206 207 208 209
    return gimp_unit_percent.abbreviation;

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

211
  return gimp_unit_defs[unit].abbreviation;
212 213
}

214
const gchar *
215
_gimp_unit_cache_get_singular (GimpUnit unit)
216 217
{
  if (unit == GIMP_UNIT_PERCENT)
218 219 220 221
    return gettext (gimp_unit_percent.singular);

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

223
  return gettext (gimp_unit_defs[unit].singular);
224 225
}

226
const gchar *
227
_gimp_unit_cache_get_plural (GimpUnit unit)
228 229
{
  if (unit == GIMP_UNIT_PERCENT)
230 231 232 233
    return gettext (gimp_unit_percent.plural);

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

235
  return gettext (gimp_unit_defs[unit].plural);
236
}