X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d88de032d3009be88d0f0ba4a882784aa9493331..d6c9c1b71e069396bbe3850862de9aa10e6812e0:/src/os2/bitmap.cpp diff --git a/src/os2/bitmap.cpp b/src/os2/bitmap.cpp index 93ea059994..2d7a3fc799 100644 --- a/src/os2/bitmap.cpp +++ b/src/os2/bitmap.cpp @@ -9,6 +9,10 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// +#ifdef __GNUG__ + #pragma implementation "bitmap.h" +#endif + // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -27,535 +31,783 @@ #include "wx/os2/private.h" #include "wx/log.h" +//#include "wx/msw/dib.h" +#include "wx/image.h" + // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- -#if !USE_SHARED_LIBRARIES IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) -#endif -wxBitmapRefData::wxBitmapRefData() -{ - m_ok = FALSE; - m_width = 0; - m_height = 0; - m_depth = 0; - m_quality = 0; - m_hBitmap = 0 ; - m_selectedInto = NULL; - m_numColors = 0; - m_bitmapMask = NULL; -} - -wxBitmapRefData::~wxBitmapRefData() -{ - if (m_selectedInto) - { - wxChar buf[200]; - wxSprintf(buf, wxT("Bitmap was deleted without selecting out of wxMemoryDC %lX."), (unsigned long) m_selectedInto); - wxFatalError(buf); - } - if (m_hBitmap) - { -// TODO: DeleteObject((HBITMAP) m_hBitmap); - } - m_hBitmap = 0 ; +IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) - if (m_bitmapMask) - delete m_bitmapMask; - m_bitmapMask = NULL; -} +// ============================================================================ +// implementation +// ============================================================================ -wxList wxBitmap::sm_handlers; +// ---------------------------------------------------------------------------- +// wxBitmapRefData +// ---------------------------------------------------------------------------- -wxBitmap::wxBitmap() +wxBitmapRefData::wxBitmapRefData() { - m_refData = NULL; - - if ( wxTheBitmapList ) - wxTheBitmapList->AddBitmap(this); + m_nQuality = 0; + m_pSelectedInto = NULL; + m_nNumColors = 0; + m_pBitmapMask = NULL; } -wxBitmap::wxBitmap(const wxBitmap& bitmap) +void wxBitmapRefData::Free() { -// TODO: -/* - wxIcon *icon = wxDynamicCast(&bitmap, wxIcon); - if ( icon ) - { - HDC hdc = ::CreateCompatibleDC(NULL); // screen DC - HBITMAP hbitmap = ::CreateCompatibleBitmap(hdc, - icon->GetWidth(), - icon->GetHeight()); - ::SelectObject(hdc, hbitmap); - ::DrawIcon(hdc, 0, 0, (HICON)icon->GetHICON()); + wxASSERT_MSG( !m_pSelectedInto, + wxT("deleting bitmap still selected into wxMemoryDC") ); - ::DeleteDC(hdc); - - SetHBITMAP((WXHBITMAP)hbitmap); - } - else + if (m_hBitmap) { - Ref(bitmap); + if ( !::GpiDeleteBitmap((HBITMAP)m_hBitmap) ) + { + wxLogLastError("GpiDeleteBitmap(hbitmap)"); + } } - if ( wxTheBitmapList ) - wxTheBitmapList->AddBitmap(this); -*/ -} - -wxBitmap::~wxBitmap() -{ - if (wxTheBitmapList) - wxTheBitmapList->DeleteObject(this); -} - -bool wxBitmap::FreeResource(bool WXUNUSED(force)) -{ - if ( !M_BITMAPDATA ) - return FALSE; - - if (M_BITMAPDATA->m_selectedInto) - { - wxChar buf[200]; - wxSprintf(buf, wxT("Bitmap %lX was deleted without selecting out of wxMemoryDC %lX."), (unsigned long) this, (unsigned long) M_BITMAPDATA->m_selectedInto); - wxFatalError(buf); - } - if (M_BITMAPDATA->m_hBitmap) - { -// TODO: DeleteObject((HBITMAP) M_BITMAPDATA->m_hBitmap); - } - M_BITMAPDATA->m_hBitmap = 0 ; - -/* - if (M_BITMAPDATA->m_bitmapPalette) - delete M_BITMAPDATA->m_bitmapPalette; - - M_BITMAPDATA->m_bitmapPalette = NULL ; -*/ - - return TRUE; + delete m_pBitmapMask; + m_pBitmapMask = NULL; } +// ---------------------------------------------------------------------------- +// wxBitmap creation +// ---------------------------------------------------------------------------- -wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) +// this function should be called from all wxBitmap ctors +void wxBitmap::Init() { - m_refData = new wxBitmapRefData; - - M_BITMAPDATA->m_width = the_width ; - M_BITMAPDATA->m_height = the_height ; - M_BITMAPDATA->m_depth = no_bits ; - M_BITMAPDATA->m_numColors = 0; + // m_refData = NULL; done in the base class ctor - /* TODO: create the bitmap from data */ - - if ( wxTheBitmapList ) + if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); } -// Create from XPM data -wxBitmap::wxBitmap(char **data, wxControl *WXUNUSED(anItem)) +bool wxBitmap::CopyFromIconOrCursor( + const wxGDIImage& rIcon +) { - (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0); -} + wxBitmapRefData* pRefData = new wxBitmapRefData; -wxBitmap::wxBitmap(int w, int h, int d) -{ - (void)Create(w, h, d); + m_refData = pRefData; - if ( wxTheBitmapList ) - wxTheBitmapList->AddBitmap(this); -} + pRefData->m_nWidth = rIcon.GetWidth(); + pRefData->m_nHeight = rIcon.GetHeight(); + pRefData->m_nDepth = wxDisplayDepth(); -wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) -{ - (void) Create(data, type, width, height, depth); + 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(const wxString& filename, long type) +bool wxBitmap::CopyFromCursor( + const wxCursor& rCursor +) { - LoadFile(filename, (int)type); + UnRef(); - if ( wxTheBitmapList ) - wxTheBitmapList->AddBitmap(this); + if (!rCursor.Ok()) + return(FALSE); + return(CopyFromIconOrCursor(rCursor)); } -bool wxBitmap::Create(int w, int h, int d) +bool wxBitmap::CopyFromIcon( + const wxIcon& rIcon +) { UnRef(); - m_refData = new wxBitmapRefData; - - M_BITMAPDATA->m_width = w; - M_BITMAPDATA->m_height = h; - M_BITMAPDATA->m_depth = d; - - /* TODO: create new bitmap */ + if (!rIcon.Ok()) + return(FALSE); - return M_BITMAPDATA->m_ok; + return CopyFromIconOrCursor(rIcon); } -bool wxBitmap::LoadFile(const wxString& filename, long type) +wxBitmap::~wxBitmap() { - UnRef(); + if (wxTheBitmapList) + wxTheBitmapList->DeleteObject(this); +} - 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"); + } - wxBitmapHandler *handler = FindHandler(type); + m_refData = pRefData; - if ( handler == NULL ) { - wxLogWarning("no bitmap handler for type %d defined.", type); + pRefData->m_nWidth = nTheWidth; + pRefData->m_nHeight = nTheHeight; + pRefData->m_nDepth = nNoBits; + pRefData->m_nNumColors = 0; + pRefData->m_pSelectedInto = NULL; - return FALSE; + HBITMAP hBmp = ::GpiCreateBitmap(hPs, &vHeader, 0L, NULL, &vInfo); + if (!hBmp) + { + wxLogLastError("CreateBitmap"); } - - return handler->LoadFile(this, filename, type, -1, -1); + SetHBITMAP((WXHBITMAP)hBmp); } -bool wxBitmap::Create(void *data, long type, int width, int height, int depth) -{ - UnRef(); +// 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; + UnRef(); m_refData = new wxBitmapRefData; - wxBitmapHandler *handler = FindHandler(type); - - if ( handler == NULL ) { - wxLogWarning("no bitmap handler for type %d defined.", type); + GetBitmapData()->m_nWidth = nW; + GetBitmapData()->m_nHeight = nH; + GetBitmapData()->m_nDepth = nD; - 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); + +#if WXWIN_COMPATIBILITY_2 + GetBitmapData()->m_bOk = hBmp != 0; +#endif // WXWIN_COMPATIBILITY_2 - return handler->Create(this, data, type, width, height, depth); + return Ok(); } -bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette) +bool wxBitmap::LoadFile( + const wxString& rFilename +, long lType +) { - wxBitmapHandler *handler = FindHandler(type); - - if ( handler == NULL ) { - wxLogWarning("no bitmap handler for type %d defined.", type); + HPS hPs = NULLHANDLE; - return FALSE; - } + UnRef(); - return handler->SaveFile(this, filename, type, palette); -} + wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType) + ,wxBitmapHandler + ); -void wxBitmap::SetWidth(int w) -{ - if (!M_BITMAPDATA) + if (pHandler) + { m_refData = new wxBitmapRefData; - M_BITMAPDATA->m_width = w; -} + return(pHandler->LoadFile( this + ,rFilename + ,hPs + ,lType + , -1 + , -1 + )); + } + else + { + wxImage vImage; -void wxBitmap::SetHeight(int h) -{ - if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + if (!vImage.LoadFile(rFilename, lType) || !vImage.Ok() ) + return(FALSE); - M_BITMAPDATA->m_height = h; + *this = vImage.ConvertToBitmap(); + + return(TRUE); + } } -void wxBitmap::SetDepth(int d) +bool wxBitmap::Create( + void* pData +, long lType +, int nWidth +, int nHeight +, int nDepth +) { - if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + UnRef(); - M_BITMAPDATA->m_depth = d; -} + wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType) + ,wxBitmapHandler + ); -void wxBitmap::SetQuality(int q) -{ - if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + if (!pHandler) + { + wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for " + "type %d defined."), lType); - M_BITMAPDATA->m_quality = q; -} + return(FALSE); + } -void wxBitmap::SetOk(bool isOk) -{ - if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + m_refData = new wxBitmapRefData; - M_BITMAPDATA->m_ok = isOk; + return(pHandler->Create( this + ,pData + ,lType + ,nWidth + ,nHeight + ,nDepth + )); } -void wxBitmap::SetPalette(const wxPalette& palette) +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_bitmapPalette = palette ; -} + if (pHandler) + { + return pHandler->SaveFile( this + ,rFilename + ,lType + ,pPalette + ); + } + else + { + // FIXME what about palette? shouldn't we use it? + wxImage vImage(*this); -void wxBitmap::SetMask(wxMask *mask) -{ - if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + if (!vImage.Ok()) + return(FALSE); - M_BITMAPDATA->m_bitmapMask = mask ; + return(vImage.SaveFile( rFilename + ,lType + )); + } } -void wxBitmap::SetHBITMAP(WXHBITMAP bmp) +// ---------------------------------------------------------------------------- +// wxBitmap accessors +// ---------------------------------------------------------------------------- + +void wxBitmap::SetQuality( + int nQ +) { - if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + EnsureHasData(); - M_BITMAPDATA->m_hBitmap = bmp; + GetBitmapData()->m_nQuality = nQ; } -void wxBitmap::AddHandler(wxBitmapHandler *handler) +#if WXWIN_COMPATIBILITY_2 +void wxBitmap::SetOk( + bool bOk +) { - sm_handlers.Append(handler); -} + EnsureHasData(); -void wxBitmap::InsertHandler(wxBitmapHandler *handler) -{ - sm_handlers.Insert(handler); + GetBitmapData()->m_bOk = bOk; } +#endif // WXWIN_COMPATIBILITY_2 -bool wxBitmap::RemoveHandler(const wxString& name) +void wxBitmap::SetPalette( + const wxPalette& rPalette +) { - wxBitmapHandler *handler = FindHandler(name); - if ( handler ) - { - sm_handlers.DeleteObject(handler); - return TRUE; - } - else - return FALSE; -} + EnsureHasData(); -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; + GetBitmapData()->m_vBitmapPalette = rPalette; } -wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, long bitmapType) +void wxBitmap::SetMask( + wxMask* pMask +) { - 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; -} + EnsureHasData(); -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; + GetBitmapData()->m_pBitmapMask = pMask; } -// Creates a bitmap that matches the device context, from -// an arbitray bitmap. At present, the original bitmap must have an -// associated palette. TODO: use a default palette if no palette exists. -// Contributed by Frederic Villeneuve -wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const +// Will try something for OS/2 but not really sure how close +// to the msw intent this is. +wxBitmap wxBitmap::GetBitmapForDC( + wxDC& rDc +) const { - wxBitmap tmpBitmap(this->GetWidth(), this->GetHeight(), dc.GetDepth()); -// TODO: -/* - wxMemoryDC memDC; - HPALETTE hPal = (HPALETTE) NULL; - LPBITMAPINFO lpDib; - void *lpBits = (void*) NULL; + wxMemoryDC vMemDC; + wxBitmap vTmpBitmap( this->GetWidth() + ,this->GetHeight() + ,rDc.GetDepth() + ); + WXHBITMAP vOldBitmap; + HPS hMemoryPS; + HPS hPs; + POINTL vPoint[4]; + SIZEL vSize = {0,0}; + 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); - wxASSERT( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) ); + // TODO: Set the points - tmpBitmap.SetPalette(this->GetPalette()); - memDC.SelectObject(tmpBitmap); - memDC.SetPalette(this->GetPalette()); + vOldBitmap = (WXHBITMAP)::GpiSetBitmap(hPs, (HBITMAP)vTmpBitmap.GetHBITMAP()); + ::GpiBitBlt(hPs, hMemoryPS, 4L, vPoint, ROP_SRCCOPY, BBO_IGNORE); - hPal = (HPALETTE) this->GetPalette()->GetHPALETTE(); - - if( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) ) - { - tmpBitmap.SetPalette(* this->GetPalette()); - memDC.SelectObject(tmpBitmap); - memDC.SetPalette(* this->GetPalette()); - hPal = (HPALETTE) this->GetPalette()->GetHPALETTE(); - } - else - { - hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); - wxPalette palette; - palette.SetHPALETTE( (WXHPALETTE)hPal ); - tmpBitmap.SetPalette( palette ); - memDC.SelectObject(tmpBitmap); - memDC.SetPalette( palette ); - } - - // set the height negative because in a DIB the order of the lines is reversed - createDIB(this->GetWidth(), -this->GetHeight(), this->GetDepth(), hPal, &lpDib); - - lpBits = malloc(lpDib->bmiHeader.biSizeImage); - - ::GetBitmapBits((HBITMAP)GetHBITMAP(), lpDib->bmiHeader.biSizeImage, lpBits); - - ::SetDIBitsToDevice((HDC) memDC.GetHDC(), 0, 0, this->GetWidth(), this->GetHeight(), - 0, 0, 0, this->GetHeight(), lpBits, lpDib, DIB_RGB_COLORS); - - free(lpBits); - - freeDIB(lpDib); -*/ - return (tmpBitmap); + return(vTmpBitmap); } -/* - * wxMask - */ +// ---------------------------------------------------------------------------- +// wxMask +// ---------------------------------------------------------------------------- wxMask::wxMask() { - m_maskBitmap = 0; + m_hMaskBitmap = 0; } // 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 +) { - 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 -wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) +wxMask::wxMask( + const wxBitmap& rBitmap +, int nPaletteIndex +) { - m_maskBitmap = 0; - Create(bitmap, paletteIndex); + m_hMaskBitmap = 0; + Create( rBitmap + ,nPaletteIndex + ); } // Construct a mask from a mono bitmap (copies the bitmap). -wxMask::wxMask(const wxBitmap& bitmap) +wxMask::wxMask( + const wxBitmap& rBitmap +) { - m_maskBitmap = 0; - Create(bitmap); + m_hMaskBitmap = 0; + Create(rBitmap); } wxMask::~wxMask() { -// TODO: delete mask bitmap + if (m_hMaskBitmap) + ::GpiDeleteBitmap((HBITMAP)m_hMaskBitmap); } // 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 -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 -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) +bool wxMask::Create( + const wxBitmap& rBitmap +, const wxColour& rColour +) { - return FALSE; -} - -bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) -{ - return FALSE; -} - -/* - * Standard handlers - */ + BITMAPINFOHEADER2 vHeader; + DEVOPENSTRUC vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL }; + SIZEL vSize = {0, 0}; + POINTL vPoint[4]; -/* 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) -*/ + ::GpiDeleteBitmap((HBITMAP) m_hMaskBitmap); + m_hMaskBitmap = 0; + } + if (!rBitmap.Ok()) + { + return(FALSE); + } -void wxBitmap::CleanUpHandlers() -{ - wxNode *node = sm_handlers.First(); - while ( node ) + // 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++) { - wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); - wxNode *next = node->Next(); - delete handler; - delete node; - node = next; + 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); } -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); -*/ +// ---------------------------------------------------------------------------- +// 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); }