]>
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 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
4b7f2165 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
2bda0e17 | 27 | #ifndef WX_PRECOMP |
57bd4c60 | 28 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
4b7f2165 VZ |
29 | #include "wx/window.h" |
30 | #include "wx/icon.h" | |
31 | #include "wx/dc.h" | |
32 | #include "wx/string.h" | |
e0f15f4a | 33 | #include "wx/dcmemory.h" |
88a7a4e1 | 34 | #include "wx/intl.h" |
e4db172a | 35 | #include "wx/log.h" |
155ecd4c | 36 | #include "wx/image.h" |
4b7f2165 | 37 | #include <stdio.h> |
2bda0e17 KB |
38 | #endif |
39 | ||
01f512d7 | 40 | #include "wx/imaglist.h" |
2bda0e17 KB |
41 | #include "wx/msw/private.h" |
42 | ||
4b7f2165 VZ |
43 | // ---------------------------------------------------------------------------- |
44 | // wxWin macros | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
2bda0e17 | 47 | IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject) |
2bda0e17 | 48 | |
4b7f2165 VZ |
49 | #define GetHImageList() ((HIMAGELIST)m_hImageList) |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // private functions | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | // returns the mask if it's valid, otherwise the bitmap mask and, if it's not | |
56 | // valid neither, a "solid" mask (no transparent zones at all) | |
3ca6a5f0 | 57 | static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask); |
4b7f2165 VZ |
58 | |
59 | // ============================================================================ | |
60 | // implementation | |
61 | // ============================================================================ | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // wxImageList creation/destruction | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | wxImageList::wxImageList() | |
2bda0e17 KB |
68 | { |
69 | m_hImageList = 0; | |
70 | } | |
71 | ||
4b7f2165 VZ |
72 | // Creates an image list |
73 | bool wxImageList::Create(int width, int height, bool mask, int initial) | |
2bda0e17 | 74 | { |
a6e2b3a8 VZ |
75 | UINT flags = 0; |
76 | ||
77 | // set appropriate color depth | |
4676948b JS |
78 | #ifdef __WXWINCE__ |
79 | flags |= ILC_COLOR; | |
80 | #else | |
665b71b1 WS |
81 | int dd = wxDisplayDepth(); |
82 | ||
59af881e WS |
83 | if (dd <= 4) flags |= ILC_COLOR; // 16 color |
84 | else if (dd <= 8) flags |= ILC_COLOR8; // 256 color | |
85 | else if (dd <= 16) flags |= ILC_COLOR16; // 64k hi-color | |
86 | else if (dd <= 24) flags |= ILC_COLOR24; // 16m truecolor | |
87 | else if (dd <= 32) flags |= ILC_COLOR32; // 16m truecolor | |
4676948b | 88 | #endif |
a6e2b3a8 | 89 | |
4b7f2165 VZ |
90 | if ( mask ) |
91 | flags |= ILC_MASK; | |
92 | ||
93 | // Grow by 1, I guess this is reasonable behaviour most of the time | |
94 | m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags, | |
95 | initial, 1); | |
96 | if ( !m_hImageList ) | |
97 | { | |
f6bcfd97 | 98 | wxLogLastError(wxT("ImageList_Create()")); |
4b7f2165 | 99 | } |
2bda0e17 | 100 | |
4b7f2165 VZ |
101 | return m_hImageList != 0; |
102 | } | |
2bda0e17 | 103 | |
4b7f2165 | 104 | wxImageList::~wxImageList() |
2bda0e17 | 105 | { |
4b7f2165 VZ |
106 | if ( m_hImageList ) |
107 | { | |
108 | ImageList_Destroy(GetHImageList()); | |
109 | m_hImageList = 0; | |
110 | } | |
2bda0e17 KB |
111 | } |
112 | ||
4b7f2165 VZ |
113 | // ---------------------------------------------------------------------------- |
114 | // wxImageList attributes | |
115 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 116 | |
4b7f2165 VZ |
117 | // Returns the number of images in the image list. |
118 | int wxImageList::GetImageCount() const | |
2bda0e17 | 119 | { |
4b7f2165 | 120 | wxASSERT_MSG( m_hImageList, _T("invalid image list") ); |
2bda0e17 | 121 | |
4b7f2165 | 122 | return ImageList_GetImageCount(GetHImageList()); |
2bda0e17 KB |
123 | } |
124 | ||
f6bcfd97 BP |
125 | // Returns the size (same for all images) of the images in the list |
126 | bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const | |
127 | { | |
128 | wxASSERT_MSG( m_hImageList, _T("invalid image list") ); | |
129 | ||
130 | return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0; | |
131 | } | |
132 | ||
4b7f2165 VZ |
133 | // ---------------------------------------------------------------------------- |
134 | // wxImageList operations | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
2bda0e17 KB |
137 | // Adds a bitmap, and optionally a mask bitmap. |
138 | // Note that wxImageList creates new bitmaps, so you may delete | |
139 | // 'bitmap' and 'mask'. | |
140 | int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) | |
141 | { | |
3ca6a5f0 | 142 | HBITMAP hbmpMask = GetMaskForImage(bitmap, mask); |
e29b83a4 | 143 | |
4b7f2165 VZ |
144 | int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask); |
145 | if ( index == -1 ) | |
e29b83a4 VZ |
146 | { |
147 | wxLogError(_("Couldn't add an image to the image list.")); | |
148 | } | |
149 | ||
4b7f2165 | 150 | ::DeleteObject(hbmpMask); |
54b37e2e | 151 | |
e29b83a4 | 152 | return index; |
2bda0e17 KB |
153 | } |
154 | ||
155 | // Adds a bitmap, using the specified colour to create the mask bitmap | |
156 | // Note that wxImageList creates new bitmaps, so you may delete | |
157 | // 'bitmap'. | |
158 | int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) | |
159 | { | |
4b7f2165 VZ |
160 | int index = ImageList_AddMasked(GetHImageList(), |
161 | GetHbitmapOf(bitmap), | |
162 | wxColourToRGB(maskColour)); | |
163 | if ( index == -1 ) | |
164 | { | |
165 | wxLogError(_("Couldn't add an image to the image list.")); | |
166 | } | |
167 | ||
168 | return index; | |
2bda0e17 KB |
169 | } |
170 | ||
171 | // Adds a bitmap and mask from an icon. | |
172 | int wxImageList::Add(const wxIcon& icon) | |
173 | { | |
4b7f2165 VZ |
174 | int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon)); |
175 | if ( index == -1 ) | |
176 | { | |
177 | wxLogError(_("Couldn't add an image to the image list.")); | |
178 | } | |
179 | ||
180 | return index; | |
2bda0e17 KB |
181 | } |
182 | ||
183 | // Replaces a bitmap, optionally passing a mask bitmap. | |
184 | // Note that wxImageList creates new bitmaps, so you may delete | |
185 | // 'bitmap' and 'mask'. | |
4b7f2165 VZ |
186 | bool wxImageList::Replace(int index, |
187 | const wxBitmap& bitmap, const wxBitmap& mask) | |
2bda0e17 | 188 | { |
3ca6a5f0 | 189 | HBITMAP hbmpMask = GetMaskForImage(bitmap, mask); |
2bda0e17 | 190 | |
4b7f2165 VZ |
191 | bool ok = ImageList_Replace(GetHImageList(), index, |
192 | GetHbitmapOf(bitmap), hbmpMask) != 0; | |
193 | if ( !ok ) | |
194 | { | |
3103e8a9 | 195 | wxLogLastError(wxT("ImageList_Replace()")); |
4b7f2165 VZ |
196 | } |
197 | ||
198 | ::DeleteObject(hbmpMask); | |
199 | ||
200 | return ok; | |
2bda0e17 | 201 | } |
2bda0e17 KB |
202 | |
203 | // Replaces a bitmap and mask from an icon. | |
4b7f2165 | 204 | bool wxImageList::Replace(int i, const wxIcon& icon) |
2bda0e17 | 205 | { |
9fe0c2fc | 206 | bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != -1; |
4b7f2165 VZ |
207 | if ( !ok ) |
208 | { | |
f6bcfd97 | 209 | wxLogLastError(wxT("ImageList_ReplaceIcon()")); |
4b7f2165 VZ |
210 | } |
211 | ||
212 | return ok; | |
2bda0e17 KB |
213 | } |
214 | ||
215 | // Removes the image at the given index. | |
debe6624 | 216 | bool wxImageList::Remove(int index) |
2bda0e17 | 217 | { |
4b7f2165 VZ |
218 | bool ok = ImageList_Remove(GetHImageList(), index) != 0; |
219 | if ( !ok ) | |
220 | { | |
f6bcfd97 | 221 | wxLogLastError(wxT("ImageList_Remove()")); |
4b7f2165 VZ |
222 | } |
223 | ||
224 | return ok; | |
2bda0e17 KB |
225 | } |
226 | ||
227 | // Remove all images | |
4b7f2165 | 228 | bool wxImageList::RemoveAll() |
2bda0e17 | 229 | { |
3c1a88d8 | 230 | // don't use ImageList_RemoveAll() because mingw32 headers don't have it |
1c304f30 | 231 | return Remove(-1); |
2bda0e17 KB |
232 | } |
233 | ||
234 | // Draws the given image on a dc at the specified position. | |
59af881e | 235 | // If 'solidBackground' is true, Draw sets the image list background |
2bda0e17 KB |
236 | // colour to the background colour of the wxDC, to speed up |
237 | // drawing by eliminating masked drawing where possible. | |
4b7f2165 VZ |
238 | bool wxImageList::Draw(int index, |
239 | wxDC& dc, | |
240 | int x, int y, | |
241 | int flags, | |
242 | bool solidBackground) | |
2bda0e17 | 243 | { |
4b7f2165 | 244 | HDC hDC = GetHdcOf(dc); |
59af881e | 245 | wxCHECK_MSG( hDC, false, _T("invalid wxDC in wxImageList::Draw") ); |
4b7f2165 VZ |
246 | |
247 | COLORREF clr = CLR_NONE; // transparent by default | |
248 | if ( solidBackground ) | |
249 | { | |
b33576e1 VZ |
250 | const wxBrush& brush = dc.GetBackground(); |
251 | if ( brush.Ok() ) | |
4b7f2165 | 252 | { |
b33576e1 | 253 | clr = wxColourToRGB(brush.GetColour()); |
4b7f2165 VZ |
254 | } |
255 | } | |
256 | ||
257 | ImageList_SetBkColor(GetHImageList(), clr); | |
2bda0e17 KB |
258 | |
259 | UINT style = 0; | |
4b7f2165 VZ |
260 | if ( flags & wxIMAGELIST_DRAW_NORMAL ) |
261 | style |= ILD_NORMAL; | |
262 | if ( flags & wxIMAGELIST_DRAW_TRANSPARENT ) | |
263 | style |= ILD_TRANSPARENT; | |
264 | if ( flags & wxIMAGELIST_DRAW_SELECTED ) | |
265 | style |= ILD_SELECTED; | |
266 | if ( flags & wxIMAGELIST_DRAW_FOCUSED ) | |
267 | style |= ILD_FOCUS; | |
268 | ||
269 | bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0; | |
270 | if ( !ok ) | |
271 | { | |
f6bcfd97 | 272 | wxLogLastError(wxT("ImageList_Draw()")); |
4b7f2165 VZ |
273 | } |
274 | ||
275 | return ok; | |
2bda0e17 KB |
276 | } |
277 | ||
49bf4e3e JS |
278 | // Get the bitmap |
279 | wxBitmap wxImageList::GetBitmap(int index) const | |
280 | { | |
a8ff046b | 281 | #if wxUSE_WXDIB && wxUSE_IMAGE |
49bf4e3e JS |
282 | int bmp_width = 0, bmp_height = 0; |
283 | GetSize(index, bmp_width, bmp_height); | |
284 | ||
285 | wxBitmap bitmap(bmp_width, bmp_height); | |
286 | wxMemoryDC dc; | |
287 | dc.SelectObject(bitmap); | |
288 | ||
289 | // draw it the first time to find a suitable mask colour | |
290 | ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT); | |
291 | dc.SelectObject(wxNullBitmap); | |
292 | ||
293 | // find the suitable mask colour | |
294 | wxImage image = bitmap.ConvertToImage(); | |
295 | unsigned char r = 0, g = 0, b = 0; | |
296 | image.FindFirstUnusedColour(&r, &g, &b); | |
297 | ||
298 | // redraw whole image and bitmap in the mask colour | |
299 | image.Create(bmp_width, bmp_height); | |
300 | image.Replace(0, 0, 0, r, g, b); | |
301 | bitmap = wxBitmap(image); | |
302 | ||
303 | // redraw icon over the mask colour to actually draw it | |
304 | dc.SelectObject(bitmap); | |
305 | ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT); | |
306 | dc.SelectObject(wxNullBitmap); | |
307 | ||
308 | // get the image, set the mask colour and convert back to get transparent bitmap | |
309 | image = bitmap.ConvertToImage(); | |
310 | image.SetMaskColour(r, g, b); | |
311 | bitmap = wxBitmap(image); | |
64c288fa JS |
312 | #else |
313 | wxBitmap bitmap; | |
64c288fa | 314 | #endif |
74aa8254 | 315 | return bitmap; |
49bf4e3e JS |
316 | } |
317 | ||
318 | // Get the icon | |
319 | wxIcon wxImageList::GetIcon(int index) const | |
320 | { | |
321 | HICON hIcon = ImageList_ExtractIcon(0, GetHImageList(), index); | |
322 | if (hIcon) | |
323 | { | |
324 | wxIcon icon; | |
325 | icon.SetHICON((WXHICON)hIcon); | |
a71d815b | 326 | |
49bf4e3e JS |
327 | int iconW, iconH; |
328 | GetSize(index, iconW, iconH); | |
329 | icon.SetSize(iconW, iconH); | |
a71d815b | 330 | |
49bf4e3e JS |
331 | return icon; |
332 | } | |
a71d815b | 333 | else |
49bf4e3e JS |
334 | return wxNullIcon; |
335 | } | |
336 | ||
4b7f2165 VZ |
337 | // ---------------------------------------------------------------------------- |
338 | // helpers | |
339 | // ---------------------------------------------------------------------------- | |
340 | ||
3ca6a5f0 | 341 | static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask) |
4b7f2165 | 342 | { |
e4a84806 VZ |
343 | #if wxUSE_IMAGE |
344 | wxBitmap bitmapWithMask; | |
345 | #endif // wxUSE_IMAGE | |
346 | ||
3ca6a5f0 | 347 | HBITMAP hbmpMask; |
90c1530a | 348 | wxMask *pMask; |
59af881e | 349 | bool deleteMask = false; |
4b7f2165 VZ |
350 | |
351 | if ( mask.Ok() ) | |
352 | { | |
3ca6a5f0 | 353 | hbmpMask = GetHbitmapOf(mask); |
90c1530a | 354 | pMask = NULL; |
4b7f2165 VZ |
355 | } |
356 | else | |
357 | { | |
90c1530a | 358 | pMask = bitmap.GetMask(); |
e4a84806 VZ |
359 | |
360 | #if wxUSE_IMAGE | |
361 | // check if we don't have alpha in this bitmap -- we can create a mask | |
362 | // from it (and we need to do it for the older systems which don't | |
363 | // support 32bpp bitmaps natively) | |
364 | if ( !pMask ) | |
365 | { | |
366 | wxImage img(bitmap.ConvertToImage()); | |
367 | if ( img.HasAlpha() ) | |
368 | { | |
369 | img.ConvertAlphaToMask(); | |
370 | bitmapWithMask = wxBitmap(img); | |
371 | pMask = bitmapWithMask.GetMask(); | |
372 | } | |
373 | } | |
374 | #endif // wxUSE_IMAGE | |
375 | ||
90c1530a | 376 | if ( !pMask ) |
4b7f2165 | 377 | { |
90c1530a VZ |
378 | // use the light grey count as transparent: the trouble here is |
379 | // that the light grey might have been changed by Windows behind | |
380 | // our back, so use the standard colour map to get its real value | |
381 | wxCOLORMAP *cmap = wxGetStdColourMap(); | |
382 | wxColour col; | |
383 | wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from); | |
4b7f2165 | 384 | |
90c1530a | 385 | pMask = new wxMask(bitmap, col); |
3ca6a5f0 | 386 | |
59af881e | 387 | deleteMask = true; |
3ca6a5f0 | 388 | } |
90c1530a VZ |
389 | |
390 | hbmpMask = (HBITMAP)pMask->GetMaskBitmap(); | |
4b7f2165 VZ |
391 | } |
392 | ||
77ffb593 | 393 | // windows mask convention is opposite to the wxWidgets one |
3ca6a5f0 | 394 | HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask); |
90c1530a VZ |
395 | |
396 | if ( deleteMask ) | |
397 | { | |
398 | delete pMask; | |
399 | } | |
3ca6a5f0 BP |
400 | |
401 | return hbmpMaskInv; | |
4b7f2165 | 402 | } |