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