]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/imaglist.cpp
further encapsulation of graphics context, wxdc becoming 'generic'
[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 #include "wx/image.h"
24 #endif
25
26 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
27
28
29 wxImageList::wxImageList( int width, int height, bool mask, int initialCount )
30 {
31 (void)Create(width, height, mask, initialCount);
32 }
33
34 wxImageList::~wxImageList()
35 {
36 (void)RemoveAll();
37 }
38
39 int wxImageList::GetImageCount() const
40 {
41 return m_images.GetCount();
42 }
43
44 bool 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
52 bool wxImageList::Create()
53 {
54 return true;
55 }
56
57 int 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 }
71
72 return m_images.GetCount() - 1;
73 }
74
75 int wxImageList::Add( const wxBitmap &bitmap )
76 {
77 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
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.") );
81
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 }
98
99 if (m_width == 0 && m_height == 0)
100 {
101 m_width = bitmap.GetWidth();
102 m_height = bitmap.GetHeight();
103 }
104
105 return m_images.GetCount() - 1;
106 }
107
108 int wxImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
109 {
110 wxBitmap bmp( bitmap );
111 if (mask.Ok())
112 bmp.SetMask( new wxMask( mask ) );
113
114 return Add( bmp );
115 }
116
117 int wxImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
118 {
119 wxImage img = bitmap.ConvertToImage();
120 img.SetMaskColour( maskColour.Red(), maskColour.Green(), maskColour.Blue() );
121
122 return Add( wxBitmap( img ) );
123 }
124
125 // Get the bitmap
126 wxBitmap 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)) )
136 return wxBitmap( *(wx_static_cast(wxIcon*, obj)) ) ;
137 else
138 return *(wx_static_cast(wxBitmap*, obj)) ;
139 }
140
141 // Get the icon
142 wxIcon 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 ;
151 else if ( obj->IsKindOf(CLASSINFO(wxBitmap)) )
152 {
153 wxFAIL_MSG( wxT("cannot convert from bitmap to icon") ) ;
154 return wxNullIcon ;
155 }
156 else
157 return *(wx_static_cast(wxIcon*, obj)) ;
158 }
159
160 bool 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
166 wxBitmap* newBitmap = new wxBitmap( bitmap );
167
168 if (index == (int) m_images.GetCount() - 1)
169 {
170 delete node->GetData();
171
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();
179
180 m_images.Erase( node );
181 m_images.Insert( next, newBitmap );
182 }
183
184 return true;
185 }
186
187 bool 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
193 wxIcon* newBitmap = new wxIcon( bitmap );
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
212 bool 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 }
233
234 if (mask.Ok())
235 newBitmap->SetMask(new wxMask(mask));
236
237 return true;
238 }
239
240 bool 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
252 bool wxImageList::RemoveAll()
253 {
254 WX_CLEAR_LIST(wxList, m_images);
255 m_images.Clear();
256
257 return true;
258 }
259
260 bool 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();
270 if (obj->IsKindOf(CLASSINFO(wxIcon)))
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 }
282
283 return true;
284 }
285
286 bool wxImageList::Draw(
287 int index, wxDC &dc, int x, int y,
288 int flags, bool WXUNUSED(solidBackground) )
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();
295 if (obj->IsKindOf(CLASSINFO(wxIcon)))
296 {
297 wxIcon *bm = wx_static_cast( wxIcon* , obj ) ;
298 dc.DrawIcon( *bm , x, y );
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