home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gpiimage.zip / IMG_PNT.C < prev    next >
C/C++ Source or Header  |  1998-04-20  |  11KB  |  309 lines

  1. /**************************************************************************
  2.  *  File name  :  img_pnt.c
  3.  *
  4.  *  Description:  This module contains the code for the main client window
  5.  *                painting.  It contains these functions:
  6.  *
  7.  *                PaintUnSizedImage()
  8.  *                PaintSizedImage()
  9.  *                PaintDrawImage()
  10.  *
  11.  *  Concepts   :  bitmaps
  12.  *
  13.  *  API's      :  GpiDeleteBitmap
  14.  *                GpiAssociate
  15.  *                GpiDestroyPS
  16.  *                DevCloseDC
  17.  *                DevOpenDC
  18.  *                GpiCreatePS
  19.  *                GpiCreateBitmap
  20.  *                GpiSetBitmap
  21.  *                GpiErase
  22.  *                GpiSetBackColor
  23.  *                GpiSetColor
  24.  *                GpiSetBackMix
  25.  *                GpiSetCurrentPosition
  26.  *                WinMessageBox
  27.  *                GpiImage
  28.  *                WinInvalidateRect
  29.  *                WinSetPointer
  30.  *                WinQueryWindowRect
  31.  *                WinDrawBitmap
  32.  *
  33.  *  Required
  34.  *    Files    :  OS2.H, STRING.H, STDIO.H, IMG_MAIN.H, IMG_XTRN.H
  35.  *
  36.  *  Copyright (C) 1991-1993 IBM Corporation
  37.  *
  38.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  39.  *      sample code created by IBM Corporation. This sample code is not
  40.  *      part of any standard or IBM product and is provided to you solely
  41.  *      for  the purpose of assisting you in the development of your
  42.  *      applications.  The code is provided "AS IS", without
  43.  *      warranty of any kind.  IBM shall not be liable for any damages
  44.  *      arising out of your use of the sample code, even if they have been
  45.  *      advised of the possibility of such damages.                                                    *
  46.  *************************************************************************/
  47. /*
  48.  *  Include files, macros, defined constants, and externs
  49.  */
  50. #define INCL_DOSMEMMGR
  51. #define INCL_DOSPROCESS
  52. #define INCL_WINMENUS
  53. #define INCL_WINFRAMEMGR
  54. #define INCL_WINPOINTERS
  55. #define INCL_WINSTDFILE
  56. #define INCL_GPIPRIMITIVES
  57. #define INCL_GPIBITMAPS
  58.  
  59. #include <os2.h>
  60. #include <string.h>
  61. #include <stdio.h>
  62.  
  63. #include "img_main.h"
  64. #include "img_xtrn.h"
  65.  
  66. /**************************************************************************
  67.  *
  68.  *  Name       : PaintDrawImage()
  69.  *
  70.  *  Description: Draws the image data into the bitmap
  71.  *
  72.  *  Concepts   : Routine is called from LoadImage()
  73.  *               - creates a memory device context
  74.  *               - defines a memory presentation space
  75.  *               - associates the memory DC with the memory PS
  76.  *               - creates a bitmap
  77.  *               - Selects bitmap into memory DC
  78.  *               - draws the image data into the bitmap
  79.  *
  80.  *  API's      :  GpiDeleteBitmap
  81.  *                GpiAssociate
  82.  *                GpiDestroyPS
  83.  *                DevCloseDC
  84.  *                DevOpenDC
  85.  *                GpiCreatePS
  86.  *                GpiCreateBitmap
  87.  *                GpiSetBitmap
  88.  *                GpiErase
  89.  *                GpiSetBackMix
  90.  *                GpiSetBackColor
  91.  *                GpiSetColor
  92.  *                GpiSetCurrentPosition
  93.  *                WinMessageBox
  94.  *                GpiImage
  95.  *                WinInvalidateRect
  96.  *
  97.  *  Parameters : [none]
  98.  *
  99.  *  Return     : [none]
  100.  *
  101.  *************************************************************************/
  102. VOID PaintDrawImage(void)
  103. {
  104.    POINTL ptl;
  105.    DEVOPENSTRUC dop;
  106. #ifdef PORT_16
  107.    BITMAPINFOHEADER  BmapInfo;
  108. #endif
  109. #ifdef PORT_32
  110.    BITMAPINFOHEADER2 BmapInfo;
  111. #endif
  112.  
  113.    if (vfImgLoaded)
  114.    {
  115.        /*
  116.         * The memory presentation space is defined to accommodate a
  117.         * particular file of image data. If an image has already been
  118.         * loaded, its resources must be deleted before a new set can be
  119.         * defined.
  120.         */
  121.         GpiDeleteBitmap(vhbm);
  122.         GpiAssociate(vhpsMem, (HDC)NULLHANDLE);
  123.         GpiDestroyPS(vhpsMem);
  124.         DevCloseDC(vhdcMem);
  125.    }
  126.  
  127.    /*
  128.     * define the memory device context
  129.     */
  130.    dop.pszLogAddress = (PSZ)NULL;
  131.    dop.pszDriverName = "DISPLAY";
  132.    vhdcMem = DevOpenDC(vhab,
  133.                        OD_MEMORY,
  134.                        "*",
  135.                        2L, /* number of valid elements */
  136.                        (PDEVOPENDATA)&dop,
  137.                        (HDC)NULLHANDLE);
  138.  
  139.    /*
  140.     * The memory presentation space is defined and associated with the
  141.     * memory device context. The presentation page is the same size in
  142.     * pels as the image.
  143.     */
  144.    vhpsMem = GpiCreatePS(vhab,
  145.                          vhdcMem,
  146.                          &vsizlImg,
  147.                          (LONG)PU_PELS | GPIT_NORMAL | GPIA_ASSOC);
  148.  
  149.    /*
  150.     * Create a bit map in a format compatible with the memory device
  151.     * context.
  152.     */
  153.  
  154.    /*
  155.     * Define the bitmap information header.
  156.     */
  157.    BmapInfo.cbFix     = 16;                /* bit-map header length   */
  158.    BmapInfo.cx        = vsizlImg.cx;
  159.    BmapInfo.cy        = vsizlImg.cy;
  160.    BmapInfo.cPlanes   = 1L;                /* number of bit planes    */
  161.    BmapInfo.cBitCount = 1L;                /* number of bits per pel  */
  162.  
  163.  
  164.    /*
  165.     * The bit map is defined and selected into the memory device
  166.     * context. The output display is cleared.
  167.     */
  168.    vhbm = GpiCreateBitmap(vhpsMem,
  169.                           &BmapInfo,
  170.                           0L,                     /* no options      */
  171.                           (PBYTE)NULL,            /* no initial data */
  172.                           (PBITMAPINFO2)NULL);
  173.  
  174.    GpiSetBitmap(vhpsMem, vhbm);
  175.    GpiErase(vhpsMem);
  176.    GpiSetBackColor(vhpsMem, vlBackClr);
  177.    GpiSetColor(vhpsMem, vlForeClr);
  178.    GpiSetBackMix(vhpsMem, BM_OVERPAINT);
  179.  
  180.    /*
  181.     * The current position is set to the top-left corner of the memory
  182.     * presentation page before GpiImage is issued to draw the image
  183.     * into it.
  184.     */
  185.    ptl.x = 0L;
  186.    ptl.y = vsizlImg.cy;
  187.    GpiSetCurrentPosition(vhpsMem, &ptl);
  188. #ifdef PORT_32
  189.    {
  190.       BYTE     acInform[50];
  191.       PBYTE    pbFileName;
  192.  
  193.       WinInvalidateRect(vhwndClient, (PRECTL)NULL, FALSE);
  194.       sprintf (acInform, "Press OK to display this image.");
  195.       pbFileName = strrchr(vfdg.szFullFile, '\\');  /* the last backslash */
  196.       if(pbFileName)
  197.          pbFileName++;
  198.       WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (PSZ)acInform, (PSZ)pbFileName,
  199.               0L, MB_OK | MB_INFORMATION | MB_APPLMODAL | MB_MOVEABLE);
  200.    }
  201. #endif
  202.    WinEnableWindowUpdate(vhwndClient, TRUE);
  203.  
  204.    GpiImage(vhpsMem,
  205.             0L,
  206.             &vsizlImg,                             /* image size           */
  207.             (LONG)((vsizlImg.cx * vsizlImg.cy)/8), /* data length in bytes */
  208.             vpbImgBuf);                            /* image data           */
  209.  
  210.    GpiSetBitmap(vhpsMem, (HBITMAP)NULLHANDLE); /* free bit map in memory DC */
  211.    WinInvalidateRect(vhwndClient, (PRECTL)NULL, FALSE);
  212. }   /* End of PaintDrawImage */
  213.  
  214.  
  215. /**************************************************************************
  216.  *
  217.  *  Name       : PaintUnSizedImage()
  218.  *
  219.  *  Description: Paints the image in the client window
  220.  *
  221.  *  Concepts   : Routine is called from MainWndProc()
  222.  *
  223.  *  API's      :  WinSetPointer
  224.  *                GpiImage
  225.  *
  226.  *  Parameters : [none]
  227.  *
  228.  *  Return     : [none]
  229.  *
  230.  *************************************************************************/
  231. VOID PaintUnSizedImage(void)
  232. {
  233.    WinSetPointer(HWND_DESKTOP, vhptrWait);
  234.  
  235.    GpiImage(vhps,
  236.             0L,
  237.             &vsizlImg,                           /* size in pels         */
  238.             (LONG)(vsizlImg.cx * vsizlImg.cy/8), /* data length in bytes */
  239.             vpbImgBuf);                          /* image data           */
  240.  
  241.    WinSetPointer(HWND_DESKTOP, vhptrArrow);
  242. }   /* End of PaintUnSizedImage */
  243.  
  244. /**************************************************************************
  245.  *
  246.  *  Name       : PaintSizedImage()
  247.  *
  248.  *  Description: Paints the image in the client window, sizes
  249.  *               it so that the whole of the image is visible
  250.  *               (i.e. scaled as necessary)
  251.  *
  252.  *  Concepts   : Routine is called from MainWndProc()
  253.  *
  254.  *  API's      :  WinQueryWindowRect
  255.  *                WinSetPointer
  256.  *                WinDrawBitmap
  257.  *
  258.  *  Parameters : [none]
  259.  *
  260.  *  Return     : [none]
  261.  *
  262.  *************************************************************************/
  263. VOID PaintSizedImage(void)
  264. {
  265.    RECTL  rcl;                   /* client window rectangle    */
  266.    POINTL aptl[5];               /* array of x,y pairs         */
  267.  
  268.    /*
  269.     * The dimensions of the target rectangle are obtained using the
  270.     * WinQueryWindowRect() call.
  271.     */
  272.    WinQueryWindowRect(vhwndClient, &rcl); /* query client window size */
  273.    aptl[0].x = 0;                         /* bottom left, in          */
  274.    aptl[0].y = 0;                         /*  device coordinates      */
  275.    aptl[1].x = rcl.xRight - rcl.xLeft;    /* top right, in            */
  276.    aptl[1].y = rcl.yTop - rcl.yBottom;    /*  device coordinates      */
  277.  
  278.    /*
  279.     * The dimensions of the source rectangle are those of the source
  280.     * image.
  281.     */
  282.    aptl[2].x = 0;                         /* bottom left, in          */
  283.    aptl[2].y = 0;                         /*  window coordinates      */
  284.    aptl[3].x = vsizlImg.cx;               /* top right, in            */
  285.    aptl[3].y = vsizlImg.cy;               /*  device coordinates      */
  286.  
  287.    WinSetPointer(HWND_DESKTOP, vhptrWait);
  288.  
  289.    /*
  290.     * Draw the image.
  291.     */
  292.    WinDrawBitmap(vhps,
  293.                  vhbm,
  294.                  (PRECTL)NULL,                /* draw whole bit map   */
  295.                  (PPOINTL)&rcl,               /* bit-map destination  */
  296.                                               /*  is client window    */
  297.                                               /*  rectangle.          */
  298.                  0L,
  299.                  0L,
  300.                  DBM_STRETCH | DBM_IMAGEATTRS);
  301.                                               /* stretch or compress  */
  302.                                               /*  to suit, and use    */
  303.                                               /*  image attributes    */
  304.                                               /*  for color.          */
  305.  
  306.    WinSetPointer(HWND_DESKTOP, vhptrArrow);
  307. }   /* End of PaintSizedImage */
  308. /***************************  End of img_pnt.c   *************************/
  309.