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