home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / slfinsta.zip / picture.c < prev    next >
C/C++ Source or Header  |  2000-03-26  |  7KB  |  209 lines

  1. /* $Id: picture.c,v 1.1 2000/03/27 04:52:57 ktk Exp $ */
  2.  
  3. /*
  4.  * Dynamic Windows:
  5.  *          A GTK like implementation of the PM GUI
  6.  *          Bitmap window class
  7.  *
  8.  * (C) 2000 Achim Hasenmueller <achimha@innotek.de>
  9.  * (C) 2000 Brian Smith <dbsoft@technologist.com>
  10.  *
  11.  */
  12. #define INCL_DOS
  13. #define INCL_WIN
  14. #define INCL_GPI
  15.  
  16. #include <os2.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <io.h>
  21. #include "picture.h"
  22. #include "dw.h"
  23.  
  24. /*
  25.  * Registers the bitmap window class
  26.  * Parameters:
  27.  *
  28.  */
  29. HWND dw_bitmapfile_new(ULONG id)
  30. {
  31.     /* static variable to check if we need to register the window class */
  32.     static BOOL classRegistered = FALSE;
  33.  
  34.     if (!classRegistered)
  35.     {
  36.         /* register class and reserve user data storage for bitmap handle */
  37.         WinRegisterClass(dwhab, "DWBITMAPFILE", dw_bitmapclass_wndproc,
  38.                          0, sizeof(HBITMAP));
  39.         classRegistered = TRUE;
  40.     }
  41.     return WinCreateWindow(HWND_OBJECT,
  42.                            "DWBITMAPFILE",
  43.                            NULL,
  44.                            WS_VISIBLE,
  45.                            0, 0, 2000, 1000,
  46.                            NULLHANDLE,
  47.                            HWND_TOP,
  48.                            id,
  49.                            NULL,
  50.                            NULL);
  51. }
  52.  
  53. /*
  54.  * Sets the bitmap used for a given bitmap window, loaded from a bitmap file
  55.  * Parameters:
  56.  *       handle: Handle to the window
  57.  *       filename: the file name that contains the bitmap file
  58.  *
  59.  */
  60. BOOL dw_bitmapfile_set_bitmap(HWND handle, char *filename)
  61. {
  62.     HFILE BitmapFileHandle = NULLHANDLE; /* handle for the file */
  63.     ULONG OpenAction = 0;
  64.     PBYTE BitmapFileBegin; /* pointer to the first byte of bitmap data  */
  65.     FILESTATUS BitmapStatus;
  66.     ULONG cbRead;
  67.     PBITMAPFILEHEADER2 pBitmapFileHeader;
  68.     PBITMAPINFOHEADER2 pBitmapInfoHeader;
  69.     ULONG ScanLines;
  70.     HBITMAP hbm;
  71.     HPS hps;
  72.  
  73.     /* check if we can read from this file (it exists and read permission) */
  74.     if (access(filename, 04) != 0)
  75.         return NULLHANDLE;
  76.     /* open bitmap file */
  77.     DosOpen(filename, &BitmapFileHandle, &OpenAction, 0L,
  78.             FILE_ARCHIVED | FILE_NORMAL | FILE_READONLY,
  79.             OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
  80.             OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY |
  81.             OPEN_FLAGS_NOINHERIT, 0L);
  82.     if (BitmapFileHandle == NULLHANDLE)
  83.         return NULLHANDLE;
  84.     /* find out how big the file is  */
  85.     DosQueryFileInfo(BitmapFileHandle, 1, &BitmapStatus,
  86.                      sizeof(BitmapStatus));
  87.     /* allocate memory to load the bitmap */
  88.     DosAllocMem((PPVOID)&BitmapFileBegin, (ULONG)BitmapStatus.cbFile,
  89.                 PAG_READ | PAG_WRITE | PAG_COMMIT);
  90.     /* read bitmap file into memory buffer */
  91.     DosRead(BitmapFileHandle, (PVOID)BitmapFileBegin,
  92.             BitmapStatus.cbFile, &cbRead);
  93.     /* access first bytes as bitmap header */
  94.     pBitmapFileHeader = (PBITMAPFILEHEADER2)BitmapFileBegin;
  95.     /* check if it's a valid bitmap data file */
  96.     if ((pBitmapFileHeader->usType != BFT_BITMAPARRAY) &&
  97.         (pBitmapFileHeader->usType != BFT_BMAP))
  98.     {
  99.         /* free memory of bitmap file buffer */
  100.         DosFreeMem(BitmapFileBegin);
  101.         /* close the bitmap file */
  102.         DosClose(BitmapFileHandle);
  103.         return NULLHANDLE;
  104.     }
  105.     /* check if it's a file with multiple bitmaps */
  106.     if (pBitmapFileHeader->usType == BFT_BITMAPARRAY)
  107.     {
  108.         /* we'll just use the first bitmap and ignore the others */
  109.         pBitmapFileHeader = &(((PBITMAPARRAYFILEHEADER2)BitmapFileBegin)->bfh2);
  110.     }
  111.     /* set pointer to bitmap information block */
  112.     pBitmapInfoHeader = &pBitmapFileHeader->bmp2;
  113.     /* find out if it's the new 2.0 format or the old format */
  114.     /* and query number of lines */
  115.     if (pBitmapInfoHeader->cbFix == sizeof(BITMAPINFOHEADER))
  116.         ScanLines = (ULONG)((PBITMAPINFOHEADER)pBitmapInfoHeader)->cy;
  117.     else
  118.         ScanLines = pBitmapInfoHeader->cy;
  119.     /* now we need a presentation space, get it from static control */
  120.     hps = WinGetPS(handle);
  121.     /* create bitmap now using the parameters from the info block */
  122.     hbm = GpiCreateBitmap(hps, pBitmapInfoHeader, 0L, NULL, NULL);
  123.     /* select the new bitmap into presentation space */
  124.     GpiSetBitmap(hps, hbm);
  125.     /* now copy the bitmap data into the bitmap */
  126.     GpiSetBitmapBits(hps, 0L, ScanLines,
  127.                      BitmapFileBegin + pBitmapFileHeader->offBits,
  128.                      (PBITMAPINFO2)pBitmapInfoHeader);
  129.     /* free memory of bitmap file buffer */
  130.     DosFreeMem(BitmapFileBegin);
  131.     /* close the bitmap file */
  132.     DosClose(BitmapFileHandle);
  133.  
  134.     /* tell static control to use that bitmap, make sure it is an icon control */
  135.     WinSendMsg(handle, SM_SETHANDLE, MPFROMP(hbm), NULL);
  136.     return TRUE;
  137.  
  138.  
  139. /*
  140.  * window class procedure
  141.  * Parameters:
  142.  *       hwnd - window handle of the window instance
  143.  *       msg - window message to handle
  144.  *       mp1 - first window message parameter
  145.  *       mp2 - second window message parameter
  146.  * Returns:
  147.  *       message specific return value
  148.  *
  149.  */
  150. MRESULT EXPENTRY dw_bitmapclass_wndproc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  151. {
  152.     switch (msg)
  153.     {
  154.         case WM_CREATE:
  155.         {
  156.             WinDefWindowProc(hwnd, msg, mp1, mp2);
  157.             /* reset bitmap handle to NULLHANDLE */
  158.             WinSetWindowULong(hwnd, QWL_USER, NULLHANDLE);
  159.             return (MRESULT)FALSE;
  160.         }
  161.  
  162.         case WM_DESTROY:
  163.         {
  164.             HBITMAP bitmapHandle;
  165.             bitmapHandle = (HBITMAP)WinQueryWindowULong(hwnd, QWL_USER);
  166.             /* check if we have a bitmap associated */
  167.             if (bitmapHandle != NULLHANDLE)
  168.                 GpiDeleteBitmap(bitmapHandle);
  169.             return (MRESULT)TRUE;
  170.         }
  171.  
  172.         case WM_PAINT:
  173.         {
  174.             HPS hps;
  175.             HBITMAP bitmapHandle;
  176.             RECTL UpdtRegion;
  177.             hps = WinBeginPaint(hwnd, NULLHANDLE, &UpdtRegion);
  178.             /* get handle of bitmap from window words */
  179.             bitmapHandle = (HBITMAP)WinQueryWindowULong(hwnd, QWL_USER);
  180.             if (bitmapHandle != NULLHANDLE)
  181.             {
  182.                 RECTL DestRectl;
  183.                 /* get the window size into the destination rectangle */
  184.                 WinQueryWindowRect(hwnd, &DestRectl);
  185.                 /* draw bitmap stretched to window size */
  186.                 WinDrawBitmap(hps, bitmapHandle, NULL,
  187.                               (PPOINTL)&DestRectl, 0, 0, DBM_STRETCH);
  188.             } else
  189.             {
  190.                 /* we have no bitmap, paint pale gray */
  191.                 WinFillRect(hps, &UpdtRegion, CLR_PALEGRAY);
  192.             }
  193.             WinEndPaint(hps);
  194.             return (MRESULT)FALSE;
  195.         }
  196.  
  197.         case SM_SETHANDLE:
  198.         {
  199.             /* store handle of new bitmap in window words */
  200.             WinSetWindowULong(hwnd, QWL_USER, (ULONG)mp1);
  201.             return (MRESULT)TRUE;
  202.         }
  203.  
  204.         default:
  205.             return WinDefWindowProc(hwnd, msg, mp1, mp2);
  206.     }
  207. }
  208.