~ubuntu-branches/ubuntu/quantal/gimp/quantal-updates

« back to all changes in this revision

Viewing changes to app/widgets/gimpunitstore.c

  • Committer: Package Import Robot
  • Author(s): Micah Gersten
  • Date: 2012-05-20 19:21:01 UTC
  • mfrom: (1.1.26) (0.4.16 sid)
  • Revision ID: package-import@ubuntu.com-20120520192101-bs7zetx8ffoq2nfv
Tags: 2.8.0-2ubuntu1
* Merge from Debian unstable (LP: #908472). Remaining Changes:
  - debian/patches/02_help-message.patch,
    debian/patches/03_gimp.desktop.in.in.patch:
    + Update some strings for Ubuntu
  - debian/control:
    + Update description
  - debian/rules:
    + Set gettext domain and update translation templates
* Drop the following patches that were applied upstream:
  - debian/patches/ghost-cursor.patch: fix Wacom tablet cursor events
  - debian/patches/embed-page-setup-dialog.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GIMP - The GNU Image Manipulation Program
2
 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
 
 *
4
 
 * gimpunitstore.c
5
 
 * Copyright (C) 2004  Sven Neumann <sven@gimp.org>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program 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
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <gtk/gtk.h>
25
 
 
26
 
#include "libgimpbase/gimpbase.h"
27
 
#include "libgimpwidgets/gimpwidgets.h"
28
 
 
29
 
#include "widgets-types.h"
30
 
 
31
 
#include "gimpunitstore.h"
32
 
 
33
 
 
34
 
enum
35
 
{
36
 
  PROP_0,
37
 
  PROP_NUM_VALUES
38
 
};
39
 
 
40
 
 
41
 
static void         gimp_unit_store_tree_model_init (GtkTreeModelIface *iface);
42
 
 
43
 
static void         gimp_unit_store_finalize        (GObject      *object);
44
 
static void         gimp_unit_store_set_property    (GObject      *object,
45
 
                                                     guint         property_id,
46
 
                                                     const GValue *value,
47
 
                                                     GParamSpec   *pspec);
48
 
static void         gimp_unit_store_get_property    (GObject      *object,
49
 
                                                     guint         property_id,
50
 
                                                     GValue       *value,
51
 
                                                     GParamSpec   *pspec);
52
 
 
53
 
static GtkTreeModelFlags gimp_unit_store_get_flags  (GtkTreeModel *tree_model);
54
 
static gint         gimp_unit_store_get_n_columns   (GtkTreeModel *tree_model);
55
 
static GType        gimp_unit_store_get_column_type (GtkTreeModel *tree_model,
56
 
                                                     gint          index);
57
 
static gboolean     gimp_unit_store_get_iter        (GtkTreeModel *tree_model,
58
 
                                                     GtkTreeIter  *iter,
59
 
                                                     GtkTreePath  *path);
60
 
static GtkTreePath *gimp_unit_store_get_path        (GtkTreeModel *tree_model,
61
 
                                                     GtkTreeIter  *iter);
62
 
static void    gimp_unit_store_tree_model_get_value (GtkTreeModel *tree_model,
63
 
                                                     GtkTreeIter  *iter,
64
 
                                                     gint          column,
65
 
                                                     GValue       *value);
66
 
static gboolean     gimp_unit_store_iter_next       (GtkTreeModel *tree_model,
67
 
                                                     GtkTreeIter  *iter);
68
 
static gboolean     gimp_unit_store_iter_children   (GtkTreeModel *tree_model,
69
 
                                                     GtkTreeIter  *iter,
70
 
                                                     GtkTreeIter  *parent);
71
 
static gboolean     gimp_unit_store_iter_has_child  (GtkTreeModel *tree_model,
72
 
                                                     GtkTreeIter  *iter);
73
 
static gint         gimp_unit_store_iter_n_children (GtkTreeModel *tree_model,
74
 
                                                     GtkTreeIter  *iter);
75
 
static gboolean     gimp_unit_store_iter_nth_child  (GtkTreeModel *tree_model,
76
 
                                                     GtkTreeIter  *iter,
77
 
                                                     GtkTreeIter  *parent,
78
 
                                                     gint          n);
79
 
static gboolean     gimp_unit_store_iter_parent     (GtkTreeModel *tree_model,
80
 
                                                     GtkTreeIter  *iter,
81
 
                                                     GtkTreeIter  *child);
82
 
 
83
 
 
84
 
G_DEFINE_TYPE_WITH_CODE (GimpUnitStore, gimp_unit_store, G_TYPE_OBJECT,
85
 
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
86
 
                                                gimp_unit_store_tree_model_init))
87
 
 
88
 
#define parent_class gimp_unit_store_parent_class
89
 
 
90
 
 
91
 
static GType column_types[GIMP_UNIT_STORE_UNIT_COLUMNS] =
92
 
{
93
 
  G_TYPE_INVALID,
94
 
  G_TYPE_DOUBLE,
95
 
  G_TYPE_INT,
96
 
  G_TYPE_STRING,
97
 
  G_TYPE_STRING,
98
 
  G_TYPE_STRING,
99
 
  G_TYPE_STRING,
100
 
  G_TYPE_STRING
101
 
};
102
 
 
103
 
 
104
 
static void
105
 
gimp_unit_store_class_init (GimpUnitStoreClass *klass)
106
 
{
107
 
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
108
 
 
109
 
  column_types[GIMP_UNIT_STORE_UNIT] = GIMP_TYPE_UNIT;
110
 
 
111
 
  object_class->finalize     = gimp_unit_store_finalize;
112
 
  object_class->set_property = gimp_unit_store_set_property;
113
 
  object_class->get_property = gimp_unit_store_get_property;
114
 
 
115
 
  g_object_class_install_property (object_class, PROP_NUM_VALUES,
116
 
                                   g_param_spec_int ("num-values",
117
 
                                                     NULL, NULL,
118
 
                                                     0, G_MAXINT, 0,
119
 
                                                     GIMP_PARAM_READWRITE |
120
 
                                                     G_PARAM_CONSTRUCT_ONLY));
121
 
}
122
 
 
123
 
static void
124
 
gimp_unit_store_init (GimpUnitStore *store)
125
 
{
126
 
}
127
 
 
128
 
static void
129
 
gimp_unit_store_tree_model_init (GtkTreeModelIface *iface)
130
 
{
131
 
  iface->get_flags       = gimp_unit_store_get_flags;
132
 
  iface->get_n_columns   = gimp_unit_store_get_n_columns;
133
 
  iface->get_column_type = gimp_unit_store_get_column_type;
134
 
  iface->get_iter        = gimp_unit_store_get_iter;
135
 
  iface->get_path        = gimp_unit_store_get_path;
136
 
  iface->get_value       = gimp_unit_store_tree_model_get_value;
137
 
  iface->iter_next       = gimp_unit_store_iter_next;
138
 
  iface->iter_children   = gimp_unit_store_iter_children;
139
 
  iface->iter_has_child  = gimp_unit_store_iter_has_child;
140
 
  iface->iter_n_children = gimp_unit_store_iter_n_children;
141
 
  iface->iter_nth_child  = gimp_unit_store_iter_nth_child;
142
 
  iface->iter_parent     = gimp_unit_store_iter_parent;
143
 
}
144
 
 
145
 
static void
146
 
gimp_unit_store_finalize (GObject *object)
147
 
{
148
 
  GimpUnitStore *store = GIMP_UNIT_STORE (object);
149
 
 
150
 
  if (store->num_values > 0)
151
 
    {
152
 
      g_free (store->values);
153
 
      g_free (store->resolutions);
154
 
      store->num_values = 0;
155
 
    }
156
 
 
157
 
  G_OBJECT_CLASS (parent_class)->finalize (object);
158
 
}
159
 
 
160
 
static void
161
 
gimp_unit_store_set_property (GObject      *object,
162
 
                              guint         property_id,
163
 
                              const GValue *value,
164
 
                              GParamSpec   *pspec)
165
 
{
166
 
  GimpUnitStore *store = GIMP_UNIT_STORE (object);
167
 
 
168
 
  switch (property_id)
169
 
    {
170
 
    case PROP_NUM_VALUES:
171
 
      g_return_if_fail (store->num_values == 0);
172
 
      store->num_values = g_value_get_int (value);
173
 
      if (store->num_values)
174
 
        {
175
 
          store->values      = g_new0 (gdouble, store->num_values);
176
 
          store->resolutions = g_new0 (gdouble, store->num_values);
177
 
        }
178
 
      break;
179
 
 
180
 
    default:
181
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
182
 
      break;
183
 
    }
184
 
}
185
 
 
186
 
static void
187
 
gimp_unit_store_get_property (GObject      *object,
188
 
                              guint         property_id,
189
 
                              GValue       *value,
190
 
                              GParamSpec   *pspec)
191
 
{
192
 
  GimpUnitStore *store = GIMP_UNIT_STORE (object);
193
 
 
194
 
  switch (property_id)
195
 
    {
196
 
    case PROP_NUM_VALUES:
197
 
      g_value_set_int (value, store->num_values);
198
 
      break;
199
 
    default:
200
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
201
 
      break;
202
 
    }
203
 
}
204
 
 
205
 
static GtkTreeModelFlags
206
 
gimp_unit_store_get_flags (GtkTreeModel *tree_model)
207
 
{
208
 
  return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
209
 
}
210
 
 
211
 
static gint
212
 
gimp_unit_store_get_n_columns (GtkTreeModel *tree_model)
213
 
{
214
 
  GimpUnitStore *store = GIMP_UNIT_STORE (tree_model);
215
 
 
216
 
  return GIMP_UNIT_STORE_UNIT_COLUMNS + store->num_values;
217
 
}
218
 
 
219
 
static GType
220
 
gimp_unit_store_get_column_type (GtkTreeModel *tree_model,
221
 
                                 gint          index)
222
 
{
223
 
  g_return_val_if_fail (index >= 0, G_TYPE_INVALID);
224
 
 
225
 
  if (index < GIMP_UNIT_STORE_UNIT_COLUMNS)
226
 
    return column_types[index];
227
 
 
228
 
  return G_TYPE_DOUBLE;
229
 
}
230
 
 
231
 
static gboolean
232
 
gimp_unit_store_get_iter (GtkTreeModel *tree_model,
233
 
                          GtkTreeIter  *iter,
234
 
                          GtkTreePath  *path)
235
 
{
236
 
  gint  unit;
237
 
 
238
 
  g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
239
 
 
240
 
  unit = gtk_tree_path_get_indices (path)[0];
241
 
 
242
 
  if (unit >= 0 && unit < gimp_unit_get_number_of_units ())
243
 
    {
244
 
      iter->user_data = GINT_TO_POINTER (unit);
245
 
      return TRUE;
246
 
    }
247
 
 
248
 
  return FALSE;
249
 
}
250
 
 
251
 
static GtkTreePath *
252
 
gimp_unit_store_get_path (GtkTreeModel *tree_model,
253
 
                          GtkTreeIter  *iter)
254
 
{
255
 
  GtkTreePath *path = gtk_tree_path_new ();
256
 
 
257
 
  gtk_tree_path_append_index (path, GPOINTER_TO_INT (iter->user_data));
258
 
 
259
 
  return path;
260
 
}
261
 
 
262
 
static void
263
 
gimp_unit_store_tree_model_get_value (GtkTreeModel *tree_model,
264
 
                                      GtkTreeIter  *iter,
265
 
                                      gint          column,
266
 
                                      GValue       *value)
267
 
{
268
 
  GimpUnitStore *store = GIMP_UNIT_STORE (tree_model);
269
 
  GimpUnit       unit;
270
 
 
271
 
  g_return_if_fail (column >= 0 &&
272
 
                    column < GIMP_UNIT_STORE_UNIT_COLUMNS + store->num_values);
273
 
 
274
 
  g_value_init (value,
275
 
                column < GIMP_UNIT_STORE_UNIT_COLUMNS ?
276
 
                column_types[column] :
277
 
                G_TYPE_DOUBLE);
278
 
 
279
 
  unit = GPOINTER_TO_INT (iter->user_data);
280
 
 
281
 
  if (unit >= 0 && unit < gimp_unit_get_number_of_units ())
282
 
    {
283
 
      switch (column)
284
 
        {
285
 
        case GIMP_UNIT_STORE_UNIT:
286
 
          g_value_set_int (value, unit);
287
 
          break;
288
 
        case GIMP_UNIT_STORE_UNIT_FACTOR:
289
 
          g_value_set_double (value, gimp_unit_get_factor (unit));
290
 
          break;
291
 
        case GIMP_UNIT_STORE_UNIT_DIGITS:
292
 
          g_value_set_int (value, gimp_unit_get_digits (unit));
293
 
          break;
294
 
        case GIMP_UNIT_STORE_UNIT_IDENTIFIER:
295
 
          g_value_set_static_string (value, gimp_unit_get_identifier (unit));
296
 
          break;
297
 
        case GIMP_UNIT_STORE_UNIT_SYMBOL:
298
 
          g_value_set_static_string (value, gimp_unit_get_symbol (unit));
299
 
          break;
300
 
        case GIMP_UNIT_STORE_UNIT_ABBREVIATION:
301
 
          g_value_set_static_string (value, gimp_unit_get_abbreviation (unit));
302
 
          break;
303
 
        case GIMP_UNIT_STORE_UNIT_SINGULAR:
304
 
          g_value_set_static_string (value, gimp_unit_get_singular (unit));
305
 
          break;
306
 
        case GIMP_UNIT_STORE_UNIT_PLURAL:
307
 
          g_value_set_static_string (value, gimp_unit_get_plural (unit));
308
 
          break;
309
 
 
310
 
        default:
311
 
          column -= GIMP_UNIT_STORE_UNIT_COLUMNS;
312
 
          if (unit == GIMP_UNIT_PIXEL)
313
 
            {
314
 
              g_value_set_double (value, store->values[column]);
315
 
            }
316
 
          else if (store->resolutions[column])
317
 
            {
318
 
              g_value_set_double (value,
319
 
                                  store->values[column] *
320
 
                                  gimp_unit_get_factor (unit) /
321
 
                                  store->resolutions[column]);
322
 
            }
323
 
          break;
324
 
        }
325
 
    }
326
 
}
327
 
 
328
 
static gboolean
329
 
gimp_unit_store_iter_next (GtkTreeModel *tree_model,
330
 
                           GtkTreeIter  *iter)
331
 
{
332
 
  gint  unit  = GPOINTER_TO_INT (iter->user_data);
333
 
 
334
 
  unit++;
335
 
  if (unit > 0 && unit < gimp_unit_get_number_of_units ())
336
 
    {
337
 
      iter->user_data = GINT_TO_POINTER (unit);
338
 
      return TRUE;
339
 
    }
340
 
 
341
 
  return FALSE;
342
 
}
343
 
 
344
 
static gboolean
345
 
gimp_unit_store_iter_children (GtkTreeModel *tree_model,
346
 
                               GtkTreeIter  *iter,
347
 
                               GtkTreeIter  *parent)
348
 
{
349
 
  /* this is a list, nodes have no children */
350
 
  if (parent)
351
 
    return FALSE;
352
 
 
353
 
  iter->user_data = GINT_TO_POINTER (0);
354
 
 
355
 
  return TRUE;
356
 
}
357
 
 
358
 
static gboolean
359
 
gimp_unit_store_iter_has_child (GtkTreeModel *tree_model,
360
 
                                GtkTreeIter  *iter)
361
 
{
362
 
  return FALSE;
363
 
}
364
 
 
365
 
static gint
366
 
gimp_unit_store_iter_n_children (GtkTreeModel *tree_model,
367
 
                                 GtkTreeIter  *iter)
368
 
{
369
 
  if (iter)
370
 
    return 0;
371
 
 
372
 
  return gimp_unit_get_number_of_units ();
373
 
}
374
 
 
375
 
static gboolean
376
 
gimp_unit_store_iter_nth_child (GtkTreeModel *tree_model,
377
 
                                GtkTreeIter  *iter,
378
 
                                GtkTreeIter  *parent,
379
 
                                gint          n)
380
 
{
381
 
  GimpUnitStore *store;
382
 
 
383
 
  if (parent)
384
 
    return FALSE;
385
 
 
386
 
  store = GIMP_UNIT_STORE (tree_model);
387
 
 
388
 
  if (n >= 0 && n < gimp_unit_get_number_of_units ())
389
 
    {
390
 
      iter->user_data = GINT_TO_POINTER (n);
391
 
      return TRUE;
392
 
    }
393
 
 
394
 
  return FALSE;
395
 
}
396
 
 
397
 
static gboolean
398
 
gimp_unit_store_iter_parent (GtkTreeModel *tree_model,
399
 
                             GtkTreeIter  *iter,
400
 
                             GtkTreeIter  *child)
401
 
{
402
 
  return FALSE;
403
 
}
404
 
 
405
 
 
406
 
GimpUnitStore *
407
 
gimp_unit_store_new (gint  num_values)
408
 
{
409
 
  return g_object_new (GIMP_TYPE_UNIT_STORE,
410
 
                       "num-values", num_values,
411
 
                       NULL);
412
 
}
413
 
 
414
 
void
415
 
gimp_unit_store_set_pixel_value (GimpUnitStore *store,
416
 
                                 gint           index,
417
 
                                 gdouble        value)
418
 
{
419
 
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
420
 
  g_return_if_fail (index > 0 && index < store->num_values);
421
 
 
422
 
  store->values[index] = value;
423
 
}
424
 
 
425
 
void
426
 
gimp_unit_store_set_pixel_values (GimpUnitStore *store,
427
 
                                  gdouble        first_value,
428
 
                                  ...)
429
 
{
430
 
  va_list  args;
431
 
  gint     i;
432
 
 
433
 
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
434
 
 
435
 
  va_start (args, first_value);
436
 
 
437
 
  for (i = 0; i < store->num_values; )
438
 
    {
439
 
      store->values[i] = first_value;
440
 
 
441
 
      if (++i < store->num_values)
442
 
        first_value = va_arg (args, gdouble);
443
 
    }
444
 
 
445
 
  va_end (args);
446
 
}
447
 
 
448
 
void
449
 
gimp_unit_store_set_resolution (GimpUnitStore *store,
450
 
                                gint           index,
451
 
                                gdouble        resolution)
452
 
{
453
 
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
454
 
  g_return_if_fail (index > 0 && index < store->num_values);
455
 
 
456
 
  store->resolutions[index] = resolution;
457
 
}
458
 
 
459
 
void
460
 
gimp_unit_store_set_resolutions  (GimpUnitStore *store,
461
 
                                  gdouble        first_resolution,
462
 
                                  ...)
463
 
{
464
 
  va_list  args;
465
 
  gint     i;
466
 
 
467
 
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
468
 
 
469
 
  va_start (args, first_resolution);
470
 
 
471
 
  for (i = 0; i < store->num_values; )
472
 
    {
473
 
      store->resolutions[i] = first_resolution;
474
 
 
475
 
      if (++i < store->num_values)
476
 
        first_resolution = va_arg (args, gdouble);
477
 
    }
478
 
 
479
 
  va_end (args);
480
 
}
481
 
 
482
 
gdouble
483
 
gimp_unit_store_get_value (GimpUnitStore *store,
484
 
                           GimpUnit       unit,
485
 
                           gint           index)
486
 
{
487
 
  GtkTreeIter  iter;
488
 
  GValue       value = { 0, };
489
 
 
490
 
  g_return_val_if_fail (GIMP_IS_UNIT_STORE (store), 0.0);
491
 
  g_return_val_if_fail (index >= 0 && index < store->num_values, 0.0);
492
 
 
493
 
  iter.user_data = GINT_TO_POINTER (unit);
494
 
 
495
 
  gimp_unit_store_tree_model_get_value (GTK_TREE_MODEL (store),
496
 
                                        &iter,
497
 
                                        GIMP_UNIT_STORE_FIRST_VALUE + index,
498
 
                                        &value);
499
 
 
500
 
  return g_value_get_double (&value);
501
 
}
502
 
 
503
 
void
504
 
gimp_unit_store_get_values (GimpUnitStore *store,
505
 
                            GimpUnit       unit,
506
 
                            gdouble       *first_value,
507
 
                            ...)
508
 
{
509
 
  va_list  args;
510
 
  gint     i;
511
 
 
512
 
  g_return_if_fail (GIMP_IS_UNIT_STORE (store));
513
 
 
514
 
  va_start (args, first_value);
515
 
 
516
 
  for (i = 0; i < store->num_values; )
517
 
    {
518
 
      if (first_value)
519
 
        *first_value = gimp_unit_store_get_value (store, unit, i);
520
 
 
521
 
      if (++i < store->num_values)
522
 
        first_value = va_arg (args, gdouble *);
523
 
    }
524
 
 
525
 
  va_end (args);
526
 
}