home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OBitmap.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  139 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OBitmap.cpp
  5.  
  6. /*
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  13.  *    endorse or promote products derived from this software
  14.  *    without specific prior written permission.
  15.  * 3. See OCL.INF for a detailed copyright notice.
  16.  *
  17.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  18.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  */
  29.  
  30. // $Header: W:/Projects/OCL/Source/rcs/OBitmap.cpp 1.50 1996/08/11 23:49:07 B.STEIN Release $
  31.  
  32. #define __OCL_SOURCE__
  33.  
  34. #define OINCL_OSTRING
  35. #define OINCL_BASE
  36.  
  37. #include <ocl.hpp>
  38. #include <OBitmap.hpp>
  39. #include <OPMException.hpp>
  40. #include <OMessage.hpp>
  41.  
  42. OBitmap::OBitmap()
  43.   : OPicture(0, 0),
  44.     hbm(NULLHANDLE)
  45.   {}
  46.  
  47.  
  48.  
  49. OBitmap::OBitmap(const HWND    bitmapOwner,
  50.                  const ULONG   resID, 
  51.                  const HMODULE module, 
  52.                  const ULONG   CX, 
  53.                  const ULONG   CY)
  54.   : OPicture(CX, CY),
  55.     hbm(NULLHANDLE)
  56. {
  57.  if ((hbm = GpiLoadBitmap(WinGetPS(bitmapOwner), 
  58.                           module, resID, CX, CY)) == NULLHANDLE)
  59.    throw OPMException(OCL::error(30), 0, OException::recoverable);
  60. }
  61.  
  62.  
  63.  
  64. OBitmap::OBitmap(const HBITMAP hbmHandle)
  65.   : OPicture(0, 0),
  66.     hbm(hbmHandle)
  67. {
  68.  if(hbm == NULLHANDLE)
  69.    throw OPMException(OCL::error(31), 0, OException::recoverable);
  70. }
  71.  
  72.  
  73.  
  74. OBitmap::OBitmap(const HWND bitmapOwner, PCSZ fileName)
  75.   : OPicture(0, 0),
  76.     hbm(NULLHANDLE)
  77. {
  78.  APIRET              rc = 0;
  79.  PBYTE               Buffer;
  80.  FILESTATUS3         stat;
  81.  fstream             bmpFile;
  82.  PBITMAPFILEHEADER   pbmfh;
  83.  
  84.  if((!fileName) ||
  85.    ((rc = DosQueryPathInfo(fileName, 1, &stat, sizeof(FILESTATUS3))) != 0) ||
  86.    ((rc = DosAllocMem((PPVOID)&Buffer,  stat.cbFile,
  87.                       PAG_READ | PAG_WRITE | PAG_COMMIT)) != 0))
  88.    throw OPMException(OMessage(rc).getText(), rc, OException::recoverable);
  89.  
  90. #if defined(__EMX__) 
  91.  bmpFile.open(fileName, ios::in);
  92. #else
  93.  bmpFile.open(fileName, ios::in | ios::binary);
  94. #endif
  95.  
  96.  if((bmpFile.fail()) || (!bmpFile.read(Buffer, stat.cbFile))) {
  97.    DosFreeMem(Buffer);
  98.    throw OPMException(OMessage(ERROR_FILE_NOT_FOUND).getText(),
  99.                       ERROR_FILE_NOT_FOUND, OException::recoverable); }
  100.  
  101.  bmpFile.close();
  102.  
  103.  pbmfh = (PBITMAPFILEHEADER)Buffer;
  104.  if ((pbmfh) && (pbmfh->usType !=  BFT_BMAP)) {
  105.    DosFreeMem(Buffer);  
  106.    throw OPMException(OMessage(ERROR_BAD_FORMAT).getText(),
  107.                       ERROR_BAD_FORMAT, OException::recoverable); }
  108.  
  109.  hbm = GpiCreateBitmap(WinGetPS(bitmapOwner), (PBITMAPINFOHEADER2) &pbmfh->bmp,
  110.                       (ULONG) CBM_INIT,
  111.                       (PBYTE) pbmfh + pbmfh->offBits,
  112.                       (PBITMAPINFO2) &pbmfh->bmp);
  113.  
  114.  width  = pbmfh->bmp.cx;
  115.  height = pbmfh->bmp.cy;
  116.  
  117.  DosFreeMem(Buffer);
  118.  
  119.  if (!hbm)
  120.    throw OPMException(OCL::error(32), 0, OException::recoverable);
  121. }
  122.  
  123.  
  124. OBitmap::~OBitmap()
  125. {
  126.  if (hbm)
  127.    GpiDeleteBitmap(hbm);
  128. }
  129.  
  130.  
  131. PSZ OBitmap::isOfType() const
  132.  return("OBitmap"); 
  133. }
  134.  
  135.  
  136.  
  137. // end of source
  138.