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