]> git.saurik.com Git - wxWidgets.git/blame - src/generic/imaglist.cpp
several g++ 4 warning fixes
[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
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),
9bd41907
VZ
68 _T("invalid bitmap size in wxImageList: this might work ")
69 _T("on this platform but definitely won't under Windows.") );
70
f60d0f94 71 if (bitmap.IsKindOf(CLASSINFO(wxIcon)))
5b9003ae 72 {
f60d0f94 73 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
5b9003ae 74 }
f60d0f94 75 else
5b9003ae
RR
76 {
77 // Mimic behavior of Windows ImageList_Add that automatically breaks up the added
78 // bitmap into sub-images of the correct size
79 if (m_width > 0 && bitmap.GetWidth() > m_width && bitmap.GetHeight() >= m_height)
80 {
81 int numImages = bitmap.GetWidth() / m_width;
82 for (int subIndex = 0; subIndex < numImages; subIndex++)
83 {
84 wxRect rect(m_width * subIndex, 0, m_width, m_height);
85 wxBitmap tmpBmp = bitmap.GetSubBitmap(rect);
86 m_images.Append( new wxBitmap(tmpBmp) );
87 }
88 }
89 else
90 {
91 m_images.Append( new wxBitmap(bitmap) );
92 }
93 }
b931414d
RD
94
95 if (m_width == 0 && m_height == 0)
96 {
97 m_width = bitmap.GetWidth();
98 m_height = bitmap.GetHeight();
99 }
93763ad5 100
b1d4dd7a 101 return m_images.GetCount()-1;
e1e955e1 102}
c801d85f 103
3cd94a0d 104int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
4d449473
VS
105{
106 wxBitmap bmp(bitmap);
a7f1fbf6
RD
107 if (mask.Ok())
108 bmp.SetMask(new wxMask(mask));
4d449473
VS
109 return Add(bmp);
110}
111
3cd94a0d 112int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
4d449473 113{
368d59f0 114 wxImage img = bitmap.ConvertToImage();
4d449473 115 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
368d59f0 116 return Add(wxBitmap(img));
4d449473
VS
117}
118
49bf4e3e 119const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
b46e8696 120{
222ed1d6 121 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 122
223d09f6 123 wxCHECK_MSG( node, (wxBitmap *) NULL, wxT("wrong index in image list") );
43543d98 124
b1d4dd7a 125 return (wxBitmap*)node->GetData();
40413a5b 126}
43543d98 127
49bf4e3e 128// Get the bitmap
f45fac95 129wxBitmap wxGenericImageList::GetBitmap(int index) const
49bf4e3e
JS
130{
131 const wxBitmap* bmp = GetBitmapPtr(index);
132 if (bmp)
133 return *bmp;
134 else
135 return wxNullBitmap;
136}
137
138// Get the icon
f45fac95 139wxIcon wxGenericImageList::GetIcon(int index) const
49bf4e3e
JS
140{
141 const wxBitmap* bmp = GetBitmapPtr(index);
142 if (bmp)
143 {
144 wxIcon icon;
145 icon.CopyFromBitmap(*bmp);
146 return icon;
147 }
148 else
149 return wxNullIcon;
150}
151
3cd94a0d 152bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
c801d85f 153{
222ed1d6 154 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 155
ca65c044 156 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
f60d0f94 157
999836aa
VZ
158 wxBitmap* newBitmap = (bitmap.IsKindOf(CLASSINFO(wxIcon))) ?
159 #if defined(__VISAGECPP__)
160 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
161 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
162 new wxBitmap(bitmap)
163 #else
164 new wxBitmap( (const wxIcon&) bitmap )
165 #endif
166 : new wxBitmap(bitmap) ;
f60d0f94 167
b1d4dd7a 168 if (index == (int) m_images.GetCount() - 1)
b46e8696 169 {
222ed1d6
MB
170 delete node->GetData();
171 m_images.Erase( node );
f60d0f94 172 m_images.Append( newBitmap );
b46e8696
RR
173 }
174 else
175 {
222ed1d6
MB
176 wxList::compatibility_iterator next = node->GetNext();
177 delete node->GetData();
178 m_images.Erase( node );
f60d0f94 179 m_images.Insert( next, newBitmap );
b46e8696 180 }
43543d98 181
ca65c044 182 return true;
e1e955e1 183}
c801d85f 184
f644bc11
RR
185bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap, const wxBitmap &mask )
186{
187 wxList::compatibility_iterator node = m_images.Item( index );
188
189 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
190
191 wxBitmap* newBitmap = (bitmap.IsKindOf(CLASSINFO(wxIcon))) ?
192 #if defined(__VISAGECPP__)
193 //just can't do this in VisualAge now, with all this new Bitmap-Icon stuff
194 //so construct it from a bitmap object until I can figure this nonsense out. (DW)
195 new wxBitmap(bitmap)
196 #else
197 new wxBitmap( (const wxIcon&) bitmap )
198 #endif
199 : new wxBitmap(bitmap) ;
200
201 if (index == (int) m_images.GetCount() - 1)
202 {
203 delete node->GetData();
204 m_images.Erase( node );
205 m_images.Append( newBitmap );
206 }
207 else
208 {
209 wxList::compatibility_iterator next = node->GetNext();
210 delete node->GetData();
211 m_images.Erase( node );
212 m_images.Insert( next, newBitmap );
213 }
da80ae71 214
f644bc11
RR
215 if (mask.Ok())
216 newBitmap->SetMask(new wxMask(mask));
217
218 return true;
219}
220
3cd94a0d 221bool wxGenericImageList::Remove( int index )
c801d85f 222{
222ed1d6 223 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 224
ca65c044 225 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 226
222ed1d6
MB
227 delete node->GetData();
228 m_images.Erase( node );
43543d98 229
ca65c044 230 return true;
e1e955e1 231}
c801d85f 232
3cd94a0d 233bool wxGenericImageList::RemoveAll()
c801d85f 234{
222ed1d6 235 WX_CLEAR_LIST(wxList, m_images);
b46e8696 236 m_images.Clear();
43543d98 237
ca65c044 238 return true;
e1e955e1 239}
c801d85f 240
3cd94a0d 241bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
c801d85f 242{
b46e8696
RR
243 width = 0;
244 height = 0;
43543d98 245
222ed1d6 246 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 247
ca65c044 248 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 249
b1d4dd7a 250 wxBitmap *bm = (wxBitmap*)node->GetData();
c801d85f
KB
251 width = bm->GetWidth();
252 height = bm->GetHeight();
43543d98 253
ca65c044 254 return true;
e1e955e1 255}
c801d85f 256
3cd94a0d 257bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
219f895a 258 int flags, bool WXUNUSED(solidBackground) )
c801d85f 259{
222ed1d6 260 wxList::compatibility_iterator node = m_images.Item( index );
43543d98 261
ca65c044 262 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
43543d98 263
b1d4dd7a 264 wxBitmap *bm = (wxBitmap*)node->GetData();
f60d0f94
JS
265
266 if (bm->IsKindOf(CLASSINFO(wxIcon)))
267 dc.DrawIcon( * ((wxIcon*) bm), x, y);
268 else
269 dc.DrawBitmap( *bm, x, y, (flags & wxIMAGELIST_DRAW_TRANSPARENT) > 0 );
219f895a 270
ca65c044 271 return true;
e1e955e1 272}
c801d85f 273
4055ed82 274#endif // __WXPALMOS__
da80ae71
WS
275
276#endif // wxUSE_IMAGLIST