1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 2004 Sven Neumann <sven@gimp.org>
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.
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.
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.
26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpwidgets/gimpwidgets.h"
29
#include "widgets-types.h"
31
#include "gimpunitstore.h"
41
static void gimp_unit_store_tree_model_init (GtkTreeModelIface *iface);
43
static void gimp_unit_store_finalize (GObject *object);
44
static void gimp_unit_store_set_property (GObject *object,
48
static void gimp_unit_store_get_property (GObject *object,
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,
57
static gboolean gimp_unit_store_get_iter (GtkTreeModel *tree_model,
60
static GtkTreePath *gimp_unit_store_get_path (GtkTreeModel *tree_model,
62
static void gimp_unit_store_tree_model_get_value (GtkTreeModel *tree_model,
66
static gboolean gimp_unit_store_iter_next (GtkTreeModel *tree_model,
68
static gboolean gimp_unit_store_iter_children (GtkTreeModel *tree_model,
71
static gboolean gimp_unit_store_iter_has_child (GtkTreeModel *tree_model,
73
static gint gimp_unit_store_iter_n_children (GtkTreeModel *tree_model,
75
static gboolean gimp_unit_store_iter_nth_child (GtkTreeModel *tree_model,
79
static gboolean gimp_unit_store_iter_parent (GtkTreeModel *tree_model,
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))
88
#define parent_class gimp_unit_store_parent_class
91
static GType column_types[GIMP_UNIT_STORE_UNIT_COLUMNS] =
105
gimp_unit_store_class_init (GimpUnitStoreClass *klass)
107
GObjectClass *object_class = G_OBJECT_CLASS (klass);
109
column_types[GIMP_UNIT_STORE_UNIT] = GIMP_TYPE_UNIT;
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;
115
g_object_class_install_property (object_class, PROP_NUM_VALUES,
116
g_param_spec_int ("num-values",
119
GIMP_PARAM_READWRITE |
120
G_PARAM_CONSTRUCT_ONLY));
124
gimp_unit_store_init (GimpUnitStore *store)
129
gimp_unit_store_tree_model_init (GtkTreeModelIface *iface)
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;
146
gimp_unit_store_finalize (GObject *object)
148
GimpUnitStore *store = GIMP_UNIT_STORE (object);
150
if (store->num_values > 0)
152
g_free (store->values);
153
g_free (store->resolutions);
154
store->num_values = 0;
157
G_OBJECT_CLASS (parent_class)->finalize (object);
161
gimp_unit_store_set_property (GObject *object,
166
GimpUnitStore *store = GIMP_UNIT_STORE (object);
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)
175
store->values = g_new0 (gdouble, store->num_values);
176
store->resolutions = g_new0 (gdouble, store->num_values);
181
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
187
gimp_unit_store_get_property (GObject *object,
192
GimpUnitStore *store = GIMP_UNIT_STORE (object);
196
case PROP_NUM_VALUES:
197
g_value_set_int (value, store->num_values);
200
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
205
static GtkTreeModelFlags
206
gimp_unit_store_get_flags (GtkTreeModel *tree_model)
208
return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
212
gimp_unit_store_get_n_columns (GtkTreeModel *tree_model)
214
GimpUnitStore *store = GIMP_UNIT_STORE (tree_model);
216
return GIMP_UNIT_STORE_UNIT_COLUMNS + store->num_values;
220
gimp_unit_store_get_column_type (GtkTreeModel *tree_model,
223
g_return_val_if_fail (index >= 0, G_TYPE_INVALID);
225
if (index < GIMP_UNIT_STORE_UNIT_COLUMNS)
226
return column_types[index];
228
return G_TYPE_DOUBLE;
232
gimp_unit_store_get_iter (GtkTreeModel *tree_model,
238
g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
240
unit = gtk_tree_path_get_indices (path)[0];
242
if (unit >= 0 && unit < gimp_unit_get_number_of_units ())
244
iter->user_data = GINT_TO_POINTER (unit);
252
gimp_unit_store_get_path (GtkTreeModel *tree_model,
255
GtkTreePath *path = gtk_tree_path_new ();
257
gtk_tree_path_append_index (path, GPOINTER_TO_INT (iter->user_data));
263
gimp_unit_store_tree_model_get_value (GtkTreeModel *tree_model,
268
GimpUnitStore *store = GIMP_UNIT_STORE (tree_model);
271
g_return_if_fail (column >= 0 &&
272
column < GIMP_UNIT_STORE_UNIT_COLUMNS + store->num_values);
275
column < GIMP_UNIT_STORE_UNIT_COLUMNS ?
276
column_types[column] :
279
unit = GPOINTER_TO_INT (iter->user_data);
281
if (unit >= 0 && unit < gimp_unit_get_number_of_units ())
285
case GIMP_UNIT_STORE_UNIT:
286
g_value_set_int (value, unit);
288
case GIMP_UNIT_STORE_UNIT_FACTOR:
289
g_value_set_double (value, gimp_unit_get_factor (unit));
291
case GIMP_UNIT_STORE_UNIT_DIGITS:
292
g_value_set_int (value, gimp_unit_get_digits (unit));
294
case GIMP_UNIT_STORE_UNIT_IDENTIFIER:
295
g_value_set_static_string (value, gimp_unit_get_identifier (unit));
297
case GIMP_UNIT_STORE_UNIT_SYMBOL:
298
g_value_set_static_string (value, gimp_unit_get_symbol (unit));
300
case GIMP_UNIT_STORE_UNIT_ABBREVIATION:
301
g_value_set_static_string (value, gimp_unit_get_abbreviation (unit));
303
case GIMP_UNIT_STORE_UNIT_SINGULAR:
304
g_value_set_static_string (value, gimp_unit_get_singular (unit));
306
case GIMP_UNIT_STORE_UNIT_PLURAL:
307
g_value_set_static_string (value, gimp_unit_get_plural (unit));
311
column -= GIMP_UNIT_STORE_UNIT_COLUMNS;
312
if (unit == GIMP_UNIT_PIXEL)
314
g_value_set_double (value, store->values[column]);
316
else if (store->resolutions[column])
318
g_value_set_double (value,
319
store->values[column] *
320
gimp_unit_get_factor (unit) /
321
store->resolutions[column]);
329
gimp_unit_store_iter_next (GtkTreeModel *tree_model,
332
gint unit = GPOINTER_TO_INT (iter->user_data);
335
if (unit > 0 && unit < gimp_unit_get_number_of_units ())
337
iter->user_data = GINT_TO_POINTER (unit);
345
gimp_unit_store_iter_children (GtkTreeModel *tree_model,
349
/* this is a list, nodes have no children */
353
iter->user_data = GINT_TO_POINTER (0);
359
gimp_unit_store_iter_has_child (GtkTreeModel *tree_model,
366
gimp_unit_store_iter_n_children (GtkTreeModel *tree_model,
372
return gimp_unit_get_number_of_units ();
376
gimp_unit_store_iter_nth_child (GtkTreeModel *tree_model,
381
GimpUnitStore *store;
386
store = GIMP_UNIT_STORE (tree_model);
388
if (n >= 0 && n < gimp_unit_get_number_of_units ())
390
iter->user_data = GINT_TO_POINTER (n);
398
gimp_unit_store_iter_parent (GtkTreeModel *tree_model,
407
gimp_unit_store_new (gint num_values)
409
return g_object_new (GIMP_TYPE_UNIT_STORE,
410
"num-values", num_values,
415
gimp_unit_store_set_pixel_value (GimpUnitStore *store,
419
g_return_if_fail (GIMP_IS_UNIT_STORE (store));
420
g_return_if_fail (index > 0 && index < store->num_values);
422
store->values[index] = value;
426
gimp_unit_store_set_pixel_values (GimpUnitStore *store,
433
g_return_if_fail (GIMP_IS_UNIT_STORE (store));
435
va_start (args, first_value);
437
for (i = 0; i < store->num_values; )
439
store->values[i] = first_value;
441
if (++i < store->num_values)
442
first_value = va_arg (args, gdouble);
449
gimp_unit_store_set_resolution (GimpUnitStore *store,
453
g_return_if_fail (GIMP_IS_UNIT_STORE (store));
454
g_return_if_fail (index > 0 && index < store->num_values);
456
store->resolutions[index] = resolution;
460
gimp_unit_store_set_resolutions (GimpUnitStore *store,
461
gdouble first_resolution,
467
g_return_if_fail (GIMP_IS_UNIT_STORE (store));
469
va_start (args, first_resolution);
471
for (i = 0; i < store->num_values; )
473
store->resolutions[i] = first_resolution;
475
if (++i < store->num_values)
476
first_resolution = va_arg (args, gdouble);
483
gimp_unit_store_get_value (GimpUnitStore *store,
488
GValue value = { 0, };
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);
493
iter.user_data = GINT_TO_POINTER (unit);
495
gimp_unit_store_tree_model_get_value (GTK_TREE_MODEL (store),
497
GIMP_UNIT_STORE_FIRST_VALUE + index,
500
return g_value_get_double (&value);
504
gimp_unit_store_get_values (GimpUnitStore *store,
506
gdouble *first_value,
512
g_return_if_fail (GIMP_IS_UNIT_STORE (store));
514
va_start (args, first_value);
516
for (i = 0; i < store->num_values; )
519
*first_value = gimp_unit_store_get_value (store, unit, i);
521
if (++i < store->num_values)
522
first_value = va_arg (args, gdouble *);