]> git.saurik.com Git - wxWidgets.git/blame - src/msw/imaglist.cpp
Compile fixes for the const patch
[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
27#if defined(__WIN95__)
28
29#ifndef WX_PRECOMP
4b7f2165
VZ
30 #include "wx/window.h"
31 #include "wx/icon.h"
32 #include "wx/dc.h"
33 #include "wx/string.h"
e0f15f4a 34 #include "wx/dcmemory.h"
4b7f2165
VZ
35
36 #include <stdio.h>
2bda0e17
KB
37#endif
38
ce3ed50d
JS
39#include "wx/log.h"
40#include "wx/intl.h"
49bf4e3e 41#include "wx/image.h"
ce3ed50d 42
2bda0e17
KB
43#include "wx/msw/imaglist.h"
44#include "wx/msw/private.h"
45
b39dbf34 46#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
4b7f2165 47 #include <commctrl.h>
2bda0e17
KB
48#endif
49
4b7f2165
VZ
50// ----------------------------------------------------------------------------
51// wxWin macros
52// ----------------------------------------------------------------------------
53
2bda0e17 54IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
2bda0e17 55
4b7f2165
VZ
56#define GetHImageList() ((HIMAGELIST)m_hImageList)
57
58// ----------------------------------------------------------------------------
59// private functions
60// ----------------------------------------------------------------------------
61
62// returns the mask if it's valid, otherwise the bitmap mask and, if it's not
63// valid neither, a "solid" mask (no transparent zones at all)
3ca6a5f0 64static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
4b7f2165
VZ
65
66// ============================================================================
67// implementation
68// ============================================================================
69
70// ----------------------------------------------------------------------------
71// wxImageList creation/destruction
72// ----------------------------------------------------------------------------
73
74wxImageList::wxImageList()
2bda0e17
KB
75{
76 m_hImageList = 0;
77}
78
4b7f2165
VZ
79// Creates an image list
80bool wxImageList::Create(int width, int height, bool mask, int initial)
2bda0e17 81{
a6e2b3a8
VZ
82 UINT flags = 0;
83
84 // set appropriate color depth
4676948b
JS
85#ifdef __WXWINCE__
86 flags |= ILC_COLOR;
87#else
665b71b1
WS
88 int dd = wxDisplayDepth();
89
59af881e
WS
90 if (dd <= 4) flags |= ILC_COLOR; // 16 color
91 else if (dd <= 8) flags |= ILC_COLOR8; // 256 color
92 else if (dd <= 16) flags |= ILC_COLOR16; // 64k hi-color
93 else if (dd <= 24) flags |= ILC_COLOR24; // 16m truecolor
94 else if (dd <= 32) flags |= ILC_COLOR32; // 16m truecolor
4676948b 95#endif
a6e2b3a8 96
4b7f2165
VZ
97 if ( mask )
98 flags |= ILC_MASK;
99
100 // Grow by 1, I guess this is reasonable behaviour most of the time
101 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
102 initial, 1);
103 if ( !m_hImageList )
104 {
f6bcfd97 105 wxLogLastError(wxT("ImageList_Create()"));
4b7f2165 106 }
2bda0e17 107
4b7f2165
VZ
108 return m_hImageList != 0;
109}
2bda0e17 110
4b7f2165 111wxImageList::~wxImageList()
2bda0e17 112{
4b7f2165
VZ
113 if ( m_hImageList )
114 {
115 ImageList_Destroy(GetHImageList());
116 m_hImageList = 0;
117 }
2bda0e17
KB
118}
119
4b7f2165
VZ
120// ----------------------------------------------------------------------------
121// wxImageList attributes
122// ----------------------------------------------------------------------------
2bda0e17 123
4b7f2165
VZ
124// Returns the number of images in the image list.
125int wxImageList::GetImageCount() const
2bda0e17 126{
4b7f2165 127 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
2bda0e17 128
4b7f2165 129 return ImageList_GetImageCount(GetHImageList());
2bda0e17
KB
130}
131
f6bcfd97
BP
132// Returns the size (same for all images) of the images in the list
133bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const
134{
135 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
136
137 return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0;
138}
139
4b7f2165
VZ
140// ----------------------------------------------------------------------------
141// wxImageList operations
142// ----------------------------------------------------------------------------
143
2bda0e17
KB
144// Adds a bitmap, and optionally a mask bitmap.
145// Note that wxImageList creates new bitmaps, so you may delete
146// 'bitmap' and 'mask'.
147int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
148{
3ca6a5f0 149 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
e29b83a4 150
4b7f2165
VZ
151 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
152 if ( index == -1 )
e29b83a4
VZ
153 {
154 wxLogError(_("Couldn't add an image to the image list."));
155 }
156
4b7f2165 157 ::DeleteObject(hbmpMask);
54b37e2e 158
e29b83a4 159 return index;
2bda0e17
KB
160}
161
162// Adds a bitmap, using the specified colour to create the mask bitmap
163// Note that wxImageList creates new bitmaps, so you may delete
164// 'bitmap'.
165int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
166{
4b7f2165
VZ
167 int index = ImageList_AddMasked(GetHImageList(),
168 GetHbitmapOf(bitmap),
169 wxColourToRGB(maskColour));
170 if ( index == -1 )
171 {
172 wxLogError(_("Couldn't add an image to the image list."));
173 }
174
175 return index;
2bda0e17
KB
176}
177
178// Adds a bitmap and mask from an icon.
179int wxImageList::Add(const wxIcon& icon)
180{
4b7f2165
VZ
181 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
182 if ( index == -1 )
183 {
184 wxLogError(_("Couldn't add an image to the image list."));
185 }
186
187 return index;
2bda0e17
KB
188}
189
190// Replaces a bitmap, optionally passing a mask bitmap.
191// Note that wxImageList creates new bitmaps, so you may delete
192// 'bitmap' and 'mask'.
4b7f2165
VZ
193bool wxImageList::Replace(int index,
194 const wxBitmap& bitmap, const wxBitmap& mask)
2bda0e17 195{
3ca6a5f0 196 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
2bda0e17 197
4b7f2165
VZ
198 bool ok = ImageList_Replace(GetHImageList(), index,
199 GetHbitmapOf(bitmap), hbmpMask) != 0;
200 if ( !ok )
201 {
3103e8a9 202 wxLogLastError(wxT("ImageList_Replace()"));
4b7f2165
VZ
203 }
204
205 ::DeleteObject(hbmpMask);
206
207 return ok;
2bda0e17 208}
2bda0e17
KB
209
210// Replaces a bitmap and mask from an icon.
4b7f2165 211bool wxImageList::Replace(int i, const wxIcon& icon)
2bda0e17 212{
4b7f2165
VZ
213 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
214 if ( !ok )
215 {
f6bcfd97 216 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
4b7f2165
VZ
217 }
218
219 return ok;
2bda0e17
KB
220}
221
222// Removes the image at the given index.
debe6624 223bool wxImageList::Remove(int index)
2bda0e17 224{
4b7f2165
VZ
225 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
226 if ( !ok )
227 {
f6bcfd97 228 wxLogLastError(wxT("ImageList_Remove()"));
4b7f2165
VZ
229 }
230
231 return ok;
2bda0e17
KB
232}
233
234// Remove all images
4b7f2165 235bool wxImageList::RemoveAll()
2bda0e17 236{
3c1a88d8
VZ
237 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
238 int count = ImageList_GetImageCount(GetHImageList());
239 for ( int i = 0; i < count; i++ )
4b7f2165 240 {
3c1a88d8
VZ
241 // the image indexes are shifted, so we should always remove the first
242 // one
243 (void)Remove(0);
4b7f2165
VZ
244 }
245
59af881e 246 return true;
2bda0e17
KB
247}
248
249// Draws the given image on a dc at the specified position.
59af881e 250// If 'solidBackground' is true, Draw sets the image list background
2bda0e17
KB
251// colour to the background colour of the wxDC, to speed up
252// drawing by eliminating masked drawing where possible.
4b7f2165
VZ
253bool wxImageList::Draw(int index,
254 wxDC& dc,
255 int x, int y,
256 int flags,
257 bool solidBackground)
2bda0e17 258{
4b7f2165 259 HDC hDC = GetHdcOf(dc);
59af881e 260 wxCHECK_MSG( hDC, false, _T("invalid wxDC in wxImageList::Draw") );
4b7f2165
VZ
261
262 COLORREF clr = CLR_NONE; // transparent by default
263 if ( solidBackground )
264 {
b33576e1
VZ
265 const wxBrush& brush = dc.GetBackground();
266 if ( brush.Ok() )
4b7f2165 267 {
b33576e1 268 clr = wxColourToRGB(brush.GetColour());
4b7f2165
VZ
269 }
270 }
271
272 ImageList_SetBkColor(GetHImageList(), clr);
2bda0e17
KB
273
274 UINT style = 0;
4b7f2165
VZ
275 if ( flags & wxIMAGELIST_DRAW_NORMAL )
276 style |= ILD_NORMAL;
277 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
278 style |= ILD_TRANSPARENT;
279 if ( flags & wxIMAGELIST_DRAW_SELECTED )
280 style |= ILD_SELECTED;
281 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
282 style |= ILD_FOCUS;
283
284 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
285 if ( !ok )
286 {
f6bcfd97 287 wxLogLastError(wxT("ImageList_Draw()"));
4b7f2165
VZ
288 }
289
290 return ok;
2bda0e17
KB
291}
292
49bf4e3e
JS
293// Get the bitmap
294wxBitmap wxImageList::GetBitmap(int index) const
295{
296 int bmp_width = 0, bmp_height = 0;
297 GetSize(index, bmp_width, bmp_height);
298
299 wxBitmap bitmap(bmp_width, bmp_height);
300 wxMemoryDC dc;
301 dc.SelectObject(bitmap);
302
303 // draw it the first time to find a suitable mask colour
304 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
305 dc.SelectObject(wxNullBitmap);
306
307 // find the suitable mask colour
308 wxImage image = bitmap.ConvertToImage();
309 unsigned char r = 0, g = 0, b = 0;
310 image.FindFirstUnusedColour(&r, &g, &b);
311
312 // redraw whole image and bitmap in the mask colour
313 image.Create(bmp_width, bmp_height);
314 image.Replace(0, 0, 0, r, g, b);
315 bitmap = wxBitmap(image);
316
317 // redraw icon over the mask colour to actually draw it
318 dc.SelectObject(bitmap);
319 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
320 dc.SelectObject(wxNullBitmap);
321
322 // get the image, set the mask colour and convert back to get transparent bitmap
323 image = bitmap.ConvertToImage();
324 image.SetMaskColour(r, g, b);
325 bitmap = wxBitmap(image);
326
327 return bitmap;
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);
338
339 int iconW, iconH;
340 GetSize(index, iconW, iconH);
341 icon.SetSize(iconW, iconH);
342
343 return icon;
344 }
345 else
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
VZ
393}
394
395#endif // Win95
2bda0e17 396