]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
ported KO's v262 change forward
[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
34 #include <stdio.h>
35 #endif
36
37 #include "wx/log.h"
38 #include "wx/intl.h"
39 #include "wx/image.h"
40
41 #include "wx/msw/imaglist.h"
42 #include "wx/msw/private.h"
43
44 // include <commctrl.h> "properly"
45 #include "wx/msw/wrapcctl.h"
46
47 // ----------------------------------------------------------------------------
48 // wxWin macros
49 // ----------------------------------------------------------------------------
50
51 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
52
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)
61 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
62
63 // ============================================================================
64 // implementation
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // wxImageList creation/destruction
69 // ----------------------------------------------------------------------------
70
71 wxImageList::wxImageList()
72 {
73 m_hImageList = 0;
74 }
75
76 // Creates an image list
77 bool wxImageList::Create(int width, int height, bool mask, int initial)
78 {
79 UINT flags = 0;
80
81 // set appropriate color depth
82 #ifdef __WXWINCE__
83 flags |= ILC_COLOR;
84 #else
85 int dd = wxDisplayDepth();
86
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
92 #endif
93
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 {
102 wxLogLastError(wxT("ImageList_Create()"));
103 }
104
105 return m_hImageList != 0;
106 }
107
108 wxImageList::~wxImageList()
109 {
110 if ( m_hImageList )
111 {
112 ImageList_Destroy(GetHImageList());
113 m_hImageList = 0;
114 }
115 }
116
117 // ----------------------------------------------------------------------------
118 // wxImageList attributes
119 // ----------------------------------------------------------------------------
120
121 // Returns the number of images in the image list.
122 int wxImageList::GetImageCount() const
123 {
124 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
125
126 return ImageList_GetImageCount(GetHImageList());
127 }
128
129 // Returns the size (same for all images) of the images in the list
130 bool 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
137 // ----------------------------------------------------------------------------
138 // wxImageList operations
139 // ----------------------------------------------------------------------------
140
141 // Adds a bitmap, and optionally a mask bitmap.
142 // Note that wxImageList creates new bitmaps, so you may delete
143 // 'bitmap' and 'mask'.
144 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
145 {
146 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
147
148 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
149 if ( index == -1 )
150 {
151 wxLogError(_("Couldn't add an image to the image list."));
152 }
153
154 ::DeleteObject(hbmpMask);
155
156 return index;
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'.
162 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
163 {
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;
173 }
174
175 // Adds a bitmap and mask from an icon.
176 int wxImageList::Add(const wxIcon& icon)
177 {
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;
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'.
190 bool wxImageList::Replace(int index,
191 const wxBitmap& bitmap, const wxBitmap& mask)
192 {
193 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
194
195 bool ok = ImageList_Replace(GetHImageList(), index,
196 GetHbitmapOf(bitmap), hbmpMask) != 0;
197 if ( !ok )
198 {
199 wxLogLastError(wxT("ImageList_Replace()"));
200 }
201
202 ::DeleteObject(hbmpMask);
203
204 return ok;
205 }
206
207 // Replaces a bitmap and mask from an icon.
208 bool wxImageList::Replace(int i, const wxIcon& icon)
209 {
210 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != -1;
211 if ( !ok )
212 {
213 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
214 }
215
216 return ok;
217 }
218
219 // Removes the image at the given index.
220 bool wxImageList::Remove(int index)
221 {
222 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
223 if ( !ok )
224 {
225 wxLogLastError(wxT("ImageList_Remove()"));
226 }
227
228 return ok;
229 }
230
231 // Remove all images
232 bool wxImageList::RemoveAll()
233 {
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++ )
237 {
238 // the image indexes are shifted, so we should always remove the first
239 // one
240 (void)Remove(0);
241 }
242
243 return true;
244 }
245
246 // Draws the given image on a dc at the specified position.
247 // If 'solidBackground' is true, Draw sets the image list background
248 // colour to the background colour of the wxDC, to speed up
249 // drawing by eliminating masked drawing where possible.
250 bool wxImageList::Draw(int index,
251 wxDC& dc,
252 int x, int y,
253 int flags,
254 bool solidBackground)
255 {
256 HDC hDC = GetHdcOf(dc);
257 wxCHECK_MSG( hDC, false, _T("invalid wxDC in wxImageList::Draw") );
258
259 COLORREF clr = CLR_NONE; // transparent by default
260 if ( solidBackground )
261 {
262 const wxBrush& brush = dc.GetBackground();
263 if ( brush.Ok() )
264 {
265 clr = wxColourToRGB(brush.GetColour());
266 }
267 }
268
269 ImageList_SetBkColor(GetHImageList(), clr);
270
271 UINT style = 0;
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 {
284 wxLogLastError(wxT("ImageList_Draw()"));
285 }
286
287 return ok;
288 }
289
290 // Get the bitmap
291 wxBitmap wxImageList::GetBitmap(int index) const
292 {
293 int bmp_width = 0, bmp_height = 0;
294 GetSize(index, bmp_width, bmp_height);
295
296 wxBitmap bitmap(bmp_width, bmp_height);
297 wxMemoryDC dc;
298 dc.SelectObject(bitmap);
299
300 // draw it the first time to find a suitable mask colour
301 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
302 dc.SelectObject(wxNullBitmap);
303
304 // find the suitable mask colour
305 wxImage image = bitmap.ConvertToImage();
306 unsigned char r = 0, g = 0, b = 0;
307 image.FindFirstUnusedColour(&r, &g, &b);
308
309 // redraw whole image and bitmap in the mask colour
310 image.Create(bmp_width, bmp_height);
311 image.Replace(0, 0, 0, r, g, b);
312 bitmap = wxBitmap(image);
313
314 // redraw icon over the mask colour to actually draw it
315 dc.SelectObject(bitmap);
316 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
317 dc.SelectObject(wxNullBitmap);
318
319 // get the image, set the mask colour and convert back to get transparent bitmap
320 image = bitmap.ConvertToImage();
321 image.SetMaskColour(r, g, b);
322 bitmap = wxBitmap(image);
323
324 return bitmap;
325 }
326
327 // Get the icon
328 wxIcon wxImageList::GetIcon(int index) const
329 {
330 HICON hIcon = ImageList_ExtractIcon(0, GetHImageList(), index);
331 if (hIcon)
332 {
333 wxIcon icon;
334 icon.SetHICON((WXHICON)hIcon);
335
336 int iconW, iconH;
337 GetSize(index, iconW, iconH);
338 icon.SetSize(iconW, iconH);
339
340 return icon;
341 }
342 else
343 return wxNullIcon;
344 }
345
346 // ----------------------------------------------------------------------------
347 // helpers
348 // ----------------------------------------------------------------------------
349
350 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
351 {
352 HBITMAP hbmpMask;
353 wxMask *pMask;
354 bool deleteMask = false;
355
356 if ( mask.Ok() )
357 {
358 hbmpMask = GetHbitmapOf(mask);
359 pMask = NULL;
360 }
361 else
362 {
363 pMask = bitmap.GetMask();
364 if ( !pMask )
365 {
366 // use the light grey count as transparent: the trouble here is
367 // that the light grey might have been changed by Windows behind
368 // our back, so use the standard colour map to get its real value
369 wxCOLORMAP *cmap = wxGetStdColourMap();
370 wxColour col;
371 wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
372
373 pMask = new wxMask(bitmap, col);
374
375 deleteMask = true;
376 }
377
378 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
379 }
380
381 // windows mask convention is opposite to the wxWidgets one
382 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
383
384 if ( deleteMask )
385 {
386 delete pMask;
387 }
388
389 return hbmpMaskInv;
390 }