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
)
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("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 // ----------------------------------------------------------------------------
131 // wxImageList operations
132 // ----------------------------------------------------------------------------
134 // Adds a bitmap, and optionally a mask bitmap.
135 // Note that wxImageList creates new bitmaps, so you may delete
136 // 'bitmap' and 'mask'.
137 int wxImageList::Add(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
139 wxBitmap bmpMask
= GetMaskForImage(bitmap
, mask
);
140 HBITMAP hbmpMask
= wxInvertMask(GetHbitmapOf(bmpMask
));
142 int index
= ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap
), hbmpMask
);
145 wxLogError(_("Couldn't add an image to the image list."));
148 ::DeleteObject(hbmpMask
);
153 // Adds a bitmap, using the specified colour to create the mask bitmap
154 // Note that wxImageList creates new bitmaps, so you may delete
156 int wxImageList::Add(const wxBitmap
& bitmap
, const wxColour
& maskColour
)
159 wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32"));
162 int index
= ImageList_AddMasked(GetHImageList(),
163 GetHbitmapOf(bitmap
),
164 wxColourToRGB(maskColour
));
167 wxLogError(_("Couldn't add an image to the image list."));
174 // Adds a bitmap and mask from an icon.
175 int wxImageList::Add(const wxIcon
& icon
)
177 int index
= ImageList_AddIcon(GetHImageList(), GetHiconOf(icon
));
180 wxLogError(_("Couldn't add an image to the image list."));
186 // Replaces a bitmap, optionally passing a mask bitmap.
187 // Note that wxImageList creates new bitmaps, so you may delete
188 // 'bitmap' and 'mask'.
189 bool wxImageList::Replace(int index
,
190 const wxBitmap
& bitmap
, const wxBitmap
& mask
)
193 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
196 wxBitmap bmpMask
= GetMaskForImage(bitmap
, mask
);
197 HBITMAP hbmpMask
= wxInvertMask(GetHbitmapOf(bmpMask
));
199 bool ok
= ImageList_Replace(GetHImageList(), index
,
200 GetHbitmapOf(bitmap
), hbmpMask
) != 0;
203 wxLogLastError("ImageList_Add()");
206 ::DeleteObject(hbmpMask
);
212 // Replaces a bitmap and mask from an icon.
213 bool wxImageList::Replace(int i
, const wxIcon
& icon
)
215 bool ok
= ImageList_ReplaceIcon(GetHImageList(), i
, GetHiconOf(icon
)) != 0;
218 wxLogLastError("ImageList_ReplaceIcon()");
224 // Removes the image at the given index.
225 bool wxImageList::Remove(int index
)
228 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
231 bool ok
= ImageList_Remove(GetHImageList(), index
) != 0;
234 wxLogLastError("ImageList_Remove()");
242 bool wxImageList::RemoveAll()
244 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
245 int count
= ImageList_GetImageCount(GetHImageList());
246 for ( int i
= 0; i
< count
; i
++ )
248 // the image indexes are shifted, so we should always remove the first
256 // Draws the given image on a dc at the specified position.
257 // If 'solidBackground' is TRUE, Draw sets the image list background
258 // colour to the background colour of the wxDC, to speed up
259 // drawing by eliminating masked drawing where possible.
260 bool wxImageList::Draw(int index
,
264 bool solidBackground
)
267 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
270 HDC hDC
= GetHdcOf(dc
);
271 wxCHECK_MSG( hDC
, FALSE
, _T("invalid wxDC in wxImageList::Draw") );
273 COLORREF clr
= CLR_NONE
; // transparent by default
274 if ( solidBackground
)
276 wxBrush
*brush
= & dc
.GetBackground();
277 if ( brush
&& brush
->Ok() )
279 clr
= wxColourToRGB(brush
->GetColour());
283 ImageList_SetBkColor(GetHImageList(), clr
);
286 if ( flags
& wxIMAGELIST_DRAW_NORMAL
)
288 if ( flags
& wxIMAGELIST_DRAW_TRANSPARENT
)
289 style
|= ILD_TRANSPARENT
;
290 if ( flags
& wxIMAGELIST_DRAW_SELECTED
)
291 style
|= ILD_SELECTED
;
292 if ( flags
& wxIMAGELIST_DRAW_FOCUSED
)
295 bool ok
= ImageList_Draw(GetHImageList(), index
, hDC
, x
, y
, style
) != 0;
298 wxLogLastError("ImageList_Draw()");
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 static wxBitmap
GetMaskForImage(const wxBitmap
& bitmap
, const wxBitmap
& mask
)
319 wxMask
*pMask
= bitmap
.GetMask();
322 bmpMask
.SetHBITMAP(pMask
->GetMaskBitmap());
328 // create a non transparent mask - apparently, this is needed under
329 // Win9x (it doesn't behave correctly if it's passed 0 mask)
330 bmpMask
.Create(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
333 dcMem
.SelectObject(bmpMask
);
335 dcMem
.SelectObject(wxNullBitmap
);