1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/imaglist.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
19 #include "wx/imaglist.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
27 wxImageList::wxImageList( int width
, int height
, bool mask
, int initialCount
)
29 (void)Create(width
, height
, mask
, initialCount
);
32 wxImageList::~wxImageList()
37 int wxImageList::GetImageCount() const
39 return m_images
.GetCount();
42 bool wxImageList::Create( int width
, int height
, bool WXUNUSED(mask
), int WXUNUSED(initialCount
) )
50 bool wxImageList::Create()
55 int wxImageList::Add( const wxIcon
&bitmap
)
57 wxASSERT_MSG( (bitmap
.GetWidth() == m_width
&& bitmap
.GetHeight() == m_height
)
58 || (m_width
== 0 && m_height
== 0),
59 _T("invalid bitmap size in wxImageList: this might work ")
60 _T("on this platform but definitely won't under Windows.") );
62 m_images
.Append( new wxIcon( bitmap
) );
64 if (m_width
== 0 && m_height
== 0)
66 m_width
= bitmap
.GetWidth();
67 m_height
= bitmap
.GetHeight();
70 return m_images
.GetCount() - 1;
73 int wxImageList::Add( const wxBitmap
&bitmap
)
75 wxASSERT_MSG( (bitmap
.GetWidth() == m_width
&& bitmap
.GetHeight() == m_height
)
76 || (m_width
== 0 && m_height
== 0),
77 _T("invalid bitmap size in wxImageList: this might work ")
78 _T("on this platform but definitely won't under Windows.") );
80 m_images
.Append( new wxBitmap( bitmap
) );
82 if (m_width
== 0 && m_height
== 0)
84 m_width
= bitmap
.GetWidth();
85 m_height
= bitmap
.GetHeight();
88 return m_images
.GetCount() - 1;
91 int wxImageList::Add( const wxBitmap
& bitmap
, const wxBitmap
& mask
)
93 wxBitmap
bmp( bitmap
);
95 bmp
.SetMask( new wxMask( mask
) );
100 int wxImageList::Add( const wxBitmap
& bitmap
, const wxColour
& maskColour
)
102 wxImage img
= bitmap
.ConvertToImage();
103 img
.SetMaskColour( maskColour
.Red(), maskColour
.Green(), maskColour
.Blue() );
105 return Add( wxBitmap( img
) );
109 wxBitmap
wxImageList::GetBitmap(int index
) const
111 wxList::compatibility_iterator node
= m_images
.Item( index
);
113 wxCHECK_MSG( node
, wxNullBitmap
, wxT("wrong index in image list") );
115 wxObject
* obj
= (wxObject
*) node
->GetData();
117 return wxNullBitmap
;
118 else if ( obj
->IsKindOf(CLASSINFO(wxIcon
)) )
119 return wxBitmap( *(wx_static_cast(wxIcon
*, obj
)) ) ;
121 return *(wx_static_cast(wxBitmap
*, obj
)) ;
125 wxIcon
wxImageList::GetIcon(int index
) const
127 wxList::compatibility_iterator node
= m_images
.Item( index
);
129 wxCHECK_MSG( node
, wxNullIcon
, wxT("wrong index in image list") );
131 wxObject
* obj
= (wxObject
*) node
->GetData();
134 else if ( obj
->IsKindOf(CLASSINFO(wxBitmap
)) )
136 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
140 return *(wx_static_cast(wxIcon
*, obj
)) ;
143 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
)
145 wxList::compatibility_iterator node
= m_images
.Item( index
);
147 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
149 wxBitmap
* newBitmap
= new wxBitmap( bitmap
);
151 if (index
== (int) m_images
.GetCount() - 1)
153 delete node
->GetData();
155 m_images
.Erase( node
);
156 m_images
.Append( newBitmap
);
160 wxList::compatibility_iterator next
= node
->GetNext();
161 delete node
->GetData();
163 m_images
.Erase( node
);
164 m_images
.Insert( next
, newBitmap
);
170 bool wxImageList::Replace( int index
, const wxIcon
&bitmap
)
172 wxList::compatibility_iterator node
= m_images
.Item( index
);
174 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
176 wxIcon
* newBitmap
= new wxIcon( bitmap
);
178 if (index
== (int) m_images
.GetCount() - 1)
180 delete node
->GetData();
181 m_images
.Erase( node
);
182 m_images
.Append( newBitmap
);
186 wxList::compatibility_iterator next
= node
->GetNext();
187 delete node
->GetData();
188 m_images
.Erase( node
);
189 m_images
.Insert( next
, newBitmap
);
195 bool wxImageList::Remove( int index
)
197 wxList::compatibility_iterator node
= m_images
.Item( index
);
199 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
201 delete node
->GetData();
202 m_images
.Erase( node
);
207 bool wxImageList::RemoveAll()
209 WX_CLEAR_LIST(wxList
, m_images
);
215 bool wxImageList::GetSize( int index
, int &width
, int &height
) const
220 wxList::compatibility_iterator node
= m_images
.Item( index
);
222 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
224 wxObject
*obj
= (wxObject
*)node
->GetData();
225 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
227 wxIcon
*bm
= wx_static_cast( wxIcon
* , obj
) ;
228 width
= bm
->GetWidth();
229 height
= bm
->GetHeight();
233 wxBitmap
*bm
= wx_static_cast( wxBitmap
* , obj
) ;
234 width
= bm
->GetWidth();
235 height
= bm
->GetHeight();
241 bool wxImageList::Draw(
242 int index
, wxDC
&dc
, int x
, int y
,
243 int flags
, bool WXUNUSED(solidBackground
) )
245 wxList::compatibility_iterator node
= m_images
.Item( index
);
247 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
249 wxObject
*obj
= (wxObject
*)node
->GetData();
250 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
252 wxIcon
*bm
= wx_static_cast( wxIcon
* , obj
) ;
253 dc
.DrawIcon( *bm
, x
, y
);
257 wxBitmap
*bm
= wx_static_cast( wxBitmap
* , obj
) ;
258 dc
.DrawBitmap( *bm
, x
, y
, (flags
& wxIMAGELIST_DRAW_TRANSPARENT
) > 0 );
264 #endif // wxUSE_IMAGLIST