]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
fixed wide char codeset detection for systems which do support LE/BE variants (broken...
[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 #if defined(__WIN95__)
28
29 #ifndef WX_PRECOMP
30 #include "wx/window.h"
31 #include "wx/icon.h"
32 #include "wx/dc.h"
33 #include "wx/string.h"
34 #include "wx/dcmemory.h"
35
36 #include <stdio.h>
37 #endif
38
39 #include "wx/log.h"
40 #include "wx/intl.h"
41 #include "wx/image.h"
42
43 #include "wx/msw/imaglist.h"
44 #include "wx/msw/private.h"
45
46 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
47 #include <commctrl.h>
48 #endif
49
50 // ----------------------------------------------------------------------------
51 // wxWin macros
52 // ----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
55
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)
64 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
65
66 // ============================================================================
67 // implementation
68 // ============================================================================
69
70 // ----------------------------------------------------------------------------
71 // wxImageList creation/destruction
72 // ----------------------------------------------------------------------------
73
74 wxImageList::wxImageList()
75 {
76 m_hImageList = 0;
77 }
78
79 // Creates an image list
80 bool wxImageList::Create(int width, int height, bool mask, int initial)
81 {
82 UINT flags = 0;
83
84 // set appropriate color depth
85 #ifdef __WXWINCE__
86 flags |= ILC_COLOR;
87 #else
88 int dd = wxDisplayDepth();
89
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
95 #endif
96
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 {
105 wxLogLastError(wxT("ImageList_Create()"));
106 }
107
108 return m_hImageList != 0;
109 }
110
111 wxImageList::~wxImageList()
112 {
113 if ( m_hImageList )
114 {
115 ImageList_Destroy(GetHImageList());
116 m_hImageList = 0;
117 }
118 }
119
120 // ----------------------------------------------------------------------------
121 // wxImageList attributes
122 // ----------------------------------------------------------------------------
123
124 // Returns the number of images in the image list.
125 int wxImageList::GetImageCount() const
126 {
127 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
128
129 return ImageList_GetImageCount(GetHImageList());
130 }
131
132 // Returns the size (same for all images) of the images in the list
133 bool 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
140 // ----------------------------------------------------------------------------
141 // wxImageList operations
142 // ----------------------------------------------------------------------------
143
144 // Adds a bitmap, and optionally a mask bitmap.
145 // Note that wxImageList creates new bitmaps, so you may delete
146 // 'bitmap' and 'mask'.
147 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
148 {
149 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
150
151 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
152 if ( index == -1 )
153 {
154 wxLogError(_("Couldn't add an image to the image list."));
155 }
156
157 ::DeleteObject(hbmpMask);
158
159 return index;
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'.
165 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
166 {
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;
176 }
177
178 // Adds a bitmap and mask from an icon.
179 int wxImageList::Add(const wxIcon& icon)
180 {
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;
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'.
193 bool wxImageList::Replace(int index,
194 const wxBitmap& bitmap, const wxBitmap& mask)
195 {
196 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
197
198 bool ok = ImageList_Replace(GetHImageList(), index,
199 GetHbitmapOf(bitmap), hbmpMask) != 0;
200 if ( !ok )
201 {
202 wxLogLastError(wxT("ImageList_Replace()"));
203 }
204
205 ::DeleteObject(hbmpMask);
206
207 return ok;
208 }
209
210 // Replaces a bitmap and mask from an icon.
211 bool wxImageList::Replace(int i, const wxIcon& icon)
212 {
213 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
214 if ( !ok )
215 {
216 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
217 }
218
219 return ok;
220 }
221
222 // Removes the image at the given index.
223 bool wxImageList::Remove(int index)
224 {
225 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
226 if ( !ok )
227 {
228 wxLogLastError(wxT("ImageList_Remove()"));
229 }
230
231 return ok;
232 }
233
234 // Remove all images
235 bool wxImageList::RemoveAll()
236 {
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++ )
240 {
241 // the image indexes are shifted, so we should always remove the first
242 // one
243 (void)Remove(0);
244 }
245
246 return true;
247 }
248
249 // Draws the given image on a dc at the specified position.
250 // If 'solidBackground' is true, Draw sets the image list background
251 // colour to the background colour of the wxDC, to speed up
252 // drawing by eliminating masked drawing where possible.
253 bool wxImageList::Draw(int index,
254 wxDC& dc,
255 int x, int y,
256 int flags,
257 bool solidBackground)
258 {
259 HDC hDC = GetHdcOf(dc);
260 wxCHECK_MSG( hDC, false, _T("invalid wxDC in wxImageList::Draw") );
261
262 COLORREF clr = CLR_NONE; // transparent by default
263 if ( solidBackground )
264 {
265 const wxBrush& brush = dc.GetBackground();
266 if ( brush.Ok() )
267 {
268 clr = wxColourToRGB(brush.GetColour());
269 }
270 }
271
272 ImageList_SetBkColor(GetHImageList(), clr);
273
274 UINT style = 0;
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 {
287 wxLogLastError(wxT("ImageList_Draw()"));
288 }
289
290 return ok;
291 }
292
293 // Get the bitmap
294 wxBitmap 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
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 }
394
395 #endif // Win95
396