1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/imaglist.cpp
3 // Purpose: wxImageList implementation for Win32
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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(__TWIN32__)) && !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
88 int dd
= wxDisplayDepth();
89 if (dd
<= 4) flags
|= ILC_COLOR
; // 16 color
90 else if (dd
<= 8) flags
|= ILC_COLOR8
; // 256 color
91 else if (dd
<= 16) flags
|= ILC_COLOR16
; // 64k hi-color
92 else if (dd
<= 24) flags
|= ILC_COLOR24
; // 16m truecolor
93 else if (dd
<= 32) flags
|= ILC_COLOR32
; // 16m truecolor
98 // Grow by 1, I guess this is reasonable behaviour most of the time
99 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
103 wxLogLastError(wxT("ImageList_Create()"));
106 return m_hImageList
!= 0;
109 wxImageList::~wxImageList()
113 ImageList_Destroy(GetHImageList());
118 // ----------------------------------------------------------------------------
119 // wxImageList attributes
120 // ----------------------------------------------------------------------------
122 // Returns the number of images in the image list.
123 int wxImageList::GetImageCount() const
125 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
127 return ImageList_GetImageCount(GetHImageList());
130 // Returns the size (same for all images) of the images in the list
131 bool wxImageList::GetSize(int WXUNUSED(index
), int &width
, int &height
) const
133 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
135 return ImageList_GetIconSize(GetHImageList(), &width
, &height
) != 0;
138 // ----------------------------------------------------------------------------
139 // wxImageList operations
140 // ----------------------------------------------------------------------------
142 // Adds a bitmap, and optionally a mask bitmap.
143 // Note that wxImageList creates new bitmaps, so you may delete
144 // 'bitmap' and 'mask'.
145 int wxImageList::Add(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
147 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
149 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
152 wxLogError(_("Couldn't add an image to the image list."));
155 ::DeleteObject(hbmpMask
);
160 // Adds a bitmap, using the specified colour to create the mask bitmap
161 // Note that wxImageList creates new bitmaps, so you may delete
163 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
166 wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32"));
169 int index
= ImageList_AddMasked(GetHImageList(),
170 GetHbitmapOf(bitmap
),
171 wxColourToRGB(maskColour
));
174 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
)
200 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
203 HBITMAP hbmpMask
= GetMaskForImage(bitmap
, mask
);
205 bool ok
= ImageList_Replace(GetHImageList(), index
,
206 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
209 wxLogLastError(wxT("ImageList_Add()"));
212 ::DeleteObject(hbmpMask
);
218 // Replaces a bitmap and mask from an icon.
219 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
221 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != 0;
224 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
230 // Removes the image at the given index.
231 bool wxImageList::Remove(int index
)
234 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
237 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
240 wxLogLastError(wxT("ImageList_Remove()"));
248 bool wxImageList::RemoveAll()
250 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
251 int count
= ImageList_GetImageCount(GetHImageList());
252 for ( int i
= 0; i
< count
; i
++ )
254 // the image indexes are shifted, so we should always remove the first
262 // Draws the given image on a dc at the specified position.
263 // If 'solidBackground' is TRUE, Draw sets the image list background
264 // colour to the background colour of the wxDC, to speed up
265 // drawing by eliminating masked drawing where possible.
266 bool wxImageList::Draw(int index
,
270 bool solidBackground
)
273 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
276 HDC hDC
= GetHdcOf(dc
);
277 wxCHECK_MSG( hDC
, FALSE
, _T("invalid wxDC in wxImageList::Draw") );
279 COLORREF clr
= CLR_NONE
; // transparent by default
280 if ( solidBackground
)
282 wxBrush
*brush
= & dc
.GetBackground();
283 if ( brush
&& brush
->Ok() )
285 clr
= wxColourToRGB(brush
->GetColour());
289 ImageList_SetBkColor(GetHImageList(), clr
);
292 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
294 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
295 style
|= ILD_TRANSPARENT
;
296 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
297 style
|= ILD_SELECTED
;
298 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
301 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
304 wxLogLastError(wxT("ImageList_Draw()"));
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 static HBITMAP
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
318 wxBitmap
*bmpMask
= NULL
;
322 hbmpMask
= GetHbitmapOf(mask
);
326 wxMask
*pMask
= bitmap
.GetMask();
329 hbmpMask
= (HBITMAP
)pMask
->GetMaskBitmap();
333 // create a non transparent mask - apparently, this is needed under
334 // Win9x (it doesn't behave correctly if it's passed 0 mask)
335 bmpMask
= new wxBitmap(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
338 dcMem
.SelectObject(*bmpMask
);
340 dcMem
.SelectObject(wxNullBitmap
);
342 hbmpMask
= GetHbitmapOf(*bmpMask
);
346 // windows mask convention is opposite to the wxWindows one
347 HBITMAP hbmpMaskInv
= wxInvertMask(hbmpMask
);