1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include <glib-object.h>
25
#include "libgimpbase/gimpbase.h"
27
#include "core-types.h"
29
#include "gimp-utils.h"
30
#include "gimpmarshal.h"
31
#include "gimpobject.h"
48
static void gimp_object_class_init (GimpObjectClass *klass);
49
static void gimp_object_init (GimpObject *object);
51
static void gimp_object_dispose (GObject *object);
52
static void gimp_object_finalize (GObject *object);
53
static void gimp_object_set_property (GObject *object,
57
static void gimp_object_get_property (GObject *object,
61
static gint64 gimp_object_real_get_memsize (GimpObject *object,
63
static void gimp_object_name_normalize (GimpObject *object);
66
static guint object_signals[LAST_SIGNAL] = { 0 };
68
static GObjectClass *parent_class = NULL;
72
gimp_object_get_type (void)
74
static GType object_type = 0;
78
static const GTypeInfo object_info =
80
sizeof (GimpObjectClass),
82
(GBaseFinalizeFunc) NULL,
83
(GClassInitFunc) gimp_object_class_init,
84
NULL, /* class_finalize */
85
NULL, /* class_data */
88
(GInstanceInitFunc) gimp_object_init,
91
object_type = g_type_register_static (G_TYPE_OBJECT,
100
gimp_object_class_init (GimpObjectClass *klass)
102
GObjectClass *object_class;
104
object_class = G_OBJECT_CLASS (klass);
106
parent_class = g_type_class_peek_parent (klass);
108
object_signals[DISCONNECT] =
109
g_signal_new ("disconnect",
110
G_TYPE_FROM_CLASS (klass),
112
G_STRUCT_OFFSET (GimpObjectClass, disconnect),
114
gimp_marshal_VOID__VOID,
117
object_signals[NAME_CHANGED] =
118
g_signal_new ("name_changed",
119
G_TYPE_FROM_CLASS (klass),
121
G_STRUCT_OFFSET (GimpObjectClass, name_changed),
123
gimp_marshal_VOID__VOID,
126
object_class->dispose = gimp_object_dispose;
127
object_class->finalize = gimp_object_finalize;
128
object_class->set_property = gimp_object_set_property;
129
object_class->get_property = gimp_object_get_property;
131
klass->disconnect = NULL;
132
klass->name_changed = NULL;
133
klass->get_memsize = gimp_object_real_get_memsize;
135
g_object_class_install_property (object_class,
137
g_param_spec_string ("name",
144
gimp_object_init (GimpObject *object)
147
object->normalized = NULL;
151
gimp_object_dispose (GObject *object)
153
gboolean disconnected;
155
disconnected = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (object),
160
g_signal_emit (object, object_signals[DISCONNECT], 0);
162
g_object_set_data (G_OBJECT (object), "disconnected",
163
GINT_TO_POINTER (TRUE));
166
G_OBJECT_CLASS (parent_class)->dispose (object);
170
gimp_object_finalize (GObject *object)
172
gimp_object_name_free (GIMP_OBJECT (object));
174
G_OBJECT_CLASS (parent_class)->finalize (object);
178
gimp_object_set_property (GObject *object,
183
GimpObject *gimp_object = GIMP_OBJECT (object);
188
gimp_object_set_name (gimp_object, g_value_get_string (value));
191
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
197
gimp_object_get_property (GObject *object,
202
GimpObject *gimp_object = GIMP_OBJECT (object);
207
g_value_set_string (value, gimp_object->name);
210
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
216
* gimp_object_set_name:
217
* @object: a #GimpObject
218
* @name: the @object's new name
220
* Sets the @object's name. Takes care of freeing the old name and
221
* emitting the "name_changed" signal if the old and new name differ.
224
gimp_object_set_name (GimpObject *object,
227
g_return_if_fail (GIMP_IS_OBJECT (object));
229
if ((!object->name && !name) ||
230
(object->name && name && !strcmp (object->name, name)))
233
gimp_object_name_free (object);
235
object->name = g_strdup (name);
237
gimp_object_name_changed (object);
241
* gimp_object_set_name_safe:
242
* @object: a #GimpObject
243
* @name: the @object's new name
245
* A safe version of gimp_object_set_name() that takes care of
246
* handling newlines and overly long names. The actual name set
247
* may be different to the @name you pass.
250
gimp_object_set_name_safe (GimpObject *object,
253
g_return_if_fail (GIMP_IS_OBJECT (object));
255
if ((!object->name && !name) ||
256
(object->name && name && !strcmp (object->name, name)))
259
gimp_object_name_free (object);
261
object->name = gimp_utf8_strtrim (name, 30);
263
gimp_object_name_changed (object);
267
* gimp_object_get_name:
268
* @object: a #GimpObject
270
* This function gives access to the name of a GimpObject. The
271
* returned name belongs to the object and must not be freed.
273
* Return value: a pointer to the @object's name
276
gimp_object_get_name (const GimpObject *object)
278
g_return_val_if_fail (GIMP_IS_OBJECT (object), NULL);
284
* gimp_object_name_changed:
285
* @object: a #GimpObject
287
* Causes the "name_changed" signal to be emitted.
290
gimp_object_name_changed (GimpObject *object)
292
g_return_if_fail (GIMP_IS_OBJECT (object));
294
g_signal_emit (object, object_signals[NAME_CHANGED], 0);
298
* gimp_object_name_free:
299
* @object: a #GimpObject
301
* Frees the name of @object and sets the name pointer to %NULL. Also
302
* takes care of the normalized name that the object might be caching.
304
* In general you should be using gimp_object_set_name() instead. But
305
* if you ever need to free the object name but don't want the
306
* "name_changed" signal to be emitted, then use this function. Never
307
* ever free the object name directly!
310
gimp_object_name_free (GimpObject *object)
312
if (object->normalized)
314
if (object->normalized != object->name)
315
g_free (object->normalized);
317
object->normalized = NULL;
322
g_free (object->name);
328
* gimp_object_name_collate:
329
* @object1: a #GimpObject
330
* @object2: another #GimpObject
332
* Compares two object names for ordering using the linguistically
333
* correct rules for the current locale. It caches the normalized
334
* version of the object name to speed up subsequent calls.
336
* Return value: -1 if object1 compares before object2,
337
* 0 if they compare equal,
338
* 1 if object1 compares after object2.
341
gimp_object_name_collate (GimpObject *object1,
344
if (! object1->normalized)
345
gimp_object_name_normalize (object1);
347
if (! object2->normalized)
348
gimp_object_name_normalize (object2);
350
return strcmp (object1->normalized, object2->normalized);
354
gimp_object_name_normalize (GimpObject *object)
356
g_return_if_fail (object->normalized == NULL);
360
gchar *key = g_utf8_collate_key (object->name, -1);
362
if (strcmp (key, object->name))
364
object->normalized = key;
369
object->normalized = object->name;
375
#define DEBUG_MEMSIZE 1
378
gboolean gimp_debug_memsize = FALSE;
382
gimp_object_get_memsize (GimpObject *object,
386
gint64 my_gui_size = 0;
388
g_return_val_if_fail (GIMP_IS_OBJECT (object), 0);
391
if (gimp_debug_memsize)
393
static gint indent_level = 0;
394
static GList *aggregation_tree = NULL;
395
static gchar indent_buf[256];
398
gint64 gui_memsize = 0;
400
gint my_indent_level;
405
my_indent_level = indent_level;
407
memsize = GIMP_OBJECT_GET_CLASS (object)->get_memsize (object,
412
for (i = 0; i < MIN (my_indent_level * 2, sizeof (indent_buf) - 1); i++)
415
indent_buf[i] = '\0';
417
object_size = g_strdup_printf ("%s%s \"%s\": "
419
"(%" G_GINT64_FORMAT ")\n",
421
g_type_name (G_TYPE_FROM_INSTANCE (object)),
426
aggregation_tree = g_list_prepend (aggregation_tree, object_size);
428
if (indent_level == 0)
430
g_list_foreach (aggregation_tree, (GFunc) g_print, NULL);
431
g_list_foreach (aggregation_tree, (GFunc) g_free, NULL);
432
g_list_free (aggregation_tree);
434
aggregation_tree = NULL;
439
#endif /* DEBUG_MEMSIZE */
441
my_size = GIMP_OBJECT_GET_CLASS (object)->get_memsize (object,
445
*gui_size = my_gui_size;
451
gimp_object_real_get_memsize (GimpObject *object,
457
memsize += strlen (object->name) + 1;
459
return memsize + gimp_g_object_get_memsize ((GObject *) object);