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 / imaglink.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  6KB  |  228 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4. /*                           All Rights Reserved                            */
  5. /*                                                                          */
  6. /****************************************************************************/
  7. /****************************** Module Header *******************************
  8. * Module Name: imaglink.c
  9. *
  10. * Contains routines for managing the linked list of images.
  11. *
  12. * History:
  13. *
  14. ****************************************************************************/
  15.  
  16. #include "imagedit.h"
  17.  
  18.  
  19.  
  20. /************************************************************************
  21. * ImageLinkAlloc
  22. *
  23. * Allocates an image link.  This is a node in the linked list of
  24. * images for the current file.  This link will be added to the
  25. * current linked list of images.
  26. *
  27. * Arguments:
  28. *   PDEVICE pDevice  - Pointer to the device structure.
  29. *   INT cx           - Width of the image.
  30. *   INT cy           - Height of the image.
  31. *   INT xHotSpot     - X location of the hotspot (cursors only).
  32. *   INT yHotSpot     - Y location of the hotspot (cursors only).
  33. *   INT nColors      - Number of colors (2 or 16).
  34. *
  35. * History:
  36. *
  37. ************************************************************************/
  38.  
  39. PIMAGEINFO ImageLinkAlloc(
  40.     PDEVICE pDevice,
  41.     INT cx,
  42.     INT cy,
  43.     INT xHotSpot,
  44.     INT yHotSpot,
  45.     INT nColors)
  46. {
  47.     PIMAGEINFO pImage;
  48.     PIMAGEINFO pImageT;
  49.  
  50.     if (!(pImage = (PIMAGEINFO)MyAlloc(sizeof(IMAGEINFO))))
  51.         return NULL;
  52.  
  53.     pImage->pDevice = pDevice;
  54.     pImage->cx = cx;
  55.     pImage->cy = cy;
  56.     pImage->iHotspotX = xHotSpot;
  57.     pImage->iHotspotY = yHotSpot;
  58.     pImage->nColors = nColors;
  59.     pImage->DIBSize = 0;
  60.     pImage->DIBhandle = NULL;
  61.     pImage->DIBPtr = NULL;
  62.     pImage->pImageNext = NULL;
  63.  
  64.     /*
  65.      * Insert the link in the list.
  66.      */
  67.     if (!gpImageHead) {
  68.         /*
  69.          * This is the first one.  Start the list.
  70.          */
  71.         gpImageHead = pImage;
  72.     }
  73.     else {
  74.         /*
  75.          * Find the end of the list and tack on the new link.
  76.          */
  77.         for (pImageT = gpImageHead; pImageT->pImageNext;
  78.                 pImageT = pImageT->pImageNext)
  79.             ;
  80.  
  81.         pImageT->pImageNext = pImage;
  82.     }
  83.  
  84.     return pImage;
  85. }
  86.  
  87.  
  88.  
  89. /************************************************************************
  90. * ImageLinkFree
  91. *
  92. * Free's the specified image link and closes up the hole
  93. * in the linked list.
  94. *
  95. * Arguments:
  96. *   PIMAGEINFO pImageFree - The image link to free.
  97. *
  98. * History:
  99. *
  100. ************************************************************************/
  101.  
  102. VOID ImageLinkFree(
  103.     PIMAGEINFO pImageFree)
  104. {
  105.     PIMAGEINFO pImage;
  106.     PIMAGEINFO pImagePrev;
  107.  
  108.     /*
  109.      * Find the existing link and get it's previous link.
  110.      */
  111.     for (pImage = gpImageHead, pImagePrev = NULL;
  112.             pImage && pImage != pImageFree;
  113.             pImagePrev = pImage, pImage = pImage->pImageNext)
  114.         ;
  115.  
  116.     /*
  117.      * Was the image link found?
  118.      */
  119.     if (pImage) {
  120.         /*
  121.          * Close up the linked list.
  122.          */
  123.         if (pImagePrev)
  124.             pImagePrev->pImageNext = pImageFree->pImageNext;
  125.         else
  126.             gpImageHead = pImageFree->pImageNext;
  127.  
  128.         /*
  129.          * Unlock and free the allocated DIB image.
  130.          */
  131.         if (pImageFree->DIBhandle) {
  132.             GlobalUnlock(pImageFree->DIBhandle);
  133.             GlobalFree(pImageFree->DIBhandle);
  134.         }
  135.  
  136.         /*
  137.          * Free the link itself.
  138.          */
  139.         MyFree(pImageFree);
  140.     }
  141. }
  142.  
  143.  
  144.  
  145. /************************************************************************
  146. * ImageLinkFreeList
  147. *
  148. * Free's all the nodes in the current image list (if there is one).
  149. *
  150. * Arguments:
  151. *
  152. * History:
  153. *
  154. ************************************************************************/
  155.  
  156. VOID ImageLinkFreeList(VOID)
  157. {
  158.     while (gpImageHead)
  159.         ImageLinkFree(gpImageHead);
  160. }
  161.  
  162.  
  163.  
  164. /************************************************************************
  165. * ImageDelete
  166. *
  167. * Deletes the current image (for icons/cursors).  If there are other images,
  168. * the first one is opened.  If the last image was just deleted, the
  169. * entire editor will be reset (if the file was untitled) or if there
  170. * is currently a file opened, some other things will happen, such as
  171. * hiding the workspace and view windows, etc.
  172. *
  173. * History:
  174. *
  175. ************************************************************************/
  176.  
  177. VOID ImageDelete(VOID)
  178. {
  179.     PIMAGEINFO pImage;
  180.  
  181.     ImageDCDelete();
  182.     ImageLinkFree(gpImageCur);
  183.     gpImageCur = NULL;
  184.     fFileDirty = TRUE;
  185.     fImageDirty = FALSE;
  186.     gnImages--;
  187.  
  188.     /*
  189.      * Look for the first remaining image that has a valid device
  190.      * (which means it is editable).
  191.      */
  192.     for (pImage = gpImageHead; pImage && !pImage->pDevice;
  193.             pImage = pImage->pImageNext)
  194.         ;
  195.  
  196.     /*
  197.      * If there are other editable images, open the first one.
  198.      * Otherwise, clear out some more things.
  199.      */
  200.     if (pImage) {
  201.         ImageOpen2(pImage);
  202.     }
  203.     else {
  204.         /*
  205.          * Is this a file off of disk?
  206.          */
  207.         if (gpszFileName) {
  208.             /*
  209.              * Hide the Workspace and View windows.
  210.              */
  211.             ShowWindow(ghwndWork, SW_HIDE);
  212.             ViewShow(FALSE);
  213.  
  214.             /*
  215.              * Update the properties bar (refill the images combo).
  216.              */
  217.             PropBarUpdate();
  218.         }
  219.         else {
  220.             /*
  221.              * Since this file was opened new and they deleted the
  222.              * last image present in it, just reset everything.
  223.              */
  224.             ClearResource();
  225.         }
  226.     }
  227. }
  228.