1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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"
26 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
29 wxImageList::wxImageList( int width
, int height
, bool mask
, int initialCount
)
31 (void)Create(width
, height
, mask
, initialCount
);
34 wxImageList::~wxImageList()
39 int wxImageList::GetImageCount() const
41 return m_images
.GetCount();
44 bool wxImageList::Create( int width
, int height
, bool WXUNUSED(mask
), int WXUNUSED(initialCount
) )
52 bool wxImageList::Create()
57 int wxImageList::Add( const wxIcon
&bitmap
)
59 wxASSERT_MSG( (bitmap
.GetWidth() == m_width
&& bitmap
.GetHeight() == m_height
)
60 || (m_width
== 0 && m_height
== 0),
61 wxT("invalid bitmap size in wxImageList: this might work ")
62 wxT("on this platform but definitely won't under Windows.") );
64 m_images
.Append( new wxIcon( bitmap
) );
66 if (m_width
== 0 && m_height
== 0)
68 m_width
= bitmap
.GetWidth();
69 m_height
= bitmap
.GetHeight();
72 return m_images
.GetCount() - 1;
75 int wxImageList::Add( const wxBitmap
&bitmap
)
77 wxASSERT_MSG( (bitmap
.GetWidth() >= m_width
&& bitmap
.GetHeight() == m_height
)
78 || (m_width
== 0 && m_height
== 0),
79 wxT("invalid bitmap size in wxImageList: this might work ")
80 wxT("on this platform but definitely won't under Windows.") );
82 // Mimic behavior of Windows ImageList_Add that automatically breaks up the added
83 // bitmap into sub-images of the correct size
84 if (m_width
> 0 && bitmap
.GetWidth() > m_width
&& bitmap
.GetHeight() >= m_height
)
86 int numImages
= bitmap
.GetWidth() / m_width
;
87 for (int subIndex
= 0; subIndex
< numImages
; subIndex
++)
89 wxRect
rect(m_width
* subIndex
, 0, m_width
, m_height
);
90 wxBitmap tmpBmp
= bitmap
.GetSubBitmap(rect
);
91 m_images
.Append( new wxBitmap(tmpBmp
) );
96 m_images
.Append( new wxBitmap(bitmap
) );
99 if (m_width
== 0 && m_height
== 0)
101 m_width
= bitmap
.GetWidth();
102 m_height
= bitmap
.GetHeight();
105 return m_images
.GetCount() - 1;
108 int wxImageList::Add( const wxBitmap
& bitmap
, const wxBitmap
& mask
)
110 wxBitmap
bmp( bitmap
);
112 bmp
.SetMask( new wxMask( mask
) );
117 int wxImageList::Add( const wxBitmap
& bitmap
, const wxColour
& maskColour
)
119 wxImage img
= bitmap
.ConvertToImage();
120 img
.SetMaskColour( maskColour
.Red(), maskColour
.Green(), maskColour
.Blue() );
122 return Add( wxBitmap( img
) );
126 wxBitmap
wxImageList::GetBitmap(int index
) const
128 wxList::compatibility_iterator node
= m_images
.Item( index
);
130 wxCHECK_MSG( node
, wxNullBitmap
, wxT("wrong index in image list") );
132 wxObject
* obj
= (wxObject
*) node
->GetData();
134 return wxNullBitmap
;
135 else if ( obj
->IsKindOf(CLASSINFO(wxIcon
)) )
136 return wxBitmap( *(static_cast<wxIcon
*>(obj
)) ) ;
138 return *(static_cast<wxBitmap
*>(obj
)) ;
142 wxIcon
wxImageList::GetIcon(int index
) const
144 wxList::compatibility_iterator node
= m_images
.Item( index
);
146 wxCHECK_MSG( node
, wxNullIcon
, wxT("wrong index in image list") );
148 wxObject
* obj
= (wxObject
*) node
->GetData();
151 else if ( obj
->IsKindOf(CLASSINFO(wxBitmap
)) )
153 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
157 return *(static_cast<wxIcon
*>(obj
)) ;
160 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
)
162 wxList::compatibility_iterator node
= m_images
.Item( index
);
164 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
166 wxBitmap
* newBitmap
= new wxBitmap( bitmap
);
168 if (index
== (int) m_images
.GetCount() - 1)
170 delete node
->GetData();
172 m_images
.Erase( node
);
173 m_images
.Append( newBitmap
);
177 wxList::compatibility_iterator next
= node
->GetNext();
178 delete node
->GetData();
180 m_images
.Erase( node
);
181 m_images
.Insert( next
, newBitmap
);
187 bool wxImageList::Replace( int index
, const wxIcon
&bitmap
)
189 wxList::compatibility_iterator node
= m_images
.Item( index
);
191 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
193 wxIcon
* newBitmap
= new wxIcon( bitmap
);
195 if (index
== (int) m_images
.GetCount() - 1)
197 delete node
->GetData();
198 m_images
.Erase( node
);
199 m_images
.Append( newBitmap
);
203 wxList::compatibility_iterator next
= node
->GetNext();
204 delete node
->GetData();
205 m_images
.Erase( node
);
206 m_images
.Insert( next
, newBitmap
);
212 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
, const wxBitmap
&mask
)
214 wxList::compatibility_iterator node
= m_images
.Item( index
);
216 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
218 wxBitmap
* newBitmap
= new wxBitmap(bitmap
);
220 if (index
== (int) m_images
.GetCount() - 1)
222 delete node
->GetData();
223 m_images
.Erase( node
);
224 m_images
.Append( newBitmap
);
228 wxList::compatibility_iterator next
= node
->GetNext();
229 delete node
->GetData();
230 m_images
.Erase( node
);
231 m_images
.Insert( next
, newBitmap
);
235 newBitmap
->SetMask(new wxMask(mask
));
240 bool wxImageList::Remove( int index
)
242 wxList::compatibility_iterator node
= m_images
.Item( index
);
244 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
246 delete node
->GetData();
247 m_images
.Erase( node
);
252 bool wxImageList::RemoveAll()
254 WX_CLEAR_LIST(wxList
, m_images
);
260 bool wxImageList::GetSize( int index
, int &width
, int &height
) const
265 wxList::compatibility_iterator node
= m_images
.Item( index
);
267 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
269 wxObject
*obj
= (wxObject
*)node
->GetData();
270 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
272 wxIcon
*bm
= static_cast< wxIcon
* >(obj
) ;
273 width
= bm
->GetWidth();
274 height
= bm
->GetHeight();
278 wxBitmap
*bm
= static_cast< wxBitmap
* >(obj
) ;
279 width
= bm
->GetWidth();
280 height
= bm
->GetHeight();
286 bool wxImageList::Draw(
287 int index
, wxDC
&dc
, int x
, int y
,
288 int flags
, bool WXUNUSED(solidBackground
) )
290 wxList::compatibility_iterator node
= m_images
.Item( index
);
292 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
294 wxObject
*obj
= (wxObject
*)node
->GetData();
295 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
297 wxIcon
*bm
= static_cast< wxIcon
* >(obj
) ;
298 dc
.DrawIcon( *bm
, x
, y
);
302 wxBitmap
*bm
= static_cast< wxBitmap
* >(obj
) ;
303 dc
.DrawBitmap( *bm
, x
, y
, (flags
& wxIMAGELIST_DRAW_TRANSPARENT
) > 0 );
309 #endif // wxUSE_IMAGLIST