]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
Forward port 2.8 fix.
[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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
29 #include "wx/window.h"
30 #include "wx/icon.h"
31 #include "wx/dc.h"
32 #include "wx/string.h"
33 #include "wx/dcmemory.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/image.h"
37 #include <stdio.h>
38 #endif
39
40 #include "wx/imaglist.h"
41 #include "wx/dc.h"
42 #include "wx/msw/dc.h"
43 #include "wx/msw/private.h"
44
45 // ----------------------------------------------------------------------------
46 // wxWin macros
47 // ----------------------------------------------------------------------------
48
49 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
50
51 #define GetHImageList() ((HIMAGELIST)m_hImageList)
52
53 // ----------------------------------------------------------------------------
54 // private functions
55 // ----------------------------------------------------------------------------
56
57 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
58 // valid neither, a "solid" mask (no transparent zones at all)
59 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // wxImageList creation/destruction
67 // ----------------------------------------------------------------------------
68
69 wxImageList::wxImageList()
70 {
71 m_hImageList = 0;
72 }
73
74 // Creates an image list
75 bool wxImageList::Create(int width, int height, bool mask, int initial)
76 {
77 UINT flags = 0;
78
79 // as we want to be able to use 32bpp bitmaps in the image lists, we always
80 // use ILC_COLOR32, even if the display resolution is less -- the system
81 // will make the best effort to show the bitmap if we do this resulting in
82 // quite acceptable display while using a lower depth ILC_COLOR constant
83 // (e.g. ILC_COLOR16) shows completely broken bitmaps
84 #ifdef __WXWINCE__
85 flags |= ILC_COLOR;
86 #else
87 flags |= ILC_COLOR32;
88 #endif
89
90 if ( mask )
91 flags |= ILC_MASK;
92
93 // Grow by 1, I guess this is reasonable behaviour most of the time
94 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
95 initial, 1);
96 if ( !m_hImageList )
97 {
98 wxLogLastError(wxT("ImageList_Create()"));
99 }
100
101 return m_hImageList != 0;
102 }
103
104 wxImageList::~wxImageList()
105 {
106 if ( m_hImageList )
107 {
108 ImageList_Destroy(GetHImageList());
109 m_hImageList = 0;
110 }
111 }
112
113 // ----------------------------------------------------------------------------
114 // wxImageList attributes
115 // ----------------------------------------------------------------------------
116
117 // Returns the number of images in the image list.
118 int wxImageList::GetImageCount() const
119 {
120 wxASSERT_MSG( m_hImageList, wxT("invalid image list") );
121
122 return ImageList_GetImageCount(GetHImageList());
123 }
124
125 // Returns the size (same for all images) of the images in the list
126 bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const
127 {
128 wxASSERT_MSG( m_hImageList, wxT("invalid image list") );
129
130 return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0;
131 }
132
133 // ----------------------------------------------------------------------------
134 // wxImageList operations
135 // ----------------------------------------------------------------------------
136
137 // Adds a bitmap, and optionally a mask bitmap.
138 // Note that wxImageList creates new bitmaps, so you may delete
139 // 'bitmap' and 'mask'.
140 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
141 {
142 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
143
144 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
145 if ( index == -1 )
146 {
147 wxLogError(_("Couldn't add an image to the image list."));
148 }
149
150 ::DeleteObject(hbmpMask);
151
152 return index;
153 }
154
155 // Adds a bitmap, using the specified colour to create the mask bitmap
156 // Note that wxImageList creates new bitmaps, so you may delete
157 // 'bitmap'.
158 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
159 {
160 int index = ImageList_AddMasked(GetHImageList(),
161 GetHbitmapOf(bitmap),
162 wxColourToRGB(maskColour));
163 if ( index == -1 )
164 {
165 wxLogError(_("Couldn't add an image to the image list."));
166 }
167
168 return index;
169 }
170
171 // Adds a bitmap and mask from an icon.
172 int wxImageList::Add(const wxIcon& icon)
173 {
174 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
175 if ( index == -1 )
176 {
177 wxLogError(_("Couldn't add an image to the image list."));
178 }
179
180 return index;
181 }
182
183 // Replaces a bitmap, optionally passing a mask bitmap.
184 // Note that wxImageList creates new bitmaps, so you may delete
185 // 'bitmap' and 'mask'.
186 bool wxImageList::Replace(int index,
187 const wxBitmap& bitmap, const wxBitmap& mask)
188 {
189 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
190
191 bool ok = ImageList_Replace(GetHImageList(), index,
192 GetHbitmapOf(bitmap), hbmpMask) != 0;
193 if ( !ok )
194 {
195 wxLogLastError(wxT("ImageList_Replace()"));
196 }
197
198 ::DeleteObject(hbmpMask);
199
200 return ok;
201 }
202
203 // Replaces a bitmap and mask from an icon.
204 bool wxImageList::Replace(int i, const wxIcon& icon)
205 {
206 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != -1;
207 if ( !ok )
208 {
209 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
210 }
211
212 return ok;
213 }
214
215 // Removes the image at the given index.
216 bool wxImageList::Remove(int index)
217 {
218 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
219 if ( !ok )
220 {
221 wxLogLastError(wxT("ImageList_Remove()"));
222 }
223
224 return ok;
225 }
226
227 // Remove all images
228 bool wxImageList::RemoveAll()
229 {
230 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
231 return Remove(-1);
232 }
233
234 // Draws the given image on a dc at the specified position.
235 // If 'solidBackground' is true, Draw sets the image list background
236 // colour to the background colour of the wxDC, to speed up
237 // drawing by eliminating masked drawing where possible.
238 bool wxImageList::Draw(int index,
239 wxDC& dc,
240 int x, int y,
241 int flags,
242 bool solidBackground)
243 {
244 wxDCImpl *impl = dc.GetImpl();
245 wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl );
246 if (!msw_impl)
247 return false;
248
249 HDC hDC = GetHdcOf(*msw_impl);
250 wxCHECK_MSG( hDC, false, wxT("invalid wxDC in wxImageList::Draw") );
251
252 COLORREF clr = CLR_NONE; // transparent by default
253 if ( solidBackground )
254 {
255 const wxBrush& brush = dc.GetBackground();
256 if ( brush.Ok() )
257 {
258 clr = wxColourToRGB(brush.GetColour());
259 }
260 }
261
262 ImageList_SetBkColor(GetHImageList(), clr);
263
264 UINT style = 0;
265 if ( flags & wxIMAGELIST_DRAW_NORMAL )
266 style |= ILD_NORMAL;
267 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
268 style |= ILD_TRANSPARENT;
269 if ( flags & wxIMAGELIST_DRAW_SELECTED )
270 style |= ILD_SELECTED;
271 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
272 style |= ILD_FOCUS;
273
274 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
275 if ( !ok )
276 {
277 wxLogLastError(wxT("ImageList_Draw()"));
278 }
279
280 return ok;
281 }
282
283 // Get the bitmap
284 wxBitmap wxImageList::GetBitmap(int index) const
285 {
286 #if wxUSE_WXDIB && wxUSE_IMAGE
287 int bmp_width = 0, bmp_height = 0;
288 GetSize(index, bmp_width, bmp_height);
289
290 wxBitmap bitmap(bmp_width, bmp_height);
291 wxMemoryDC dc;
292 dc.SelectObject(bitmap);
293
294 // draw it the first time to find a suitable mask colour
295 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
296 dc.SelectObject(wxNullBitmap);
297
298 // find the suitable mask colour
299 wxImage image = bitmap.ConvertToImage();
300 unsigned char r = 0, g = 0, b = 0;
301 image.FindFirstUnusedColour(&r, &g, &b);
302
303 // redraw whole image and bitmap in the mask colour
304 image.Create(bmp_width, bmp_height);
305 image.Replace(0, 0, 0, r, g, b);
306 bitmap = wxBitmap(image);
307
308 // redraw icon over the mask colour to actually draw it
309 dc.SelectObject(bitmap);
310 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
311 dc.SelectObject(wxNullBitmap);
312
313 // get the image, set the mask colour and convert back to get transparent bitmap
314 image = bitmap.ConvertToImage();
315 image.SetMaskColour(r, g, b);
316 bitmap = wxBitmap(image);
317 #else
318 wxBitmap bitmap;
319 #endif
320 return bitmap;
321 }
322
323 // Get the icon
324 wxIcon wxImageList::GetIcon(int index) const
325 {
326 HICON hIcon = ImageList_ExtractIcon(0, GetHImageList(), index);
327 if (hIcon)
328 {
329 wxIcon icon;
330 icon.SetHICON((WXHICON)hIcon);
331
332 int iconW, iconH;
333 GetSize(index, iconW, iconH);
334 icon.SetSize(iconW, iconH);
335
336 return icon;
337 }
338 else
339 return wxNullIcon;
340 }
341
342 // ----------------------------------------------------------------------------
343 // helpers
344 // ----------------------------------------------------------------------------
345
346 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
347 {
348 #if wxUSE_IMAGE
349 wxBitmap bitmapWithMask;
350 #endif // wxUSE_IMAGE
351
352 HBITMAP hbmpMask;
353 wxMask *pMask;
354 bool deleteMask = false;
355
356 if ( mask.Ok() )
357 {
358 hbmpMask = GetHbitmapOf(mask);
359 pMask = NULL;
360 }
361 else
362 {
363 pMask = bitmap.GetMask();
364
365 #if wxUSE_IMAGE
366 // check if we don't have alpha in this bitmap -- we can create a mask
367 // from it (and we need to do it for the older systems which don't
368 // support 32bpp bitmaps natively)
369 if ( !pMask )
370 {
371 wxImage img(bitmap.ConvertToImage());
372 if ( img.HasAlpha() )
373 {
374 img.ConvertAlphaToMask();
375 bitmapWithMask = wxBitmap(img);
376 pMask = bitmapWithMask.GetMask();
377 }
378 }
379 #endif // wxUSE_IMAGE
380
381 if ( !pMask )
382 {
383 // use the light grey count as transparent: the trouble here is
384 // that the light grey might have been changed by Windows behind
385 // our back, so use the standard colour map to get its real value
386 wxCOLORMAP *cmap = wxGetStdColourMap();
387 wxColour col;
388 wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
389
390 pMask = new wxMask(bitmap, col);
391
392 deleteMask = true;
393 }
394
395 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
396 }
397
398 // windows mask convention is opposite to the wxWidgets one
399 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
400
401 if ( deleteMask )
402 {
403 delete pMask;
404 }
405
406 return hbmpMaskInv;
407 }