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