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
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 <glib-object.h>
#include "core-types.h"
#include "config/gimpcoreconfig.h"
#include "gimp.h"
#include "gimp-utils.h"
#include "gimpimage.h"
#include "gimpimage-undo.h"
#include "gimpitem.h"
#include "gimpitemundo.h"
#include "gimplist.h"
#include "gimpundostack.h"
/* local function prototypes */
static void gimp_image_undo_pop_stack (GimpImage *gimage,
GimpUndoStack *undo_stack,
GimpUndoStack *redo_stack,
GimpUndoMode undo_mode);
static void gimp_image_undo_free_space (GimpImage *gimage);
static void gimp_image_undo_free_redo (GimpImage *gimage);
static GimpDirtyMask gimp_image_undo_dirty_from_type (GimpUndoType undo_type);
/* public functions */
gboolean
gimp_image_undo (GimpImage *gimage)
{
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
g_return_val_if_fail (gimage->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
FALSE);
gimp_image_undo_pop_stack (gimage,
gimage->undo_stack,
gimage->redo_stack,
GIMP_UNDO_MODE_UNDO);
return TRUE;
}
gboolean
gimp_image_redo (GimpImage *gimage)
{
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
g_return_val_if_fail (gimage->pushing_undo_group == GIMP_UNDO_GROUP_NONE,
FALSE);
gimp_image_undo_pop_stack (gimage,
gimage->redo_stack,
gimage->undo_stack,
GIMP_UNDO_MODE_REDO);
return TRUE;
}
void
gimp_image_undo_free (GimpImage *gimage)
{
g_return_if_fail (GIMP_IS_IMAGE (gimage));
/* Emit the UNDO_FREE event before actually freeing everything
* so the views can properly detach from the undo items
*/
gimp_image_undo_event (gimage, GIMP_UNDO_EVENT_UNDO_FREE, NULL);
gimp_undo_free (GIMP_UNDO (gimage->undo_stack), GIMP_UNDO_MODE_UNDO);
gimp_undo_free (GIMP_UNDO (gimage->redo_stack), GIMP_UNDO_MODE_REDO);
/* If the image was dirty, but could become clean by redo-ing
* some actions, then it should now become 'infinitely' dirty.
* This is because we've just nuked the actions that would allow
* the image to become clean again. The only hope for salvation
* is to save the image now! -- austin
*/
if (gimage->dirty < 0)
gimage->dirty = 10000;
/* The same applies to the case where the image would become clean
* due to undo actions, but since user can't undo without an undo
* stack, that's not so much a problem.
*/
}
gboolean
gimp_image_undo_group_start (GimpImage *gimage,
GimpUndoType undo_type,
const gchar *name)
{
GimpUndoStack *undo_group;
GimpDirtyMask dirty_mask;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
g_return_val_if_fail (undo_type > GIMP_UNDO_GROUP_FIRST &&
undo_type <= GIMP_UNDO_GROUP_LAST, FALSE);
if (! name)
name = gimp_undo_type_to_name (undo_type);
dirty_mask = gimp_image_undo_dirty_from_type (undo_type);
/* Notify listeners that the image will be modified */
if (gimage->group_count == 0 && dirty_mask != GIMP_DIRTY_NONE)
gimp_image_dirty (gimage, dirty_mask);
if (gimage->undo_freeze_count > 0)
return FALSE;
gimage->group_count++;
/* If we're already in a group...ignore */
if (gimage->group_count > 1)
return TRUE;
/* nuke the redo stack */
gimp_image_undo_free_redo (gimage);
/* If the image was dirty, but could become clean by redo-ing
* some actions, then it should now become 'infinitely' dirty.
* This is because we've just nuked the actions that would allow
* the image to become clean again. The only hope for salvation
* is to save the image now! -- austin
*/
if (gimage->dirty < 0)
gimage->dirty = 10000;
undo_group = gimp_undo_stack_new (gimage);
gimp_object_set_name (GIMP_OBJECT (undo_group), name);
GIMP_UNDO (undo_group)->undo_type = undo_type;
GIMP_UNDO (undo_group)->dirty_mask = dirty_mask;
gimp_undo_stack_push_undo (gimage->undo_stack, GIMP_UNDO (undo_group));
gimage->pushing_undo_group = undo_type;
return TRUE;
}
gboolean
gimp_image_undo_group_end (GimpImage *gimage)
{
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
if (gimage->undo_freeze_count > 0)
return FALSE;
g_return_val_if_fail (gimage->group_count > 0, FALSE);
gimage->group_count--;
if (gimage->group_count == 0)
{
gimage->pushing_undo_group = GIMP_UNDO_GROUP_NONE;
/* Do it here, since undo_push doesn't emit this event while in
* the middle of a group
*/
gimp_image_undo_event (gimage, GIMP_UNDO_EVENT_UNDO_PUSHED,
gimp_undo_stack_peek (gimage->undo_stack));
gimp_image_undo_free_space (gimage);
}
return TRUE;
}
GimpUndo *
gimp_image_undo_push (GimpImage *gimage,
GType object_type,
gint64 size,
gsize struct_size,
GimpUndoType undo_type,
const gchar *name,
GimpDirtyMask dirty_mask,
GimpUndoPopFunc pop_func,
GimpUndoFreeFunc free_func,
...)
{
GParameter *params = NULL;
gint n_params = 0;
va_list args;
GimpUndo *undo;
gpointer undo_struct = NULL;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (g_type_is_a (object_type, GIMP_TYPE_UNDO), NULL);
g_return_val_if_fail (undo_type > GIMP_UNDO_GROUP_LAST, NULL);
/* Does this undo dirty the image? If so, we always want to mark
* image dirty, even if we can't actually push the undo.
*/
if (dirty_mask != GIMP_DIRTY_NONE)
gimp_image_dirty (gimage, dirty_mask);
if (gimage->undo_freeze_count > 0)
return NULL;
if (! name)
name = gimp_undo_type_to_name (undo_type);
if (struct_size > 0)
undo_struct = g_malloc0 (struct_size);
params = gimp_parameters_append (object_type, params, &n_params,
"name", name,
"image", gimage,
"undo-type", undo_type,
"dirty-mask", dirty_mask,
"data", undo_struct,
"size", size,
"pop-func", pop_func,
"free-func", free_func,
NULL);
va_start (args, free_func);
params = gimp_parameters_append_valist (object_type, params, &n_params, args);
va_end (args);
undo = g_object_newv (object_type, n_params, params);
gimp_parameters_free (params, n_params);
/* nuke the redo stack */
gimp_image_undo_free_redo (gimage);
/* If the image was dirty, but could become clean by redo-ing
* some actions, then it should now become 'infinitely' dirty.
* This is because we've just nuked the actions that would allow
* the image to become clean again. The only hope for salvation
* is to save the image now! -- austin
*/
if (gimage->dirty < 0)
gimage->dirty = 10000;
if (gimage->pushing_undo_group == GIMP_UNDO_GROUP_NONE)
{
gimp_undo_stack_push_undo (gimage->undo_stack, undo);
gimp_image_undo_event (gimage, GIMP_UNDO_EVENT_UNDO_PUSHED, undo);
gimp_image_undo_free_space (gimage);
/* freeing undo space may have freed the newly pushed undo */
if (gimp_undo_stack_peek (gimage->undo_stack) == undo)
return undo;
}
else
{
GimpUndoStack *undo_group;
undo_group = GIMP_UNDO_STACK (gimp_undo_stack_peek (gimage->undo_stack));
gimp_undo_stack_push_undo (undo_group, undo);
return undo;
}
return NULL;
}
GimpUndo *
gimp_image_undo_can_compress (GimpImage *gimage,
GType object_type,
GimpUndoType undo_type)
{
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
if (gimage->dirty != 0 && ! gimp_undo_stack_peek (gimage->redo_stack))
{
GimpUndo *undo = gimp_undo_stack_peek (gimage->undo_stack);
if (undo && undo->undo_type == undo_type &&
g_type_is_a (G_TYPE_FROM_INSTANCE (undo), object_type))
{
return undo;
}
}
return NULL;
}
/* private functions */
static void
gimp_image_undo_pop_stack (GimpImage *gimage,
GimpUndoStack *undo_stack,
GimpUndoStack *redo_stack,
GimpUndoMode undo_mode)
{
GimpUndo *undo;
GimpUndoAccumulator accum = { 0, };
g_object_freeze_notify (G_OBJECT (gimage));
undo = gimp_undo_stack_pop_undo (undo_stack, undo_mode, &accum);
if (undo)
{
if (GIMP_IS_UNDO_STACK (undo))
gimp_list_reverse (GIMP_LIST (GIMP_UNDO_STACK (undo)->undos));
gimp_undo_stack_push_undo (redo_stack, undo);
if (accum.mode_changed)
gimp_image_mode_changed (gimage);
if (accum.size_changed)
gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
if (accum.resolution_changed)
gimp_image_resolution_changed (gimage);
if (accum.unit_changed)
gimp_image_unit_changed (gimage);
if (accum.qmask_changed)
gimp_image_qmask_changed (gimage);
if (accum.alpha_changed)
gimp_image_alpha_changed (gimage);
/* let others know that we just popped an action */
gimp_image_undo_event (gimage,
(undo_mode == GIMP_UNDO_MODE_UNDO) ?
GIMP_UNDO_EVENT_UNDO : GIMP_UNDO_EVENT_REDO,
undo);
}
g_object_thaw_notify (G_OBJECT (gimage));
}
static void
gimp_image_undo_free_space (GimpImage *gimage)
{
GimpContainer *container;
gint min_undo_levels;
gint max_undo_levels;
gint64 undo_size;
container = gimage->undo_stack->undos;
min_undo_levels = gimage->gimp->config->levels_of_undo;
max_undo_levels = 1024; /* FIXME */
undo_size = gimage->gimp->config->undo_size;
#ifdef DEBUG_IMAGE_UNDO
g_printerr ("undo_steps: %d undo_bytes: %ld\n",
gimp_container_num_children (container),
(glong) gimp_object_get_memsize (GIMP_OBJECT (container), NULL));
#endif
/* keep at least min_undo_levels undo steps */
if (gimp_container_num_children (container) <= min_undo_levels)
return;
while ((gimp_object_get_memsize (GIMP_OBJECT (container), NULL) > undo_size) ||
(gimp_container_num_children (container) > max_undo_levels))
{
GimpUndo *freed = gimp_undo_stack_free_bottom (gimage->undo_stack,
GIMP_UNDO_MODE_UNDO);
#ifdef DEBUG_IMAGE_UNDO
g_printerr ("freed one step: undo_steps: %d undo_bytes: %ld\n",
gimp_container_num_children (container),
(glong) gimp_object_get_memsize (GIMP_OBJECT (container),
NULL));
#endif
gimp_image_undo_event (gimage, GIMP_UNDO_EVENT_UNDO_EXPIRED, freed);
g_object_unref (freed);
if (gimp_container_num_children (container) <= min_undo_levels)
return;
}
}
static void
gimp_image_undo_free_redo (GimpImage *gimage)
{
GimpContainer *container;
container = gimage->redo_stack->undos;
#ifdef DEBUG_IMAGE_UNDO
g_printerr ("redo_steps: %d redo_bytes: %ld\n",
gimp_container_num_children (container),
(glong) gimp_object_get_memsize (GIMP_OBJECT (container), NULL));
#endif
while (gimp_container_num_children (container) > 0)
{
GimpUndo *freed = gimp_undo_stack_free_bottom (gimage->redo_stack,
GIMP_UNDO_MODE_REDO);
#ifdef DEBUG_IMAGE_UNDO
g_printerr ("freed one step: redo_steps: %d redo_bytes: %ld\n",
gimp_container_num_children (container),
(glong )gimp_object_get_memsize (GIMP_OBJECT (container),
NULL));
#endif
gimp_image_undo_event (gimage, GIMP_UNDO_EVENT_REDO_EXPIRED, freed);
g_object_unref (freed);
}
}
static GimpDirtyMask
gimp_image_undo_dirty_from_type (GimpUndoType undo_type)
{
switch (undo_type)
{
case GIMP_UNDO_GROUP_IMAGE_SCALE:
case GIMP_UNDO_GROUP_IMAGE_RESIZE:
case GIMP_UNDO_GROUP_IMAGE_FLIP:
case GIMP_UNDO_GROUP_IMAGE_ROTATE:
case GIMP_UNDO_GROUP_IMAGE_CROP:
return GIMP_DIRTY_IMAGE | GIMP_DIRTY_IMAGE_SIZE;
case GIMP_UNDO_GROUP_IMAGE_CONVERT:
return GIMP_DIRTY_IMAGE | GIMP_DIRTY_DRAWABLE;
case GIMP_UNDO_GROUP_IMAGE_LAYERS_MERGE:
return GIMP_DIRTY_IMAGE_STRUCTURE | GIMP_DIRTY_DRAWABLE;
case GIMP_UNDO_GROUP_IMAGE_VECTORS_MERGE:
return GIMP_DIRTY_IMAGE_STRUCTURE | GIMP_DIRTY_VECTORS;
case GIMP_UNDO_GROUP_IMAGE_QMASK: /* FIXME */
return GIMP_DIRTY_IMAGE_STRUCTURE | GIMP_DIRTY_SELECTION;
case GIMP_UNDO_GROUP_IMAGE_GRID:
case GIMP_UNDO_GROUP_IMAGE_GUIDE:
return GIMP_DIRTY_IMAGE_META;
case GIMP_UNDO_GROUP_DRAWABLE:
case GIMP_UNDO_GROUP_DRAWABLE_MOD:
return GIMP_DIRTY_ITEM | GIMP_DIRTY_DRAWABLE;
case GIMP_UNDO_GROUP_MASK: /* FIXME */
return GIMP_DIRTY_SELECTION;
case GIMP_UNDO_GROUP_ITEM_VISIBILITY:
case GIMP_UNDO_GROUP_ITEM_LINKED:
case GIMP_UNDO_GROUP_ITEM_PROPERTIES:
return GIMP_DIRTY_ITEM_META;
case GIMP_UNDO_GROUP_ITEM_DISPLACE: /* FIXME */
return GIMP_DIRTY_ITEM | GIMP_DIRTY_DRAWABLE | GIMP_DIRTY_VECTORS;
case GIMP_UNDO_GROUP_ITEM_SCALE: /* FIXME */
case GIMP_UNDO_GROUP_ITEM_RESIZE: /* FIXME */
return GIMP_DIRTY_ITEM | GIMP_DIRTY_DRAWABLE | GIMP_DIRTY_VECTORS;
case GIMP_UNDO_GROUP_LAYER_ADD_MASK:
case GIMP_UNDO_GROUP_LAYER_APPLY_MASK:
return GIMP_DIRTY_IMAGE_STRUCTURE;
case GIMP_UNDO_GROUP_FS_TO_LAYER:
case GIMP_UNDO_GROUP_FS_FLOAT:
case GIMP_UNDO_GROUP_FS_ANCHOR:
case GIMP_UNDO_GROUP_FS_REMOVE:
return GIMP_DIRTY_IMAGE_STRUCTURE;
case GIMP_UNDO_GROUP_EDIT_PASTE:
return GIMP_DIRTY_IMAGE_STRUCTURE;
case GIMP_UNDO_GROUP_EDIT_CUT:
return GIMP_DIRTY_ITEM | GIMP_DIRTY_DRAWABLE;
case GIMP_UNDO_GROUP_TEXT:
return GIMP_DIRTY_ITEM | GIMP_DIRTY_DRAWABLE;
case GIMP_UNDO_GROUP_TRANSFORM: /* FIXME */
return GIMP_DIRTY_ITEM | GIMP_DIRTY_DRAWABLE | GIMP_DIRTY_VECTORS;
case GIMP_UNDO_GROUP_PAINT:
return GIMP_DIRTY_ITEM | GIMP_DIRTY_DRAWABLE;
case GIMP_UNDO_GROUP_PARASITE_ATTACH:
case GIMP_UNDO_GROUP_PARASITE_REMOVE:
return GIMP_DIRTY_IMAGE_META | GIMP_DIRTY_ITEM_META;
case GIMP_UNDO_GROUP_VECTORS_IMPORT:
return GIMP_DIRTY_IMAGE_STRUCTURE | GIMP_DIRTY_VECTORS;
case GIMP_UNDO_GROUP_MISC:
return GIMP_DIRTY_ALL;
default:
break;
}
return GIMP_DIRTY_ALL;
}
|