]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
4cd16e46c2ae1ed67645240178ff2408ffd0cfa1
[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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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
46 #include "wx/msw/imaglist.h"
47 #include "wx/msw/private.h"
48
49 #if !defined(__GNUWIN32_OLD__) && !defined(__TWIN32__)
50 #include <commctrl.h>
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // wxWin macros
55 // ----------------------------------------------------------------------------
56
57 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
58
59 #define GetHImageList() ((HIMAGELIST)m_hImageList)
60
61 // ----------------------------------------------------------------------------
62 // private functions
63 // ----------------------------------------------------------------------------
64
65 // returns the mask if it's valid, otherwise the bitmap mask and, if it's not
66 // valid neither, a "solid" mask (no transparent zones at all)
67 static wxBitmap GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
68
69 // ============================================================================
70 // implementation
71 // ============================================================================
72
73 // ----------------------------------------------------------------------------
74 // wxImageList creation/destruction
75 // ----------------------------------------------------------------------------
76
77 wxImageList::wxImageList()
78 {
79 m_hImageList = 0;
80 }
81
82 // Creates an image list
83 bool wxImageList::Create(int width, int height, bool mask, int initial)
84 {
85 UINT flags = 0; // TODO shouldallow to specify ILC_COLORxxx here
86 if ( mask )
87 flags |= ILC_MASK;
88
89 // Grow by 1, I guess this is reasonable behaviour most of the time
90 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
91 initial, 1);
92 if ( !m_hImageList )
93 {
94 wxLogLastError("ImageList_Create()");
95 }
96
97 return m_hImageList != 0;
98 }
99
100 wxImageList::~wxImageList()
101 {
102 if ( m_hImageList )
103 {
104 ImageList_Destroy(GetHImageList());
105 m_hImageList = 0;
106 }
107 }
108
109 // ----------------------------------------------------------------------------
110 // wxImageList attributes
111 // ----------------------------------------------------------------------------
112
113 // Returns the number of images in the image list.
114 int wxImageList::GetImageCount() const
115 {
116 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
117
118 return ImageList_GetImageCount(GetHImageList());
119 }
120
121 // ----------------------------------------------------------------------------
122 // wxImageList operations
123 // ----------------------------------------------------------------------------
124
125 // Adds a bitmap, and optionally a mask bitmap.
126 // Note that wxImageList creates new bitmaps, so you may delete
127 // 'bitmap' and 'mask'.
128 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
129 {
130 wxBitmap bmpMask = GetMaskForImage(bitmap, mask);
131 HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask));
132
133 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
134 if ( index == -1 )
135 {
136 wxLogError(_("Couldn't add an image to the image list."));
137 }
138
139 ::DeleteObject(hbmpMask);
140
141 return index;
142 }
143
144 // Adds a bitmap, using the specified colour to create the mask bitmap
145 // Note that wxImageList creates new bitmaps, so you may delete
146 // 'bitmap'.
147 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
148 {
149 #ifdef __TWIN32__
150 wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32"));
151 return -1;
152 #else
153 int index = ImageList_AddMasked(GetHImageList(),
154 GetHbitmapOf(bitmap),
155 wxColourToRGB(maskColour));
156 if ( index == -1 )
157 {
158 wxLogError(_("Couldn't add an image to the image list."));
159 }
160
161 return index;
162 #endif
163 }
164
165 // Adds a bitmap and mask from an icon.
166 int wxImageList::Add(const wxIcon& icon)
167 {
168 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
169 if ( index == -1 )
170 {
171 wxLogError(_("Couldn't add an image to the image list."));
172 }
173
174 return index;
175 }
176
177 // Replaces a bitmap, optionally passing a mask bitmap.
178 // Note that wxImageList creates new bitmaps, so you may delete
179 // 'bitmap' and 'mask'.
180 bool wxImageList::Replace(int index,
181 const wxBitmap& bitmap, const wxBitmap& mask)
182 {
183 #ifdef __TWIN32__
184 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
185 return FALSE;
186 #else
187 wxBitmap bmpMask = GetMaskForImage(bitmap, mask);
188 HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask));
189
190 bool ok = ImageList_Replace(GetHImageList(), index,
191 GetHbitmapOf(bitmap), hbmpMask) != 0;
192 if ( !ok )
193 {
194 wxLogLastError("ImageList_Add()");
195 }
196
197 ::DeleteObject(hbmpMask);
198
199 return ok;
200 #endif
201 }
202
203 // Replaces a bitmap and mask from an icon.
204 bool wxImageList::Replace(int i, const wxIcon& icon)
205 {
206 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
207 if ( !ok )
208 {
209 wxLogLastError("ImageList_ReplaceIcon()");
210 }
211
212 return ok;
213 }
214
215 // Removes the image at the given index.
216 bool wxImageList::Remove(int index)
217 {
218 #ifdef __TWIN32__
219 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
220 return FALSE;
221 #else
222 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
223 if ( !ok )
224 {
225 wxLogLastError("ImageList_Remove()");
226 }
227
228 return ok;
229 #endif
230 }
231
232 // Remove all images
233 bool wxImageList::RemoveAll()
234 {
235 bool ok = ImageList_RemoveAll(GetHImageList()) != 0;
236 if ( !ok )
237 {
238 wxLogLastError("ImageList_RemoveAll()");
239 }
240
241 return ok;
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 #ifdef __TWIN32__
255 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
256 return FALSE;
257 #else
258 HDC hDC = GetHdcOf(dc);
259 wxCHECK_MSG( hDC, FALSE, _T("invalid wxDC in wxImageList::Draw") );
260
261 COLORREF clr = CLR_NONE; // transparent by default
262 if ( solidBackground )
263 {
264 wxBrush *brush = & dc.GetBackground();
265 if ( brush && brush->Ok() )
266 {
267 clr = wxColourToRGB(brush->GetColour());
268 }
269 }
270
271 ImageList_SetBkColor(GetHImageList(), clr);
272
273 UINT style = 0;
274 if ( flags & wxIMAGELIST_DRAW_NORMAL )
275 style |= ILD_NORMAL;
276 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
277 style |= ILD_TRANSPARENT;
278 if ( flags & wxIMAGELIST_DRAW_SELECTED )
279 style |= ILD_SELECTED;
280 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
281 style |= ILD_FOCUS;
282
283 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
284 if ( !ok )
285 {
286 wxLogLastError("ImageList_Draw()");
287 }
288
289 return ok;
290 #endif
291 }
292
293 // ----------------------------------------------------------------------------
294 // helpers
295 // ----------------------------------------------------------------------------
296
297 static wxBitmap GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
298 {
299 wxBitmap bmpMask;
300
301 if ( mask.Ok() )
302 {
303 bmpMask = mask;
304 }
305 else
306 {
307 wxMask *pMask = bitmap.GetMask();
308 if ( pMask )
309 {
310 bmpMask.SetHBITMAP(pMask->GetMaskBitmap());
311 }
312 }
313
314 if ( !bmpMask.Ok() )
315 {
316 // create a non transparent mask - apparently, this is needed under
317 // Win9x (it doesn't behave correctly if it's passed 0 mask)
318 bmpMask.Create(bitmap.GetWidth(), bitmap.GetHeight(), 1);
319
320 wxMemoryDC dcMem;
321 dcMem.SelectObject(bmpMask);
322 dcMem.Clear();
323 dcMem.SelectObject(wxNullBitmap);
324 }
325
326 return bmpMask;
327 }
328
329 #endif // Win95
330