]>
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" | |
e0f15f4a | 38 | #include "wx/dcmemory.h" |
4b7f2165 VZ |
39 | |
40 | #include <stdio.h> | |
2bda0e17 KB |
41 | #endif |
42 | ||
ce3ed50d JS |
43 | #include "wx/log.h" |
44 | #include "wx/intl.h" | |
45 | ||
2bda0e17 KB |
46 | #include "wx/msw/imaglist.h" |
47 | #include "wx/msw/private.h" | |
48 | ||
4b7f2165 VZ |
49 | #if !defined(__GNUWIN32_OLD__) && !defined(__TWIN32__) |
50 | #include <commctrl.h> | |
2bda0e17 KB |
51 | #endif |
52 | ||
4b7f2165 VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // wxWin macros | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
2bda0e17 | 57 | IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject) |
2bda0e17 | 58 | |
4b7f2165 VZ |
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() | |
2bda0e17 KB |
78 | { |
79 | m_hImageList = 0; | |
80 | } | |
81 | ||
4b7f2165 VZ |
82 | // Creates an image list |
83 | bool wxImageList::Create(int width, int height, bool mask, int initial) | |
2bda0e17 | 84 | { |
a6e2b3a8 VZ |
85 | UINT flags = 0; |
86 | ||
87 | // set appropriate color depth | |
88 | int dd = wxDisplayDepth(); | |
89 | if (dd <= 4) flags |= ILC_COLOR; // 16 color | |
90 | else if (dd <= 8) flags |= ILC_COLOR8; // 256 color | |
91 | else if (dd <= 16) flags |= ILC_COLOR16; // 64k hi-color | |
92 | else if (dd <= 24) flags |= ILC_COLOR24; // 16m truecolor | |
93 | else if (dd <= 32) flags |= ILC_COLOR32; // 16m truecolor | |
94 | ||
4b7f2165 VZ |
95 | if ( mask ) |
96 | flags |= ILC_MASK; | |
97 | ||
98 | // Grow by 1, I guess this is reasonable behaviour most of the time | |
99 | m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags, | |
100 | initial, 1); | |
101 | if ( !m_hImageList ) | |
102 | { | |
103 | wxLogLastError("ImageList_Create()"); | |
104 | } | |
2bda0e17 | 105 | |
4b7f2165 VZ |
106 | return m_hImageList != 0; |
107 | } | |
2bda0e17 | 108 | |
4b7f2165 | 109 | wxImageList::~wxImageList() |
2bda0e17 | 110 | { |
4b7f2165 VZ |
111 | if ( m_hImageList ) |
112 | { | |
113 | ImageList_Destroy(GetHImageList()); | |
114 | m_hImageList = 0; | |
115 | } | |
2bda0e17 KB |
116 | } |
117 | ||
4b7f2165 VZ |
118 | // ---------------------------------------------------------------------------- |
119 | // wxImageList attributes | |
120 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 121 | |
4b7f2165 VZ |
122 | // Returns the number of images in the image list. |
123 | int wxImageList::GetImageCount() const | |
2bda0e17 | 124 | { |
4b7f2165 | 125 | wxASSERT_MSG( m_hImageList, _T("invalid image list") ); |
2bda0e17 | 126 | |
4b7f2165 | 127 | return ImageList_GetImageCount(GetHImageList()); |
2bda0e17 KB |
128 | } |
129 | ||
4b7f2165 VZ |
130 | // ---------------------------------------------------------------------------- |
131 | // wxImageList operations | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
2bda0e17 KB |
134 | // Adds a bitmap, and optionally a mask bitmap. |
135 | // Note that wxImageList creates new bitmaps, so you may delete | |
136 | // 'bitmap' and 'mask'. | |
137 | int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) | |
138 | { | |
4b7f2165 VZ |
139 | wxBitmap bmpMask = GetMaskForImage(bitmap, mask); |
140 | HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask)); | |
e29b83a4 | 141 | |
4b7f2165 VZ |
142 | int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask); |
143 | if ( index == -1 ) | |
e29b83a4 VZ |
144 | { |
145 | wxLogError(_("Couldn't add an image to the image list.")); | |
146 | } | |
147 | ||
4b7f2165 | 148 | ::DeleteObject(hbmpMask); |
54b37e2e | 149 | |
e29b83a4 | 150 | return index; |
2bda0e17 KB |
151 | } |
152 | ||
153 | // Adds a bitmap, using the specified colour to create the mask bitmap | |
154 | // Note that wxImageList creates new bitmaps, so you may delete | |
155 | // 'bitmap'. | |
156 | int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) | |
157 | { | |
57c208c5 | 158 | #ifdef __TWIN32__ |
4b7f2165 VZ |
159 | wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32")); |
160 | return -1; | |
57c208c5 | 161 | #else |
4b7f2165 VZ |
162 | int index = ImageList_AddMasked(GetHImageList(), |
163 | GetHbitmapOf(bitmap), | |
164 | wxColourToRGB(maskColour)); | |
165 | if ( index == -1 ) | |
166 | { | |
167 | wxLogError(_("Couldn't add an image to the image list.")); | |
168 | } | |
169 | ||
170 | return index; | |
57c208c5 | 171 | #endif |
2bda0e17 KB |
172 | } |
173 | ||
174 | // Adds a bitmap and mask from an icon. | |
175 | int wxImageList::Add(const wxIcon& icon) | |
176 | { | |
4b7f2165 VZ |
177 | int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon)); |
178 | if ( index == -1 ) | |
179 | { | |
180 | wxLogError(_("Couldn't add an image to the image list.")); | |
181 | } | |
182 | ||
183 | return index; | |
2bda0e17 KB |
184 | } |
185 | ||
186 | // Replaces a bitmap, optionally passing a mask bitmap. | |
187 | // Note that wxImageList creates new bitmaps, so you may delete | |
188 | // 'bitmap' and 'mask'. | |
4b7f2165 VZ |
189 | bool wxImageList::Replace(int index, |
190 | const wxBitmap& bitmap, const wxBitmap& mask) | |
2bda0e17 | 191 | { |
57c208c5 | 192 | #ifdef __TWIN32__ |
4b7f2165 VZ |
193 | wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32")); |
194 | return FALSE; | |
57c208c5 | 195 | #else |
4b7f2165 VZ |
196 | wxBitmap bmpMask = GetMaskForImage(bitmap, mask); |
197 | HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask)); | |
2bda0e17 | 198 | |
4b7f2165 VZ |
199 | bool ok = ImageList_Replace(GetHImageList(), index, |
200 | GetHbitmapOf(bitmap), hbmpMask) != 0; | |
201 | if ( !ok ) | |
202 | { | |
203 | wxLogLastError("ImageList_Add()"); | |
204 | } | |
205 | ||
206 | ::DeleteObject(hbmpMask); | |
207 | ||
208 | return ok; | |
209 | #endif | |
2bda0e17 | 210 | } |
2bda0e17 KB |
211 | |
212 | // Replaces a bitmap and mask from an icon. | |
4b7f2165 | 213 | bool wxImageList::Replace(int i, const wxIcon& icon) |
2bda0e17 | 214 | { |
4b7f2165 VZ |
215 | bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0; |
216 | if ( !ok ) | |
217 | { | |
218 | wxLogLastError("ImageList_ReplaceIcon()"); | |
219 | } | |
220 | ||
221 | return ok; | |
2bda0e17 KB |
222 | } |
223 | ||
224 | // Removes the image at the given index. | |
debe6624 | 225 | bool wxImageList::Remove(int index) |
2bda0e17 | 226 | { |
57c208c5 | 227 | #ifdef __TWIN32__ |
4b7f2165 VZ |
228 | wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32")); |
229 | return FALSE; | |
57c208c5 | 230 | #else |
4b7f2165 VZ |
231 | bool ok = ImageList_Remove(GetHImageList(), index) != 0; |
232 | if ( !ok ) | |
233 | { | |
234 | wxLogLastError("ImageList_Remove()"); | |
235 | } | |
236 | ||
237 | return ok; | |
57c208c5 | 238 | #endif |
2bda0e17 KB |
239 | } |
240 | ||
241 | // Remove all images | |
4b7f2165 | 242 | bool wxImageList::RemoveAll() |
2bda0e17 | 243 | { |
3c1a88d8 VZ |
244 | // don't use ImageList_RemoveAll() because mingw32 headers don't have it |
245 | int count = ImageList_GetImageCount(GetHImageList()); | |
246 | for ( int i = 0; i < count; i++ ) | |
4b7f2165 | 247 | { |
3c1a88d8 VZ |
248 | // the image indexes are shifted, so we should always remove the first |
249 | // one | |
250 | (void)Remove(0); | |
4b7f2165 VZ |
251 | } |
252 | ||
3c1a88d8 | 253 | return TRUE; |
2bda0e17 KB |
254 | } |
255 | ||
256 | // Draws the given image on a dc at the specified position. | |
257 | // If 'solidBackground' is TRUE, Draw sets the image list background | |
258 | // colour to the background colour of the wxDC, to speed up | |
259 | // drawing by eliminating masked drawing where possible. | |
4b7f2165 VZ |
260 | bool wxImageList::Draw(int index, |
261 | wxDC& dc, | |
262 | int x, int y, | |
263 | int flags, | |
264 | bool solidBackground) | |
2bda0e17 | 265 | { |
57c208c5 | 266 | #ifdef __TWIN32__ |
4b7f2165 VZ |
267 | wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32")); |
268 | return FALSE; | |
57c208c5 | 269 | #else |
4b7f2165 VZ |
270 | HDC hDC = GetHdcOf(dc); |
271 | wxCHECK_MSG( hDC, FALSE, _T("invalid wxDC in wxImageList::Draw") ); | |
272 | ||
273 | COLORREF clr = CLR_NONE; // transparent by default | |
274 | if ( solidBackground ) | |
275 | { | |
276 | wxBrush *brush = & dc.GetBackground(); | |
277 | if ( brush && brush->Ok() ) | |
278 | { | |
279 | clr = wxColourToRGB(brush->GetColour()); | |
280 | } | |
281 | } | |
282 | ||
283 | ImageList_SetBkColor(GetHImageList(), clr); | |
2bda0e17 KB |
284 | |
285 | UINT style = 0; | |
4b7f2165 VZ |
286 | if ( flags & wxIMAGELIST_DRAW_NORMAL ) |
287 | style |= ILD_NORMAL; | |
288 | if ( flags & wxIMAGELIST_DRAW_TRANSPARENT ) | |
289 | style |= ILD_TRANSPARENT; | |
290 | if ( flags & wxIMAGELIST_DRAW_SELECTED ) | |
291 | style |= ILD_SELECTED; | |
292 | if ( flags & wxIMAGELIST_DRAW_FOCUSED ) | |
293 | style |= ILD_FOCUS; | |
294 | ||
295 | bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0; | |
296 | if ( !ok ) | |
297 | { | |
298 | wxLogLastError("ImageList_Draw()"); | |
299 | } | |
300 | ||
301 | return ok; | |
57c208c5 | 302 | #endif |
2bda0e17 KB |
303 | } |
304 | ||
4b7f2165 VZ |
305 | // ---------------------------------------------------------------------------- |
306 | // helpers | |
307 | // ---------------------------------------------------------------------------- | |
308 | ||
309 | static wxBitmap GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask) | |
310 | { | |
311 | wxBitmap bmpMask; | |
312 | ||
313 | if ( mask.Ok() ) | |
314 | { | |
315 | bmpMask = mask; | |
316 | } | |
317 | else | |
318 | { | |
319 | wxMask *pMask = bitmap.GetMask(); | |
320 | if ( pMask ) | |
321 | { | |
322 | bmpMask.SetHBITMAP(pMask->GetMaskBitmap()); | |
323 | } | |
324 | } | |
325 | ||
326 | if ( !bmpMask.Ok() ) | |
327 | { | |
328 | // create a non transparent mask - apparently, this is needed under | |
329 | // Win9x (it doesn't behave correctly if it's passed 0 mask) | |
330 | bmpMask.Create(bitmap.GetWidth(), bitmap.GetHeight(), 1); | |
331 | ||
332 | wxMemoryDC dcMem; | |
333 | dcMem.SelectObject(bmpMask); | |
334 | dcMem.Clear(); | |
335 | dcMem.SelectObject(wxNullBitmap); | |
336 | } | |
337 | ||
338 | return bmpMask; | |
339 | } | |
340 | ||
341 | #endif // Win95 | |
2bda0e17 | 342 |