]> git.saurik.com Git - wxWidgets.git/blame - src/generic/imaglist.cpp
Guard against comparing invalid iterators, which produces an assertion
[wxWidgets.git] / src / generic / imaglist.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/generic/imaglist.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
01111366
RR
5// Id: $id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
1e6d9499
JS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
93763ad5 14 #pragma hdrstop
1e6d9499
JS
15#endif
16
93763ad5 17#if wxUSE_IMAGLIST
9de05dfd 18
4055ed82 19#ifndef __WXPALMOS__
9de05dfd 20
f60d0f94 21#include "wx/generic/imaglist.h"
9de05dfd 22
da80ae71
WS
23#ifndef WX_PRECOMP
24 #include "wx/dc.h"
923d28da 25 #include "wx/icon.h"
da80ae71
WS
26#endif
27
4d449473 28#include "wx/image.h"
c801d85f
KB
29
30//-----------------------------------------------------------------------------
31// wxImageList
32//-----------------------------------------------------------------------------
33
3cd94a0d 34IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject)
c801d85f 35
3a5bcc4d 36#if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
b31989e2
JS
37/*
38 * wxImageList has to be a real class or we have problems with
39 * the run-time information.
40 */
41
42IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList)
43#endif
44
3cd94a0d 45wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount )
c801d85f 46{
f6bcfd97 47 (void)Create(width, height, mask, initialCount);
e1e955e1 48}
c801d85f 49
3cd94a0d 50wxGenericImageList::~wxGenericImageList()
c801d85f 51{
3fc93ebd 52 (void)RemoveAll();
e1e955e1 53}
c801d85f 54
3cd94a0d 55int wxGenericImageList::GetImageCount() const
c801d85f 56{
b1d4dd7a 57 return m_images.GetCount();
e1e955e1 58}
c801d85f 59
3cd94a0d 60bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
f6bcfd97
BP
61{
62 m_width = width;
63 m_height = height;
64
65 return Create();
66}
67
3cd94a0d 68bool wxGenericImageList::Create()
c801d85f 69{
ca65c044 70 return true;
e1e955e1 71}
c801d85f 72
3cd94a0d 73int wxGenericImageList::Add( const wxBitmap &bitmap )
c801d85f 74{
5b9003ae 75 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
b931414d 76 || (m_width == 0 && m_height == 0),
9bd41907
VZ
77 _T("invalid bitmap size in wxImageList: this might work ")
78 _T("on this platform but definitely won't under Windows.") );
79
f60d0f94 80 if (bitmap.IsKindOf(CLASSINFO(wxIcon)))
5b9003ae 81 {
f60d0f94 82 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
5b9003ae 83 }
f60d0f94 84 else
5b9003ae
RR
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 }
b931414d
RD
103
104 if (m_width == 0 && m_height == 0)
105 {
106 m_width = bitmap.GetWidth();
107 m_height = bitmap.GetHeight();
108 }
93763ad5 109
b1d4dd7a 110 return m_images.GetCount()-1;
e1e955e1 111}
c801d85f 112
3cd94a0d 113int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
4d449473
VS
114{
115 wxBitmap bmp(bitmap);
a7f1fbf6
RD
116 if (mask.Ok())
117 bmp.SetMask(new wxMask(mask));
4d449473
VS
118 return Add(bmp);
119}
120
3cd94a0d 121int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
4d449473 122{
368d59f0 123 wxImage img = bitmap.ConvertToImage();
4d449473 124 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
368d59f0 125 return Add(wxBitmap(img));
4d449473
VS
126}
127
49bf4e3e 128const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
b46e8696 129{
222ed1d6 130 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 131
223d09f6 132 wxCHECK_MSG( node, (wxBitmap *) NULL, wxT("wrong index in image list") );
43543d98 133
b1d4dd7a 134 return (wxBitmap*)node->GetData();
40413a5b 135}
43543d98 136
49bf4e3e 137// Get the bitmap
f45fac95 138wxBitmap wxGenericImageList::GetBitmap(int index) const
49bf4e3e
JS
139{
140 const wxBitmap* bmp = GetBitmapPtr(index);
141 if (bmp)
142 return *bmp;
143 else
144 return wxNullBitmap;
145}
146
147// Get the icon
f45fac95 148wxIcon wxGenericImageList::GetIcon(int index) const
49bf4e3e
JS
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
3cd94a0d 161bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
c801d85f 162{
222ed1d6 163 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 164
ca65c044 165 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
f60d0f94 166
999836aa
VZ
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) ;
f60d0f94 176
b1d4dd7a 177 if (index == (int) m_images.GetCount() - 1)
b46e8696 178 {
222ed1d6
MB
179 delete node->GetData();
180 m_images.Erase( node );
f60d0f94 181 m_images.Append( newBitmap );
b46e8696
RR
182 }
183 else
184 {
222ed1d6
MB
185 wxList::compatibility_iterator next = node->GetNext();
186 delete node->GetData();
187 m_images.Erase( node );
f60d0f94 188 m_images.Insert( next, newBitmap );
b46e8696 189 }
43543d98 190
ca65c044 191 return true;
e1e955e1 192}
c801d85f 193
f644bc11
RR
194bool 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 }
da80ae71 223
f644bc11
RR
224 if (mask.Ok())
225 newBitmap->SetMask(new wxMask(mask));
226
227 return true;
228}
229
3cd94a0d 230bool wxGenericImageList::Remove( int index )
c801d85f 231{
222ed1d6 232 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 233
ca65c044 234 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 235
222ed1d6
MB
236 delete node->GetData();
237 m_images.Erase( node );
43543d98 238
ca65c044 239 return true;
e1e955e1 240}
c801d85f 241
3cd94a0d 242bool wxGenericImageList::RemoveAll()
c801d85f 243{
222ed1d6 244 WX_CLEAR_LIST(wxList, m_images);
b46e8696 245 m_images.Clear();
43543d98 246
ca65c044 247 return true;
e1e955e1 248}
c801d85f 249
3cd94a0d 250bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
c801d85f 251{
b46e8696
RR
252 width = 0;
253 height = 0;
43543d98 254
222ed1d6 255 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 256
ca65c044 257 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 258
b1d4dd7a 259 wxBitmap *bm = (wxBitmap*)node->GetData();
c801d85f
KB
260 width = bm->GetWidth();
261 height = bm->GetHeight();
43543d98 262
ca65c044 263 return true;
e1e955e1 264}
c801d85f 265
3cd94a0d 266bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
219f895a 267 int flags, bool WXUNUSED(solidBackground) )
c801d85f 268{
222ed1d6 269 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 270
ca65c044 271 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 272
b1d4dd7a 273 wxBitmap *bm = (wxBitmap*)node->GetData();
f60d0f94
JS
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 );
219f895a 279
ca65c044 280 return true;
e1e955e1 281}
c801d85f 282
4055ed82 283#endif // __WXPALMOS__
da80ae71
WS
284
285#endif // wxUSE_IMAGLIST