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/window.h"
31 #include "wx/string.h"
32 #include "wx/dcmemory.h"
41 #include "wx/msw/imaglist.h"
42 #include "wx/msw/private.h"
44 // include <commctrl.h> "properly"
45 #include "wx/msw/wrapcctl.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
53 #define GetHImageList() ((HIMAGELIST)m_hImageList)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
60 // valid neither, a "solid" mask (no transparent zones at all)
61 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
);
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // wxImageList creation/destruction
69 // ----------------------------------------------------------------------------
71 wxImageList::wxImageList()
76 // Creates an image list
77 bool wxImageList::Create(int width
, int height
, bool mask
, int initial
)
81 // set appropriate color depth
85 int dd
= wxDisplayDepth();
87 if (dd
<= 4) flags
|= ILC_COLOR
; // 16 color
88 else if (dd
<= 8) flags
|= ILC_COLOR8
; // 256 color
89 else if (dd
<= 16) flags
|= ILC_COLOR16
; // 64k hi-color
90 else if (dd
<= 24) flags
|= ILC_COLOR24
; // 16m truecolor
91 else if (dd
<= 32) flags
|= ILC_COLOR32
; // 16m truecolor
97 // Grow by 1, I guess this is reasonable behaviour most of the time
98 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
102 wxLogLastError(wxT("ImageList_Create()"));
105 return m_hImageList
!= 0;
108 wxImageList::~wxImageList()
112 ImageList_Destroy(GetHImageList());
117 // ----------------------------------------------------------------------------
118 // wxImageList attributes
119 // ----------------------------------------------------------------------------
121 // Returns the number of images in the image list.
122 int wxImageList::GetImageCount() const
124 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
126 return ImageList_GetImageCount(GetHImageList());
129 // Returns the size (same for all images) of the images in the list
130 bool wxImageList::GetSize(int WXUNUSED(index
), int &width
, int &height
) const
132 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
134 return ImageList_GetIconSize(GetHImageList(), &width
, &height
) != 0;
137 // ----------------------------------------------------------------------------
138 // wxImageList operations
139 // ----------------------------------------------------------------------------
141 // Adds a bitmap, and optionally a mask bitmap.
142 // Note that wxImageList creates new bitmaps, so you may delete
143 // 'bitmap' and 'mask'.
144 int wxImageList::Add(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
146 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
148 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
151 wxLogError(_("Couldn't add an image to the image list."));
154 ::DeleteObject(hbmpMask
);
159 // Adds a bitmap, using the specified colour to create the mask bitmap
160 // Note that wxImageList creates new bitmaps, so you may delete
162 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
164 int index
= ImageList_AddMasked(GetHImageList(),
165 GetHbitmapOf(bitmap
),
166 wxColourToRGB(maskColour
));
169 wxLogError(_("Couldn't add an image to the image list."));
175 // Adds a bitmap and mask from an icon.
176 int wxImageList::Add(const wxIcon
& icon
)
178 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
181 wxLogError(_("Couldn't add an image to the image list."));
187 // Replaces a bitmap, optionally passing a mask bitmap.
188 // Note that wxImageList creates new bitmaps, so you may delete
189 // 'bitmap' and 'mask'.
190 bool wxImageList::Replace(int index
,
191 const wxBitmap
& bitmap
, const wxBitmap
& mask
)
193 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
195 bool ok
= ImageList_Replace(GetHImageList(), index
,
196 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
199 wxLogLastError(wxT("ImageList_Replace()"));
202 ::DeleteObject(hbmpMask
);
207 // Replaces a bitmap and mask from an icon.
208 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
210 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != -1;
213 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
219 // Removes the image at the given index.
220 bool wxImageList::Remove(int index
)
222 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
225 wxLogLastError(wxT("ImageList_Remove()"));
232 bool wxImageList::RemoveAll()
234 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
235 int count
= ImageList_GetImageCount(GetHImageList());
236 for ( int i
= 0; i
< count
; i
++ )
238 // the image indexes are shifted, so we should always remove the first
246 // Draws the given image on a dc at the specified position.
247 // If 'solidBackground' is true, Draw sets the image list background
248 // colour to the background colour of the wxDC, to speed up
249 // drawing by eliminating masked drawing where possible.
250 bool wxImageList::Draw(int index
,
254 bool solidBackground
)
256 HDC hDC
= GetHdcOf(dc
);
257 wxCHECK_MSG( hDC
, false, _T("invalid wxDC in wxImageList::Draw") );
259 COLORREF clr
= CLR_NONE
; // transparent by default
260 if ( solidBackground
)
262 const wxBrush
& brush
= dc
.GetBackground();
265 clr
= wxColourToRGB(brush
.GetColour());
269 ImageList_SetBkColor(GetHImageList(), clr
);
272 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
274 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
275 style
|= ILD_TRANSPARENT
;
276 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
277 style
|= ILD_SELECTED
;
278 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
281 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
284 wxLogLastError(wxT("ImageList_Draw()"));
291 wxBitmap
wxImageList::GetBitmap(int index
) const
294 int bmp_width
= 0, bmp_height
= 0;
295 GetSize(index
, bmp_width
, bmp_height
);
297 wxBitmap
bitmap(bmp_width
, bmp_height
);
299 dc
.SelectObject(bitmap
);
301 // draw it the first time to find a suitable mask colour
302 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
303 dc
.SelectObject(wxNullBitmap
);
305 // find the suitable mask colour
306 wxImage image
= bitmap
.ConvertToImage();
307 unsigned char r
= 0, g
= 0, b
= 0;
308 image
.FindFirstUnusedColour(&r
, &g
, &b
);
310 // redraw whole image and bitmap in the mask colour
311 image
.Create(bmp_width
, bmp_height
);
312 image
.Replace(0, 0, 0, r
, g
, b
);
313 bitmap
= wxBitmap(image
);
315 // redraw icon over the mask colour to actually draw it
316 dc
.SelectObject(bitmap
);
317 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
318 dc
.SelectObject(wxNullBitmap
);
320 // get the image, set the mask colour and convert back to get transparent bitmap
321 image
= bitmap
.ConvertToImage();
322 image
.SetMaskColour(r
, g
, b
);
323 bitmap
= wxBitmap(image
);
331 wxIcon
wxImageList::GetIcon(int index
) const
333 HICON hIcon
= ImageList_ExtractIcon(0, GetHImageList(), index
);
337 icon
.SetHICON((WXHICON
)hIcon
);
340 GetSize(index
, iconW
, iconH
);
341 icon
.SetSize(iconW
, iconH
);
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
357 bool deleteMask
= false;
361 hbmpMask
= GetHbitmapOf(mask
);
366 pMask
= bitmap
.GetMask();
369 // use the light grey count as transparent: the trouble here is
370 // that the light grey might have been changed by Windows behind
371 // our back, so use the standard colour map to get its real value
372 wxCOLORMAP
*cmap
= wxGetStdColourMap();
374 wxRGBToColour(col
, cmap
[wxSTD_COL_BTNFACE
].from
);
376 pMask
= new wxMask(bitmap
, col
);
381 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
384 // windows mask convention is opposite to the wxWidgets one
385 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);