]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/bitmap.cpp
*** empty log message ***
[wxWidgets.git] / src / os2 / bitmap.cpp
index 036eb640ed948980b078f3424fbb2e6b2edc4639..2d7a3fc799f6a204147935360603498f455577b3 100644 (file)
 /////////////////////////////////////////////////////////////////////////////
 // Name:        bitmap.cpp
 // Purpose:     wxBitmap
 /////////////////////////////////////////////////////////////////////////////
 // Name:        bitmap.cpp
 // Purpose:     wxBitmap
-// Author:      AUTHOR
+// Author:      David Webster
 // Modified by:
 // Modified by:
-// Created:     ??/??/98
+// Created:     08/08/99
 // RCS-ID:      $Id$
 // RCS-ID:      $Id$
-// Copyright:   (c) AUTHOR
+// Copyright:   (c) David Webster
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #ifdef __GNUG__
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #ifdef __GNUG__
-#pragma implementation "bitmap.h"
+    #pragma implementation "bitmap.h"
 #endif
 
 #endif
 
-#include "wx/setup.h"
-#include "wx/utils.h"
-#include "wx/palette.h"
-#include "wx/bitmap.h"
-#include "wx/icon.h"
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#ifndef WX_PRECOMP
+    #include <stdio.h>
+
+    #include "wx/list.h"
+    #include "wx/utils.h"
+    #include "wx/app.h"
+    #include "wx/palette.h"
+    #include "wx/dcmemory.h"
+    #include "wx/bitmap.h"
+    #include "wx/icon.h"
+#endif
+
+#include "wx/os2/private.h"
 #include "wx/log.h"
 
 #include "wx/log.h"
 
-#if !USE_SHARED_LIBRARIES
+//#include "wx/msw/dib.h"
+#include "wx/image.h"
+
+// ----------------------------------------------------------------------------
+// macros
+// ----------------------------------------------------------------------------
+
 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
-#endif
+
+IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
+
+// ============================================================================
+// implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxBitmapRefData
+// ----------------------------------------------------------------------------
 
 wxBitmapRefData::wxBitmapRefData()
 {
 
 wxBitmapRefData::wxBitmapRefData()
 {
-    m_ok = FALSE;
-    m_width = 0;
-    m_height = 0;
-    m_depth = 0;
-    m_quality = 0;
-    m_numColors = 0;
-    m_bitmapMask = NULL;
+    m_nQuality      = 0;
+    m_pSelectedInto = NULL;
+    m_nNumColors    = 0;
+    m_pBitmapMask   = NULL;
 }
 
 }
 
-wxBitmapRefData::~wxBitmapRefData()
+void wxBitmapRefData::Free()
 {
 {
-    /*
-     * TODO: delete the bitmap data here.
-     */
+    wxASSERT_MSG( !m_pSelectedInto,
+                  wxT("deleting bitmap still selected into wxMemoryDC") );
+
+    if (m_hBitmap)
+    {
+        if ( !::GpiDeleteBitmap((HBITMAP)m_hBitmap) )
+        {
+            wxLogLastError("GpiDeleteBitmap(hbitmap)");
+        }
+    }
 
 
-    if (m_bitmapMask)
-        delete m_bitmapMask;
-    m_bitmapMask = NULL;
+    delete m_pBitmapMask;
+    m_pBitmapMask = NULL;
 }
 
 }
 
-wxList wxBitmap::sm_handlers;
+// ----------------------------------------------------------------------------
+// wxBitmap creation
+// ----------------------------------------------------------------------------
 
 
-wxBitmap::wxBitmap()
+// this function should be called from all wxBitmap ctors
+void wxBitmap::Init()
 {
 {
-    m_refData = NULL;
+    // m_refData = NULL; done in the base class ctor
 
 
-    if ( wxTheBitmapList )
+    if (wxTheBitmapList)
         wxTheBitmapList->AddBitmap(this);
 }
 
         wxTheBitmapList->AddBitmap(this);
 }
 
-wxBitmap::~wxBitmap()
+bool wxBitmap::CopyFromIconOrCursor(
+  const wxGDIImage&                 rIcon
+)
 {
 {
-    if (wxTheBitmapList)
-        wxTheBitmapList->DeleteObject(this);
-}
+    wxBitmapRefData*                pRefData = new wxBitmapRefData;
 
 
-wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
-{
-    m_refData = new wxBitmapRefData;
+    m_refData = pRefData;
 
 
-    M_BITMAPDATA->m_width = the_width ;
-    M_BITMAPDATA->m_height = the_height ;
-    M_BITMAPDATA->m_depth = no_bits ;
-    M_BITMAPDATA->m_numColors = 0;
+    pRefData->m_nWidth = rIcon.GetWidth();
+    pRefData->m_nHeight = rIcon.GetHeight();
+    pRefData->m_nDepth = wxDisplayDepth();
 
 
-    /* TODO: create the bitmap from data */
+    pRefData->m_hBitmap = (WXHBITMAP)rIcon.GetHandle();
+    // no mask???
+    pRefData->m_pBitmapMask = new wxMask();
 
 
-    if ( wxTheBitmapList )
-        wxTheBitmapList->AddBitmap(this);
+#if WXWIN_COMPATIBILITY_2
+    pRefData->m_bOk = TRUE;
+#endif // WXWIN_COMPATIBILITY_2
+    return(TRUE);
 }
 
 }
 
-wxBitmap::wxBitmap(int w, int h, int d)
+bool wxBitmap::CopyFromCursor(
+  const wxCursor&                   rCursor
+)
 {
 {
-    (void)Create(w, h, d);
+    UnRef();
 
 
-    if ( wxTheBitmapList )
-        wxTheBitmapList->AddBitmap(this);
+    if (!rCursor.Ok())
+        return(FALSE);
+    return(CopyFromIconOrCursor(rCursor));
 }
 
 }
 
-wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
+bool wxBitmap::CopyFromIcon(
+  const wxIcon&                     rIcon
+)
 {
 {
-    (void) Create(data, type, width, height, depth);
-
-    if ( wxTheBitmapList )
-        wxTheBitmapList->AddBitmap(this);
-}
+    UnRef();
 
 
-wxBitmap::wxBitmap(const wxString& filename, long type)
-{
-    LoadFile(filename, (int)type);
+    if (!rIcon.Ok())
+        return(FALSE);
 
 
-    if ( wxTheBitmapList )
-        wxTheBitmapList->AddBitmap(this);
+    return CopyFromIconOrCursor(rIcon);
 }
 
 }
 
-/* TODO: maybe allow creation from XPM
-// Create from data
-wxBitmap::wxBitmap(const char **data)
+wxBitmap::~wxBitmap()
 {
 {
-    (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
+    if (wxTheBitmapList)
+        wxTheBitmapList->DeleteObject(this);
 }
 }
-*/
-
-bool wxBitmap::Create(int w, int h, int d)
-{
-    UnRef();
 
 
-    m_refData = new wxBitmapRefData;
+wxBitmap::wxBitmap(
+  const char                        zBits[]
+, int                               nTheWidth
+, int                               nTheHeight
+, int                               nNoBits
+)
+{
+    Init();
+
+    wxBitmapRefData*                pRefData = new wxBitmapRefData;
+    BITMAPINFOHEADER2               vHeader;
+    BITMAPINFO2                     vInfo;
+    HDC                             hDc;
+    HPS                             hPs;
+    DEVOPENSTRUC                    vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+    SIZEL                           vSize = {0, 0};
+
+    wxASSERT(vHabmain != NULL);
+
+    hDc = ::DevOpenDC(vHabmain, OD_MEMORY, (PSZ)"*", 1L, (PDEVOPENDATA)&vDop, 0L);
+
+    vHeader.cbFix           = sizeof(vHeader);
+    vHeader.cx              = (USHORT)nTheWidth;
+    vHeader.cy              = (USHORT)nTheHeight;
+    vHeader.cPlanes         = 1L;
+    vHeader.cBitCount       = nNoBits;
+    vHeader.ulCompression   = BCA_UNCOMP;
+    vHeader.cxResolution    = 0;
+    vHeader.cyResolution    = 0;
+    vHeader.cclrUsed        = 0;
+    vHeader.cclrImportant   = 0;
+    vHeader.usUnits         = BRU_METRIC;
+    vHeader.usRecording     = BRA_BOTTOMUP;
+    vHeader.usRendering     = BRH_NOTHALFTONED;
+    vHeader.cSize1          = 0;
+    vHeader.cSize2          = 0;
+    vHeader.ulColorEncoding = 0;
+    vHeader.ulIdentifier    = 0;
+
+    hPs = ::GpiCreatePS(vHabmain, hDc, &vSize, GPIA_ASSOC | PU_PELS);
+    if (hPs == 0)
+    {
+        wxLogLastError("GpiCreatePS Failure");
+    }
 
 
-    M_BITMAPDATA->m_width = w;
-    M_BITMAPDATA->m_height = h;
-    M_BITMAPDATA->m_depth = d;
+    m_refData = pRefData;
 
 
-    /* TODO: create new bitmap */
+    pRefData->m_nWidth = nTheWidth;
+    pRefData->m_nHeight = nTheHeight;
+    pRefData->m_nDepth = nNoBits;
+    pRefData->m_nNumColors = 0;
+    pRefData->m_pSelectedInto = NULL;
 
 
-    return M_BITMAPDATA->m_ok;
-}
+    HBITMAP hBmp = ::GpiCreateBitmap(hPs, &vHeader, 0L, NULL, &vInfo);
+    if (!hBmp)
+    {
+        wxLogLastError("CreateBitmap");
+    }
+    SetHBITMAP((WXHBITMAP)hBmp);
+}
+
+// Create from XPM data
+wxBitmap::wxBitmap(
+  char**                            ppData
+, wxControl*                        WXUNUSED(pAnItem))
+{
+    Init();
+
+    (void)Create( (void *)ppData
+                 ,wxBITMAP_TYPE_XPM_DATA
+                 ,0
+                 ,0
+                 ,0
+                );
+}
+
+wxBitmap::wxBitmap(
+  int                               nW
+, int                               nH
+, int                               nD
+)
+{
+    Init();
+
+    (void)Create( nW
+                 ,nH
+                 ,nD
+                );
+}
+
+wxBitmap::wxBitmap(
+  void*                             pData
+, long                              lType
+, int                               nWidth
+, int                               nHeight
+, int                               nDepth
+)
+{
+    Init();
+
+    (void)Create( pData
+                 ,lType
+                 ,nWidth
+                 ,nHeight
+                 ,nDepth
+                );
+}
+
+wxBitmap::wxBitmap(
+  const wxString&                   rFilename
+, long                              lType
+)
+{
+    Init();
+
+    LoadFile( rFilename
+             ,(int)lType
+            );
+}
+
+bool wxBitmap::Create(
+  int                               nW
+, int                               nH
+, int                               nD
+)
+{
+    HBITMAP                         hBmp;
+    BITMAPINFOHEADER2               vHeader;
+    BITMAPINFO2                     vInfo;
+    HPS                             hpsScreen;
+    HDC                             hdcScreen;
+    DEVOPENSTRUC                    vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+    SIZEL                           vSize = {0, 0};
+    LONG                            lBitCount;
+
+    wxASSERT(vHabmain != NULL);
+
+    hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
+    hdcScreen = ::GpiQueryDevice(hpsScreen);
+    ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitCount);
+
+    vHeader.cbFix           = sizeof(vHeader);
+    vHeader.cx              = (USHORT)nW;
+    vHeader.cy              = (USHORT)nH;
+    vHeader.cPlanes         = (USHORT)nD;
+    vHeader.cBitCount       = lBitCount;
+    vHeader.ulCompression   = BCA_UNCOMP;
+    vHeader.cxResolution    = 0;
+    vHeader.cyResolution    = 0;
+    vHeader.cclrUsed        = 0;
+    vHeader.cclrImportant   = 0;
+    vHeader.usUnits         = BRU_METRIC;
+    vHeader.usRecording     = BRA_BOTTOMUP;
+    vHeader.usRendering     = BRH_NOTHALFTONED;
+    vHeader.cSize1          = 0;
+    vHeader.cSize2          = 0;
+    vHeader.ulColorEncoding = 0;
+    vHeader.ulIdentifier    = 0;
 
 
-bool wxBitmap::LoadFile(const wxString& filename, long type)
-{
     UnRef();
     UnRef();
-
     m_refData = new wxBitmapRefData;
 
     m_refData = new wxBitmapRefData;
 
-    wxBitmapHandler *handler = FindHandler(type);
+    GetBitmapData()->m_nWidth = nW;
+    GetBitmapData()->m_nHeight = nH;
+    GetBitmapData()->m_nDepth = nD;
 
 
-    if ( handler == NULL ) {
-        wxLogWarning("no bitmap handler for type %d defined.", type);
-
-        return FALSE;
+    if (nD > 0)
+    {
+        hBmp = ::GpiCreateBitmap(hpsScreen, &vHeader, 0L, NULL, &vInfo);
+        if (!hBmp)
+        {
+            wxLogLastError("CreateBitmap");
+        }
+    }
+    else
+    {
+        LONG                            lPlanes;
+
+        ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes);
+        hBmp = ::GpiCreateBitmap(hpsScreen, &vHeader, 0L, NULL, &vInfo);
+        if (!hBmp)
+        {
+            wxLogLastError("CreateBitmap");
+        }
+        GetBitmapData()->m_nDepth = wxDisplayDepth();
     }
     }
+    SetHBITMAP((WXHBITMAP)hBmp);
 
 
-    return handler->LoadFile(this, filename, type, -1, -1);
+#if WXWIN_COMPATIBILITY_2
+    GetBitmapData()->m_bOk = hBmp != 0;
+#endif // WXWIN_COMPATIBILITY_2
+
+    return Ok();
 }
 
 }
 
-bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
+bool wxBitmap::LoadFile(
+  const wxString&                   rFilename
+, long                              lType
+)
 {
 {
-    UnRef();
+    HPS                             hPs = NULLHANDLE;
 
 
-    m_refData = new wxBitmapRefData;
+    UnRef();
 
 
-    wxBitmapHandler *handler = FindHandler(type);
+    wxBitmapHandler*                pHandler = wxDynamicCast( FindHandler(lType)
+                                                             ,wxBitmapHandler
+                                                            );
 
 
-    if ( handler == NULL ) {
-        wxLogWarning("no bitmap handler for type %d defined.", type);
+    if (pHandler)
+    {
+        m_refData = new wxBitmapRefData;
 
 
-        return FALSE;
+        return(pHandler->LoadFile( this
+                                  ,rFilename
+                                  ,hPs
+                                  ,lType
+                                  , -1
+                                  , -1
+                                 ));
     }
     }
+    else
+    {
+        wxImage                     vImage;
+
+        if (!vImage.LoadFile(rFilename, lType) || !vImage.Ok() )
+            return(FALSE);
 
 
-    return handler->Create(this, data, type, width, height, depth);
+        *this = vImage.ConvertToBitmap();
+
+        return(TRUE);
+    }
 }
 
 }
 
-bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
+bool wxBitmap::Create(
+  void*                             pData
+, long                              lType
+, int                               nWidth
+, int                               nHeight
+, int                               nDepth
+)
 {
 {
-    wxBitmapHandler *handler = FindHandler(type);
+    UnRef();
 
 
-    if ( handler == NULL ) {
-        wxLogWarning("no bitmap handler for type %d defined.", type);
+    wxBitmapHandler*                pHandler = wxDynamicCast( FindHandler(lType)
+                                                             ,wxBitmapHandler
+                                                            );
 
 
-        return FALSE;
-  }
+    if (!pHandler)
+    {
+        wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
+                       "type %d defined."), lType);
 
 
-  return handler->SaveFile(this, filename, type, palette);
-}
+        return(FALSE);
+    }
 
 
-void wxBitmap::SetWidth(int w)
-{
-    if (!M_BITMAPDATA)
-        m_refData = new wxBitmapRefData;
+    m_refData = new wxBitmapRefData;
 
 
-    M_BITMAPDATA->m_width = w;
+    return(pHandler->Create( this
+                            ,pData
+                            ,lType
+                            ,nWidth
+                            ,nHeight
+                            ,nDepth
+                           ));
 }
 
 }
 
-void wxBitmap::SetHeight(int h)
+bool wxBitmap::SaveFile(
+  const wxString&                   rFilename
+, int                               lType
+, const wxPalette*                  pPalette
+)
 {
 {
-    if (!M_BITMAPDATA)
-        m_refData = new wxBitmapRefData;
+    wxBitmapHandler*                pHandler = wxDynamicCast( FindHandler(lType)
+                                                             ,wxBitmapHandler
+                                                            );
 
 
-    M_BITMAPDATA->m_height = h;
-}
+    if (pHandler)
+    {
+        return pHandler->SaveFile( this
+                                  ,rFilename
+                                  ,lType
+                                  ,pPalette
+                                 );
+    }
+    else
+    {
+        // FIXME what about palette? shouldn't we use it?
+        wxImage                     vImage(*this);
 
 
-void wxBitmap::SetDepth(int d)
-{
-    if (!M_BITMAPDATA)
-        m_refData = new wxBitmapRefData;
+        if (!vImage.Ok())
+            return(FALSE);
 
 
-    M_BITMAPDATA->m_depth = d;
+        return(vImage.SaveFile( rFilename
+                               ,lType
+                              ));
+    }
 }
 
 }
 
-void wxBitmap::SetQuality(int q)
-{
-    if (!M_BITMAPDATA)
-        m_refData = new wxBitmapRefData;
-
-    M_BITMAPDATA->m_quality = q;
-}
+// ----------------------------------------------------------------------------
+// wxBitmap accessors
+// ----------------------------------------------------------------------------
 
 
-void wxBitmap::SetOk(bool isOk)
+void wxBitmap::SetQuality(
+  int                               nQ
+)
 {
 {
-    if (!M_BITMAPDATA)
-        m_refData = new wxBitmapRefData;
+    EnsureHasData();
 
 
-    M_BITMAPDATA->m_ok = isOk;
+    GetBitmapData()->m_nQuality = nQ;
 }
 
 }
 
-void wxBitmap::SetPalette(const wxPalette& palette)
+#if WXWIN_COMPATIBILITY_2
+void wxBitmap::SetOk(
+  bool                              bOk
+)
 {
 {
-    if (!M_BITMAPDATA)
-        m_refData = new wxBitmapRefData;
+    EnsureHasData();
 
 
-    M_BITMAPDATA->m_bitmapPalette = palette ;
+    GetBitmapData()->m_bOk = bOk;
 }
 }
+#endif // WXWIN_COMPATIBILITY_2
 
 
-void wxBitmap::SetMask(wxMask *mask)
+void wxBitmap::SetPalette(
+  const wxPalette&                  rPalette
+)
 {
 {
-    if (!M_BITMAPDATA)
-        m_refData = new wxBitmapRefData;
+    EnsureHasData();
 
 
-    M_BITMAPDATA->m_bitmapMask = mask ;
+    GetBitmapData()->m_vBitmapPalette = rPalette;
 }
 
 }
 
-void wxBitmap::AddHandler(wxBitmapHandler *handler)
+void wxBitmap::SetMask(
+  wxMask*                           pMask
+)
 {
 {
-    sm_handlers.Append(handler);
-}
+    EnsureHasData();
 
 
-void wxBitmap::InsertHandler(wxBitmapHandler *handler)
-{
-    sm_handlers.Insert(handler);
+    GetBitmapData()->m_pBitmapMask = pMask;
 }
 
 }
 
-bool wxBitmap::RemoveHandler(const wxString& name)
+// Will try something for OS/2 but not really sure how close
+// to the msw intent this is.
+wxBitmap wxBitmap::GetBitmapForDC(
+  wxDC&                             rDc
+) const
 {
 {
-    wxBitmapHandler *handler = FindHandler(name);
-    if ( handler )
-    {
-        sm_handlers.DeleteObject(handler);
-        return TRUE;
-    }
-    else
-        return FALSE;
-}
+    wxMemoryDC                      vMemDC;
+    wxBitmap                        vTmpBitmap( this->GetWidth()
+                                               ,this->GetHeight()
+                                               ,rDc.GetDepth()
+                                              );
+    WXHBITMAP                       vOldBitmap;
+    HPS                             hMemoryPS;
+    HPS                             hPs;
+    POINTL                          vPoint[4];
+    SIZEL                           vSize = {0,0};
 
 
-wxBitmapHandler *wxBitmap::FindHandler(const wxString& name)
-{
-    wxNode *node = sm_handlers.First();
-    while ( node )
-    {
-        wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
-        if ( handler->GetName() == name )
-            return handler;
-        node = node->Next();
-    }
-    return NULL;
-}
+    hMemoryPS = ::GpiCreatePS(vHabmain, (HDC)vMemDC.GetHDC(), &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
+    hPs       = ::GpiCreatePS(vHabmain, (HDC)rDc.GetHDC(), &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
 
 
-wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, long bitmapType)
-{
-    wxNode *node = sm_handlers.First();
-    while ( node )
-    {
-        wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
-        if ( handler->GetExtension() == extension &&
-                    (bitmapType == -1 || handler->GetType() == bitmapType) )
-            return handler;
-        node = node->Next();
-    }
-    return NULL;
-}
+    // TODO: Set the points
 
 
-wxBitmapHandler *wxBitmap::FindHandler(long bitmapType)
-{
-    wxNode *node = sm_handlers.First();
-    while ( node )
-    {
-        wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
-        if (handler->GetType() == bitmapType)
-            return handler;
-        node = node->Next();
-    }
-    return NULL;
+    vOldBitmap = (WXHBITMAP)::GpiSetBitmap(hPs, (HBITMAP)vTmpBitmap.GetHBITMAP());
+    ::GpiBitBlt(hPs, hMemoryPS, 4L, vPoint, ROP_SRCCOPY, BBO_IGNORE);
+
+    return(vTmpBitmap);
 }
 
 }
 
-/*
- * wxMask
- */
+// ----------------------------------------------------------------------------
+// wxMask
+// ----------------------------------------------------------------------------
 
 wxMask::wxMask()
 {
 
 wxMask::wxMask()
 {
-/* TODO
-    m_maskBitmap = 0;
-*/
+    m_hMaskBitmap = 0;
 }
 
 // Construct a mask from a bitmap and a colour indicating
 // the transparent area
 }
 
 // Construct a mask from a bitmap and a colour indicating
 // the transparent area
-wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
+wxMask::wxMask(
+  const wxBitmap&                   rBitmap
+, const wxColour&                   rColour
+)
 {
 {
-/* TODO
-    m_maskBitmap = 0;
-*/
-    Create(bitmap, colour);
+    m_hMaskBitmap = 0;
+    Create( rBitmap
+           ,rColour
+          );
 }
 
 // Construct a mask from a bitmap and a palette index indicating
 // the transparent area
 }
 
 // Construct a mask from a bitmap and a palette index indicating
 // the transparent area
-wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
+wxMask::wxMask(
+  const wxBitmap&                   rBitmap
+, int                               nPaletteIndex
+)
 {
 {
-/* TODO
-    m_maskBitmap = 0;
-*/
-
-    Create(bitmap, paletteIndex);
+    m_hMaskBitmap = 0;
+    Create( rBitmap
+           ,nPaletteIndex
+          );
 }
 
 // Construct a mask from a mono bitmap (copies the bitmap).
 }
 
 // Construct a mask from a mono bitmap (copies the bitmap).
-wxMask::wxMask(const wxBitmap& bitmap)
+wxMask::wxMask(
+  const wxBitmap&                   rBitmap
+)
 {
 {
-/* TODO
-    m_maskBitmap = 0;
-*/
-
-    Create(bitmap);
+    m_hMaskBitmap = 0;
+    Create(rBitmap);
 }
 
 wxMask::~wxMask()
 {
 }
 
 wxMask::~wxMask()
 {
-// TODO: delete mask bitmap
+    if (m_hMaskBitmap)
+        ::GpiDeleteBitmap((HBITMAP)m_hMaskBitmap);
 }
 
 // Create a mask from a mono bitmap (copies the bitmap).
 }
 
 // Create a mask from a mono bitmap (copies the bitmap).
-bool wxMask::Create(const wxBitmap& bitmap)
+bool wxMask::Create(
+  const wxBitmap&                   rBitmap
+)
 {
 {
-// TODO
-    return FALSE;
+    BITMAPINFOHEADER2               vHeader;
+    DEVOPENSTRUC                    vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+    SIZEL                           vSize = {0, 0};
+    POINTL                          vPoint[4];
+
+    if (m_hMaskBitmap)
+    {
+        ::GpiDeleteBitmap((HBITMAP) m_hMaskBitmap);
+        m_hMaskBitmap = 0;
+    }
+    if (!rBitmap.Ok() || rBitmap.GetDepth() != 1)
+    {
+        return(FALSE);
+    }
+    vHeader.cbFix           = sizeof(vHeader);
+    vHeader.cx              = (USHORT)rBitmap.GetWidth();
+    vHeader.cy              = (USHORT)rBitmap.GetHeight();
+    vHeader.cPlanes         = 1;
+    vHeader.cBitCount       = 1;
+
+    m_hMaskBitmap = (WXHBITMAP) ::GpiCreateBitmap( m_hPs
+                                                  ,&vHeader
+                                                  ,0L
+                                                  ,NULL
+                                                  ,NULL
+                                                 );
+
+    HPS srcPS = ::GpiCreatePS(vHabmain, m_hDc, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
+    ::GpiSetBitmap(srcPS, (HBITMAP)rBitmap.GetHBITMAP());
+    HPS destPS = ::GpiCreatePS(vHabmain, m_hDc, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
+    ::GpiSetBitmap(srcPS, (HBITMAP)m_hMaskBitmap);
+    // TODO: Set the point array
+    ::GpiBitBlt(destPS, srcPS, 4L, vPoint, ROP_SRCCOPY , BBO_IGNORE);
+
+    ::GpiDestroyPS(srcPS);
+    ::GpiDestroyPS(destPS);
+    return(TRUE);
 }
 
 // Create a mask from a bitmap and a palette index indicating
 // the transparent area
 }
 
 // Create a mask from a bitmap and a palette index indicating
 // the transparent area
-bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
+bool wxMask::Create(
+  const wxBitmap&                   rBitmap
+, int                               nPaletteIndex
+)
 {
 {
-// TODO
-    return FALSE;
+    if (m_hMaskBitmap)
+    {
+        ::GpiDeleteBitmap((HBITMAP) m_hMaskBitmap);
+        m_hMaskBitmap = 0;
+    }
+    if (rBitmap.Ok() && rBitmap.GetPalette()->Ok())
+    {
+        unsigned char               cRed;
+        unsigned char               cGreen;
+        unsigned char               cBlue;
+
+        if (rBitmap.GetPalette()->GetRGB( nPaletteIndex
+                                         ,&cRed
+                                         ,&cGreen
+                                         ,&cBlue
+                                        ))
+        {
+            wxColour                vTransparentColour( cRed
+                                                       ,cGreen
+                                                       ,cBlue
+                                                      );
+
+            return (Create( rBitmap
+                           ,vTransparentColour
+                          ));
+        }
+    }
+    return(FALSE);
 }
 
 // Create a mask from a bitmap and a colour indicating
 // the transparent area
 }
 
 // Create a mask from a bitmap and a colour indicating
 // the transparent area
-bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
-{
-// TODO
-    return FALSE;
-}
-
-/*
- * wxBitmapHandler
- */
-
-IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
-
-bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
-{
-    return FALSE;
-}
-
-bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type,
-        int desiredWidth, int desiredHeight)
-{
-    return FALSE;
-}
-
-bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
+bool wxMask::Create(
+  const wxBitmap&                   rBitmap
+, const wxColour&                   rColour
+)
 {
 {
-    return FALSE;
-}
+    BITMAPINFOHEADER2               vHeader;
+    DEVOPENSTRUC                    vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+    SIZEL                           vSize = {0, 0};
+    POINTL                          vPoint[4];
 
 
-/*
- * Standard handlers
- */
-
-/* TODO: bitmap handlers, a bit like this:
-class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
-{
-    DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
-public:
-    inline wxBMPResourceHandler()
+    if (m_hMaskBitmap)
     {
     {
-        m_name = "Windows bitmap resource";
-        m_extension = "";
-        m_type = wxBITMAP_TYPE_BMP_RESOURCE;
-    };
-
-    virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
-          int desiredWidth, int desiredHeight);
-};
-IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
-*/
-
-void wxBitmap::CleanUpHandlers()
-{
-    wxNode *node = sm_handlers.First();
-    while ( node )
+        ::GpiDeleteBitmap((HBITMAP) m_hMaskBitmap);
+        m_hMaskBitmap = 0;
+    }
+    if (!rBitmap.Ok())
     {
     {
-        wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
-        wxNode *next = node->Next();
-        delete handler;
-        delete node;
-        node = next;
+        return(FALSE);
     }
     }
-}
-
-void wxBitmap::InitStandardHandlers()
-{
-/* TODO: initialize all standard bitmap or derive class handlers here.
-    AddHandler(new wxBMPResourceHandler);
-    AddHandler(new wxBMPFileHandler);
-    AddHandler(new wxXPMFileHandler);
-    AddHandler(new wxXPMDataHandler);
-    AddHandler(new wxICOResourceHandler);
-    AddHandler(new wxICOFileHandler);
-*/
-}
 
 
-void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
-{
-  if (!M_BITMAPDATA)
-  m_refData = new wxBitmapRefData;
-
-  M_BITMAPDATA->m_hBitmap = bmp;
+    // scan the bitmap for the transparent colour and set
+    // the corresponding pixels in the mask to BLACK and
+    // the rest to WHITE
+    COLORREF                        vMaskColour = OS2RGB(rColour.Red(), rColour.Green(), rColour.Blue());
+
+    vHeader.cbFix           = sizeof(vHeader);
+    vHeader.cx              = (USHORT)rBitmap.GetWidth();
+    vHeader.cy              = (USHORT)rBitmap.GetHeight();
+    vHeader.cPlanes         = 1;
+    vHeader.cBitCount       = 1;
+
+    m_hMaskBitmap = (WXHBITMAP) ::GpiCreateBitmap( m_hPs
+                                                  ,&vHeader
+                                                  ,0L
+                                                  ,NULL
+                                                  ,NULL
+                                                 );
+
+    HPS srcPS = ::GpiCreatePS(vHabmain, m_hDc, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
+    ::GpiSetBitmap(srcPS, (HBITMAP)rBitmap.GetHBITMAP());
+    HPS destPS = ::GpiCreatePS(vHabmain, m_hDc, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
+    ::GpiSetBitmap(srcPS, (HBITMAP)m_hMaskBitmap);
+
+    // this is not very efficient, but I can't think
+    // of a better way of doing it
+    for (int w = 0; w < rBitmap.GetWidth(); w++)
+    {
+        for (int h = 0; h < rBitmap.GetHeight(); h++)
+        {
+            POINTL                  vPoint;
+
+            vPoint.x = w;
+            vPoint.y = h;
+
+            COLORREF                col = ::GpiQueryPel(srcPS, &vPoint);
+
+            if (col == vMaskColour)
+            {
+                ::GpiSetColor(destPS, CLR_WHITE);
+                ::GpiSetPel(destPS, &vPoint);
+            }
+            else
+            {
+                ::GpiSetColor(destPS, CLR_BLACK);
+                ::GpiSetPel(destPS, &vPoint);
+            }
+        }
+    }
+    ::GpiDestroyPS(srcPS);
+    ::GpiDestroyPS(destPS);
+    return(TRUE);
+}
+
+// ----------------------------------------------------------------------------
+// wxBitmapHandler
+// ----------------------------------------------------------------------------
+
+bool wxBitmapHandler::Create(
+  wxGDIImage*                       pImage
+, void*                             pData
+, long                              lFlags
+, int                               nWidth
+, int                               nHeight
+, int                               nDepth
+)
+{
+    wxBitmap*                       pBitmap = wxDynamicCast( pImage
+                                                            ,wxBitmap
+                                                           );
+
+    return(pBitmap ? Create( pBitmap
+                            ,pData
+                            ,nWidth
+                            ,nHeight
+                            ,nDepth
+                           ) : FALSE);
+}
+
+bool wxBitmapHandler::Load(
+  wxGDIImage*                       pImage
+, const wxString&                   rName
+, HPS                               hPs
+, long                              lFlags
+, int                               nWidth
+, int                               nHeight
+)
+{
+    wxBitmap*                       pBitmap = wxDynamicCast( pImage
+                                                            ,wxBitmap
+                                                           );
+
+    return(pBitmap ? LoadFile( pBitmap
+                              ,rName
+                              ,hPs
+                              ,lFlags
+                              ,nWidth
+                              ,nHeight
+                             ) : FALSE);
+}
+
+bool wxBitmapHandler::Save(
+  wxGDIImage*                       pImage
+, const wxString&                   rName
+, int                               lType
+)
+{
+    wxBitmap*                       pBitmap = wxDynamicCast( pImage
+                                                            ,wxBitmap
+                                                           );
+
+    return(pBitmap ? SaveFile( pBitmap
+                              ,rName
+                              ,lType
+                             ) : FALSE);
+}
+
+bool wxBitmapHandler::Create(
+  wxBitmap*                         WXUNUSED(pBitmap)
+, void*                             WXUNUSED(pData)
+, long                              WXUNUSED(lType)
+, int                               WXUNUSED(nWidth)
+, int                               WXUNUSED(nHeight)
+, int                               WXUNUSED(nDepth)
+)
+{
+    return(FALSE);
+}
+
+bool wxBitmapHandler::LoadFile(
+  wxBitmap*                         WXUNUSED(pBitmap)
+, const wxString&                   WXUNUSED(rName)
+, HPS                               WXUNUSED(hPs)
+, long                              WXUNUSED(lType)
+, int                               WXUNUSED(nDesiredWidth)
+, int                               WXUNUSED(nDesiredHeight)
+)
+{
+    return(FALSE);
+}
+
+bool wxBitmapHandler::SaveFile(
+  wxBitmap*                         WXUNUSED(pBitmap)
+, const wxString&                   WXUNUSED(rName)
+, int                               WXUNUSED(nType)
+, const wxPalette*                  WXUNUSED(pPalette)
+)
+{
+    return(FALSE);
 }
 
 }