~ubuntu-branches/ubuntu/vivid/gimp/vivid

« back to all changes in this revision

Viewing changes to libgimpwidgets/gimpunitstore.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-08 18:50:03 UTC
  • mto: (1.1.26) (0.5.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: package-import@ubuntu.com-20120508185003-tltkvbaysf8d2426
ImportĀ upstreamĀ versionĀ 2.8.0

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
 * gimpunitstore.c
 
5
 * Copyright (C) 2004  Sven Neumann <sven@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
 * 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, see
 
19
 * <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <gtk/gtk.h>
 
25
 
 
26
#include "libgimpbase/gimpbase.h"
 
27
 
 
28
#include "gimpwidgetstypes.h"
 
29
 
 
30
#include "gimpunitstore.h"
 
31
 
 
32
 
 
33
enum
 
34
{
 
35
  PROP_0,
 
36
  PROP_NUM_VALUES,
 
37
  PROP_HAS_PIXELS,
 
38
  PROP_HAS_PERCENT,
 
39
  PROP_SHORT_FORMAT,
 
40
  PROP_LONG_FORMAT
 
41
};
 
42
 
 
43
typedef struct
 
44
{
 
45
  gint      num_values;
 
46
  gboolean  has_pixels;
 
47
  gboolean  has_percent;
 
48
 
 
49
  gchar    *short_format;
 
50
  gchar    *long_format;
 
51
 
 
52
  gdouble  *values;
 
53
  gdouble  *resolutions;
 
54
} GimpUnitStorePrivate;
 
55
 
 
56
#define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE (obj, \
 
57
                                                      GIMP_TYPE_UNIT_STORE, \
 
58
                                                      GimpUnitStorePrivate)
 
59
 
 
60
 
 
61
static void         gimp_unit_store_tree_model_init (GtkTreeModelIface *iface);
 
62
 
 
63
static void         gimp_unit_store_finalize        (GObject      *object);
 
64
static void         gimp_unit_store_set_property    (GObject      *object,
 
65
                                                     guint         property_id,
 
66
                                                     const GValue *value,
 
67
                                                     GParamSpec   *pspec);
 
68
static void         gimp_unit_store_get_property    (GObject      *object,
 
69
                                                     guint         property_id,
 
70
                                                     GValue       *value,
 
71
                                                     GParamSpec   *pspec);
 
72
 
 
73
static GtkTreeModelFlags gimp_unit_store_get_flags  (GtkTreeModel *tree_model);
 
74
static gint         gimp_unit_store_get_n_columns   (GtkTreeModel *tree_model);
 
75
static GType        gimp_unit_store_get_column_type (GtkTreeModel *tree_model,
 
76
                                                     gint          index);
 
77
static gboolean     gimp_unit_store_get_iter        (GtkTreeModel *tree_model,
 
78
                                                     GtkTreeIter  *iter,
 
79
                                                     GtkTreePath  *path);
 
80
static GtkTreePath *gimp_unit_store_get_path        (GtkTreeModel *tree_model,
 
81
                                                     GtkTreeIter  *iter);
 
82
static void    gimp_unit_store_tree_model_get_value (GtkTreeModel *tree_model,
 
83
                                                     GtkTreeIter  *iter,
 
84
                                                     gint          column,
 
85
                                                     GValue       *value);
 
86
static gboolean     gimp_unit_store_iter_next       (GtkTreeModel *tree_model,
 
87
                                                     GtkTreeIter  *iter);
 
88
static gboolean     gimp_unit_store_iter_children   (GtkTreeModel *tree_model,
 
89
                                                     GtkTreeIter  *iter,
 
90
                                                     GtkTreeIter  *parent);
 
91
static gboolean     gimp_unit_store_iter_has_child  (GtkTreeModel *tree_model,
 
92
                                                     GtkTreeIter  *iter);
 
93
static gint         gimp_unit_store_iter_n_children (GtkTreeModel *tree_model,
 
94
                                                     GtkTreeIter  *iter);
 
95
static gboolean     gimp_unit_store_iter_nth_child  (GtkTreeModel *tree_model,
 
96
                                                     GtkTreeIter  *iter,
 
97
                                                     GtkTreeIter  *parent,
 
98
                                                     gint          n);
 
99
static gboolean     gimp_unit_store_iter_parent     (GtkTreeModel *tree_model,
 
100
                                                     GtkTreeIter  *iter,
 
101
                                                     GtkTreeIter  *child);
 
102
 
 
103
 
 
104
G_DEFINE_TYPE_WITH_CODE (GimpUnitStore, gimp_unit_store, G_TYPE_OBJECT,
 
105
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
 
106
                                                gimp_unit_store_tree_model_init))
 
107
 
 
108
#define parent_class gimp_unit_store_parent_class
 
109
 
 
110
 
 
111
static GType column_types[GIMP_UNIT_STORE_UNIT_COLUMNS] =
 
112
{
 
113
  G_TYPE_INVALID,
 
114
  G_TYPE_DOUBLE,
 
115
  G_TYPE_INT,
 
116
  G_TYPE_STRING,
 
117
  G_TYPE_STRING,
 
118
  G_TYPE_STRING,
 
119
  G_TYPE_STRING,
 
120
  G_TYPE_STRING,
 
121
  G_TYPE_STRING,
 
122
  G_TYPE_STRING
 
123
};
 
124
 
 
125
 
 
126
static void
 
127
gimp_unit_store_class_init (GimpUnitStoreClass *klass)
 
128
{
 
129
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
130
 
 
131
  column_types[GIMP_UNIT_STORE_UNIT] = GIMP_TYPE_UNIT;
 
132
 
 
133
  object_class->finalize     = gimp_unit_store_finalize;
 
134
  object_class->set_property = gimp_unit_store_set_property;
 
135
  object_class->get_property = gimp_unit_store_get_property;
 
136
 
 
137
  g_object_class_install_property (object_class, PROP_NUM_VALUES,
 
138
                                   g_param_spec_int ("num-values",
 
139
                                                     NULL, NULL,
 
140
                                                     0, G_MAXINT, 0,
 
141
                                                     GIMP_PARAM_READWRITE |
 
142
                                                     G_PARAM_CONSTRUCT_ONLY));
 
143
 
 
144
  g_object_class_install_property (object_class, PROP_HAS_PIXELS,
 
145
                                   g_param_spec_boolean ("has-pixels",
 
146
                                                         NULL, NULL,
 
147
                                                         TRUE,
 
148
                                                         GIMP_PARAM_READWRITE));
 
149
 
 
150
  g_object_class_install_property (object_class, PROP_HAS_PERCENT,
 
151
                                   g_param_spec_boolean ("has-percent",
 
152
                                                         NULL, NULL,
 
153
                                                         FALSE,
 
154
                                                         GIMP_PARAM_READWRITE));
 
155
 
 
156
  g_object_class_install_property (object_class, PROP_SHORT_FORMAT,
 
157
                                   g_param_spec_string ("short-format",
 
158
                                                        NULL, NULL,
 
159
                                                        "%a",
 
160
                                                        GIMP_PARAM_READWRITE));
 
161
 
 
162
  g_object_class_install_property (object_class, PROP_LONG_FORMAT,
 
163
                                   g_param_spec_string ("long-format",
 
164
                                                        NULL, NULL,
 
165
                                                        "%p",
 
166
                                                        GIMP_PARAM_READWRITE));
 
167
 
 
168
  g_type_class_add_private (object_class, sizeof (GimpUnitStorePrivate));
 
169
}
 
170
 
 
171
static void
 
172
gimp_unit_store_init (GimpUnitStore *store)
 
173
{
 
174
  GimpUnitStorePrivate *private = GET_PRIVATE (store);
 
175
 
 
176
  private->has_pixels   = TRUE;
 
177
  private->has_percent  = FALSE;
 
178
  private->short_format = g_strdup ("%a");
 
179
  private->long_format  = g_strdup ("%p");
 
180
}
 
181
 
 
182
static void
 
183
gimp_unit_store_tree_model_init (GtkTreeModelIface *iface)
 
184
{
 
185
  iface->get_flags       = gimp_unit_store_get_flags;
 
186
  iface->get_n_columns   = gimp_unit_store_get_n_columns;
 
187
  iface->get_column_type = gimp_unit_store_get_column_type;
 
188
  iface->get_iter        = gimp_unit_store_get_iter;
 
189
  iface->get_path        = gimp_unit_store_get_path;
 
190
  iface->get_value       = gimp_unit_store_tree_model_get_value;
 
191
  iface->iter_next       = gimp_unit_store_iter_next;
 
192
  iface->iter_children   = gimp_unit_store_iter_children;
 
193
  iface->iter_has_child  = gimp_unit_store_iter_has_child;
 
194
  iface->iter_n_children = gimp_unit_store_iter_n_children;
 
195
  iface->iter_nth_child  = gimp_unit_store_iter_nth_child;
 
196
  iface->iter_parent     = gimp_unit_store_iter_parent;
 
197
}
 
198
 
 
199
static void
 
200
gimp_unit_store_finalize (GObject *object)
 
201
{
 
202
  GimpUnitStorePrivate *private = GET_PRIVATE (object);
 
203
 
 
204
  if (private->short_format)
 
205
    {
 
206
      g_free (private->short_format);
 
207
      private->short_format = NULL;
 
208
    }
 
209
 
 
210
  if (private->long_format)
 
211
    {
 
212
      g_free (private->long_format);
 
213
      private->long_format = NULL;
 
214
    }
 
215
 
 
216
  if (private->num_values > 0)
 
217
    {
 
218
      g_free (private->values);
 
219
      g_free (private->resolutions);
 
220
      private->num_values = 0;
 
221
    }
 
222
 
 
223
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
224
}
 
225
 
 
226
static void
 
227
gimp_unit_store_set_property (GObject      *object,
 
228
                              guint         property_id,
 
229
                              const GValue *value,
 
230
                              GParamSpec   *pspec)
 
231
{
 
232
  GimpUnitStorePrivate *private = GET_PRIVATE (object);
 
233
 
 
234
  switch (property_id)
 
235
    {
 
236
    case PROP_NUM_VALUES:
 
237
      g_return_if_fail (private->num_values == 0);
 
238
      private->num_values = g_value_get_int (value);
 
239
      if (private->num_values)
 
240
        {
 
241
          private->values      = g_new0 (gdouble, private->num_values);
 
242
          private->resolutions = g_new0 (gdouble, private->num_values);
 
243
        }
 
244
      break;
 
245
    case PROP_HAS_PIXELS:
 
246
      gimp_unit_store_set_has_pixels (GIMP_UNIT_STORE (object),
 
247
                                      g_value_get_boolean (value));
 
248
      break;
 
249
    case PROP_HAS_PERCENT:
 
250
      gimp_unit_store_set_has_percent (GIMP_UNIT_STORE (object),
 
251
                                       g_value_get_boolean (value));
 
252
      break;
 
253
    case PROP_SHORT_FORMAT:
 
254
      g_free (private->short_format);
 
255
      private->short_format = g_value_dup_string (value);
 
256
      if (! private->short_format)
 
257
        private->short_format = g_strdup ("%a");
 
258
      break;
 
259
    case PROP_LONG_FORMAT:
 
260
      g_free (private->long_format);
 
261
      private->long_format = g_value_dup_string (value);
 
262
      if (! private->long_format)
 
263
        private->long_format = g_strdup ("%a");
 
264
      break;
 
265
 
 
266
    default:
 
267
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
268
      break;
 
269
    }
 
270
}
 
271
 
 
272
static void
 
273
gimp_unit_store_get_property (GObject      *object,
 
274
                              guint         property_id,
 
275
                              GValue       *value,
 
276
                              GParamSpec   *pspec)
 
277
{
 
278
  GimpUnitStorePrivate *private = GET_PRIVATE (object);
 
279
 
 
280
  switch (property_id)
 
281
    {
 
282
    case PROP_NUM_VALUES:
 
283
      g_value_set_int (value, private->num_values);
 
284
      break;
 
285
    case PROP_HAS_PIXELS:
 
286
      g_value_set_boolean (value, private->has_pixels);
 
287
      break;
 
288
    case PROP_HAS_PERCENT:
 
289
      g_value_set_boolean (value, private->has_percent);
 
290
      break;
 
291
    case PROP_SHORT_FORMAT:
 
292
      g_value_set_string (value, private->short_format);
 
293
      break;
 
294
    case PROP_LONG_FORMAT:
 
295
      g_value_set_string (value, private->long_format);
 
296
      break;
 
297
 
 
298
    default:
 
299
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
300
      break;
 
301
    }
 
302
}
 
303
 
 
304
static GtkTreeModelFlags
 
305
gimp_unit_store_get_flags (GtkTreeModel *tree_model)
 
306
{
 
307
  return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
 
308
}
 
309
 
 
310
static gint
 
311
gimp_unit_store_get_n_columns (GtkTreeModel *tree_model)
 
312
{
 
313
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
314
 
 
315
  return GIMP_UNIT_STORE_UNIT_COLUMNS + private->num_values;
 
316
}
 
317
 
 
318
static GType
 
319
gimp_unit_store_get_column_type (GtkTreeModel *tree_model,
 
320
                                 gint          index)
 
321
{
 
322
  g_return_val_if_fail (index >= 0, G_TYPE_INVALID);
 
323
 
 
324
  if (index < GIMP_UNIT_STORE_UNIT_COLUMNS)
 
325
    return column_types[index];
 
326
 
 
327
  return G_TYPE_DOUBLE;
 
328
}
 
329
 
 
330
static gboolean
 
331
gimp_unit_store_get_iter (GtkTreeModel *tree_model,
 
332
                          GtkTreeIter  *iter,
 
333
                          GtkTreePath  *path)
 
334
{
 
335
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
336
  gint                  index;
 
337
  GimpUnit              unit;
 
338
 
 
339
  g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
 
340
 
 
341
  index = gtk_tree_path_get_indices (path)[0];
 
342
 
 
343
  unit = index;
 
344
 
 
345
  if (! private->has_pixels)
 
346
    unit++;
 
347
 
 
348
  if (private->has_percent)
 
349
    {
 
350
      unit--;
 
351
 
 
352
      if (private->has_pixels)
 
353
        {
 
354
          if (index == 0)
 
355
            unit = GIMP_UNIT_PIXEL;
 
356
          else if (index == 1)
 
357
            unit = GIMP_UNIT_PERCENT;
 
358
        }
 
359
      else
 
360
        {
 
361
          if (index == 0)
 
362
            unit = GIMP_UNIT_PERCENT;
 
363
        }
 
364
    }
 
365
 
 
366
  if ((unit >= 0 && unit < gimp_unit_get_number_of_units ()) ||
 
367
      ((unit == GIMP_UNIT_PERCENT && private->has_percent)))
 
368
    {
 
369
      iter->user_data = GINT_TO_POINTER (unit);
 
370
      return TRUE;
 
371
    }
 
372
 
 
373
  return FALSE;
 
374
}
 
375
 
 
376
static GtkTreePath *
 
377
gimp_unit_store_get_path (GtkTreeModel *tree_model,
 
378
                          GtkTreeIter  *iter)
 
379
{
 
380
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
381
  GtkTreePath          *path    = gtk_tree_path_new ();
 
382
  GimpUnit              unit    = GPOINTER_TO_INT (iter->user_data);
 
383
  gint                  index;
 
384
 
 
385
  index = unit;
 
386
 
 
387
  if (! private->has_pixels)
 
388
    index--;
 
389
 
 
390
  if (private->has_percent)
 
391
    {
 
392
      index++;
 
393
 
 
394
      if (private->has_pixels)
 
395
        {
 
396
          if (unit == GIMP_UNIT_PIXEL)
 
397
            index = 0;
 
398
          else if (unit == GIMP_UNIT_PERCENT)
 
399
            index = 1;
 
400
        }
 
401
      else
 
402
        {
 
403
          if (unit == GIMP_UNIT_PERCENT)
 
404
            index = 0;
 
405
        }
 
406
    }
 
407
 
 
408
  gtk_tree_path_append_index (path, index);
 
409
 
 
410
  return path;
 
411
}
 
412
 
 
413
static void
 
414
gimp_unit_store_tree_model_get_value (GtkTreeModel *tree_model,
 
415
                                      GtkTreeIter  *iter,
 
416
                                      gint          column,
 
417
                                      GValue       *value)
 
418
{
 
419
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
420
  GimpUnit              unit;
 
421
 
 
422
  g_return_if_fail (column >= 0 &&
 
423
                    column < GIMP_UNIT_STORE_UNIT_COLUMNS + private->num_values);
 
424
 
 
425
  g_value_init (value,
 
426
                column < GIMP_UNIT_STORE_UNIT_COLUMNS ?
 
427
                column_types[column] :
 
428
                G_TYPE_DOUBLE);
 
429
 
 
430
  unit = GPOINTER_TO_INT (iter->user_data);
 
431
 
 
432
  if ((unit >= 0 && unit < gimp_unit_get_number_of_units ()) ||
 
433
      ((unit == GIMP_UNIT_PERCENT && private->has_percent)))
 
434
    {
 
435
      switch (column)
 
436
        {
 
437
        case GIMP_UNIT_STORE_UNIT:
 
438
          g_value_set_int (value, unit);
 
439
          break;
 
440
        case GIMP_UNIT_STORE_UNIT_FACTOR:
 
441
          g_value_set_double (value, gimp_unit_get_factor (unit));
 
442
          break;
 
443
        case GIMP_UNIT_STORE_UNIT_DIGITS:
 
444
          g_value_set_int (value, gimp_unit_get_digits (unit));
 
445
          break;
 
446
        case GIMP_UNIT_STORE_UNIT_IDENTIFIER:
 
447
          g_value_set_static_string (value, gimp_unit_get_identifier (unit));
 
448
          break;
 
449
        case GIMP_UNIT_STORE_UNIT_SYMBOL:
 
450
          g_value_set_static_string (value, gimp_unit_get_symbol (unit));
 
451
          break;
 
452
        case GIMP_UNIT_STORE_UNIT_ABBREVIATION:
 
453
          g_value_set_static_string (value, gimp_unit_get_abbreviation (unit));
 
454
          break;
 
455
        case GIMP_UNIT_STORE_UNIT_SINGULAR:
 
456
          g_value_set_static_string (value, gimp_unit_get_singular (unit));
 
457
          break;
 
458
        case GIMP_UNIT_STORE_UNIT_PLURAL:
 
459
          g_value_set_static_string (value, gimp_unit_get_plural (unit));
 
460
          break;
 
461
        case GIMP_UNIT_STORE_UNIT_SHORT_FORMAT:
 
462
          g_value_take_string (value,
 
463
                               gimp_unit_format_string (private->short_format,
 
464
                                                        unit));
 
465
          break;
 
466
        case GIMP_UNIT_STORE_UNIT_LONG_FORMAT:
 
467
          g_value_take_string (value,
 
468
                               gimp_unit_format_string (private->long_format,
 
469
                                                        unit));
 
470
          break;
 
471
 
 
472
        default:
 
473
          column -= GIMP_UNIT_STORE_UNIT_COLUMNS;
 
474
          if (unit == GIMP_UNIT_PIXEL)
 
475
            {
 
476
              g_value_set_double (value, private->values[column]);
 
477
            }
 
478
          else if (private->resolutions[column])
 
479
            {
 
480
              g_value_set_double (value,
 
481
                                  private->values[column] *
 
482
                                  gimp_unit_get_factor (unit) /
 
483
                                  private->resolutions[column]);
 
484
            }
 
485
          break;
 
486
        }
 
487
    }
 
488
}
 
489
 
 
490
static gboolean
 
491
gimp_unit_store_iter_next (GtkTreeModel *tree_model,
 
492
                           GtkTreeIter  *iter)
 
493
{
 
494
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
495
  GimpUnit              unit    = GPOINTER_TO_INT (iter->user_data);
 
496
 
 
497
  if (unit == GIMP_UNIT_PIXEL && private->has_percent)
 
498
    {
 
499
      unit = GIMP_UNIT_PERCENT;
 
500
    }
 
501
  else if (unit == GIMP_UNIT_PERCENT)
 
502
    {
 
503
      unit = GIMP_UNIT_INCH;
 
504
    }
 
505
  else if (unit >= 0 && unit < gimp_unit_get_number_of_units () - 1)
 
506
    {
 
507
      unit++;
 
508
    }
 
509
  else
 
510
    {
 
511
      return FALSE;
 
512
    }
 
513
 
 
514
  iter->user_data = GINT_TO_POINTER (unit);
 
515
 
 
516
  return TRUE;
 
517
}
 
518
 
 
519
static gboolean
 
520
gimp_unit_store_iter_children (GtkTreeModel *tree_model,
 
521
                               GtkTreeIter  *iter,
 
522
                               GtkTreeIter  *parent)
 
523
{
 
524
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
525
  GimpUnit              unit;
 
526
 
 
527
  /* this is a list, nodes have no children */
 
528
  if (parent)
 
529
    return FALSE;
 
530
 
 
531
  if (private->has_pixels)
 
532
    {
 
533
      unit = GIMP_UNIT_PIXEL;
 
534
    }
 
535
  else if (private->has_percent)
 
536
    {
 
537
      unit = GIMP_UNIT_PERCENT;
 
538
    }
 
539
  else
 
540
    {
 
541
      unit = GIMP_UNIT_INCH;
 
542
    }
 
543
 
 
544
  iter->user_data = GINT_TO_POINTER (unit);
 
545
 
 
546
  return TRUE;
 
547
}
 
548
 
 
549
static gboolean
 
550
gimp_unit_store_iter_has_child (GtkTreeModel *tree_model,
 
551
                                GtkTreeIter  *iter)
 
552
{
 
553
  return FALSE;
 
554
}
 
555
 
 
556
static gint
 
557
gimp_unit_store_iter_n_children (GtkTreeModel *tree_model,
 
558
                                 GtkTreeIter  *iter)
 
559
{
 
560
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
561
  gint                  n_children;
 
562
 
 
563
  if (iter)
 
564
    return 0;
 
565
 
 
566
  n_children = gimp_unit_get_number_of_units ();
 
567
 
 
568
  if (! private->has_pixels)
 
569
    n_children--;
 
570
 
 
571
  if (private->has_percent)
 
572
    n_children++;
 
573
 
 
574
  return n_children;
 
575
}
 
576
 
 
577
static gboolean
 
578
gimp_unit_store_iter_nth_child (GtkTreeModel *tree_model,
 
579
                                GtkTreeIter  *iter,
 
580
                                GtkTreeIter  *parent,
 
581
                                gint          n)
 
582
{
 
583
  GimpUnitStorePrivate *private = GET_PRIVATE (tree_model);
 
584
  gint                  n_children;
 
585
 
 
586
  if (parent)
 
587
    return FALSE;
 
588
 
 
589
  n_children = gimp_unit_store_iter_n_children (tree_model, NULL);
 
590
 
 
591
  if (n >= 0 && n < n_children)
 
592
    {
 
593
      GimpUnit unit = n;
 
594
 
 
595
      if (! private->has_pixels)
 
596
        unit++;
 
597
 
 
598
      if (private->has_percent)
 
599
        {
 
600
          unit--;
 
601
 
 
602
          if (private->has_pixels)
 
603
            {
 
604
              if (n == 0)
 
605
                unit = GIMP_UNIT_PIXEL;
 
606
              else if (n == 1)
 
607
                unit = GIMP_UNIT_PERCENT;
 
608
            }
 
609
          else
 
610
            {
 
611
              if (n == 0)
 
612
                unit = GIMP_UNIT_PERCENT;
 
613
            }
 
614
        }
 
615
 
 
616
      iter->user_data = GINT_TO_POINTER (unit);
 
617
 
 
618
      return TRUE;
 
619
    }
 
620
 
 
621
  return FALSE;
 
622
}
 
623
 
 
624
static gboolean
 
625
gimp_unit_store_iter_parent (GtkTreeModel *tree_model,
 
626
                             GtkTreeIter  *iter,
 
627
                             GtkTreeIter  *child)
 
628
{
 
629
  return FALSE;
 
630
}
 
631
 
 
632
 
 
633
GimpUnitStore *
 
634
gimp_unit_store_new (gint  num_values)
 
635
{
 
636
  return g_object_new (GIMP_TYPE_UNIT_STORE,
 
637
                       "num-values", num_values,
 
638
                       NULL);
 
639
}
 
640
 
 
641
void
 
642
gimp_unit_store_set_has_pixels (GimpUnitStore *store,
 
643
                                gboolean       has_pixels)
 
644
{
 
645
  GimpUnitStorePrivate *private;
 
646
 
 
647
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
 
648
 
 
649
  private = GET_PRIVATE (store);
 
650
 
 
651
  has_pixels = has_pixels ? TRUE : FALSE;
 
652
 
 
653
  if (has_pixels != private->has_pixels)
 
654
    {
 
655
      GtkTreeModel *model        = GTK_TREE_MODEL (store);
 
656
      GtkTreePath  *deleted_path = NULL;
 
657
 
 
658
      if (! has_pixels)
 
659
        {
 
660
          GtkTreeIter iter;
 
661
 
 
662
          gtk_tree_model_get_iter_first (model, &iter);
 
663
          deleted_path = gtk_tree_model_get_path (model, &iter);
 
664
        }
 
665
 
 
666
      private->has_pixels = has_pixels;
 
667
 
 
668
      if (has_pixels)
 
669
        {
 
670
          GtkTreePath *path;
 
671
          GtkTreeIter  iter;
 
672
 
 
673
          gtk_tree_model_get_iter_first (model, &iter);
 
674
          path = gtk_tree_model_get_path (model, &iter);
 
675
          gtk_tree_model_row_inserted (model, path, &iter);
 
676
          gtk_tree_path_free (path);
 
677
        }
 
678
      else if (deleted_path)
 
679
        {
 
680
          gtk_tree_model_row_deleted (model, deleted_path);
 
681
          gtk_tree_path_free (deleted_path);
 
682
        }
 
683
 
 
684
      g_object_notify (G_OBJECT (store), "has-pixels");
 
685
    }
 
686
}
 
687
 
 
688
gboolean
 
689
gimp_unit_store_get_has_pixels (GimpUnitStore *store)
 
690
{
 
691
  GimpUnitStorePrivate *private;
 
692
 
 
693
  g_return_val_if_fail (GIMP_IS_UNIT_STORE (store), FALSE);
 
694
 
 
695
  private = GET_PRIVATE (store);
 
696
 
 
697
  return private->has_pixels;
 
698
}
 
699
 
 
700
void
 
701
gimp_unit_store_set_has_percent (GimpUnitStore *store,
 
702
                                 gboolean       has_percent)
 
703
{
 
704
  GimpUnitStorePrivate *private;
 
705
 
 
706
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
 
707
 
 
708
  private = GET_PRIVATE (store);
 
709
 
 
710
  has_percent = has_percent ? TRUE : FALSE;
 
711
 
 
712
  if (has_percent != private->has_percent)
 
713
    {
 
714
      GtkTreeModel *model        = GTK_TREE_MODEL (store);
 
715
      GtkTreePath  *deleted_path = NULL;
 
716
 
 
717
      if (! has_percent)
 
718
        {
 
719
          GtkTreeIter iter;
 
720
 
 
721
          gtk_tree_model_get_iter_first (model, &iter);
 
722
          if (private->has_pixels)
 
723
            gtk_tree_model_iter_next (model, &iter);
 
724
          deleted_path = gtk_tree_model_get_path (model, &iter);
 
725
        }
 
726
 
 
727
      private->has_percent = has_percent;
 
728
 
 
729
      if (has_percent)
 
730
        {
 
731
          GtkTreePath *path;
 
732
          GtkTreeIter  iter;
 
733
 
 
734
          gtk_tree_model_get_iter_first (model, &iter);
 
735
           if (private->has_pixels)
 
736
            gtk_tree_model_iter_next (model, &iter);
 
737
         path = gtk_tree_model_get_path (model, &iter);
 
738
          gtk_tree_model_row_inserted (model, path, &iter);
 
739
          gtk_tree_path_free (path);
 
740
        }
 
741
      else if (deleted_path)
 
742
        {
 
743
          gtk_tree_model_row_deleted (model, deleted_path);
 
744
          gtk_tree_path_free (deleted_path);
 
745
        }
 
746
 
 
747
      g_object_notify (G_OBJECT (store), "has-percent");
 
748
    }
 
749
}
 
750
 
 
751
gboolean
 
752
gimp_unit_store_get_has_percent (GimpUnitStore *store)
 
753
{
 
754
  GimpUnitStorePrivate *private;
 
755
 
 
756
  g_return_val_if_fail (GIMP_IS_UNIT_STORE (store), FALSE);
 
757
 
 
758
  private = GET_PRIVATE (store);
 
759
 
 
760
  return private->has_percent;
 
761
}
 
762
 
 
763
void
 
764
gimp_unit_store_set_pixel_value (GimpUnitStore *store,
 
765
                                 gint           index,
 
766
                                 gdouble        value)
 
767
{
 
768
  GimpUnitStorePrivate *private;
 
769
 
 
770
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
 
771
 
 
772
  private = GET_PRIVATE (store);
 
773
 
 
774
  g_return_if_fail (index > 0 && index < private->num_values);
 
775
 
 
776
  private->values[index] = value;
 
777
}
 
778
 
 
779
void
 
780
gimp_unit_store_set_pixel_values (GimpUnitStore *store,
 
781
                                  gdouble        first_value,
 
782
                                  ...)
 
783
{
 
784
  GimpUnitStorePrivate *private;
 
785
  va_list               args;
 
786
  gint                  i;
 
787
 
 
788
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
 
789
 
 
790
  private = GET_PRIVATE (store);
 
791
 
 
792
  va_start (args, first_value);
 
793
 
 
794
  for (i = 0; i < private->num_values; )
 
795
    {
 
796
      private->values[i] = first_value;
 
797
 
 
798
      if (++i < private->num_values)
 
799
        first_value = va_arg (args, gdouble);
 
800
    }
 
801
 
 
802
  va_end (args);
 
803
}
 
804
 
 
805
void
 
806
gimp_unit_store_set_resolution (GimpUnitStore *store,
 
807
                                gint           index,
 
808
                                gdouble        resolution)
 
809
{
 
810
  GimpUnitStorePrivate *private;
 
811
 
 
812
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
 
813
 
 
814
  private = GET_PRIVATE (store);
 
815
 
 
816
  g_return_if_fail (index > 0 && index < private->num_values);
 
817
 
 
818
  private->resolutions[index] = resolution;
 
819
}
 
820
 
 
821
void
 
822
gimp_unit_store_set_resolutions  (GimpUnitStore *store,
 
823
                                  gdouble        first_resolution,
 
824
                                  ...)
 
825
{
 
826
  GimpUnitStorePrivate *private;
 
827
  va_list               args;
 
828
  gint                  i;
 
829
 
 
830
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
 
831
 
 
832
  private = GET_PRIVATE (store);
 
833
 
 
834
  va_start (args, first_resolution);
 
835
 
 
836
  for (i = 0; i < private->num_values; )
 
837
    {
 
838
      private->resolutions[i] = first_resolution;
 
839
 
 
840
      if (++i < private->num_values)
 
841
        first_resolution = va_arg (args, gdouble);
 
842
    }
 
843
 
 
844
  va_end (args);
 
845
}
 
846
 
 
847
gdouble
 
848
gimp_unit_store_get_value (GimpUnitStore *store,
 
849
                           GimpUnit       unit,
 
850
                           gint           index)
 
851
{
 
852
  GimpUnitStorePrivate *private;
 
853
  GtkTreeIter          iter;
 
854
  GValue               value = { 0, };
 
855
 
 
856
  g_return_val_if_fail (GIMP_IS_UNIT_STORE (store), 0.0);
 
857
 
 
858
  private = GET_PRIVATE (store);
 
859
 
 
860
  g_return_val_if_fail (index >= 0 && index < private->num_values, 0.0);
 
861
 
 
862
  iter.user_data = GINT_TO_POINTER (unit);
 
863
 
 
864
  gimp_unit_store_tree_model_get_value (GTK_TREE_MODEL (store),
 
865
                                        &iter,
 
866
                                        GIMP_UNIT_STORE_FIRST_VALUE + index,
 
867
                                        &value);
 
868
 
 
869
  return g_value_get_double (&value);
 
870
}
 
871
 
 
872
void
 
873
gimp_unit_store_get_values (GimpUnitStore *store,
 
874
                            GimpUnit       unit,
 
875
                            gdouble       *first_value,
 
876
                            ...)
 
877
{
 
878
  GimpUnitStorePrivate *private;
 
879
  va_list               args;
 
880
  gint                  i;
 
881
 
 
882
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
 
883
 
 
884
  private = GET_PRIVATE (store);
 
885
 
 
886
  va_start (args, first_value);
 
887
 
 
888
  for (i = 0; i < private->num_values; )
 
889
    {
 
890
      if (first_value)
 
891
        *first_value = gimp_unit_store_get_value (store, unit, i);
 
892
 
 
893
      if (++i < private->num_values)
 
894
        first_value = va_arg (args, gdouble *);
 
895
    }
 
896
 
 
897
  va_end (args);
 
898
}