]> git.saurik.com Git - wxWidgets.git/blame - src/msw/imaglist.cpp
fix for (too big) combobox height
[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)
67static wxBitmap GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
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{
4b7f2165
VZ
85 UINT flags = 0; // TODO shouldallow to specify ILC_COLORxxx here
86 if ( mask )
87 flags |= ILC_MASK;
88
89 // Grow by 1, I guess this is reasonable behaviour most of the time
90 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
91 initial, 1);
92 if ( !m_hImageList )
93 {
94 wxLogLastError("ImageList_Create()");
95 }
2bda0e17 96
4b7f2165
VZ
97 return m_hImageList != 0;
98}
2bda0e17 99
4b7f2165 100wxImageList::~wxImageList()
2bda0e17 101{
4b7f2165
VZ
102 if ( m_hImageList )
103 {
104 ImageList_Destroy(GetHImageList());
105 m_hImageList = 0;
106 }
2bda0e17
KB
107}
108
4b7f2165
VZ
109// ----------------------------------------------------------------------------
110// wxImageList attributes
111// ----------------------------------------------------------------------------
2bda0e17 112
4b7f2165
VZ
113// Returns the number of images in the image list.
114int wxImageList::GetImageCount() const
2bda0e17 115{
4b7f2165 116 wxASSERT_MSG( m_hImageList, _T("invalid image list") );
2bda0e17 117
4b7f2165 118 return ImageList_GetImageCount(GetHImageList());
2bda0e17
KB
119}
120
4b7f2165
VZ
121// ----------------------------------------------------------------------------
122// wxImageList operations
123// ----------------------------------------------------------------------------
124
2bda0e17
KB
125// Adds a bitmap, and optionally a mask bitmap.
126// Note that wxImageList creates new bitmaps, so you may delete
127// 'bitmap' and 'mask'.
128int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
129{
4b7f2165
VZ
130 wxBitmap bmpMask = GetMaskForImage(bitmap, mask);
131 HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask));
e29b83a4 132
4b7f2165
VZ
133 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
134 if ( index == -1 )
e29b83a4
VZ
135 {
136 wxLogError(_("Couldn't add an image to the image list."));
137 }
138
4b7f2165 139 ::DeleteObject(hbmpMask);
54b37e2e 140
e29b83a4 141 return index;
2bda0e17
KB
142}
143
144// Adds a bitmap, using the specified colour to create the mask bitmap
145// Note that wxImageList creates new bitmaps, so you may delete
146// 'bitmap'.
147int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
148{
57c208c5 149#ifdef __TWIN32__
4b7f2165
VZ
150 wxFAIL_MSG(_T("ImageList_AddMasked not implemented in TWIN32"));
151 return -1;
57c208c5 152#else
4b7f2165
VZ
153 int index = ImageList_AddMasked(GetHImageList(),
154 GetHbitmapOf(bitmap),
155 wxColourToRGB(maskColour));
156 if ( index == -1 )
157 {
158 wxLogError(_("Couldn't add an image to the image list."));
159 }
160
161 return index;
57c208c5 162#endif
2bda0e17
KB
163}
164
165// Adds a bitmap and mask from an icon.
166int wxImageList::Add(const wxIcon& icon)
167{
4b7f2165
VZ
168 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
169 if ( index == -1 )
170 {
171 wxLogError(_("Couldn't add an image to the image list."));
172 }
173
174 return index;
2bda0e17
KB
175}
176
177// Replaces a bitmap, optionally passing a mask bitmap.
178// Note that wxImageList creates new bitmaps, so you may delete
179// 'bitmap' and 'mask'.
4b7f2165
VZ
180bool wxImageList::Replace(int index,
181 const wxBitmap& bitmap, const wxBitmap& mask)
2bda0e17 182{
57c208c5 183#ifdef __TWIN32__
4b7f2165
VZ
184 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
185 return FALSE;
57c208c5 186#else
4b7f2165
VZ
187 wxBitmap bmpMask = GetMaskForImage(bitmap, mask);
188 HBITMAP hbmpMask = wxInvertMask(GetHbitmapOf(bmpMask));
2bda0e17 189
4b7f2165
VZ
190 bool ok = ImageList_Replace(GetHImageList(), index,
191 GetHbitmapOf(bitmap), hbmpMask) != 0;
192 if ( !ok )
193 {
194 wxLogLastError("ImageList_Add()");
195 }
196
197 ::DeleteObject(hbmpMask);
198
199 return ok;
200#endif
2bda0e17 201}
2bda0e17
KB
202
203// Replaces a bitmap and mask from an icon.
4b7f2165 204bool wxImageList::Replace(int i, const wxIcon& icon)
2bda0e17 205{
4b7f2165
VZ
206 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != 0;
207 if ( !ok )
208 {
209 wxLogLastError("ImageList_ReplaceIcon()");
210 }
211
212 return ok;
2bda0e17
KB
213}
214
215// Removes the image at the given index.
debe6624 216bool wxImageList::Remove(int index)
2bda0e17 217{
57c208c5 218#ifdef __TWIN32__
4b7f2165
VZ
219 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
220 return FALSE;
57c208c5 221#else
4b7f2165
VZ
222 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
223 if ( !ok )
224 {
225 wxLogLastError("ImageList_Remove()");
226 }
227
228 return ok;
57c208c5 229#endif
2bda0e17
KB
230}
231
232// Remove all images
4b7f2165 233bool wxImageList::RemoveAll()
2bda0e17 234{
3c1a88d8
VZ
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++ )
4b7f2165 238 {
3c1a88d8
VZ
239 // the image indexes are shifted, so we should always remove the first
240 // one
241 (void)Remove(0);
4b7f2165
VZ
242 }
243
3c1a88d8 244 return TRUE;
2bda0e17
KB
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.
4b7f2165
VZ
251bool wxImageList::Draw(int index,
252 wxDC& dc,
253 int x, int y,
254 int flags,
255 bool solidBackground)
2bda0e17 256{
57c208c5 257#ifdef __TWIN32__
4b7f2165
VZ
258 wxFAIL_MSG(_T("ImageList_Replace not implemented in TWIN32"));
259 return FALSE;
57c208c5 260#else
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 {
289 wxLogLastError("ImageList_Draw()");
290 }
291
292 return ok;
57c208c5 293#endif
2bda0e17
KB
294}
295
4b7f2165
VZ
296// ----------------------------------------------------------------------------
297// helpers
298// ----------------------------------------------------------------------------
299
300static wxBitmap GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
301{
302 wxBitmap bmpMask;
303
304 if ( mask.Ok() )
305 {
306 bmpMask = mask;
307 }
308 else
309 {
310 wxMask *pMask = bitmap.GetMask();
311 if ( pMask )
312 {
313 bmpMask.SetHBITMAP(pMask->GetMaskBitmap());
314 }
315 }
316
317 if ( !bmpMask.Ok() )
318 {
319 // create a non transparent mask - apparently, this is needed under
320 // Win9x (it doesn't behave correctly if it's passed 0 mask)
321 bmpMask.Create(bitmap.GetWidth(), bitmap.GetHeight(), 1);
322
323 wxMemoryDC dcMem;
324 dcMem.SelectObject(bmpMask);
325 dcMem.Clear();
326 dcMem.SelectObject(wxNullBitmap);
327 }
328
329 return bmpMask;
330}
331
332#endif // Win95
2bda0e17 333