home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / undo.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  71.3 KB  |  2,966 lines

  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 <stdlib.h>
  22. #include <string.h>
  23.  
  24. #include <glib.h>
  25.  
  26. #include "apptypes.h"
  27.  
  28. #include "appenv.h"
  29. #include "by_color_select.h"
  30. #include "channel.h"
  31. #include "drawable.h"
  32. #include "floating_sel.h"
  33. #include "gdisplay.h"
  34. #include "gdisplay_ops.h"
  35. #include "gimage_mask.h"
  36. #include "gimpparasite.h"
  37. #include "gimprc.h"
  38. #include "layer.h"
  39. #include "paint_core.h"
  40. #include "paint_funcs.h"
  41. #include "parasitelist.h"
  42. #include "path_transform.h"
  43. #include "tools.h"
  44. #include "transform_core.h"
  45. #include "undo.h"
  46.  
  47. #include "drawable_pvt.h"
  48. #include "layer_pvt.h"
  49. #include "channel_pvt.h"
  50. #include "tile_manager_pvt.h"
  51. #include "tile.h"            /* ick. */
  52.  
  53. #include "libgimp/gimpparasite.h"
  54.  
  55. #include "libgimp/gimpintl.h"
  56.  
  57.  
  58. /*#define DEBUG*/
  59.  
  60. #ifdef DEBUG
  61. #define TRC(x) printf x
  62. #else
  63. #define TRC(x)
  64. #endif
  65.  
  66.  
  67. typedef enum
  68. {
  69.   UNDO = 0,
  70.   REDO = 1
  71. } UndoState;
  72.  
  73.  
  74. typedef gboolean  (* UndoPopFunc)  (GImage *, UndoState, UndoType, gpointer);
  75. typedef void      (* UndoFreeFunc) (UndoState, UndoType, gpointer);
  76.  
  77. typedef struct _Undo Undo;
  78.  
  79. struct _Undo
  80. {
  81.   UndoType      type;           /* undo type                           */
  82.   gpointer      data;           /* data to implement the undo, NULL for group */
  83.   glong         bytes;          /* size of undo item                   */
  84.   gboolean      dirties_image;  /* TRUE if undo mutates image */
  85.   gboolean      group_boundary; /* TRUE if this is the start/end of a group */
  86.  
  87.   UndoPopFunc   pop_func;      /* function pointer to undo pop proc   */
  88.   UndoFreeFunc  free_func;     /* function with specifics for freeing */
  89. };
  90.  
  91. /*  Pop functions  */
  92.  
  93. static gboolean undo_pop_image            (GImage *,
  94.                        UndoState, UndoType, gpointer);
  95. static gboolean undo_pop_mask             (GImage *,
  96.                        UndoState, UndoType, gpointer);
  97. static gboolean undo_pop_layer_displace   (GImage *,
  98.                        UndoState, UndoType, gpointer);
  99. static gboolean undo_pop_transform        (GImage *,
  100.                        UndoState, UndoType, gpointer);
  101. static gboolean undo_pop_paint            (GImage *,
  102.                        UndoState, UndoType, gpointer);
  103. static gboolean undo_pop_layer            (GImage *,
  104.                        UndoState, UndoType, gpointer);
  105. static gboolean undo_pop_layer_mod        (GImage *,
  106.                        UndoState, UndoType, gpointer);
  107. static gboolean undo_pop_layer_mask       (GImage *,
  108.                        UndoState, UndoType, gpointer);
  109. static gboolean undo_pop_channel          (GImage *,
  110.                        UndoState, UndoType, gpointer);
  111. static gboolean undo_pop_channel_mod      (GImage *,
  112.                        UndoState, UndoType, gpointer);
  113. static gboolean undo_pop_fs_to_layer      (GImage *,
  114.                        UndoState, UndoType, gpointer);
  115. static gboolean undo_pop_fs_rigor         (GImage *,
  116.                        UndoState, UndoType, gpointer);
  117. static gboolean undo_pop_fs_relax         (GImage *,
  118.                        UndoState, UndoType, gpointer);
  119. static gboolean undo_pop_gimage_mod       (GImage *,
  120.                        UndoState, UndoType, gpointer);
  121. static gboolean undo_pop_guide            (GImage *,
  122.                        UndoState, UndoType, gpointer);
  123. static gboolean undo_pop_parasite         (GImage *,
  124.                        UndoState, UndoType, gpointer);
  125. static gboolean undo_pop_qmask            (GImage *,
  126.                        UndoState, UndoType, gpointer);
  127. static gboolean undo_pop_resolution       (GImage *,
  128.                        UndoState, UndoType, gpointer);
  129. static gboolean undo_pop_layer_rename     (GImage *,
  130.                        UndoState, UndoType, gpointer);
  131. static gboolean undo_pop_layer_reposition (GImage *,
  132.                        UndoState, UndoType, gpointer);
  133. static gboolean undo_pop_cantundo         (GImage *,
  134.                        UndoState, UndoType, gpointer);
  135.  
  136.  
  137. /*  Free functions  */
  138.  
  139. static void     undo_free_image           (UndoState, UndoType, gpointer);
  140. static void     undo_free_mask            (UndoState, UndoType, gpointer);
  141. static void     undo_free_layer_displace  (UndoState, UndoType, gpointer);
  142. static void     undo_free_transform       (UndoState, UndoType, gpointer);
  143. static void     undo_free_paint           (UndoState, UndoType, gpointer);
  144. static void     undo_free_layer           (UndoState, UndoType, gpointer);
  145. static void     undo_free_layer_mod       (UndoState, UndoType, gpointer);
  146. static void     undo_free_layer_mask      (UndoState, UndoType, gpointer);
  147. static void     undo_free_channel         (UndoState, UndoType, gpointer);
  148. static void     undo_free_channel_mod     (UndoState, UndoType, gpointer);
  149. static void     undo_free_fs_to_layer     (UndoState, UndoType, gpointer);
  150. static void     undo_free_fs_rigor        (UndoState, UndoType, gpointer);
  151. static void     undo_free_fs_relax        (UndoState, UndoType, gpointer);
  152. static void     undo_free_gimage_mod      (UndoState, UndoType, gpointer);
  153. static void     undo_free_guide           (UndoState, UndoType, gpointer);
  154. static void     undo_free_parasite        (UndoState, UndoType, gpointer);
  155. static void     undo_free_qmask           (UndoState, UndoType, gpointer);
  156. static void     undo_free_resolution      (UndoState, UndoType, gpointer);
  157. static void     undo_free_layer_rename    (UndoState, UndoType, gpointer);
  158. static void     undo_free_layer_reposition(UndoState, UndoType, gpointer);
  159. static void     undo_free_cantundo        (UndoState, UndoType, gpointer);
  160.  
  161.  
  162. /*  Sizing functions  */
  163. static gint          layer_size         (Layer    *layer);
  164. static gint          channel_size       (Channel  *channel);
  165.  
  166. static const gchar * undo_type_to_name  (UndoType  undo_type);
  167.  
  168. static Undo        * undo_new           (UndoType  undo_type, 
  169.                      glong     size, 
  170.                      gboolean  dirties_image);
  171.  
  172.  
  173. static gboolean shrink_wrap = FALSE;
  174.  
  175.  
  176. static gint
  177. layer_size (Layer *layer)
  178. {
  179.   gint size;
  180.  
  181.   size =
  182.     sizeof (Layer) + 
  183.     GIMP_DRAWABLE (layer)->width * GIMP_DRAWABLE (layer)->height *
  184.     GIMP_DRAWABLE (layer)->bytes + 
  185.     strlen (GIMP_DRAWABLE (layer)->name);
  186.  
  187.   if (layer_get_mask (layer))
  188.     size += channel_size (GIMP_CHANNEL (layer_get_mask (layer)));
  189.  
  190.   return size;
  191. }
  192.  
  193.  
  194. static gint
  195. channel_size (Channel *channel)
  196. {
  197.   gint size;
  198.  
  199.   size =
  200.     sizeof (Channel) +
  201.     GIMP_DRAWABLE (channel)->width * GIMP_DRAWABLE (channel)->height +
  202.     strlen (GIMP_DRAWABLE (channel)->name);
  203.  
  204.   return size;
  205. }
  206.  
  207.  
  208. static void
  209. undo_free_list (GImage     *gimage,
  210.         UndoState   state,
  211.         GSList     *list)
  212. {
  213.   GSList *orig;
  214.   Undo   *undo;
  215.  
  216.   orig = list;
  217.  
  218.   while (list)
  219.     {
  220.       undo = (Undo *) list->data;
  221.       if (undo)
  222.     {
  223.       if (undo->free_func)
  224.         (*undo->free_func) (state, undo->type, undo->data);
  225.       gimage->undo_bytes -= undo->bytes;
  226.       g_free (undo);
  227.     }
  228.       list = g_slist_next (list);
  229.     }
  230.  
  231.   g_slist_free (orig);
  232. }
  233.  
  234.  
  235. static GSList *
  236. remove_stack_bottom (GImage *gimage)
  237. {
  238.   GSList   *list;
  239.   GSList   *last;
  240.   Undo     *object;
  241.   gboolean  in_group = FALSE;
  242.  
  243.   list = gimage->undo_stack;
  244.  
  245.   last = NULL;
  246.   while (list)
  247.     {
  248.       if (list->next == NULL)
  249.     {
  250.       if (last)
  251.         undo_free_list (gimage, UNDO, last->next);
  252.       else
  253.         undo_free_list (gimage, UNDO, list);
  254.  
  255.       gimage->undo_levels --;
  256.       if (last)
  257.         last->next = NULL;
  258.       list = NULL;
  259.  
  260.       gimp_image_undo_event (gimage, UNDO_EXPIRED);
  261.     }
  262.       else
  263.     {
  264.       object = (Undo *) list->data;
  265.       if (object->group_boundary)
  266.         in_group = (in_group) ? FALSE : TRUE;
  267.  
  268.       /*  Make sure last points to only a single item, or the
  269.        *  tail of an aggregate undo item
  270.        */
  271.       if (!in_group)
  272.         last = list;
  273.  
  274.       list = g_slist_next (list);
  275.     }
  276.     }
  277.  
  278.   if (last)
  279.     return gimage->undo_stack;
  280.   else
  281.     return NULL;
  282. }
  283.  
  284.  
  285. /* Allocate and initialise a new Undo.  Leaves data and function
  286.  * pointers zeroed ready to be filled in by caller. */
  287. static Undo *
  288. undo_new (UndoType  type, 
  289.       glong     size, 
  290.       gboolean  dirties_image)
  291. {
  292.   Undo *new;
  293.  
  294.   new = g_new0 (Undo, 1);
  295.  
  296.   new->type          = type;
  297.   new->bytes         = size;
  298.   new->dirties_image = dirties_image;
  299.  
  300.   return new;
  301. }
  302.  
  303.  
  304. static gboolean
  305. undo_free_up_space (GImage *gimage)
  306. {
  307.   /* If there are 0 levels of undo return FALSE.  */
  308.   if (levels_of_undo == 0)
  309.     return FALSE;
  310.  
  311.   /*  Delete the item on the bottom of the stack if we have the maximum
  312.    *  levels of undo already
  313.    */
  314.   while (gimage->undo_levels >= levels_of_undo)
  315.     gimage->undo_stack = remove_stack_bottom (gimage);
  316.  
  317.   return TRUE;
  318. }
  319.  
  320.  
  321. static Undo *
  322. undo_push (GImage   *gimage,
  323.        glong     size,
  324.        UndoType  type,
  325.        gboolean  dirties_image)
  326. {
  327.   Undo *new;
  328.  
  329.   /* Does this undo dirty the image?  If so, we always want to mark
  330.    * image dirty, even if we can't actually push the undo. */
  331.   if (dirties_image)
  332.       gimp_image_dirty (gimage);
  333.  
  334.   if (! gimage->undo_on)
  335.     return NULL;
  336.  
  337.   size += sizeof (Undo);
  338.  
  339.   if (gimage->redo_stack)
  340.     {
  341.       undo_free_list (gimage, REDO, gimage->redo_stack);
  342.       gimage->redo_stack = NULL;
  343.  
  344.       /* If the image was dirty, but could become clean by redo-ing
  345.        * some actions, then it should now become 'infinitely' dirty.
  346.        * This is because we've just nuked the actions that would allow
  347.        * the image to become clean again.  The only hope for salvation
  348.        * is to save the image now!  -- austin */
  349.       if (gimage->dirty < 0)
  350.     gimage->dirty = 10000;
  351.     }
  352.  
  353.   if (gimage->pushing_undo_group == UNDO_NULL)
  354.     if (! undo_free_up_space (gimage))
  355.       return NULL;
  356.  
  357.   new = undo_new (type, size, dirties_image);
  358.  
  359.   gimage->undo_bytes += size;
  360.  
  361.   /*  only increment levels if not in a group  */
  362.   if (gimage->pushing_undo_group == UNDO_NULL)
  363.       gimage->undo_levels ++;
  364.  
  365.   gimage->undo_stack = g_slist_prepend (gimage->undo_stack, (gpointer) new);
  366.   TRC (("undo_push: %s\n", undo_type_to_name (type)));
  367.  
  368.   /* lastly, tell people about the newly pushed undo (must come after
  369.    * modification of undo_stack).  */
  370.   if (gimage->pushing_undo_group == UNDO_NULL)
  371.       gimp_image_undo_event (gimage, UNDO_PUSHED);
  372.  
  373.   return new;
  374. }
  375.  
  376.  
  377. static gboolean
  378. pop_stack (GImage     *gimage,
  379.        GSList    **stack_ptr,
  380.        GSList    **unstack_ptr,
  381.        UndoState   state)
  382. {
  383.   Undo     *object;
  384.   GSList   *stack;
  385.   GSList   *tmp;
  386.   gint      status = 0;
  387.   gint      in_group = 0;
  388.   gint      x, y;
  389.   GDisplay *gdisp;
  390.  
  391.   /*  Keep popping until we pop a valid object
  392.    *  or get to the end of a group if we're in one
  393.    */
  394.   while (*stack_ptr)
  395.     {
  396.       stack = *stack_ptr;
  397.  
  398.       object = (Undo *) stack->data;
  399.       if (object->group_boundary)
  400.     {
  401.       in_group = (in_group) ? 0 : 1;
  402.       if (in_group)
  403.         gimage->undo_levels += (state == UNDO) ? -1 : 1;
  404.  
  405.       if (status && !in_group)
  406.         status = 1;
  407.       else
  408.         status = 0;
  409.     }
  410.       else
  411.     {
  412.       TRC (("undo_pop: %s\n", undo_type_to_name (object->type)));
  413.       status = (* object->pop_func) (gimage, state, object->type,
  414.                      object->data);
  415.  
  416.       if (object->dirties_image)
  417.         {
  418.           switch (state)
  419.         {
  420.         case UNDO:
  421.           gimp_image_clean (gimage);
  422.           break;
  423.         case REDO:
  424.           gimp_image_dirty (gimage);
  425.           break;
  426.         }
  427.         }
  428.  
  429.       if (!in_group)
  430.         gimage->undo_levels += (state == UNDO) ? -1 : 1;
  431.     }
  432.  
  433.       *unstack_ptr = g_slist_prepend (*unstack_ptr, (gpointer) object);
  434.  
  435.       tmp = stack;
  436.       *stack_ptr = g_slist_next (*stack_ptr);
  437.       tmp->next = NULL;
  438.       g_slist_free (tmp);
  439.  
  440.       if (status && !in_group)
  441.     {
  442.       /*  Flush any image updates and displays  */
  443.       gdisp = gdisplay_active();
  444.       if (gdisp != NULL)
  445.         {
  446.           if (gdisp->disp_xoffset || gdisp->disp_yoffset)
  447.         {
  448.           gdk_window_get_size (gdisp->canvas->window, &x, &y);
  449.           if (gdisp->disp_yoffset)
  450.             {
  451.               gdisplay_expose_area (gdisp, 0, 0,
  452.                         gdisp->disp_width,
  453.                         gdisp->disp_yoffset);
  454.               gdisplay_expose_area (gdisp, 0, gdisp->disp_yoffset + y,
  455.                         gdisp->disp_width,
  456.                         gdisp->disp_height);
  457.             }
  458.           if (gdisp->disp_xoffset)
  459.             {
  460.               gdisplay_expose_area (gdisp, 0, 0,
  461.                         gdisp->disp_xoffset,
  462.                         gdisp->disp_height);
  463.               gdisplay_expose_area (gdisp, gdisp->disp_xoffset + x, 0,
  464.                         gdisp->disp_width,
  465.                         gdisp->disp_height);
  466.             }
  467.         }
  468.         }
  469.       gdisplays_flush ();
  470.  
  471.       /*  If the shrink_wrap flag was set  */
  472.       if (shrink_wrap)
  473.         {
  474.           gdisplays_resize_cursor_label (gimage);
  475.           gdisplays_shrink_wrap (gimage);
  476.           shrink_wrap = FALSE;
  477.         }
  478.  
  479.       /* let others know that we just popped an action */
  480.       gimp_image_undo_event (gimage,
  481.                  (state == UNDO)? UNDO_POPPED : UNDO_REDO);
  482.  
  483.       return TRUE;
  484.     }
  485.     }
  486.  
  487.   return FALSE;
  488. }
  489.  
  490.  
  491. gboolean
  492. undo_pop (GImage *gimage)
  493. {
  494.   /* Very bad idea to pop an action off the undo stack if we're in the
  495.    * middle of a group, since the whole group won't be popped.  Might
  496.    * leave unbalanced group start marker earlier in the stack too,
  497.    * causing much confusion when it's later reached and
  498.    * mis-interpreted as a group start.  */
  499.   g_return_val_if_fail (gimage->pushing_undo_group == UNDO_NULL, FALSE);
  500.  
  501.   return pop_stack (gimage, &gimage->undo_stack, &gimage->redo_stack, UNDO);
  502. }
  503.  
  504.  
  505. gboolean
  506. undo_redo (GImage *gimage)
  507. {
  508.   /* ditto for redo stack */
  509.   g_return_val_if_fail (gimage->pushing_undo_group == UNDO_NULL, FALSE);
  510.  
  511.   return pop_stack (gimage, &gimage->redo_stack, &gimage->undo_stack, REDO);
  512. }
  513.  
  514.  
  515.  
  516. /* Return the name of the action that would be used if undo_pop or
  517.  * undo_redo is called, or NULL if there are no actions pushed on the
  518.  * stack.  */
  519. static const gchar *
  520. undo_get_topitem_name (GSList *stack)
  521. {
  522.   Undo *object;
  523.  
  524.   if (!stack)
  525.     return NULL;
  526.  
  527.   object = stack->data;
  528.  
  529.   /* For group boundaries, the type of the boundary marker is the
  530.    * type of the whole group (but each individual action in the
  531.    * group retains its own type so layer/channel unrefs work
  532.    * correctly).
  533.    */
  534.  
  535.   return undo_type_to_name (object->type);
  536. }
  537.  
  538. /* Return the type of the top undo action */
  539. static UndoType
  540. undo_get_topitem_type (GSList *stack)
  541. {
  542.   Undo *object;
  543.  
  544.   if (!stack)
  545.     return UNDO_NULL;
  546.  
  547.   object = stack->data;
  548.  
  549.   /* For group boundaries, the type of the boundary marker is the
  550.    * type of the whole group (but each individual action in the
  551.    * group retains its own type so layer/channel unrefs work
  552.    * correctly).
  553.    */
  554.  
  555.   return object->type;
  556. }
  557.  
  558.  
  559. const gchar *
  560. undo_get_undo_name (GImage *gimage)
  561. {
  562.   g_return_val_if_fail (gimage != NULL, NULL);
  563.  
  564.   /* don't want to encourage undo while a group is open */
  565.   if (gimage->pushing_undo_group != UNDO_NULL)
  566.     return NULL;
  567.  
  568.   return undo_get_topitem_name (gimage->undo_stack);
  569. }
  570.  
  571. UndoType
  572. undo_get_undo_top_type (GImage *gimage)
  573. {
  574.   g_return_val_if_fail (gimage != NULL, UNDO_NULL);
  575.  
  576.   /* don't want to encourage undo while a group is open */
  577.   if (gimage->pushing_undo_group != UNDO_NULL)
  578.     return UNDO_NULL;
  579.  
  580.   return undo_get_topitem_type (gimage->undo_stack);
  581. }
  582.  
  583.  
  584. const gchar *
  585. undo_get_redo_name (GImage *gimage)
  586. {
  587.   g_return_val_if_fail (gimage != NULL, NULL);
  588.  
  589.   /* don't want to encourage redo while a group is open */
  590.   if (gimage->pushing_undo_group != UNDO_NULL)
  591.     return NULL;
  592.  
  593.   return undo_get_topitem_name (gimage->redo_stack);
  594. }
  595.  
  596.  
  597. static void
  598. undo_map_over_stack (GSList      *stack, 
  599.              undo_map_fn  fn, 
  600.              gpointer     data)
  601. {
  602.   gboolean  in_group = FALSE;
  603.   gint      count = 0;
  604.   Undo     *object;
  605.  
  606.   while (stack)
  607.     {
  608.       object = (Undo *) stack->data;
  609.       if (object->group_boundary)
  610.     in_group = !in_group;
  611.  
  612.       /* Keep track of group length.  0 means not in group (or group
  613.        * end marker), 1 is group start marker, >= 2 are group
  614.        * members.
  615.        */
  616.       if (in_group)
  617.     count++;
  618.       else
  619.     count = 0;
  620.  
  621.       /* Is this a group end marker or non-grouped action? */
  622.       if (count == 0)
  623.     {
  624.       if (fn (undo_type_to_name (object->type), data))
  625.         return; /* early termination option exercised */
  626.     }
  627.  
  628.       stack = g_slist_next (stack);
  629.     }
  630. }
  631.  
  632. void
  633. undo_map_over_undo_stack (GImage      *gimage, 
  634.               undo_map_fn  fn, 
  635.               gpointer     data)
  636. {
  637.   /* shouldn't have group open */
  638.   g_return_if_fail (gimage->pushing_undo_group == UNDO_NULL);
  639.   undo_map_over_stack (gimage->undo_stack, fn, data);
  640. }
  641.  
  642. void
  643. undo_map_over_redo_stack (GImage      *gimage, 
  644.               undo_map_fn  fn, 
  645.               gpointer     data)
  646. {
  647.   /* shouldn't have group open */
  648.   g_return_if_fail (gimage->pushing_undo_group == UNDO_NULL);
  649.   undo_map_over_stack (gimage->redo_stack, fn, data);
  650. }
  651.  
  652.  
  653. void
  654. undo_free (GImage *gimage)
  655. {
  656.   undo_free_list (gimage, UNDO, gimage->undo_stack);
  657.   undo_free_list (gimage, REDO, gimage->redo_stack);
  658.  
  659.   gimage->undo_stack  = NULL;
  660.   gimage->redo_stack  = NULL;
  661.   gimage->undo_bytes  = 0;
  662.   gimage->undo_levels = 0;
  663.  
  664.   /* If the image was dirty, but could become clean by redo-ing
  665.    * some actions, then it should now become 'infinitely' dirty.
  666.    * This is because we've just nuked the actions that would allow
  667.    * the image to become clean again.  The only hope for salvation
  668.    * is to save the image now!  -- austin
  669.    */
  670.   if (gimage->dirty < 0)
  671.     gimage->dirty = 10000;
  672.  
  673.   /* The same applies to the case where the image would become clean
  674.    * due to undo actions, but since user can't undo without an undo
  675.    * stack, that's not so much a problem.
  676.    */
  677.   gimp_image_undo_event (gimage, UNDO_FREE);
  678. }
  679.  
  680.  
  681. /**********************/
  682. /*  group Undo funcs  */
  683.  
  684. gboolean
  685. undo_push_group_start (GImage    *gimage,
  686.                UndoType   type)
  687. {
  688.   Undo *boundary_marker;
  689.  
  690.   if (! gimage->undo_on)
  691.     return FALSE;
  692.  
  693.   /* Bad idea to push 0 as the group type, since that won't
  694.    * be recognized as the start of the group later.
  695.    */
  696.   g_return_val_if_fail (type != 0, FALSE);
  697.  
  698.   gimage->group_count ++;
  699.   TRC (("group_start (%s)\n", undo_type_to_name (type)));
  700.  
  701.   /*  If we're already in a group...ignore  */
  702.   if (gimage->group_count > 1)
  703.     return TRUE;
  704.  
  705.   if (gimage->redo_stack)
  706.     {
  707.       undo_free_list (gimage, REDO, gimage->redo_stack);
  708.       gimage->redo_stack = NULL;
  709.       /* If the image was dirty, but could become clean by redo-ing
  710.        * some actions, then it should now become 'infinitely' dirty.
  711.        * This is because we've just nuked the actions that would allow
  712.        * the image to become clean again.  The only hope for salvation
  713.        * is to save the image now!  -- austin
  714.        */
  715.       if (gimage->dirty < 0)
  716.     gimage->dirty = 10000;
  717.     }
  718.  
  719.   if (! undo_free_up_space (gimage))
  720.     return FALSE;
  721.  
  722.   boundary_marker = undo_new (type, sizeof (Undo), FALSE);
  723.   gimage->undo_bytes += boundary_marker->bytes;
  724.   boundary_marker->group_boundary = TRUE;
  725.  
  726.   gimage->pushing_undo_group = type;
  727.   gimage->undo_stack = g_slist_prepend (gimage->undo_stack, boundary_marker);
  728.   gimage->undo_levels++;
  729.  
  730.   return TRUE;
  731. }
  732.  
  733.  
  734. gboolean
  735. undo_push_group_end (GImage *gimage)
  736. {
  737.   Undo *boundary_marker;
  738.  
  739.   if (! gimage->undo_on)
  740.     return FALSE;
  741.  
  742.   gimage->group_count --;
  743.   TRC (("group end\n"));
  744.  
  745.   if (gimage->group_count == 0)
  746.     {
  747.       boundary_marker = undo_new (gimage->pushing_undo_group,
  748.                   sizeof (Undo), FALSE);
  749.       boundary_marker->group_boundary = TRUE;
  750.       gimage->undo_stack = g_slist_prepend (gimage->undo_stack,
  751.                         boundary_marker);
  752.       gimage->undo_bytes += boundary_marker->bytes;
  753.  
  754.       gimage->pushing_undo_group = UNDO_NULL;
  755.  
  756.       /* Do it here, since undo_push doesn't emit this event while in the
  757.        * middle of a group */
  758.       gimp_image_undo_event (gimage, UNDO_PUSHED);
  759.     }
  760.  
  761.   return TRUE;
  762. }
  763.  
  764.  
  765. /*********************************/
  766. /*  Image Undo functions         */
  767.  
  768. typedef struct _ImageUndo ImageUndo;
  769.  
  770. struct _ImageUndo
  771. {
  772.   TileManager  *tiles;
  773.   GimpDrawable *drawable;
  774.   gint          x1, y1, x2, y2;
  775.   gboolean      sparse;
  776. };
  777.  
  778.  
  779. gboolean
  780. undo_push_image (GImage       *gimage,
  781.          GimpDrawable *drawable,
  782.          gint          x1,
  783.          gint          y1,
  784.          gint          x2,
  785.          gint          y2)
  786. {
  787.   glong        size;
  788.   Undo        *new;
  789.   ImageUndo   *image_undo;
  790.   TileManager *tiles;
  791.   PixelRegion  srcPR, destPR;
  792.  
  793.   x1 = CLAMP (x1, 0, drawable_width (drawable));
  794.   y1 = CLAMP (y1, 0, drawable_height (drawable));
  795.   x2 = CLAMP (x2, 0, drawable_width (drawable));
  796.   y2 = CLAMP (y2, 0, drawable_height (drawable));
  797.  
  798.   size = (x2 - x1) * (y2 - y1) * drawable_bytes (drawable) + sizeof (gpointer) * 2;
  799.  
  800.   if ((new = undo_push (gimage, size, IMAGE_UNDO, TRUE)))
  801.     {
  802.       image_undo = g_new (ImageUndo, 1);
  803.  
  804.       /*  If we cannot create a new temp buf--either because our parameters are
  805.        *  degenerate or something else failed, simply return an unsuccessful push.
  806.        */
  807.       tiles = tile_manager_new ((x2 - x1), (y2 - y1), drawable_bytes (drawable));
  808.       pixel_region_init (&srcPR, drawable_data (drawable),
  809.              x1, y1, (x2 - x1), (y2 - y1), FALSE);
  810.       pixel_region_init (&destPR, tiles,
  811.              0, 0, (x2 - x1), (y2 - y1), TRUE);
  812.       copy_region (&srcPR, &destPR);
  813.  
  814.       /*  set the image undo structure  */
  815.       image_undo->tiles = tiles;
  816.       image_undo->drawable = drawable;
  817.       image_undo->x1 = x1;
  818.       image_undo->y1 = y1;
  819.       image_undo->x2 = x2;
  820.       image_undo->y2 = y2;
  821.       image_undo->sparse = FALSE;
  822.  
  823.       new->data      = image_undo;
  824.       new->pop_func  = undo_pop_image;
  825.       new->free_func = undo_free_image;
  826.  
  827.       return TRUE;
  828.     }
  829.   else
  830.     return FALSE;
  831. }
  832.  
  833.  
  834. gboolean
  835. undo_push_image_mod (GImage       *gimage,
  836.              GimpDrawable *drawable,
  837.              gint          x1,
  838.              gint          y1,
  839.              gint          x2,
  840.              gint          y2,
  841.              gpointer      tiles_ptr,
  842.              gboolean      sparse)
  843. {
  844.   glong  size;
  845.   gint   dwidth, dheight;
  846.   Undo  *new;
  847.   ImageUndo   *image_undo;
  848.   TileManager *tiles;
  849.  
  850.   if (! tiles_ptr)
  851.     return FALSE;
  852.  
  853.   dwidth = drawable_width (drawable);
  854.   dheight = drawable_height (drawable);
  855.  
  856.   x1 = CLAMP (x1, 0, dwidth);
  857.   y1 = CLAMP (y1, 0, dheight);
  858.   x2 = CLAMP (x2, 0, dwidth);
  859.   y2 = CLAMP (y2, 0, dheight);
  860.  
  861.   tiles = (TileManager *) tiles_ptr;
  862.   size = tiles->width * tiles->height *
  863.     tiles->bpp + sizeof (gpointer) * 2;
  864.  
  865.   if ((new = undo_push (gimage, size, IMAGE_MOD_UNDO, TRUE)))
  866.     {
  867.       image_undo = g_new (ImageUndo, 1);
  868.       image_undo->tiles    = tiles;
  869.       image_undo->drawable = drawable;
  870.       image_undo->x1       = x1;
  871.       image_undo->y1       = y1;
  872.       image_undo->x2       = x2;
  873.       image_undo->y2       = y2;
  874.       image_undo->sparse   = sparse;
  875.  
  876.       new->data      = image_undo;
  877.       new->pop_func  = undo_pop_image;
  878.       new->free_func = undo_free_image;
  879.  
  880.       return TRUE;
  881.     }
  882.   else
  883.     {
  884.       tile_manager_destroy (tiles);
  885.       return FALSE;
  886.     }
  887. }
  888.  
  889.  
  890. static gboolean
  891. undo_pop_image (GImage    *gimage,
  892.         UndoState  state,
  893.         UndoType   type,
  894.         gpointer   image_undo_ptr)
  895. {
  896.   ImageUndo   *image_undo;
  897.   TileManager *tiles;
  898.   PixelRegion  PR1, PR2;
  899.   gint x, y;
  900.   gint w, h;
  901.  
  902.   image_undo = (ImageUndo *) image_undo_ptr;
  903.   tiles = image_undo->tiles;
  904.  
  905.   /*  some useful values  */
  906.   x = image_undo->x1;
  907.   y = image_undo->y1;
  908.  
  909.   if (image_undo->sparse == FALSE)
  910.     {
  911.       w = tiles->width;
  912.       h = tiles->height;
  913.  
  914.       pixel_region_init (&PR1, tiles,
  915.              0, 0, w, h, TRUE);
  916.       pixel_region_init (&PR2, drawable_data (image_undo->drawable),
  917.              x, y, w, h, TRUE);
  918.  
  919.       /*  swap the regions  */
  920.       swap_region (&PR1, &PR2);
  921.     }
  922.   else
  923.     {
  924.       int i, j;
  925.       Tile *src_tile;
  926.       Tile *dest_tile;
  927.  
  928.       w = image_undo->x2 - image_undo->x1;
  929.       h = image_undo->y2 - image_undo->y1;
  930.  
  931.       for (i = y; i < image_undo->y2; i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  932.     {
  933.       for (j = x; j < image_undo->x2; j += (TILE_WIDTH - (j % TILE_WIDTH)))
  934.         {
  935.           src_tile = tile_manager_get_tile (tiles, j, i, FALSE, FALSE);
  936.           if (tile_is_valid (src_tile) == TRUE)
  937.         {
  938.           /* swap tiles, not pixels! */
  939.  
  940.           src_tile = tile_manager_get_tile (tiles, j, i, TRUE, FALSE /* TRUE */);
  941.           dest_tile = tile_manager_get_tile (drawable_data (image_undo->drawable), j, i, TRUE, FALSE /* TRUE */);
  942.  
  943.           tile_manager_map_tile (tiles, j, i, dest_tile);
  944.           tile_manager_map_tile (drawable_data (image_undo->drawable), j, i, src_tile);
  945. #if 0
  946.           swap_pixels (tile_data_pointer (src_tile, 0, 0),
  947.                    tile_data_pointer (dest_tile, 0, 0),
  948.                    tile_size (src_tile));
  949. #endif
  950.           
  951.           tile_release (dest_tile, FALSE /* TRUE */);
  952.           tile_release (src_tile, FALSE /* TRUE */);
  953.         }
  954.         }
  955.     }
  956.     }
  957.  
  958.   drawable_update (image_undo->drawable, x, y, w, h);
  959.  
  960.   return TRUE;
  961. }
  962.  
  963.  
  964. static void
  965. undo_free_image (UndoState  state,
  966.          UndoType   type,
  967.          gpointer   image_undo_ptr)
  968. {
  969.   ImageUndo *image_undo;
  970.  
  971.   image_undo = (ImageUndo *) image_undo_ptr;
  972.  
  973.   tile_manager_destroy (image_undo->tiles);
  974.   g_free (image_undo);
  975. }
  976.  
  977.  
  978. /******************************/
  979. /*  Mask Undo functions  */
  980.  
  981. gboolean
  982. undo_push_mask (GImage   *gimage,
  983.         gpointer  mask_ptr)
  984. {
  985.   Undo     *new;
  986.   MaskUndo *mask_undo;
  987.   gint      size;
  988.  
  989.   mask_undo = (MaskUndo *) mask_ptr;
  990.  
  991.   if (mask_undo->tiles)
  992.     size = mask_undo->tiles->width * mask_undo->tiles->height;
  993.   else
  994.     size = 0;
  995.  
  996.   if ((new = undo_push (gimage, size, MASK_UNDO, FALSE)))
  997.     {
  998.       new->data          = mask_undo;
  999.       new->pop_func      = undo_pop_mask;
  1000.       new->free_func     = undo_free_mask;
  1001.  
  1002.       return TRUE;
  1003.     }
  1004.   else
  1005.     {
  1006.       if (mask_undo->tiles)
  1007.     tile_manager_destroy (mask_undo->tiles);
  1008.       g_free (mask_undo);
  1009.       return FALSE;
  1010.     }
  1011. }
  1012.  
  1013.  
  1014. static gboolean
  1015. undo_pop_mask (GImage     *gimage,
  1016.            UndoState   state,
  1017.            UndoType    type,
  1018.            gpointer    mask_ptr)
  1019. {
  1020.   MaskUndo    *mask_undo;
  1021.   TileManager *new_tiles;
  1022.   Channel     *sel_mask;
  1023.   PixelRegion  srcPR, destPR;
  1024.   gint   selection;
  1025.   gint   x1, y1, x2, y2;
  1026.   gint   width, height;
  1027.   guchar empty = 0;
  1028.  
  1029.   width = height = 0;
  1030.  
  1031.   mask_undo = (MaskUndo *) mask_ptr;
  1032.  
  1033.   /*  save current selection mask  */
  1034.   sel_mask = gimage_get_mask (gimage);
  1035.   selection = channel_bounds (sel_mask, &x1, &y1, &x2, &y2);
  1036.   pixel_region_init (&srcPR, GIMP_DRAWABLE (sel_mask)->tiles,
  1037.              x1, y1, (x2 - x1), (y2 - y1), FALSE);
  1038.  
  1039.   if (selection)
  1040.     {
  1041.       new_tiles = tile_manager_new (srcPR.w, srcPR.h, 1);
  1042.       pixel_region_init (&destPR, new_tiles,
  1043.              0, 0, srcPR.w, srcPR.h, TRUE);
  1044.  
  1045.       copy_region (&srcPR, &destPR);
  1046.  
  1047.       pixel_region_init (&srcPR, GIMP_DRAWABLE (sel_mask)->tiles,
  1048.              x1, y1, (x2 - x1), (y2 - y1), TRUE);
  1049.       color_region (&srcPR, &empty);
  1050.     }
  1051.   else
  1052.     new_tiles = NULL;
  1053.  
  1054.   if (mask_undo->tiles)
  1055.     {
  1056.       width = mask_undo->tiles->width;
  1057.       height = mask_undo->tiles->height;
  1058.       pixel_region_init (&srcPR, mask_undo->tiles,
  1059.              0, 0, width, height, FALSE);
  1060.       pixel_region_init (&destPR, GIMP_DRAWABLE (sel_mask)->tiles,
  1061.              mask_undo->x, mask_undo->y, width, height, TRUE);
  1062.       copy_region (&srcPR, &destPR);
  1063.  
  1064.       tile_manager_destroy (mask_undo->tiles);
  1065.     }
  1066.  
  1067.   /* invalidate the current bounds and boundary of the mask */
  1068.   gimage_mask_invalidate (gimage);
  1069.  
  1070.   if (mask_undo->tiles)
  1071.     {
  1072.       sel_mask->empty = FALSE;
  1073.       sel_mask->x1 = mask_undo->x;
  1074.       sel_mask->y1 = mask_undo->y;
  1075.       sel_mask->x2 = mask_undo->x + width;
  1076.       sel_mask->y2 = mask_undo->y + height;
  1077.     }
  1078.   else
  1079.     {
  1080.       sel_mask->empty = TRUE;
  1081.       sel_mask->x1 = 0;
  1082.       sel_mask->y1 = 0;
  1083.       sel_mask->x2 = GIMP_DRAWABLE (sel_mask)->width;
  1084.       sel_mask->y2 = GIMP_DRAWABLE (sel_mask)->height;
  1085.     }
  1086.  
  1087.   /*  set the new mask undo parameters  */
  1088.   mask_undo->tiles = new_tiles;
  1089.   mask_undo->x = x1;
  1090.   mask_undo->y = y1;
  1091.  
  1092.   /* we know the bounds */
  1093.   sel_mask->bounds_known = TRUE;
  1094.  
  1095.   /*  if there is a "by color" selection dialog active
  1096.    *  for this gimage's mask, send it an update notice
  1097.    */
  1098.   if (gimage->by_color_select)
  1099.     by_color_select_initialize_by_image (gimage);
  1100.  
  1101.   return TRUE;
  1102. }
  1103.  
  1104.  
  1105. static void
  1106. undo_free_mask (UndoState  state,
  1107.         UndoType   type,
  1108.         gpointer   mask_ptr)
  1109. {
  1110.   MaskUndo *mask_undo;
  1111.  
  1112.   mask_undo = (MaskUndo *) mask_ptr;
  1113.   if (mask_undo->tiles)
  1114.     tile_manager_destroy (mask_undo->tiles);
  1115.   g_free (mask_undo);
  1116. }
  1117.  
  1118.  
  1119. /***************************************/
  1120. /*  Layer displacement Undo functions  */
  1121.  
  1122. typedef struct _LayerDisplayUndo LayerDisplaceUndo;
  1123.  
  1124. struct _LayerDisplayUndo 
  1125. {
  1126.   gint      info[3];
  1127.   PathUndo *path_undo;
  1128. };
  1129.  
  1130. gboolean
  1131. undo_push_layer_displace (GImage    *gimage,
  1132.               GimpLayer *layer)
  1133. {
  1134.   Undo              *new;
  1135.   LayerDisplaceUndo *ldu;
  1136.  
  1137.   if ((new = undo_push (gimage, 12, LAYER_DISPLACE_UNDO, TRUE)))
  1138.     {
  1139.       new->data          = g_new (LayerDisplaceUndo, 1);
  1140.       new->pop_func      = undo_pop_layer_displace;
  1141.       new->free_func     = undo_free_layer_displace;
  1142.  
  1143.       ldu = (LayerDisplaceUndo *) new->data;
  1144.       ldu->info[0] = drawable_ID (GIMP_DRAWABLE (layer));
  1145.       ldu->info[1] = GIMP_DRAWABLE (layer)->offset_x;
  1146.       ldu->info[2] = GIMP_DRAWABLE (layer)->offset_y;
  1147.       ldu->path_undo = path_transform_start_undo (gimage);
  1148.  
  1149.       return TRUE;
  1150.     }
  1151.   else
  1152.     return FALSE;
  1153. }
  1154.  
  1155.  
  1156. static gboolean
  1157. undo_pop_layer_displace (GImage     *gimage,
  1158.              UndoState   state,
  1159.              UndoType    type,
  1160.              gpointer    info_ptr)
  1161. {
  1162.   Layer *layer;
  1163.   gint   old_offsets[2];
  1164.   LayerDisplaceUndo *ldu;
  1165.  
  1166.   ldu = (LayerDisplaceUndo *) info_ptr;
  1167.   layer = layer_get_ID (ldu->info[0]);
  1168.   if (layer)
  1169.     {
  1170.       old_offsets[0] = GIMP_DRAWABLE (layer)->offset_x;
  1171.       old_offsets[1] = GIMP_DRAWABLE (layer)->offset_y;
  1172.       drawable_update (GIMP_DRAWABLE (layer), 0, 0,
  1173.                GIMP_DRAWABLE (layer)->width,
  1174.                GIMP_DRAWABLE (layer)->height);
  1175.  
  1176.       GIMP_DRAWABLE (layer)->offset_x = ldu->info[1];
  1177.       GIMP_DRAWABLE (layer)->offset_y = ldu->info[2];
  1178.       drawable_update (GIMP_DRAWABLE (layer), 0, 0,
  1179.                GIMP_DRAWABLE (layer)->width,
  1180.                GIMP_DRAWABLE (layer)->height);
  1181.       
  1182.       if (layer->mask) 
  1183.     {
  1184.       GIMP_DRAWABLE (layer->mask)->offset_x = ldu->info[1];
  1185.       GIMP_DRAWABLE (layer->mask)->offset_y = ldu->info[2];
  1186.       drawable_update (GIMP_DRAWABLE (layer->mask), 0, 0,
  1187.                GIMP_DRAWABLE (layer->mask)->width,
  1188.                GIMP_DRAWABLE (layer->mask)->height);
  1189.     }
  1190.  
  1191.  
  1192.       /*  invalidate the selection boundary because of a layer modification  */
  1193.       layer_invalidate_boundary (layer);
  1194.  
  1195.       ldu->info[1] = old_offsets[0];
  1196.       ldu->info[2] = old_offsets[1];
  1197.  
  1198.       /* Now undo paths bits */
  1199.       if (ldu->path_undo)
  1200.     path_transform_do_undo (gimage, ldu->path_undo);
  1201.  
  1202.       return TRUE;
  1203.     }
  1204.   else
  1205.     return FALSE;
  1206. }
  1207.  
  1208.  
  1209. static void
  1210. undo_free_layer_displace (UndoState  state,
  1211.               UndoType   type,
  1212.               gpointer   info_ptr)
  1213. {
  1214.   LayerDisplaceUndo * ldu;
  1215.  
  1216.   ldu = (LayerDisplaceUndo *) info_ptr;
  1217.  
  1218.   /* Free mem held for paths undo stuff */
  1219.   if (ldu->path_undo)
  1220.     path_transform_free_undo (ldu->path_undo);  
  1221.  
  1222.   g_free (info_ptr);
  1223. }
  1224.  
  1225.  
  1226. /*********************************/
  1227. /*  Transform Undo               */
  1228.  
  1229. gboolean
  1230. undo_push_transform (GImage   *gimage,
  1231.              gpointer  tu_ptr)
  1232. {
  1233.   Undo *new;
  1234.  
  1235.   if ((new = undo_push (gimage, 
  1236.             sizeof (TransformUndo), TRANSFORM_UNDO, FALSE)))
  1237.     {
  1238.       new->data      = tu_ptr;
  1239.       new->pop_func  = undo_pop_transform;
  1240.       new->free_func = undo_free_transform;
  1241.  
  1242.       return TRUE;
  1243.     }
  1244.   else
  1245.     {
  1246.       g_free (tu_ptr);
  1247.       return FALSE;
  1248.     }
  1249. }
  1250.  
  1251.  
  1252. static gboolean
  1253. undo_pop_transform (GImage    *gimage,
  1254.             UndoState  state,
  1255.             UndoType   type,
  1256.             gpointer   tu_ptr)
  1257. {
  1258.   TransformCore *tc;
  1259.   TransformUndo *tu;
  1260.   TileManager   *temp;
  1261.   gdouble d;
  1262.   gint    i;
  1263.  
  1264.   /* Can't have ANY tool selected - maybe a plugin running */
  1265.   if (active_tool == NULL)
  1266.     return TRUE;
  1267.  
  1268.   tc = (TransformCore *) active_tool->private;
  1269.   tu = (TransformUndo *) tu_ptr;
  1270.  
  1271.   path_transform_do_undo (gimage,tu->path_undo);
  1272.  
  1273.   /*  only pop if the active tool is the tool that pushed this undo  */
  1274.   if (tu->tool_ID != active_tool->ID)
  1275.     return TRUE;
  1276.  
  1277.   /*  swap the transformation information arrays  */
  1278.   for (i = 0; i < TRAN_INFO_SIZE; i++)
  1279.     {
  1280.       d = tu->trans_info[i];
  1281.       tu->trans_info[i] = tc->trans_info[i];
  1282.       tc->trans_info[i] = d;
  1283.     }
  1284.  
  1285.   /*  swap the original buffer--the source buffer for repeated transforms
  1286.    */
  1287.   temp = tu->original;
  1288.   tu->original = tc->original;
  1289.   tc->original = temp;
  1290.  
  1291.   /*  If we're re-implementing the first transform, reactivate tool  */
  1292.   if (state == REDO && tc->original)
  1293.     {
  1294.       active_tool->state = ACTIVE;
  1295.       draw_core_resume (tc->core, active_tool);
  1296.     }
  1297.  
  1298.   return TRUE;
  1299. }
  1300.  
  1301.  
  1302. static void
  1303. undo_free_transform (UndoState   state,
  1304.              UndoType    type,
  1305.              gpointer    tu_ptr)
  1306. {
  1307.   TransformUndo * tu;
  1308.  
  1309.   tu = (TransformUndo *) tu_ptr;
  1310.   if (tu->original)
  1311.     tile_manager_destroy (tu->original);
  1312.   path_transform_free_undo (tu->path_undo);
  1313.   g_free (tu);
  1314. }
  1315.  
  1316.  
  1317. /*********************************/
  1318. /*  Paint Undo                   */
  1319.  
  1320. gboolean
  1321. undo_push_paint (GImage   *gimage,
  1322.          gpointer  pu_ptr)
  1323. {
  1324.   Undo *new;
  1325.  
  1326.   if ((new = undo_push (gimage, 
  1327.             sizeof (PaintUndo), PAINT_UNDO, FALSE)))
  1328.     {
  1329.       new->data      = pu_ptr;
  1330.       new->pop_func  = undo_pop_paint;
  1331.       new->free_func = undo_free_paint;
  1332.  
  1333.       return TRUE;
  1334.     }
  1335.   else
  1336.     {
  1337.       g_free (pu_ptr);
  1338.       return FALSE;
  1339.     }
  1340. }
  1341.  
  1342.  
  1343. static gboolean
  1344. undo_pop_paint (GImage    *gimage,
  1345.         UndoState  state,
  1346.         UndoType   type,
  1347.         gpointer   pu_ptr)
  1348. {
  1349.   PaintCore *pc;
  1350.   PaintUndo *pu;
  1351.   gdouble    tmp;
  1352.  
  1353.   /* Can't have ANY tool selected - maybe a plugin running */
  1354.   if (active_tool == NULL)
  1355.     return TRUE;
  1356.  
  1357.   pc = (PaintCore *) active_tool->private;
  1358.   pu = (PaintUndo *) pu_ptr;
  1359.  
  1360.   /*  only pop if the active tool is the tool that pushed this undo  */
  1361.   if (pu->tool_ID != active_tool->ID)
  1362.     return TRUE;
  1363.  
  1364.   /*  swap the paint core information  */
  1365.   tmp = pc->lastx;
  1366.   pc->lastx = pu->lastx;
  1367.   pu->lastx = tmp;
  1368.  
  1369.   tmp = pc->lasty;
  1370.   pc->lasty = pu->lasty;
  1371.   pu->lasty = tmp;
  1372.  
  1373.   tmp = pc->lastpressure;
  1374.   pc->lastpressure = pu->lastpressure;
  1375.   pu->lastpressure = tmp;
  1376.  
  1377.   tmp = pc->lastxtilt;
  1378.   pc->lastxtilt = pu->lastxtilt;
  1379.   pu->lastxtilt = tmp;
  1380.  
  1381.   tmp = pc->lastytilt;
  1382.   pc->lastytilt = pu->lastytilt;
  1383.   pu->lastytilt = tmp;
  1384.  
  1385.   return TRUE;
  1386. }
  1387.  
  1388.  
  1389. static void
  1390. undo_free_paint (UndoState  state,
  1391.          UndoType   type,
  1392.          gpointer   pu_ptr)
  1393. {
  1394.   PaintUndo *pu;
  1395.  
  1396.   pu = (PaintUndo *) pu_ptr;
  1397.   g_free (pu);
  1398. }
  1399.  
  1400.  
  1401. /*********************************/
  1402. /*  New Layer Undo               */
  1403.  
  1404. gboolean
  1405. undo_push_layer (GImage   *gimage,
  1406.          UndoType  type,
  1407.          gpointer  lu_ptr)
  1408. {
  1409.   LayerUndo *lu;
  1410.   Undo      *new;
  1411.   gint       size;
  1412.  
  1413.   lu = (LayerUndo *) lu_ptr;
  1414.  
  1415.   g_return_val_if_fail (type == LAYER_ADD_UNDO ||
  1416.             type == LAYER_REMOVE_UNDO,
  1417.             FALSE);
  1418.  
  1419.   size = layer_size (lu->layer) + sizeof (LayerUndo);
  1420.  
  1421.   if ((new = undo_push (gimage, size, type, TRUE)))
  1422.     {
  1423.       new->data      = lu_ptr;
  1424.       new->pop_func  = undo_pop_layer;
  1425.       new->free_func = undo_free_layer;
  1426.  
  1427.       return TRUE;
  1428.     }
  1429.   else
  1430.     {
  1431.       /*  if this is a remove layer, delete the layer  */
  1432.       if (type == LAYER_REMOVE_UNDO)
  1433.     layer_unref (lu->layer);
  1434.       g_free (lu);
  1435.       return FALSE;
  1436.     }
  1437. }
  1438.  
  1439.  
  1440. static gboolean
  1441. undo_pop_layer (GImage    *gimage,
  1442.         UndoState  state,
  1443.         UndoType   type,
  1444.         gpointer   lu_ptr)
  1445. {
  1446.   LayerUndo *lu;
  1447.  
  1448.   lu = (LayerUndo *) lu_ptr;
  1449.  
  1450.   /*  remove layer  */
  1451.   if ((state == UNDO && type == LAYER_ADD_UNDO) ||
  1452.       (state == REDO && type == LAYER_REMOVE_UNDO))
  1453.     {
  1454.       /*  record the current position  */
  1455.       lu->prev_position = gimage_get_layer_index (gimage, lu->layer);
  1456.       /*  set the previous layer  */
  1457.       gimage_set_active_layer (gimage, lu->prev_layer);
  1458.  
  1459.       /*  remove the layer  */
  1460.       gimage->layers = g_slist_remove (gimage->layers, lu->layer);
  1461.       gimage->layer_stack = g_slist_remove (gimage->layer_stack, lu->layer);
  1462.  
  1463.       /*  reset the gimage values  */
  1464.       if (layer_is_floating_sel (lu->layer))
  1465.     {
  1466.       gimage->floating_sel = NULL;
  1467.       /*  reset the old drawable  */
  1468.       floating_sel_reset (lu->layer);
  1469.     }
  1470.  
  1471.       drawable_update (GIMP_DRAWABLE (lu->layer), 0, 0,
  1472.                GIMP_DRAWABLE (lu->layer)->width,
  1473.                GIMP_DRAWABLE (lu->layer)->height);
  1474.     }
  1475.   /*  restore layer  */
  1476.   else
  1477.     {
  1478.       /*  record the active layer  */
  1479.       lu->prev_layer = gimage->active_layer;
  1480.  
  1481.       /*  hide the current selection--for all views  */
  1482.       if (gimage->active_layer != NULL)
  1483.     layer_invalidate_boundary ((gimage->active_layer));
  1484.  
  1485.       /*  if this is a floating selection, set the fs pointer  */
  1486.       if (layer_is_floating_sel (lu->layer))
  1487.     gimage->floating_sel = lu->layer;
  1488.  
  1489.       /*  add the new layer  */
  1490.       gimage->layers = g_slist_insert (gimage->layers, lu->layer,
  1491.                        lu->prev_position);
  1492.       gimage->layer_stack = g_slist_prepend (gimage->layer_stack, lu->layer);
  1493.       gimage->active_layer = lu->layer;
  1494.  
  1495.       drawable_update (GIMP_DRAWABLE (lu->layer), 0, 0,
  1496.                GIMP_DRAWABLE (lu->layer)->width,
  1497.                GIMP_DRAWABLE (lu->layer)->height);
  1498.     }
  1499.  
  1500.   return TRUE;
  1501. }
  1502.  
  1503.  
  1504. static void
  1505. undo_free_layer (UndoState  state,
  1506.          UndoType   type,
  1507.          gpointer   lu_ptr)
  1508. {
  1509.   LayerUndo *lu;
  1510.  
  1511.   lu = (LayerUndo *) lu_ptr;
  1512.  
  1513.   /*  Only free the layer if we're freeing from the redo
  1514.    *  stack and it's a layer add, or if we're freeing from
  1515.    *  the undo stack and it's a layer remove
  1516.    */
  1517.   if ((state == REDO && type == LAYER_ADD_UNDO) ||
  1518.       (state == UNDO && type == LAYER_REMOVE_UNDO))
  1519.     layer_unref (lu->layer);
  1520.  
  1521.   g_free (lu);
  1522. }
  1523.  
  1524.  
  1525. /*********************************/
  1526. /*  Layer Mod Undo               */
  1527.  
  1528. gboolean
  1529. undo_push_layer_mod (GImage   *gimage,
  1530.              gpointer  layer_ptr)
  1531. {
  1532.   Layer       *layer;
  1533.   Undo        *new;
  1534.   TileManager *tiles;
  1535.   gpointer    *data;
  1536.   gint         size;
  1537.  
  1538.   layer = (Layer *) layer_ptr;
  1539.  
  1540.   tiles    =  GIMP_DRAWABLE (layer)->tiles;
  1541.   tiles->x =  GIMP_DRAWABLE (layer)->offset_x;
  1542.   tiles->y =  GIMP_DRAWABLE (layer)->offset_y;
  1543.   size     = (GIMP_DRAWABLE (layer)->width * GIMP_DRAWABLE (layer)->height * 
  1544.           GIMP_DRAWABLE (layer)->bytes + sizeof (gpointer) * 3);
  1545.  
  1546.   if ((new = undo_push (gimage, size, LAYER_MOD, TRUE)))
  1547.     {
  1548.       data           = g_new (gpointer, 3);
  1549.       new->data      = data;
  1550.       new->pop_func  = undo_pop_layer_mod;
  1551.       new->free_func = undo_free_layer_mod;
  1552.  
  1553.       data[0] = layer_ptr;
  1554.       data[1] = (gpointer) tiles;
  1555.       data[2] = (gpointer) ((glong) GIMP_DRAWABLE (layer)->type);
  1556.  
  1557.       return TRUE;
  1558.     }
  1559.   else
  1560.     {
  1561.       tile_manager_destroy (tiles);
  1562.       return FALSE;
  1563.     }
  1564. }
  1565.  
  1566.  
  1567. static gboolean
  1568. undo_pop_layer_mod (GImage    *gimage,
  1569.             UndoState  state,
  1570.             UndoType   type,
  1571.             gpointer   data_ptr)
  1572. {
  1573.   gpointer    *data;
  1574.   gint         layer_type;
  1575.   TileManager *tiles;
  1576.   TileManager *temp;
  1577.   Layer       *layer;
  1578.  
  1579.   data = (gpointer *) data_ptr;
  1580.   layer = (Layer *) data[0];
  1581.  
  1582.   tiles = (TileManager *) data[1];
  1583.  
  1584.   /*  Issue the first update  */
  1585.   gdisplays_update_area (gimage,
  1586.              GIMP_DRAWABLE (layer)->offset_x,
  1587.              GIMP_DRAWABLE (layer)->offset_y,
  1588.              GIMP_DRAWABLE (layer)->width,
  1589.              GIMP_DRAWABLE (layer)->height);
  1590.  
  1591.   /*  Create a tile manager to store the current layer contents  */
  1592.   temp       = GIMP_DRAWABLE (layer)->tiles;
  1593.   temp->x    = GIMP_DRAWABLE (layer)->offset_x;
  1594.   temp->y    = GIMP_DRAWABLE (layer)->offset_y;
  1595.   layer_type = (glong) data[2];
  1596.   data[2]    = (gpointer) ((glong) GIMP_DRAWABLE (layer)->type);
  1597.  
  1598.   /*  restore the layer's data  */
  1599.   GIMP_DRAWABLE (layer)->tiles     = tiles;
  1600.   GIMP_DRAWABLE (layer)->offset_x  = tiles->x;
  1601.   GIMP_DRAWABLE (layer)->offset_y  = tiles->y;
  1602.   GIMP_DRAWABLE (layer)->width     = tiles->width;
  1603.   GIMP_DRAWABLE (layer)->height    = tiles->height;
  1604.   GIMP_DRAWABLE (layer)->bytes     = tiles->bpp;
  1605.   GIMP_DRAWABLE (layer)->type      = layer_type;
  1606.   GIMP_DRAWABLE (layer)->has_alpha = GIMP_IMAGE_TYPE_HAS_ALPHA (layer_type);
  1607.  
  1608.   if (layer->mask) 
  1609.     {
  1610.       GIMP_DRAWABLE (layer->mask)->offset_x = tiles->x;
  1611.       GIMP_DRAWABLE (layer->mask)->offset_y = tiles->y;
  1612.     }
  1613.  
  1614.   /*  If the layer type changed, update the gdisplay titles  */
  1615.   if (GIMP_DRAWABLE (layer)->type != (glong) data[2])
  1616.     gdisplays_update_title (GIMP_DRAWABLE (layer)->gimage);
  1617.  
  1618.   /*  Set the new tile manager  */
  1619.   data[1] = temp;
  1620.  
  1621.   /*  Issue the second update  */
  1622.   drawable_update (GIMP_DRAWABLE (layer), 0, 0, 
  1623.            GIMP_DRAWABLE (layer)->width,
  1624.            GIMP_DRAWABLE(layer)->height);
  1625.  
  1626.   return TRUE;
  1627. }
  1628.  
  1629.  
  1630. static void
  1631. undo_free_layer_mod (UndoState  state,
  1632.              UndoType   type,
  1633.              gpointer   data_ptr)
  1634. {
  1635.   gpointer *data;
  1636.  
  1637.   data = (gpointer *) data_ptr;
  1638.   tile_manager_destroy ((TileManager *) data[1]);
  1639.   g_free (data);
  1640. }
  1641.  
  1642.  
  1643. /*********************************/
  1644. /*  Layer Mask Undo               */
  1645.  
  1646. gboolean
  1647. undo_push_layer_mask (GImage   *gimage,
  1648.               UndoType  type,
  1649.               gpointer  lmu_ptr)
  1650. {
  1651.   LayerMaskUndo *lmu;
  1652.   Undo *new;
  1653.   gint  size;
  1654.  
  1655.   lmu = (LayerMaskUndo *) lmu_ptr;
  1656.  
  1657.   g_return_val_if_fail (type == LAYER_MASK_ADD_UNDO ||
  1658.             type == LAYER_MASK_REMOVE_UNDO,
  1659.             FALSE);
  1660.  
  1661.   size = channel_size (GIMP_CHANNEL (lmu->mask)) + sizeof (LayerMaskUndo);
  1662.  
  1663.   if ((new = undo_push (gimage, size, type, TRUE)))
  1664.     {
  1665.       new->data      = lmu_ptr;
  1666.       new->pop_func  = undo_pop_layer_mask;
  1667.       new->free_func = undo_free_layer_mask;
  1668.  
  1669.       return TRUE;
  1670.     }
  1671.   else
  1672.     {
  1673.       if (type == LAYER_MASK_REMOVE_UNDO)
  1674.     layer_mask_delete (lmu->mask);
  1675.       g_free (lmu);
  1676.       return FALSE;
  1677.     }
  1678. }
  1679.  
  1680.  
  1681. static gboolean
  1682. undo_pop_layer_mask (GImage    *gimage,
  1683.              UndoState  state,
  1684.              UndoType   type,
  1685.              gpointer   lmu_ptr)
  1686. {
  1687.   LayerMaskUndo *lmu;
  1688.  
  1689.   lmu = (LayerMaskUndo *) lmu_ptr;
  1690.  
  1691.   /*  remove layer mask  */
  1692.   if ((state == UNDO && type == LAYER_MASK_ADD_UNDO) ||
  1693.       (state == REDO && type == LAYER_MASK_REMOVE_UNDO))
  1694.     {
  1695.       /*  remove the layer mask  */
  1696.       lmu->layer->mask       = NULL;
  1697.       lmu->layer->apply_mask = FALSE;
  1698.       lmu->layer->edit_mask  = FALSE;
  1699.       lmu->layer->show_mask  = FALSE;
  1700.  
  1701.       /*  if this is redoing a remove operation &
  1702.        *  the mode of application was DISCARD or
  1703.        *  this is undoing an add...
  1704.        */
  1705.       if ((state == REDO && lmu->mode == DISCARD) || state == UNDO)
  1706.     drawable_update (GIMP_DRAWABLE (lmu->layer), 0, 0, 
  1707.              GIMP_DRAWABLE (lmu->layer)->width,
  1708.              GIMP_DRAWABLE (lmu->layer)->height);
  1709.     }
  1710.   /*  restore layer  */
  1711.   else
  1712.     {
  1713.       lmu->layer->mask       = lmu->mask;
  1714.       lmu->layer->apply_mask = lmu->apply_mask;
  1715.       lmu->layer->edit_mask  = lmu->edit_mask;
  1716.       lmu->layer->show_mask  = lmu->show_mask;
  1717.  
  1718.       gimage_set_layer_mask_edit (gimage, lmu->layer, lmu->edit_mask);
  1719.  
  1720.       /*  if this is undoing a remove operation &
  1721.        *  the mode of application was DISCARD or
  1722.        *  this is redoing an add
  1723.        */
  1724.       if ((state == UNDO && lmu->mode == DISCARD) || state == REDO)
  1725.     drawable_update (GIMP_DRAWABLE (lmu->layer), 0, 0, 
  1726.              GIMP_DRAWABLE (lmu->layer)->width,
  1727.              GIMP_DRAWABLE (lmu->layer)->height);
  1728.     }
  1729.  
  1730.   return TRUE;
  1731. }
  1732.  
  1733.  
  1734. static void
  1735. undo_free_layer_mask (UndoState  state,
  1736.               UndoType   type,
  1737.               gpointer   lmu_ptr)
  1738. {
  1739.   LayerMaskUndo *lmu;
  1740.  
  1741.   lmu = (LayerMaskUndo *) lmu_ptr;
  1742.  
  1743.   /*  Only free the layer mask if we're freeing from the redo
  1744.    *  stack and it's a layer add, or if we're freeing from
  1745.    *  the undo stack and it's a layer remove
  1746.    */
  1747.   if ((state == REDO && type == LAYER_MASK_ADD_UNDO) ||
  1748.       (state == UNDO && type == LAYER_MASK_REMOVE_UNDO))
  1749.     layer_mask_delete (lmu->mask);
  1750.  
  1751.   g_free (lmu);
  1752. }
  1753.  
  1754.  
  1755. /*********************************/
  1756. /*  New Channel Undo             */
  1757.  
  1758. gboolean
  1759. undo_push_channel (GImage   *gimage,
  1760.            UndoType  type,
  1761.            gpointer  cu_ptr)
  1762. {
  1763.   ChannelUndo *cu;
  1764.   Undo *new;
  1765.   gint  size;
  1766.  
  1767.   g_return_val_if_fail (type == CHANNEL_ADD_UNDO ||
  1768.             type == CHANNEL_REMOVE_UNDO,
  1769.             FALSE);
  1770.  
  1771.   cu = (ChannelUndo *) cu_ptr;
  1772.  
  1773.   size = channel_size (cu->channel) + sizeof (ChannelUndo);
  1774.  
  1775.   if ((new = undo_push (gimage, size, type, TRUE)))
  1776.     {
  1777.       new->data      = cu_ptr;
  1778.       new->pop_func  = undo_pop_channel;
  1779.       new->free_func = undo_free_channel;
  1780.  
  1781.       return TRUE;
  1782.     }
  1783.   else
  1784.     {
  1785.       if (type == CHANNEL_REMOVE_UNDO)
  1786.     channel_delete (cu->channel);
  1787.       g_free (cu);
  1788.       return FALSE;
  1789.     }
  1790. }
  1791.  
  1792.  
  1793. static gboolean
  1794. undo_pop_channel (GImage    *gimage,
  1795.           UndoState  state,
  1796.           UndoType   type,
  1797.           gpointer   cu_ptr)
  1798. {
  1799.   ChannelUndo *cu;
  1800.  
  1801.   cu = (ChannelUndo *) cu_ptr;
  1802.  
  1803.   /*  remove channel  */
  1804.   if ((state == UNDO && type == CHANNEL_ADD_UNDO) ||
  1805.       (state == REDO && type == CHANNEL_REMOVE_UNDO))
  1806.     {
  1807.       /*  record the current position  */
  1808.       cu->prev_position = gimage_get_channel_index (gimage, cu->channel);
  1809.  
  1810.       /*  remove the channel  */
  1811.       gimage->channels = g_slist_remove (gimage->channels, cu->channel);
  1812.  
  1813.       /*  set the previous channel  */
  1814.       gimage_set_active_channel (gimage, cu->prev_channel);
  1815.  
  1816.       /*  update the area  */
  1817.       drawable_update (GIMP_DRAWABLE (cu->channel), 0, 0, 
  1818.                GIMP_DRAWABLE (cu->channel)->width,
  1819.                GIMP_DRAWABLE (cu->channel)->height);
  1820.     }
  1821.   /*  restore channel  */
  1822.   else
  1823.     {
  1824.       /*  record the active channel  */
  1825.       cu->prev_channel = gimage->active_channel;
  1826.  
  1827.       /*  add the new channel  */
  1828.       gimage->channels = g_slist_insert (gimage->channels, cu->channel,
  1829.                      cu->prev_position);
  1830.  
  1831.       /*  set the new channel  */
  1832.       gimage_set_active_channel (gimage, cu->channel);
  1833.  
  1834.       /*  update the area  */
  1835.       drawable_update (GIMP_DRAWABLE (cu->channel), 0, 0, 
  1836.                GIMP_DRAWABLE (cu->channel)->width,
  1837.                GIMP_DRAWABLE (cu->channel)->height);
  1838.     }
  1839.  
  1840.   return TRUE;
  1841. }
  1842.  
  1843.  
  1844. static void
  1845. undo_free_channel (UndoState  state,
  1846.            UndoType   type,
  1847.            gpointer   cu_ptr)
  1848. {
  1849.   ChannelUndo *cu;
  1850.  
  1851.   cu = (ChannelUndo *) cu_ptr;
  1852.  
  1853.   /*  Only free the channel if we're freeing from the redo
  1854.    *  stack and it's a channel add, or if we're freeing from
  1855.    *  the undo stack and it's a channel remove
  1856.    */
  1857.   if ((state == REDO && type == CHANNEL_ADD_UNDO) ||
  1858.       (state == UNDO && type == CHANNEL_REMOVE_UNDO))
  1859.     channel_delete (cu->channel);
  1860.  
  1861.   g_free (cu);
  1862. }
  1863.  
  1864.  
  1865.  
  1866. /*********************************/
  1867. /*  Channel Mod Undo               */
  1868.  
  1869. gboolean
  1870. undo_push_channel_mod (GImage   *gimage,
  1871.                gpointer  channel_ptr)
  1872. {
  1873.   Channel     *channel;
  1874.   TileManager *tiles;
  1875.   Undo        *new;
  1876.   gpointer    *data;
  1877.   gint         size;
  1878.  
  1879.   channel = (Channel *) channel_ptr;
  1880.  
  1881.   tiles = GIMP_DRAWABLE (channel)->tiles;
  1882.   size  = GIMP_DRAWABLE (channel)->width * GIMP_DRAWABLE (channel)->height +
  1883.     sizeof (gpointer) * 2;
  1884.  
  1885.   if ((new = undo_push (gimage, size, CHANNEL_MOD, TRUE)))
  1886.     {
  1887.       data           = g_new (gpointer, 2);
  1888.       new->data      = data;
  1889.       new->pop_func  = undo_pop_channel_mod;
  1890.       new->free_func = undo_free_channel_mod;
  1891.  
  1892.       data[0] = channel_ptr;
  1893.       data[1] = (gpointer) tiles;
  1894.  
  1895.       return TRUE;
  1896.     }
  1897.   else
  1898.     {
  1899.       tile_manager_destroy (tiles);
  1900.       return FALSE;
  1901.     }
  1902. }
  1903.  
  1904.  
  1905. static gboolean
  1906. undo_pop_channel_mod (GImage    *gimage,
  1907.               UndoState  state,
  1908.               UndoType   type,
  1909.               gpointer   data_ptr)
  1910. {
  1911.   gpointer    *data;
  1912.   TileManager *tiles;
  1913.   TileManager *temp;
  1914.   Channel     *channel;
  1915.  
  1916.   data = (gpointer *) data_ptr;
  1917.   channel = (Channel *) data[0];
  1918.  
  1919.   tiles = (TileManager *) data[1];
  1920.  
  1921.   /*  Issue the first update  */
  1922.   drawable_update (GIMP_DRAWABLE (channel), 0, 0, 
  1923.            GIMP_DRAWABLE (channel)->width,
  1924.            GIMP_DRAWABLE (channel)->height);
  1925.  
  1926.   temp = GIMP_DRAWABLE (channel)->tiles;
  1927.   GIMP_DRAWABLE (channel)->tiles = tiles;
  1928.   GIMP_DRAWABLE (channel)->width = tiles->width;
  1929.   GIMP_DRAWABLE (channel)->height = tiles->height;
  1930.   GIMP_CHANNEL (channel)->bounds_known = FALSE; /* #4840. set to False because bounds 
  1931.                                                   reflect previous tile set */
  1932.  
  1933.   /*  Set the new buffer  */
  1934.   data[1] = temp;
  1935.  
  1936.   /*  Issue the second update  */
  1937.   drawable_update (GIMP_DRAWABLE (channel), 0, 0, 
  1938.            GIMP_DRAWABLE (channel)->width,
  1939.            GIMP_DRAWABLE (channel)->height);
  1940.  
  1941.   return TRUE;
  1942. }
  1943.  
  1944.  
  1945. static void
  1946. undo_free_channel_mod (UndoState  state,
  1947.                UndoType   type,
  1948.                gpointer   data_ptr)
  1949. {
  1950.   gpointer *data;
  1951.  
  1952.   data = (gpointer *) data_ptr;
  1953.   tile_manager_destroy ((TileManager *) data[1]);
  1954.   g_free (data);
  1955. }
  1956.  
  1957.  
  1958. /**************************************/
  1959. /*  Floating Selection to Layer Undo  */
  1960.  
  1961. gboolean
  1962. undo_push_fs_to_layer (GImage   *gimage,
  1963.                gpointer  fsu_ptr)
  1964. {
  1965.   FStoLayerUndo *fsu;
  1966.   Undo *new;
  1967.  
  1968.   fsu = (FStoLayerUndo *) fsu_ptr;
  1969.  
  1970.   if ((new = undo_push (gimage, 
  1971.             sizeof (FStoLayerUndo), FS_TO_LAYER_UNDO, TRUE)))
  1972.     {
  1973.       new->data      = fsu_ptr;
  1974.       new->pop_func  = undo_pop_fs_to_layer;
  1975.       new->free_func = undo_free_fs_to_layer;
  1976.  
  1977.       return TRUE;
  1978.     }
  1979.   else
  1980.     {
  1981.       tile_manager_destroy (fsu->layer->fs.backing_store);
  1982.       g_free (fsu);
  1983.       return FALSE;
  1984.     }
  1985. }
  1986.  
  1987.  
  1988. static gboolean
  1989. undo_pop_fs_to_layer (GImage    *gimage,
  1990.               UndoState  state,
  1991.               UndoType   type,
  1992.               gpointer   fsu_ptr)
  1993. {
  1994.   FStoLayerUndo *fsu;
  1995.  
  1996.   fsu = (FStoLayerUndo *) fsu_ptr;
  1997.  
  1998.   switch (state)
  1999.     {
  2000.     case UNDO:
  2001.       /*  Update the preview for the floating sel  */
  2002.       gimp_drawable_invalidate_preview (GIMP_DRAWABLE (fsu->layer), TRUE);
  2003.  
  2004.       fsu->layer->fs.drawable = fsu->drawable;
  2005.       gimage->active_layer = fsu->layer;
  2006.       gimage->floating_sel = fsu->layer;
  2007.  
  2008.       /*  restore the contents of the drawable  */
  2009.       floating_sel_store (fsu->layer, 
  2010.               GIMP_DRAWABLE (fsu->layer)->offset_x, 
  2011.               GIMP_DRAWABLE (fsu->layer)->offset_y,
  2012.               GIMP_DRAWABLE (fsu->layer)->width, 
  2013.               GIMP_DRAWABLE (fsu->layer)->height);
  2014.       fsu->layer->fs.initial = TRUE;
  2015.  
  2016.       /*  clear the selection  */
  2017.       layer_invalidate_boundary (fsu->layer);
  2018.  
  2019.       /*  Update the preview for the gimage and underlying drawable  */
  2020.       gimp_drawable_invalidate_preview (GIMP_DRAWABLE (fsu->layer), TRUE);
  2021.       break;
  2022.  
  2023.     case REDO:
  2024.       /*  restore the contents of the drawable  */
  2025.       floating_sel_restore (fsu->layer, 
  2026.                 GIMP_DRAWABLE (fsu->layer)->offset_x, 
  2027.                 GIMP_DRAWABLE (fsu->layer)->offset_y,
  2028.                 GIMP_DRAWABLE (fsu->layer)->width, 
  2029.                 GIMP_DRAWABLE (fsu->layer)->height);
  2030.  
  2031.       /*  Update the preview for the gimage and underlying drawable  */
  2032.       gimp_drawable_invalidate_preview (GIMP_DRAWABLE (fsu->layer), TRUE);
  2033.  
  2034.       /*  clear the selection  */
  2035.       layer_invalidate_boundary (fsu->layer);
  2036.  
  2037.       /*  update the pointers  */
  2038.       fsu->layer->fs.drawable = NULL;
  2039.       gimage->floating_sel = NULL;
  2040.  
  2041.       /*  Update the fs drawable  */
  2042.       gimp_drawable_invalidate_preview (GIMP_DRAWABLE (fsu->layer), TRUE);
  2043.       break;
  2044.     }
  2045.  
  2046.   return TRUE;
  2047. }
  2048.  
  2049.  
  2050. static void
  2051. undo_free_fs_to_layer (UndoState  state,
  2052.                UndoType   type,
  2053.                gpointer   fsu_ptr)
  2054. {
  2055.   FStoLayerUndo *fsu;
  2056.  
  2057.   fsu = (FStoLayerUndo *) fsu_ptr;
  2058.  
  2059.   if (state == UNDO)
  2060.     tile_manager_destroy (fsu->layer->fs.backing_store);
  2061.   g_free (fsu);
  2062. }
  2063.  
  2064.  
  2065. /***********************************/
  2066. /*  Floating Selection Rigor Undo  */
  2067.  
  2068. gboolean
  2069. undo_push_fs_rigor (GImage *gimage,
  2070.             gint   layer_ID)
  2071. {
  2072.   Undo *new;
  2073.  
  2074.   if ((new = undo_push (gimage, 
  2075.             sizeof (gint32), FS_RIGOR, FALSE)))
  2076.     {
  2077.       new->data      = g_new (gint32, 1);
  2078.       new->pop_func  = undo_pop_fs_rigor;
  2079.       new->free_func = undo_free_fs_rigor;
  2080.  
  2081.       *((gint32 *) new->data) = layer_ID;
  2082.  
  2083.       return TRUE;
  2084.     }
  2085.   else
  2086.     return FALSE;
  2087. }
  2088.  
  2089.  
  2090. static gboolean
  2091. undo_pop_fs_rigor (GImage    *gimage,
  2092.            UndoState  state,
  2093.            UndoType   type,
  2094.            gpointer   layer_ptr)
  2095. {
  2096.   gint32  layer_ID;
  2097.   Layer  *floating_layer;
  2098.  
  2099.   layer_ID = *((gint32 *) layer_ptr);
  2100.  
  2101.   if ((floating_layer = layer_get_ID (layer_ID)) == NULL)
  2102.     return FALSE;
  2103.  
  2104.   if (! layer_is_floating_sel (floating_layer))
  2105.     return FALSE;
  2106.  
  2107.   switch (state)
  2108.     {
  2109.     case UNDO:
  2110.       /*  restore the contents of drawable the floating layer is attached to  */
  2111.       if (floating_layer->fs.initial == FALSE)
  2112.     floating_sel_restore (floating_layer,
  2113.                   GIMP_DRAWABLE (floating_layer)->offset_x, 
  2114.                   GIMP_DRAWABLE (floating_layer)->offset_y,
  2115.                   GIMP_DRAWABLE (floating_layer)->width, 
  2116.                   GIMP_DRAWABLE (floating_layer)->height);
  2117.       floating_layer->fs.initial = TRUE;
  2118.       break;
  2119.  
  2120.     case REDO:
  2121.       /*  store the affected area from the drawable in the backing store  */
  2122.       floating_sel_store (floating_layer,
  2123.               GIMP_DRAWABLE (floating_layer)->offset_x, 
  2124.               GIMP_DRAWABLE (floating_layer)->offset_y,
  2125.               GIMP_DRAWABLE (floating_layer)->width, 
  2126.               GIMP_DRAWABLE (floating_layer)->height);
  2127.       floating_layer->fs.initial = TRUE;
  2128.       break;
  2129.     }
  2130.  
  2131.   return TRUE;
  2132. }
  2133.  
  2134.  
  2135. static void
  2136. undo_free_fs_rigor (UndoState  state,
  2137.             UndoType   type,
  2138.             gpointer   layer_ptr)
  2139. {
  2140.   g_free (layer_ptr);
  2141. }
  2142.  
  2143.  
  2144. /***********************************/
  2145. /*  Floating Selection Relax Undo  */
  2146.  
  2147. gboolean
  2148. undo_push_fs_relax (GImage *gimage,
  2149.             gint32  layer_ID)
  2150. {
  2151.   Undo *new;
  2152.  
  2153.   if ((new = undo_push (gimage, 
  2154.             sizeof (gint32), FS_RELAX, FALSE)))
  2155.     {
  2156.       new->data      = g_new (gint32, 1);
  2157.       new->pop_func  = undo_pop_fs_relax;
  2158.       new->free_func = undo_free_fs_relax;
  2159.  
  2160.       *((gint32 *) new->data) = layer_ID;
  2161.  
  2162.       return TRUE;
  2163.     }
  2164.   else
  2165.     return FALSE;
  2166. }
  2167.  
  2168.  
  2169. static gboolean
  2170. undo_pop_fs_relax (GImage    *gimage,
  2171.            UndoState  state,
  2172.            UndoType   type,
  2173.            gpointer   layer_ptr)
  2174. {
  2175.   gint32  layer_ID;
  2176.   Layer  *floating_layer;
  2177.  
  2178.   layer_ID = *((gint32 *) layer_ptr);
  2179.  
  2180.   if ((floating_layer = layer_get_ID (layer_ID)) == NULL)
  2181.     return FALSE;
  2182.  
  2183.   if (! layer_is_floating_sel (floating_layer))
  2184.     return FALSE;
  2185.  
  2186.   switch (state)
  2187.     {
  2188.     case UNDO:
  2189.       /*  store the affected area from the drawable in the backing store  */
  2190.       floating_sel_store (floating_layer,
  2191.               GIMP_DRAWABLE (floating_layer)->offset_x, 
  2192.               GIMP_DRAWABLE (floating_layer)->offset_y,
  2193.               GIMP_DRAWABLE (floating_layer)->width, 
  2194.               GIMP_DRAWABLE (floating_layer)->height);
  2195.       floating_layer->fs.initial = TRUE;
  2196.       break;
  2197.  
  2198.     case REDO:
  2199.       /*  restore the contents of drawable the floating layer is attached to  */
  2200.       if (floating_layer->fs.initial == FALSE)
  2201.     floating_sel_restore (floating_layer,
  2202.                   GIMP_DRAWABLE (floating_layer)->offset_x, 
  2203.                   GIMP_DRAWABLE (floating_layer)->offset_y,
  2204.                   GIMP_DRAWABLE (floating_layer)->width, 
  2205.                   GIMP_DRAWABLE (floating_layer)->height);
  2206.       floating_layer->fs.initial = TRUE;
  2207.       break;
  2208.     }
  2209.  
  2210.   return TRUE;
  2211. }
  2212.  
  2213.  
  2214. static void
  2215. undo_free_fs_relax (UndoState  state,
  2216.             UndoType   type,
  2217.             gpointer   layer_ptr)
  2218. {
  2219.   g_free (layer_ptr);
  2220. }
  2221.  
  2222.  
  2223. /*********************/
  2224. /*  GImage Mod Undo  */
  2225.  
  2226. gboolean
  2227. undo_push_gimage_mod (GImage *gimage)
  2228. {
  2229.   Undo *new;
  2230.   gint *data;
  2231.  
  2232.   if ((new = undo_push (gimage, 
  2233.             sizeof (gint) * 3, GIMAGE_MOD, TRUE)))
  2234.     {
  2235.       data           = g_new (gint, 3);
  2236.       new->data      = data;
  2237.       new->pop_func  = undo_pop_gimage_mod;
  2238.       new->free_func = undo_free_gimage_mod;
  2239.  
  2240.       data[0] = gimage->width;
  2241.       data[1] = gimage->height;
  2242.       data[2] = gimage->base_type;
  2243.  
  2244.       return TRUE;
  2245.     }
  2246.  
  2247.   return FALSE;
  2248. }
  2249.  
  2250.  
  2251. static gboolean
  2252. undo_pop_gimage_mod (GImage    *gimage,
  2253.              UndoState  state,
  2254.              UndoType   type,
  2255.              gpointer   data_ptr)
  2256. {
  2257.   gint *data;
  2258.   gint  tmp;
  2259.  
  2260.   data = (int *) data_ptr;
  2261.   tmp = data[0];
  2262.   data[0] = gimage->width;
  2263.   gimage->width = tmp;
  2264.   tmp = data[1];
  2265.   data[1] = gimage->height;
  2266.   gimage->height = tmp;
  2267.   tmp = data[2];
  2268.   data[2] = gimage->base_type;
  2269.   gimage->base_type = tmp;
  2270.  
  2271.   gimage_projection_realloc (gimage);
  2272.  
  2273.   gimage_mask_invalidate (gimage);
  2274.   channel_invalidate_previews (gimage);
  2275.   layer_invalidate_previews (gimage);
  2276.   gimage_invalidate_preview (gimage);
  2277.   gdisplays_update_full (gimage);
  2278.   gdisplays_update_title (gimage);
  2279.  
  2280.   gimp_image_colormap_changed (gimage, -1);
  2281.  
  2282.   if (gimage->width != (int) data[0] || gimage->height != (int) data[1])
  2283.     shrink_wrap = TRUE;
  2284.  
  2285.   return TRUE;
  2286. }
  2287.  
  2288.  
  2289. static void
  2290. undo_free_gimage_mod (UndoState  state,
  2291.               UndoType   type,
  2292.               gpointer   data_ptr)
  2293. {
  2294.   g_free (data_ptr);
  2295. }
  2296.  
  2297. /***********/
  2298. /*  Qmask  */
  2299.  
  2300. typedef struct _QmaskUndo QmaskUndo;
  2301.  
  2302. struct _QmaskUndo
  2303. {
  2304.   GImage   *gimage;
  2305.   gboolean  qmask;
  2306. };
  2307.  
  2308.  
  2309. gboolean
  2310. undo_push_qmask (GImage *gimage)
  2311. {
  2312.   Undo      *new;
  2313.   QmaskUndo *data;
  2314.  
  2315.   if ((new = undo_push (gimage, 
  2316.             sizeof (QmaskUndo), QMASK_UNDO, TRUE)))
  2317.     {
  2318.       data = g_new (QmaskUndo, 1);
  2319.       new->data      = data;
  2320.       new->pop_func  = undo_pop_qmask;
  2321.       new->free_func = undo_free_qmask;
  2322.  
  2323.       data->gimage = gimage;
  2324.       data->qmask  = gimage->qmask_state;
  2325.  
  2326.       return TRUE;
  2327.     }
  2328.  
  2329.   return FALSE;
  2330. }
  2331.  
  2332. static gboolean
  2333. undo_pop_qmask (GImage    *gimage,
  2334.         UndoState  state,
  2335.         UndoType   type,
  2336.         gpointer   data_ptr)
  2337. {
  2338.   QmaskUndo *data;
  2339.   gint       tmp;
  2340.  
  2341.   data = (QmaskUndo *) data_ptr;
  2342.   
  2343.   tmp = gimage->qmask_state;
  2344.   gimage->qmask_state = data->qmask;
  2345.   data->qmask = tmp;
  2346.   
  2347.   /*  make sure the buttons on all displays are updated  */
  2348.   gdisplays_flush ();
  2349.  
  2350.   return TRUE;
  2351. }
  2352.  
  2353.  
  2354. static void
  2355. undo_free_qmask (UndoState  state,
  2356.          UndoType   type,
  2357.          gpointer   data_ptr)
  2358. {
  2359.   g_free (data_ptr);
  2360. }
  2361.  
  2362. /***********/
  2363. /*  Guide  */
  2364.  
  2365. typedef struct _GuideUndo GuideUndo;
  2366.  
  2367. struct _GuideUndo
  2368. {
  2369.   GImage *gimage;
  2370.   Guide  *guide;
  2371.   Guide   orig;
  2372. };
  2373.  
  2374. gboolean
  2375. undo_push_guide (GImage   *gimage,
  2376.          gpointer  guide)
  2377. {
  2378.   Undo      *new;
  2379.   GuideUndo *data;
  2380.  
  2381.   if ((new = undo_push (gimage, 
  2382.             sizeof (GuideUndo), GUIDE_UNDO, TRUE)))
  2383.     {
  2384.       ((Guide *)(guide))->ref_count++;
  2385.       
  2386.       data           = g_new (GuideUndo, 1);
  2387.       new->data      = data;
  2388.       new->pop_func  = undo_pop_guide;
  2389.       new->free_func = undo_free_guide;
  2390.  
  2391.       data->gimage = gimage;
  2392.       data->guide  = guide;
  2393.       data->orig   = *(data->guide);
  2394.  
  2395.       return TRUE;
  2396.     }
  2397.  
  2398.   return FALSE;
  2399. }
  2400.  
  2401.  
  2402. static gboolean
  2403. undo_pop_guide (GImage    *gimage,
  2404.         UndoState  state,
  2405.         UndoType   type,
  2406.         gpointer   data_ptr)
  2407. {
  2408.   GuideUndo *data;
  2409.   Guide      tmp;
  2410.   gint       tmp_ref;
  2411.  
  2412.   data = data_ptr;
  2413.  
  2414.   gdisplays_expose_guide (gimage, data->guide);
  2415.  
  2416.   tmp_ref = data->guide->ref_count;
  2417.   tmp = *(data->guide);
  2418.   *(data->guide) = data->orig;
  2419.   data->guide->ref_count = tmp_ref;
  2420.   data->orig = tmp;
  2421.  
  2422.   gdisplays_expose_guide (gimage, data->guide);
  2423.  
  2424.   return TRUE;
  2425. }
  2426.  
  2427.  
  2428. static void
  2429. undo_free_guide (UndoState  state,
  2430.          UndoType   type,
  2431.          gpointer   data_ptr)
  2432. {
  2433.   GuideUndo *data;
  2434.  
  2435.   data = data_ptr;
  2436.  
  2437.   data->guide->ref_count--;
  2438.   if (data->guide->position < 0 && data->guide->ref_count <= 0)
  2439.     {
  2440.       gimp_image_remove_guide (data->gimage, data->guide);
  2441.       g_free (data->guide);
  2442.     }
  2443.   g_free (data);
  2444. }
  2445.  
  2446. /****************/
  2447. /*  Resolution  */
  2448.  
  2449. typedef struct _ResolutionUndo ResolutionUndo;
  2450.  
  2451. struct _ResolutionUndo
  2452. {
  2453.   gdouble  xres;
  2454.   gdouble  yres;
  2455.   GimpUnit unit;
  2456. };
  2457.  
  2458. gboolean
  2459. undo_push_resolution (GImage *gimage)
  2460. {
  2461.   Undo           *new;
  2462.   ResolutionUndo *data;
  2463.  
  2464.   if ((new = undo_push (gimage, 
  2465.             sizeof (ResolutionUndo), RESOLUTION_UNDO, TRUE)))
  2466.     {
  2467.       data           = g_new (ResolutionUndo, 1);
  2468.       new->data      = data;
  2469.       new->pop_func  = undo_pop_resolution;
  2470.       new->free_func = undo_free_resolution;
  2471.  
  2472.       data->xres = gimage->xresolution;
  2473.       data->yres = gimage->yresolution;
  2474.       data->unit = gimage->unit;
  2475.  
  2476.       return TRUE;
  2477.     }
  2478.  
  2479.   return FALSE;
  2480. }
  2481.  
  2482. static gboolean
  2483. undo_pop_resolution (GImage    *gimage,
  2484.              UndoState  state,
  2485.              UndoType   type,
  2486.              gpointer   data_ptr)
  2487. {
  2488.   ResolutionUndo *data;
  2489.   gdouble         tmpres;
  2490.   GimpUnit        tmpunit;
  2491.  
  2492.   data = data_ptr;
  2493.  
  2494.   tmpres = gimage->xresolution;
  2495.   gimage->xresolution = data->xres;
  2496.   data->xres = tmpres;
  2497.  
  2498.   tmpres = gimage->yresolution;
  2499.   gimage->yresolution = data->yres;
  2500.   data->yres = tmpres;
  2501.  
  2502.   if (data->unit != gimage->unit)
  2503.     {
  2504.       tmpunit = gimage->unit;
  2505.       gimage->unit = data->unit;
  2506.       data->unit = tmpunit;
  2507.     }
  2508.  
  2509.   /* really just want to recalc size and repaint */
  2510.   shrink_wrap = TRUE;
  2511.  
  2512.   return TRUE;
  2513. }
  2514.  
  2515. static void
  2516. undo_free_resolution (UndoState  state,
  2517.               UndoType   type,
  2518.               gpointer   data_ptr)
  2519. {
  2520.   g_free (data_ptr);
  2521. }
  2522.  
  2523.  
  2524. /************/
  2525. /* Parasite */
  2526.  
  2527. typedef struct _ParasiteUndo ParasiteUndo;
  2528.  
  2529. struct _ParasiteUndo
  2530. {
  2531.   GImage       *gimage;
  2532.   GimpDrawable *drawable;
  2533.   GimpParasite *parasite;
  2534.   gchar        *name;
  2535. };
  2536.  
  2537. gboolean
  2538. undo_push_image_parasite (GImage   *gimage,
  2539.               gpointer  parasite)
  2540. {
  2541.   Undo         *new;
  2542.   ParasiteUndo *data;
  2543.  
  2544.   if ((new = undo_push (gimage, 
  2545.             sizeof (ParasiteUndo), PARASITE_ATTACH_UNDO, TRUE)))
  2546.     {
  2547.       data           = g_new (ParasiteUndo, 1);
  2548.       new->data      = data;
  2549.       new->pop_func  = undo_pop_parasite;
  2550.       new->free_func = undo_free_parasite;
  2551.  
  2552.       data->gimage   = gimage;
  2553.       data->drawable = NULL;
  2554.       data->name     = g_strdup (gimp_parasite_name (parasite));
  2555.       data->parasite = gimp_parasite_copy (gimp_image_parasite_find (gimage, data->name));
  2556.  
  2557.       return TRUE;
  2558.     }
  2559.  
  2560.   return FALSE;
  2561. }
  2562.  
  2563. gboolean
  2564. undo_push_image_parasite_remove (GImage      *gimage,
  2565.                  const gchar *name)
  2566. {
  2567.   Undo         *new;
  2568.   ParasiteUndo *data;
  2569.  
  2570.   if ((new = undo_push (gimage, 
  2571.             sizeof (ParasiteUndo), PARASITE_REMOVE_UNDO, TRUE)))
  2572.     {
  2573.       data           = g_new (ParasiteUndo, 1);
  2574.       new->data      = data;
  2575.       new->pop_func  = undo_pop_parasite;
  2576.       new->free_func = undo_free_parasite;
  2577.  
  2578.       data->gimage = gimage;
  2579.       data->drawable = NULL;
  2580.       data->name     = g_strdup (name);
  2581.       data->parasite = gimp_parasite_copy (gimp_image_parasite_find (gimage, data->name));
  2582.  
  2583.       return TRUE;
  2584.     }
  2585.  
  2586.   return FALSE;
  2587. }
  2588.  
  2589. gboolean
  2590. undo_push_drawable_parasite (GImage       *gimage,
  2591.                  GimpDrawable *drawable,
  2592.                  gpointer      parasite)
  2593. {
  2594.   Undo         *new;
  2595.   ParasiteUndo *data;
  2596.  
  2597.   if ((new = undo_push (gimage, 
  2598.             sizeof (ParasiteUndo), GIMAGE_MOD, TRUE)))
  2599.     {
  2600.       data           = g_new (ParasiteUndo, 1);
  2601.       new->data      = data;
  2602.       new->pop_func  = undo_pop_parasite;
  2603.       new->free_func = undo_free_parasite;
  2604.  
  2605.       data->gimage   = NULL;
  2606.       data->drawable = drawable;
  2607.       data->name     = g_strdup (gimp_parasite_name (parasite));
  2608.       data->parasite = gimp_parasite_copy (gimp_drawable_parasite_find (drawable, data->name));
  2609.       return TRUE;
  2610.     }
  2611.  
  2612.   return FALSE;
  2613. }
  2614.  
  2615. gboolean
  2616. undo_push_drawable_parasite_remove (GImage       *gimage,
  2617.                     GimpDrawable *drawable,
  2618.                     const gchar  *name)
  2619. {
  2620.   Undo         *new;
  2621.   ParasiteUndo *data;
  2622.  
  2623.   if ((new = undo_push (gimage, 
  2624.             sizeof (ParasiteUndo), GIMAGE_MOD, TRUE)))
  2625.     {
  2626.       data           = g_new (ParasiteUndo, 1);
  2627.       new->data      = data;
  2628.       new->pop_func  = undo_pop_parasite;
  2629.       new->free_func = undo_free_parasite;
  2630.  
  2631.       data->gimage   = NULL;
  2632.       data->drawable = drawable;
  2633.       data->name     = g_strdup (name);
  2634.       data->parasite = gimp_parasite_copy (gimp_drawable_parasite_find (drawable, data->name));
  2635.       return TRUE;
  2636.     }
  2637.  
  2638.   return FALSE;
  2639. }
  2640.  
  2641.  
  2642. static gboolean
  2643. undo_pop_parasite (GImage    *gimage,
  2644.            UndoState  state,
  2645.            UndoType   type,
  2646.            gpointer   data_ptr)
  2647. {
  2648.   ParasiteUndo *data;
  2649.   GimpParasite *tmp;
  2650.  
  2651.   data = data_ptr;
  2652.  
  2653.   tmp = data->parasite;
  2654.   
  2655.   if (data->gimage)
  2656.     {
  2657.       data->parasite =
  2658.     gimp_parasite_copy (gimp_image_parasite_find (gimage, data->name));
  2659.  
  2660.       if (tmp)
  2661.     parasite_list_add (data->gimage->parasites, tmp);
  2662.       else
  2663.     parasite_list_remove (data->gimage->parasites, data->name);
  2664.     }
  2665.   else if (data->drawable)
  2666.     {
  2667.       data->parasite =
  2668.     gimp_parasite_copy (gimp_drawable_parasite_find (data->drawable,
  2669.                              data->name));
  2670.       if (tmp)
  2671.     parasite_list_add (data->drawable->parasites, tmp);
  2672.       else
  2673.     parasite_list_remove (data->drawable->parasites, data->name);
  2674.     }
  2675.   else
  2676.     {
  2677.       data->parasite = gimp_parasite_copy (gimp_parasite_find (data->name));
  2678.       if (tmp)
  2679.     gimp_parasite_attach (tmp);
  2680.       else
  2681.     gimp_parasite_detach (data->name);
  2682.     }
  2683.     
  2684.   if (tmp)
  2685.     gimp_parasite_free (tmp);
  2686.  
  2687.   return TRUE;
  2688. }
  2689.  
  2690.  
  2691. static void
  2692. undo_free_parasite (UndoState  state,
  2693.             UndoType   type,
  2694.             gpointer   data_ptr)
  2695. {
  2696.   ParasiteUndo *data;
  2697.  
  2698.   data = data_ptr;
  2699.  
  2700.   if (data->parasite)
  2701.     gimp_parasite_free (data->parasite);
  2702.   if (data->name)
  2703.     g_free (data->name);
  2704.  
  2705.   g_free (data_ptr);
  2706. }
  2707.  
  2708.  
  2709.  
  2710. /*************/
  2711. /* Layer re-position */
  2712.  
  2713. typedef struct _LayerRepositionUndo LayerRepositionUndo;
  2714.  
  2715. struct _LayerRepositionUndo
  2716. {
  2717.   GimpLayer *layer;
  2718.   gint       old_position;
  2719. };
  2720.  
  2721. gboolean
  2722. undo_push_layer_reposition (GImage    *gimage, 
  2723.                 GimpLayer *layer)
  2724. {
  2725.   Undo                *new;
  2726.   LayerRepositionUndo *data;
  2727.  
  2728.   if ((new = undo_push (gimage, 
  2729.             sizeof (LayerRepositionUndo), LAYER_REPOSITION_UNDO, TRUE)))
  2730.     {
  2731.       data           = g_new (LayerRepositionUndo, 1);
  2732.       new->data      = data;
  2733.       new->pop_func  = undo_pop_layer_reposition;
  2734.       new->free_func = undo_free_layer_reposition;
  2735.  
  2736.       data->layer        = layer;
  2737.       data->old_position = g_slist_index (gimage->layers, layer);
  2738.  
  2739.       return TRUE;
  2740.     }
  2741.  
  2742.   return FALSE;
  2743. }
  2744.  
  2745. static gboolean
  2746. undo_pop_layer_reposition (GImage    *gimage,
  2747.                UndoState  state,
  2748.                UndoType   type,
  2749.                void      *data_ptr)
  2750. {
  2751.   LayerRepositionUndo *data = data_ptr;
  2752.   gint tmp;
  2753.  
  2754.   /* what's the layer's current index? */
  2755.   tmp = g_slist_index (gimage->layers, data->layer);
  2756.  
  2757.   gimp_image_position_layer (gimage, data->layer, data->old_position, FALSE);
  2758.  
  2759.   data->old_position = tmp;
  2760.  
  2761.   return TRUE;
  2762. }
  2763.  
  2764. static void
  2765. undo_free_layer_reposition (UndoState  state,
  2766.                 UndoType   type,
  2767.                 void      *data_ptr)
  2768. {
  2769.   g_free (data_ptr);
  2770. }
  2771.  
  2772.  
  2773. /*************/
  2774. /* Layer name change */
  2775.  
  2776. typedef struct _LayerRenameUndo LayerRenameUndo;
  2777.  
  2778. struct _LayerRenameUndo
  2779. {
  2780.   GimpLayer *layer;
  2781.   gchar     *old_name;
  2782. };
  2783.  
  2784. gboolean
  2785. undo_push_layer_rename (GImage    *gimage, 
  2786.             GimpLayer *layer)
  2787. {
  2788.   Undo            *new;
  2789.   LayerRenameUndo *data;
  2790.  
  2791.   if ((new = undo_push (gimage, 
  2792.             sizeof (LayerRenameUndo), LAYER_RENAME_UNDO, TRUE)))
  2793.     {
  2794.       data           = g_new (LayerRenameUndo, 1);
  2795.       new->data      = data;
  2796.       new->pop_func  = undo_pop_layer_rename;
  2797.       new->free_func = undo_free_layer_rename;
  2798.  
  2799.       data->layer    = layer;
  2800.       data->old_name = g_strdup (layer_get_name (layer));
  2801.  
  2802.       return TRUE;
  2803.     }
  2804.  
  2805.   return FALSE;
  2806. }
  2807.  
  2808. static gboolean
  2809. undo_pop_layer_rename (GImage    *gimage,
  2810.                UndoState  state,
  2811.                UndoType   type,
  2812.                void      *data_ptr)
  2813. {
  2814.   LayerRenameUndo *data = data_ptr;
  2815.   gchar *tmp;
  2816.  
  2817.   tmp = g_strdup (layer_get_name (data->layer));
  2818.   layer_set_name (data->layer, data->old_name);
  2819.   g_free (data->old_name);
  2820.   data->old_name = tmp;
  2821.  
  2822.   return TRUE;
  2823. }
  2824.  
  2825. static void
  2826. undo_free_layer_rename (UndoState  state,
  2827.             UndoType   type,
  2828.             gpointer   data_ptr)
  2829. {
  2830.   LayerRenameUndo *data = data_ptr;
  2831.  
  2832.   g_free (data->old_name);
  2833.   g_free (data);
  2834. }
  2835.  
  2836.  
  2837.  
  2838. /*************/
  2839. /* Something for which programmer is too lazy to write an undo 
  2840.  * function for.
  2841.  */
  2842.  
  2843. gboolean
  2844. undo_push_cantundo (GImage      *gimage,
  2845.             const gchar *action)
  2846. {
  2847.   Undo *new;
  2848.  
  2849.   /* This is the sole purpose of this type of undo: the ability to
  2850.    * mark an image as having been mutated, without really providing
  2851.    * any adequate undo facility. */
  2852.  
  2853.   new = undo_push (gimage, 0, GIMAGE_MOD, TRUE);
  2854.   if (!new)
  2855.     return FALSE;
  2856.  
  2857.   new->data      = (void*) action;
  2858.   new->pop_func  = undo_pop_cantundo;
  2859.   new->free_func = undo_free_cantundo;
  2860.  
  2861.   return TRUE;
  2862. }
  2863.  
  2864. static gboolean
  2865. undo_pop_cantundo (GImage    *gimage,
  2866.            UndoState  state,
  2867.            UndoType   type,
  2868.            gpointer   data_ptr)
  2869. {
  2870.   gchar *action = data_ptr;
  2871.  
  2872.   switch (state)
  2873.     {
  2874.     case UNDO:
  2875.       g_message (_("Can't undo %s"), action);
  2876.       break;
  2877.  
  2878.     case REDO:
  2879.       break;
  2880.     }
  2881.  
  2882.   return TRUE;
  2883. }
  2884.  
  2885.  
  2886. static void
  2887. undo_free_cantundo (UndoState  state,
  2888.             UndoType   type,
  2889.             gpointer   data_ptr)
  2890. {
  2891.   /* nothing to free */
  2892. }
  2893.  
  2894.  
  2895.  
  2896.  
  2897. /* A "ok" to the name means I've checked where it's used and
  2898.    it seems plausible.  -- austin 23/9/99 */
  2899. static struct undo_name_t
  2900. {
  2901.   UndoType     type;
  2902.   const gchar *name;
  2903. }
  2904. undo_name[] =
  2905. {
  2906.   { UNDO_NULL,             N_("<<invalid>>") },
  2907.   { IMAGE_UNDO,                N_("image") },
  2908.   { IMAGE_MOD_UNDO,        N_("image mod") },
  2909.   { MASK_UNDO,            N_("mask") },
  2910.   { LAYER_DISPLACE_UNDO,    N_("layer move") }, /* ok */
  2911.   { TRANSFORM_UNDO,        N_("transform") },
  2912.   { PAINT_UNDO,                N_("paint") },
  2913.   { LAYER_ADD_UNDO,        N_("new layer") },
  2914.   { LAYER_REMOVE_UNDO,        N_("delete layer") },
  2915.   { LAYER_MOD,            N_("layer mod") },
  2916.   { LAYER_MASK_ADD_UNDO,    N_("add layer mask") },  /* ok */
  2917.   { LAYER_MASK_REMOVE_UNDO,    N_("delete layer mask") }, /* ok */
  2918.   { LAYER_RENAME_UNDO,        N_("rename layer") },
  2919.   { LAYER_REPOSITION_UNDO,    N_("layer reposition") }, /* ok */
  2920.   { CHANNEL_ADD_UNDO,        N_("new channel") },
  2921.   { CHANNEL_REMOVE_UNDO,    N_("delete channel") },
  2922.   { CHANNEL_MOD,        N_("channel mod") },
  2923.   { FS_TO_LAYER_UNDO,        N_("FS to layer") }, /* ok */
  2924.   { GIMAGE_MOD,                N_("gimage") },
  2925.   { FS_RIGOR,            N_("FS rigor") },
  2926.   { FS_RELAX,            N_("FS relax") },
  2927.   { GUIDE_UNDO,                N_("guide") },
  2928.   { TEXT_UNDO,            N_("text") },
  2929.   { FLOAT_MASK_UNDO,        N_("float selection") },
  2930.   { EDIT_PASTE_UNDO,        N_("paste") },
  2931.   { EDIT_CUT_UNDO,        N_("cut") },
  2932.   { TRANSFORM_CORE_UNDO,    N_("transform core") },
  2933.   { PAINT_CORE_UNDO,        N_("paint core") },
  2934.   { FLOATING_LAYER_UNDO,    N_("floating layer") }, /* unused! */
  2935.   { LINKED_LAYER_UNDO,        N_("linked layer") },
  2936.   { LAYER_APPLY_MASK_UNDO,    N_("apply layer mask") }, /* ok */
  2937.   { LAYER_MERGE_UNDO,        N_("layer merge") },
  2938.   { FS_ANCHOR_UNDO,        N_("FS anchor") },
  2939.   { GIMAGE_MOD_UNDO,        N_("gimage mod") },
  2940.   { CROP_UNDO,            N_("crop") },
  2941.   { LAYER_SCALE_UNDO,        N_("layer scale") },
  2942.   { LAYER_RESIZE_UNDO,        N_("layer resize") },
  2943.   { QMASK_UNDO,                N_("quickmask") },
  2944.   { PARASITE_ATTACH_UNDO,    N_("attach parasite") },
  2945.   { PARASITE_REMOVE_UNDO,    N_("remove parasite") },
  2946.   { RESOLUTION_UNDO,        N_("resolution change") },
  2947.   { IMAGE_SCALE_UNDO,        N_("image scale") },
  2948.   { IMAGE_RESIZE_UNDO,        N_("image resize") },
  2949.   { MISC_UNDO,            N_("misc") }
  2950. };
  2951. #define NUM_NAMES (sizeof (undo_name) / sizeof (struct undo_name_t))
  2952.  
  2953.  
  2954. static const gchar *
  2955. undo_type_to_name (UndoType type)
  2956. {
  2957.   gint i;
  2958.  
  2959.   for (i=0; i < NUM_NAMES; i++)
  2960.     if (undo_name[i].type == type)
  2961.       return gettext (undo_name[i].name);
  2962.  
  2963.   /* no name found */
  2964.   return "";
  2965. }
  2966.