1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/imaglist.cpp
3 // Purpose: wxImageList implementation for Win32
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
29 #include "wx/window.h"
32 #include "wx/string.h"
33 #include "wx/dcmemory.h"
40 #include "wx/imaglist.h"
42 #include "wx/msw/dc.h"
43 #include "wx/msw/dib.h"
44 #include "wx/msw/private.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
52 #define GetHImageList() ((HIMAGELIST)m_hImageList)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
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
);
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxImageList creation/destruction
68 // ----------------------------------------------------------------------------
70 wxImageList::wxImageList()
75 // Creates an image list
76 bool wxImageList::Create(int width
, int height
, bool mask
, int initial
)
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
94 // Grow by 1, I guess this is reasonable behaviour most of the time
95 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
99 wxLogLastError(wxT("ImageList_Create()"));
102 return m_hImageList
!= 0;
105 wxImageList::~wxImageList()
109 ImageList_Destroy(GetHImageList());
114 // ----------------------------------------------------------------------------
115 // wxImageList attributes
116 // ----------------------------------------------------------------------------
118 // Returns the number of images in the image list.
119 int wxImageList::GetImageCount() const
121 wxASSERT_MSG( m_hImageList
, wxT("invalid image list") );
123 return ImageList_GetImageCount(GetHImageList());
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
129 wxASSERT_MSG( m_hImageList
, wxT("invalid image list") );
131 return ImageList_GetIconSize(GetHImageList(), &width
, &height
) != 0;
134 // ----------------------------------------------------------------------------
135 // wxImageList operations
136 // ----------------------------------------------------------------------------
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
)
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() )
154 hbmp
= wxDIB(bitmap
.ConvertToImage(),
155 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
156 hbmpRelease
.Init(hbmp
);
159 #endif // wxUSE_WXDIB && wxUSE_IMAGE
160 hbmp
= GetHbitmapOf(bitmap
);
162 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
164 int index
= ImageList_Add(GetHImageList(), hbmp
, hbmpMask
);
167 wxLogError(_("Couldn't add an image to the image list."));
170 ::DeleteObject(hbmpMask
);
175 // Adds a bitmap, using the specified colour to create the mask bitmap
176 // Note that wxImageList creates new bitmaps, so you may delete
178 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
182 #if wxUSE_WXDIB && wxUSE_IMAGE
183 // See the comment in overloaded Add() above.
184 AutoHBITMAP hbmpRelease
;
185 if ( bitmap
.HasAlpha() )
187 hbmp
= wxDIB(bitmap
.ConvertToImage(),
188 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
189 hbmpRelease
.Init(hbmp
);
192 #endif // wxUSE_WXDIB && wxUSE_IMAGE
193 hbmp
= GetHbitmapOf(bitmap
);
195 int index
= ImageList_AddMasked(GetHImageList(),
197 wxColourToRGB(maskColour
));
200 wxLogError(_("Couldn't add an image to the image list."));
206 // Adds a bitmap and mask from an icon.
207 int wxImageList::Add(const wxIcon
& icon
)
209 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
212 wxLogError(_("Couldn't add an image to the image list."));
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
)
227 #if wxUSE_WXDIB && wxUSE_IMAGE
228 // See the comment in Add() above.
229 AutoHBITMAP hbmpRelease
;
230 if ( bitmap
.HasAlpha() )
232 hbmp
= wxDIB(bitmap
.ConvertToImage(),
233 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
234 hbmpRelease
.Init(hbmp
);
237 #endif // wxUSE_WXDIB && wxUSE_IMAGE
238 hbmp
= GetHbitmapOf(bitmap
);
240 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
242 bool ok
= ImageList_Replace(GetHImageList(), index
, hbmp
, hbmpMask
) != 0;
245 wxLogLastError(wxT("ImageList_Replace()"));
248 ::DeleteObject(hbmpMask
);
253 // Replaces a bitmap and mask from an icon.
254 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
256 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != -1;
259 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
265 // Removes the image at the given index.
266 bool wxImageList::Remove(int index
)
268 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
271 wxLogLastError(wxT("ImageList_Remove()"));
278 bool wxImageList::RemoveAll()
280 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
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
,
292 bool solidBackground
)
294 wxDCImpl
*impl
= dc
.GetImpl();
295 wxMSWDCImpl
*msw_impl
= wxDynamicCast( impl
, wxMSWDCImpl
);
299 HDC hDC
= GetHdcOf(*msw_impl
);
300 wxCHECK_MSG( hDC
, false, wxT("invalid wxDC in wxImageList::Draw") );
302 COLORREF clr
= CLR_NONE
; // transparent by default
303 if ( solidBackground
)
305 const wxBrush
& brush
= dc
.GetBackground();
308 clr
= wxColourToRGB(brush
.GetColour());
312 ImageList_SetBkColor(GetHImageList(), clr
);
315 if ( flags
& wxIMAGELIST_DRAW_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
)
324 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
327 wxLogLastError(wxT("ImageList_Draw()"));
334 wxBitmap
wxImageList::GetBitmap(int index
) const
336 #if wxUSE_WXDIB && wxUSE_IMAGE
337 int bmp_width
= 0, bmp_height
= 0;
338 GetSize(index
, bmp_width
, bmp_height
);
340 wxBitmap
bitmap(bmp_width
, bmp_height
);
342 dc
.SelectObject(bitmap
);
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
);
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
);
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
);
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
);
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
);
374 wxIcon
wxImageList::GetIcon(int index
) const
376 HICON hIcon
= ImageList_ExtractIcon(0, GetHImageList(), index
);
380 icon
.SetHICON((WXHICON
)hIcon
);
383 GetSize(index
, iconW
, iconH
);
384 icon
.SetSize(iconW
, iconH
);
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
399 wxBitmap bitmapWithMask
;
400 #endif // wxUSE_IMAGE
404 bool deleteMask
= false;
408 hbmpMask
= GetHbitmapOf(mask
);
413 pMask
= bitmap
.GetMask();
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)
421 wxImage
img(bitmap
.ConvertToImage());
422 if ( img
.HasAlpha() )
424 img
.ConvertAlphaToMask();
425 bitmapWithMask
= wxBitmap(img
);
426 pMask
= bitmapWithMask
.GetMask();
429 #endif // wxUSE_IMAGE
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();
438 wxRGBToColour(col
, cmap
[wxSTD_COL_BTNFACE
].from
);
440 pMask
= new wxMask(bitmap
, col
);
445 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
448 // windows mask convention is opposite to the wxWidgets one
449 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);