Move duplicated code to the shared function in wxImageList
This commit is contained in:
@@ -132,71 +132,65 @@ bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const
|
|||||||
// wxImageList operations
|
// wxImageList operations
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// Adds a bitmap, and optionally a mask bitmap.
|
namespace
|
||||||
// Note that wxImageList creates new bitmaps, so you may delete
|
{
|
||||||
// 'bitmap' and 'mask'.
|
void GetImageListBitmaps(const wxBitmap& bitmap, const wxBitmap& mask, bool useMask,
|
||||||
int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
|
AutoHBITMAP& hbmpRelease, AutoHBITMAP& hbmpMask, HBITMAP& hbmp)
|
||||||
{
|
{
|
||||||
HBITMAP hbmp = NULL;
|
|
||||||
|
|
||||||
#if wxUSE_WXDIB && wxUSE_IMAGE
|
#if wxUSE_WXDIB && wxUSE_IMAGE
|
||||||
// wxBitmap normally stores alpha in pre-multiplied format but
|
// wxBitmap normally stores alpha in pre-multiplied format but
|
||||||
// ImageList_Draw() does pre-multiplication internally so we need to undo
|
// ImageList_Draw() does pre-multiplication internally so we need to undo
|
||||||
// the pre-multiplication here. Converting back and forth like this is, of
|
// the pre-multiplication here. Converting back and forth like this is, of
|
||||||
// course, very inefficient but it's better than wrong appearance so we do
|
// course, very inefficient but it's better than wrong appearance so we do
|
||||||
// this for now until a better way can be found.
|
// this for now until a better way can be found.
|
||||||
AutoHBITMAP hbmpRelease;
|
if ( useMask )
|
||||||
if ( bitmap.HasAlpha() )
|
|
||||||
{
|
{
|
||||||
wxImage img;
|
if ( bitmap.HasAlpha() )
|
||||||
if ( m_useMask )
|
|
||||||
{
|
{
|
||||||
// Remove alpha channel from image to prevent
|
// Remove alpha channel from image to prevent
|
||||||
// possible interferences with the mask.
|
// possible interferences with the mask.
|
||||||
// The bitmap isn't drawn correctly if we use both.
|
// The bitmap isn't drawn correctly if we use both.
|
||||||
img = bitmap.ConvertToImage();
|
wxImage img = bitmap.ConvertToImage();
|
||||||
img.ClearAlpha();
|
img.ClearAlpha();
|
||||||
|
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
||||||
|
hbmpRelease.Init(hbmp);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
wxBitmap bmpWithoutMask = bitmap;
|
hbmp = GetHbitmapOf(bitmap);
|
||||||
if ( mask.IsOk() || bmpWithoutMask.GetMask() )
|
}
|
||||||
|
|
||||||
|
hbmpMask.Init(GetMaskForImage(bitmap, mask));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ( bitmap.HasAlpha() )
|
||||||
|
{
|
||||||
|
wxBitmap bmp(bitmap);
|
||||||
|
if ( mask.IsOk() || bmp.GetMask() )
|
||||||
{
|
{
|
||||||
// Blend mask with alpha channel.
|
// Blend mask with alpha channel.
|
||||||
if ( mask.IsOk() )
|
if ( mask.IsOk() )
|
||||||
{
|
{
|
||||||
bmpWithoutMask.SetMask(new wxMask(mask));
|
bmp.SetMask(new wxMask(mask));
|
||||||
}
|
}
|
||||||
bmpWithoutMask.MSWBlendMaskWithAlpha();
|
bmp.MSWBlendMaskWithAlpha();
|
||||||
}
|
}
|
||||||
img = bmpWithoutMask.ConvertToImage();
|
wxImage img = bmp.ConvertToImage();
|
||||||
}
|
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
||||||
|
hbmpRelease.Init(hbmp);
|
||||||
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
|
||||||
hbmpRelease.Init(hbmp);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ( m_useMask )
|
|
||||||
{
|
|
||||||
hbmp = GetHbitmapOf(bitmap);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( mask.IsOk() || bitmap.GetMask() )
|
if ( mask.IsOk() || bitmap.GetMask() )
|
||||||
{
|
{
|
||||||
// Convert mask to alpha channel.
|
// Convert mask to alpha channel.
|
||||||
wxBitmap bmpWithMask;
|
wxBitmap bmp(bitmap);
|
||||||
if ( mask.IsOk() )
|
if ( mask.IsOk() )
|
||||||
{
|
{
|
||||||
bmpWithMask = bitmap;
|
bmp.SetMask(new wxMask(mask));
|
||||||
bmpWithMask.SetMask(new wxMask(mask));
|
|
||||||
}
|
}
|
||||||
else
|
wxImage img = bmp.ConvertToImage();
|
||||||
{
|
|
||||||
bmpWithMask = bitmap;
|
|
||||||
}
|
|
||||||
wxImage img = bmpWithMask.ConvertToImage();
|
|
||||||
img.InitAlpha();
|
img.InitAlpha();
|
||||||
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
||||||
hbmpRelease.Init(hbmp);
|
hbmpRelease.Init(hbmp);
|
||||||
@@ -210,12 +204,18 @@ int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
|
|||||||
#else
|
#else
|
||||||
hbmp = GetHbitmapOf(bitmap);
|
hbmp = GetHbitmapOf(bitmap);
|
||||||
#endif // wxUSE_WXDIB && wxUSE_IMAGE
|
#endif // wxUSE_WXDIB && wxUSE_IMAGE
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Use mask only if we don't have alpha, the bitmap isn't drawn correctly
|
// Adds a bitmap, and optionally a mask bitmap.
|
||||||
// if we use both.
|
// Note that wxImageList creates new bitmaps, so you may delete
|
||||||
|
// 'bitmap' and 'mask'.
|
||||||
|
int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
|
||||||
|
{
|
||||||
|
HBITMAP hbmp = NULL;
|
||||||
|
AutoHBITMAP hbmpRelease;
|
||||||
AutoHBITMAP hbmpMask;
|
AutoHBITMAP hbmpMask;
|
||||||
if ( m_useMask )
|
GetImageListBitmaps(bitmap, mask, m_useMask, hbmpRelease, hbmpMask, hbmp);
|
||||||
hbmpMask.Init(GetMaskForImage(bitmap, mask));
|
|
||||||
|
|
||||||
int index = ImageList_Add(GetHImageList(), hbmp, hbmpMask);
|
int index = ImageList_Add(GetHImageList(), hbmp, hbmpMask);
|
||||||
if ( index == -1 )
|
if ( index == -1 )
|
||||||
@@ -296,78 +296,9 @@ bool wxImageList::Replace(int index,
|
|||||||
const wxBitmap& mask)
|
const wxBitmap& mask)
|
||||||
{
|
{
|
||||||
HBITMAP hbmp = NULL;
|
HBITMAP hbmp = NULL;
|
||||||
|
|
||||||
#if wxUSE_WXDIB && wxUSE_IMAGE
|
|
||||||
// See the comment in Add() above.
|
|
||||||
AutoHBITMAP hbmpRelease;
|
AutoHBITMAP hbmpRelease;
|
||||||
if ( bitmap.HasAlpha() )
|
|
||||||
{
|
|
||||||
wxImage img;
|
|
||||||
if ( m_useMask )
|
|
||||||
{
|
|
||||||
// Remove alpha channel from image to prevent
|
|
||||||
// possible interferences with the mask.
|
|
||||||
// The bitmap isn't drawn correctly if we use both.
|
|
||||||
img = bitmap.ConvertToImage();
|
|
||||||
img.ClearAlpha();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
wxBitmap bmpWithoutMask = bitmap;
|
|
||||||
if ( mask.IsOk() || bmpWithoutMask.GetMask() )
|
|
||||||
{
|
|
||||||
// Blend mask with alpha channel.
|
|
||||||
if ( mask.IsOk() )
|
|
||||||
{
|
|
||||||
bmpWithoutMask.SetMask(new wxMask(mask));
|
|
||||||
}
|
|
||||||
bmpWithoutMask.MSWBlendMaskWithAlpha();
|
|
||||||
}
|
|
||||||
img = bmpWithoutMask.ConvertToImage();
|
|
||||||
}
|
|
||||||
|
|
||||||
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
|
||||||
hbmpRelease.Init(hbmp);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ( m_useMask )
|
|
||||||
{
|
|
||||||
hbmp = GetHbitmapOf(bitmap);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ( mask.IsOk() || bitmap.GetMask() )
|
|
||||||
{
|
|
||||||
// Convert mask to alpha channel.
|
|
||||||
wxBitmap bmpWithMask;
|
|
||||||
if ( mask.IsOk() )
|
|
||||||
{
|
|
||||||
bmpWithMask = bitmap;
|
|
||||||
bmpWithMask.SetMask(new wxMask(mask));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
bmpWithMask = bitmap;
|
|
||||||
}
|
|
||||||
wxImage img = bmpWithMask.ConvertToImage();
|
|
||||||
img.InitAlpha();
|
|
||||||
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
|
||||||
hbmpRelease.Init(hbmp);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
hbmp = GetHbitmapOf(bitmap);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
hbmp = GetHbitmapOf(bitmap);
|
|
||||||
#endif // wxUSE_WXDIB && wxUSE_IMAGE
|
|
||||||
|
|
||||||
AutoHBITMAP hbmpMask;
|
AutoHBITMAP hbmpMask;
|
||||||
if ( m_useMask )
|
GetImageListBitmaps(bitmap, mask, m_useMask, hbmpRelease, hbmpMask, hbmp);
|
||||||
hbmpMask.Init(GetMaskForImage(bitmap, mask));
|
|
||||||
|
|
||||||
if ( !ImageList_Replace(GetHImageList(), index, hbmp, hbmpMask) )
|
if ( !ImageList_Replace(GetHImageList(), index, hbmp, hbmpMask) )
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user