1
1
/* GIMP - The GNU Image Manipulation Program
2
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
4
* This program is free software: you can redistribute it and/or modify
5
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
6
* the Free Software Foundation; either version 3 of the License, or
7
7
* (at your option) any later version.
9
9
* This program is distributed in the hope that it will be useful,
12
12
* GNU General Public License for more details.
14
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.
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
18
#include "config.h"
21
#include <glib-object.h>
23
22
#include "core-types.h"
49
49
/* public functions */
52
gimp_image_undo_is_enabled (const GimpImage *image)
54
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
56
return (GIMP_IMAGE_GET_PRIVATE (image)->undo_freeze_count == 0);
60
gimp_image_undo_enable (GimpImage *image)
62
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
64
/* Free all undo steps as they are now invalidated */
65
gimp_image_undo_free (image);
67
return gimp_image_undo_thaw (image);
71
gimp_image_undo_disable (GimpImage *image)
73
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
75
return gimp_image_undo_freeze (image);
79
gimp_image_undo_freeze (GimpImage *image)
81
GimpImagePrivate *private;
83
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
85
private = GIMP_IMAGE_GET_PRIVATE (image);
87
private->undo_freeze_count++;
89
if (private->undo_freeze_count == 1)
90
gimp_image_undo_event (image, GIMP_UNDO_EVENT_UNDO_FREEZE, NULL);
96
gimp_image_undo_thaw (GimpImage *image)
98
GimpImagePrivate *private;
100
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
102
private = GIMP_IMAGE_GET_PRIVATE (image);
104
g_return_val_if_fail (private->undo_freeze_count > 0, FALSE);
106
private->undo_freeze_count--;
108
if (private->undo_freeze_count == 0)
109
gimp_image_undo_event (image, GIMP_UNDO_EVENT_UNDO_THAW, NULL);
52
115
gimp_image_undo (GimpImage *image)
117
GimpImagePrivate *private;
54
119
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
55
g_return_val_if_fail (image->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
121
private = GIMP_IMAGE_GET_PRIVATE (image);
123
g_return_val_if_fail (private->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
58
126
gimp_image_undo_pop_stack (image,
61
129
GIMP_UNDO_MODE_UNDO);
135
gimp_image_redo (GimpImage *image)
137
GimpImagePrivate *private;
139
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
141
private = GIMP_IMAGE_GET_PRIVATE (image);
143
g_return_val_if_fail (private->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
146
gimp_image_undo_pop_stack (image,
149
GIMP_UNDO_MODE_REDO);
67
155
* this function continues to undo as long as it only sees certain
68
156
* undo types, in particular visibility changes.
71
159
gimp_image_strong_undo (GimpImage *image)
161
GimpImagePrivate *private;
75
164
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
76
g_return_val_if_fail (image->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
166
private = GIMP_IMAGE_GET_PRIVATE (image);
168
g_return_val_if_fail (private->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
79
undo = gimp_undo_stack_peek (image->undo_stack);
171
undo = gimp_undo_stack_peek (private->undo_stack);
81
173
gimp_image_undo (image);
83
175
while (gimp_undo_is_weak (undo))
85
undo = gimp_undo_stack_peek (image->undo_stack);
177
undo = gimp_undo_stack_peek (private->undo_stack);
86
178
if (gimp_undo_is_weak (undo))
87
179
gimp_image_undo (image);
94
gimp_image_redo (GimpImage *image)
96
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
97
g_return_val_if_fail (image->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
100
gimp_image_undo_pop_stack (image,
103
GIMP_UNDO_MODE_REDO);
109
186
* this function continues to redo as long as it only sees certain
110
187
* undo types, in particular visibility changes. Note that the
115
192
gimp_image_strong_redo (GimpImage *image)
194
GimpImagePrivate *private;
119
197
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
120
g_return_val_if_fail (image->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
199
private = GIMP_IMAGE_GET_PRIVATE (image);
201
g_return_val_if_fail (private->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
123
undo = gimp_undo_stack_peek (image->redo_stack);
204
undo = gimp_undo_stack_peek (private->redo_stack);
125
206
gimp_image_redo (image);
127
208
while (gimp_undo_is_weak (undo))
129
undo = gimp_undo_stack_peek (image->redo_stack);
210
undo = gimp_undo_stack_peek (private->redo_stack);
130
211
if (gimp_undo_is_weak (undo))
131
212
gimp_image_redo (image);
219
gimp_image_get_undo_stack (const GimpImage *image)
221
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
223
return GIMP_IMAGE_GET_PRIVATE (image)->undo_stack;
227
gimp_image_get_redo_stack (const GimpImage *image)
229
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
231
return GIMP_IMAGE_GET_PRIVATE (image)->redo_stack;
138
235
gimp_image_undo_free (GimpImage *image)
237
GimpImagePrivate *private;
140
239
g_return_if_fail (GIMP_IS_IMAGE (image));
241
private = GIMP_IMAGE_GET_PRIVATE (image);
142
243
/* Emit the UNDO_FREE event before actually freeing everything
143
244
* so the views can properly detach from the undo items
145
246
gimp_image_undo_event (image, GIMP_UNDO_EVENT_UNDO_FREE, NULL);
147
gimp_undo_free (GIMP_UNDO (image->undo_stack), GIMP_UNDO_MODE_UNDO);
148
gimp_undo_free (GIMP_UNDO (image->redo_stack), GIMP_UNDO_MODE_REDO);
248
gimp_undo_free (GIMP_UNDO (private->undo_stack), GIMP_UNDO_MODE_UNDO);
249
gimp_undo_free (GIMP_UNDO (private->redo_stack), GIMP_UNDO_MODE_REDO);
150
251
/* If the image was dirty, but could become clean by redo-ing
151
252
* some actions, then it should now become 'infinitely' dirty.
152
253
* This is because we've just nuked the actions that would allow
153
254
* the image to become clean again.
155
if (image->dirty < 0)
156
image->dirty = 100000;
256
if (private->dirty < 0)
257
private->dirty = 100000;
158
259
/* The same applies to the case where the image would become clean
159
260
* due to undo actions, but since user can't undo without an undo
266
gimp_image_get_undo_group_count (const GimpImage *image)
268
g_return_val_if_fail (GIMP_IS_IMAGE (image), 0);
270
return GIMP_IMAGE_GET_PRIVATE (image)->group_count;
165
274
gimp_image_undo_group_start (GimpImage *image,
166
275
GimpUndoType undo_type,
167
276
const gchar *name)
169
GimpUndoStack *undo_group;
170
GimpDirtyMask dirty_mask;
278
GimpImagePrivate *private;
279
GimpUndoStack *undo_group;
280
GimpDirtyMask dirty_mask;
172
282
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
173
283
g_return_val_if_fail (undo_type > GIMP_UNDO_GROUP_FIRST &&
174
284
undo_type <= GIMP_UNDO_GROUP_LAST, FALSE);
286
private = GIMP_IMAGE_GET_PRIVATE (image);
177
289
name = gimp_undo_type_to_name (undo_type);
179
291
dirty_mask = gimp_image_undo_dirty_from_type (undo_type);
181
293
/* Notify listeners that the image will be modified */
182
if (image->group_count == 0 && dirty_mask != GIMP_DIRTY_NONE)
294
if (private->group_count == 0 && dirty_mask != GIMP_DIRTY_NONE)
183
295
gimp_image_dirty (image, dirty_mask);
185
if (image->undo_freeze_count > 0)
297
if (private->undo_freeze_count > 0)
188
image->group_count++;
300
private->group_count++;
190
302
/* If we're already in a group...ignore */
191
if (image->group_count > 1)
303
if (private->group_count > 1)
194
306
/* nuke the redo stack */
200
312
GIMP_UNDO (undo_group)->undo_type = undo_type;
201
313
GIMP_UNDO (undo_group)->dirty_mask = dirty_mask;
203
gimp_undo_stack_push_undo (image->undo_stack, GIMP_UNDO (undo_group));
315
gimp_undo_stack_push_undo (private->undo_stack, GIMP_UNDO (undo_group));
205
image->pushing_undo_group = undo_type;
317
private->pushing_undo_group = undo_type;
211
323
gimp_image_undo_group_end (GimpImage *image)
325
GimpImagePrivate *private;
213
327
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
215
if (image->undo_freeze_count > 0)
329
private = GIMP_IMAGE_GET_PRIVATE (image);
331
if (private->undo_freeze_count > 0)
218
g_return_val_if_fail (image->group_count > 0, FALSE);
220
image->group_count--;
222
if (image->group_count == 0)
334
g_return_val_if_fail (private->group_count > 0, FALSE);
336
private->group_count--;
338
if (private->group_count == 0)
224
image->pushing_undo_group = GIMP_UNDO_GROUP_NONE;
340
private->pushing_undo_group = GIMP_UNDO_GROUP_NONE;
226
342
/* Do it here, since undo_push doesn't emit this event while in
227
343
* the middle of a group
229
345
gimp_image_undo_event (image, GIMP_UNDO_EVENT_UNDO_PUSHED,
230
gimp_undo_stack_peek (image->undo_stack));
346
gimp_undo_stack_peek (private->undo_stack));
232
348
gimp_image_undo_free_space (image);
243
359
GimpDirtyMask dirty_mask,
246
GParameter *params = NULL;
362
GimpImagePrivate *private;
363
GParameter *params = NULL;
251
368
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
252
369
g_return_val_if_fail (g_type_is_a (object_type, GIMP_TYPE_UNDO), NULL);
253
370
g_return_val_if_fail (undo_type > GIMP_UNDO_GROUP_LAST, NULL);
372
private = GIMP_IMAGE_GET_PRIVATE (image);
255
374
/* Does this undo dirty the image? If so, we always want to mark
256
375
* image dirty, even if we can't actually push the undo.
258
377
if (dirty_mask != GIMP_DIRTY_NONE)
259
378
gimp_image_dirty (image, dirty_mask);
261
if (image->undo_freeze_count > 0)
380
if (private->undo_freeze_count > 0)
282
401
/* nuke the redo stack */
283
402
gimp_image_undo_free_redo (image);
285
if (image->pushing_undo_group == GIMP_UNDO_GROUP_NONE)
404
if (private->pushing_undo_group == GIMP_UNDO_GROUP_NONE)
287
gimp_undo_stack_push_undo (image->undo_stack, undo);
406
gimp_undo_stack_push_undo (private->undo_stack, undo);
289
408
gimp_image_undo_event (image, GIMP_UNDO_EVENT_UNDO_PUSHED, undo);
291
410
gimp_image_undo_free_space (image);
293
412
/* freeing undo space may have freed the newly pushed undo */
294
if (gimp_undo_stack_peek (image->undo_stack) == undo)
413
if (gimp_undo_stack_peek (private->undo_stack) == undo)
299
418
GimpUndoStack *undo_group;
301
undo_group = GIMP_UNDO_STACK (gimp_undo_stack_peek (image->undo_stack));
420
undo_group = GIMP_UNDO_STACK (gimp_undo_stack_peek (private->undo_stack));
303
422
gimp_undo_stack_push_undo (undo_group, undo);
313
432
GType object_type,
314
433
GimpUndoType undo_type)
435
GimpImagePrivate *private;
316
437
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
318
if (image->dirty != 0 && ! gimp_undo_stack_peek (image->redo_stack))
439
private = GIMP_IMAGE_GET_PRIVATE (image);
441
if (gimp_image_is_dirty (image) &&
442
! gimp_undo_stack_peek (private->redo_stack))
320
GimpUndo *undo = gimp_undo_stack_peek (image->undo_stack);
444
GimpUndo *undo = gimp_undo_stack_peek (private->undo_stack);
322
446
if (undo && undo->undo_type == undo_type &&
323
447
g_type_is_a (G_TYPE_FROM_INSTANCE (undo), object_type))
333
457
gimp_image_undo_get_fadeable (GimpImage *image)
459
GimpImagePrivate *private;
337
462
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
339
undo = gimp_undo_stack_peek (image->undo_stack);
464
private = GIMP_IMAGE_GET_PRIVATE (image);
466
undo = gimp_undo_stack_peek (private->undo_stack);
341
468
if (GIMP_IS_UNDO_STACK (undo) && undo->undo_type == GIMP_UNDO_GROUP_PAINT)
393
520
if (accum.unit_changed)
394
521
gimp_image_unit_changed (image);
396
if (accum.quick_mask_changed)
397
gimp_image_quick_mask_changed (image);
399
if (accum.alpha_changed)
400
gimp_image_alpha_changed (image);
402
523
/* let others know that we just popped an action */
403
524
gimp_image_undo_event (image,
404
525
(undo_mode == GIMP_UNDO_MODE_UNDO) ?
413
534
gimp_image_undo_free_space (GimpImage *image)
415
GimpContainer *container;
416
gint min_undo_levels;
417
gint max_undo_levels;
536
GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
537
GimpContainer *container;
538
gint min_undo_levels;
539
gint max_undo_levels;
420
container = image->undo_stack->undos;
542
container = private->undo_stack->undos;
422
544
min_undo_levels = image->gimp->config->levels_of_undo;
423
545
max_undo_levels = 1024; /* FIXME */
426
548
#ifdef DEBUG_IMAGE_UNDO
427
549
g_printerr ("undo_steps: %d undo_bytes: %ld\n",
428
gimp_container_num_children (container),
550
gimp_container_get_n_children (container),
429
551
(glong) gimp_object_get_memsize (GIMP_OBJECT (container), NULL));
432
554
/* keep at least min_undo_levels undo steps */
433
if (gimp_container_num_children (container) <= min_undo_levels)
555
if (gimp_container_get_n_children (container) <= min_undo_levels)
436
558
while ((gimp_object_get_memsize (GIMP_OBJECT (container), NULL) > undo_size) ||
437
(gimp_container_num_children (container) > max_undo_levels))
559
(gimp_container_get_n_children (container) > max_undo_levels))
439
GimpUndo *freed = gimp_undo_stack_free_bottom (image->undo_stack,
561
GimpUndo *freed = gimp_undo_stack_free_bottom (private->undo_stack,
440
562
GIMP_UNDO_MODE_UNDO);
442
564
#ifdef DEBUG_IMAGE_UNDO
443
565
g_printerr ("freed one step: undo_steps: %d undo_bytes: %ld\n",
444
gimp_container_num_children (container),
566
gimp_container_get_n_children (container),
445
567
(glong) gimp_object_get_memsize (GIMP_OBJECT (container),
459
581
gimp_image_undo_free_redo (GimpImage *image)
461
GimpContainer *container = image->redo_stack->undos;
583
GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
584
GimpContainer *container = private->redo_stack->undos;
463
586
#ifdef DEBUG_IMAGE_UNDO
464
587
g_printerr ("redo_steps: %d redo_bytes: %ld\n",
465
gimp_container_num_children (container),
588
gimp_container_get_n_children (container),
466
589
(glong) gimp_object_get_memsize (GIMP_OBJECT (container), NULL));
469
592
if (gimp_container_is_empty (container))
472
while (gimp_container_num_children (container) > 0)
595
while (gimp_container_get_n_children (container) > 0)
474
GimpUndo *freed = gimp_undo_stack_free_bottom (image->redo_stack,
597
GimpUndo *freed = gimp_undo_stack_free_bottom (private->redo_stack,
475
598
GIMP_UNDO_MODE_REDO);
477
600
#ifdef DEBUG_IMAGE_UNDO
478
601
g_printerr ("freed one step: redo_steps: %d redo_bytes: %ld\n",
479
gimp_container_num_children (container),
602
gimp_container_get_n_children (container),
480
603
(glong )gimp_object_get_memsize (GIMP_OBJECT (container),
489
612
/* We need to use <= here because the undo counter has already been
490
613
* incremented at this point.
492
if (image->dirty <= 0)
615
if (private->dirty <= 0)
494
617
/* If the image was dirty, but could become clean by redo-ing
495
618
* some actions, then it should now become 'infinitely' dirty.
496
619
* This is because we've just nuked the actions that would allow
497
620
* the image to become clean again.
499
image->dirty = 100000;
622
private->dirty = 100000;