]>
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" |
888dde65 RR |
41 | #include "wx/dc.h" |
42 | #include "wx/msw/dc.h" | |
5c96c864 | 43 | #include "wx/msw/dib.h" |
2bda0e17 KB |
44 | #include "wx/msw/private.h" |
45 | ||
4b7f2165 VZ |
46 | // ---------------------------------------------------------------------------- |
47 | // wxWin macros | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
2bda0e17 | 50 | IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject) |
2bda0e17 | 51 | |
4b7f2165 VZ |
52 | #define GetHImageList() ((HIMAGELIST)m_hImageList) |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // private functions | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | // returns the mask if it's valid, otherwise the bitmap mask and, if it's not | |
59 | // valid neither, a "solid" mask (no transparent zones at all) | |
3ca6a5f0 | 60 | static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask); |
4b7f2165 VZ |
61 | |
62 | // ============================================================================ | |
63 | // implementation | |
64 | // ============================================================================ | |
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // wxImageList creation/destruction | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
70 | wxImageList::wxImageList() | |
2bda0e17 KB |
71 | { |
72 | m_hImageList = 0; | |
73 | } | |
74 | ||
4b7f2165 VZ |
75 | // Creates an image list |
76 | bool wxImageList::Create(int width, int height, bool mask, int initial) | |
2bda0e17 | 77 | { |
a6e2b3a8 VZ |
78 | UINT flags = 0; |
79 | ||
a6712ab9 VZ |
80 | // as we want to be able to use 32bpp bitmaps in the image lists, we always |
81 | // use ILC_COLOR32, even if the display resolution is less -- the system | |
82 | // will make the best effort to show the bitmap if we do this resulting in | |
83 | // quite acceptable display while using a lower depth ILC_COLOR constant | |
84 | // (e.g. ILC_COLOR16) shows completely broken bitmaps | |
4676948b JS |
85 | #ifdef __WXWINCE__ |
86 | flags |= ILC_COLOR; | |
87 | #else | |
a6712ab9 | 88 | flags |= ILC_COLOR32; |
4676948b | 89 | #endif |
a6e2b3a8 | 90 | |
4b7f2165 VZ |
91 | if ( mask ) |
92 | flags |= ILC_MASK; | |
93 | ||
94 | // Grow by 1, I guess this is reasonable behaviour most of the time | |
95 | m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags, | |
96 | initial, 1); | |
97 | if ( !m_hImageList ) | |
98 | { | |
f6bcfd97 | 99 | wxLogLastError(wxT("ImageList_Create()")); |
4b7f2165 | 100 | } |
2bda0e17 | 101 | |
4b7f2165 VZ |
102 | return m_hImageList != 0; |
103 | } | |
2bda0e17 | 104 | |
4b7f2165 | 105 | wxImageList::~wxImageList() |
2bda0e17 | 106 | { |
4b7f2165 VZ |
107 | if ( m_hImageList ) |
108 | { | |
109 | ImageList_Destroy(GetHImageList()); | |
110 | m_hImageList = 0; | |
111 | } | |
2bda0e17 KB |
112 | } |
113 | ||
4b7f2165 VZ |
114 | // ---------------------------------------------------------------------------- |
115 | // wxImageList attributes | |
116 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 117 | |
4b7f2165 VZ |
118 | // Returns the number of images in the image list. |
119 | int wxImageList::GetImageCount() const | |
2bda0e17 | 120 | { |
9a83f860 | 121 | wxASSERT_MSG( m_hImageList, wxT("invalid image list") ); |
2bda0e17 | 122 | |
4b7f2165 | 123 | return ImageList_GetImageCount(GetHImageList()); |
2bda0e17 KB |
124 | } |
125 | ||
f6bcfd97 BP |
126 | // Returns the size (same for all images) of the images in the list |
127 | bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const | |
128 | { | |
9a83f860 | 129 | wxASSERT_MSG( m_hImageList, wxT("invalid image list") ); |
f6bcfd97 BP |
130 | |
131 | return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0; | |
132 | } | |
133 | ||
4b7f2165 VZ |
134 | // ---------------------------------------------------------------------------- |
135 | // wxImageList operations | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
2bda0e17 KB |
138 | // Adds a bitmap, and optionally a mask bitmap. |
139 | // Note that wxImageList creates new bitmaps, so you may delete | |
140 | // 'bitmap' and 'mask'. | |
141 | int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) | |
142 | { | |
5c96c864 VZ |
143 | HBITMAP hbmp; |
144 | ||
145 | #if wxUSE_WXDIB && wxUSE_IMAGE | |
146 | // wxBitmap normally stores alpha in pre-multiplied format but | |
147 | // ImageList_Draw() does pre-multiplication internally so we need to undo | |
148 | // the pre-multiplication here. Converting back and forth like this is, of | |
149 | // course, very inefficient but it's better than wrong appearance so we do | |
150 | // this for now until a better way can be found. | |
151 | AutoHBITMAP hbmpRelease; | |
152 | if ( bitmap.HasAlpha() ) | |
153 | { | |
154 | hbmp = wxDIB(bitmap.ConvertToImage(), | |
155 | wxDIB::PixelFormat_NotPreMultiplied).Detach(); | |
156 | hbmpRelease.Init(hbmp); | |
157 | } | |
158 | else | |
159 | #endif // wxUSE_WXDIB && wxUSE_IMAGE | |
160 | hbmp = GetHbitmapOf(bitmap); | |
161 | ||
f7196178 | 162 | AutoHBITMAP hbmpMask(GetMaskForImage(bitmap, mask)); |
e29b83a4 | 163 | |
5c96c864 | 164 | int index = ImageList_Add(GetHImageList(), hbmp, hbmpMask); |
4b7f2165 | 165 | if ( index == -1 ) |
e29b83a4 VZ |
166 | { |
167 | wxLogError(_("Couldn't add an image to the image list.")); | |
168 | } | |
169 | ||
170 | return index; | |
2bda0e17 KB |
171 | } |
172 | ||
173 | // Adds a bitmap, using the specified colour to create the mask bitmap | |
174 | // Note that wxImageList creates new bitmaps, so you may delete | |
175 | // 'bitmap'. | |
176 | int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) | |
177 | { | |
5c96c864 VZ |
178 | HBITMAP hbmp; |
179 | ||
180 | #if wxUSE_WXDIB && wxUSE_IMAGE | |
181 | // See the comment in overloaded Add() above. | |
182 | AutoHBITMAP hbmpRelease; | |
183 | if ( bitmap.HasAlpha() ) | |
184 | { | |
185 | hbmp = wxDIB(bitmap.ConvertToImage(), | |
186 | wxDIB::PixelFormat_NotPreMultiplied).Detach(); | |
187 | hbmpRelease.Init(hbmp); | |
188 | } | |
189 | else | |
190 | #endif // wxUSE_WXDIB && wxUSE_IMAGE | |
191 | hbmp = GetHbitmapOf(bitmap); | |
192 | ||
4b7f2165 | 193 | int index = ImageList_AddMasked(GetHImageList(), |
5c96c864 | 194 | hbmp, |
4b7f2165 VZ |
195 | wxColourToRGB(maskColour)); |
196 | if ( index == -1 ) | |
197 | { | |
198 | wxLogError(_("Couldn't add an image to the image list.")); | |
199 | } | |
200 | ||
201 | return index; | |
2bda0e17 KB |
202 | } |
203 | ||
204 | // Adds a bitmap and mask from an icon. | |
205 | int wxImageList::Add(const wxIcon& icon) | |
206 | { | |
4b7f2165 VZ |
207 | int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon)); |
208 | if ( index == -1 ) | |
209 | { | |
210 | wxLogError(_("Couldn't add an image to the image list.")); | |
211 | } | |
212 | ||
213 | return index; | |
2bda0e17 KB |
214 | } |
215 | ||
216 | // Replaces a bitmap, optionally passing a mask bitmap. | |
217 | // Note that wxImageList creates new bitmaps, so you may delete | |
218 | // 'bitmap' and 'mask'. | |
4b7f2165 | 219 | bool wxImageList::Replace(int index, |
5c96c864 VZ |
220 | const wxBitmap& bitmap, |
221 | const wxBitmap& mask) | |
2bda0e17 | 222 | { |
5c96c864 VZ |
223 | HBITMAP hbmp; |
224 | ||
225 | #if wxUSE_WXDIB && wxUSE_IMAGE | |
226 | // See the comment in Add() above. | |
227 | AutoHBITMAP hbmpRelease; | |
228 | if ( bitmap.HasAlpha() ) | |
229 | { | |
230 | hbmp = wxDIB(bitmap.ConvertToImage(), | |
231 | wxDIB::PixelFormat_NotPreMultiplied).Detach(); | |
232 | hbmpRelease.Init(hbmp); | |
233 | } | |
234 | else | |
235 | #endif // wxUSE_WXDIB && wxUSE_IMAGE | |
236 | hbmp = GetHbitmapOf(bitmap); | |
237 | ||
f7196178 | 238 | AutoHBITMAP hbmpMask(GetMaskForImage(bitmap, mask)); |
2bda0e17 | 239 | |
f7196178 | 240 | if ( !ImageList_Replace(GetHImageList(), index, hbmp, hbmpMask) ) |
4b7f2165 | 241 | { |
3103e8a9 | 242 | wxLogLastError(wxT("ImageList_Replace()")); |
f7196178 | 243 | return false; |
4b7f2165 VZ |
244 | } |
245 | ||
f7196178 | 246 | return true; |
2bda0e17 | 247 | } |
2bda0e17 KB |
248 | |
249 | // Replaces a bitmap and mask from an icon. | |
4b7f2165 | 250 | bool wxImageList::Replace(int i, const wxIcon& icon) |
2bda0e17 | 251 | { |
9fe0c2fc | 252 | bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != -1; |
4b7f2165 VZ |
253 | if ( !ok ) |
254 | { | |
f6bcfd97 | 255 | wxLogLastError(wxT("ImageList_ReplaceIcon()")); |
4b7f2165 VZ |
256 | } |
257 | ||
258 | return ok; | |
2bda0e17 KB |
259 | } |
260 | ||
261 | // Removes the image at the given index. | |
debe6624 | 262 | bool wxImageList::Remove(int index) |
2bda0e17 | 263 | { |
4b7f2165 VZ |
264 | bool ok = ImageList_Remove(GetHImageList(), index) != 0; |
265 | if ( !ok ) | |
266 | { | |
f6bcfd97 | 267 | wxLogLastError(wxT("ImageList_Remove()")); |
4b7f2165 VZ |
268 | } |
269 | ||
270 | return ok; | |
2bda0e17 KB |
271 | } |
272 | ||
273 | // Remove all images | |
4b7f2165 | 274 | bool wxImageList::RemoveAll() |
2bda0e17 | 275 | { |
3c1a88d8 | 276 | // don't use ImageList_RemoveAll() because mingw32 headers don't have it |
1c304f30 | 277 | return Remove(-1); |
2bda0e17 KB |
278 | } |
279 | ||
280 | // Draws the given image on a dc at the specified position. | |
59af881e | 281 | // If 'solidBackground' is true, Draw sets the image list background |
2bda0e17 KB |
282 | // colour to the background colour of the wxDC, to speed up |
283 | // drawing by eliminating masked drawing where possible. | |
4b7f2165 VZ |
284 | bool wxImageList::Draw(int index, |
285 | wxDC& dc, | |
286 | int x, int y, | |
287 | int flags, | |
288 | bool solidBackground) | |
2bda0e17 | 289 | { |
888dde65 RR |
290 | wxDCImpl *impl = dc.GetImpl(); |
291 | wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl ); | |
292 | if (!msw_impl) | |
293 | return false; | |
03647350 | 294 | |
888dde65 | 295 | HDC hDC = GetHdcOf(*msw_impl); |
9a83f860 | 296 | wxCHECK_MSG( hDC, false, wxT("invalid wxDC in wxImageList::Draw") ); |
4b7f2165 VZ |
297 | |
298 | COLORREF clr = CLR_NONE; // transparent by default | |
299 | if ( solidBackground ) | |
300 | { | |
b33576e1 | 301 | const wxBrush& brush = dc.GetBackground(); |
a1b806b9 | 302 | if ( brush.IsOk() ) |
4b7f2165 | 303 | { |
b33576e1 | 304 | clr = wxColourToRGB(brush.GetColour()); |
4b7f2165 VZ |
305 | } |
306 | } | |
307 | ||
308 | ImageList_SetBkColor(GetHImageList(), clr); | |
2bda0e17 KB |
309 | |
310 | UINT style = 0; | |
4b7f2165 VZ |
311 | if ( flags & wxIMAGELIST_DRAW_NORMAL ) |
312 | style |= ILD_NORMAL; | |
313 | if ( flags & wxIMAGELIST_DRAW_TRANSPARENT ) | |
314 | style |= ILD_TRANSPARENT; | |
315 | if ( flags & wxIMAGELIST_DRAW_SELECTED ) | |
316 | style |= ILD_SELECTED; | |
317 | if ( flags & wxIMAGELIST_DRAW_FOCUSED ) | |
318 | style |= ILD_FOCUS; | |
319 | ||
320 | bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0; | |
321 | if ( !ok ) | |
322 | { | |
f6bcfd97 | 323 | wxLogLastError(wxT("ImageList_Draw()")); |
4b7f2165 VZ |
324 | } |
325 | ||
326 | return ok; | |
2bda0e17 KB |
327 | } |
328 | ||
49bf4e3e JS |
329 | // Get the bitmap |
330 | wxBitmap wxImageList::GetBitmap(int index) const | |
331 | { | |
a8ff046b | 332 | #if wxUSE_WXDIB && wxUSE_IMAGE |
49bf4e3e JS |
333 | int bmp_width = 0, bmp_height = 0; |
334 | GetSize(index, bmp_width, bmp_height); | |
335 | ||
336 | wxBitmap bitmap(bmp_width, bmp_height); | |
337 | wxMemoryDC dc; | |
338 | dc.SelectObject(bitmap); | |
339 | ||
340 | // draw it the first time to find a suitable mask colour | |
341 | ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT); | |
342 | dc.SelectObject(wxNullBitmap); | |
343 | ||
344 | // find the suitable mask colour | |
345 | wxImage image = bitmap.ConvertToImage(); | |
346 | unsigned char r = 0, g = 0, b = 0; | |
347 | image.FindFirstUnusedColour(&r, &g, &b); | |
348 | ||
349 | // redraw whole image and bitmap in the mask colour | |
350 | image.Create(bmp_width, bmp_height); | |
351 | image.Replace(0, 0, 0, r, g, b); | |
352 | bitmap = wxBitmap(image); | |
353 | ||
354 | // redraw icon over the mask colour to actually draw it | |
355 | dc.SelectObject(bitmap); | |
356 | ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT); | |
357 | dc.SelectObject(wxNullBitmap); | |
358 | ||
359 | // get the image, set the mask colour and convert back to get transparent bitmap | |
360 | image = bitmap.ConvertToImage(); | |
361 | image.SetMaskColour(r, g, b); | |
362 | bitmap = wxBitmap(image); | |
64c288fa JS |
363 | #else |
364 | wxBitmap bitmap; | |
64c288fa | 365 | #endif |
74aa8254 | 366 | return bitmap; |
49bf4e3e JS |
367 | } |
368 | ||
369 | // Get the icon | |
370 | wxIcon wxImageList::GetIcon(int index) const | |
371 | { | |
372 | HICON hIcon = ImageList_ExtractIcon(0, GetHImageList(), index); | |
373 | if (hIcon) | |
374 | { | |
375 | wxIcon icon; | |
376 | icon.SetHICON((WXHICON)hIcon); | |
a71d815b | 377 | |
49bf4e3e JS |
378 | int iconW, iconH; |
379 | GetSize(index, iconW, iconH); | |
380 | icon.SetSize(iconW, iconH); | |
a71d815b | 381 | |
49bf4e3e JS |
382 | return icon; |
383 | } | |
a71d815b | 384 | else |
49bf4e3e JS |
385 | return wxNullIcon; |
386 | } | |
387 | ||
4b7f2165 VZ |
388 | // ---------------------------------------------------------------------------- |
389 | // helpers | |
390 | // ---------------------------------------------------------------------------- | |
391 | ||
3ca6a5f0 | 392 | static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask) |
4b7f2165 | 393 | { |
e4a84806 VZ |
394 | #if wxUSE_IMAGE |
395 | wxBitmap bitmapWithMask; | |
396 | #endif // wxUSE_IMAGE | |
397 | ||
3ca6a5f0 | 398 | HBITMAP hbmpMask; |
90c1530a | 399 | wxMask *pMask; |
59af881e | 400 | bool deleteMask = false; |
4b7f2165 | 401 | |
a1b806b9 | 402 | if ( mask.IsOk() ) |
4b7f2165 | 403 | { |
3ca6a5f0 | 404 | hbmpMask = GetHbitmapOf(mask); |
90c1530a | 405 | pMask = NULL; |
4b7f2165 VZ |
406 | } |
407 | else | |
408 | { | |
90c1530a | 409 | pMask = bitmap.GetMask(); |
e4a84806 VZ |
410 | |
411 | #if wxUSE_IMAGE | |
412 | // check if we don't have alpha in this bitmap -- we can create a mask | |
413 | // from it (and we need to do it for the older systems which don't | |
414 | // support 32bpp bitmaps natively) | |
415 | if ( !pMask ) | |
416 | { | |
417 | wxImage img(bitmap.ConvertToImage()); | |
418 | if ( img.HasAlpha() ) | |
419 | { | |
420 | img.ConvertAlphaToMask(); | |
421 | bitmapWithMask = wxBitmap(img); | |
422 | pMask = bitmapWithMask.GetMask(); | |
423 | } | |
424 | } | |
425 | #endif // wxUSE_IMAGE | |
426 | ||
90c1530a | 427 | if ( !pMask ) |
4b7f2165 | 428 | { |
90c1530a VZ |
429 | // use the light grey count as transparent: the trouble here is |
430 | // that the light grey might have been changed by Windows behind | |
431 | // our back, so use the standard colour map to get its real value | |
432 | wxCOLORMAP *cmap = wxGetStdColourMap(); | |
433 | wxColour col; | |
434 | wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from); | |
4b7f2165 | 435 | |
90c1530a | 436 | pMask = new wxMask(bitmap, col); |
3ca6a5f0 | 437 | |
59af881e | 438 | deleteMask = true; |
3ca6a5f0 | 439 | } |
90c1530a VZ |
440 | |
441 | hbmpMask = (HBITMAP)pMask->GetMaskBitmap(); | |
4b7f2165 VZ |
442 | } |
443 | ||
77ffb593 | 444 | // windows mask convention is opposite to the wxWidgets one |
3ca6a5f0 | 445 | HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask); |
90c1530a VZ |
446 | |
447 | if ( deleteMask ) | |
448 | { | |
449 | delete pMask; | |
450 | } | |
3ca6a5f0 BP |
451 | |
452 | return hbmpMaskInv; | |
4b7f2165 | 453 | } |