]> git.saurik.com Git - wxWidgets.git/blame - src/msw/imaglist.cpp
added obsolescence note
[wxWidgets.git] / src / msw / imaglist.cpp
CommitLineData
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$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4b7f2165
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
b39dbf34 49#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
4b7f2165 50 #include <commctrl.h>
2bda0e17
KB
51#endif
52
4b7f2165
VZ
53// ----------------------------------------------------------------------------
54// wxWin macros
55// ----------------------------------------------------------------------------
56
2bda0e17 57IMPLEMENT_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)
3ca6a5f0 67static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
4b7f2165
VZ
68
69// ============================================================================
70// implementation
71// ============================================================================
72
73// ----------------------------------------------------------------------------
74// wxImageList creation/destruction
75// ----------------------------------------------------------------------------
76
77wxImageList::wxImageList()
2bda0e17
KB
78{
79 m_hImageList = 0;
80}
81
4b7f2165
VZ
82// Creates an image list
83bool 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();
4676948b
JS
89#ifdef __WXWINCE__
90 flags |= ILC_COLOR;
91#else
a6e2b3a8
VZ
92 if (dd <= 4) flags |= ILC_COLOR; // 16 color
93 else if (dd <= 8) flags |= ILC_COLOR8; // 256 color
94 else if (dd <= 16) flags |= ILC_COLOR16; // 64k hi-color
95 else if (dd <= 24) flags |= ILC_COLOR24; // 16m truecolor
96 else if (dd <= 32) flags |= ILC_COLOR32; // 16m truecolor
4676948b 97#endif
a6e2b3a8 98
4b7f2165
VZ
99 if ( mask )
100 flags |= ILC_MASK;
101
102 // Grow by 1, I guess this is reasonable behaviour most of the time
103 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
104 initial, 1);
105 if ( !m_hImageList )
106 {
f6bcfd97 107 wxLogLastError(wxT("ImageList_Create()"));
4b7f2165 108 }
2bda0e17 109
4b7f2165
VZ
110 return m_hImageList != 0;
111}
2bda0e17 112
4b7f2165 113wxImageList::~wxImageList()
2bda0e17 114{
4b7f2165
VZ
115 if ( m_hImageList )
116 {
117 ImageList_Destroy(GetHImageList());
118 m_hImageList = 0;
119 }
2bda0e17
KB
120}
121
4b7f2165
VZ
122// ----------------------------------------------------------------------------
123// wxImageList attributes
124// ----------------------------------------------------------------------------
2bda0e17 125
4b7f2165
VZ
126// Returns the number of images in the image list.
127int wxImageList::GetImageCount() const
2bda0e17 128{
4b7f2165 129 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
2bda0e17 130
4b7f2165 131 return ImageList_GetImageCount(GetHImageList());
2bda0e17
KB
132}
133
f6bcfd97
BP
134// Returns the size (same for all images) of the images in the list
135bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const
136{
137 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
138
139 return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0;
140}
141
4b7f2165
VZ
142// ----------------------------------------------------------------------------
143// wxImageList operations
144// ----------------------------------------------------------------------------
145
2bda0e17
KB
146// Adds a bitmap, and optionally a mask bitmap.
147// Note that wxImageList creates new bitmaps, so you may delete
148// 'bitmap' and 'mask'.
149int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
150{
3ca6a5f0 151 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
e29b83a4 152
4b7f2165
VZ
153 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
154 if ( index == -1 )
e29b83a4
VZ
155 {
156 wxLogError(_("Couldn't add an image to the image list."));
157 }
158
4b7f2165 159 ::DeleteObject(hbmpMask);
54b37e2e 160
e29b83a4 161 return index;
2bda0e17
KB
162}
163
164// Adds a bitmap, using the specified colour to create the mask bitmap
165// Note that wxImageList creates new bitmaps, so you may delete
166// 'bitmap'.
167int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
168{
4b7f2165
VZ
169 int index = ImageList_AddMasked(GetHImageList(),
170 GetHbitmapOf(bitmap),
171 wxColourToRGB(maskColour));
172 if ( index == -1 )
173 {
174 wxLogError(_("Couldn't add an image to the image list."));
175 }
176
177 return index;
2bda0e17
KB
178}
179
180// Adds a bitmap and mask from an icon.
181int wxImageList::Add(const wxIcon& icon)
182{
4b7f2165
VZ
183 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
184 if ( index == -1 )
185 {
186 wxLogError(_("Couldn't add an image to the image list."));
187 }
188
189 return index;
2bda0e17
KB
190}
191
192// Replaces a bitmap, optionally passing a mask bitmap.
193// Note that wxImageList creates new bitmaps, so you may delete
194// 'bitmap' and 'mask'.
4b7f2165
VZ
195bool wxImageList::Replace(int index,
196 const wxBitmap& bitmap, const wxBitmap& mask)
2bda0e17 197{
3ca6a5f0 198 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
2bda0e17 199
4b7f2165
VZ
200 bool ok = ImageList_Replace(GetHImageList(), index,
201 GetHbitmapOf(bitmap), hbmpMask) != 0;
202 if ( !ok )
203 {
f6bcfd97 204 wxLogLastError(wxT("ImageList_Add()"));
4b7f2165
VZ
205 }
206
207 ::DeleteObject(hbmpMask);
208
209 return ok;
2bda0e17 210}
2bda0e17
KB
211
212// Replaces a bitmap and mask from an icon.
4b7f2165 213bool 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 {
f6bcfd97 218 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
4b7f2165
VZ
219 }
220
221 return ok;
2bda0e17
KB
222}
223
224// Removes the image at the given index.
debe6624 225bool wxImageList::Remove(int index)
2bda0e17 226{
4b7f2165
VZ
227 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
228 if ( !ok )
229 {
f6bcfd97 230 wxLogLastError(wxT("ImageList_Remove()"));
4b7f2165
VZ
231 }
232
233 return ok;
2bda0e17
KB
234}
235
236// Remove all images
4b7f2165 237bool wxImageList::RemoveAll()
2bda0e17 238{
3c1a88d8
VZ
239 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
240 int count = ImageList_GetImageCount(GetHImageList());
241 for ( int i = 0; i < count; i++ )
4b7f2165 242 {
3c1a88d8
VZ
243 // the image indexes are shifted, so we should always remove the first
244 // one
245 (void)Remove(0);
4b7f2165
VZ
246 }
247
3c1a88d8 248 return TRUE;
2bda0e17
KB
249}
250
251// Draws the given image on a dc at the specified position.
252// If 'solidBackground' is TRUE, Draw sets the image list background
253// colour to the background colour of the wxDC, to speed up
254// drawing by eliminating masked drawing where possible.
4b7f2165
VZ
255bool wxImageList::Draw(int index,
256 wxDC& dc,
257 int x, int y,
258 int flags,
259 bool solidBackground)
2bda0e17 260{
4b7f2165
VZ
261 HDC hDC = GetHdcOf(dc);
262 wxCHECK_MSG( hDC, FALSE, _T("invalid wxDC in wxImageList::Draw") );
263
264 COLORREF clr = CLR_NONE; // transparent by default
265 if ( solidBackground )
266 {
267 wxBrush *brush = & dc.GetBackground();
268 if ( brush && brush->Ok() )
269 {
270 clr = wxColourToRGB(brush->GetColour());
271 }
272 }
273
274 ImageList_SetBkColor(GetHImageList(), clr);
2bda0e17
KB
275
276 UINT style = 0;
4b7f2165
VZ
277 if ( flags & wxIMAGELIST_DRAW_NORMAL )
278 style |= ILD_NORMAL;
279 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
280 style |= ILD_TRANSPARENT;
281 if ( flags & wxIMAGELIST_DRAW_SELECTED )
282 style |= ILD_SELECTED;
283 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
284 style |= ILD_FOCUS;
285
286 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
287 if ( !ok )
288 {
f6bcfd97 289 wxLogLastError(wxT("ImageList_Draw()"));
4b7f2165
VZ
290 }
291
292 return ok;
2bda0e17
KB
293}
294
4b7f2165
VZ
295// ----------------------------------------------------------------------------
296// helpers
297// ----------------------------------------------------------------------------
298
3ca6a5f0 299static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
4b7f2165 300{
3ca6a5f0 301 HBITMAP hbmpMask;
90c1530a
VZ
302 wxMask *pMask;
303 bool deleteMask = FALSE;
4b7f2165
VZ
304
305 if ( mask.Ok() )
306 {
3ca6a5f0 307 hbmpMask = GetHbitmapOf(mask);
90c1530a 308 pMask = NULL;
4b7f2165
VZ
309 }
310 else
311 {
90c1530a
VZ
312 pMask = bitmap.GetMask();
313 if ( !pMask )
4b7f2165 314 {
90c1530a
VZ
315 // use the light grey count as transparent: the trouble here is
316 // that the light grey might have been changed by Windows behind
317 // our back, so use the standard colour map to get its real value
318 wxCOLORMAP *cmap = wxGetStdColourMap();
319 wxColour col;
320 wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
4b7f2165 321
90c1530a 322 pMask = new wxMask(bitmap, col);
3ca6a5f0 323
90c1530a 324 deleteMask = TRUE;
3ca6a5f0 325 }
90c1530a
VZ
326
327 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
4b7f2165
VZ
328 }
329
3ca6a5f0
BP
330 // windows mask convention is opposite to the wxWindows one
331 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
90c1530a
VZ
332
333 if ( deleteMask )
334 {
335 delete pMask;
336 }
3ca6a5f0
BP
337
338 return hbmpMaskInv;
4b7f2165
VZ
339}
340
341#endif // Win95
2bda0e17 342