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