]> git.saurik.com Git - wxWidgets.git/blame - src/msw/imaglist.cpp
Do not allow multiple selection when dragging in the 'value' column
[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 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4b7f2165
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
4b7f2165 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
2bda0e17 27#ifndef WX_PRECOMP
57bd4c60 28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
4b7f2165
VZ
29 #include "wx/window.h"
30 #include "wx/icon.h"
31 #include "wx/dc.h"
32 #include "wx/string.h"
e0f15f4a 33 #include "wx/dcmemory.h"
88a7a4e1 34 #include "wx/intl.h"
e4db172a 35 #include "wx/log.h"
155ecd4c 36 #include "wx/image.h"
4b7f2165 37 #include <stdio.h>
2bda0e17
KB
38#endif
39
01f512d7 40#include "wx/imaglist.h"
888dde65
RR
41#include "wx/dc.h"
42#include "wx/msw/dc.h"
2bda0e17
KB
43#include "wx/msw/private.h"
44
4b7f2165
VZ
45// ----------------------------------------------------------------------------
46// wxWin macros
47// ----------------------------------------------------------------------------
48
2bda0e17 49IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
2bda0e17 50
4b7f2165
VZ
51#define GetHImageList() ((HIMAGELIST)m_hImageList)
52
53// ----------------------------------------------------------------------------
54// private functions
55// ----------------------------------------------------------------------------
56
57// returns the mask if it's valid, otherwise the bitmap mask and, if it's not
58// valid neither, a "solid" mask (no transparent zones at all)
3ca6a5f0 59static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
4b7f2165
VZ
60
61// ============================================================================
62// implementation
63// ============================================================================
64
65// ----------------------------------------------------------------------------
66// wxImageList creation/destruction
67// ----------------------------------------------------------------------------
68
69wxImageList::wxImageList()
2bda0e17
KB
70{
71 m_hImageList = 0;
72}
73
4b7f2165
VZ
74// Creates an image list
75bool wxImageList::Create(int width, int height, bool mask, int initial)
2bda0e17 76{
a6e2b3a8
VZ
77 UINT flags = 0;
78
79 // set appropriate color depth
4676948b
JS
80#ifdef __WXWINCE__
81 flags |= ILC_COLOR;
82#else
665b71b1
WS
83 int dd = wxDisplayDepth();
84
59af881e
WS
85 if (dd <= 4) flags |= ILC_COLOR; // 16 color
86 else if (dd <= 8) flags |= ILC_COLOR8; // 256 color
87 else if (dd <= 16) flags |= ILC_COLOR16; // 64k hi-color
88 else if (dd <= 24) flags |= ILC_COLOR24; // 16m truecolor
89 else if (dd <= 32) flags |= ILC_COLOR32; // 16m truecolor
4676948b 90#endif
a6e2b3a8 91
4b7f2165
VZ
92 if ( mask )
93 flags |= ILC_MASK;
94
95 // Grow by 1, I guess this is reasonable behaviour most of the time
96 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
97 initial, 1);
98 if ( !m_hImageList )
99 {
f6bcfd97 100 wxLogLastError(wxT("ImageList_Create()"));
4b7f2165 101 }
2bda0e17 102
4b7f2165
VZ
103 return m_hImageList != 0;
104}
2bda0e17 105
4b7f2165 106wxImageList::~wxImageList()
2bda0e17 107{
4b7f2165
VZ
108 if ( m_hImageList )
109 {
110 ImageList_Destroy(GetHImageList());
111 m_hImageList = 0;
112 }
2bda0e17
KB
113}
114
4b7f2165
VZ
115// ----------------------------------------------------------------------------
116// wxImageList attributes
117// ----------------------------------------------------------------------------
2bda0e17 118
4b7f2165
VZ
119// Returns the number of images in the image list.
120int wxImageList::GetImageCount() const
2bda0e17 121{
9a83f860 122 wxASSERT_MSG( m_hImageList, wxT("invalid image list") );
2bda0e17 123
4b7f2165 124 return ImageList_GetImageCount(GetHImageList());
2bda0e17
KB
125}
126
f6bcfd97
BP
127// Returns the size (same for all images) of the images in the list
128bool wxImageList::GetSize(int WXUNUSED(index), int &width, int &height) const
129{
9a83f860 130 wxASSERT_MSG( m_hImageList, wxT("invalid image list") );
f6bcfd97
BP
131
132 return ImageList_GetIconSize(GetHImageList(), &width, &height) != 0;
133}
134
4b7f2165
VZ
135// ----------------------------------------------------------------------------
136// wxImageList operations
137// ----------------------------------------------------------------------------
138
2bda0e17
KB
139// Adds a bitmap, and optionally a mask bitmap.
140// Note that wxImageList creates new bitmaps, so you may delete
141// 'bitmap' and 'mask'.
142int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
143{
3ca6a5f0 144 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
e29b83a4 145
4b7f2165
VZ
146 int index = ImageList_Add(GetHImageList(), GetHbitmapOf(bitmap), hbmpMask);
147 if ( index == -1 )
e29b83a4
VZ
148 {
149 wxLogError(_("Couldn't add an image to the image list."));
150 }
151
4b7f2165 152 ::DeleteObject(hbmpMask);
54b37e2e 153
e29b83a4 154 return index;
2bda0e17
KB
155}
156
157// Adds a bitmap, using the specified colour to create the mask bitmap
158// Note that wxImageList creates new bitmaps, so you may delete
159// 'bitmap'.
160int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
161{
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;
2bda0e17
KB
171}
172
173// Adds a bitmap and mask from an icon.
174int wxImageList::Add(const wxIcon& icon)
175{
4b7f2165
VZ
176 int index = ImageList_AddIcon(GetHImageList(), GetHiconOf(icon));
177 if ( index == -1 )
178 {
179 wxLogError(_("Couldn't add an image to the image list."));
180 }
181
182 return index;
2bda0e17
KB
183}
184
185// Replaces a bitmap, optionally passing a mask bitmap.
186// Note that wxImageList creates new bitmaps, so you may delete
187// 'bitmap' and 'mask'.
4b7f2165
VZ
188bool wxImageList::Replace(int index,
189 const wxBitmap& bitmap, const wxBitmap& mask)
2bda0e17 190{
3ca6a5f0 191 HBITMAP hbmpMask = GetMaskForImage(bitmap, mask);
2bda0e17 192
4b7f2165
VZ
193 bool ok = ImageList_Replace(GetHImageList(), index,
194 GetHbitmapOf(bitmap), hbmpMask) != 0;
195 if ( !ok )
196 {
3103e8a9 197 wxLogLastError(wxT("ImageList_Replace()"));
4b7f2165
VZ
198 }
199
200 ::DeleteObject(hbmpMask);
201
202 return ok;
2bda0e17 203}
2bda0e17
KB
204
205// Replaces a bitmap and mask from an icon.
4b7f2165 206bool wxImageList::Replace(int i, const wxIcon& icon)
2bda0e17 207{
9fe0c2fc 208 bool ok = ImageList_ReplaceIcon(GetHImageList(), i, GetHiconOf(icon)) != -1;
4b7f2165
VZ
209 if ( !ok )
210 {
f6bcfd97 211 wxLogLastError(wxT("ImageList_ReplaceIcon()"));
4b7f2165
VZ
212 }
213
214 return ok;
2bda0e17
KB
215}
216
217// Removes the image at the given index.
debe6624 218bool wxImageList::Remove(int index)
2bda0e17 219{
4b7f2165
VZ
220 bool ok = ImageList_Remove(GetHImageList(), index) != 0;
221 if ( !ok )
222 {
f6bcfd97 223 wxLogLastError(wxT("ImageList_Remove()"));
4b7f2165
VZ
224 }
225
226 return ok;
2bda0e17
KB
227}
228
229// Remove all images
4b7f2165 230bool wxImageList::RemoveAll()
2bda0e17 231{
3c1a88d8 232 // don't use ImageList_RemoveAll() because mingw32 headers don't have it
1c304f30 233 return Remove(-1);
2bda0e17
KB
234}
235
236// Draws the given image on a dc at the specified position.
59af881e 237// If 'solidBackground' is true, Draw sets the image list background
2bda0e17
KB
238// colour to the background colour of the wxDC, to speed up
239// drawing by eliminating masked drawing where possible.
4b7f2165
VZ
240bool wxImageList::Draw(int index,
241 wxDC& dc,
242 int x, int y,
243 int flags,
244 bool solidBackground)
2bda0e17 245{
888dde65
RR
246 wxDCImpl *impl = dc.GetImpl();
247 wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl );
248 if (!msw_impl)
249 return false;
03647350 250
888dde65 251 HDC hDC = GetHdcOf(*msw_impl);
9a83f860 252 wxCHECK_MSG( hDC, false, wxT("invalid wxDC in wxImageList::Draw") );
4b7f2165
VZ
253
254 COLORREF clr = CLR_NONE; // transparent by default
255 if ( solidBackground )
256 {
b33576e1
VZ
257 const wxBrush& brush = dc.GetBackground();
258 if ( brush.Ok() )
4b7f2165 259 {
b33576e1 260 clr = wxColourToRGB(brush.GetColour());
4b7f2165
VZ
261 }
262 }
263
264 ImageList_SetBkColor(GetHImageList(), clr);
2bda0e17
KB
265
266 UINT style = 0;
4b7f2165
VZ
267 if ( flags & wxIMAGELIST_DRAW_NORMAL )
268 style |= ILD_NORMAL;
269 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
270 style |= ILD_TRANSPARENT;
271 if ( flags & wxIMAGELIST_DRAW_SELECTED )
272 style |= ILD_SELECTED;
273 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
274 style |= ILD_FOCUS;
275
276 bool ok = ImageList_Draw(GetHImageList(), index, hDC, x, y, style) != 0;
277 if ( !ok )
278 {
f6bcfd97 279 wxLogLastError(wxT("ImageList_Draw()"));
4b7f2165
VZ
280 }
281
282 return ok;
2bda0e17
KB
283}
284
49bf4e3e
JS
285// Get the bitmap
286wxBitmap wxImageList::GetBitmap(int index) const
287{
a8ff046b 288#if wxUSE_WXDIB && wxUSE_IMAGE
49bf4e3e
JS
289 int bmp_width = 0, bmp_height = 0;
290 GetSize(index, bmp_width, bmp_height);
291
292 wxBitmap bitmap(bmp_width, bmp_height);
293 wxMemoryDC dc;
294 dc.SelectObject(bitmap);
295
296 // draw it the first time to find a suitable mask colour
297 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
298 dc.SelectObject(wxNullBitmap);
299
300 // find the suitable mask colour
301 wxImage image = bitmap.ConvertToImage();
302 unsigned char r = 0, g = 0, b = 0;
303 image.FindFirstUnusedColour(&r, &g, &b);
304
305 // redraw whole image and bitmap in the mask colour
306 image.Create(bmp_width, bmp_height);
307 image.Replace(0, 0, 0, r, g, b);
308 bitmap = wxBitmap(image);
309
310 // redraw icon over the mask colour to actually draw it
311 dc.SelectObject(bitmap);
312 ((wxImageList*)this)->Draw(index, dc, 0, 0, wxIMAGELIST_DRAW_TRANSPARENT);
313 dc.SelectObject(wxNullBitmap);
314
315 // get the image, set the mask colour and convert back to get transparent bitmap
316 image = bitmap.ConvertToImage();
317 image.SetMaskColour(r, g, b);
318 bitmap = wxBitmap(image);
64c288fa
JS
319#else
320 wxBitmap bitmap;
64c288fa 321#endif
74aa8254 322 return bitmap;
49bf4e3e
JS
323}
324
325// Get the icon
326wxIcon wxImageList::GetIcon(int index) const
327{
328 HICON hIcon = ImageList_ExtractIcon(0, GetHImageList(), index);
329 if (hIcon)
330 {
331 wxIcon icon;
332 icon.SetHICON((WXHICON)hIcon);
a71d815b 333
49bf4e3e
JS
334 int iconW, iconH;
335 GetSize(index, iconW, iconH);
336 icon.SetSize(iconW, iconH);
a71d815b 337
49bf4e3e
JS
338 return icon;
339 }
a71d815b 340 else
49bf4e3e
JS
341 return wxNullIcon;
342}
343
4b7f2165
VZ
344// ----------------------------------------------------------------------------
345// helpers
346// ----------------------------------------------------------------------------
347
3ca6a5f0 348static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
4b7f2165 349{
e4a84806
VZ
350#if wxUSE_IMAGE
351 wxBitmap bitmapWithMask;
352#endif // wxUSE_IMAGE
353
3ca6a5f0 354 HBITMAP hbmpMask;
90c1530a 355 wxMask *pMask;
59af881e 356 bool deleteMask = false;
4b7f2165
VZ
357
358 if ( mask.Ok() )
359 {
3ca6a5f0 360 hbmpMask = GetHbitmapOf(mask);
90c1530a 361 pMask = NULL;
4b7f2165
VZ
362 }
363 else
364 {
90c1530a 365 pMask = bitmap.GetMask();
e4a84806
VZ
366
367#if wxUSE_IMAGE
368 // check if we don't have alpha in this bitmap -- we can create a mask
369 // from it (and we need to do it for the older systems which don't
370 // support 32bpp bitmaps natively)
371 if ( !pMask )
372 {
373 wxImage img(bitmap.ConvertToImage());
374 if ( img.HasAlpha() )
375 {
376 img.ConvertAlphaToMask();
377 bitmapWithMask = wxBitmap(img);
378 pMask = bitmapWithMask.GetMask();
379 }
380 }
381#endif // wxUSE_IMAGE
382
90c1530a 383 if ( !pMask )
4b7f2165 384 {
90c1530a
VZ
385 // use the light grey count as transparent: the trouble here is
386 // that the light grey might have been changed by Windows behind
387 // our back, so use the standard colour map to get its real value
388 wxCOLORMAP *cmap = wxGetStdColourMap();
389 wxColour col;
390 wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
4b7f2165 391
90c1530a 392 pMask = new wxMask(bitmap, col);
3ca6a5f0 393
59af881e 394 deleteMask = true;
3ca6a5f0 395 }
90c1530a
VZ
396
397 hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
4b7f2165
VZ
398 }
399
77ffb593 400 // windows mask convention is opposite to the wxWidgets one
3ca6a5f0 401 HBITMAP hbmpMaskInv = wxInvertMask(hbmpMask);
90c1530a
VZ
402
403 if ( deleteMask )
404 {
405 delete pMask;
406 }
3ca6a5f0
BP
407
408 return hbmpMaskInv;
4b7f2165 409}