From 2410fdb89d1d2ba7f36f92786b58b1844741076e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 18 Feb 2014 15:14:34 +0000 Subject: [PATCH] Fix transparency in toolbar buttons when not using comctl32.dll v6. Old versions of comctl32.dll don't support alpha in the toolbar image list, so use the masks only for them. This is a backport of r75785, r75794 and r75923 from trunk. Closes #2609. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_3_0_BRANCH@75924 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/msw/imaglist.cpp | 64 +++++++++++++++++++++++++++++++++++++------- src/msw/toolbar.cpp | 5 +++- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/msw/imaglist.cpp b/src/msw/imaglist.cpp index 928a9f6b74..7e100c32e0 100644 --- a/src/msw/imaglist.cpp +++ b/src/msw/imaglist.cpp @@ -24,6 +24,7 @@ #endif #ifndef WX_PRECOMP + #include "wx/app.h" #include "wx/msw/wrapcctl.h" // include "properly" #include "wx/window.h" #include "wx/icon.h" @@ -87,7 +88,8 @@ bool wxImageList::Create(int width, int height, bool mask, int initial) flags |= ILC_COLOR32; #endif - if ( mask ) + // For comctl32.dll < 6 always use masks as it doesn't support alpha. + if ( mask || wxApp::GetComCtl32Version() < 600 ) flags |= ILC_MASK; // Grow by 1, I guess this is reasonable behaviour most of the time @@ -140,6 +142,7 @@ bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) { HBITMAP hbmp; + bool useMask; #if wxUSE_WXDIB && wxUSE_IMAGE // wxBitmap normally stores alpha in pre-multiplied format but @@ -150,15 +153,35 @@ int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) AutoHBITMAP hbmpRelease; if ( bitmap.HasAlpha() ) { - hbmp = wxDIB(bitmap.ConvertToImage(), - wxDIB::PixelFormat_NotPreMultiplied).Detach(); + wxImage img = bitmap.ConvertToImage(); + + // For comctl32.dll < 6 remove alpha channel from image + // to prevent possible interferences with the mask. + if ( wxApp::GetComCtl32Version() < 600 ) + { + img.ClearAlpha(); + useMask = true; + } + else + { + useMask = false; + } + + hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach(); hbmpRelease.Init(hbmp); } else #endif // wxUSE_WXDIB && wxUSE_IMAGE + { hbmp = GetHbitmapOf(bitmap); + useMask = true; + } - AutoHBITMAP hbmpMask(GetMaskForImage(bitmap, mask)); + // Use mask only if we don't have alpha, the bitmap isn't drawn correctly + // if we use both. + AutoHBITMAP hbmpMask; + if ( useMask ) + hbmpMask.Init(GetMaskForImage(bitmap, mask)); int index = ImageList_Add(GetHImageList(), hbmp, hbmpMask); if ( index == -1 ) @@ -181,8 +204,14 @@ int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) AutoHBITMAP hbmpRelease; if ( bitmap.HasAlpha() ) { - hbmp = wxDIB(bitmap.ConvertToImage(), - wxDIB::PixelFormat_NotPreMultiplied).Detach(); + wxImage img = bitmap.ConvertToImage(); + + if ( wxApp::GetComCtl32Version() < 600 ) + { + img.ClearAlpha(); + } + + hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach(); hbmpRelease.Init(hbmp); } else @@ -220,21 +249,38 @@ bool wxImageList::Replace(int index, const wxBitmap& mask) { HBITMAP hbmp; + bool useMask; #if wxUSE_WXDIB && wxUSE_IMAGE // See the comment in Add() above. AutoHBITMAP hbmpRelease; if ( bitmap.HasAlpha() ) { - hbmp = wxDIB(bitmap.ConvertToImage(), - wxDIB::PixelFormat_NotPreMultiplied).Detach(); + wxImage img = bitmap.ConvertToImage(); + + if ( wxApp::GetComCtl32Version() < 600 ) + { + img.ClearAlpha(); + useMask = true; + } + else + { + useMask = false; + } + + hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach(); hbmpRelease.Init(hbmp); } else #endif // wxUSE_WXDIB && wxUSE_IMAGE + { hbmp = GetHbitmapOf(bitmap); + useMask = true; + } - AutoHBITMAP hbmpMask(GetMaskForImage(bitmap, mask)); + AutoHBITMAP hbmpMask; + if ( useMask ) + hbmpMask.Init(GetMaskForImage(bitmap, mask)); if ( !ImageList_Replace(GetHImageList(), index, hbmp, hbmpMask) ) { diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index a328e95446..05ff1534f5 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -663,7 +663,10 @@ void wxToolBar::CreateDisabledImageList() ( sizeBitmap.x, sizeBitmap.y, - bmpDisabled.GetMask() != NULL, + // Don't use mask if we have alpha + // (wxImageList will fall back to + // mask if alpha not supported) + !bmpDisabled.HasAlpha(), GetToolsCount() ); break;