~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to libgimpbase/gimpunit.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LIBGIMP - The GIMP Library
 
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 
3
 *
 
4
 * gimpunit.c
 
5
 * Copyright (C) 2003 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 2 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
 * Lesser 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, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <string.h>
 
26
 
 
27
#include <glib-object.h>
 
28
 
 
29
#include "gimpbasetypes.h"
 
30
 
 
31
#include "gimpbase-private.h"
 
32
#include "gimpunit.h"
 
33
 
 
34
 
 
35
static void   unit_to_string (const GValue *src_value,
 
36
                              GValue       *dest_value);
 
37
static void   string_to_unit (const GValue *src_value,
 
38
                              GValue       *dest_value);
 
39
 
 
40
GType
 
41
gimp_unit_get_type (void)
 
42
{
 
43
  static GType unit_type = 0;
 
44
 
 
45
  if (!unit_type)
 
46
    {
 
47
      static const GTypeInfo type_info = { 0, };
 
48
 
 
49
      unit_type = g_type_register_static (G_TYPE_INT, "GimpUnit",
 
50
                                          &type_info, 0);
 
51
 
 
52
      g_value_register_transform_func (unit_type, G_TYPE_STRING,
 
53
                                       unit_to_string);
 
54
      g_value_register_transform_func (G_TYPE_STRING, unit_type,
 
55
                                       string_to_unit);
 
56
    }
 
57
 
 
58
  return unit_type;
 
59
}
 
60
 
 
61
static void
 
62
unit_to_string (const GValue *src_value,
 
63
                GValue       *dest_value)
 
64
{
 
65
  GimpUnit unit = (GimpUnit) g_value_get_int (src_value);
 
66
 
 
67
  g_value_set_string (dest_value, gimp_unit_get_identifier (unit));
 
68
}
 
69
 
 
70
static void
 
71
string_to_unit (const GValue *src_value,
 
72
                GValue       *dest_value)
 
73
{
 
74
  const gchar *str;
 
75
  gint         num_units;
 
76
  gint         i;
 
77
 
 
78
  str = g_value_get_string (src_value);
 
79
 
 
80
  if (!str || !*str)
 
81
    goto error;
 
82
 
 
83
  num_units = gimp_unit_get_number_of_units ();
 
84
 
 
85
  for (i = GIMP_UNIT_PIXEL; i < num_units; i++)
 
86
    if (strcmp (str, gimp_unit_get_identifier (i)) == 0)
 
87
      break;
 
88
 
 
89
  if (i == num_units)
 
90
    {
 
91
      if (strcmp (str, gimp_unit_get_identifier (GIMP_UNIT_PERCENT)) == 0)
 
92
        i = GIMP_UNIT_PERCENT;
 
93
      else
 
94
        goto error;
 
95
    }
 
96
 
 
97
  g_value_set_int (dest_value, i);
 
98
  return;
 
99
 
 
100
 error:
 
101
  g_warning ("Can't convert string to GimpUnit.");
 
102
}
 
103
 
 
104
 
 
105
/**
 
106
 * gimp_unit_get_number_of_units:
 
107
 *
 
108
 * Returns the number of units which are known to the #GimpUnit system.
 
109
 *
 
110
 * Returns: The number of defined units.
 
111
 **/
 
112
gint
 
113
gimp_unit_get_number_of_units (void)
 
114
{
 
115
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_number_of_units != NULL,
 
116
                        GIMP_UNIT_END);
 
117
 
 
118
  return _gimp_unit_vtable.unit_get_number_of_units ();
 
119
}
 
120
 
 
121
/**
 
122
 * gimp_unit_get_number_of_built_in_units:
 
123
 *
 
124
 * Returns the number of #GimpUnit's which are hardcoded in the unit system
 
125
 * (UNIT_INCH, UNIT_MM, UNIT_POINT, UNIT_PICA and the two "pseudo unit"
 
126
 *  UNIT_PIXEL).
 
127
 *
 
128
 * Returns: The number of built-in units.
 
129
 **/
 
130
gint
 
131
gimp_unit_get_number_of_built_in_units (void)
 
132
{
 
133
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_number_of_built_in_units
 
134
                        != NULL, GIMP_UNIT_END);
 
135
 
 
136
  return _gimp_unit_vtable.unit_get_number_of_built_in_units ();
 
137
}
 
138
 
 
139
/**
 
140
 * gimp_unit_new:
 
141
 * @identifier: The unit's identifier string.
 
142
 * @factor: The unit's factor (how many units are in one inch).
 
143
 * @digits: The unit's suggested number of digits (see gimp_unit_get_digits()).
 
144
 * @symbol: The symbol of the unit (e.g. "''" for inch).
 
145
 * @abbreviation: The abbreviation of the unit.
 
146
 * @singular: The singular form of the unit.
 
147
 * @plural: The plural form of the unit.
 
148
 *
 
149
 * Returns the integer ID of the new #GimpUnit.
 
150
 *
 
151
 * Note that a new unit is always created with it's deletion flag
 
152
 * set to %TRUE. You will have to set it to %FALSE with
 
153
 * gimp_unit_set_deletion_flag() to make the unit definition persistent.
 
154
 *
 
155
 * Returns: The ID of the new unit.
 
156
 **/
 
157
GimpUnit
 
158
gimp_unit_new (gchar   *identifier,
 
159
               gdouble  factor,
 
160
               gint     digits,
 
161
               gchar   *symbol,
 
162
               gchar   *abbreviation,
 
163
               gchar   *singular,
 
164
               gchar   *plural)
 
165
{
 
166
  g_return_val_if_fail (_gimp_unit_vtable.unit_new != NULL, GIMP_UNIT_INCH);
 
167
 
 
168
  return _gimp_unit_vtable.unit_new (identifier, factor, digits,
 
169
                                     symbol, abbreviation, singular, plural);
 
170
}
 
171
 
 
172
/**
 
173
 * gimp_unit_get_deletion_flag:
 
174
 * @unit: The unit you want to know the @deletion_flag of.
 
175
 *
 
176
 * Returns: The unit's @deletion_flag.
 
177
 **/
 
178
gboolean
 
179
gimp_unit_get_deletion_flag (GimpUnit unit)
 
180
{
 
181
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_deletion_flag != NULL, FALSE);
 
182
 
 
183
  return _gimp_unit_vtable.unit_get_deletion_flag (unit);
 
184
}
 
185
 
 
186
/**
 
187
 * gimp_unit_set_deletion_flag:
 
188
 * @unit: The unit you want to set the @deletion_flag for.
 
189
 * @deletion_flag: The new deletion_flag.
 
190
 *
 
191
 * Sets a #GimpUnit's @deletion_flag. If the @deletion_flag of a unit is
 
192
 * %TRUE when GIMP exits, this unit will not be saved in the users's
 
193
 * "unitrc" file.
 
194
 *
 
195
 * Trying to change the @deletion_flag of a built-in unit will be silently
 
196
 * ignored.
 
197
 **/
 
198
void
 
199
gimp_unit_set_deletion_flag (GimpUnit unit,
 
200
                             gboolean deletion_flag)
 
201
{
 
202
  g_return_if_fail (_gimp_unit_vtable.unit_set_deletion_flag != NULL);
 
203
 
 
204
  _gimp_unit_vtable.unit_set_deletion_flag (unit, deletion_flag);
 
205
}
 
206
 
 
207
/**
 
208
 * gimp_unit_get_factor:
 
209
 * @unit: The unit you want to know the factor of.
 
210
 *
 
211
 * A #GimpUnit's @factor is defined to be:
 
212
 *
 
213
 * distance_in_units == (@factor * distance_in_inches)
 
214
 *
 
215
 * Returns 0 for @unit == GIMP_UNIT_PIXEL.
 
216
 *
 
217
 * Returns: The unit's factor.
 
218
 **/
 
219
gdouble
 
220
gimp_unit_get_factor (GimpUnit unit)
 
221
{
 
222
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_factor != NULL, 1.0);
 
223
 
 
224
  return _gimp_unit_vtable.unit_get_factor (unit);
 
225
}
 
226
 
 
227
/**
 
228
 * gimp_unit_get_digits:
 
229
 * @unit: The unit you want to know the digits.
 
230
 *
 
231
 * Returns the number of digits an entry field should provide to get
 
232
 * approximately the same accuracy as an inch input field with two digits.
 
233
 *
 
234
 * Returns 0 for @unit == GIMP_UNIT_PIXEL.
 
235
 *
 
236
 * Returns: The suggested number of digits.
 
237
 **/
 
238
gint
 
239
gimp_unit_get_digits (GimpUnit unit)
 
240
{
 
241
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_digits != NULL, 2);
 
242
 
 
243
  return _gimp_unit_vtable.unit_get_digits (unit);
 
244
}
 
245
 
 
246
/**
 
247
 * gimp_unit_get_identifier:
 
248
 * @unit: The unit you want to know the identifier of.
 
249
 *
 
250
 * This is an unstranslated string and must not be changed or freed.
 
251
 *
 
252
 * Returns: The unit's identifier.
 
253
 **/
 
254
const gchar *
 
255
gimp_unit_get_identifier (GimpUnit unit)
 
256
{
 
257
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_identifier != NULL, NULL);
 
258
 
 
259
  return _gimp_unit_vtable.unit_get_identifier (unit);
 
260
}
 
261
 
 
262
/**
 
263
 * gimp_unit_get_symbol:
 
264
 * @unit: The unit you want to know the symbol of.
 
265
 *
 
266
 * This is e.g. "''" for UNIT_INCH.
 
267
 *
 
268
 * NOTE: This string must not be changed or freed.
 
269
 *
 
270
 * Returns: The unit's symbol.
 
271
 **/
 
272
const gchar *
 
273
gimp_unit_get_symbol (GimpUnit unit)
 
274
{
 
275
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_symbol != NULL, NULL);
 
276
 
 
277
  return _gimp_unit_vtable.unit_get_symbol (unit);
 
278
}
 
279
 
 
280
/**
 
281
 * gimp_unit_get_abbreviation:
 
282
 * @unit: The unit you want to know the abbreviation of.
 
283
 *
 
284
 * For built-in units, this function returns the translated abbreviation
 
285
 * of the unit.
 
286
 *
 
287
 * NOTE: This string must not be changed or freed.
 
288
 *
 
289
 * Returns: The unit's abbreviation.
 
290
 **/
 
291
const gchar *
 
292
gimp_unit_get_abbreviation (GimpUnit unit)
 
293
{
 
294
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_abbreviation != NULL, NULL);
 
295
 
 
296
  return _gimp_unit_vtable.unit_get_abbreviation (unit);
 
297
}
 
298
 
 
299
/**
 
300
 * gimp_unit_get_singular:
 
301
 * @unit: The unit you want to know the singular form of.
 
302
 *
 
303
 * For built-in units, this function returns the translated singular form
 
304
 * of the unit's name.
 
305
 *
 
306
 * NOTE: This string must not be changed or freed.
 
307
 *
 
308
 * Returns: The unit's singular form.
 
309
 **/
 
310
const gchar *
 
311
gimp_unit_get_singular (GimpUnit unit)
 
312
{
 
313
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_singular != NULL, NULL);
 
314
 
 
315
  return _gimp_unit_vtable.unit_get_singular (unit);
 
316
}
 
317
 
 
318
/**
 
319
 * gimp_unit_get_plural:
 
320
 * @unit: The unit you want to know the plural form of.
 
321
 *
 
322
 * For built-in units, this function returns the translated plural form
 
323
 * of the unit's name.
 
324
 *
 
325
 * NOTE: This string must not be changed or freed.
 
326
 *
 
327
 * Returns: The unit's plural form.
 
328
 **/
 
329
const gchar *
 
330
gimp_unit_get_plural (GimpUnit unit)
 
331
{
 
332
  g_return_val_if_fail (_gimp_unit_vtable.unit_get_plural != NULL, NULL);
 
333
 
 
334
  return _gimp_unit_vtable.unit_get_plural (unit);
 
335
}