From 1b7c01c976a2fd8a6ca3e46aab827dbc0b0e8da9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 31 Oct 2006 14:43:04 +0000 Subject: [PATCH] fix wxBitmap mask copying git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42850 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/bitmap.h | 3 +++ src/msw/bitmap.cpp | 54 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/include/wx/msw/bitmap.h b/include/wx/msw/bitmap.h index 4a02193bed..3d5fc34156 100644 --- a/include/wx/msw/bitmap.h +++ b/include/wx/msw/bitmap.h @@ -198,6 +198,9 @@ class WXDLLEXPORT wxMask : public wxObject public: wxMask(); + // Copy constructor + wxMask(const wxMask &mask); + // Construct a mask from a bitmap and a colour indicating the transparent // area wxMask(const wxBitmap& bitmap, const wxColour& colour); diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index f182df3c2a..20a2aa22b9 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -205,8 +205,13 @@ wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data) m_selectedInto = NULL; #endif - // can't copy the mask as the other bitmap destroys it + // (deep) copy the mask if present m_bitmapMask = NULL; + if (data.m_bitmapMask) + m_bitmapMask = new wxMask(*data.m_bitmapMask); + + // FIXME: we don't copy m_hBitmap currently but we should, see wxBitmap:: + // CloneRefData() wxASSERT_MSG( !data.m_isDIB, _T("can't copy bitmap locked for raw access!") ); @@ -252,6 +257,10 @@ wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *dataOrig) const if ( !data ) return NULL; + // FIXME: this method is backwards, it should just create a new + // wxBitmapRefData using its copy ctor but instead it modifies this + // bitmap itself and then returns its m_refData -- which works, of + // course (except in !wxUSE_WXDIB), but is completely illogical wxBitmap *self = wx_const_cast(wxBitmap *, this); #if wxUSE_WXDIB @@ -264,10 +273,19 @@ wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *dataOrig) const else #endif // wxUSE_WXDIB { - // don't copy the bitmap data, but do copy the size, depth, ... + // copy the bitmap data self->m_refData = new wxBitmapRefData(*data); } + // copy also the mask + wxMask * const maskSrc = data->GetMask(); + if ( maskSrc ) + { + wxBitmapRefData *selfdata = wx_static_cast(wxBitmapRefData *, m_refData); + + selfdata->SetMask(new wxMask(*maskSrc)); + } + return m_refData; } @@ -1327,6 +1345,38 @@ wxMask::wxMask() m_maskBitmap = 0; } +// Copy constructor +wxMask::wxMask(const wxMask &mask) +{ + BITMAP bmp; + + HDC srcDC = CreateCompatibleDC(0); + HDC destDC = CreateCompatibleDC(0); + + // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used + // so we'll use GetObject() API here: + if (::GetObject((HGDIOBJ)mask.m_maskBitmap, sizeof(bmp), &bmp) == 0) + { + wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy")); + return; + } + + // create our HBITMAP + int w = bmp.bmWidth, h = bmp.bmHeight; + m_maskBitmap = (WXHBITMAP)CreateCompatibleBitmap(srcDC, w, h); + + // copy the mask's HBITMAP into our HBITMAP + SelectObject(srcDC, (HBITMAP) mask.m_maskBitmap); + SelectObject(destDC, (HBITMAP) m_maskBitmap); + + BitBlt(destDC, 0, 0, w, h, srcDC, 0, 0, SRCCOPY); + + SelectObject(srcDC, 0); + DeleteDC(srcDC); + SelectObject(destDC, 0); + DeleteDC(destDC); +} + // Construct a mask from a bitmap and a colour indicating // the transparent area wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) -- 2.45.2