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