]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
4b7f2165 VZ |
2 | // Name: src/msw/imaglist.cpp |
3 | // Purpose: wxImageList implementation for Win32 | |
2bda0e17 KB |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
4b7f2165 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4b7f2165 | 21 | #pragma implementation "imaglist.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
4b7f2165 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #if defined(__WIN95__) | |
32 | ||
33 | #ifndef WX_PRECOMP | |
4b7f2165 VZ |
34 | #include "wx/window.h" |
35 | #include "wx/icon.h" | |
36 | #include "wx/dc.h" | |
37 | #include "wx/string.h" | |
e0f15f4a | 38 | #include "wx/dcmemory.h" |
4b7f2165 VZ |
39 | |
40 | #include <stdio.h> | |
2bda0e17 KB |
41 | #endif |
42 | ||
ce3ed50d JS |
43 | #include "wx/log.h" |
44 | #include "wx/intl.h" | |
45 | ||
2bda0e17 KB |
46 | #include "wx/msw/imaglist.h" |
47 | #include "wx/msw/private.h" | |
48 | ||
b39dbf34 | 49 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) |
4b7f2165 | 50 | #include <commctrl.h> |
2bda0e17 KB |
51 | #endif |
52 | ||
4b7f2165 VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // wxWin macros | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
2bda0e17 | 57 | IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject) |
2bda0e17 | 58 | |
4b7f2165 VZ |
59 | #define GetHImageList() ((HIMAGELIST)m_hImageList) |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // private functions | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | // returns the mask if it's valid, otherwise the bitmap mask and, if it's not | |
66 | // valid neither, a "solid" mask (no transparent zones at all) | |
3ca6a5f0 | 67 | static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask); |
4b7f2165 VZ |
68 | |
69 | // ============================================================================ | |
70 | // implementation | |
71 | // ============================================================================ | |
72 | ||
73 | // ---------------------------------------------------------------------------- | |
74 | // wxImageList creation/destruction | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
77 | wxImageList::wxImageList() | |
2bda0e17 KB |
78 | { |
79 | m_hImageList = 0; | |
80 | } | |
81 | ||
4b7f2165 VZ |
82 | // Creates an image list |
83 | bool wxImageList::Create(int width, int height, bool mask, int initial) | |
2bda0e17 | 84 | { |
a6e2b3a8 VZ |
85 | UINT flags = 0; |
86 | ||
87 | // set appropriate color depth | |
4676948b JS |
88 | #ifdef __WXWINCE__ |
89 | flags |= ILC_COLOR; | |
90 | #else | |
665b71b1 WS |
91 | int dd = wxDisplayDepth(); |
92 | ||
a6e2b3a8 VZ |
93 | if (dd <= 4) flags |= ILC_COLOR; // 16 color |
94 | else if (dd <= 8) flags |= ILC_COLOR8; // 256 color | |
95 | else if (dd <= 16) flags |= ILC_COLOR16; // 64k hi-color | |
96 | else if (dd <= 24) flags |= ILC_COLOR24; // 16m truecolor | |
97 | else if (dd <= 32) flags |= ILC_COLOR32; // 16m truecolor | |
4676948b | 98 | #endif |
a6e2b3a8 | 99 | |
4b7f2165 VZ |
100 | if ( mask ) |
101 | flags |= ILC_MASK; | |
102 | ||
103 | // Grow by 1, I guess this is reasonable behaviour most of the time | |
104 | m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags, | |
105 | initial, 1); | |
106 | if ( !m_hImageList ) | |
107 | { | |
f6bcfd97 | 108 | wxLogLastError(wxT("ImageList_Create()")); |
4b7f2165 | 109 | } |
2bda0e17 | 110 | |
4b7f2165 VZ |
111 | return m_hImageList != 0; |
112 | } | |
2bda0e17 | 113 | |
4b7f2165 | 114 | wxImageList::~wxImageList() |
2bda0e17 | 115 | { |
4b7f2165 VZ |
116 | if ( m_hImageList ) |
117 | { | |
118 | ImageList_Destroy(GetHImageList()); | |
119 | m_hImageList = 0; | |
120 | } | |
2bda0e17 KB |
121 | } |
122 | ||
4b7f2165 VZ |
123 | // ---------------------------------------------------------------------------- |
124 | // wxImageList attributes | |
125 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 126 | |
4b7f2165 VZ |
127 | // Returns the number of images in the image list. |
128 | int wxImageList::GetImageCount() const | |
2bda0e17 | 129 | { |
4b7f2165 | 130 | wxASSERT_MSG( m_hImageList, _T("invalid image list") ); |
2bda0e17 | 131 | |
4b7f2165 | 132 | return ImageList_GetImageCount(GetHImageList()); |
2bda0e17 KB |
133 | } |
134 | ||
f6bcfd97 BP |
135 | // Returns the size (same for all images) of the images in the list |
136 | bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const | |
137 | { | |
138 | wxASSERT_MSG( m_hImageList, _T("invalid image list") ); | |
139 | ||
140 | return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0; | |
141 | } | |
142 | ||
4b7f2165 VZ |
143 | // ---------------------------------------------------------------------------- |
144 | // wxImageList operations | |
145 | // ---------------------------------------------------------------------------- | |
146 | ||
2bda0e17 KB |
147 | // Adds a bitmap, and optionally a mask bitmap. |
148 | // Note that wxImageList creates new bitmaps, so you may delete | |
149 | // 'bitmap' and 'mask'. | |
150 | int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) | |
151 | { | |
3ca6a5f0 | 152 | HBITMAP hbmpMask = GetMaskForImage(bitmap, mask); |
e29b83a4 | 153 | |
4b7f2165 VZ |
154 | int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask); |
155 | if ( index == -1 ) | |
e29b83a4 VZ |
156 | { |
157 | wxLogError(_("Couldn't add an image to the image list.")); | |
158 | } | |
159 | ||
4b7f2165 | 160 | ::DeleteObject(hbmpMask); |
54b37e2e | 161 | |
e29b83a4 | 162 | return index; |
2bda0e17 KB |
163 | } |
164 | ||
165 | // Adds a bitmap, using the specified colour to create the mask bitmap | |
166 | // Note that wxImageList creates new bitmaps, so you may delete | |
167 | // 'bitmap'. | |
168 | int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) | |
169 | { | |
4b7f2165 VZ |
170 | int index = ImageList_AddMasked(GetHImageList(), |
171 | GetHbitmapOf(bitmap), | |
172 | wxColourToRGB(maskColour)); | |
173 | if ( index == -1 ) | |
174 | { | |
175 | wxLogError(_("Couldn't add an image to the image list.")); | |
176 | } | |
177 | ||
178 | return index; | |
2bda0e17 KB |
179 | } |
180 | ||
181 | // Adds a bitmap and mask from an icon. | |
182 | int wxImageList::Add(const wxIcon& icon) | |
183 | { | |
4b7f2165 VZ |
184 | int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon)); |
185 | if ( index == -1 ) | |
186 | { | |
187 | wxLogError(_("Couldn't add an image to the image list.")); | |
188 | } | |
189 | ||
190 | return index; | |
2bda0e17 KB |
191 | } |
192 | ||
193 | // Replaces a bitmap, optionally passing a mask bitmap. | |
194 | // Note that wxImageList creates new bitmaps, so you may delete | |
195 | // 'bitmap' and 'mask'. | |
4b7f2165 VZ |
196 | bool wxImageList::Replace(int index, |
197 | const wxBitmap& bitmap, const wxBitmap& mask) | |
2bda0e17 | 198 | { |
3ca6a5f0 | 199 | HBITMAP hbmpMask = GetMaskForImage(bitmap, mask); |
2bda0e17 | 200 | |
4b7f2165 VZ |
201 | bool ok = ImageList_Replace(GetHImageList(), index, |
202 | GetHbitmapOf(bitmap), hbmpMask) != 0; | |
203 | if ( !ok ) | |
204 | { | |
f6bcfd97 | 205 | wxLogLastError(wxT("ImageList_Add()")); |
4b7f2165 VZ |
206 | } |
207 | ||
208 | ::DeleteObject(hbmpMask); | |
209 | ||
210 | return ok; | |
2bda0e17 | 211 | } |
2bda0e17 KB |
212 | |
213 | // Replaces a bitmap and mask from an icon. | |
4b7f2165 | 214 | bool wxImageList::Replace(int i, const wxIcon& icon) |
2bda0e17 | 215 | { |
4b7f2165 VZ |
216 | bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0; |
217 | if ( !ok ) | |
218 | { | |
f6bcfd97 | 219 | wxLogLastError(wxT("ImageList_ReplaceIcon()")); |
4b7f2165 VZ |
220 | } |
221 | ||
222 | return ok; | |
2bda0e17 KB |
223 | } |
224 | ||
225 | // Removes the image at the given index. | |
debe6624 | 226 | bool wxImageList::Remove(int index) |
2bda0e17 | 227 | { |
4b7f2165 VZ |
228 | bool ok = ImageList_Remove(GetHImageList(), index) != 0; |
229 | if ( !ok ) | |
230 | { | |
f6bcfd97 | 231 | wxLogLastError(wxT("ImageList_Remove()")); |
4b7f2165 VZ |
232 | } |
233 | ||
234 | return ok; | |
2bda0e17 KB |
235 | } |
236 | ||
237 | // Remove all images | |
4b7f2165 | 238 | bool wxImageList::RemoveAll() |
2bda0e17 | 239 | { |
3c1a88d8 VZ |
240 | // don't use ImageList_RemoveAll() because mingw32 headers don't have it |
241 | int count = ImageList_GetImageCount(GetHImageList()); | |
242 | for ( int i = 0; i < count; i++ ) | |
4b7f2165 | 243 | { |
3c1a88d8 VZ |
244 | // the image indexes are shifted, so we should always remove the first |
245 | // one | |
246 | (void)Remove(0); | |
4b7f2165 VZ |
247 | } |
248 | ||
3c1a88d8 | 249 | return TRUE; |
2bda0e17 KB |
250 | } |
251 | ||
252 | // Draws the given image on a dc at the specified position. | |
253 | // If 'solidBackground' is TRUE, Draw sets the image list background | |
254 | // colour to the background colour of the wxDC, to speed up | |
255 | // drawing by eliminating masked drawing where possible. | |
4b7f2165 VZ |
256 | bool wxImageList::Draw(int index, |
257 | wxDC& dc, | |
258 | int x, int y, | |
259 | int flags, | |
260 | bool solidBackground) | |
2bda0e17 | 261 | { |
4b7f2165 VZ |
262 | HDC hDC = GetHdcOf(dc); |
263 | wxCHECK_MSG( hDC, FALSE, _T("invalid wxDC in wxImageList::Draw") ); | |
264 | ||
265 | COLORREF clr = CLR_NONE; // transparent by default | |
266 | if ( solidBackground ) | |
267 | { | |
b33576e1 VZ |
268 | const wxBrush& brush = dc.GetBackground(); |
269 | if ( brush.Ok() ) | |
4b7f2165 | 270 | { |
b33576e1 | 271 | clr = wxColourToRGB(brush.GetColour()); |
4b7f2165 VZ |
272 | } |
273 | } | |
274 | ||
275 | ImageList_SetBkColor(GetHImageList(), clr); | |
2bda0e17 KB |
276 | |
277 | UINT style = 0; | |
4b7f2165 VZ |
278 | if ( flags & wxIMAGELIST_DRAW_NORMAL ) |
279 | style |= ILD_NORMAL; | |
280 | if ( flags & wxIMAGELIST_DRAW_TRANSPARENT ) | |
281 | style |= ILD_TRANSPARENT; | |
282 | if ( flags & wxIMAGELIST_DRAW_SELECTED ) | |
283 | style |= ILD_SELECTED; | |
284 | if ( flags & wxIMAGELIST_DRAW_FOCUSED ) | |
285 | style |= ILD_FOCUS; | |
286 | ||
287 | bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0; | |
288 | if ( !ok ) | |
289 | { | |
f6bcfd97 | 290 | wxLogLastError(wxT("ImageList_Draw()")); |
4b7f2165 VZ |
291 | } |
292 | ||
293 | return ok; | |
2bda0e17 KB |
294 | } |
295 | ||
4b7f2165 VZ |
296 | // ---------------------------------------------------------------------------- |
297 | // helpers | |
298 | // ---------------------------------------------------------------------------- | |
299 | ||
3ca6a5f0 | 300 | static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask) |
4b7f2165 | 301 | { |
3ca6a5f0 | 302 | HBITMAP hbmpMask; |
90c1530a VZ |
303 | wxMask *pMask; |
304 | bool deleteMask = FALSE; | |
4b7f2165 VZ |
305 | |
306 | if ( mask.Ok() ) | |
307 | { | |
3ca6a5f0 | 308 | hbmpMask = GetHbitmapOf(mask); |
90c1530a | 309 | pMask = NULL; |
4b7f2165 VZ |
310 | } |
311 | else | |
312 | { | |
90c1530a VZ |
313 | pMask = bitmap.GetMask(); |
314 | if ( !pMask ) | |
4b7f2165 | 315 | { |
90c1530a VZ |
316 | // use the light grey count as transparent: the trouble here is |
317 | // that the light grey might have been changed by Windows behind | |
318 | // our back, so use the standard colour map to get its real value | |
319 | wxCOLORMAP *cmap = wxGetStdColourMap(); | |
320 | wxColour col; | |
321 | wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from); | |
4b7f2165 | 322 | |
90c1530a | 323 | pMask = new wxMask(bitmap, col); |
3ca6a5f0 | 324 | |
90c1530a | 325 | deleteMask = TRUE; |
3ca6a5f0 | 326 | } |
90c1530a VZ |
327 | |
328 | hbmpMask = (HBITMAP)pMask->GetMaskBitmap(); | |
4b7f2165 VZ |
329 | } |
330 | ||
77ffb593 | 331 | // windows mask convention is opposite to the wxWidgets one |
3ca6a5f0 | 332 | HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask); |
90c1530a VZ |
333 | |
334 | if ( deleteMask ) | |
335 | { | |
336 | delete pMask; | |
337 | } | |
3ca6a5f0 BP |
338 | |
339 | return hbmpMaskInv; | |
4b7f2165 VZ |
340 | } |
341 | ||
342 | #endif // Win95 | |
2bda0e17 | 343 |