]> git.saurik.com Git - wxWidgets.git/blame - src/generic/imaglist.cpp
Added a flag suppressing node content conversion when saving to XML.
[wxWidgets.git] / src / generic / imaglist.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/generic/imaglist.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
0e1381f2 5// Id: $Id$
01111366 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
9cd7a3f7 17#if wxUSE_IMAGLIST && !defined(wxHAS_NATIVE_IMAGELIST)
9de05dfd 18
4055ed82 19#ifndef __WXPALMOS__
9de05dfd 20
8a3e173a 21#include "wx/imaglist.h"
9de05dfd 22
da80ae71
WS
23#ifndef WX_PRECOMP
24 #include "wx/dc.h"
923d28da 25 #include "wx/icon.h"
155ecd4c 26 #include "wx/image.h"
da80ae71
WS
27#endif
28
c801d85f
KB
29//-----------------------------------------------------------------------------
30// wxImageList
31//-----------------------------------------------------------------------------
32
3cd94a0d 33IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject)
b31989e2 34IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList)
b31989e2 35
3cd94a0d 36wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount )
c801d85f 37{
f6bcfd97 38 (void)Create(width, height, mask, initialCount);
e1e955e1 39}
c801d85f 40
3cd94a0d 41wxGenericImageList::~wxGenericImageList()
c801d85f 42{
3fc93ebd 43 (void)RemoveAll();
e1e955e1 44}
c801d85f 45
3cd94a0d 46int wxGenericImageList::GetImageCount() const
c801d85f 47{
b1d4dd7a 48 return m_images.GetCount();
e1e955e1 49}
c801d85f 50
3cd94a0d 51bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
f6bcfd97
BP
52{
53 m_width = width;
54 m_height = height;
55
56 return Create();
57}
58
3cd94a0d 59bool wxGenericImageList::Create()
c801d85f 60{
ca65c044 61 return true;
e1e955e1 62}
c801d85f 63
3cd94a0d 64int wxGenericImageList::Add( const wxBitmap &bitmap )
c801d85f 65{
5b9003ae 66 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
b931414d 67 || (m_width == 0 && m_height == 0),
9a83f860
VZ
68 wxT("invalid bitmap size in wxImageList: this might work ")
69 wxT("on this platform but definitely won't under Windows.") );
9bd41907 70
0e1381f2
PC
71 const int index = int(m_images.GetCount());
72
f60d0f94 73 if (bitmap.IsKindOf(CLASSINFO(wxIcon)))
5b9003ae 74 {
f60d0f94 75 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
5b9003ae 76 }
f60d0f94 77 else
5b9003ae
RR
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 }
b931414d
RD
96
97 if (m_width == 0 && m_height == 0)
98 {
99 m_width = bitmap.GetWidth();
100 m_height = bitmap.GetHeight();
101 }
93763ad5 102
0e1381f2 103 return index;
e1e955e1 104}
c801d85f 105
3cd94a0d 106int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
4d449473
VS
107{
108 wxBitmap bmp(bitmap);
a7f1fbf6
RD
109 if (mask.Ok())
110 bmp.SetMask(new wxMask(mask));
4d449473
VS
111 return Add(bmp);
112}
113
3cd94a0d 114int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
4d449473 115{
368d59f0 116 wxImage img = bitmap.ConvertToImage();
4d449473 117 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
368d59f0 118 return Add(wxBitmap(img));
4d449473
VS
119}
120
49bf4e3e 121const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
b46e8696 122{
a1abd1a9 123 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 124
d3b9f782 125 wxCHECK_MSG( node, NULL, wxT("wrong index in image list") );
43543d98 126
b1d4dd7a 127 return (wxBitmap*)node->GetData();
40413a5b 128}
43543d98 129
49bf4e3e 130// Get the bitmap
f45fac95 131wxBitmap wxGenericImageList::GetBitmap(int index) const
49bf4e3e
JS
132{
133 const wxBitmap* bmp = GetBitmapPtr(index);
134 if (bmp)
135 return *bmp;
136 else
137 return wxNullBitmap;
138}
139
140// Get the icon
f45fac95 141wxIcon wxGenericImageList::GetIcon(int index) const
49bf4e3e
JS
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
3cd94a0d 154bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
c801d85f 155{
a1abd1a9 156 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 157
ca65c044 158 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
f60d0f94 159
999836aa
VZ
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) ;
f60d0f94 169
b1d4dd7a 170 if (index == (int) m_images.GetCount() - 1)
b46e8696 171 {
222ed1d6
MB
172 delete node->GetData();
173 m_images.Erase( node );
f60d0f94 174 m_images.Append( newBitmap );
b46e8696
RR
175 }
176 else
177 {
a1abd1a9 178 wxObjectList::compatibility_iterator next = node->GetNext();
222ed1d6
MB
179 delete node->GetData();
180 m_images.Erase( node );
f60d0f94 181 m_images.Insert( next, newBitmap );
b46e8696 182 }
43543d98 183
ca65c044 184 return true;
e1e955e1 185}
c801d85f 186
f644bc11
RR
187bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap, const wxBitmap &mask )
188{
a1abd1a9 189 wxObjectList::compatibility_iterator node = m_images.Item( index );
f644bc11
RR
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 {
a1abd1a9 211 wxObjectList::compatibility_iterator next = node->GetNext();
f644bc11
RR
212 delete node->GetData();
213 m_images.Erase( node );
214 m_images.Insert( next, newBitmap );
215 }
da80ae71 216
f644bc11
RR
217 if (mask.Ok())
218 newBitmap->SetMask(new wxMask(mask));
219
220 return true;
221}
222
3cd94a0d 223bool wxGenericImageList::Remove( int index )
c801d85f 224{
a1abd1a9 225 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 226
ca65c044 227 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 228
222ed1d6
MB
229 delete node->GetData();
230 m_images.Erase( node );
43543d98 231
ca65c044 232 return true;
e1e955e1 233}
c801d85f 234
3cd94a0d 235bool wxGenericImageList::RemoveAll()
c801d85f 236{
a1abd1a9 237 WX_CLEAR_LIST(wxObjectList, m_images);
b46e8696 238 m_images.Clear();
43543d98 239
ca65c044 240 return true;
e1e955e1 241}
c801d85f 242
3cd94a0d 243bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
c801d85f 244{
b46e8696
RR
245 width = 0;
246 height = 0;
43543d98 247
a1abd1a9 248 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 249
ca65c044 250 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 251
b1d4dd7a 252 wxBitmap *bm = (wxBitmap*)node->GetData();
c801d85f
KB
253 width = bm->GetWidth();
254 height = bm->GetHeight();
43543d98 255
ca65c044 256 return true;
e1e955e1 257}
c801d85f 258
3cd94a0d 259bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
219f895a 260 int flags, bool WXUNUSED(solidBackground) )
c801d85f 261{
a1abd1a9 262 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 263
ca65c044 264 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 265
b1d4dd7a 266 wxBitmap *bm = (wxBitmap*)node->GetData();
f60d0f94
JS
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 );
219f895a 272
ca65c044 273 return true;
e1e955e1 274}
c801d85f 275
4055ed82 276#endif // __WXPALMOS__
da80ae71
WS
277
278#endif // wxUSE_IMAGLIST