]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/imaglist.cpp
f97e0715ac21a285194b429b5a739ae0daeb456d
[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 #include "wx/icon.h"
20 #include "wx/image.h"
21 #include "wx/dc.h"
22
23 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
24
25
26 wxImageList::wxImageList( int width, int height, bool mask, int initialCount )
27 {
28 (void)Create(width, height, mask, initialCount);
29 }
30
31 wxImageList::~wxImageList()
32 {
33 (void)RemoveAll();
34 }
35
36 int wxImageList::GetImageCount() const
37 {
38 return m_images.GetCount();
39 }
40
41 bool 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
49 bool wxImageList::Create()
50 {
51 return true;
52 }
53
54 int 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 }
68
69 return m_images.GetCount() - 1;
70 }
71
72 int wxImageList::Add( const wxBitmap &bitmap )
73 {
74 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
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.") );
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 }
95
96 if (m_width == 0 && m_height == 0)
97 {
98 m_width = bitmap.GetWidth();
99 m_height = bitmap.GetHeight();
100 }
101
102 return m_images.GetCount() - 1;
103 }
104
105 int wxImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
106 {
107 wxBitmap bmp( bitmap );
108 if (mask.Ok())
109 bmp.SetMask( new wxMask( mask ) );
110
111 return Add( bmp );
112 }
113
114 int wxImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
115 {
116 wxImage img = bitmap.ConvertToImage();
117 img.SetMaskColour( maskColour.Red(), maskColour.Green(), maskColour.Blue() );
118
119 return Add( wxBitmap( img ) );
120 }
121
122 // Get the bitmap
123 wxBitmap 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)) )
133 return wxBitmap( *(wx_static_cast(wxIcon*, obj)) ) ;
134 else
135 return *(wx_static_cast(wxBitmap*, obj)) ;
136 }
137
138 // Get the icon
139 wxIcon 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 ;
148 else if ( obj->IsKindOf(CLASSINFO(wxBitmap)) )
149 {
150 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
151 return wxNullIcon ;
152 }
153 else
154 return *(wx_static_cast(wxIcon*, obj)) ;
155 }
156
157 bool 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
163 wxBitmap* newBitmap = new wxBitmap( bitmap );
164
165 if (index == (int) m_images.GetCount() - 1)
166 {
167 delete node->GetData();
168
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();
176
177 m_images.Erase( node );
178 m_images.Insert( next, newBitmap );
179 }
180
181 return true;
182 }
183
184 bool 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
190 wxIcon* newBitmap = new wxIcon( bitmap );
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
209 bool 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
237 bool 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
249 bool wxImageList::RemoveAll()
250 {
251 WX_CLEAR_LIST(wxList, m_images);
252 m_images.Clear();
253
254 return true;
255 }
256
257 bool 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();
267 if (obj->IsKindOf(CLASSINFO(wxIcon)))
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 }
279
280 return true;
281 }
282
283 bool wxImageList::Draw(
284 int index, wxDC &dc, int x, int y,
285 int flags, bool WXUNUSED(solidBackground) )
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();
292 if (obj->IsKindOf(CLASSINFO(wxIcon)))
293 {
294 wxIcon *bm = wx_static_cast( wxIcon* , obj ) ;
295 dc.DrawIcon( *bm , x, y );
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