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/msw/imaglist.h"
41 #include "wx/msw/private.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
49 #define GetHImageList() ((HIMAGELIST)m_hImageList)
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
56 // valid neither, a "solid" mask (no transparent zones at all)
57 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
);
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxImageList creation/destruction
65 // ----------------------------------------------------------------------------
67 wxImageList::wxImageList()
72 // Creates an image list
73 bool wxImageList::Create(int width
, int height
, bool mask
, int initial
)
77 // set appropriate color depth
81 int dd
= wxDisplayDepth();
83 if (dd
<= 4) flags
|= ILC_COLOR
; // 16 color
84 else if (dd
<= 8) flags
|= ILC_COLOR8
; // 256 color
85 else if (dd
<= 16) flags
|= ILC_COLOR16
; // 64k hi-color
86 else if (dd
<= 24) flags
|= ILC_COLOR24
; // 16m truecolor
87 else if (dd
<= 32) flags
|= ILC_COLOR32
; // 16m truecolor
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
, _T("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
, _T("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
)
142 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
144 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
147 wxLogError(_("Couldn't add an image to the image list."));
150 ::DeleteObject(hbmpMask
);
155 // Adds a bitmap, using the specified colour to create the mask bitmap
156 // Note that wxImageList creates new bitmaps, so you may delete
158 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
160 int index
= ImageList_AddMasked(GetHImageList(),
161 GetHbitmapOf(bitmap
),
162 wxColourToRGB(maskColour
));
165 wxLogError(_("Couldn't add an image to the image list."));
171 // Adds a bitmap and mask from an icon.
172 int wxImageList::Add(const wxIcon
& icon
)
174 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
177 wxLogError(_("Couldn't add an image to the image list."));
183 // Replaces a bitmap, optionally passing a mask bitmap.
184 // Note that wxImageList creates new bitmaps, so you may delete
185 // 'bitmap' and 'mask'.
186 bool wxImageList::Replace(int index
,
187 const wxBitmap
& bitmap
, const wxBitmap
& mask
)
189 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
191 bool ok
= ImageList_Replace(GetHImageList(), index
,
192 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
195 wxLogLastError(wxT("ImageList_Replace()"));
198 ::DeleteObject(hbmpMask
);
203 // Replaces a bitmap and mask from an icon.
204 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
206 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != -1;
209 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
215 // Removes the image at the given index.
216 bool wxImageList::Remove(int index
)
218 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
221 wxLogLastError(wxT("ImageList_Remove()"));
228 bool wxImageList::RemoveAll()
230 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
231 int count
= ImageList_GetImageCount(GetHImageList());
232 for ( int i
= 0; i
< count
; i
++ )
234 // the image indexes are shifted, so we should always remove the first
242 // Draws the given image on a dc at the specified position.
243 // If 'solidBackground' is true, Draw sets the image list background
244 // colour to the background colour of the wxDC, to speed up
245 // drawing by eliminating masked drawing where possible.
246 bool wxImageList::Draw(int index
,
250 bool solidBackground
)
252 HDC hDC
= GetHdcOf(dc
);
253 wxCHECK_MSG( hDC
, false, _T("invalid wxDC in wxImageList::Draw") );
255 COLORREF clr
= CLR_NONE
; // transparent by default
256 if ( solidBackground
)
258 const wxBrush
& brush
= dc
.GetBackground();
261 clr
= wxColourToRGB(brush
.GetColour());
265 ImageList_SetBkColor(GetHImageList(), clr
);
268 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
270 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
271 style
|= ILD_TRANSPARENT
;
272 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
273 style
|= ILD_SELECTED
;
274 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
277 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
280 wxLogLastError(wxT("ImageList_Draw()"));
287 wxBitmap
wxImageList::GetBitmap(int index
) const
290 int bmp_width
= 0, bmp_height
= 0;
291 GetSize(index
, bmp_width
, bmp_height
);
293 wxBitmap
bitmap(bmp_width
, bmp_height
);
295 dc
.SelectObject(bitmap
);
297 // draw it the first time to find a suitable mask colour
298 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
299 dc
.SelectObject(wxNullBitmap
);
301 // find the suitable mask colour
302 wxImage image
= bitmap
.ConvertToImage();
303 unsigned char r
= 0, g
= 0, b
= 0;
304 image
.FindFirstUnusedColour(&r
, &g
, &b
);
306 // redraw whole image and bitmap in the mask colour
307 image
.Create(bmp_width
, bmp_height
);
308 image
.Replace(0, 0, 0, r
, g
, b
);
309 bitmap
= wxBitmap(image
);
311 // redraw icon over the mask colour to actually draw it
312 dc
.SelectObject(bitmap
);
313 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
314 dc
.SelectObject(wxNullBitmap
);
316 // get the image, set the mask colour and convert back to get transparent bitmap
317 image
= bitmap
.ConvertToImage();
318 image
.SetMaskColour(r
, g
, b
);
319 bitmap
= wxBitmap(image
);
327 wxIcon
wxImageList::GetIcon(int index
) const
329 HICON hIcon
= ImageList_ExtractIcon(0, GetHImageList(), index
);
333 icon
.SetHICON((WXHICON
)hIcon
);
336 GetSize(index
, iconW
, iconH
);
337 icon
.SetSize(iconW
, iconH
);
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
353 bool deleteMask
= false;
357 hbmpMask
= GetHbitmapOf(mask
);
362 pMask
= bitmap
.GetMask();
365 // use the light grey count as transparent: the trouble here is
366 // that the light grey might have been changed by Windows behind
367 // our back, so use the standard colour map to get its real value
368 wxCOLORMAP
*cmap
= wxGetStdColourMap();
370 wxRGBToColour(col
, cmap
[wxSTD_COL_BTNFACE
].from
);
372 pMask
= new wxMask(bitmap
, col
);
377 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
380 // windows mask convention is opposite to the wxWidgets one
381 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);