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