1. fixed (to test) the bug with bitmaps without masks in wxImageList
[wxWidgets.git] / src / msw / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/imaglist.cpp
3 // Purpose: wxImageList implementation for Win32
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "imaglist.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if defined(__WIN95__)
32
33 #ifndef WX_PRECOMP
34 #include "wx/window.h"
35 #include "wx/icon.h"
36 #include "wx/dc.h"
37 #include "wx/string.h"
38
39 #include <stdio.h>
40 #endif
41
42 #include "wx/log.h"
43 #include "wx/intl.h"
44
45 #include "wx/msw/imaglist.h"
46 #include "wx/msw/private.h"
47
48 #if !defined(__GNUWIN32_OLD__) && !defined(__TWIN32__)
49 #include <commctrl.h>
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // wxWin macros
54 // ----------------------------------------------------------------------------
55
56 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
57
58 #define GetHImageList() ((HIMAGELIST)m_hImageList)
59
60 // ----------------------------------------------------------------------------
61 // private functions
62 // ----------------------------------------------------------------------------
63
64 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
65 // valid neither, a "solid" mask (no transparent zones at all)
66 static wxBitmap GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
67
68 // ============================================================================
69 // implementation
70 // ============================================================================
71
72 // ----------------------------------------------------------------------------
73 // wxImageList creation/destruction
74 // ----------------------------------------------------------------------------
75
76 wxImageList::wxImageList()
77 {
78 m_hImageList = 0;
79 }
80
81 // Creates an image list
82 bool wxImageList::Create(int width, int height, bool mask, int initial)
83 {
84 UINT flags = 0; // TODO shouldallow to specify ILC_COLORxxx here
85 if ( mask )
86 flags |= ILC_MASK;
87
88 // Grow by 1, I guess this is reasonable behaviour most of the time
89 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
90 initial, 1);
91 if ( !m_hImageList )
92 {
93 wxLogLastError("ImageList_Create()");
94 }
95
96 return m_hImageList != 0;
97 }
98
99 wxImageList::~wxImageList()
100 {
101 if ( m_hImageList )
102 {
103 ImageList_Destroy(GetHImageList());
104 m_hImageList = 0;
105 }
106 }
107
108 // ----------------------------------------------------------------------------
109 // wxImageList attributes
110 // ----------------------------------------------------------------------------
111
112 // Returns the number of images in the image list.
113 int wxImageList::GetImageCount() const
114 {
115 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
116
117 return ImageList_GetImageCount(GetHImageList());
118 }
119
120 // ----------------------------------------------------------------------------
121 // wxImageList operations
122 // ----------------------------------------------------------------------------
123
124 // Adds a bitmap, and optionally a mask bitmap.
125 // Note that wxImageList creates new bitmaps, so you may delete
126 // 'bitmap' and 'mask'.
127 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
128 {
129 wxBitmap bmpMask = GetMaskForImage(bitmap, mask);
130 HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask));
131
132 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
133 if ( index == -1 )
134 {
135 wxLogError(_("Couldn't add an image to the image list."));
136 }
137
138 ::DeleteObject(hbmpMask);
139
140 return index;
141 }
142
143 // Adds a bitmap, using the specified colour to create the mask bitmap
144 // Note that wxImageList creates new bitmaps, so you may delete
145 // 'bitmap'.
146 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
147 {
148 #ifdef __TWIN32__
149 wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32"));
150 return -1;
151 #else
152 int index = ImageList_AddMasked(GetHImageList(),
153 GetHbitmapOf(bitmap),
154 wxColourToRGB(maskColour));
155 if ( index == -1 )
156 {
157 wxLogError(_("Couldn't add an image to the image list."));
158 }
159
160 return index;
161 #endif
162 }
163
164 // Adds a bitmap and mask from an icon.
165 int wxImageList::Add(const wxIcon& icon)
166 {
167 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
168 if ( index == -1 )
169 {
170 wxLogError(_("Couldn't add an image to the image list."));
171 }
172
173 return index;
174 }
175
176 // Replaces a bitmap, optionally passing a mask bitmap.
177 // Note that wxImageList creates new bitmaps, so you may delete
178 // 'bitmap' and 'mask'.
179 bool wxImageList::Replace(int index,
180 const wxBitmap& bitmap, const wxBitmap& mask)
181 {
182 #ifdef __TWIN32__
183 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
184 return FALSE;
185 #else
186 wxBitmap bmpMask = GetMaskForImage(bitmap, mask);
187 HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask));
188
189 bool ok = ImageList_Replace(GetHImageList(), index,
190 GetHbitmapOf(bitmap), hbmpMask) != 0;
191 if ( !ok )
192 {
193 wxLogLastError("ImageList_Add()");
194 }
195
196 ::DeleteObject(hbmpMask);
197
198 return ok;
199 #endif
200 }
201
202 // Replaces a bitmap and mask from an icon.
203 bool wxImageList::Replace(int i, const wxIcon& icon)
204 {
205 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
206 if ( !ok )
207 {
208 wxLogLastError("ImageList_ReplaceIcon()");
209 }
210
211 return ok;
212 }
213
214 // Removes the image at the given index.
215 bool wxImageList::Remove(int index)
216 {
217 #ifdef __TWIN32__
218 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
219 return FALSE;
220 #else
221 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
222 if ( !ok )
223 {
224 wxLogLastError("ImageList_Remove()");
225 }
226
227 return ok;
228 #endif
229 }
230
231 // Remove all images
232 bool wxImageList::RemoveAll()
233 {
234 bool ok = ImageList_RemoveAll(GetHImageList()) != 0;
235 if ( !ok )
236 {
237 wxLogLastError("ImageList_RemoveAll()");
238 }
239
240 return ok;
241 }
242
243 // Draws the given image on a dc at the specified position.
244 // If 'solidBackground' is TRUE, Draw sets the image list background
245 // colour to the background colour of the wxDC, to speed up
246 // drawing by eliminating masked drawing where possible.
247 bool wxImageList::Draw(int index,
248 wxDC& dc,
249 int x, int y,
250 int flags,
251 bool solidBackground)
252 {
253 #ifdef __TWIN32__
254 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
255 return FALSE;
256 #else
257 HDC hDC = GetHdcOf(dc);
258 wxCHECK_MSG( hDC, FALSE, _T("invalid wxDC in wxImageList::Draw") );
259
260 COLORREF clr = CLR_NONE; // transparent by default
261 if ( solidBackground )
262 {
263 wxBrush *brush = & dc.GetBackground();
264 if ( brush && brush->Ok() )
265 {
266 clr = wxColourToRGB(brush->GetColour());
267 }
268 }
269
270 ImageList_SetBkColor(GetHImageList(), clr);
271
272 UINT style = 0;
273 if ( flags & wxIMAGELIST_DRAW_NORMAL )
274 style |= ILD_NORMAL;
275 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
276 style |= ILD_TRANSPARENT;
277 if ( flags & wxIMAGELIST_DRAW_SELECTED )
278 style |= ILD_SELECTED;
279 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
280 style |= ILD_FOCUS;
281
282 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
283 if ( !ok )
284 {
285 wxLogLastError("ImageList_Draw()");
286 }
287
288 return ok;
289 #endif
290 }
291
292 // ----------------------------------------------------------------------------
293 // helpers
294 // ----------------------------------------------------------------------------
295
296 static wxBitmap GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
297 {
298 wxBitmap bmpMask;
299
300 if ( mask.Ok() )
301 {
302 bmpMask = mask;
303 }
304 else
305 {
306 wxMask *pMask = bitmap.GetMask();
307 if ( pMask )
308 {
309 bmpMask.SetHBITMAP(pMask->GetMaskBitmap());
310 }
311 }
312
313 if ( !bmpMask.Ok() )
314 {
315 // create a non transparent mask - apparently, this is needed under
316 // Win9x (it doesn't behave correctly if it's passed 0 mask)
317 bmpMask.Create(bitmap.GetWidth(), bitmap.GetHeight(), 1);
318
319 wxMemoryDC dcMem;
320 dcMem.SelectObject(bmpMask);
321 dcMem.Clear();
322 dcMem.SelectObject(wxNullBitmap);
323 }
324
325 return bmpMask;
326 }
327
328 #endif // Win95
329