]> git.saurik.com Git - wxWidgets.git/blame - src/generic/imaglist.cpp
wxRTC: fixed guidelines overwriting adjacent cell borders; corrected capitalisation...
[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 5// Copyright: (c) 1998 Robert Roebling
65571936 6// Licence: wxWindows licence
c801d85f
KB
7/////////////////////////////////////////////////////////////////////////////
8
1e6d9499
JS
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
11
12#ifdef __BORLANDC__
93763ad5 13 #pragma hdrstop
1e6d9499
JS
14#endif
15
9cd7a3f7 16#if wxUSE_IMAGLIST && !defined(wxHAS_NATIVE_IMAGELIST)
9de05dfd 17
8a3e173a 18#include "wx/imaglist.h"
9de05dfd 19
da80ae71
WS
20#ifndef WX_PRECOMP
21 #include "wx/dc.h"
923d28da 22 #include "wx/icon.h"
155ecd4c 23 #include "wx/image.h"
da80ae71
WS
24#endif
25
c801d85f
KB
26//-----------------------------------------------------------------------------
27// wxImageList
28//-----------------------------------------------------------------------------
29
3cd94a0d 30IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject)
b31989e2 31IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList)
b31989e2 32
3cd94a0d 33wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount )
c801d85f 34{
f6bcfd97 35 (void)Create(width, height, mask, initialCount);
e1e955e1 36}
c801d85f 37
3cd94a0d 38wxGenericImageList::~wxGenericImageList()
c801d85f 39{
3fc93ebd 40 (void)RemoveAll();
e1e955e1 41}
c801d85f 42
3cd94a0d 43int wxGenericImageList::GetImageCount() const
c801d85f 44{
b1d4dd7a 45 return m_images.GetCount();
e1e955e1 46}
c801d85f 47
3cd94a0d 48bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
f6bcfd97
BP
49{
50 m_width = width;
51 m_height = height;
52
53 return Create();
54}
55
3cd94a0d 56bool wxGenericImageList::Create()
c801d85f 57{
ca65c044 58 return true;
e1e955e1 59}
c801d85f 60
3cd94a0d 61int wxGenericImageList::Add( const wxBitmap &bitmap )
c801d85f 62{
5b9003ae 63 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
b931414d 64 || (m_width == 0 && m_height == 0),
9a83f860
VZ
65 wxT("invalid bitmap size in wxImageList: this might work ")
66 wxT("on this platform but definitely won't under Windows.") );
9bd41907 67
0e1381f2
PC
68 const int index = int(m_images.GetCount());
69
80a46597 70 if (bitmap.IsKindOf(wxCLASSINFO(wxIcon)))
5b9003ae 71 {
f60d0f94 72 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
5b9003ae 73 }
f60d0f94 74 else
5b9003ae 75 {
4c51a665 76 // Mimic behaviour of Windows ImageList_Add that automatically breaks up the added
5b9003ae
RR
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 }
b931414d
RD
93
94 if (m_width == 0 && m_height == 0)
95 {
96 m_width = bitmap.GetWidth();
97 m_height = bitmap.GetHeight();
98 }
93763ad5 99
0e1381f2 100 return index;
e1e955e1 101}
c801d85f 102
3cd94a0d 103int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
4d449473
VS
104{
105 wxBitmap bmp(bitmap);
a1b806b9 106 if (mask.IsOk())
a7f1fbf6 107 bmp.SetMask(new wxMask(mask));
4d449473
VS
108 return Add(bmp);
109}
110
3cd94a0d 111int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
4d449473 112{
368d59f0 113 wxImage img = bitmap.ConvertToImage();
4d449473 114 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
368d59f0 115 return Add(wxBitmap(img));
4d449473
VS
116}
117
49bf4e3e 118const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
b46e8696 119{
a1abd1a9 120 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 121
d3b9f782 122 wxCHECK_MSG( node, NULL, wxT("wrong index in image list") );
43543d98 123
b1d4dd7a 124 return (wxBitmap*)node->GetData();
40413a5b 125}
43543d98 126
49bf4e3e 127// Get the bitmap
f45fac95 128wxBitmap wxGenericImageList::GetBitmap(int index) const
49bf4e3e
JS
129{
130 const wxBitmap* bmp = GetBitmapPtr(index);
131 if (bmp)
132 return *bmp;
133 else
134 return wxNullBitmap;
135}
136
137// Get the icon
f45fac95 138wxIcon wxGenericImageList::GetIcon(int index) const
49bf4e3e
JS
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
3cd94a0d 151bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
c801d85f 152{
a1abd1a9 153 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 154
ca65c044 155 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
f60d0f94 156
80a46597 157 wxBitmap* newBitmap = (bitmap.IsKindOf(wxCLASSINFO(wxIcon))) ?
999836aa
VZ
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) ;
f60d0f94 166
b1d4dd7a 167 if (index == (int) m_images.GetCount() - 1)
b46e8696 168 {
222ed1d6
MB
169 delete node->GetData();
170 m_images.Erase( node );
f60d0f94 171 m_images.Append( newBitmap );
b46e8696
RR
172 }
173 else
174 {
a1abd1a9 175 wxObjectList::compatibility_iterator next = node->GetNext();
222ed1d6
MB
176 delete node->GetData();
177 m_images.Erase( node );
f60d0f94 178 m_images.Insert( next, newBitmap );
b46e8696 179 }
43543d98 180
ca65c044 181 return true;
e1e955e1 182}
c801d85f 183
f644bc11
RR
184bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap, const wxBitmap &mask )
185{
a1abd1a9 186 wxObjectList::compatibility_iterator node = m_images.Item( index );
f644bc11
RR
187
188 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
189
80a46597 190 wxBitmap* newBitmap = (bitmap.IsKindOf(wxCLASSINFO(wxIcon))) ?
f644bc11
RR
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 {
a1abd1a9 208 wxObjectList::compatibility_iterator next = node->GetNext();
f644bc11
RR
209 delete node->GetData();
210 m_images.Erase( node );
211 m_images.Insert( next, newBitmap );
212 }
da80ae71 213
a1b806b9 214 if (mask.IsOk())
f644bc11
RR
215 newBitmap->SetMask(new wxMask(mask));
216
217 return true;
218}
219
3cd94a0d 220bool wxGenericImageList::Remove( int index )
c801d85f 221{
a1abd1a9 222 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 223
ca65c044 224 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 225
222ed1d6
MB
226 delete node->GetData();
227 m_images.Erase( node );
43543d98 228
ca65c044 229 return true;
e1e955e1 230}
c801d85f 231
3cd94a0d 232bool wxGenericImageList::RemoveAll()
c801d85f 233{
a1abd1a9 234 WX_CLEAR_LIST(wxObjectList, m_images);
b46e8696 235 m_images.Clear();
43543d98 236
ca65c044 237 return true;
e1e955e1 238}
c801d85f 239
3cd94a0d 240bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
c801d85f 241{
b46e8696
RR
242 width = 0;
243 height = 0;
43543d98 244
a1abd1a9 245 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 246
ca65c044 247 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 248
b1d4dd7a 249 wxBitmap *bm = (wxBitmap*)node->GetData();
c801d85f
KB
250 width = bm->GetWidth();
251 height = bm->GetHeight();
43543d98 252
ca65c044 253 return true;
e1e955e1 254}
c801d85f 255
3cd94a0d 256bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
219f895a 257 int flags, bool WXUNUSED(solidBackground) )
c801d85f 258{
a1abd1a9 259 wxObjectList::compatibility_iterator node = m_images.Item( index );
43543d98 260
ca65c044 261 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 262
b1d4dd7a 263 wxBitmap *bm = (wxBitmap*)node->GetData();
f60d0f94 264
80a46597 265 if (bm->IsKindOf(wxCLASSINFO(wxIcon)))
f60d0f94
JS
266 dc.DrawIcon( * ((wxIcon*) bm), x, y);
267 else
268 dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
219f895a 269
ca65c044 270 return true;
e1e955e1 271}
c801d85f 272
da80ae71 273#endif // wxUSE_IMAGLIST