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