1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/imaglist.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
18 #include "wx/imaglist.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
26 wxImageList::wxImageList( int width
, int height
, bool mask
, int initialCount
)
28 (void)Create(width
, height
, mask
, initialCount
);
31 wxImageList::~wxImageList()
36 int wxImageList::GetImageCount() const
38 return m_images
.GetCount();
41 bool wxImageList::Create( int width
, int height
, bool WXUNUSED(mask
), int WXUNUSED(initialCount
) )
49 bool wxImageList::Create()
54 int wxImageList::Add( const wxIcon
&bitmap
)
56 wxASSERT_MSG( (bitmap
.GetWidth() == m_width
&& bitmap
.GetHeight() == m_height
)
57 || (m_width
== 0 && m_height
== 0),
58 _T("invalid bitmap size in wxImageList: this might work ")
59 _T("on this platform but definitely won't under Windows.") );
61 m_images
.Append( new wxIcon( bitmap
) );
63 if (m_width
== 0 && m_height
== 0)
65 m_width
= bitmap
.GetWidth();
66 m_height
= bitmap
.GetHeight();
69 return m_images
.GetCount() - 1;
72 int wxImageList::Add( const wxBitmap
&bitmap
)
74 wxASSERT_MSG( (bitmap
.GetWidth() >= m_width
&& bitmap
.GetHeight() == m_height
)
75 || (m_width
== 0 && m_height
== 0),
76 _T("invalid bitmap size in wxImageList: this might work ")
77 _T("on this platform but definitely won't under Windows.") );
79 // Mimic behavior of Windows ImageList_Add that automatically breaks up the added
80 // bitmap into sub-images of the correct size
81 if (m_width
> 0 && bitmap
.GetWidth() > m_width
&& bitmap
.GetHeight() >= m_height
)
83 int numImages
= bitmap
.GetWidth() / m_width
;
84 for (int subIndex
= 0; subIndex
< numImages
; subIndex
++)
86 wxRect
rect(m_width
* subIndex
, 0, m_width
, m_height
);
87 wxBitmap tmpBmp
= bitmap
.GetSubBitmap(rect
);
88 m_images
.Append( new wxBitmap(tmpBmp
) );
93 m_images
.Append( new wxBitmap(bitmap
) );
96 if (m_width
== 0 && m_height
== 0)
98 m_width
= bitmap
.GetWidth();
99 m_height
= bitmap
.GetHeight();
102 return m_images
.GetCount() - 1;
105 int wxImageList::Add( const wxBitmap
& bitmap
, const wxBitmap
& mask
)
107 wxBitmap
bmp( bitmap
);
109 bmp
.SetMask( new wxMask( mask
) );
114 int wxImageList::Add( const wxBitmap
& bitmap
, const wxColour
& maskColour
)
116 wxImage img
= bitmap
.ConvertToImage();
117 img
.SetMaskColour( maskColour
.Red(), maskColour
.Green(), maskColour
.Blue() );
119 return Add( wxBitmap( img
) );
123 wxBitmap
wxImageList::GetBitmap(int index
) const
125 wxList::compatibility_iterator node
= m_images
.Item( index
);
127 wxCHECK_MSG( node
, wxNullBitmap
, wxT("wrong index in image list") );
129 wxObject
* obj
= (wxObject
*) node
->GetData();
131 return wxNullBitmap
;
132 else if ( obj
->IsKindOf(CLASSINFO(wxIcon
)) )
133 return wxBitmap( *(wx_static_cast(wxIcon
*, obj
)) ) ;
135 return *(wx_static_cast(wxBitmap
*, obj
)) ;
139 wxIcon
wxImageList::GetIcon(int index
) const
141 wxList::compatibility_iterator node
= m_images
.Item( index
);
143 wxCHECK_MSG( node
, wxNullIcon
, wxT("wrong index in image list") );
145 wxObject
* obj
= (wxObject
*) node
->GetData();
148 else if ( obj
->IsKindOf(CLASSINFO(wxBitmap
)) )
150 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
154 return *(wx_static_cast(wxIcon
*, obj
)) ;
157 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
)
159 wxList::compatibility_iterator node
= m_images
.Item( index
);
161 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
163 wxBitmap
* newBitmap
= new wxBitmap( bitmap
);
165 if (index
== (int) m_images
.GetCount() - 1)
167 delete node
->GetData();
169 m_images
.Erase( node
);
170 m_images
.Append( newBitmap
);
174 wxList::compatibility_iterator next
= node
->GetNext();
175 delete node
->GetData();
177 m_images
.Erase( node
);
178 m_images
.Insert( next
, newBitmap
);
184 bool wxImageList::Replace( int index
, const wxIcon
&bitmap
)
186 wxList::compatibility_iterator node
= m_images
.Item( index
);
188 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
190 wxIcon
* newBitmap
= new wxIcon( bitmap
);
192 if (index
== (int) m_images
.GetCount() - 1)
194 delete node
->GetData();
195 m_images
.Erase( node
);
196 m_images
.Append( newBitmap
);
200 wxList::compatibility_iterator next
= node
->GetNext();
201 delete node
->GetData();
202 m_images
.Erase( node
);
203 m_images
.Insert( next
, newBitmap
);
209 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
, const wxBitmap
&mask
)
211 wxList::compatibility_iterator node
= m_images
.Item( index
);
213 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
215 wxBitmap
* newBitmap
= new wxBitmap(bitmap
);
217 if (index
== (int) m_images
.GetCount() - 1)
219 delete node
->GetData();
220 m_images
.Erase( node
);
221 m_images
.Append( newBitmap
);
225 wxList::compatibility_iterator next
= node
->GetNext();
226 delete node
->GetData();
227 m_images
.Erase( node
);
228 m_images
.Insert( next
, newBitmap
);
232 newBitmap
->SetMask(new wxMask(mask
));
237 bool wxImageList::Remove( int index
)
239 wxList::compatibility_iterator node
= m_images
.Item( index
);
241 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
243 delete node
->GetData();
244 m_images
.Erase( node
);
249 bool wxImageList::RemoveAll()
251 WX_CLEAR_LIST(wxList
, m_images
);
257 bool wxImageList::GetSize( int index
, int &width
, int &height
) const
262 wxList::compatibility_iterator node
= m_images
.Item( index
);
264 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
266 wxObject
*obj
= (wxObject
*)node
->GetData();
267 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
269 wxIcon
*bm
= wx_static_cast( wxIcon
* , obj
) ;
270 width
= bm
->GetWidth();
271 height
= bm
->GetHeight();
275 wxBitmap
*bm
= wx_static_cast( wxBitmap
* , obj
) ;
276 width
= bm
->GetWidth();
277 height
= bm
->GetHeight();
283 bool wxImageList::Draw(
284 int index
, wxDC
&dc
, int x
, int y
,
285 int flags
, bool WXUNUSED(solidBackground
) )
287 wxList::compatibility_iterator node
= m_images
.Item( index
);
289 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
291 wxObject
*obj
= (wxObject
*)node
->GetData();
292 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
294 wxIcon
*bm
= wx_static_cast( wxIcon
* , obj
) ;
295 dc
.DrawIcon( *bm
, x
, y
);
299 wxBitmap
*bm
= wx_static_cast( wxBitmap
* , obj
) ;
300 dc
.DrawBitmap( *bm
, x
, y
, (flags
& wxIMAGELIST_DRAW_TRANSPARENT
) > 0 );
306 #endif // wxUSE_IMAGLIST