]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
Replaced some images
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "imaglist.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if defined(__WIN95__)
32
33 #ifndef WX_PRECOMP
34 #include "wx/window.h"
35 #include "wx/icon.h"
36 #include "wx/dc.h"
37 #include "wx/string.h"
38 #include "wx/dcmemory.h"
39
40 #include <stdio.h>
41 #endif
42
43 #include "wx/log.h"
44 #include "wx/intl.h"
45 #include "wx/image.h"
46
47 #include "wx/msw/imaglist.h"
48 #include "wx/msw/private.h"
49
50 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
51 #include <commctrl.h>
52 #endif
53
54 // ----------------------------------------------------------------------------
55 // wxWin macros
56 // ----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
59
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)
68 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
69
70 // ============================================================================
71 // implementation
72 // ============================================================================
73
74 // ----------------------------------------------------------------------------
75 // wxImageList creation/destruction
76 // ----------------------------------------------------------------------------
77
78 wxImageList::wxImageList()
79 {
80 m_hImageList = 0;
81 }
82
83 // Creates an image list
84 bool wxImageList::Create(int width, int height, bool mask, int initial)
85 {
86 UINT flags = 0;
87
88 // set appropriate color depth
89 #ifdef __WXWINCE__
90 flags |= ILC_COLOR;
91 #else
92 int dd = wxDisplayDepth();
93
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
99 #endif
100
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 {
109 wxLogLastError(wxT("ImageList_Create()"));
110 }
111
112 return m_hImageList != 0;
113 }
114
115 wxImageList::~wxImageList()
116 {
117 if ( m_hImageList )
118 {
119 ImageList_Destroy(GetHImageList());
120 m_hImageList = 0;
121 }
122 }
123
124 // ----------------------------------------------------------------------------
125 // wxImageList attributes
126 // ----------------------------------------------------------------------------
127
128 // Returns the number of images in the image list.
129 int wxImageList::GetImageCount() const
130 {
131 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
132
133 return ImageList_GetImageCount(GetHImageList());
134 }
135
136 // Returns the size (same for all images) of the images in the list
137 bool 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
144 // ----------------------------------------------------------------------------
145 // wxImageList operations
146 // ----------------------------------------------------------------------------
147
148 // Adds a bitmap, and optionally a mask bitmap.
149 // Note that wxImageList creates new bitmaps, so you may delete
150 // 'bitmap' and 'mask'.
151 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
152 {
153 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
154
155 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
156 if ( index == -1 )
157 {
158 wxLogError(_("Couldn't add an image to the image list."));
159 }
160
161 ::DeleteObject(hbmpMask);
162
163 return index;
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'.
169 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
170 {
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;
180 }
181
182 // Adds a bitmap and mask from an icon.
183 int wxImageList::Add(const wxIcon& icon)
184 {
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;
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'.
197 bool wxImageList::Replace(int index,
198 const wxBitmap& bitmap, const wxBitmap& mask)
199 {
200 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
201
202 bool ok = ImageList_Replace(GetHImageList(), index,
203 GetHbitmapOf(bitmap), hbmpMask) != 0;
204 if ( !ok )
205 {
206 wxLogLastError(wxT("ImageList_Add()"));
207 }
208
209 ::DeleteObject(hbmpMask);
210
211 return ok;
212 }
213
214 // Replaces a bitmap and mask from an icon.
215 bool wxImageList::Replace(int i, const wxIcon& icon)
216 {
217 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
218 if ( !ok )
219 {
220 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
221 }
222
223 return ok;
224 }
225
226 // Removes the image at the given index.
227 bool wxImageList::Remove(int index)
228 {
229 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
230 if ( !ok )
231 {
232 wxLogLastError(wxT("ImageList_Remove()"));
233 }
234
235 return ok;
236 }
237
238 // Remove all images
239 bool wxImageList::RemoveAll()
240 {
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++ )
244 {
245 // the image indexes are shifted, so we should always remove the first
246 // one
247 (void)Remove(0);
248 }
249
250 return true;
251 }
252
253 // Draws the given image on a dc at the specified position.
254 // If 'solidBackground' is true, Draw sets the image list background
255 // colour to the background colour of the wxDC, to speed up
256 // drawing by eliminating masked drawing where possible.
257 bool wxImageList::Draw(int index,
258 wxDC& dc,
259 int x, int y,
260 int flags,
261 bool solidBackground)
262 {
263 HDC hDC = GetHdcOf(dc);
264 wxCHECK_MSG( hDC, false, _T("invalid wxDC in wxImageList::Draw") );
265
266 COLORREF clr = CLR_NONE; // transparent by default
267 if ( solidBackground )
268 {
269 const wxBrush& brush = dc.GetBackground();
270 if ( brush.Ok() )
271 {
272 clr = wxColourToRGB(brush.GetColour());
273 }
274 }
275
276 ImageList_SetBkColor(GetHImageList(), clr);
277
278 UINT style = 0;
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 {
291 wxLogLastError(wxT("ImageList_Draw()"));
292 }
293
294 return ok;
295 }
296
297 // Get the bitmap
298 wxBitmap 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
335 wxIcon 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
353 // ----------------------------------------------------------------------------
354 // helpers
355 // ----------------------------------------------------------------------------
356
357 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
358 {
359 HBITMAP hbmpMask;
360 wxMask *pMask;
361 bool deleteMask = false;
362
363 if ( mask.Ok() )
364 {
365 hbmpMask = GetHbitmapOf(mask);
366 pMask = NULL;
367 }
368 else
369 {
370 pMask = bitmap.GetMask();
371 if ( !pMask )
372 {
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);
379
380 pMask = new wxMask(bitmap, col);
381
382 deleteMask = true;
383 }
384
385 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
386 }
387
388 // windows mask convention is opposite to the wxWidgets one
389 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
390
391 if ( deleteMask )
392 {
393 delete pMask;
394 }
395
396 return hbmpMaskInv;
397 }
398
399 #endif // Win95
400