1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/imaglist.cpp
3 // Purpose: wxImageList implementation for Win32
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
28 #include "wx/window.h"
31 #include "wx/string.h"
32 #include "wx/dcmemory.h"
39 #include "wx/imaglist.h"
41 #include "wx/msw/dc.h"
42 #include "wx/msw/dib.h"
43 #include "wx/msw/private.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
51 #define GetHImageList() ((HIMAGELIST)m_hImageList)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
58 // valid neither, a "solid" mask (no transparent zones at all)
59 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
);
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxImageList creation/destruction
67 // ----------------------------------------------------------------------------
69 wxImageList::wxImageList()
74 // Creates an image list
75 bool wxImageList::Create(int width
, int height
, bool mask
, int initial
)
79 // as we want to be able to use 32bpp bitmaps in the image lists, we always
80 // use ILC_COLOR32, even if the display resolution is less -- the system
81 // will make the best effort to show the bitmap if we do this resulting in
82 // quite acceptable display while using a lower depth ILC_COLOR constant
83 // (e.g. ILC_COLOR16) shows completely broken bitmaps
93 // Grow by 1, I guess this is reasonable behaviour most of the time
94 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
98 wxLogLastError(wxT("ImageList_Create()"));
101 return m_hImageList
!= 0;
104 wxImageList::~wxImageList()
108 ImageList_Destroy(GetHImageList());
113 // ----------------------------------------------------------------------------
114 // wxImageList attributes
115 // ----------------------------------------------------------------------------
117 // Returns the number of images in the image list.
118 int wxImageList::GetImageCount() const
120 wxASSERT_MSG( m_hImageList
, wxT("invalid image list") );
122 return ImageList_GetImageCount(GetHImageList());
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
128 wxASSERT_MSG( m_hImageList
, wxT("invalid image list") );
130 return ImageList_GetIconSize(GetHImageList(), &width
, &height
) != 0;
133 // ----------------------------------------------------------------------------
134 // wxImageList operations
135 // ----------------------------------------------------------------------------
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
)
144 #if wxUSE_WXDIB && wxUSE_IMAGE
145 // wxBitmap normally stores alpha in pre-multiplied format but
146 // ImageList_Draw() does pre-multiplication internally so we need to undo
147 // the pre-multiplication here. Converting back and forth like this is, of
148 // course, very inefficient but it's better than wrong appearance so we do
149 // this for now until a better way can be found.
150 AutoHBITMAP hbmpRelease
;
151 if ( bitmap
.HasAlpha() )
153 hbmp
= wxDIB(bitmap
.ConvertToImage(),
154 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
155 hbmpRelease
.Init(hbmp
);
158 #endif // wxUSE_WXDIB && wxUSE_IMAGE
159 hbmp
= GetHbitmapOf(bitmap
);
161 AutoHBITMAP
hbmpMask(GetMaskForImage(bitmap
, mask
));
163 int index
= ImageList_Add(GetHImageList(), hbmp
, hbmpMask
);
166 wxLogError(_("Couldn't add an image to the image list."));
172 // Adds a bitmap, using the specified colour to create the mask bitmap
173 // Note that wxImageList creates new bitmaps, so you may delete
175 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
179 #if wxUSE_WXDIB && wxUSE_IMAGE
180 // See the comment in overloaded Add() above.
181 AutoHBITMAP hbmpRelease
;
182 if ( bitmap
.HasAlpha() )
184 hbmp
= wxDIB(bitmap
.ConvertToImage(),
185 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
186 hbmpRelease
.Init(hbmp
);
189 #endif // wxUSE_WXDIB && wxUSE_IMAGE
190 hbmp
= GetHbitmapOf(bitmap
);
192 int index
= ImageList_AddMasked(GetHImageList(),
194 wxColourToRGB(maskColour
));
197 wxLogError(_("Couldn't add an image to the image list."));
203 // Adds a bitmap and mask from an icon.
204 int wxImageList::Add(const wxIcon
& icon
)
206 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
209 wxLogError(_("Couldn't add an image to the image list."));
215 // Replaces a bitmap, optionally passing a mask bitmap.
216 // Note that wxImageList creates new bitmaps, so you may delete
217 // 'bitmap' and 'mask'.
218 bool wxImageList::Replace(int index
,
219 const wxBitmap
& bitmap
,
220 const wxBitmap
& mask
)
224 #if wxUSE_WXDIB && wxUSE_IMAGE
225 // See the comment in Add() above.
226 AutoHBITMAP hbmpRelease
;
227 if ( bitmap
.HasAlpha() )
229 hbmp
= wxDIB(bitmap
.ConvertToImage(),
230 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
231 hbmpRelease
.Init(hbmp
);
234 #endif // wxUSE_WXDIB && wxUSE_IMAGE
235 hbmp
= GetHbitmapOf(bitmap
);
237 AutoHBITMAP
hbmpMask(GetMaskForImage(bitmap
, mask
));
239 if ( !ImageList_Replace(GetHImageList(), index
, hbmp
, hbmpMask
) )
241 wxLogLastError(wxT("ImageList_Replace()"));
248 // Replaces a bitmap and mask from an icon.
249 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
251 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != -1;
254 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
260 // Removes the image at the given index.
261 bool wxImageList::Remove(int index
)
263 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
266 wxLogLastError(wxT("ImageList_Remove()"));
273 bool wxImageList::RemoveAll()
275 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
279 // Draws the given image on a dc at the specified position.
280 // If 'solidBackground' is true, Draw sets the image list background
281 // colour to the background colour of the wxDC, to speed up
282 // drawing by eliminating masked drawing where possible.
283 bool wxImageList::Draw(int index
,
287 bool solidBackground
)
289 wxDCImpl
*impl
= dc
.GetImpl();
290 wxMSWDCImpl
*msw_impl
= wxDynamicCast( impl
, wxMSWDCImpl
);
294 HDC hDC
= GetHdcOf(*msw_impl
);
295 wxCHECK_MSG( hDC
, false, wxT("invalid wxDC in wxImageList::Draw") );
297 COLORREF clr
= CLR_NONE
; // transparent by default
298 if ( solidBackground
)
300 const wxBrush
& brush
= dc
.GetBackground();
303 clr
= wxColourToRGB(brush
.GetColour());
307 ImageList_SetBkColor(GetHImageList(), clr
);
310 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
312 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
313 style
|= ILD_TRANSPARENT
;
314 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
315 style
|= ILD_SELECTED
;
316 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
319 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
322 wxLogLastError(wxT("ImageList_Draw()"));
329 wxBitmap
wxImageList::GetBitmap(int index
) const
331 #if wxUSE_WXDIB && wxUSE_IMAGE
332 int bmp_width
= 0, bmp_height
= 0;
333 GetSize(index
, bmp_width
, bmp_height
);
335 wxBitmap
bitmap(bmp_width
, bmp_height
);
337 dc
.SelectObject(bitmap
);
339 // draw it the first time to find a suitable mask colour
340 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
341 dc
.SelectObject(wxNullBitmap
);
343 // find the suitable mask colour
344 wxImage image
= bitmap
.ConvertToImage();
345 unsigned char r
= 0, g
= 0, b
= 0;
346 image
.FindFirstUnusedColour(&r
, &g
, &b
);
348 // redraw whole image and bitmap in the mask colour
349 image
.Create(bmp_width
, bmp_height
);
350 image
.Replace(0, 0, 0, r
, g
, b
);
351 bitmap
= wxBitmap(image
);
353 // redraw icon over the mask colour to actually draw it
354 dc
.SelectObject(bitmap
);
355 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
356 dc
.SelectObject(wxNullBitmap
);
358 // get the image, set the mask colour and convert back to get transparent bitmap
359 image
= bitmap
.ConvertToImage();
360 image
.SetMaskColour(r
, g
, b
);
361 bitmap
= wxBitmap(image
);
369 wxIcon
wxImageList::GetIcon(int index
) const
371 HICON hIcon
= ImageList_ExtractIcon(0, GetHImageList(), index
);
375 icon
.SetHICON((WXHICON
)hIcon
);
378 GetSize(index
, iconW
, iconH
);
379 icon
.SetSize(iconW
, iconH
);
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
391 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
394 wxBitmap bitmapWithMask
;
395 #endif // wxUSE_IMAGE
399 bool deleteMask
= false;
403 hbmpMask
= GetHbitmapOf(mask
);
408 pMask
= bitmap
.GetMask();
411 // check if we don't have alpha in this bitmap -- we can create a mask
412 // from it (and we need to do it for the older systems which don't
413 // support 32bpp bitmaps natively)
416 wxImage
img(bitmap
.ConvertToImage());
417 if ( img
.HasAlpha() )
419 img
.ConvertAlphaToMask();
420 bitmapWithMask
= wxBitmap(img
);
421 pMask
= bitmapWithMask
.GetMask();
424 #endif // wxUSE_IMAGE
428 // use the light grey count as transparent: the trouble here is
429 // that the light grey might have been changed by Windows behind
430 // our back, so use the standard colour map to get its real value
431 wxCOLORMAP
*cmap
= wxGetStdColourMap();
433 wxRGBToColour(col
, cmap
[wxSTD_COL_BTNFACE
].from
);
435 pMask
= new wxMask(bitmap
, col
);
440 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
443 // windows mask convention is opposite to the wxWidgets one
444 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);