]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
don't compile DirectDraw code when wxUSE_DIRECTDRAW==0 (patch 1461271); set wxUSE_DIR...
[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 #if wxUSE_WXDIB
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);
324 #else
325 wxBitmap bitmap;
326 #endif
327 return bitmap;
328 }
329
330 // Get the icon
331 wxIcon 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
349 // ----------------------------------------------------------------------------
350 // helpers
351 // ----------------------------------------------------------------------------
352
353 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
354 {
355 HBITMAP hbmpMask;
356 wxMask *pMask;
357 bool deleteMask = false;
358
359 if ( mask.Ok() )
360 {
361 hbmpMask = GetHbitmapOf(mask);
362 pMask = NULL;
363 }
364 else
365 {
366 pMask = bitmap.GetMask();
367 if ( !pMask )
368 {
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);
375
376 pMask = new wxMask(bitmap, col);
377
378 deleteMask = true;
379 }
380
381 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
382 }
383
384 // windows mask convention is opposite to the wxWidgets one
385 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
386
387 if ( deleteMask )
388 {
389 delete pMask;
390 }
391
392 return hbmpMaskInv;
393 }