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