Make GetImageListBitmaps() member function of wxImageList

No real changes, just simplify the code a bit by using a member function
as this avoids having to pass m_useMask to it separately.
This commit is contained in:
Vadim Zeitlin
2022-05-05 19:06:18 +01:00
parent 68dee47694
commit dcc39d942f
2 changed files with 46 additions and 25 deletions

View File

@@ -200,6 +200,14 @@ protected:
wxSize m_size; wxSize m_size;
private: private:
// Private helper used by GetImageListBitmaps().
class wxMSWBitmaps;
// Fills the provided output "bitmaps" object with the actual bitmaps we need
// to use with the native control.
void GetImageListBitmaps(wxMSWBitmaps& bitmaps,
const wxBitmap& bitmap, const wxBitmap& mask);
bool m_useMask; bool m_useMask;
void Init() void Init()

View File

@@ -137,16 +137,36 @@ bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const
// wxImageList operations // wxImageList operations
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
namespace class wxImageList::wxMSWBitmaps
{ {
void GetImageListBitmaps(const wxBitmap& bitmap, const wxBitmap& mask, bool useMask, public:
AutoHBITMAP& hbmpRelease, AutoHBITMAP& hbmpMask, HBITMAP& hbmp) wxMSWBitmaps() : hbmp(NULL) { }
// These fields are filled by GetImageListBitmaps().
HBITMAP hbmp;
AutoHBITMAP hbmpMask;
private:
// This one is only used to delete a temporary bitmap, if necessary, and
// shouldn't be used otherwise, so it's private.
AutoHBITMAP hbmpRelease;
friend void wxImageList::GetImageListBitmaps(wxMSWBitmaps&,
const wxBitmap&,
const wxBitmap&);
wxDECLARE_NO_COPY_CLASS(wxMSWBitmaps);
};
void
wxImageList::GetImageListBitmaps(wxMSWBitmaps& bitmaps,
const wxBitmap& bitmap, const wxBitmap& mask)
{ {
#if wxUSE_WXDIB && wxUSE_IMAGE #if wxUSE_WXDIB && wxUSE_IMAGE
// We can only use directly bitmaps without alpha and without mask unless // We can only use directly bitmaps without alpha and without mask unless
// the image list uses masks and need to modify bitmap in all the other // the image list uses masks and need to modify bitmap in all the other
// cases, so check if this is necessary. // cases, so check if this is necessary.
if ( bitmap.HasAlpha() || (!useMask && (mask.IsOk() || bitmap.GetMask())) ) if ( bitmap.HasAlpha() || (!m_useMask && (mask.IsOk() || bitmap.GetMask())) )
{ {
wxBitmap bmp(bitmap); wxBitmap bmp(bitmap);
@@ -170,8 +190,8 @@ void GetImageListBitmaps(const wxBitmap& bitmap, const wxBitmap& mask, bool useM
wxImage img = bmp.ConvertToImage(); wxImage img = bmp.ConvertToImage();
if ( !img.HasAlpha() ) if ( !img.HasAlpha() )
img.InitAlpha(); img.InitAlpha();
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach(); bitmaps.hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
hbmpRelease.Init(hbmp); bitmaps.hbmpRelease.Init(bitmaps.hbmp);
// In any case we'll never use mask at the native image list level as // In any case we'll never use mask at the native image list level as
// it's incompatible with alpha and we need to use alpha. // it's incompatible with alpha and we need to use alpha.
@@ -179,24 +199,21 @@ void GetImageListBitmaps(const wxBitmap& bitmap, const wxBitmap& mask, bool useM
else else
#endif // wxUSE_WXDIB && wxUSE_IMAGE #endif // wxUSE_WXDIB && wxUSE_IMAGE
{ {
hbmp = GetHbitmapOf(bitmap); bitmaps.hbmp = GetHbitmapOf(bitmap);
if ( useMask ) if ( m_useMask )
hbmpMask.Init(GetMaskForImage(bitmap, mask)); bitmaps.hbmpMask.Init(GetMaskForImage(bitmap, mask));
} }
} }
} // anonymous namespace
// Adds a bitmap, and optionally a mask bitmap. // Adds a bitmap, and optionally a mask bitmap.
// Note that wxImageList creates new bitmaps, so you may delete // Note that wxImageList creates new bitmaps, so you may delete
// 'bitmap' and 'mask'. // 'bitmap' and 'mask'.
int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
{ {
HBITMAP hbmp = NULL; wxMSWBitmaps bitmaps;
AutoHBITMAP hbmpRelease; GetImageListBitmaps(bitmaps, bitmap, mask);
AutoHBITMAP hbmpMask;
GetImageListBitmaps(bitmap, mask, m_useMask, hbmpRelease, hbmpMask, hbmp);
int index = ImageList_Add(GetHImageList(), hbmp, hbmpMask); int index = ImageList_Add(GetHImageList(), bitmaps.hbmp, bitmaps.hbmpMask);
if ( index == -1 ) if ( index == -1 )
{ {
wxLogError(_("Couldn't add an image to the image list.")); wxLogError(_("Couldn't add an image to the image list."));
@@ -210,14 +227,12 @@ int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
// 'bitmap'. // 'bitmap'.
int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
{ {
HBITMAP hbmp = NULL; wxMSWBitmaps bitmaps;
AutoHBITMAP hbmpRelease;
AutoHBITMAP hbmpMask;
wxMask mask(bitmap, maskColour); wxMask mask(bitmap, maskColour);
GetImageListBitmaps(bitmap, mask.GetBitmap(), m_useMask, hbmpRelease, hbmpMask, hbmp); GetImageListBitmaps(bitmaps, bitmap, mask.GetBitmap());
int index = ImageList_AddMasked(GetHImageList(), int index = ImageList_AddMasked(GetHImageList(),
hbmp, bitmaps.hbmp,
wxColourToRGB(maskColour)); wxColourToRGB(maskColour));
if ( index == -1 ) if ( index == -1 )
{ {
@@ -259,12 +274,10 @@ bool wxImageList::Replace(int index,
const wxBitmap& bitmap, const wxBitmap& bitmap,
const wxBitmap& mask) const wxBitmap& mask)
{ {
HBITMAP hbmp = NULL; wxMSWBitmaps bitmaps;
AutoHBITMAP hbmpRelease; GetImageListBitmaps(bitmaps, bitmap, mask);
AutoHBITMAP hbmpMask;
GetImageListBitmaps(bitmap, mask, m_useMask, hbmpRelease, hbmpMask, hbmp);
if ( !ImageList_Replace(GetHImageList(), index, hbmp, hbmpMask) ) if ( !ImageList_Replace(GetHImageList(), index, bitmaps.hbmp, bitmaps.hbmpMask) )
{ {
wxLogLastError(wxT("ImageList_Replace()")); wxLogLastError(wxT("ImageList_Replace()"));
return false; return false;