Always store wxBitmap objects in wxGenericImageList
There doesn't seem to be any point in storing pointers to wxBitmap or wxIcon and storing the objects directly allows to avoid an extra heap allocation and all the code dealing with freeing memory when replacing or removing images from the list, making things much simpler. Also use wxVector<> for storage instead of the obsolete and ugly wxObjectList. There shouldn't be any user-visible changes.
This commit is contained in:
@@ -10,10 +10,11 @@
|
|||||||
#ifndef _WX_IMAGLISTG_H_
|
#ifndef _WX_IMAGLISTG_H_
|
||||||
#define _WX_IMAGLISTG_H_
|
#define _WX_IMAGLISTG_H_
|
||||||
|
|
||||||
|
#include "wx/bitmap.h"
|
||||||
#include "wx/gdicmn.h"
|
#include "wx/gdicmn.h"
|
||||||
|
#include "wx/vector.h"
|
||||||
|
|
||||||
class WXDLLIMPEXP_FWD_CORE wxDC;
|
class WXDLLIMPEXP_FWD_CORE wxDC;
|
||||||
class WXDLLIMPEXP_FWD_CORE wxBitmap;
|
|
||||||
class WXDLLIMPEXP_FWD_CORE wxIcon;
|
class WXDLLIMPEXP_FWD_CORE wxIcon;
|
||||||
class WXDLLIMPEXP_FWD_CORE wxColour;
|
class WXDLLIMPEXP_FWD_CORE wxColour;
|
||||||
|
|
||||||
@@ -35,8 +36,9 @@ public:
|
|||||||
int Add( const wxBitmap& bitmap, const wxColour& maskColour );
|
int Add( const wxBitmap& bitmap, const wxColour& maskColour );
|
||||||
wxBitmap GetBitmap(int index) const;
|
wxBitmap GetBitmap(int index) const;
|
||||||
wxIcon GetIcon(int index) const;
|
wxIcon GetIcon(int index) const;
|
||||||
bool Replace( int index, const wxBitmap &bitmap );
|
bool Replace( int index,
|
||||||
bool Replace( int index, const wxBitmap &bitmap, const wxBitmap& mask );
|
const wxBitmap& bitmap,
|
||||||
|
const wxBitmap& mask = wxNullBitmap );
|
||||||
bool Remove( int index );
|
bool Remove( int index );
|
||||||
bool RemoveAll();
|
bool RemoveAll();
|
||||||
|
|
||||||
@@ -55,7 +57,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
const wxBitmap *DoGetPtr(int index) const;
|
const wxBitmap *DoGetPtr(int index) const;
|
||||||
|
|
||||||
wxObjectList m_images;
|
wxVector<wxBitmap> m_images;
|
||||||
|
|
||||||
// Size of a single bitmap in the list.
|
// Size of a single bitmap in the list.
|
||||||
wxSize m_size;
|
wxSize m_size;
|
||||||
|
@@ -42,7 +42,7 @@ wxGenericImageList::~wxGenericImageList()
|
|||||||
|
|
||||||
int wxGenericImageList::GetImageCount() const
|
int wxGenericImageList::GetImageCount() const
|
||||||
{
|
{
|
||||||
return m_images.GetCount();
|
return static_cast<int>(m_images.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
|
bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
|
||||||
@@ -78,23 +78,16 @@ int wxGenericImageList::Add( const wxBitmap &bitmap )
|
|||||||
Add(bitmap.GetSubBitmap(rect));
|
Add(bitmap.GetSubBitmap(rect));
|
||||||
}
|
}
|
||||||
|
|
||||||
return m_images.GetCount() - 1;
|
return GetImageCount() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
wxASSERT_MSG( bitmapSize == m_size,
|
wxASSERT_MSG( bitmapSize == m_size,
|
||||||
"All bitmaps in wxImageList must have the same size" );
|
"All bitmaps in wxImageList must have the same size" );
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bitmap.IsKindOf(wxCLASSINFO(wxIcon)))
|
m_images.push_back(bitmap);
|
||||||
{
|
|
||||||
m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_images.Append( new wxBitmap(bitmap) );
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_images.GetCount() - 1;
|
return GetImageCount() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
|
int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
|
||||||
@@ -114,11 +107,10 @@ int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour
|
|||||||
|
|
||||||
const wxBitmap *wxGenericImageList::DoGetPtr( int index ) const
|
const wxBitmap *wxGenericImageList::DoGetPtr( int index ) const
|
||||||
{
|
{
|
||||||
wxObjectList::compatibility_iterator node = m_images.Item( index );
|
wxCHECK_MSG( index >= 0 && (size_t)index < m_images.size(),
|
||||||
|
NULL, wxT("wrong index in image list") );
|
||||||
|
|
||||||
wxCHECK_MSG( node, NULL, wxT("wrong index in image list") );
|
return &m_images[index];
|
||||||
|
|
||||||
return (wxBitmap*)node->GetData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the bitmap
|
// Get the bitmap
|
||||||
@@ -128,10 +120,7 @@ wxBitmap wxGenericImageList::GetBitmap(int index) const
|
|||||||
if (!bmp)
|
if (!bmp)
|
||||||
return wxNullBitmap;
|
return wxNullBitmap;
|
||||||
|
|
||||||
if ( bmp->IsKindOf(wxCLASSINFO(wxIcon)) )
|
return *bmp;
|
||||||
return wxBitmap( *(static_cast<const wxIcon*>(bmp)) );
|
|
||||||
else
|
|
||||||
return *bmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the icon
|
// Get the icon
|
||||||
@@ -141,105 +130,54 @@ wxIcon wxGenericImageList::GetIcon(int index) const
|
|||||||
if (!bmp)
|
if (!bmp)
|
||||||
return wxNullIcon;
|
return wxNullIcon;
|
||||||
|
|
||||||
if ( bmp->IsKindOf(wxCLASSINFO(wxIcon)) )
|
wxIcon icon;
|
||||||
return *(static_cast<const wxIcon*>(bmp));
|
icon.CopyFromBitmap(*bmp);
|
||||||
else
|
return icon;
|
||||||
{
|
|
||||||
wxIcon icon;
|
|
||||||
icon.CopyFromBitmap(*bmp);
|
|
||||||
return icon;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
|
bool
|
||||||
|
wxGenericImageList::Replace(int index,
|
||||||
|
const wxBitmap& bitmap,
|
||||||
|
const wxBitmap& mask)
|
||||||
{
|
{
|
||||||
wxObjectList::compatibility_iterator node = m_images.Item( index );
|
// Call DoGetPtr() just to check the index validity.
|
||||||
|
if ( !DoGetPtr(index) )
|
||||||
|
return false;
|
||||||
|
|
||||||
wxCHECK_MSG( node, false, wxT("wrong index in image list") );
|
m_images[index] = bitmap;
|
||||||
|
|
||||||
wxBitmap* newBitmap = (bitmap.IsKindOf(wxCLASSINFO(wxIcon))) ?
|
if ( mask.IsOk() )
|
||||||
new wxBitmap( (const wxIcon&) bitmap )
|
m_images[index].SetMask(new wxMask(mask));
|
||||||
: new wxBitmap(bitmap) ;
|
|
||||||
|
|
||||||
if (index == (int) m_images.GetCount() - 1)
|
|
||||||
{
|
|
||||||
delete node->GetData();
|
|
||||||
m_images.Erase( node );
|
|
||||||
m_images.Append( newBitmap );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
wxObjectList::compatibility_iterator next = node->GetNext();
|
|
||||||
delete node->GetData();
|
|
||||||
m_images.Erase( node );
|
|
||||||
m_images.Insert( next, newBitmap );
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap, const wxBitmap &mask )
|
|
||||||
{
|
|
||||||
wxObjectList::compatibility_iterator node = m_images.Item( index );
|
|
||||||
|
|
||||||
wxCHECK_MSG( node, false, wxT("wrong index in image list") );
|
|
||||||
|
|
||||||
wxBitmap* newBitmap = (bitmap.IsKindOf(wxCLASSINFO(wxIcon))) ?
|
|
||||||
new wxBitmap( (const wxIcon&) bitmap )
|
|
||||||
: new wxBitmap(bitmap) ;
|
|
||||||
|
|
||||||
if (index == (int) m_images.GetCount() - 1)
|
|
||||||
{
|
|
||||||
delete node->GetData();
|
|
||||||
m_images.Erase( node );
|
|
||||||
m_images.Append( newBitmap );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
wxObjectList::compatibility_iterator next = node->GetNext();
|
|
||||||
delete node->GetData();
|
|
||||||
m_images.Erase( node );
|
|
||||||
m_images.Insert( next, newBitmap );
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mask.IsOk())
|
|
||||||
newBitmap->SetMask(new wxMask(mask));
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxGenericImageList::Remove( int index )
|
bool wxGenericImageList::Remove( int index )
|
||||||
{
|
{
|
||||||
wxObjectList::compatibility_iterator node = m_images.Item( index );
|
m_images.erase(m_images.begin() + index);
|
||||||
|
|
||||||
wxCHECK_MSG( node, false, wxT("wrong index in image list") );
|
|
||||||
|
|
||||||
delete node->GetData();
|
|
||||||
m_images.Erase( node );
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxGenericImageList::RemoveAll()
|
bool wxGenericImageList::RemoveAll()
|
||||||
{
|
{
|
||||||
WX_CLEAR_LIST(wxObjectList, m_images);
|
m_images.clear();
|
||||||
m_images.Clear();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
|
bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
|
||||||
{
|
{
|
||||||
width = 0;
|
const wxBitmap* bmp = DoGetPtr(index);
|
||||||
height = 0;
|
if ( !bmp )
|
||||||
|
{
|
||||||
|
width = 0;
|
||||||
|
height = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
wxObjectList::compatibility_iterator node = m_images.Item( index );
|
width = bmp->GetScaledWidth();
|
||||||
|
height = bmp->GetScaledHeight();
|
||||||
wxCHECK_MSG( node, false, wxT("wrong index in image list") );
|
|
||||||
|
|
||||||
wxBitmap *bm = (wxBitmap*)node->GetData();
|
|
||||||
width = bm->GetScaledWidth();
|
|
||||||
height = bm->GetScaledHeight();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -247,16 +185,11 @@ bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
|
|||||||
bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
|
bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
|
||||||
int flags, bool WXUNUSED(solidBackground) )
|
int flags, bool WXUNUSED(solidBackground) )
|
||||||
{
|
{
|
||||||
wxObjectList::compatibility_iterator node = m_images.Item( index );
|
const wxBitmap* bmp = DoGetPtr(index);
|
||||||
|
if ( !bmp )
|
||||||
|
return false;
|
||||||
|
|
||||||
wxCHECK_MSG( node, false, wxT("wrong index in image list") );
|
dc.DrawBitmap(*bmp, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) != 0);
|
||||||
|
|
||||||
wxBitmap *bm = (wxBitmap*)node->GetData();
|
|
||||||
|
|
||||||
if (bm->IsKindOf(wxCLASSINFO(wxIcon)))
|
|
||||||
dc.DrawIcon( * ((wxIcon*) bm), x, y);
|
|
||||||
else
|
|
||||||
dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user