1 /* LIBGIMP - The GIMP Library
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 *
4 * gimpunitcache.c
5 * Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
6 *
7 * This library is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see
19 * <https://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25
26 #include "libgimpbase/gimpbase.h"
27
28 #include "gimpunitcache.h"
29 #include "gimpunit_pdb.h"
30
31 #include "libgimp-intl.h"
32
33 /* internal structures */
34
35 typedef struct
36 {
37 gdouble factor;
38 gint digits;
39 const gchar *identifier;
40 const gchar *symbol;
41 const gchar *abbreviation;
42 const gchar *singular;
43 const gchar *plural;
44 } GimpUnitDef;
45
46
47 static GimpUnitDef * gimp_unit_defs = NULL;
48 static GimpUnit gimp_units_initialized = 0;
49
50 /* not a unit at all but kept here to have the strings in one place
51 */
52 static const GimpUnitDef gimp_unit_percent =
53 {
54 0.0, 0, "percent", "%", "%", N_("percent"), N_("percent")
55 };
56
57
58 static void gimp_unit_def_init (GimpUnitDef *unit_def,
59 GimpUnit unit);
60
61
62 static gboolean
gimp_unit_init(GimpUnit unit)63 gimp_unit_init (GimpUnit unit)
64 {
65 gint i, n;
66
67 if (unit < gimp_units_initialized)
68 return TRUE;
69
70 n = _gimp_unit_get_number_of_units ();
71
72 if (unit >= n)
73 return FALSE;
74
75 gimp_unit_defs = g_renew (GimpUnitDef, gimp_unit_defs, n);
76
77 for (i = gimp_units_initialized; i < n; i++)
78 {
79 gimp_unit_def_init (&gimp_unit_defs[i], i);
80 }
81
82 gimp_units_initialized = n;
83
84 return TRUE;
85 }
86
87 static void
gimp_unit_def_init(GimpUnitDef * unit_def,GimpUnit unit)88 gimp_unit_def_init (GimpUnitDef *unit_def,
89 GimpUnit unit)
90 {
91 unit_def->factor = _gimp_unit_get_factor (unit);
92 unit_def->digits = _gimp_unit_get_digits (unit);
93 unit_def->identifier = _gimp_unit_get_identifier (unit);
94 unit_def->symbol = _gimp_unit_get_symbol (unit);
95 unit_def->abbreviation = _gimp_unit_get_abbreviation (unit);
96 unit_def->singular = _gimp_unit_get_singular (unit);
97 unit_def->plural = _gimp_unit_get_plural (unit);
98 }
99
100 gint
_gimp_unit_cache_get_number_of_units(void)101 _gimp_unit_cache_get_number_of_units (void)
102 {
103 return _gimp_unit_get_number_of_units ();
104 }
105
106 gint
_gimp_unit_cache_get_number_of_built_in_units(void)107 _gimp_unit_cache_get_number_of_built_in_units (void)
108 {
109 return GIMP_UNIT_END;
110 }
111
112 GimpUnit
_gimp_unit_cache_new(gchar * identifier,gdouble factor,gint digits,gchar * symbol,gchar * abbreviation,gchar * singular,gchar * plural)113 _gimp_unit_cache_new (gchar *identifier,
114 gdouble factor,
115 gint digits,
116 gchar *symbol,
117 gchar *abbreviation,
118 gchar *singular,
119 gchar *plural)
120 {
121 return _gimp_unit_new (identifier,
122 factor,
123 digits,
124 symbol,
125 abbreviation,
126 singular,
127 plural);
128 }
129
130 gboolean
_gimp_unit_cache_get_deletion_flag(GimpUnit unit)131 _gimp_unit_cache_get_deletion_flag (GimpUnit unit)
132 {
133 if (unit < GIMP_UNIT_END)
134 return FALSE;
135
136 return _gimp_unit_get_deletion_flag (unit);
137 }
138
139 void
_gimp_unit_cache_set_deletion_flag(GimpUnit unit,gboolean deletion_flag)140 _gimp_unit_cache_set_deletion_flag (GimpUnit unit,
141 gboolean deletion_flag)
142 {
143 if (unit < GIMP_UNIT_END)
144 return;
145
146 _gimp_unit_set_deletion_flag (unit,
147 deletion_flag);
148 }
149
150 gdouble
_gimp_unit_cache_get_factor(GimpUnit unit)151 _gimp_unit_cache_get_factor (GimpUnit unit)
152 {
153 g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 1.0);
154
155 if (unit == GIMP_UNIT_PERCENT)
156 return gimp_unit_percent.factor;
157
158 if (!gimp_unit_init (unit))
159 return 1.0;
160
161 return gimp_unit_defs[unit].factor;
162 }
163
164 gint
_gimp_unit_cache_get_digits(GimpUnit unit)165 _gimp_unit_cache_get_digits (GimpUnit unit)
166 {
167 g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 0);
168
169 if (unit == GIMP_UNIT_PERCENT)
170 return gimp_unit_percent.digits;
171
172 if (!gimp_unit_init (unit))
173 return 0;
174
175 return gimp_unit_defs[unit].digits;
176 }
177
178 const gchar *
_gimp_unit_cache_get_identifier(GimpUnit unit)179 _gimp_unit_cache_get_identifier (GimpUnit unit)
180 {
181 if (unit == GIMP_UNIT_PERCENT)
182 return gimp_unit_percent.identifier;
183
184 if (!gimp_unit_init (unit))
185 return NULL;
186
187 return gimp_unit_defs[unit].identifier;
188 }
189
190 const gchar *
_gimp_unit_cache_get_symbol(GimpUnit unit)191 _gimp_unit_cache_get_symbol (GimpUnit unit)
192 {
193 if (unit == GIMP_UNIT_PERCENT)
194 return gimp_unit_percent.symbol;
195
196 if (!gimp_unit_init (unit))
197 return NULL;
198
199 return gimp_unit_defs[unit].symbol;
200 }
201
202 const gchar *
_gimp_unit_cache_get_abbreviation(GimpUnit unit)203 _gimp_unit_cache_get_abbreviation (GimpUnit unit)
204 {
205 if (unit == GIMP_UNIT_PERCENT)
206 return gimp_unit_percent.abbreviation;
207
208 if (!gimp_unit_init (unit))
209 return NULL;
210
211 return gimp_unit_defs[unit].abbreviation;
212 }
213
214 const gchar *
_gimp_unit_cache_get_singular(GimpUnit unit)215 _gimp_unit_cache_get_singular (GimpUnit unit)
216 {
217 if (unit == GIMP_UNIT_PERCENT)
218 return gettext (gimp_unit_percent.singular);
219
220 if (!gimp_unit_init (unit))
221 return NULL;
222
223 return gettext (gimp_unit_defs[unit].singular);
224 }
225
226 const gchar *
_gimp_unit_cache_get_plural(GimpUnit unit)227 _gimp_unit_cache_get_plural (GimpUnit unit)
228 {
229 if (unit == GIMP_UNIT_PERCENT)
230 return gettext (gimp_unit_percent.plural);
231
232 if (!gimp_unit_init (unit))
233 return NULL;
234
235 return gettext (gimp_unit_defs[unit].plural);
236 }
237