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"
27 #if defined(__WIN95__)
30 #include "wx/window.h"
33 #include "wx/string.h"
34 #include "wx/dcmemory.h"
43 #include "wx/msw/imaglist.h"
44 #include "wx/msw/private.h"
46 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
56 #define GetHImageList() ((HIMAGELIST)m_hImageList)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
63 // valid neither, a "solid" mask (no transparent zones at all)
64 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
);
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
71 // wxImageList creation/destruction
72 // ----------------------------------------------------------------------------
74 wxImageList::wxImageList()
79 // Creates an image list
80 bool wxImageList::Create(int width
, int height
, bool mask
, int initial
)
84 // set appropriate color depth
88 int dd
= wxDisplayDepth();
90 if (dd
<= 4) flags
|= ILC_COLOR
; // 16 color
91 else if (dd
<= 8) flags
|= ILC_COLOR8
; // 256 color
92 else if (dd
<= 16) flags
|= ILC_COLOR16
; // 64k hi-color
93 else if (dd
<= 24) flags
|= ILC_COLOR24
; // 16m truecolor
94 else if (dd
<= 32) flags
|= ILC_COLOR32
; // 16m truecolor
100 // Grow by 1, I guess this is reasonable behaviour most of the time
101 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
105 wxLogLastError(wxT("ImageList_Create()"));
108 return m_hImageList
!= 0;
111 wxImageList::~wxImageList()
115 ImageList_Destroy(GetHImageList());
120 // ----------------------------------------------------------------------------
121 // wxImageList attributes
122 // ----------------------------------------------------------------------------
124 // Returns the number of images in the image list.
125 int wxImageList::GetImageCount() const
127 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
129 return ImageList_GetImageCount(GetHImageList());
132 // Returns the size (same for all images) of the images in the list
133 bool wxImageList::GetSize(int WXUNUSED(index
), int &width
, int &height
) const
135 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
137 return ImageList_GetIconSize(GetHImageList(), &width
, &height
) != 0;
140 // ----------------------------------------------------------------------------
141 // wxImageList operations
142 // ----------------------------------------------------------------------------
144 // Adds a bitmap, and optionally a mask bitmap.
145 // Note that wxImageList creates new bitmaps, so you may delete
146 // 'bitmap' and 'mask'.
147 int wxImageList::Add(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
149 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
151 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
154 wxLogError(_("Couldn't add an image to the image list."));
157 ::DeleteObject(hbmpMask
);
162 // Adds a bitmap, using the specified colour to create the mask bitmap
163 // Note that wxImageList creates new bitmaps, so you may delete
165 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
167 int index
= ImageList_AddMasked(GetHImageList(),
168 GetHbitmapOf(bitmap
),
169 wxColourToRGB(maskColour
));
172 wxLogError(_("Couldn't add an image to the image list."));
178 // Adds a bitmap and mask from an icon.
179 int wxImageList::Add(const wxIcon
& icon
)
181 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
184 wxLogError(_("Couldn't add an image to the image list."));
190 // Replaces a bitmap, optionally passing a mask bitmap.
191 // Note that wxImageList creates new bitmaps, so you may delete
192 // 'bitmap' and 'mask'.
193 bool wxImageList::Replace(int index
,
194 const wxBitmap
& bitmap
, const wxBitmap
& mask
)
196 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
198 bool ok
= ImageList_Replace(GetHImageList(), index
,
199 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
202 wxLogLastError(wxT("ImageList_Replace()"));
205 ::DeleteObject(hbmpMask
);
210 // Replaces a bitmap and mask from an icon.
211 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
213 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != 0;
216 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
222 // Removes the image at the given index.
223 bool wxImageList::Remove(int index
)
225 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
228 wxLogLastError(wxT("ImageList_Remove()"));
235 bool wxImageList::RemoveAll()
237 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
238 int count
= ImageList_GetImageCount(GetHImageList());
239 for ( int i
= 0; i
< count
; i
++ )
241 // the image indexes are shifted, so we should always remove the first
249 // Draws the given image on a dc at the specified position.
250 // If 'solidBackground' is true, Draw sets the image list background
251 // colour to the background colour of the wxDC, to speed up
252 // drawing by eliminating masked drawing where possible.
253 bool wxImageList::Draw(int index
,
257 bool solidBackground
)
259 HDC hDC
= GetHdcOf(dc
);
260 wxCHECK_MSG( hDC
, false, _T("invalid wxDC in wxImageList::Draw") );
262 COLORREF clr
= CLR_NONE
; // transparent by default
263 if ( solidBackground
)
265 const wxBrush
& brush
= dc
.GetBackground();
268 clr
= wxColourToRGB(brush
.GetColour());
272 ImageList_SetBkColor(GetHImageList(), clr
);
275 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
277 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
278 style
|= ILD_TRANSPARENT
;
279 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
280 style
|= ILD_SELECTED
;
281 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
284 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
287 wxLogLastError(wxT("ImageList_Draw()"));
294 wxBitmap
wxImageList::GetBitmap(int index
) const
296 int bmp_width
= 0, bmp_height
= 0;
297 GetSize(index
, bmp_width
, bmp_height
);
299 wxBitmap
bitmap(bmp_width
, bmp_height
);
301 dc
.SelectObject(bitmap
);
303 // draw it the first time to find a suitable mask colour
304 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
305 dc
.SelectObject(wxNullBitmap
);
307 // find the suitable mask colour
308 wxImage image
= bitmap
.ConvertToImage();
309 unsigned char r
= 0, g
= 0, b
= 0;
310 image
.FindFirstUnusedColour(&r
, &g
, &b
);
312 // redraw whole image and bitmap in the mask colour
313 image
.Create(bmp_width
, bmp_height
);
314 image
.Replace(0, 0, 0, r
, g
, b
);
315 bitmap
= wxBitmap(image
);
317 // redraw icon over the mask colour to actually draw it
318 dc
.SelectObject(bitmap
);
319 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
320 dc
.SelectObject(wxNullBitmap
);
322 // get the image, set the mask colour and convert back to get transparent bitmap
323 image
= bitmap
.ConvertToImage();
324 image
.SetMaskColour(r
, g
, b
);
325 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
);