]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/imaglist.cpp
Include wx/memory.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / carbon / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/imaglist.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS_ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_IMAGLIST
17
18 #include "wx/imaglist.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/dc.h"
22 #include "wx/icon.h"
23 #endif
24
25 #include "wx/image.h"
26
27 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
28
29
30 wxImageList::wxImageList( int width, int height, bool mask, int initialCount )
31 {
32 (void)Create(width, height, mask, initialCount);
33 }
34
35 wxImageList::~wxImageList()
36 {
37 (void)RemoveAll();
38 }
39
40 int wxImageList::GetImageCount() const
41 {
42 return m_images.GetCount();
43 }
44
45 bool 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
53 bool wxImageList::Create()
54 {
55 return true;
56 }
57
58 int 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 }
72
73 return m_images.GetCount() - 1;
74 }
75
76 int wxImageList::Add( const wxBitmap &bitmap )
77 {
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.") );
82
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 }
99
100 if (m_width == 0 && m_height == 0)
101 {
102 m_width = bitmap.GetWidth();
103 m_height = bitmap.GetHeight();
104 }
105
106 return m_images.GetCount() - 1;
107 }
108
109 int wxImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
110 {
111 wxBitmap bmp( bitmap );
112 if (mask.Ok())
113 bmp.SetMask( new wxMask( mask ) );
114
115 return Add( bmp );
116 }
117
118 int wxImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
119 {
120 wxImage img = bitmap.ConvertToImage();
121 img.SetMaskColour( maskColour.Red(), maskColour.Green(), maskColour.Blue() );
122
123 return Add( wxBitmap( img ) );
124 }
125
126 // Get the bitmap
127 wxBitmap 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)) )
137 return wxBitmap( *(wx_static_cast(wxIcon*, obj)) ) ;
138 else
139 return *(wx_static_cast(wxBitmap*, obj)) ;
140 }
141
142 // Get the icon
143 wxIcon 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 ;
152 else if ( obj->IsKindOf(CLASSINFO(wxBitmap)) )
153 {
154 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
155 return wxNullIcon ;
156 }
157 else
158 return *(wx_static_cast(wxIcon*, obj)) ;
159 }
160
161 bool 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
167 wxBitmap* newBitmap = new wxBitmap( bitmap );
168
169 if (index == (int) m_images.GetCount() - 1)
170 {
171 delete node->GetData();
172
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();
180
181 m_images.Erase( node );
182 m_images.Insert( next, newBitmap );
183 }
184
185 return true;
186 }
187
188 bool 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
194 wxIcon* newBitmap = new wxIcon( bitmap );
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
213 bool 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 }
234
235 if (mask.Ok())
236 newBitmap->SetMask(new wxMask(mask));
237
238 return true;
239 }
240
241 bool 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
253 bool wxImageList::RemoveAll()
254 {
255 WX_CLEAR_LIST(wxList, m_images);
256 m_images.Clear();
257
258 return true;
259 }
260
261 bool 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();
271 if (obj->IsKindOf(CLASSINFO(wxIcon)))
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 }
283
284 return true;
285 }
286
287 bool wxImageList::Draw(
288 int index, wxDC &dc, int x, int y,
289 int flags, bool WXUNUSED(solidBackground) )
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();
296 if (obj->IsKindOf(CLASSINFO(wxIcon)))
297 {
298 wxIcon *bm = wx_static_cast( wxIcon* , obj ) ;
299 dc.DrawIcon( *bm , x, y );
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