]> git.saurik.com Git - wxWidgets.git/blob - src/generic/imaglist.cpp
prevent memory leak
[wxWidgets.git] / src / generic / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/imaglist.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_IMAGLIST && !defined(wxHAS_NATIVE_IMAGELIST)
18
19 #ifndef __WXPALMOS__
20
21 #include "wx/imaglist.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/dc.h"
25 #include "wx/icon.h"
26 #include "wx/image.h"
27 #endif
28
29 //-----------------------------------------------------------------------------
30 // wxImageList
31 //-----------------------------------------------------------------------------
32
33 IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject)
34 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList)
35
36 wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount )
37 {
38 (void)Create(width, height, mask, initialCount);
39 }
40
41 wxGenericImageList::~wxGenericImageList()
42 {
43 (void)RemoveAll();
44 }
45
46 int wxGenericImageList::GetImageCount() const
47 {
48 return m_images.GetCount();
49 }
50
51 bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) )
52 {
53 m_width = width;
54 m_height = height;
55
56 return Create();
57 }
58
59 bool wxGenericImageList::Create()
60 {
61 return true;
62 }
63
64 int wxGenericImageList::Add( const wxBitmap &bitmap )
65 {
66 wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height)
67 || (m_width == 0 && m_height == 0),
68 _T("invalid bitmap size in wxImageList: this might work ")
69 _T("on this platform but definitely won't under Windows.") );
70
71 if (bitmap.IsKindOf(CLASSINFO(wxIcon)))
72 {
73 m_images.Append( new wxIcon( (const wxIcon&) bitmap ) );
74 }
75 else
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 }
94
95 if (m_width == 0 && m_height == 0)
96 {
97 m_width = bitmap.GetWidth();
98 m_height = bitmap.GetHeight();
99 }
100
101 return m_images.GetCount()-1;
102 }
103
104 int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask )
105 {
106 wxBitmap bmp(bitmap);
107 if (mask.Ok())
108 bmp.SetMask(new wxMask(mask));
109 return Add(bmp);
110 }
111
112 int wxGenericImageList::Add( const wxBitmap& bitmap, const wxColour& maskColour )
113 {
114 wxImage img = bitmap.ConvertToImage();
115 img.SetMaskColour(maskColour.Red(), maskColour.Green(), maskColour.Blue());
116 return Add(wxBitmap(img));
117 }
118
119 const wxBitmap *wxGenericImageList::GetBitmapPtr( int index ) const
120 {
121 wxList::compatibility_iterator node = m_images.Item( index );
122
123 wxCHECK_MSG( node, (wxBitmap *) NULL, wxT("wrong index in image list") );
124
125 return (wxBitmap*)node->GetData();
126 }
127
128 // Get the bitmap
129 wxBitmap wxGenericImageList::GetBitmap(int index) const
130 {
131 const wxBitmap* bmp = GetBitmapPtr(index);
132 if (bmp)
133 return *bmp;
134 else
135 return wxNullBitmap;
136 }
137
138 // Get the icon
139 wxIcon wxGenericImageList::GetIcon(int index) const
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
152 bool wxGenericImageList::Replace( int index, const wxBitmap &bitmap )
153 {
154 wxList::compatibility_iterator node = m_images.Item( index );
155
156 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
157
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) ;
167
168 if (index == (int) m_images.GetCount() - 1)
169 {
170 delete node->GetData();
171 m_images.Erase( node );
172 m_images.Append( newBitmap );
173 }
174 else
175 {
176 wxList::compatibility_iterator next = node->GetNext();
177 delete node->GetData();
178 m_images.Erase( node );
179 m_images.Insert( next, newBitmap );
180 }
181
182 return true;
183 }
184
185 bool 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 }
214
215 if (mask.Ok())
216 newBitmap->SetMask(new wxMask(mask));
217
218 return true;
219 }
220
221 bool wxGenericImageList::Remove( int index )
222 {
223 wxList::compatibility_iterator node = m_images.Item( index );
224
225 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
226
227 delete node->GetData();
228 m_images.Erase( node );
229
230 return true;
231 }
232
233 bool wxGenericImageList::RemoveAll()
234 {
235 WX_CLEAR_LIST(wxList, m_images);
236 m_images.Clear();
237
238 return true;
239 }
240
241 bool wxGenericImageList::GetSize( int index, int &width, int &height ) const
242 {
243 width = 0;
244 height = 0;
245
246 wxList::compatibility_iterator node = m_images.Item( index );
247
248 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
249
250 wxBitmap *bm = (wxBitmap*)node->GetData();
251 width = bm->GetWidth();
252 height = bm->GetHeight();
253
254 return true;
255 }
256
257 bool wxGenericImageList::Draw( int index, wxDC &dc, int x, int y,
258 int flags, bool WXUNUSED(solidBackground) )
259 {
260 wxList::compatibility_iterator node = m_images.Item( index );
261
262 wxCHECK_MSG( node, false, wxT("wrong index in image list") );
263
264 wxBitmap *bm = (wxBitmap*)node->GetData();
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 );
270
271 return true;
272 }
273
274 #endif // __WXPALMOS__
275
276 #endif // wxUSE_IMAGLIST