/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "pika.h"
#include "pikachannelpropundo.h"
#include "pikachannelundo.h"
#include "pikadrawablemodundo.h"
#include "pikadrawablepropundo.h"
#include "pikadrawableundo.h"
#include "pikafloatingselectionundo.h"
#include "pikagrid.h"
#include "pikagrouplayer.h"
#include "pikagrouplayerundo.h"
#include "pikaguide.h"
#include "pikaguideundo.h"
#include "pikaimage.h"
#include "pikaimage-undo.h"
#include "pikaimage-undo-push.h"
#include "pikaimageundo.h"
#include "pikaitempropundo.h"
#include "pikalayermask.h"
#include "pikalayermaskpropundo.h"
#include "pikalayermaskundo.h"
#include "pikalayerpropundo.h"
#include "pikalayerundo.h"
#include "pikamaskundo.h"
#include "pikasamplepoint.h"
#include "pikasamplepointundo.h"
#include "pikaselection.h"

#include "text/pikatextlayer.h"
#include "text/pikatextundo.h"

#include "vectors/pikavectors.h"
#include "vectors/pikavectorsmodundo.h"
#include "vectors/pikavectorspropundo.h"
#include "vectors/pikavectorsundo.h"

#include "pika-intl.h"


/**************************/
/*  Image Property Undos  */
/**************************/

PikaUndo *
pika_image_undo_push_image_type (PikaImage   *image,
                                 const gchar *undo_desc)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_TYPE, undo_desc,
                               PIKA_DIRTY_IMAGE,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_precision (PikaImage   *image,
                                      const gchar *undo_desc)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_PRECISION, undo_desc,
                               PIKA_DIRTY_IMAGE,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_size (PikaImage   *image,
                                 const gchar *undo_desc,
                                 gint         previous_origin_x,
                                 gint         previous_origin_y,
                                 gint         previous_width,
                                 gint         previous_height)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_SIZE, undo_desc,
                               PIKA_DIRTY_IMAGE | PIKA_DIRTY_IMAGE_SIZE,
                               "previous-origin-x", previous_origin_x,
                               "previous-origin-y", previous_origin_y,
                               "previous-width",    previous_width,
                               "previous-height",   previous_height,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_resolution (PikaImage   *image,
                                       const gchar *undo_desc)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_RESOLUTION, undo_desc,
                               PIKA_DIRTY_IMAGE,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_grid (PikaImage   *image,
                                 const gchar *undo_desc,
                                 PikaGrid    *grid)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GRID (grid), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_GRID, undo_desc,
                               PIKA_DIRTY_IMAGE_META,
                               "grid", grid,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_colormap (PikaImage   *image,
                                     const gchar *undo_desc)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_COLORMAP, undo_desc,
                               PIKA_DIRTY_IMAGE,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_hidden_profile (PikaImage   *image,
                                           const gchar *undo_desc)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_HIDDEN_PROFILE, undo_desc,
                               PIKA_DIRTY_IMAGE,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_metadata (PikaImage   *image,
                                     const gchar *undo_desc)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_IMAGE_METADATA, undo_desc,
                               PIKA_DIRTY_IMAGE_META,
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_parasite (PikaImage          *image,
                                     const gchar        *undo_desc,
                                     const PikaParasite *parasite)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (parasite != NULL, NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_PARASITE_ATTACH, undo_desc,
                               PIKA_DIRTY_IMAGE_META,
                               "parasite-name", pika_parasite_get_name (parasite),
                               NULL);
}

PikaUndo *
pika_image_undo_push_image_parasite_remove (PikaImage   *image,
                                            const gchar *undo_desc,
                                            const gchar *name)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  return pika_image_undo_push (image, PIKA_TYPE_IMAGE_UNDO,
                               PIKA_UNDO_PARASITE_REMOVE, undo_desc,
                               PIKA_DIRTY_IMAGE_META,
                               "parasite-name", name,
                               NULL);
}


/********************************/
/*  Guide & Sample Point Undos  */
/********************************/

PikaUndo *
pika_image_undo_push_guide (PikaImage   *image,
                            const gchar *undo_desc,
                            PikaGuide   *guide)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GUIDE (guide), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GUIDE_UNDO,
                               PIKA_UNDO_GUIDE, undo_desc,
                               PIKA_DIRTY_IMAGE_META,
                               "aux-item", guide,
                               NULL);
}

PikaUndo *
pika_image_undo_push_sample_point (PikaImage       *image,
                                   const gchar     *undo_desc,
                                   PikaSamplePoint *sample_point)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_SAMPLE_POINT (sample_point), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_SAMPLE_POINT_UNDO,
                               PIKA_UNDO_SAMPLE_POINT, undo_desc,
                               PIKA_DIRTY_IMAGE_META,
                               "aux-item", sample_point,
                               NULL);
}


/********************/
/*  Drawable Undos  */
/********************/

PikaUndo *
pika_image_undo_push_drawable (PikaImage    *image,
                               const gchar  *undo_desc,
                               PikaDrawable *drawable,
                               GeglBuffer   *buffer,
                               gint          x,
                               gint          y)
{
  PikaItem *item;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (GEGL_IS_BUFFER (buffer), NULL);

  item = PIKA_ITEM (drawable);

  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_DRAWABLE_UNDO,
                               PIKA_UNDO_DRAWABLE, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",   item,
                               "buffer", buffer,
                               "x",      x,
                               "y",      y,
                               NULL);
}

PikaUndo *
pika_image_undo_push_drawable_mod (PikaImage    *image,
                                   const gchar  *undo_desc,
                                   PikaDrawable *drawable,
                                   gboolean      copy_buffer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_DRAWABLE_MOD_UNDO,
                               PIKA_UNDO_DRAWABLE_MOD, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",        drawable,
                               "copy-buffer", copy_buffer,
                               NULL);
}

PikaUndo *
pika_image_undo_push_drawable_format (PikaImage    *image,
                                      const gchar  *undo_desc,
                                      PikaDrawable *drawable)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_DRAWABLE_PROP_UNDO,
                               PIKA_UNDO_DRAWABLE_FORMAT, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item", drawable,
                               NULL);
}


/****************/
/*  Mask Undos  */
/****************/

PikaUndo *
pika_image_undo_push_mask (PikaImage   *image,
                           const gchar *undo_desc,
                           PikaChannel *mask)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CHANNEL (mask), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (mask)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_MASK_UNDO,
                               PIKA_UNDO_MASK, undo_desc,
                               PIKA_IS_SELECTION (mask) ?
                               PIKA_DIRTY_SELECTION :
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item", mask,
                               NULL);
}

PikaUndo *
pika_image_undo_push_mask_precision (PikaImage   *image,
                                     const gchar *undo_desc,
                                     PikaChannel *mask)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CHANNEL (mask), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (mask)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_MASK_UNDO,
                               PIKA_UNDO_MASK, undo_desc,
                               PIKA_IS_SELECTION (mask) ?
                               PIKA_DIRTY_SELECTION :
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",           mask,
                               "convert-format", TRUE,
                               NULL);
}


/****************/
/*  Item Undos  */
/****************/

PikaUndo *
pika_image_undo_push_item_reorder (PikaImage   *image,
                                   const gchar *undo_desc,
                                   PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_REORDER, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_rename (PikaImage   *image,
                                  const gchar *undo_desc,
                                  PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_RENAME, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_displace (PikaImage   *image,
                                    const gchar *undo_desc,
                                    PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_DISPLACE, undo_desc,
                               PIKA_IS_DRAWABLE (item) ?
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE :
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_VECTORS,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_visibility (PikaImage   *image,
                                      const gchar *undo_desc,
                                      PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_VISIBILITY, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_color_tag (PikaImage   *image,
                                     const gchar *undo_desc,
                                     PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_COLOR_TAG, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_lock_content (PikaImage   *image,
                                        const gchar *undo_desc,
                                        PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_LOCK_CONTENT, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_lock_position (PikaImage   *image,
                                         const gchar *undo_desc,
                                         PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_LOCK_POSITION, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_lock_visibility (PikaImage   *image,
                                           const gchar *undo_desc,
                                           PikaItem    *item)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_ITEM_LOCK_VISIBILITY, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", item,
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_parasite (PikaImage          *image,
                                    const gchar        *undo_desc,
                                    PikaItem           *item,
                                    const PikaParasite *parasite)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);
  g_return_val_if_fail (parasite != NULL, NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_PARASITE_ATTACH, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item",          item,
                               "parasite-name", pika_parasite_get_name (parasite),
                               NULL);
}

PikaUndo *
pika_image_undo_push_item_parasite_remove (PikaImage   *image,
                                           const gchar *undo_desc,
                                           PikaItem    *item,
                                           const gchar *name)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_ITEM (item), NULL);
  g_return_val_if_fail (pika_item_is_attached (item), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  return pika_image_undo_push (image, PIKA_TYPE_ITEM_PROP_UNDO,
                               PIKA_UNDO_PARASITE_REMOVE, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item",          item,
                               "parasite-name", name,
                               NULL);
}


/*****************/
/*  Layer Undos  */
/*****************/

PikaUndo *
pika_image_undo_push_layer_add (PikaImage   *image,
                                const gchar *undo_desc,
                                PikaLayer   *layer,
                                GList       *prev_layers)
{
  GList *iter;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (! pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  for (iter = prev_layers; iter; iter = iter->next)
    g_return_val_if_fail (PIKA_IS_LAYER (iter->data), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_UNDO,
                               PIKA_UNDO_LAYER_ADD, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",        layer,
                               "prev-layers", prev_layers,
                               NULL);
}

PikaUndo *
pika_image_undo_push_layer_remove (PikaImage   *image,
                                   const gchar *undo_desc,
                                   PikaLayer   *layer,
                                   PikaLayer   *prev_parent,
                                   gint         prev_position,
                                   GList       *prev_layers)
{
  GList *iter;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);
  g_return_val_if_fail (prev_parent == NULL || PIKA_IS_LAYER (prev_parent),
                        NULL);

  for (iter = prev_layers; iter; iter = iter->next)
    g_return_val_if_fail (PIKA_IS_LAYER (iter->data), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_UNDO,
                               PIKA_UNDO_LAYER_REMOVE, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",          layer,
                               "prev-parent",   prev_parent,
                               "prev-position", prev_position,
                               "prev-layers",   prev_layers,
                               NULL);
}

PikaUndo *
pika_image_undo_push_layer_mode (PikaImage   *image,
                                 const gchar *undo_desc,
                                 PikaLayer   *layer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_PROP_UNDO,
                               PIKA_UNDO_LAYER_MODE, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", layer,
                               NULL);
}

PikaUndo *
pika_image_undo_push_layer_opacity (PikaImage   *image,
                                    const gchar *undo_desc,
                                    PikaLayer   *layer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_PROP_UNDO,
                               PIKA_UNDO_LAYER_OPACITY, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", layer,
                               NULL);
}

PikaUndo *
pika_image_undo_push_layer_lock_alpha (PikaImage   *image,
                                       const gchar *undo_desc,
                                       PikaLayer   *layer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_PROP_UNDO,
                               PIKA_UNDO_LAYER_LOCK_ALPHA, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", layer,
                               NULL);
}


/***********************/
/*  Group Layer Undos  */
/***********************/

PikaUndo *
pika_image_undo_push_group_layer_suspend_resize (PikaImage      *image,
                                                 const gchar    *undo_desc,
                                                 PikaGroupLayer *group)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GROUP_LAYER_UNDO,
                               PIKA_UNDO_GROUP_LAYER_SUSPEND_RESIZE, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",  group,
                               NULL);
}

PikaUndo *
pika_image_undo_push_group_layer_resume_resize (PikaImage      *image,
                                                const gchar    *undo_desc,
                                                PikaGroupLayer *group)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GROUP_LAYER_UNDO,
                               PIKA_UNDO_GROUP_LAYER_RESUME_RESIZE, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",  group,
                               NULL);
}

PikaUndo *
pika_image_undo_push_group_layer_suspend_mask (PikaImage      *image,
                                               const gchar    *undo_desc,
                                               PikaGroupLayer *group)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GROUP_LAYER_UNDO,
                               PIKA_UNDO_GROUP_LAYER_SUSPEND_MASK, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",  group,
                               NULL);
}

PikaUndo *
pika_image_undo_push_group_layer_resume_mask (PikaImage      *image,
                                              const gchar    *undo_desc,
                                              PikaGroupLayer *group)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GROUP_LAYER_UNDO,
                               PIKA_UNDO_GROUP_LAYER_RESUME_MASK, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",  group,
                               NULL);
}

PikaUndo *
pika_image_undo_push_group_layer_start_transform (PikaImage      *image,
                                                  const gchar    *undo_desc,
                                                  PikaGroupLayer *group)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GROUP_LAYER_UNDO,
                               PIKA_UNDO_GROUP_LAYER_START_TRANSFORM, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",  group,
                               NULL);
}

PikaUndo *
pika_image_undo_push_group_layer_end_transform (PikaImage      *image,
                                                const gchar    *undo_desc,
                                                PikaGroupLayer *group)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GROUP_LAYER_UNDO,
                               PIKA_UNDO_GROUP_LAYER_END_TRANSFORM, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",  group,
                               NULL);
}

PikaUndo *
pika_image_undo_push_group_layer_convert (PikaImage      *image,
                                          const gchar    *undo_desc,
                                          PikaGroupLayer *group)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (group)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_GROUP_LAYER_UNDO,
                               PIKA_UNDO_GROUP_LAYER_CONVERT, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item", group,
                               NULL);
}


/**********************/
/*  Text Layer Undos  */
/**********************/

PikaUndo *
pika_image_undo_push_text_layer (PikaImage        *image,
                                 const gchar      *undo_desc,
                                 PikaTextLayer    *layer,
                                 const GParamSpec *pspec)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_TEXT_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_TEXT_UNDO,
                               PIKA_UNDO_TEXT_LAYER, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item",  layer,
                               "param", pspec,
                               NULL);
}

PikaUndo *
pika_image_undo_push_text_layer_modified (PikaImage     *image,
                                          const gchar   *undo_desc,
                                          PikaTextLayer *layer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_TEXT_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_TEXT_UNDO,
                               PIKA_UNDO_TEXT_LAYER_MODIFIED, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", layer,
                               NULL);
}

PikaUndo *
pika_image_undo_push_text_layer_convert (PikaImage     *image,
                                         const gchar   *undo_desc,
                                         PikaTextLayer *layer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_TEXT_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_TEXT_UNDO,
                               PIKA_UNDO_TEXT_LAYER_CONVERT, undo_desc,
                               PIKA_DIRTY_ITEM,
                               "item", layer,
                               NULL);
}


/**********************/
/*  Layer Mask Undos  */
/**********************/

PikaUndo *
pika_image_undo_push_layer_mask_add (PikaImage     *image,
                                     const gchar   *undo_desc,
                                     PikaLayer     *layer,
                                     PikaLayerMask *mask)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER_MASK (mask), NULL);
  g_return_val_if_fail (! pika_item_is_attached (PIKA_ITEM (mask)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_MASK_UNDO,
                               PIKA_UNDO_LAYER_MASK_ADD, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",       layer,
                               "layer-mask", mask,
                               NULL);
}

PikaUndo *
pika_image_undo_push_layer_mask_remove (PikaImage     *image,
                                        const gchar   *undo_desc,
                                        PikaLayer     *layer,
                                        PikaLayerMask *mask)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER_MASK (mask), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (mask)), NULL);
  g_return_val_if_fail (pika_layer_mask_get_layer (mask) == layer, NULL);
  g_return_val_if_fail (pika_layer_get_mask (layer) == mask, NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_MASK_UNDO,
                               PIKA_UNDO_LAYER_MASK_REMOVE, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",       layer,
                               "layer-mask", mask,
                               NULL);
}

PikaUndo *
pika_image_undo_push_layer_mask_apply (PikaImage   *image,
                                       const gchar *undo_desc,
                                       PikaLayer   *layer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_MASK_PROP_UNDO,
                               PIKA_UNDO_LAYER_MASK_APPLY, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", layer,
                               NULL);
}

PikaUndo *
pika_image_undo_push_layer_mask_show (PikaImage   *image,
                                      const gchar *undo_desc,
                                      PikaLayer   *layer)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (layer), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (layer)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_LAYER_MASK_PROP_UNDO,
                               PIKA_UNDO_LAYER_MASK_SHOW, undo_desc,
                               PIKA_DIRTY_ITEM_META,
                               "item", layer,
                               NULL);
}


/*******************/
/*  Channel Undos  */
/*******************/

PikaUndo *
pika_image_undo_push_channel_add (PikaImage   *image,
                                  const gchar *undo_desc,
                                  PikaChannel *channel,
                                  GList       *prev_channels)
{
  GList *iter;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CHANNEL (channel), NULL);
  g_return_val_if_fail (! pika_item_is_attached (PIKA_ITEM (channel)), NULL);

  for (iter = prev_channels; iter; iter = iter->next)
    g_return_val_if_fail (PIKA_IS_CHANNEL (iter->data), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_CHANNEL_UNDO,
                               PIKA_UNDO_CHANNEL_ADD, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",          channel,
                               "prev-channels", prev_channels,
                               NULL);
}

PikaUndo *
pika_image_undo_push_channel_remove (PikaImage   *image,
                                     const gchar *undo_desc,
                                     PikaChannel *channel,
                                     PikaChannel *prev_parent,
                                     gint         prev_position,
                                     GList       *prev_channels)
{
  GList *iter;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CHANNEL (channel), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (channel)), NULL);
  g_return_val_if_fail (prev_parent == NULL || PIKA_IS_CHANNEL (prev_parent),
                        NULL);

  for (iter = prev_channels; iter; iter = iter->next)
    g_return_val_if_fail (PIKA_IS_CHANNEL (iter->data), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_CHANNEL_UNDO,
                               PIKA_UNDO_CHANNEL_REMOVE, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",          channel,
                               "prev-parent",   prev_parent,
                               "prev-position", prev_position,
                               "prev-channels", prev_channels,
                               NULL);
}

PikaUndo *
pika_image_undo_push_channel_color (PikaImage   *image,
                                    const gchar *undo_desc,
                                    PikaChannel *channel)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_CHANNEL (channel), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (channel)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_CHANNEL_PROP_UNDO,
                               PIKA_UNDO_CHANNEL_COLOR, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_DRAWABLE,
                               "item", channel,
                               NULL);
}


/*******************/
/*  Vectors Undos  */
/*******************/

PikaUndo *
pika_image_undo_push_vectors_add (PikaImage   *image,
                                  const gchar *undo_desc,
                                  PikaVectors *vectors,
                                  GList       *prev_vectors)
{
  GList *iter;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_VECTORS (vectors), NULL);
  g_return_val_if_fail (! pika_item_is_attached (PIKA_ITEM (vectors)), NULL);

  for (iter = prev_vectors; iter; iter = iter->next)
    g_return_val_if_fail (PIKA_IS_VECTORS (iter->data), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_VECTORS_UNDO,
                               PIKA_UNDO_VECTORS_ADD, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",         vectors,
                               "prev-vectors", prev_vectors,
                               NULL);
}

PikaUndo *
pika_image_undo_push_vectors_remove (PikaImage   *image,
                                     const gchar *undo_desc,
                                     PikaVectors *vectors,
                                     PikaVectors *prev_parent,
                                     gint         prev_position,
                                     GList       *prev_vectors)
{
  GList *iter;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_VECTORS (vectors), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (vectors)), NULL);
  g_return_val_if_fail (prev_parent == NULL || PIKA_IS_VECTORS (prev_parent),
                        NULL);

  for (iter = prev_vectors; iter; iter = iter->next)
    g_return_val_if_fail (PIKA_IS_VECTORS (iter->data), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_VECTORS_UNDO,
                               PIKA_UNDO_VECTORS_REMOVE, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item",          vectors,
                               "prev-parent",   prev_parent,
                               "prev-position", prev_position,
                               "prev-vectors",  prev_vectors,
                               NULL);
}

PikaUndo *
pika_image_undo_push_vectors_mod (PikaImage   *image,
                                  const gchar *undo_desc,
                                  PikaVectors *vectors)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_VECTORS (vectors), NULL);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (vectors)), NULL);

  return pika_image_undo_push (image, PIKA_TYPE_VECTORS_MOD_UNDO,
                               PIKA_UNDO_VECTORS_MOD, undo_desc,
                               PIKA_DIRTY_ITEM | PIKA_DIRTY_VECTORS,
                               "item", vectors,
                               NULL);
}


/******************************/
/*  Floating Selection Undos  */
/******************************/

PikaUndo *
pika_image_undo_push_fs_to_layer (PikaImage    *image,
                                  const gchar  *undo_desc,
                                  PikaLayer    *floating_layer)
{
  PikaUndo *undo;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (PIKA_IS_LAYER (floating_layer), NULL);

  undo = pika_image_undo_push (image, PIKA_TYPE_FLOATING_SELECTION_UNDO,
                               PIKA_UNDO_FS_TO_LAYER, undo_desc,
                               PIKA_DIRTY_IMAGE_STRUCTURE,
                               "item", floating_layer,
                               NULL);

  return undo;
}


/******************************************************************************/
/*  Something for which programmer is too lazy to write an undo function for  */
/******************************************************************************/

static void
undo_pop_cantundo (PikaUndo            *undo,
                   PikaUndoMode         undo_mode,
                   PikaUndoAccumulator *accum)
{
  switch (undo_mode)
    {
    case PIKA_UNDO_MODE_UNDO:
      pika_message (undo->image->pika, NULL, PIKA_MESSAGE_WARNING,
                    _("Can't undo %s"), pika_object_get_name (undo));
      break;

    case PIKA_UNDO_MODE_REDO:
      break;
    }
}

PikaUndo *
pika_image_undo_push_cantundo (PikaImage   *image,
                               const gchar *undo_desc)
{
  PikaUndo *undo;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  /* This is the sole purpose of this type of undo: the ability to
   * mark an image as having been mutated, without really providing
   * any adequate undo facility.
   */

  undo = pika_image_undo_push (image, PIKA_TYPE_UNDO,
                               PIKA_UNDO_CANT, undo_desc,
                               PIKA_DIRTY_ALL,
                               NULL);

  if (undo)
    g_signal_connect (undo, "pop",
                      G_CALLBACK (undo_pop_cantundo),
                      NULL);

  return undo;
}
