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