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