]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
small wxGTK compilation fixes
[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 #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(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
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 HBITMAP 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;
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
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(wxT("ImageList_Create()"));
104 }
105
106 return m_hImageList != 0;
107 }
108
109 wxImageList::~wxImageList()
110 {
111 if ( m_hImageList )
112 {
113 ImageList_Destroy(GetHImageList());
114 m_hImageList = 0;
115 }
116 }
117
118 // ----------------------------------------------------------------------------
119 // wxImageList attributes
120 // ----------------------------------------------------------------------------
121
122 // Returns the number of images in the image list.
123 int wxImageList::GetImageCount() const
124 {
125 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
126
127 return ImageList_GetImageCount(GetHImageList());
128 }
129
130 // Returns the size (same for all images) of the images in the list
131 bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const
132 {
133 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
134
135 return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0;
136 }
137
138 // ----------------------------------------------------------------------------
139 // wxImageList operations
140 // ----------------------------------------------------------------------------
141
142 // Adds a bitmap, and optionally a mask bitmap.
143 // Note that wxImageList creates new bitmaps, so you may delete
144 // 'bitmap' and 'mask'.
145 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
146 {
147 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
148
149 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
150 if ( index == -1 )
151 {
152 wxLogError(_("Couldn't add an image to the image list."));
153 }
154
155 ::DeleteObject(hbmpMask);
156
157 return index;
158 }
159
160 // Adds a bitmap, using the specified colour to create the mask bitmap
161 // Note that wxImageList creates new bitmaps, so you may delete
162 // 'bitmap'.
163 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
164 {
165 int index = ImageList_AddMasked(GetHImageList(),
166 GetHbitmapOf(bitmap),
167 wxColourToRGB(maskColour));
168 if ( index == -1 )
169 {
170 wxLogError(_("Couldn't add an image to the image list."));
171 }
172
173 return index;
174 }
175
176 // Adds a bitmap and mask from an icon.
177 int wxImageList::Add(const wxIcon& icon)
178 {
179 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
180 if ( index == -1 )
181 {
182 wxLogError(_("Couldn't add an image to the image list."));
183 }
184
185 return index;
186 }
187
188 // Replaces a bitmap, optionally passing a mask bitmap.
189 // Note that wxImageList creates new bitmaps, so you may delete
190 // 'bitmap' and 'mask'.
191 bool wxImageList::Replace(int index,
192 const wxBitmap& bitmap, const wxBitmap& mask)
193 {
194 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
195
196 bool ok = ImageList_Replace(GetHImageList(), index,
197 GetHbitmapOf(bitmap), hbmpMask) != 0;
198 if ( !ok )
199 {
200 wxLogLastError(wxT("ImageList_Add()"));
201 }
202
203 ::DeleteObject(hbmpMask);
204
205 return ok;
206 }
207
208 // Replaces a bitmap and mask from an icon.
209 bool wxImageList::Replace(int i, const wxIcon& icon)
210 {
211 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
212 if ( !ok )
213 {
214 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
215 }
216
217 return ok;
218 }
219
220 // Removes the image at the given index.
221 bool wxImageList::Remove(int index)
222 {
223 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
224 if ( !ok )
225 {
226 wxLogLastError(wxT("ImageList_Remove()"));
227 }
228
229 return ok;
230 }
231
232 // Remove all images
233 bool wxImageList::RemoveAll()
234 {
235 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
236 int count = ImageList_GetImageCount(GetHImageList());
237 for ( int i = 0; i < count; i++ )
238 {
239 // the image indexes are shifted, so we should always remove the first
240 // one
241 (void)Remove(0);
242 }
243
244 return TRUE;
245 }
246
247 // Draws the given image on a dc at the specified position.
248 // If 'solidBackground' is TRUE, Draw sets the image list background
249 // colour to the background colour of the wxDC, to speed up
250 // drawing by eliminating masked drawing where possible.
251 bool wxImageList::Draw(int index,
252 wxDC& dc,
253 int x, int y,
254 int flags,
255 bool solidBackground)
256 {
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);
271
272 UINT style = 0;
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(wxT("ImageList_Draw()"));
286 }
287
288 return ok;
289 }
290
291 // ----------------------------------------------------------------------------
292 // helpers
293 // ----------------------------------------------------------------------------
294
295 static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
296 {
297 HBITMAP hbmpMask;
298 wxMask *pMask;
299 bool deleteMask = FALSE;
300
301 if ( mask.Ok() )
302 {
303 hbmpMask = GetHbitmapOf(mask);
304 pMask = NULL;
305 }
306 else
307 {
308 pMask = bitmap.GetMask();
309 if ( !pMask )
310 {
311 // use the light grey count as transparent: the trouble here is
312 // that the light grey might have been changed by Windows behind
313 // our back, so use the standard colour map to get its real value
314 wxCOLORMAP *cmap = wxGetStdColourMap();
315 wxColour col;
316 wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
317
318 pMask = new wxMask(bitmap, col);
319
320 deleteMask = TRUE;
321 }
322
323 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
324 }
325
326 // windows mask convention is opposite to the wxWindows one
327 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
328
329 if ( deleteMask )
330 {
331 delete pMask;
332 }
333
334 return hbmpMaskInv;
335 }
336
337 #endif // Win95
338