X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6faac21b0bf815c01b1d98cf3ea3310bb5238f6f..fca418ae12286d47a0e8490185daa84fde9f20e9:/src/msw/bitmap.cpp diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index b174fdf9de..a07199e234 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -1,5 +1,5 @@ //////////////////////////////////////////////////////////////////////////// -// Name: bitmap.cpp +// Name: src/msw/bitmap.cpp // Purpose: wxBitmap // Author: Julian Smart // Modified by: @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "bitmap.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -28,6 +24,8 @@ #pragma hdrstop #endif +#include "wx/bitmap.h" + #ifndef WX_PRECOMP #include @@ -36,22 +34,21 @@ #include "wx/app.h" #include "wx/palette.h" #include "wx/dcmemory.h" - #include "wx/bitmap.h" #include "wx/icon.h" + #include "wx/log.h" + #include "wx/image.h" #endif #include "wx/msw/private.h" -#include "wx/log.h" #if wxUSE_WXDIB -#include "wx/msw/dib.h" + #include "wx/msw/dib.h" #endif -#include "wx/image.h" #include "wx/xpmdecod.h" #ifdef wxHAVE_RAW_BITMAP -#include "wx/rawbmp.h" + #include "wx/rawbmp.h" #endif // missing from mingw32 header @@ -67,6 +64,7 @@ class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData { public: wxBitmapRefData(); + wxBitmapRefData(const wxBitmapRefData& data); virtual ~wxBitmapRefData() { Free(); } virtual void Free(); @@ -120,7 +118,9 @@ private: // optional mask for transparent drawing wxMask *m_bitmapMask; - DECLARE_NO_COPY_CLASS(wxBitmapRefData) + + // not implemented + wxBitmapRefData& operator=(const wxBitmapRefData&); }; // ---------------------------------------------------------------------------- @@ -197,7 +197,24 @@ wxBitmapRefData::wxBitmapRefData() #endif m_isDIB = - m_hasAlpha = FALSE; + m_hasAlpha = false; +} + +wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data) + : wxGDIImageRefData(data) +{ +#ifdef __WXDEBUG__ + m_selectedInto = NULL; +#endif + + // can't copy the mask as the other bitmap destroys it + m_bitmapMask = NULL; + + wxASSERT_MSG( !data.m_isDIB, + _T("can't copy bitmap locked for raw access!") ); + m_isDIB = false; + + m_hasAlpha = data.m_hasAlpha; } void wxBitmapRefData::Free() @@ -225,15 +242,35 @@ void wxBitmapRefData::Free() // wxBitmap creation // ---------------------------------------------------------------------------- -// this function should be called from all wxBitmap ctors -void wxBitmap::Init() +wxGDIImageRefData *wxBitmap::CreateData() const { - // m_refData = NULL; done in the base class ctor + return new wxBitmapRefData; } -wxGDIImageRefData *wxBitmap::CreateData() const +wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *dataOrig) const { - return new wxBitmapRefData; + const wxBitmapRefData * + data = wx_static_cast(const wxBitmapRefData *, dataOrig); + if ( !data ) + return NULL; + + wxBitmap *self = wx_const_cast(wxBitmap *, this); + +#if wxUSE_WXDIB + // copy the other bitmap + if ( data->m_hBitmap ) + { + wxDIB dib((HBITMAP)(data->m_hBitmap)); + self->CopyFromDIB(dib); + } + else +#endif // wxUSE_WXDIB + { + // don't copy the bitmap data, but do copy the size, depth, ... + self->m_refData = new wxBitmapRefData(*data); + } + + return m_refData; } #ifdef __WIN32__ @@ -264,10 +301,37 @@ bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon) refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor; - // the mask returned by GetIconInfo() is inversed compared to the usual - // wxWin convention - refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h)); - +#if wxUSE_WXDIB + // If the icon is 32 bits per pixel then it may have alpha channel data, + // although there are some icons that are 32 bpp but have no alpha... So + // convert to a DIB and manually check the 4th byte for each pixel. + BITMAP bm; + if ( ::GetObject(iconInfo.hbmColor, sizeof(BITMAP), (LPVOID)&bm) + && bm.bmBitsPixel == 32) + { + wxDIB dib(iconInfo.hbmColor); + if (dib.IsOk()) + { + unsigned char* pixels = dib.GetData(); + for (int idx=0; idxm_hasAlpha = true; + break; + } + } + } + } +#endif + if ( !refData->m_hasAlpha ) + { + // the mask returned by GetIconInfo() is inverted compared to the usual + // wxWin convention + refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h)); + } // delete the old one now as we don't need it any more ::DeleteObject(iconInfo.hbmMask); @@ -286,7 +350,7 @@ bool wxBitmap::CopyFromCursor(const wxCursor& cursor) UnRef(); if ( !cursor.Ok() ) - return FALSE; + return false; return CopyFromIconOrCursor(cursor); } @@ -296,7 +360,7 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) UnRef(); if ( !icon.Ok() ) - return FALSE; + return false; return CopyFromIconOrCursor(icon); } @@ -305,12 +369,12 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) bool wxBitmap::CopyFromDIB(const wxDIB& dib) { - wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") ); + wxCHECK_MSG( dib.IsOk(), false, _T("invalid DIB in CopyFromDIB") ); #ifdef SOMETIMES_USE_DIB HBITMAP hbitmap = dib.CreateDDB(); if ( !hbitmap ) - return FALSE; + return false; #else // ALWAYS_USE_DIB HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB @@ -336,7 +400,7 @@ bool wxBitmap::CopyFromDIB(const wxDIB& dib) delete palette; #endif // wxUSE_PALETTE - return TRUE; + return true; } #endif // NEVER_USE_DIB @@ -347,8 +411,6 @@ wxBitmap::~wxBitmap() wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) { - Init(); - #ifndef __WXMICROWIN__ wxBitmapRefData *refData = new wxBitmapRefData; m_refData = refData; @@ -362,7 +424,7 @@ wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) { // we assume that it is in XBM format which is not quite the same as // the format CreateBitmap() wants because the order of bytes in the - // line is inversed! + // line is reversed! const size_t bytesPerLine = (width + 7) / 8; const size_t padding = bytesPerLine % 2; const size_t len = height * ( padding + bytesPerLine ); @@ -380,7 +442,7 @@ wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) for ( int bits = 0; bits < 8; bits++) { reversed <<= 1; - reversed |= (val & 0x01); + reversed |= (unsigned char)(val & 0x01); val >>= 1; } *dst++ = reversed; @@ -412,53 +474,40 @@ wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) } // Create from XPM data -#if wxUSE_IMAGE && wxUSE_XPM bool wxBitmap::CreateFromXpm(const char **data) -#else -bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data)) -#endif { -#if wxUSE_IMAGE && wxUSE_XPM - Init(); - - wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") ) +#if wxUSE_IMAGE && wxUSE_XPM && wxUSE_WXDIB + wxCHECK_MSG( data != NULL, false, wxT("invalid bitmap data") ); wxXPMDecoder decoder; wxImage img = decoder.ReadData(data); - wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) + wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") ); *this = wxBitmap(img); - return TRUE; + return true; #else - return FALSE; + wxUnusedVar(data); + return false; #endif } wxBitmap::wxBitmap(int w, int h, int d) { - Init(); - (void)Create(w, h, d); } wxBitmap::wxBitmap(int w, int h, const wxDC& dc) { - Init(); - (void)Create(w, h, dc); } wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) { - Init(); - (void)Create(data, type, width, height, depth); } wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) { - Init(); - LoadFile(filename, (int)type); } @@ -469,7 +518,7 @@ bool wxBitmap::Create(int width, int height, int depth) bool wxBitmap::Create(int width, int height, const wxDC& dc) { - wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") ); + wxCHECK_MSG( dc.Ok(), false, _T("invalid HDC in wxBitmap::Create()") ); return DoCreate(width, height, -1, dc.GetHDC()); } @@ -496,12 +545,12 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) wxDIB dib(w, h, d); if ( !dib.IsOk() ) - return FALSE; + return false; // don't delete the DIB section in dib object dtor hbmp = dib.Detach(); - GetBitmapData()->m_isDIB = TRUE; + GetBitmapData()->m_isDIB = true; GetBitmapData()->m_depth = d; } else // create a DDB @@ -576,7 +625,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) if (hBitmap == NULL) { ::ReleaseDC(NULL, hScreenDC); - return FALSE; + return false; } HDC hMemDC = ::CreateCompatibleDC(hScreenDC); @@ -602,7 +651,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) if (!hMaskBitmap) { - hasMask = FALSE; + hasMask = false; } else { @@ -613,7 +662,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap); } #else - hasMask = FALSE; + hasMask = false; #endif } @@ -660,7 +709,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) SetPalette(image.GetPalette()); #endif // wxUSE_PALETTE - return TRUE; + return true; } wxImage wxBitmap::ConvertToImage() const @@ -732,8 +781,6 @@ wxImage wxBitmap::ConvertToImage() const // wxImage to/from conversions // ---------------------------------------------------------------------------- -#if wxUSE_WXDIB - bool wxBitmap::CreateFromImage(const wxImage& image, int depth) { return CreateFromImage(image, depth, 0); @@ -741,15 +788,17 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth) bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc) { - wxCHECK_MSG( dc.Ok(), FALSE, + wxCHECK_MSG( dc.Ok(), false, _T("invalid HDC in wxBitmap::CreateFromImage()") ); return CreateFromImage(image, -1, dc.GetHDC()); } +#if wxUSE_WXDIB + bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) { - wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ); + wxCHECK_MSG( image.Ok(), false, wxT("invalid image") ); UnRef(); @@ -759,8 +808,10 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) wxDIB dib(image); if ( !dib.IsOk() ) - return FALSE; + return false; + if ( depth == -1 ) + depth = dib.GetDepth(); // Get depth from image if none specified // store the bitmap parameters wxBitmapRefData *refData = new wxBitmapRefData; @@ -782,15 +833,15 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) // don't delete the DIB section in dib object dtor hbitmap = dib.Detach(); - refData->m_isDIB = TRUE; - refData->m_depth = dib.GetDepth(); + refData->m_isDIB = true; + refData->m_depth = depth; } #ifndef ALWAYS_USE_DIB else // we need to convert DIB to DDB { hbitmap = dib.CreateDDB((HDC)hdc); - refData->m_depth = depth == -1 ? dib.GetDepth() : depth; + refData->m_depth = depth; } #endif // !ALWAYS_USE_DIB @@ -800,12 +851,45 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) // finally also set the mask if we have one if ( image.HasMask() ) { - SetMask(new wxMask(*this, wxColour(image.GetMaskRed(), - image.GetMaskGreen(), - image.GetMaskBlue()))); + const size_t len = 2*((w+15)/16); + BYTE *src = image.GetData(); + BYTE *data = new BYTE[h*len]; + memset(data, 0, h*len); + BYTE r = image.GetMaskRed(), + g = image.GetMaskGreen(), + b = image.GetMaskBlue(); + BYTE *dst = data; + for ( int y = 0; y < h; y++, dst += len ) + { + BYTE *dstLine = dst; + BYTE mask = 0x80; + for ( int x = 0; x < w; x++, src += 3 ) + { + if (src[0] != r || src[1] != g || src[2] != b) + *dstLine |= mask; + + if ( (mask >>= 1) == 0 ) + { + dstLine++; + mask = 0x80; + } + } + } + + hbitmap = ::CreateBitmap(w, h, 1, 1, data); + if ( !hbitmap ) + { + wxLogLastError(_T("CreateBitmap(mask)")); + } + else + { + SetMask(new wxMask((WXHBITMAP)hbitmap)); + } + + delete[] data; } - return TRUE; + return true; } wxImage wxBitmap::ConvertToImage() const @@ -891,7 +975,22 @@ wxImage wxBitmap::ConvertToImage() const return image; } -#endif // wxUSE_WXDIB +#else // !wxUSE_WXDIB + +bool +wxBitmap::CreateFromImage(const wxImage& WXUNUSED(image), + int WXUNUSED(depth), + WXHDC WXUNUSED(hdc)) +{ + return false; +} + +wxImage wxBitmap::ConvertToImage() const +{ + return wxImage(); +} + +#endif // wxUSE_WXDIB/!wxUSE_WXDIB #endif // wxUSE_IMAGE @@ -911,7 +1010,7 @@ bool wxBitmap::LoadFile(const wxString& filename, long type) return handler->LoadFile(this, filename, type, -1, -1); } -#if wxUSE_IMAGE +#if wxUSE_IMAGE && wxUSE_WXDIB else // no bitmap handler found { wxImage image; @@ -919,12 +1018,12 @@ bool wxBitmap::LoadFile(const wxString& filename, long type) { *this = wxBitmap(image); - return TRUE; + return true; } } #endif // wxUSE_IMAGE - return FALSE; + return false; } bool wxBitmap::Create(void *data, long type, int width, int height, int depth) @@ -937,7 +1036,7 @@ bool wxBitmap::Create(void *data, long type, int width, int height, int depth) { wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type); - return FALSE; + return false; } m_refData = new wxBitmapRefData; @@ -955,7 +1054,7 @@ bool wxBitmap::SaveFile(const wxString& filename, { return handler->SaveFile(this, filename, type, palette); } -#if wxUSE_IMAGE +#if wxUSE_IMAGE && wxUSE_WXDIB else // no bitmap handler found { // FIXME what about palette? shouldn't we use it? @@ -967,7 +1066,7 @@ bool wxBitmap::SaveFile(const wxString& filename, } #endif // wxUSE_IMAGE - return FALSE; + return false; } // ---------------------------------------------------------------------------- @@ -982,11 +1081,13 @@ wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const (rect.y+rect.height <= GetHeight()), wxNullBitmap, wxT("Invalid bitmap or bitmap region") ); - wxBitmap ret( rect.width, rect.height ); + wxBitmap ret( rect.width, rect.height, GetDepth() ); wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); #ifndef __WXMICROWIN__ - // TODO: copy alpha channel data if any + // handle alpha channel, if any + if (HasAlpha()) + ret.UseAlpha(); // copy bitmap data MemoryHDC dcSrc, @@ -1047,6 +1148,15 @@ wxMask *wxBitmap::GetMask() const return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL; } +wxBitmap wxBitmap::GetMaskBitmap() const +{ + wxBitmap bmp; + wxMask *mask = GetMask(); + if ( mask ) + bmp.SetHBITMAP(mask->GetMaskBitmap()); + return bmp; +} + #ifdef __WXDEBUG__ wxDC *wxBitmap::GetSelectedInto() const @@ -1094,7 +1204,7 @@ void wxBitmap::SetSelectedInto(wxDC *dc) void wxBitmap::SetPalette(const wxPalette& palette) { - EnsureHasData(); + AllocExclusive(); GetBitmapData()->m_bitmapPalette = palette; } @@ -1103,7 +1213,7 @@ void wxBitmap::SetPalette(const wxPalette& palette) void wxBitmap::SetMask(wxMask *mask) { - EnsureHasData(); + AllocExclusive(); GetBitmapData()->SetMask(mask); } @@ -1136,7 +1246,7 @@ void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) HBITMAP hDIB; if ( !GetBitmapData()->m_isDIB ) { - wxCHECK_MSG( !GetBitmapData()->m_dib, FALSE, + wxCHECK_MSG( !GetBitmapData()->m_dib, NULL, _T("GetRawData() may be called only once") ); wxDIB *dib = new wxDIB(*this); @@ -1180,7 +1290,11 @@ void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) data.m_height = h; // remember that DIBs are stored in top to bottom order! - const LONG bytesPerRow = ds.dsBm.bmWidthBytes; + // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a + // multiple of 2, as required by the documentation. So we use the official + // formula, which we already use elsewhere.) + const LONG bytesPerRow = + wxDIB::GetLineSize(ds.dsBm.bmWidth, ds.dsBm.bmBitsPixel); data.m_stride = -bytesPerRow; char *bits = (char *)ds.dsBm.bmBits; @@ -1201,12 +1315,7 @@ void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) if ( !Ok() ) return; - // the cast is ugly but we can't do without it and without making this - // function template (and hence inline) unfortunately - typedef wxPixelData PixelData; - PixelData& data = (PixelData &)dataBase; - - if ( !data ) + if ( !&dataBase ) { // invalid data, don't crash -- but don't assert neither as we're // called automatically from wxPixelDataBase dtor and so there is no @@ -1214,36 +1323,6 @@ void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) return; } - if ( GetBitmapData()->m_hasAlpha ) - { - // AlphaBlend() wants to have premultiplied source alpha but - // wxRawBitmap API uses normal, not premultiplied, colours, so adjust - // them here now - PixelData::Iterator p(data); - - const int w = data.GetWidth(); - const int h = data.GetHeight(); - - for ( int y = 0; y < h; y++ ) - { - PixelData::Iterator rowStart = p; - - for ( int x = 0; x < w; x++ ) - { - const unsigned alpha = p.Alpha(); - - p.Red() = (p.Red() * alpha + 127) / 255; - p.Blue() = (p.Blue() * alpha + 127) / 255; - p.Green() = (p.Green() * alpha + 127) / 255; - - ++p; - } - - p = rowStart; - p.OffsetY(data, 1); - } - } - // if we're a DDB we need to convert DIB back to DDB now to make the // changes made via raw bitmap access effective if ( !GetBitmapData()->m_isDIB ) @@ -1301,7 +1380,7 @@ wxMask::~wxMask() bool wxMask::Create(const wxBitmap& bitmap) { #ifndef __WXMICROWIN__ - wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE, + wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, false, _T("can't create mask from invalid or not monochrome bitmap") ); if ( m_maskBitmap ) @@ -1324,9 +1403,10 @@ bool wxMask::Create(const wxBitmap& bitmap) DeleteDC(srcDC); SelectObject(destDC, 0); DeleteDC(destDC); - return TRUE; + return true; #else - return FALSE; + wxUnusedVar(bitmap); + return false; #endif } @@ -1352,7 +1432,7 @@ bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) } #endif // wxUSE_PALETTE - return FALSE; + return false; } // Create a mask from a bitmap and a colour indicating @@ -1360,7 +1440,7 @@ bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) { #ifndef __WXMICROWIN__ - wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") ); + wxCHECK_MSG( bitmap.Ok(), false, _T("invalid bitmap in wxMask::Create") ); if ( m_maskBitmap ) { @@ -1383,7 +1463,7 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) wxLogLastError(wxT("CreateCompatibleDC")); } - bool ok = TRUE; + bool ok = true; // SelectObject() will fail wxASSERT_MSG( !bitmap.GetSelectedInto(), @@ -1394,7 +1474,7 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) { wxLogLastError(wxT("SelectObject")); - ok = FALSE; + ok = false; } HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap); @@ -1402,7 +1482,7 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) { wxLogLastError(wxT("SelectObject")); - ok = FALSE; + ok = false; } if ( ok ) @@ -1421,7 +1501,9 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) return ok; #else // __WXMICROWIN__ - return FALSE; + wxUnusedVar(bitmap); + wxUnusedVar(colour); + return false; #endif // __WXMICROWIN__/!__WXMICROWIN__ } @@ -1436,7 +1518,7 @@ bool wxBitmapHandler::Create(wxGDIImage *image, { wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); - return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE; + return bitmap ? Create(bitmap, data, flags, width, height, depth) : false; } bool wxBitmapHandler::Load(wxGDIImage *image, @@ -1446,7 +1528,7 @@ bool wxBitmapHandler::Load(wxGDIImage *image, { wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); - return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE; + return bitmap ? LoadFile(bitmap, name, flags, width, height) : false; } bool wxBitmapHandler::Save(wxGDIImage *image, @@ -1455,7 +1537,7 @@ bool wxBitmapHandler::Save(wxGDIImage *image, { wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); - return bitmap ? SaveFile(bitmap, name, type) : FALSE; + return bitmap ? SaveFile(bitmap, name, type) : false; } bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap), @@ -1465,7 +1547,7 @@ bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(height), int WXUNUSED(depth)) { - return FALSE; + return false; } bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap), @@ -1474,7 +1556,7 @@ bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(desiredWidth), int WXUNUSED(desiredHeight)) { - return FALSE; + return false; } bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap), @@ -1482,7 +1564,7 @@ bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(type), const wxPalette *WXUNUSED(palette)) { - return FALSE; + return false; } // ---------------------------------------------------------------------------- @@ -1513,7 +1595,7 @@ bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel, // this value must be 1, 4, 8 or 24 so PixelDepth can only be lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel); lpDIBheader->bmiHeader.biCompression = BI_RGB; - lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3; + lpDIBheader->bmiHeader.biSizeImage = (xSize * abs(ySize) * bitsPerPixel) >> 3; lpDIBheader->bmiHeader.biClrUsed = 256; @@ -1527,7 +1609,7 @@ bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel, *lpDIBHeader = lpDIBheader; - return TRUE; + return true; } void wxFreeDIB(LPBITMAPINFO lpDIBHeader) @@ -1553,7 +1635,33 @@ HICON wxBitmapToIconOrCursor(const wxBitmap& bmp, return 0; } - wxMask *mask = bmp.GetMask(); + if ( bmp.HasAlpha() ) + { + // Create an empty mask bitmap. + // it doesn't seem to work if we mess with the mask at all. + HBITMAP hMonoBitmap = CreateBitmap(bmp.GetWidth(),bmp.GetHeight(),1,1,NULL); + + ICONINFO iconInfo; + wxZeroMemory(iconInfo); + iconInfo.fIcon = iconWanted; // do we want an icon or a cursor? + if ( !iconWanted ) + { + iconInfo.xHotspot = hotSpotX; + iconInfo.yHotspot = hotSpotY; + } + + iconInfo.hbmMask = hMonoBitmap; + iconInfo.hbmColor = GetHbitmapOf(bmp); + + HICON hicon = ::CreateIconIndirect(&iconInfo); + + ::DeleteObject(hMonoBitmap); + + return hicon; + } + + wxMask* mask = bmp.GetMask(); + if ( !mask ) { // we must have a mask for an icon, so even if it's probably incorrect, @@ -1590,7 +1698,7 @@ HICON wxBitmapToIconOrCursor(const wxBitmap& bmp, HICON hicon = ::CreateIconIndirect(&iconInfo); - if ( !bmp.GetMask() ) + if ( !bmp.GetMask() && !bmp.HasAlpha() ) { // we created the mask, now delete it delete mask; @@ -1604,12 +1712,12 @@ HICON wxBitmapToIconOrCursor(const wxBitmap& bmp, HICON wxBitmapToHICON(const wxBitmap& bmp) { - return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0); + return wxBitmapToIconOrCursor(bmp, true, 0, 0); } HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY) { - return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY); + return (HCURSOR)wxBitmapToIconOrCursor(bmp, false, hotSpotX, hotSpotY); } HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) @@ -1651,7 +1759,7 @@ HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) // Deselect objects SelectObject(hdcSrc,srcTmp); SelectObject(hdcDst,dstTmp); - + ::DeleteDC(hdcSrc); ::DeleteDC(hdcDst);