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