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 AutoHBITMAP
hbmpMask(GetMaskForImage(bitmap
, mask
));
164 int index
= ImageList_Add(GetHImageList(), hbmp
, hbmpMask
);
167 wxLogError(_("Couldn't add an image to the image list."));
173 // Adds a bitmap, using the specified colour to create the mask bitmap
174 // Note that wxImageList creates new bitmaps, so you may delete
176 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
180 #if wxUSE_WXDIB && wxUSE_IMAGE
181 // See the comment in overloaded Add() above.
182 AutoHBITMAP hbmpRelease
;
183 if ( bitmap
.HasAlpha() )
185 hbmp
= wxDIB(bitmap
.ConvertToImage(),
186 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
187 hbmpRelease
.Init(hbmp
);
190 #endif // wxUSE_WXDIB && wxUSE_IMAGE
191 hbmp
= GetHbitmapOf(bitmap
);
193 int index
= ImageList_AddMasked(GetHImageList(),
195 wxColourToRGB(maskColour
));
198 wxLogError(_("Couldn't add an image to the image list."));
204 // Adds a bitmap and mask from an icon.
205 int wxImageList::Add(const wxIcon
& icon
)
207 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
210 wxLogError(_("Couldn't add an image to the image list."));
216 // Replaces a bitmap, optionally passing a mask bitmap.
217 // Note that wxImageList creates new bitmaps, so you may delete
218 // 'bitmap' and 'mask'.
219 bool wxImageList::Replace(int index
,
220 const wxBitmap
& bitmap
,
221 const wxBitmap
& mask
)
225 #if wxUSE_WXDIB && wxUSE_IMAGE
226 // See the comment in Add() above.
227 AutoHBITMAP hbmpRelease
;
228 if ( bitmap
.HasAlpha() )
230 hbmp
= wxDIB(bitmap
.ConvertToImage(),
231 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
232 hbmpRelease
.Init(hbmp
);
235 #endif // wxUSE_WXDIB && wxUSE_IMAGE
236 hbmp
= GetHbitmapOf(bitmap
);
238 AutoHBITMAP
hbmpMask(GetMaskForImage(bitmap
, mask
));
240 if ( !ImageList_Replace(GetHImageList(), index
, hbmp
, hbmpMask
) )
242 wxLogLastError(wxT("ImageList_Replace()"));
249 // Replaces a bitmap and mask from an icon.
250 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
252 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != -1;
255 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
261 // Removes the image at the given index.
262 bool wxImageList::Remove(int index
)
264 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
267 wxLogLastError(wxT("ImageList_Remove()"));
274 bool wxImageList::RemoveAll()
276 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
280 // Draws the given image on a dc at the specified position.
281 // If 'solidBackground' is true, Draw sets the image list background
282 // colour to the background colour of the wxDC, to speed up
283 // drawing by eliminating masked drawing where possible.
284 bool wxImageList::Draw(int index
,
288 bool solidBackground
)
290 wxDCImpl
*impl
= dc
.GetImpl();
291 wxMSWDCImpl
*msw_impl
= wxDynamicCast( impl
, wxMSWDCImpl
);
295 HDC hDC
= GetHdcOf(*msw_impl
);
296 wxCHECK_MSG( hDC
, false, wxT("invalid wxDC in wxImageList::Draw") );
298 COLORREF clr
= CLR_NONE
; // transparent by default
299 if ( solidBackground
)
301 const wxBrush
& brush
= dc
.GetBackground();
304 clr
= wxColourToRGB(brush
.GetColour());
308 ImageList_SetBkColor(GetHImageList(), clr
);
311 if ( flags
& wxIMAGELIST_DRAW_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
)
320 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
323 wxLogLastError(wxT("ImageList_Draw()"));
330 wxBitmap
wxImageList::GetBitmap(int index
) const
332 #if wxUSE_WXDIB && wxUSE_IMAGE
333 int bmp_width
= 0, bmp_height
= 0;
334 GetSize(index
, bmp_width
, bmp_height
);
336 wxBitmap
bitmap(bmp_width
, bmp_height
);
338 dc
.SelectObject(bitmap
);
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
);
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
);
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
);
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
);
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
);
370 wxIcon
wxImageList::GetIcon(int index
) const
372 HICON hIcon
= ImageList_ExtractIcon(0, GetHImageList(), index
);
376 icon
.SetHICON((WXHICON
)hIcon
);
379 GetSize(index
, iconW
, iconH
);
380 icon
.SetSize(iconW
, iconH
);
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
395 wxBitmap bitmapWithMask
;
396 #endif // wxUSE_IMAGE
400 bool deleteMask
= false;
404 hbmpMask
= GetHbitmapOf(mask
);
409 pMask
= bitmap
.GetMask();
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)
417 wxImage
img(bitmap
.ConvertToImage());
418 if ( img
.HasAlpha() )
420 img
.ConvertAlphaToMask();
421 bitmapWithMask
= wxBitmap(img
);
422 pMask
= bitmapWithMask
.GetMask();
425 #endif // wxUSE_IMAGE
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();
434 wxRGBToColour(col
, cmap
[wxSTD_COL_BTNFACE
].from
);
436 pMask
= new wxMask(bitmap
, col
);
441 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
444 // windows mask convention is opposite to the wxWidgets one
445 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);