]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/imaglist.cpp
Renamed DrawCheckButton due to conflict with existing and public api of wxUniversal...
[wxWidgets.git] / src / mac / carbon / imaglist.cpp
CommitLineData
6ce43ac9 1/////////////////////////////////////////////////////////////////////////////
18f3decb 2// Name: src/mac/carbon/imaglist.cpp
6ce43ac9
SC
3// Purpose:
4// Author: Robert Roebling
18f3decb 5// RCS_ID: $Id$
6ce43ac9
SC
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
6ce43ac9
SC
10#include "wx/wxprec.h"
11
6ce43ac9 12#ifdef __BORLANDC__
274b7a40 13 #pragma hdrstop
6ce43ac9
SC
14#endif
15
18f3decb
WS
16#if wxUSE_IMAGLIST
17
6ce43ac9 18#include "wx/imaglist.h"
6ce43ac9
SC
19#include "wx/icon.h"
20#include "wx/image.h"
21#include "wx/dc.h"
22
6ce43ac9
SC
23IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
24
274b7a40 25
6ce43ac9
SC
26wxImageList::wxImageList( int width, int height, bool mask, int initialCount )
27{
28 (void)Create(width, height, mask, initialCount);
29}
30
31wxImageList::~wxImageList()
32{
33 (void)RemoveAll();
34}
35
36int wxImageList::GetImageCount() const
37{
38 return m_images.GetCount();
39}
40
41bool wxImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
42{
43 m_width = width;
44 m_height = height;
45
46 return Create();
47}
48
49bool wxImageList::Create()
50{
51 return true;
52}
53
54int wxImageList::Add( const wxIcon &bitmap )
55{
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.") );
60
61 m_images.Append( new wxIcon( bitmap ) );
62
63 if (m_width == 0 && m_height == 0)
64 {
65 m_width = bitmap.GetWidth();
66 m_height = bitmap.GetHeight();
67 }
274b7a40
DS
68
69 return m_images.GetCount() - 1;
6ce43ac9
SC
70}
71
72int wxImageList::Add( const wxBitmap &bitmap )
73{
5b9003ae 74 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
6ce43ac9
SC
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.") );
5b9003ae
RR
78
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)
82 {
83 int numImages = bitmap.GetWidth() / m_width;
84 for (int subIndex = 0; subIndex < numImages; subIndex++)
85 {
86 wxRect rect(m_width * subIndex, 0, m_width, m_height);
87 wxBitmap tmpBmp = bitmap.GetSubBitmap(rect);
88 m_images.Append( new wxBitmap(tmpBmp) );
89 }
90 }
91 else
92 {
93 m_images.Append( new wxBitmap(bitmap) );
94 }
6ce43ac9
SC
95
96 if (m_width == 0 && m_height == 0)
97 {
98 m_width = bitmap.GetWidth();
99 m_height = bitmap.GetHeight();
100 }
5b9003ae 101
274b7a40 102 return m_images.GetCount() - 1;
6ce43ac9
SC
103}
104
105int wxImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
106{
274b7a40 107 wxBitmap bmp( bitmap );
6ce43ac9 108 if (mask.Ok())
274b7a40
DS
109 bmp.SetMask( new wxMask( mask ) );
110
111 return Add( bmp );
6ce43ac9
SC
112}
113
114int wxImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
115{
116 wxImage img = bitmap.ConvertToImage();
274b7a40
DS
117 img.SetMaskColour( maskColour.Red(), maskColour.Green(), maskColour.Blue() );
118
119 return Add( wxBitmap( img ) );
6ce43ac9
SC
120}
121
122// Get the bitmap
123wxBitmap wxImageList::GetBitmap(int index) const
124{
125 wxList::compatibility_iterator node = m_images.Item( index );
126
127 wxCHECK_MSG( node, wxNullBitmap , wxT("wrong index in image list") );
128
129 wxObject* obj = (wxObject*) node->GetData();
130 if ( obj == NULL )
131 return wxNullBitmap ;
132 else if ( obj->IsKindOf(CLASSINFO(wxIcon)) )
274b7a40
DS
133 return wxBitmap( *(wx_static_cast(wxIcon*, obj)) ) ;
134 else
135 return *(wx_static_cast(wxBitmap*, obj)) ;
6ce43ac9
SC
136}
137
138// Get the icon
139wxIcon wxImageList::GetIcon(int index) const
140{
141 wxList::compatibility_iterator node = m_images.Item( index );
142
143 wxCHECK_MSG( node, wxNullIcon , wxT("wrong index in image list") );
144
145 wxObject* obj = (wxObject*) node->GetData();
146 if ( obj == NULL )
147 return wxNullIcon ;
274b7a40 148 else if ( obj->IsKindOf(CLASSINFO(wxBitmap)) )
6ce43ac9
SC
149 {
150 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
151 return wxNullIcon ;
152 }
274b7a40
DS
153 else
154 return *(wx_static_cast(wxIcon*, obj)) ;
6ce43ac9
SC
155}
156
157bool wxImageList::Replace( int index, const wxBitmap &bitmap )
158{
159 wxList::compatibility_iterator node = m_images.Item( index );
160
161 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
162
274b7a40 163 wxBitmap* newBitmap = new wxBitmap( bitmap );
6ce43ac9
SC
164
165 if (index == (int) m_images.GetCount() - 1)
166 {
167 delete node->GetData();
274b7a40 168
6ce43ac9
SC
169 m_images.Erase( node );
170 m_images.Append( newBitmap );
171 }
172 else
173 {
174 wxList::compatibility_iterator next = node->GetNext();
175 delete node->GetData();
274b7a40 176
6ce43ac9
SC
177 m_images.Erase( node );
178 m_images.Insert( next, newBitmap );
179 }
180
181 return true;
182}
183
184bool wxImageList::Replace( int index, const wxIcon &bitmap )
185{
186 wxList::compatibility_iterator node = m_images.Item( index );
187
188 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
189
274b7a40 190 wxIcon* newBitmap = new wxIcon( bitmap );
6ce43ac9
SC
191
192 if (index == (int) m_images.GetCount() - 1)
193 {
194 delete node->GetData();
195 m_images.Erase( node );
196 m_images.Append( newBitmap );
197 }
198 else
199 {
200 wxList::compatibility_iterator next = node->GetNext();
201 delete node->GetData();
202 m_images.Erase( node );
203 m_images.Insert( next, newBitmap );
204 }
205
206 return true;
207}
208
5b9003ae
RR
209bool wxImageList::Replace( int index, const wxBitmap &bitmap, const wxBitmap &mask )
210{
211 wxList::compatibility_iterator node = m_images.Item( index );
212
213 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
214
215 wxBitmap* newBitmap = new wxBitmap(bitmap);
216
217 if (index == (int) m_images.GetCount() - 1)
218 {
219 delete node->GetData();
220 m_images.Erase( node );
221 m_images.Append( newBitmap );
222 }
223 else
224 {
225 wxList::compatibility_iterator next = node->GetNext();
226 delete node->GetData();
227 m_images.Erase( node );
228 m_images.Insert( next, newBitmap );
229 }
230
231 if (mask.Ok())
232 newBitmap->SetMask(new wxMask(mask));
233
234 return true;
235}
236
6ce43ac9
SC
237bool wxImageList::Remove( int index )
238{
239 wxList::compatibility_iterator node = m_images.Item( index );
240
241 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
242
243 delete node->GetData();
244 m_images.Erase( node );
245
246 return true;
247}
248
249bool wxImageList::RemoveAll()
250{
251 WX_CLEAR_LIST(wxList, m_images);
252 m_images.Clear();
253
254 return true;
255}
256
257bool wxImageList::GetSize( int index, int &width, int &height ) const
258{
259 width = 0;
260 height = 0;
261
262 wxList::compatibility_iterator node = m_images.Item( index );
263
264 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
265
266 wxObject *obj = (wxObject*)node->GetData();
274b7a40 267 if (obj->IsKindOf(CLASSINFO(wxIcon)))
6ce43ac9
SC
268 {
269 wxIcon *bm = wx_static_cast( wxIcon* , obj ) ;
270 width = bm->GetWidth();
271 height = bm->GetHeight();
272 }
273 else
274 {
275 wxBitmap *bm = wx_static_cast( wxBitmap* , obj ) ;
276 width = bm->GetWidth();
277 height = bm->GetHeight();
278 }
274b7a40 279
6ce43ac9
SC
280 return true;
281}
282
274b7a40
DS
283bool wxImageList::Draw(
284 int index, wxDC &dc, int x, int y,
285 int flags, bool WXUNUSED(solidBackground) )
6ce43ac9
SC
286{
287 wxList::compatibility_iterator node = m_images.Item( index );
288
289 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
290
291 wxObject *obj = (wxObject*)node->GetData();
274b7a40 292 if (obj->IsKindOf(CLASSINFO(wxIcon)))
6ce43ac9
SC
293 {
294 wxIcon *bm = wx_static_cast( wxIcon* , obj ) ;
274b7a40 295 dc.DrawIcon( *bm , x, y );
6ce43ac9
SC
296 }
297 else
298 {
299 wxBitmap *bm = wx_static_cast( wxBitmap* , obj ) ;
300 dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
301 }
302
303 return true;
304}
305
306#endif // wxUSE_IMAGLIST