Add bitmaps in non-premultiplied format to wxImageList in wxMSW.
[wxWidgets.git] / src / msw / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/imaglist.cpp
3 // Purpose: wxImageList implementation for Win32
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
29 #include "wx/window.h"
30 #include "wx/icon.h"
31 #include "wx/dc.h"
32 #include "wx/string.h"
33 #include "wx/dcmemory.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/image.h"
37 #include <stdio.h>
38 #endif
39
40 #include "wx/imaglist.h"
41 #include "wx/dc.h"
42 #include "wx/msw/dc.h"
43 #include "wx/msw/dib.h"
44 #include "wx/msw/private.h"
45
46 // ----------------------------------------------------------------------------
47 // wxWin macros
48 // ----------------------------------------------------------------------------
49
50 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
51
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)
60 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // wxImageList creation/destruction
68 // ----------------------------------------------------------------------------
69
70 wxImageList::wxImageList()
71 {
72 m_hImageList = 0;
73 }
74
75 // Creates an image list
76 bool wxImageList::Create(int width, int height, bool mask, int initial)
77 {
78 UINT flags = 0;
79
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
85 #ifdef __WXWINCE__
86 flags |= ILC_COLOR;
87 #else
88 flags |= ILC_COLOR32;
89 #endif
90
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 {
99 wxLogLastError(wxT("ImageList_Create()"));
100 }
101
102 return m_hImageList != 0;
103 }
104
105 wxImageList::~wxImageList()
106 {
107 if ( m_hImageList )
108 {
109 ImageList_Destroy(GetHImageList());
110 m_hImageList = 0;
111 }
112 }
113
114 // ----------------------------------------------------------------------------
115 // wxImageList attributes
116 // ----------------------------------------------------------------------------
117
118 // Returns the number of images in the image list.
119 int wxImageList::GetImageCount() const
120 {
121 wxASSERT_MSG( m_hImageList, wxT("invalid image list") );
122
123 return ImageList_GetImageCount(GetHImageList());
124 }
125
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 {
129 wxASSERT_MSG( m_hImageList, wxT("invalid image list") );
130
131 return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0;
132 }
133
134 // ----------------------------------------------------------------------------
135 // wxImageList operations
136 // ----------------------------------------------------------------------------
137
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 {
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
162 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
163
164 int index = ImageList_Add(GetHImageList(), hbmp, hbmpMask);
165 if ( index == -1 )
166 {
167 wxLogError(_("Couldn't add an image to the image list."));
168 }
169
170 ::DeleteObject(hbmpMask);
171
172 return index;
173 }
174
175 // Adds a bitmap, using the specified colour to create the mask bitmap
176 // Note that wxImageList creates new bitmaps, so you may delete
177 // 'bitmap'.
178 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
179 {
180 HBITMAP hbmp;
181
182 #if wxUSE_WXDIB && wxUSE_IMAGE
183 // See the comment in overloaded Add() above.
184 AutoHBITMAP hbmpRelease;
185 if ( bitmap.HasAlpha() )
186 {
187 hbmp = wxDIB(bitmap.ConvertToImage(),
188 wxDIB::PixelFormat_NotPreMultiplied).Detach();
189 hbmpRelease.Init(hbmp);
190 }
191 else
192 #endif // wxUSE_WXDIB && wxUSE_IMAGE
193 hbmp = GetHbitmapOf(bitmap);
194
195 int index = ImageList_AddMasked(GetHImageList(),
196 hbmp,
197 wxColourToRGB(maskColour));
198 if ( index == -1 )
199 {
200 wxLogError(_("Couldn't add an image to the image list."));
201 }
202
203 return index;
204 }
205
206 // Adds a bitmap and mask from an icon.
207 int wxImageList::Add(const wxIcon& icon)
208 {
209 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
210 if ( index == -1 )
211 {
212 wxLogError(_("Couldn't add an image to the image list."));
213 }
214
215 return index;
216 }
217
218 // Replaces a bitmap, optionally passing a mask bitmap.
219 // Note that wxImageList creates new bitmaps, so you may delete
220 // 'bitmap' and 'mask'.
221 bool wxImageList::Replace(int index,
222 const wxBitmap& bitmap,
223 const wxBitmap& mask)
224 {
225 HBITMAP hbmp;
226
227 #if wxUSE_WXDIB && wxUSE_IMAGE
228 // See the comment in Add() above.
229 AutoHBITMAP hbmpRelease;
230 if ( bitmap.HasAlpha() )
231 {
232 hbmp = wxDIB(bitmap.ConvertToImage(),
233 wxDIB::PixelFormat_NotPreMultiplied).Detach();
234 hbmpRelease.Init(hbmp);
235 }
236 else
237 #endif // wxUSE_WXDIB && wxUSE_IMAGE
238 hbmp = GetHbitmapOf(bitmap);
239
240 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
241
242 bool ok = ImageList_Replace(GetHImageList(), index, hbmp, hbmpMask) != 0;
243 if ( !ok )
244 {
245 wxLogLastError(wxT("ImageList_Replace()"));
246 }
247
248 ::DeleteObject(hbmpMask);
249
250 return ok;
251 }
252
253 // Replaces a bitmap and mask from an icon.
254 bool wxImageList::Replace(int i, const wxIcon& icon)
255 {
256 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != -1;
257 if ( !ok )
258 {
259 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
260 }
261
262 return ok;
263 }
264
265 // Removes the image at the given index.
266 bool wxImageList::Remove(int index)
267 {
268 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
269 if ( !ok )
270 {
271 wxLogLastError(wxT("ImageList_Remove()"));
272 }
273
274 return ok;
275 }
276
277 // Remove all images
278 bool wxImageList::RemoveAll()
279 {
280 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
281 return Remove(-1);
282 }
283
284 // Draws the given image on a dc at the specified position.
285 // If 'solidBackground' is true, Draw sets the image list background
286 // colour to the background colour of the wxDC, to speed up
287 // drawing by eliminating masked drawing where possible.
288 bool wxImageList::Draw(int index,
289 wxDC& dc,
290 int x, int y,
291 int flags,
292 bool solidBackground)
293 {
294 wxDCImpl *impl = dc.GetImpl();
295 wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl );
296 if (!msw_impl)
297 return false;
298
299 HDC hDC = GetHdcOf(*msw_impl);
300 wxCHECK_MSG( hDC, false, wxT("invalid wxDC in wxImageList::Draw") );
301
302 COLORREF clr = CLR_NONE; // transparent by default
303 if ( solidBackground )
304 {
305 const wxBrush& brush = dc.GetBackground();
306 if ( brush.Ok() )
307 {
308 clr = wxColourToRGB(brush.GetColour());
309 }
310 }
311
312 ImageList_SetBkColor(GetHImageList(), clr);
313
314 UINT style = 0;
315 if ( flags & wxIMAGELIST_DRAW_NORMAL )
316 style |= ILD_NORMAL;
317 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
318 style |= ILD_TRANSPARENT;
319 if ( flags & wxIMAGELIST_DRAW_SELECTED )
320 style |= ILD_SELECTED;
321 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
322 style |= ILD_FOCUS;
323
324 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
325 if ( !ok )
326 {
327 wxLogLastError(wxT("ImageList_Draw()"));
328 }
329
330 return ok;
331 }
332
333 // Get the bitmap
334 wxBitmap wxImageList::GetBitmap(int index) const
335 {
336 #if wxUSE_WXDIB && wxUSE_IMAGE
337 int bmp_width = 0, bmp_height = 0;
338 GetSize(index, bmp_width, bmp_height);
339
340 wxBitmap bitmap(bmp_width, bmp_height);
341 wxMemoryDC dc;
342 dc.SelectObject(bitmap);
343
344 // draw it the first time to find a suitable mask colour
345 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
346 dc.SelectObject(wxNullBitmap);
347
348 // find the suitable mask colour
349 wxImage image = bitmap.ConvertToImage();
350 unsigned char r = 0, g = 0, b = 0;
351 image.FindFirstUnusedColour(&r, &g, &b);
352
353 // redraw whole image and bitmap in the mask colour
354 image.Create(bmp_width, bmp_height);
355 image.Replace(0, 0, 0, r, g, b);
356 bitmap = wxBitmap(image);
357
358 // redraw icon over the mask colour to actually draw it
359 dc.SelectObject(bitmap);
360 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
361 dc.SelectObject(wxNullBitmap);
362
363 // get the image, set the mask colour and convert back to get transparent bitmap
364 image = bitmap.ConvertToImage();
365 image.SetMaskColour(r, g, b);
366 bitmap = wxBitmap(image);
367 #else
368 wxBitmap bitmap;
369 #endif
370 return bitmap;
371 }
372
373 // Get the icon
374 wxIcon wxImageList::GetIcon(int index) const
375 {
376 HICON hIcon = ImageList_ExtractIcon(0, GetHImageList(), index);
377 if (hIcon)
378 {
379 wxIcon icon;
380 icon.SetHICON((WXHICON)hIcon);
381
382 int iconW, iconH;
383 GetSize(index, iconW, iconH);
384 icon.SetSize(iconW, iconH);
385
386 return icon;
387 }
388 else
389 return wxNullIcon;
390 }
391
392 // ----------------------------------------------------------------------------
393 // helpers
394 // ----------------------------------------------------------------------------
395
396 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
397 {
398 #if wxUSE_IMAGE
399 wxBitmap bitmapWithMask;
400 #endif // wxUSE_IMAGE
401
402 HBITMAP hbmpMask;
403 wxMask *pMask;
404 bool deleteMask = false;
405
406 if ( mask.Ok() )
407 {
408 hbmpMask = GetHbitmapOf(mask);
409 pMask = NULL;
410 }
411 else
412 {
413 pMask = bitmap.GetMask();
414
415 #if wxUSE_IMAGE
416 // check if we don't have alpha in this bitmap -- we can create a mask
417 // from it (and we need to do it for the older systems which don't
418 // support 32bpp bitmaps natively)
419 if ( !pMask )
420 {
421 wxImage img(bitmap.ConvertToImage());
422 if ( img.HasAlpha() )
423 {
424 img.ConvertAlphaToMask();
425 bitmapWithMask = wxBitmap(img);
426 pMask = bitmapWithMask.GetMask();
427 }
428 }
429 #endif // wxUSE_IMAGE
430
431 if ( !pMask )
432 {
433 // use the light grey count as transparent: the trouble here is
434 // that the light grey might have been changed by Windows behind
435 // our back, so use the standard colour map to get its real value
436 wxCOLORMAP *cmap = wxGetStdColourMap();
437 wxColour col;
438 wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
439
440 pMask = new wxMask(bitmap, col);
441
442 deleteMask = true;
443 }
444
445 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
446 }
447
448 // windows mask convention is opposite to the wxWidgets one
449 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
450
451 if ( deleteMask )
452 {
453 delete pMask;
454 }
455
456 return hbmpMaskInv;
457 }