Remove duplication in wxImageList defines and always default to using the native...
[wxWidgets.git] / src / generic / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/imaglist.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_IMAGLIST
18
19 #ifndef __WXPALMOS__
20
21 #include "wx/imaglist.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/dc.h"
25 #include "wx/icon.h"
26 #include "wx/image.h"
27 #endif
28
29 //-----------------------------------------------------------------------------
30 // wxImageList
31 //-----------------------------------------------------------------------------
32
33 IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject)
34
35 #if !HAVE_NATIVE_IMAGELIST
36 /*
37 * wxImageList has to be a real class or we have problems with
38 * the run-time information.
39 */
40
41 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList)
42 #endif
43
44 wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount )
45 {
46 (void)Create(width, height, mask, initialCount);
47 }
48
49 wxGenericImageList::~wxGenericImageList()
50 {
51 (void)RemoveAll();
52 }
53
54 int wxGenericImageList::GetImageCount() const
55 {
56 return m_images.GetCount();
57 }
58
59 bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
60 {
61 m_width = width;
62 m_height = height;
63
64 return Create();
65 }
66
67 bool wxGenericImageList::Create()
68 {
69 return true;
70 }
71
72 int wxGenericImageList::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 if (bitmap.IsKindOf(CLASSINFO(wxIcon)))
80 {
81 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
82 }
83 else
84 {
85 // Mimic behavior of Windows ImageList_Add that automatically breaks up the added
86 // bitmap into sub-images of the correct size
87 if (m_width > 0 && bitmap.GetWidth() > m_width && bitmap.GetHeight() >= m_height)
88 {
89 int numImages = bitmap.GetWidth() / m_width;
90 for (int subIndex = 0; subIndex < numImages; subIndex++)
91 {
92 wxRect rect(m_width * subIndex, 0, m_width, m_height);
93 wxBitmap tmpBmp = bitmap.GetSubBitmap(rect);
94 m_images.Append( new wxBitmap(tmpBmp) );
95 }
96 }
97 else
98 {
99 m_images.Append( new wxBitmap(bitmap) );
100 }
101 }
102
103 if (m_width == 0 && m_height == 0)
104 {
105 m_width = bitmap.GetWidth();
106 m_height = bitmap.GetHeight();
107 }
108
109 return m_images.GetCount()-1;
110 }
111
112 int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
113 {
114 wxBitmap bmp(bitmap);
115 if (mask.Ok())
116 bmp.SetMask(new wxMask(mask));
117 return Add(bmp);
118 }
119
120 int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
121 {
122 wxImage img = bitmap.ConvertToImage();
123 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
124 return Add(wxBitmap(img));
125 }
126
127 const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
128 {
129 wxList::compatibility_iterator node = m_images.Item( index );
130
131 wxCHECK_MSG( node, (wxBitmap *) NULL, wxT("wrong index in image list") );
132
133 return (wxBitmap*)node->GetData();
134 }
135
136 // Get the bitmap
137 wxBitmap wxGenericImageList::GetBitmap(int index) const
138 {
139 const wxBitmap* bmp = GetBitmapPtr(index);
140 if (bmp)
141 return *bmp;
142 else
143 return wxNullBitmap;
144 }
145
146 // Get the icon
147 wxIcon wxGenericImageList::GetIcon(int index) const
148 {
149 const wxBitmap* bmp = GetBitmapPtr(index);
150 if (bmp)
151 {
152 wxIcon icon;
153 icon.CopyFromBitmap(*bmp);
154 return icon;
155 }
156 else
157 return wxNullIcon;
158 }
159
160 bool wxGenericImageList::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 = (bitmap.IsKindOf(CLASSINFO(wxIcon))) ?
167 #if defined(__VISAGECPP__)
168 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
169 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
170 new wxBitmap(bitmap)
171 #else
172 new wxBitmap( (const wxIcon&) bitmap )
173 #endif
174 : new wxBitmap(bitmap) ;
175
176 if (index == (int) m_images.GetCount() - 1)
177 {
178 delete node->GetData();
179 m_images.Erase( node );
180 m_images.Append( newBitmap );
181 }
182 else
183 {
184 wxList::compatibility_iterator next = node->GetNext();
185 delete node->GetData();
186 m_images.Erase( node );
187 m_images.Insert( next, newBitmap );
188 }
189
190 return true;
191 }
192
193 bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap, const wxBitmap &mask )
194 {
195 wxList::compatibility_iterator node = m_images.Item( index );
196
197 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
198
199 wxBitmap* newBitmap = (bitmap.IsKindOf(CLASSINFO(wxIcon))) ?
200 #if defined(__VISAGECPP__)
201 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
202 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
203 new wxBitmap(bitmap)
204 #else
205 new wxBitmap( (const wxIcon&) bitmap )
206 #endif
207 : new wxBitmap(bitmap) ;
208
209 if (index == (int) m_images.GetCount() - 1)
210 {
211 delete node->GetData();
212 m_images.Erase( node );
213 m_images.Append( newBitmap );
214 }
215 else
216 {
217 wxList::compatibility_iterator next = node->GetNext();
218 delete node->GetData();
219 m_images.Erase( node );
220 m_images.Insert( next, newBitmap );
221 }
222
223 if (mask.Ok())
224 newBitmap->SetMask(new wxMask(mask));
225
226 return true;
227 }
228
229 bool wxGenericImageList::Remove( int index )
230 {
231 wxList::compatibility_iterator node = m_images.Item( index );
232
233 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
234
235 delete node->GetData();
236 m_images.Erase( node );
237
238 return true;
239 }
240
241 bool wxGenericImageList::RemoveAll()
242 {
243 WX_CLEAR_LIST(wxList, m_images);
244 m_images.Clear();
245
246 return true;
247 }
248
249 bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
250 {
251 width = 0;
252 height = 0;
253
254 wxList::compatibility_iterator node = m_images.Item( index );
255
256 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
257
258 wxBitmap *bm = (wxBitmap*)node->GetData();
259 width = bm->GetWidth();
260 height = bm->GetHeight();
261
262 return true;
263 }
264
265 bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
266 int flags, bool WXUNUSED(solidBackground) )
267 {
268 wxList::compatibility_iterator node = m_images.Item( index );
269
270 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
271
272 wxBitmap *bm = (wxBitmap*)node->GetData();
273
274 if (bm->IsKindOf(CLASSINFO(wxIcon)))
275 dc.DrawIcon( * ((wxIcon*) bm), x, y);
276 else
277 dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
278
279 return true;
280 }
281
282 #endif // __WXPALMOS__
283
284 #endif // wxUSE_IMAGLIST