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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "imaglist.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if defined(__WIN95__)
34 #include "wx/window.h"
37 #include "wx/string.h"
38 #include "wx/dcmemory.h"
47 #include "wx/msw/imaglist.h"
48 #include "wx/msw/private.h"
50 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
60 #define GetHImageList() ((HIMAGELIST)m_hImageList)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
67 // valid neither, a "solid" mask (no transparent zones at all)
68 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
);
70 // ============================================================================
72 // ============================================================================
74 // ----------------------------------------------------------------------------
75 // wxImageList creation/destruction
76 // ----------------------------------------------------------------------------
78 wxImageList::wxImageList()
83 // Creates an image list
84 bool wxImageList::Create(int width
, int height
, bool mask
, int initial
)
88 // set appropriate color depth
92 int dd
= wxDisplayDepth();
94 if (dd
<= 4) flags
|= ILC_COLOR
; // 16 color
95 else if (dd
<= 8) flags
|= ILC_COLOR8
; // 256 color
96 else if (dd
<= 16) flags
|= ILC_COLOR16
; // 64k hi-color
97 else if (dd
<= 24) flags
|= ILC_COLOR24
; // 16m truecolor
98 else if (dd
<= 32) flags
|= ILC_COLOR32
; // 16m truecolor
104 // Grow by 1, I guess this is reasonable behaviour most of the time
105 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
109 wxLogLastError(wxT("ImageList_Create()"));
112 return m_hImageList
!= 0;
115 wxImageList::~wxImageList()
119 ImageList_Destroy(GetHImageList());
124 // ----------------------------------------------------------------------------
125 // wxImageList attributes
126 // ----------------------------------------------------------------------------
128 // Returns the number of images in the image list.
129 int wxImageList::GetImageCount() const
131 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
133 return ImageList_GetImageCount(GetHImageList());
136 // Returns the size (same for all images) of the images in the list
137 bool wxImageList::GetSize(int WXUNUSED(index
), int &width
, int &height
) const
139 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
141 return ImageList_GetIconSize(GetHImageList(), &width
, &height
) != 0;
144 // ----------------------------------------------------------------------------
145 // wxImageList operations
146 // ----------------------------------------------------------------------------
148 // Adds a bitmap, and optionally a mask bitmap.
149 // Note that wxImageList creates new bitmaps, so you may delete
150 // 'bitmap' and 'mask'.
151 int wxImageList::Add(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
153 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
155 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
158 wxLogError(_("Couldn't add an image to the image list."));
161 ::DeleteObject(hbmpMask
);
166 // Adds a bitmap, using the specified colour to create the mask bitmap
167 // Note that wxImageList creates new bitmaps, so you may delete
169 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
171 int index
= ImageList_AddMasked(GetHImageList(),
172 GetHbitmapOf(bitmap
),
173 wxColourToRGB(maskColour
));
176 wxLogError(_("Couldn't add an image to the image list."));
182 // Adds a bitmap and mask from an icon.
183 int wxImageList::Add(const wxIcon
& icon
)
185 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
188 wxLogError(_("Couldn't add an image to the image list."));
194 // Replaces a bitmap, optionally passing a mask bitmap.
195 // Note that wxImageList creates new bitmaps, so you may delete
196 // 'bitmap' and 'mask'.
197 bool wxImageList::Replace(int index
,
198 const wxBitmap
& bitmap
, const wxBitmap
& mask
)
200 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
202 bool ok
= ImageList_Replace(GetHImageList(), index
,
203 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
206 wxLogLastError(wxT("ImageList_Replace()"));
209 ::DeleteObject(hbmpMask
);
214 // Replaces a bitmap and mask from an icon.
215 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
217 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != 0;
220 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
226 // Removes the image at the given index.
227 bool wxImageList::Remove(int index
)
229 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
232 wxLogLastError(wxT("ImageList_Remove()"));
239 bool wxImageList::RemoveAll()
241 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
242 int count
= ImageList_GetImageCount(GetHImageList());
243 for ( int i
= 0; i
< count
; i
++ )
245 // the image indexes are shifted, so we should always remove the first
253 // Draws the given image on a dc at the specified position.
254 // If 'solidBackground' is true, Draw sets the image list background
255 // colour to the background colour of the wxDC, to speed up
256 // drawing by eliminating masked drawing where possible.
257 bool wxImageList::Draw(int index
,
261 bool solidBackground
)
263 HDC hDC
= GetHdcOf(dc
);
264 wxCHECK_MSG( hDC
, false, _T("invalid wxDC in wxImageList::Draw") );
266 COLORREF clr
= CLR_NONE
; // transparent by default
267 if ( solidBackground
)
269 const wxBrush
& brush
= dc
.GetBackground();
272 clr
= wxColourToRGB(brush
.GetColour());
276 ImageList_SetBkColor(GetHImageList(), clr
);
279 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
281 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
282 style
|= ILD_TRANSPARENT
;
283 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
284 style
|= ILD_SELECTED
;
285 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
288 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
291 wxLogLastError(wxT("ImageList_Draw()"));
298 wxBitmap
wxImageList::GetBitmap(int index
) const
300 int bmp_width
= 0, bmp_height
= 0;
301 GetSize(index
, bmp_width
, bmp_height
);
303 wxBitmap
bitmap(bmp_width
, bmp_height
);
305 dc
.SelectObject(bitmap
);
307 // draw it the first time to find a suitable mask colour
308 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
309 dc
.SelectObject(wxNullBitmap
);
311 // find the suitable mask colour
312 wxImage image
= bitmap
.ConvertToImage();
313 unsigned char r
= 0, g
= 0, b
= 0;
314 image
.FindFirstUnusedColour(&r
, &g
, &b
);
316 // redraw whole image and bitmap in the mask colour
317 image
.Create(bmp_width
, bmp_height
);
318 image
.Replace(0, 0, 0, r
, g
, b
);
319 bitmap
= wxBitmap(image
);
321 // redraw icon over the mask colour to actually draw it
322 dc
.SelectObject(bitmap
);
323 ((wxImageList
*)this)->Draw(index
, dc
, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT
);
324 dc
.SelectObject(wxNullBitmap
);
326 // get the image, set the mask colour and convert back to get transparent bitmap
327 image
= bitmap
.ConvertToImage();
328 image
.SetMaskColour(r
, g
, b
);
329 bitmap
= wxBitmap(image
);
335 wxIcon
wxImageList::GetIcon(int index
) const
337 HICON hIcon
= ImageList_ExtractIcon(0, GetHImageList(), index
);
341 icon
.SetHICON((WXHICON
)hIcon
);
344 GetSize(index
, iconW
, iconH
);
345 icon
.SetSize(iconW
, iconH
);
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
361 bool deleteMask
= false;
365 hbmpMask
= GetHbitmapOf(mask
);
370 pMask
= bitmap
.GetMask();
373 // use the light grey count as transparent: the trouble here is
374 // that the light grey might have been changed by Windows behind
375 // our back, so use the standard colour map to get its real value
376 wxCOLORMAP
*cmap
= wxGetStdColourMap();
378 wxRGBToColour(col
, cmap
[wxSTD_COL_BTNFACE
].from
);
380 pMask
= new wxMask(bitmap
, col
);
385 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
388 // windows mask convention is opposite to the wxWidgets one
389 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);