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 / devinfo.c < prev    next >
Text File  |  1996-06-12  |  10KB  |  340 lines

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: devinfo.c
  9.     *
  10.     * Contains routines for manipulating the linked list of known target
  11.     * devices for icons and cursors.
  12.     *
  13.     * History:
  14.     *
  15.     ****************************************************************************/
  16.     
  17.     #include "imagedit.h"
  18.     
  19.     #include <stdio.h>
  20.     #include <stdlib.h>
  21.     #include <string.h>
  22.     
  23.     
  24.     STATICFN VOID ProcessDeviceSection(INT iType);
  25.     STATICFN BOOL ParseDeviceLine(PSTR pszLine, PINT pnColors, PINT pcx,
  26.         PINT pcy);
  27.     
  28.     
  29.     
  30.     /************************************************************************
  31.     * InitDeviceList
  32.     *
  33.     *
  34.     *
  35.     * Arguments:
  36.     *
  37.     * History:
  38.     *
  39.     ************************************************************************/
  40.     
  41.     VOID InitDeviceList(VOID)
  42.     {
  43.         /*
  44.          * Allocate the standard icon devices.
  45.          */
  46.         DeviceLinkAlloc(FT_ICON, ids(IDS_ICONDEVNAMEEGAVGA), 16, 32, 32);
  47.         DeviceLinkAlloc(FT_ICON, ids(IDS_ICONDEVNAMEMONO), 2, 32, 32);
  48.         DeviceLinkAlloc(FT_ICON, ids(IDS_ICONDEVNAMECGA), 2, 32, 16);
  49.     
  50.         ProcessDeviceSection(FT_ICON);
  51.     
  52.         /*
  53.          * Allocate the standard cursor devices.
  54.          */
  55.         DeviceLinkAlloc(FT_CURSOR, ids(IDS_CURDEVNAMEVGAMONO), 2, 32, 32);
  56.         DeviceLinkAlloc(FT_CURSOR, ids(IDS_CURDEVNAMEVGACOLOR), 16, 32, 32);
  57.     
  58.         ProcessDeviceSection(FT_CURSOR);
  59.     }
  60.     
  61.     
  62.     
  63.     /************************************************************************
  64.     * ProcessDeviceSection
  65.     *
  66.     *
  67.     *
  68.     * Arguments:
  69.     *
  70.     * History:
  71.     *
  72.     ************************************************************************/
  73.     
  74.     STATICFN VOID ProcessDeviceSection(
  75.         INT iType)
  76.     {
  77.         PSTR pszSectionName;
  78.         CHAR szValueBuf[CCHTEXTMAX];
  79.         CHAR szKeyNameBuf[CCHTEXTMAX];
  80.         PSTR pszKeyName;
  81.         INT nColors;
  82.         INT cx;
  83.         INT cy;
  84.     
  85.         if (iType == FT_ICON) {
  86.             pszSectionName = ids(IDS_ICONINISECTION);
  87.         }
  88.         else {
  89.             pszSectionName = ids(IDS_CURSORINISECTION);
  90.         }
  91.     
  92.         if (!GetPrivateProfileString(pszSectionName, NULL, "",
  93.                 szKeyNameBuf, sizeof(szKeyNameBuf), ids(IDS_IMAGEDITINI)))
  94.             return;
  95.     
  96.         pszKeyName = szKeyNameBuf;
  97.         while (*pszKeyName) {
  98.             if (GetPrivateProfileString(pszSectionName, pszKeyName, "",
  99.                     szValueBuf, sizeof(szValueBuf), ids(IDS_IMAGEDITINI))) {
  100.                 if (ParseDeviceLine(szValueBuf, &nColors, &cx, &cy))
  101.                     DeviceLinkAlloc(iType, pszKeyName, nColors, cx, cy);
  102.             }
  103.     
  104.             pszKeyName += strlen(pszKeyName) + 1;
  105.         }
  106.     }
  107.     
  108.     
  109.     
  110.     /************************************************************************
  111.     * ParseDeviceLine
  112.     *
  113.     *
  114.     *
  115.     * Arguments:
  116.     *
  117.     * History:
  118.     *
  119.     ************************************************************************/
  120.     
  121.     STATICFN BOOL ParseDeviceLine(
  122.         PSTR pszLine,
  123.         PINT pnColors,
  124.         PINT pcx,
  125.         PINT pcy)
  126.     {
  127.         static CHAR szSep[] = " ,";
  128.         PSTR pstr;
  129.     
  130.         if (!(pstr = strtok(pszLine, szSep)))
  131.             return FALSE;
  132.     
  133.         *pnColors = atoi(pstr);
  134.     
  135.         if (!(pstr = strtok(NULL, szSep)))
  136.             return FALSE;
  137.     
  138.         *pcx = atoi(pstr);
  139.     
  140.         if (!(pstr = strtok(NULL, szSep)))
  141.             return FALSE;
  142.     
  143.         *pcy = atoi(pstr);
  144.     
  145.         return TRUE;
  146.     }
  147.     
  148.     
  149.     
  150.     /************************************************************************
  151.     * DeviceLinkAlloc
  152.     *
  153.     * Allocates a DEVICE structure, initializes it with the given values
  154.     * and adds it to the appropriate linked list.  Because these are
  155.     * specified only at init time, there is no need to ever free them.
  156.     *
  157.     * There is a special case if the image type is FT_BITMAP.  Because
  158.     * there is only one type of bitmap image device at any one time,
  159.     * this routine does not really allocate a link for these.  Instead,
  160.     * it uses a static structure for bitmaps.  It will be set to the
  161.     * given values and a pointer to it will be returned.  This means
  162.     * that for bitmaps, this function must be called every time that
  163.     * the current bitmap image changes to set up the proper values in
  164.     * the structure.
  165.     *
  166.     * Arguments:
  167.     *   INT iType    - Type of image.  FT_* constant.
  168.     *   PSTR pszName - Device name ("VGA", for example).
  169.     *   INT nColors  - Number of colors.
  170.     *   INT cx       - Width of image.
  171.     *   INT cy       - Height of image.
  172.     *
  173.     * History:
  174.     *
  175.     ************************************************************************/
  176.     
  177.     PDEVICE DeviceLinkAlloc(
  178.         INT iType,
  179.         PSTR pszName,
  180.         INT nColors,
  181.         INT cx,
  182.         INT cy)
  183.     {
  184.         static DEVICE DeviceBitmap;     // Device structure for bitmaps.
  185.         PDEVICE pDevice;
  186.         PDEVICE pDeviceT;
  187.         PDEVICE *ppDeviceHead;
  188.     
  189.         /*
  190.          * Currently we only support 1 and 4 plane devices.
  191.          */
  192.         if (nColors != 2 && nColors != 16) {
  193.             Message(MSG_BADDEVICECOLORS);
  194.             return NULL;
  195.         }
  196.     
  197.         /*
  198.          * There is a limit to the size of image we will edit.  For icons
  199.          * and cursors, the field that carries the dimensions is a byte,
  200.          * so the size cannot be greater than 256.  This is also what
  201.          * we limit bitmaps to.
  202.          */
  203.         if (cx < 1 || cx > MAXIMAGEDIM || cy < 1 || cy > MAXIMAGEDIM) {
  204.             Message(MSG_BADDEVICESIZE, MAXIMAGEDIM);
  205.             return NULL;
  206.         }
  207.     
  208.         /*
  209.          * For bitmaps, don't really allocate a link, just reuse the
  210.          * static DEVICE structure used for bitmaps.
  211.          */
  212.         if (iType == FT_BITMAP) {
  213.             pDevice = &DeviceBitmap;
  214.         }
  215.         else {
  216.             if (!(pDevice = (PDEVICE)MyAlloc(sizeof(DEVICE))))
  217.                 return NULL;
  218.     
  219.             switch (iType) {
  220.                 case FT_ICON:
  221.                     ppDeviceHead = &gpIconDeviceHead;
  222.                     gnIconDevices++;
  223.                     break;
  224.     
  225.                 case FT_CURSOR:
  226.                     ppDeviceHead = &gpCursorDeviceHead;
  227.                     gnCursorDevices++;
  228.                     break;
  229.             }
  230.         }
  231.     
  232.         pDevice->pDeviceNext = NULL;
  233.         pDevice->iType = iType;
  234.         pDevice->nColors = nColors;
  235.         pDevice->cx = cx;
  236.         pDevice->cy = cy;
  237.     
  238.         if (pszName) {
  239.             strcpy(pDevice->szName, pszName);
  240.             wsprintf(pDevice->szDesc, "%s %d-Color %dx%d",  //   hardcoded strings!
  241.                     (LPSTR)pszName, nColors, cx, cy);
  242.         }
  243.         else {
  244.             *pDevice->szName = '\0';
  245.             wsprintf(pDevice->szDesc, "%d-Color %dx%d", nColors, cx, cy);
  246.         }
  247.     
  248.         /*
  249.          * Because there is only one bitmap link, there is no need
  250.          * for a list for these, only icons and cursors.
  251.          */
  252.         if (iType != FT_BITMAP) {
  253.             /*
  254.              * Insert the link in the specified list.
  255.              */
  256.             if (!(*ppDeviceHead)) {
  257.                 /*
  258.                  * This is the first one.  Start the list.
  259.                  */
  260.                 *ppDeviceHead = pDevice;
  261.             }
  262.             else {
  263.                 /*
  264.                  * Find the end of the list and tack on the new link.
  265.                  */
  266.                 for (pDeviceT = *ppDeviceHead; pDeviceT->pDeviceNext;
  267.                         pDeviceT = pDeviceT->pDeviceNext)
  268.                     ;
  269.     
  270.                 pDeviceT->pDeviceNext = pDevice;
  271.             }
  272.         }
  273.     
  274.         return pDevice;
  275.     }
  276.     
  277.     
  278.     
  279.     /************************************************************************
  280.     * DeviceLinkFind
  281.     *
  282.     *
  283.     *
  284.     * Arguments:
  285.     *
  286.     * History:
  287.     *
  288.     ************************************************************************/
  289.     
  290.     PDEVICE DeviceLinkFind(
  291.         PDEVICE pDeviceHead,
  292.         INT nColors,
  293.         INT cx,
  294.         INT cy)
  295.     {
  296.         PDEVICE pDevice;
  297.     
  298.         /*
  299.          * Search the specified list.
  300.          */
  301.         for (pDevice = pDeviceHead; pDevice; pDevice = pDevice->pDeviceNext) {
  302.             /*
  303.              * Is this a match?
  304.              */
  305.             if (pDevice->nColors == nColors && pDevice->cx == cx &&
  306.                     pDevice->cy == cy)
  307.                 break;
  308.         }
  309.     
  310.         return pDevice;
  311.     }
  312.     
  313.     
  314.     
  315.     /************************************************************************
  316.     * DeviceLinkUsed
  317.     *
  318.     * This function returns TRUE if the specified device is used in
  319.     * the current image list.
  320.     *
  321.     * Arguments:
  322.     *   PDEVICE pDevice - Device to look for.
  323.     *
  324.     * History:
  325.     *
  326.     ************************************************************************/
  327.     
  328.     BOOL DeviceLinkUsed(
  329.         PDEVICE pDevice)
  330.     {
  331.         PIMAGEINFO pImage;
  332.     
  333.         for (pImage = gpImageHead; pImage; pImage = pImage->pImageNext) {
  334.             if (pImage->pDevice == pDevice)
  335.                 return TRUE;
  336.         }
  337.     
  338.         return FALSE;
  339.     }
  340.