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