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"
27 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
30 wxImageList::wxImageList( int width
, int height
, bool mask
, int initialCount
)
32 (void)Create(width
, height
, mask
, initialCount
);
35 wxImageList::~wxImageList()
40 int wxImageList::GetImageCount() const
42 return m_images
.GetCount();
45 bool wxImageList::Create( int width
, int height
, bool WXUNUSED(mask
), int WXUNUSED(initialCount
) )
53 bool wxImageList::Create()
58 int wxImageList::Add( const wxIcon
&bitmap
)
60 wxASSERT_MSG( (bitmap
.GetWidth() == m_width
&& bitmap
.GetHeight() == m_height
)
61 || (m_width
== 0 && m_height
== 0),
62 _T("invalid bitmap size in wxImageList: this might work ")
63 _T("on this platform but definitely won't under Windows.") );
65 m_images
.Append( new wxIcon( bitmap
) );
67 if (m_width
== 0 && m_height
== 0)
69 m_width
= bitmap
.GetWidth();
70 m_height
= bitmap
.GetHeight();
73 return m_images
.GetCount() - 1;
76 int wxImageList::Add( const wxBitmap
&bitmap
)
78 wxASSERT_MSG( (bitmap
.GetWidth() >= m_width
&& bitmap
.GetHeight() == m_height
)
79 || (m_width
== 0 && m_height
== 0),
80 _T("invalid bitmap size in wxImageList: this might work ")
81 _T("on this platform but definitely won't under Windows.") );
83 // Mimic behavior of Windows ImageList_Add that automatically breaks up the added
84 // bitmap into sub-images of the correct size
85 if (m_width
> 0 && bitmap
.GetWidth() > m_width
&& bitmap
.GetHeight() >= m_height
)
87 int numImages
= bitmap
.GetWidth() / m_width
;
88 for (int subIndex
= 0; subIndex
< numImages
; subIndex
++)
90 wxRect
rect(m_width
* subIndex
, 0, m_width
, m_height
);
91 wxBitmap tmpBmp
= bitmap
.GetSubBitmap(rect
);
92 m_images
.Append( new wxBitmap(tmpBmp
) );
97 m_images
.Append( new wxBitmap(bitmap
) );
100 if (m_width
== 0 && m_height
== 0)
102 m_width
= bitmap
.GetWidth();
103 m_height
= bitmap
.GetHeight();
106 return m_images
.GetCount() - 1;
109 int wxImageList::Add( const wxBitmap
& bitmap
, const wxBitmap
& mask
)
111 wxBitmap
bmp( bitmap
);
113 bmp
.SetMask( new wxMask( mask
) );
118 int wxImageList::Add( const wxBitmap
& bitmap
, const wxColour
& maskColour
)
120 wxImage img
= bitmap
.ConvertToImage();
121 img
.SetMaskColour( maskColour
.Red(), maskColour
.Green(), maskColour
.Blue() );
123 return Add( wxBitmap( img
) );
127 wxBitmap
wxImageList::GetBitmap(int index
) const
129 wxList::compatibility_iterator node
= m_images
.Item( index
);
131 wxCHECK_MSG( node
, wxNullBitmap
, wxT("wrong index in image list") );
133 wxObject
* obj
= (wxObject
*) node
->GetData();
135 return wxNullBitmap
;
136 else if ( obj
->IsKindOf(CLASSINFO(wxIcon
)) )
137 return wxBitmap( *(wx_static_cast(wxIcon
*, obj
)) ) ;
139 return *(wx_static_cast(wxBitmap
*, obj
)) ;
143 wxIcon
wxImageList::GetIcon(int index
) const
145 wxList::compatibility_iterator node
= m_images
.Item( index
);
147 wxCHECK_MSG( node
, wxNullIcon
, wxT("wrong index in image list") );
149 wxObject
* obj
= (wxObject
*) node
->GetData();
152 else if ( obj
->IsKindOf(CLASSINFO(wxBitmap
)) )
154 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
158 return *(wx_static_cast(wxIcon
*, obj
)) ;
161 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
)
163 wxList::compatibility_iterator node
= m_images
.Item( index
);
165 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
167 wxBitmap
* newBitmap
= new wxBitmap( bitmap
);
169 if (index
== (int) m_images
.GetCount() - 1)
171 delete node
->GetData();
173 m_images
.Erase( node
);
174 m_images
.Append( newBitmap
);
178 wxList::compatibility_iterator next
= node
->GetNext();
179 delete node
->GetData();
181 m_images
.Erase( node
);
182 m_images
.Insert( next
, newBitmap
);
188 bool wxImageList::Replace( int index
, const wxIcon
&bitmap
)
190 wxList::compatibility_iterator node
= m_images
.Item( index
);
192 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
194 wxIcon
* newBitmap
= new wxIcon( bitmap
);
196 if (index
== (int) m_images
.GetCount() - 1)
198 delete node
->GetData();
199 m_images
.Erase( node
);
200 m_images
.Append( newBitmap
);
204 wxList::compatibility_iterator next
= node
->GetNext();
205 delete node
->GetData();
206 m_images
.Erase( node
);
207 m_images
.Insert( next
, newBitmap
);
213 bool wxImageList::Replace( int index
, const wxBitmap
&bitmap
, const wxBitmap
&mask
)
215 wxList::compatibility_iterator node
= m_images
.Item( index
);
217 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
219 wxBitmap
* newBitmap
= new wxBitmap(bitmap
);
221 if (index
== (int) m_images
.GetCount() - 1)
223 delete node
->GetData();
224 m_images
.Erase( node
);
225 m_images
.Append( newBitmap
);
229 wxList::compatibility_iterator next
= node
->GetNext();
230 delete node
->GetData();
231 m_images
.Erase( node
);
232 m_images
.Insert( next
, newBitmap
);
236 newBitmap
->SetMask(new wxMask(mask
));
241 bool wxImageList::Remove( int index
)
243 wxList::compatibility_iterator node
= m_images
.Item( index
);
245 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
247 delete node
->GetData();
248 m_images
.Erase( node
);
253 bool wxImageList::RemoveAll()
255 WX_CLEAR_LIST(wxList
, m_images
);
261 bool wxImageList::GetSize( int index
, int &width
, int &height
) const
266 wxList::compatibility_iterator node
= m_images
.Item( index
);
268 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
270 wxObject
*obj
= (wxObject
*)node
->GetData();
271 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
273 wxIcon
*bm
= wx_static_cast( wxIcon
* , obj
) ;
274 width
= bm
->GetWidth();
275 height
= bm
->GetHeight();
279 wxBitmap
*bm
= wx_static_cast( wxBitmap
* , obj
) ;
280 width
= bm
->GetWidth();
281 height
= bm
->GetHeight();
287 bool wxImageList::Draw(
288 int index
, wxDC
&dc
, int x
, int y
,
289 int flags
, bool WXUNUSED(solidBackground
) )
291 wxList::compatibility_iterator node
= m_images
.Item( index
);
293 wxCHECK_MSG( node
, false, wxT("wrong index in image list") );
295 wxObject
*obj
= (wxObject
*)node
->GetData();
296 if (obj
->IsKindOf(CLASSINFO(wxIcon
)))
298 wxIcon
*bm
= wx_static_cast( wxIcon
* , obj
) ;
299 dc
.DrawIcon( *bm
, x
, y
);
303 wxBitmap
*bm
= wx_static_cast( wxBitmap
* , obj
) ;
304 dc
.DrawBitmap( *bm
, x
, y
, (flags
& wxIMAGELIST_DRAW_TRANSPARENT
) > 0 );
310 #endif // wxUSE_IMAGLIST