]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/imaglist.h" | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/dc.h" | |
23 | #include "wx/icon.h" | |
24 | #include "wx/image.h" | |
25 | #endif | |
26 | ||
27 | //----------------------------------------------------------------------------- | |
28 | // wxImageList | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | IMPLEMENT_DYNAMIC_CLASS(wxGenericImageList, wxObject) | |
32 | IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxGenericImageList) | |
33 | ||
34 | wxGenericImageList::wxGenericImageList( int width, int height, bool mask, int initialCount ) | |
35 | { | |
36 | (void)Create(width, height, mask, initialCount); | |
37 | } | |
38 | ||
39 | wxGenericImageList::~wxGenericImageList() | |
40 | { | |
41 | (void)RemoveAll(); | |
42 | } | |
43 | ||
44 | int wxGenericImageList::GetImageCount() const | |
45 | { | |
46 | return m_images.GetCount(); | |
47 | } | |
48 | ||
49 | bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) ) | |
50 | { | |
51 | m_width = width; | |
52 | m_height = height; | |
53 | ||
54 | return Create(); | |
55 | } | |
56 | ||
57 | bool wxGenericImageList::Create() | |
58 | { | |
59 | return true; | |
60 | } | |
61 | ||
62 | int wxGenericImageList::Add( const wxBitmap &bitmap ) | |
63 | { | |
64 | wxASSERT_MSG( (bitmap.GetWidth() >= m_width && bitmap.GetHeight() == m_height) | |
65 | || (m_width == 0 && m_height == 0), | |
66 | wxT("invalid bitmap size in wxImageList: this might work ") | |
67 | wxT("on this platform but definitely won't under Windows.") ); | |
68 | ||
69 | const int index = int(m_images.GetCount()); | |
70 | ||
71 | if (bitmap.IsKindOf(wxCLASSINFO(wxIcon))) | |
72 | { | |
73 | m_images.Append( new wxIcon( (const wxIcon&) bitmap ) ); | |
74 | } | |
75 | else | |
76 | { | |
77 | // Mimic behaviour 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 index; | |
102 | } | |
103 | ||
104 | int wxGenericImageList::Add( const wxBitmap& bitmap, const wxBitmap& mask ) | |
105 | { | |
106 | wxBitmap bmp(bitmap); | |
107 | if (mask.IsOk()) | |
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 | wxObjectList::compatibility_iterator node = m_images.Item( index ); | |
122 | ||
123 | wxCHECK_MSG( node, 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 | wxObjectList::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(wxCLASSINFO(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 | wxObjectList::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 | wxObjectList::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(wxCLASSINFO(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 | wxObjectList::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.IsOk()) | |
216 | newBitmap->SetMask(new wxMask(mask)); | |
217 | ||
218 | return true; | |
219 | } | |
220 | ||
221 | bool wxGenericImageList::Remove( int index ) | |
222 | { | |
223 | wxObjectList::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(wxObjectList, 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 | wxObjectList::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 | wxObjectList::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(wxCLASSINFO(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 // wxUSE_IMAGLIST |