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(__GNUWIN32_OLD__) && !defined(__TWIN32__)
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 wxBitmap
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
)
85 UINT flags
= 0; // TODO shouldallow to specify ILC_COLORxxx here
89 // Grow by 1, I guess this is reasonable behaviour most of the time
90 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(width
, height
, flags
,
94 wxLogLastError("ImageList_Create()");
97 return m_hImageList
!= 0;
100 wxImageList::~wxImageList()
104 ImageList_Destroy(GetHImageList());
109 // ----------------------------------------------------------------------------
110 // wxImageList attributes
111 // ----------------------------------------------------------------------------
113 // Returns the number of images in the image list.
114 int wxImageList::GetImageCount() const
116 wxASSERT_MSG( m_hImageList
, _T("invalid image list") );
118 return ImageList_GetImageCount(GetHImageList());
121 // ----------------------------------------------------------------------------
122 // wxImageList operations
123 // ----------------------------------------------------------------------------
125 // Adds a bitmap, and optionally a mask bitmap.
126 // Note that wxImageList creates new bitmaps, so you may delete
127 // 'bitmap' and 'mask'.
128 int wxImageList::Add(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
130 wxBitmap bmpMask
= GetMaskForImage(bitmap
, mask
);
131 HBITMAP hbmpMask
= wxInvertMask(GetHbitmapOf(bmpMask
));
133 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
136 wxLogError(_("Couldn't add an image to the image list."));
139 ::DeleteObject(hbmpMask
);
144 // Adds a bitmap, using the specified colour to create the mask bitmap
145 // Note that wxImageList creates new bitmaps, so you may delete
147 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
150 wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32"));
153 int index
= ImageList_AddMasked(GetHImageList(),
154 GetHbitmapOf(bitmap
),
155 wxColourToRGB(maskColour
));
158 wxLogError(_("Couldn't add an image to the image list."));
165 // Adds a bitmap and mask from an icon.
166 int wxImageList::Add(const wxIcon
& icon
)
168 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
171 wxLogError(_("Couldn't add an image to the image list."));
177 // Replaces a bitmap, optionally passing a mask bitmap.
178 // Note that wxImageList creates new bitmaps, so you may delete
179 // 'bitmap' and 'mask'.
180 bool wxImageList::Replace(int index
,
181 const wxBitmap
& bitmap
, const wxBitmap
& mask
)
184 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
187 wxBitmap bmpMask
= GetMaskForImage(bitmap
, mask
);
188 HBITMAP hbmpMask
= wxInvertMask(GetHbitmapOf(bmpMask
));
190 bool ok
= ImageList_Replace(GetHImageList(), index
,
191 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
194 wxLogLastError("ImageList_Add()");
197 ::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
)) != 0;
209 wxLogLastError("ImageList_ReplaceIcon()");
215 // Removes the image at the given index.
216 bool wxImageList::Remove(int index
)
219 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
222 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
225 wxLogLastError("ImageList_Remove()");
233 bool wxImageList::RemoveAll()
235 bool ok
= ImageList_RemoveAll(GetHImageList()) != 0;
238 wxLogLastError("ImageList_RemoveAll()");
244 // Draws the given image on a dc at the specified position.
245 // If 'solidBackground' is TRUE, Draw sets the image list background
246 // colour to the background colour of the wxDC, to speed up
247 // drawing by eliminating masked drawing where possible.
248 bool wxImageList::Draw(int index
,
252 bool solidBackground
)
255 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
258 HDC hDC
= GetHdcOf(dc
);
259 wxCHECK_MSG( hDC
, FALSE
, _T("invalid wxDC in wxImageList::Draw") );
261 COLORREF clr
= CLR_NONE
; // transparent by default
262 if ( solidBackground
)
264 wxBrush
*brush
= & dc
.GetBackground();
265 if ( brush
&& brush
->Ok() )
267 clr
= wxColourToRGB(brush
->GetColour());
271 ImageList_SetBkColor(GetHImageList(), clr
);
274 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
276 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
277 style
|= ILD_TRANSPARENT
;
278 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
279 style
|= ILD_SELECTED
;
280 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
283 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
286 wxLogLastError("ImageList_Draw()");
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 static wxBitmap
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
307 wxMask
*pMask
= bitmap
.GetMask();
310 bmpMask
.SetHBITMAP(pMask
->GetMaskBitmap());
316 // create a non transparent mask - apparently, this is needed under
317 // Win9x (it doesn't behave correctly if it's passed 0 mask)
318 bmpMask
.Create(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
321 dcMem
.SelectObject(bmpMask
);
323 dcMem
.SelectObject(wxNullBitmap
);