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"
46 #include "wx/msw/imaglist.h"
47 #include "wx/msw/private.h"
49 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxImageList
, wxObject
)
59 #define GetHImageList() ((HIMAGELIST)m_hImageList)
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
66 // valid neither, a "solid" mask (no transparent zones at all)
67 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
);
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
74 // wxImageList creation/destruction
75 // ----------------------------------------------------------------------------
77 wxImageList::wxImageList()
82 // Creates an image list
83 bool wxImageList::Create(int width
, int height
, bool mask
, int initial
)
87 // set appropriate color depth
91 int dd
= wxDisplayDepth();
93 if (dd
<= 4) flags
|= ILC_COLOR
; // 16 color
94 else if (dd
<= 8) flags
|= ILC_COLOR8
; // 256 color
95 else if (dd
<= 16) flags
|= ILC_COLOR16
; // 64k hi-color
96 else if (dd
<= 24) flags
|= ILC_COLOR24
; // 16m truecolor
97 else if (dd
<= 32) flags
|= ILC_COLOR32
; // 16m truecolor
103 // Grow by 1, I guess this is reasonable behaviour most of the time
104 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
108 wxLogLastError(wxT("ImageList_Create()"));
111 return m_hImageList
!= 0;
114 wxImageList::~wxImageList()
118 ImageList_Destroy(GetHImageList());
123 // ----------------------------------------------------------------------------
124 // wxImageList attributes
125 // ----------------------------------------------------------------------------
127 // Returns the number of images in the image list.
128 int wxImageList::GetImageCount() const
130 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
132 return ImageList_GetImageCount(GetHImageList());
135 // Returns the size (same for all images) of the images in the list
136 bool wxImageList::GetSize(int WXUNUSED(index
), int &width
, int &height
) const
138 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
140 return ImageList_GetIconSize(GetHImageList(), &width
, &height
) != 0;
143 // ----------------------------------------------------------------------------
144 // wxImageList operations
145 // ----------------------------------------------------------------------------
147 // Adds a bitmap, and optionally a mask bitmap.
148 // Note that wxImageList creates new bitmaps, so you may delete
149 // 'bitmap' and 'mask'.
150 int wxImageList::Add(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
152 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
154 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
157 wxLogError(_("Couldn't add an image to the image list."));
160 ::DeleteObject(hbmpMask
);
165 // Adds a bitmap, using the specified colour to create the mask bitmap
166 // Note that wxImageList creates new bitmaps, so you may delete
168 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
170 int index
= ImageList_AddMasked(GetHImageList(),
171 GetHbitmapOf(bitmap
),
172 wxColourToRGB(maskColour
));
175 wxLogError(_("Couldn't add an image to the image list."));
181 // Adds a bitmap and mask from an icon.
182 int wxImageList::Add(const wxIcon
& icon
)
184 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
187 wxLogError(_("Couldn't add an image to the image list."));
193 // Replaces a bitmap, optionally passing a mask bitmap.
194 // Note that wxImageList creates new bitmaps, so you may delete
195 // 'bitmap' and 'mask'.
196 bool wxImageList::Replace(int index
,
197 const wxBitmap
& bitmap
, const wxBitmap
& mask
)
199 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
201 bool ok
= ImageList_Replace(GetHImageList(), index
,
202 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
205 wxLogLastError(wxT("ImageList_Add()"));
208 ::DeleteObject(hbmpMask
);
213 // Replaces a bitmap and mask from an icon.
214 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
216 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != 0;
219 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
225 // Removes the image at the given index.
226 bool wxImageList::Remove(int index
)
228 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
231 wxLogLastError(wxT("ImageList_Remove()"));
238 bool wxImageList::RemoveAll()
240 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
241 int count
= ImageList_GetImageCount(GetHImageList());
242 for ( int i
= 0; i
< count
; i
++ )
244 // the image indexes are shifted, so we should always remove the first
252 // Draws the given image on a dc at the specified position.
253 // If 'solidBackground' is true, Draw sets the image list background
254 // colour to the background colour of the wxDC, to speed up
255 // drawing by eliminating masked drawing where possible.
256 bool wxImageList::Draw(int index
,
260 bool solidBackground
)
262 HDC hDC
= GetHdcOf(dc
);
263 wxCHECK_MSG( hDC
, false, _T("invalid wxDC in wxImageList::Draw") );
265 COLORREF clr
= CLR_NONE
; // transparent by default
266 if ( solidBackground
)
268 const wxBrush
& brush
= dc
.GetBackground();
271 clr
= wxColourToRGB(brush
.GetColour());
275 ImageList_SetBkColor(GetHImageList(), clr
);
278 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
280 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
281 style
|= ILD_TRANSPARENT
;
282 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
283 style
|= ILD_SELECTED
;
284 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
287 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
290 wxLogLastError(wxT("ImageList_Draw()"));
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
304 bool deleteMask
= false;
308 hbmpMask
= GetHbitmapOf(mask
);
313 pMask
= bitmap
.GetMask();
316 // use the light grey count as transparent: the trouble here is
317 // that the light grey might have been changed by Windows behind
318 // our back, so use the standard colour map to get its real value
319 wxCOLORMAP
*cmap
= wxGetStdColourMap();
321 wxRGBToColour(col
, cmap
[wxSTD_COL_BTNFACE
].from
);
323 pMask
= new wxMask(bitmap
, col
);
328 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
331 // windows mask convention is opposite to the wxWidgets one
332 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);