~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/text/gimptextundo.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <glib-object.h>
 
22
 
 
23
#include "text-types.h"
 
24
 
 
25
#include "config/gimpconfig.h"
 
26
#include "config/gimpconfig-utils.h"
 
27
 
 
28
#include "core/gimpitem.h"
 
29
#include "core/gimpitemundo.h"
 
30
#include "core/gimp-utils.h"
 
31
 
 
32
#include "gimptext.h"
 
33
#include "gimptextlayer.h"
 
34
#include "gimptextundo.h"
 
35
 
 
36
 
 
37
enum
 
38
{
 
39
  PROP_0,
 
40
  PROP_PARAM
 
41
};
 
42
 
 
43
 
 
44
static void      gimp_text_undo_class_init   (GimpTextUndoClass     *klass);
 
45
 
 
46
static GObject * gimp_text_undo_constructor  (GType                  type,
 
47
                                              guint                  n_params,
 
48
                                              GObjectConstructParam *params);
 
49
static void      gimp_text_undo_set_property (GObject               *object,
 
50
                                              guint                  property_id,
 
51
                                              const GValue          *value,
 
52
                                              GParamSpec            *pspec);
 
53
static void      gimp_text_undo_get_property (GObject               *object,
 
54
                                              guint                  property_id,
 
55
                                              GValue                *value,
 
56
                                              GParamSpec            *pspec);
 
57
 
 
58
static gint64    gimp_text_undo_get_memsize  (GimpObject            *object,
 
59
                                              gint64                *gui_size);
 
60
 
 
61
static void      gimp_text_undo_pop          (GimpUndo              *undo,
 
62
                                              GimpUndoMode           undo_mode,
 
63
                                              GimpUndoAccumulator   *accum);
 
64
static void      gimp_text_undo_free         (GimpUndo              *undo,
 
65
                                              GimpUndoMode           undo_mode);
 
66
 
 
67
 
 
68
static GimpUndoClass *parent_class = NULL;
 
69
 
 
70
 
 
71
GType
 
72
gimp_text_undo_get_type (void)
 
73
{
 
74
  static GType undo_type = 0;
 
75
 
 
76
  if (! undo_type)
 
77
    {
 
78
      static const GTypeInfo undo_info =
 
79
      {
 
80
        sizeof (GimpTextUndoClass),
 
81
        (GBaseInitFunc) NULL,
 
82
        (GBaseFinalizeFunc) NULL,
 
83
        (GClassInitFunc) gimp_text_undo_class_init,
 
84
        NULL,           /* class_finalize */
 
85
        NULL,           /* class_data     */
 
86
        sizeof (GimpTextUndo),
 
87
        0,              /* n_preallocs    */
 
88
        NULL            /* instance_init  */
 
89
      };
 
90
 
 
91
      undo_type = g_type_register_static (GIMP_TYPE_ITEM_UNDO, "GimpTextUndo",
 
92
                                          &undo_info, 0);
 
93
  }
 
94
 
 
95
  return undo_type;
 
96
}
 
97
 
 
98
static void
 
99
gimp_text_undo_class_init (GimpTextUndoClass *klass)
 
100
{
 
101
  GObjectClass    *object_class      = G_OBJECT_CLASS (klass);
 
102
  GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
 
103
  GimpUndoClass   *undo_class        = GIMP_UNDO_CLASS (klass);
 
104
 
 
105
  parent_class = g_type_class_peek_parent (klass);
 
106
 
 
107
  object_class->constructor      = gimp_text_undo_constructor;
 
108
  object_class->set_property     = gimp_text_undo_set_property;
 
109
  object_class->get_property     = gimp_text_undo_get_property;
 
110
 
 
111
  gimp_object_class->get_memsize = gimp_text_undo_get_memsize;
 
112
 
 
113
  undo_class->pop                = gimp_text_undo_pop;
 
114
  undo_class->free               = gimp_text_undo_free;
 
115
 
 
116
  g_object_class_install_property (object_class, PROP_PARAM,
 
117
                                   g_param_spec_param ("param", NULL, NULL,
 
118
                                                       G_TYPE_PARAM,
 
119
                                                       G_PARAM_READWRITE |
 
120
                                                       G_PARAM_CONSTRUCT_ONLY));
 
121
}
 
122
 
 
123
static GObject *
 
124
gimp_text_undo_constructor (GType                  type,
 
125
                            guint                  n_params,
 
126
                            GObjectConstructParam *params)
 
127
{
 
128
  GObject       *object;
 
129
  GimpTextUndo  *text_undo;
 
130
  GimpTextLayer *layer;
 
131
 
 
132
  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
 
133
 
 
134
  text_undo = GIMP_TEXT_UNDO (object);
 
135
 
 
136
  g_assert (GIMP_IS_TEXT_LAYER (GIMP_ITEM_UNDO (text_undo)->item));
 
137
 
 
138
  layer = GIMP_TEXT_LAYER (GIMP_ITEM_UNDO (text_undo)->item);
 
139
 
 
140
  if (text_undo->pspec)
 
141
    {
 
142
      g_assert (text_undo->pspec->owner_type == GIMP_TYPE_TEXT);
 
143
 
 
144
      text_undo->value = g_new0 (GValue, 1);
 
145
 
 
146
      g_value_init (text_undo->value, text_undo->pspec->value_type);
 
147
      g_object_get_property (G_OBJECT (layer->text),
 
148
                             text_undo->pspec->name, text_undo->value);
 
149
    }
 
150
  else if (layer->text)
 
151
    {
 
152
      text_undo->text = gimp_config_duplicate (GIMP_CONFIG (layer->text));
 
153
    }
 
154
 
 
155
  return object;
 
156
}
 
157
 
 
158
static void
 
159
gimp_text_undo_set_property (GObject      *object,
 
160
                             guint         property_id,
 
161
                             const GValue *value,
 
162
                             GParamSpec   *pspec)
 
163
{
 
164
  GimpTextUndo *text_undo = GIMP_TEXT_UNDO (object);
 
165
 
 
166
  switch (property_id)
 
167
    {
 
168
    case PROP_PARAM:
 
169
      text_undo->pspec = g_value_get_param (value);
 
170
      break;
 
171
    default:
 
172
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
173
      break;
 
174
    }
 
175
}
 
176
 
 
177
static void
 
178
gimp_text_undo_get_property (GObject    *object,
 
179
                             guint       property_id,
 
180
                             GValue     *value,
 
181
                             GParamSpec *pspec)
 
182
{
 
183
  GimpTextUndo *text_undo = GIMP_TEXT_UNDO (object);
 
184
 
 
185
  switch (property_id)
 
186
    {
 
187
    case PROP_PARAM:
 
188
      g_value_set_param (value, (GParamSpec *) text_undo->pspec);
 
189
      break;
 
190
    default:
 
191
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
192
      break;
 
193
    }
 
194
}
 
195
 
 
196
static gint64
 
197
gimp_text_undo_get_memsize (GimpObject *object,
 
198
                            gint64     *gui_size)
 
199
{
 
200
  GimpTextUndo *undo    = GIMP_TEXT_UNDO (object);
 
201
  gint64        memsize = 0;
 
202
 
 
203
  if (undo->value)
 
204
    memsize += gimp_g_value_get_memsize (undo->value);
 
205
 
 
206
  if (undo->text)
 
207
    memsize += gimp_object_get_memsize (GIMP_OBJECT (undo->text), NULL);
 
208
 
 
209
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
 
210
                                                                  gui_size);
 
211
}
 
212
 
 
213
static void
 
214
gimp_text_undo_pop (GimpUndo            *undo,
 
215
                    GimpUndoMode         undo_mode,
 
216
                    GimpUndoAccumulator *accum)
 
217
{
 
218
  GimpTextUndo  *text_undo = GIMP_TEXT_UNDO (undo);
 
219
  GimpTextLayer *layer     = GIMP_TEXT_LAYER (GIMP_ITEM_UNDO (undo)->item);
 
220
 
 
221
  if (text_undo->pspec)
 
222
    {
 
223
      GValue *value;
 
224
 
 
225
      g_return_if_fail (layer->text != NULL);
 
226
 
 
227
      value = g_new0 (GValue, 1);
 
228
      g_value_init (value, text_undo->pspec->value_type);
 
229
 
 
230
      g_object_get_property (G_OBJECT (layer->text),
 
231
                             text_undo->pspec->name, value);
 
232
 
 
233
      g_object_set_property (G_OBJECT (layer->text),
 
234
                             text_undo->pspec->name, text_undo->value);
 
235
 
 
236
      g_value_unset (text_undo->value);
 
237
      g_free (text_undo->value);
 
238
 
 
239
      text_undo->value = value;
 
240
    }
 
241
  else
 
242
    {
 
243
      GimpText *text;
 
244
 
 
245
      text = (layer->text ?
 
246
              gimp_config_duplicate (GIMP_CONFIG (layer->text)) : NULL);
 
247
 
 
248
      if (layer->text && text_undo->text)
 
249
        gimp_config_sync (GIMP_CONFIG (text_undo->text),
 
250
                          GIMP_CONFIG (layer->text), 0);
 
251
      else
 
252
        gimp_text_layer_set_text (layer, text_undo->text);
 
253
 
 
254
      if (text_undo->text)
 
255
        g_object_unref (text_undo->text);
 
256
 
 
257
      text_undo->text = text;
 
258
    }
 
259
 
 
260
  GIMP_UNDO_CLASS (parent_class)->pop (undo, undo_mode, accum);
 
261
}
 
262
 
 
263
static void
 
264
gimp_text_undo_free (GimpUndo     *undo,
 
265
                     GimpUndoMode  undo_mode)
 
266
{
 
267
  GimpTextUndo *text_undo = GIMP_TEXT_UNDO (undo);
 
268
 
 
269
  if (text_undo->text)
 
270
    {
 
271
      g_object_unref (text_undo->text);
 
272
      text_undo->text = NULL;
 
273
    }
 
274
 
 
275
  if (text_undo->pspec)
 
276
    {
 
277
      g_value_unset (text_undo->value);
 
278
      g_free (text_undo->value);
 
279
 
 
280
      text_undo->value = NULL;
 
281
      text_undo->pspec = NULL;
 
282
    }
 
283
 
 
284
  GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 
285
}