home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / imagedit / imagundo.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  4KB  |  172 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4. /*                           All Rights Reserved                            */
  5. /*                                                                          */
  6. /****************************************************************************/
  7. /****************************** Module Header *******************************
  8. * Module Name: imagundo.c
  9. *
  10. * Contains routines for handling the Undo buffers.
  11. *
  12. * History:
  13. *
  14. ****************************************************************************/
  15.  
  16. #include "imagedit.h"
  17.  
  18.  
  19. STATICFN VOID NEAR ImageAllocUndo(VOID);
  20.  
  21.  
  22.  
  23. /************************************************************************
  24. * ImageUndo
  25. *
  26. * Undoes the last editing operation by restoring the image to the
  27. * saved undo buffer.
  28. *
  29. * History:
  30. *
  31. ************************************************************************/
  32.  
  33. VOID ImageUndo(VOID)
  34. {
  35.     HDC hdcTemp;
  36.     HBITMAP hbmOld;
  37.  
  38.     /*
  39.      * Is there anything to undo?
  40.      */
  41.     if (!ghbmUndo)
  42.         return;
  43.  
  44.     hdcTemp = CreateCompatibleDC(ghdcImage);
  45.     hbmOld = SelectObject(hdcTemp, ghbmUndo);
  46.     BitBlt(ghdcImage, 0, 0, gcxImage, gcyImage, hdcTemp, 0, 0, SRCCOPY);
  47.  
  48.     /*
  49.      * For icons and cursors, restore the AND mask also.
  50.      */
  51.     if (giType != FT_BITMAP) {
  52.         SelectObject(hdcTemp, ghbmUndoMask);
  53.         BitBlt(ghdcANDMask, 0, 0, gcxImage, gcyImage, hdcTemp, 0, 0, SRCCOPY);
  54.     }
  55.  
  56.     SelectObject(hdcTemp, hbmOld);
  57.     DeleteDC(hdcTemp);
  58.  
  59.     fImageDirty = TRUE;
  60.  
  61.     /*
  62.      * Delete the undo buffer, now that it has been used.
  63.      */
  64.     ImageFreeUndo();
  65.  
  66.     ViewUpdate();
  67. }
  68.  
  69.  
  70.  
  71. /************************************************************************
  72. * ImageUpdateUndo
  73. *
  74. * Makes a snapshot of the current image and places it in the undo
  75. * buffer.
  76. *
  77. * Arguments:
  78. *
  79. * History:
  80. *
  81. ************************************************************************/
  82.  
  83. VOID ImageUpdateUndo(VOID)
  84. {
  85.     HDC hdcTemp;
  86.     HBITMAP hbmOld;
  87.  
  88.     /*
  89.      * If there are currently no undo buffers, allocate them now.
  90.      */
  91.     if (!ghbmUndo)
  92.         ImageAllocUndo();
  93.  
  94.     hdcTemp = CreateCompatibleDC(ghdcImage);
  95.     hbmOld = SelectObject(hdcTemp, ghbmUndo);
  96.     BitBlt(hdcTemp, 0, 0, gcxImage, gcyImage, ghdcImage, 0, 0, SRCCOPY);
  97.  
  98.     /*
  99.      * For icons and cursors, update the undo AND mask also.
  100.      */
  101.     if (giType != FT_BITMAP) {
  102.         SelectObject(hdcTemp, ghbmUndoMask);
  103.         BitBlt(hdcTemp, 0, 0, gcxImage, gcyImage, ghdcANDMask, 0, 0, SRCCOPY);
  104.     }
  105.  
  106.     SelectObject(hdcTemp, hbmOld);
  107.     DeleteDC(hdcTemp);
  108. }
  109.  
  110.  
  111.  
  112. /************************************************************************
  113. * ImageAllocUndo
  114. *
  115. * Allocates buffers for an undo operation.  For icons and cursors,
  116. * this includes an AND mask undo buffer.  This function does not
  117. * initialize the bits.  The function ImageFreeUndo frees the buffers
  118. * allocated by this function.
  119. *
  120. * History:
  121. *
  122. ************************************************************************/
  123.  
  124. STATICFN VOID NEAR ImageAllocUndo(VOID)
  125. {
  126.     ImageFreeUndo();
  127.  
  128.     /*
  129.      * Allocate an undo bitmap of the specified size.
  130.      */
  131.     if (!(ghbmUndo = MyCreateBitmap(ghdcImage, gcxImage, gcyImage, 16))) {
  132.         Message(MSG_OUTOFMEMORY);
  133.         return;
  134.     }
  135.  
  136.     /*
  137.      * For icons and cursors, allocate an undo AND mask also.
  138.      */
  139.     if (giType != FT_BITMAP) {
  140.         if (!(ghbmUndoMask = CreateBitmap(gcxImage, gcyImage,
  141.                 (BYTE)1, (BYTE)1, NULL))) {
  142.             ImageFreeUndo();
  143.             Message(MSG_OUTOFMEMORY);
  144.             return;
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150.  
  151. /************************************************************************
  152. * ImageFreeUndo
  153. *
  154. * Free's the undo buffers.
  155. *
  156. * History:
  157. *
  158. ************************************************************************/
  159.  
  160. VOID ImageFreeUndo(VOID)
  161. {
  162.     if (ghbmUndo) {
  163.         DeleteObject(ghbmUndo);
  164.         ghbmUndo = NULL;
  165.     }
  166.  
  167.     if (ghbmUndoMask) {
  168.         DeleteObject(ghbmUndoMask);
  169.         ghbmUndoMask = NULL;
  170.     }
  171. }
  172.