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