1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/imaglist.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/imaglist.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
28 wxImageList::wxImageList( int width
, int height
, bool mask
, int initialCount
)
30 (void)Create(width
, height
, mask
, initialCount
);
33 wxImageList::~wxImageList()
38 int wxImageList::GetImageCount() const
40 return m_images
.GetCount();
43 bool wxImageList::Create( int width
, int height
, bool WXUNUSED(mask
), int WXUNUSED(initialCount
) )
51 bool wxImageList::Create()
56 int wxImageList::Add( const wxIcon
&bitmap
)
58 wxASSERT_MSG( (bitmap
.GetWidth() == m_width
&& bitmap
.GetHeight() == m_height
)
59 || (m_width
== 0 && m_height
== 0),
60 wxT("invalid bitmap size in wxImageList: this might work ")
61 wxT("on this platform but definitely won't under Windows.") );
63 m_images
.Append( new wxIcon( bitmap
) );
65 if (m_width
== 0 && m_height
== 0)
67 m_width
= bitmap
.GetWidth();
68 m_height
= bitmap
.GetHeight();
71 return m_images
.GetCount() - 1;
74 int wxImageList::Add( const wxBitmap
&bitmap
)
76 wxASSERT_MSG( (bitmap
.GetWidth() >= m_width
&& bitmap
.GetHeight() == m_height
)
77 || (m_width
== 0 && m_height
== 0),
78 wxT("invalid bitmap size in wxImageList: this might work ")
79 wxT("on this platform but definitely won't under Windows.") );
81 // Mimic behaviour of Windows ImageList_Add that automatically breaks up the added
82 // bitmap into sub-images of the correct size
83 if (m_width
> 0 && bitmap
.GetWidth() > m_width
&& bitmap
.GetHeight() >= m_height
)
85 int numImages
= bitmap
.GetWidth() / m_width
;
86 for (int subIndex
= 0; subIndex
< numImages
; subIndex
++)
88 wxRect
rect(m_width
* subIndex
, 0, m_width
, m_height
);
89 wxBitmap tmpBmp
= bitmap
.GetSubBitmap(rect
);
90 m_images
.Append( new wxBitmap(tmpBmp
) );
95 m_images
.Append( new wxBitmap(bitmap
) );
98 if (m_width
== 0 && m_height
== 0)
100 m_width
= bitmap
.GetWidth();
101 m_height
= bitmap
.GetHeight();
104 return m_images
.GetCount() - 1;
107 int wxImageList::Add( const wxBitmap
& bitmap
, const wxBitmap
& mask
)
109 wxBitmap
bmp( bitmap
);
111 bmp
.SetMask( new wxMask( mask
) );
116 int wxImageList::Add( const wxBitmap
& bitmap
, const wxColour
& maskColour
)
118 wxImage img
= bitmap
.ConvertToImage();
119 img
.SetMaskColour( maskColour
.Red(), maskColour
.Green(), maskColour
.Blue() );
121 return Add( wxBitmap( img
) );
125 wxBitmap
wxImageList::GetBitmap(int index
) const
127 wxList::compatibility_iterator node
= m_images
.Item( index
);
129 wxCHECK_MSG( node
, wxNullBitmap
, wxT("wrong index in image list") );
131 wxObject
* obj
= (wxObject
*) node
->GetData();
133 return wxNullBitmap
;
134 else if ( obj
->IsKindOf(CLASSINFO(wxIcon
)) )
135 return wxBitmap( *(static_cast<wxIcon
*>(obj
)) ) ;
137 return *(static_cast<wxBitmap
*>(obj
)) ;
141 wxIcon
wxImageList::GetIcon(int index
) const
143 wxList::compatibility_iterator node
= m_images
.Item( index
);
145 wxCHECK_MSG( node
, wxNullIcon
, wxT("wrong index in image list") );
147 wxObject
* obj
= (wxObject
*) node
->GetData();
150 else if ( obj
->IsKindOf(CLASSINFO(wxBitmap
)) )
152 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
156 return *(static_cast<wxIcon
*>(obj
)) ;
159 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
)
161 wxList::compatibility_iterator node
= m_images
.Item( index
);
163 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
165 wxBitmap
* newBitmap
= new wxBitmap( bitmap
);
167 if (index
== (int) m_images
.GetCount() - 1)
169 delete node
->GetData();
171 m_images
.Erase( node
);
172 m_images
.Append( newBitmap
);
176 wxList::compatibility_iterator next
= node
->GetNext();
177 delete node
->GetData();
179 m_images
.Erase( node
);
180 m_images
.Insert( next
, newBitmap
);
186 bool wxImageList::Replace( int index
, const wxIcon
&bitmap
)
188 wxList::compatibility_iterator node
= m_images
.Item( index
);
190 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
192 wxIcon
* newBitmap
= new wxIcon( bitmap
);
194 if (index
== (int) m_images
.GetCount() - 1)
196 delete node
->GetData();
197 m_images
.Erase( node
);
198 m_images
.Append( newBitmap
);
202 wxList::compatibility_iterator next
= node
->GetNext();
203 delete node
->GetData();
204 m_images
.Erase( node
);
205 m_images
.Insert( next
, newBitmap
);
211 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
, const wxBitmap
&mask
)
213 wxList::compatibility_iterator node
= m_images
.Item( index
);
215 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
217 wxBitmap
* newBitmap
= new wxBitmap(bitmap
);
219 if (index
== (int) m_images
.GetCount() - 1)
221 delete node
->GetData();
222 m_images
.Erase( node
);
223 m_images
.Append( newBitmap
);
227 wxList::compatibility_iterator next
= node
->GetNext();
228 delete node
->GetData();
229 m_images
.Erase( node
);
230 m_images
.Insert( next
, newBitmap
);
234 newBitmap
->SetMask(new wxMask(mask
));
239 bool wxImageList::Remove( int index
)
241 wxList::compatibility_iterator node
= m_images
.Item( index
);
243 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
245 delete node
->GetData();
246 m_images
.Erase( node
);
251 bool wxImageList::RemoveAll()
253 WX_CLEAR_LIST(wxList
, m_images
);
259 bool wxImageList::GetSize( int index
, int &width
, int &height
) const
264 wxList::compatibility_iterator node
= m_images
.Item( index
);
266 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
268 wxObject
*obj
= (wxObject
*)node
->GetData();
269 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
271 wxIcon
*bm
= static_cast< wxIcon
* >(obj
) ;
272 width
= bm
->GetWidth();
273 height
= bm
->GetHeight();
277 wxBitmap
*bm
= static_cast< wxBitmap
* >(obj
) ;
278 width
= bm
->GetWidth();
279 height
= bm
->GetHeight();
285 bool wxImageList::Draw(
286 int index
, wxDC
&dc
, int x
, int y
,
287 int flags
, bool WXUNUSED(solidBackground
) )
289 wxList::compatibility_iterator node
= m_images
.Item( index
);
291 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
293 wxObject
*obj
= (wxObject
*)node
->GetData();
294 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
296 wxIcon
*bm
= static_cast< wxIcon
* >(obj
) ;
297 dc
.DrawIcon( *bm
, x
, y
);
301 wxBitmap
*bm
= static_cast< wxBitmap
* >(obj
) ;
302 dc
.DrawBitmap( *bm
, x
, y
, (flags
& wxIMAGELIST_DRAW_TRANSPARENT
) > 0 );
308 #endif // wxUSE_IMAGLIST