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