]> git.saurik.com Git - wxWidgets.git/blame - src/msw/imaglist.cpp
OS/2 updates for statusbar processing
[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$
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 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();
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 {
f6bcfd97 103 wxLogLastError(wxT("ImageList_Create()"));
4b7f2165 104 }
2bda0e17 105
4b7f2165
VZ
106 return m_hImageList != 0;
107}
2bda0e17 108
4b7f2165 109wxImageList::~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.
123int 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
f6bcfd97
BP
130// Returns the size (same for all images) of the images in the list
131bool 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
4b7f2165
VZ
138// ----------------------------------------------------------------------------
139// wxImageList operations
140// ----------------------------------------------------------------------------
141
2bda0e17
KB
142// Adds a bitmap, and optionally a mask bitmap.
143// Note that wxImageList creates new bitmaps, so you may delete
144// 'bitmap' and 'mask'.
145int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
146{
3ca6a5f0 147 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
e29b83a4 148
4b7f2165
VZ
149 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
150 if ( index == -1 )
e29b83a4
VZ
151 {
152 wxLogError(_("Couldn't add an image to the image list."));
153 }
154
4b7f2165 155 ::DeleteObject(hbmpMask);
54b37e2e 156
e29b83a4 157 return index;
2bda0e17
KB
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'.
163int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
164{
57c208c5 165#ifdef __TWIN32__
4b7f2165
VZ
166 wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32"));
167 return -1;
57c208c5 168#else
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;
57c208c5 178#endif
2bda0e17
KB
179}
180
181// Adds a bitmap and mask from an icon.
182int wxImageList::Add(const wxIcon& icon)
183{
4b7f2165
VZ
184 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
185 if ( index == -1 )
186 {
187 wxLogError(_("Couldn't add an image to the image list."));
188 }
189
190 return index;
2bda0e17
KB
191}
192
193// Replaces a bitmap, optionally passing a mask bitmap.
194// Note that wxImageList creates new bitmaps, so you may delete
195// 'bitmap' and 'mask'.
4b7f2165
VZ
196bool wxImageList::Replace(int index,
197 const wxBitmap& bitmap, const wxBitmap& mask)
2bda0e17 198{
57c208c5 199#ifdef __TWIN32__
4b7f2165
VZ
200 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
201 return FALSE;
57c208c5 202#else
3ca6a5f0 203 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
2bda0e17 204
4b7f2165
VZ
205 bool ok = ImageList_Replace(GetHImageList(), index,
206 GetHbitmapOf(bitmap), hbmpMask) != 0;
207 if ( !ok )
208 {
f6bcfd97 209 wxLogLastError(wxT("ImageList_Add()"));
4b7f2165
VZ
210 }
211
212 ::DeleteObject(hbmpMask);
213
214 return ok;
215#endif
2bda0e17 216}
2bda0e17
KB
217
218// Replaces a bitmap and mask from an icon.
4b7f2165 219bool wxImageList::Replace(int i, const wxIcon& icon)
2bda0e17 220{
4b7f2165
VZ
221 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
222 if ( !ok )
223 {
f6bcfd97 224 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
4b7f2165
VZ
225 }
226
227 return ok;
2bda0e17
KB
228}
229
230// Removes the image at the given index.
debe6624 231bool wxImageList::Remove(int index)
2bda0e17 232{
57c208c5 233#ifdef __TWIN32__
4b7f2165
VZ
234 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
235 return FALSE;
57c208c5 236#else
4b7f2165
VZ
237 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
238 if ( !ok )
239 {
f6bcfd97 240 wxLogLastError(wxT("ImageList_Remove()"));
4b7f2165
VZ
241 }
242
243 return ok;
57c208c5 244#endif
2bda0e17
KB
245}
246
247// Remove all images
4b7f2165 248bool wxImageList::RemoveAll()
2bda0e17 249{
3c1a88d8
VZ
250 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
251 int count = ImageList_GetImageCount(GetHImageList());
252 for ( int i = 0; i < count; i++ )
4b7f2165 253 {
3c1a88d8
VZ
254 // the image indexes are shifted, so we should always remove the first
255 // one
256 (void)Remove(0);
4b7f2165
VZ
257 }
258
3c1a88d8 259 return TRUE;
2bda0e17
KB
260}
261
262// Draws the given image on a dc at the specified position.
263// If 'solidBackground' is TRUE, Draw sets the image list background
264// colour to the background colour of the wxDC, to speed up
265// drawing by eliminating masked drawing where possible.
4b7f2165
VZ
266bool wxImageList::Draw(int index,
267 wxDC& dc,
268 int x, int y,
269 int flags,
270 bool solidBackground)
2bda0e17 271{
57c208c5 272#ifdef __TWIN32__
4b7f2165
VZ
273 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
274 return FALSE;
57c208c5 275#else
4b7f2165
VZ
276 HDC hDC = GetHdcOf(dc);
277 wxCHECK_MSG( hDC, FALSE, _T("invalid wxDC in wxImageList::Draw") );
278
279 COLORREF clr = CLR_NONE; // transparent by default
280 if ( solidBackground )
281 {
282 wxBrush *brush = & dc.GetBackground();
283 if ( brush && brush->Ok() )
284 {
285 clr = wxColourToRGB(brush->GetColour());
286 }
287 }
288
289 ImageList_SetBkColor(GetHImageList(), clr);
2bda0e17
KB
290
291 UINT style = 0;
4b7f2165
VZ
292 if ( flags & wxIMAGELIST_DRAW_NORMAL )
293 style |= ILD_NORMAL;
294 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
295 style |= ILD_TRANSPARENT;
296 if ( flags & wxIMAGELIST_DRAW_SELECTED )
297 style |= ILD_SELECTED;
298 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
299 style |= ILD_FOCUS;
300
301 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
302 if ( !ok )
303 {
f6bcfd97 304 wxLogLastError(wxT("ImageList_Draw()"));
4b7f2165
VZ
305 }
306
307 return ok;
57c208c5 308#endif
2bda0e17
KB
309}
310
4b7f2165
VZ
311// ----------------------------------------------------------------------------
312// helpers
313// ----------------------------------------------------------------------------
314
3ca6a5f0 315static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
4b7f2165 316{
3ca6a5f0
BP
317 HBITMAP hbmpMask;
318 wxBitmap *bmpMask = NULL;
4b7f2165
VZ
319
320 if ( mask.Ok() )
321 {
3ca6a5f0 322 hbmpMask = GetHbitmapOf(mask);
4b7f2165
VZ
323 }
324 else
325 {
326 wxMask *pMask = bitmap.GetMask();
327 if ( pMask )
328 {
3ca6a5f0 329 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
4b7f2165 330 }
3ca6a5f0
BP
331 else
332 {
333 // create a non transparent mask - apparently, this is needed under
334 // Win9x (it doesn't behave correctly if it's passed 0 mask)
335 bmpMask = new wxBitmap(bitmap.GetWidth(), bitmap.GetHeight(), 1);
4b7f2165 336
3ca6a5f0
BP
337 wxMemoryDC dcMem;
338 dcMem.SelectObject(*bmpMask);
339 dcMem.Clear();
340 dcMem.SelectObject(wxNullBitmap);
341
342 hbmpMask = GetHbitmapOf(*bmpMask);
343 }
4b7f2165
VZ
344 }
345
3ca6a5f0
BP
346 // windows mask convention is opposite to the wxWindows one
347 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
348 delete bmpMask;
349
350 return hbmpMaskInv;
4b7f2165
VZ
351}
352
353#endif // Win95
2bda0e17 354