]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
help search is much faster now (7 times! that's what I call optimization ;-)
[wxWidgets.git] / src / msw / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imaglist.cpp
3 // Purpose: wxImageList
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "imaglist.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if defined(__WIN95__)
24
25 #ifndef WX_PRECOMP
26 #include <stdio.h>
27 #include "wx/setup.h"
28 #include "wx/window.h"
29 #include "wx/icon.h"
30 #include "wx/dc.h"
31 #include "wx/string.h"
32 #endif
33
34 #include "wx/log.h"
35 #include "wx/intl.h"
36
37 #include "wx/msw/imaglist.h"
38 #include "wx/msw/private.h"
39
40 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
41 #include <commctrl.h>
42 #endif
43
44 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
45
46 wxImageList::wxImageList(void)
47 {
48 m_hImageList = 0;
49 }
50
51 wxImageList::~wxImageList(void)
52 {
53 if ( m_hImageList )
54 ImageList_Destroy((HIMAGELIST) m_hImageList);
55 m_hImageList = 0;
56 }
57
58
59 // Attributes
60 ////////////////////////////////////////////////////////////////////////////
61
62 // Returns the number of images in the image list.
63 int wxImageList::GetImageCount(void) const
64 {
65 return ImageList_GetImageCount((HIMAGELIST) m_hImageList);
66 }
67
68 // Operations
69 ////////////////////////////////////////////////////////////////////////////
70
71 // Creates an image list
72 bool wxImageList::Create(int width, int height, bool mask, int initial)
73 {
74 UINT flags = 0;
75 if ( mask )
76 flags |= ILC_MASK;
77
78 // Grow by 1, I guess this is reasonable behaviour most of the time
79 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags, initial, 1);
80 return (m_hImageList != 0);
81 }
82
83 // Adds a bitmap, and optionally a mask bitmap.
84 // Note that wxImageList creates new bitmaps, so you may delete
85 // 'bitmap' and 'mask'.
86 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
87 {
88 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
89 HBITMAP hBitmap2 = 0;
90 if ( mask.Ok() )
91 hBitmap2 = (HBITMAP) mask.GetHBITMAP();
92 else if (bitmap.GetMask())
93 hBitmap2 = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
94
95 HBITMAP hBitmapI=0;
96 if(hBitmap2!=0) {
97 // Microsoft imagelist masks are inverted from wxWindows mask standard (white is mask color)
98 BITMAP bm;
99 ::GetObject(hBitmap2,sizeof(BITMAP),(LPVOID)&bm);
100 int w=bm.bmWidth;
101 int h=bm.bmHeight;
102 HDC hdc = ::CreateCompatibleDC(NULL);
103 HDC hdci = ::CreateCompatibleDC(NULL);
104 hBitmapI = ::CreateCompatibleBitmap(hdci, w, h);
105 ::SelectObject(hdc, hBitmap2);
106 ::SelectObject(hdci, hBitmapI);
107 ::BitBlt(hdci, 0, 0, w, h, hdc, 0, 0, NOTSRCCOPY);
108 ::DeleteDC(hdc);
109 ::DeleteDC(hdci);
110 }
111
112 int index = ImageList_Add((HIMAGELIST) GetHIMAGELIST(), hBitmap1, hBitmapI);
113 if ( index == -1 )
114 {
115 wxLogError(_("Couldn't add an image to the image list."));
116 }
117
118 // Clean up inverted mask
119 if(hBitmapI!=0)
120 ::DeleteObject(hBitmapI);
121
122 return index;
123 }
124
125 // Adds a bitmap, using the specified colour to create the mask bitmap
126 // Note that wxImageList creates new bitmaps, so you may delete
127 // 'bitmap'.
128 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
129 {
130 #ifdef __TWIN32__
131 wxFAIL_MSG("ImageList_AddMasked not implemented in TWIN32");
132 return -1;
133 #else
134 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
135 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
136 return ImageList_AddMasked((HIMAGELIST) GetHIMAGELIST(), hBitmap1, colorRef);
137 #endif
138 }
139
140 // Adds a bitmap and mask from an icon.
141 int wxImageList::Add(const wxIcon& icon)
142 {
143 HICON hIcon = (HICON) icon.GetHICON();
144 return ImageList_AddIcon((HIMAGELIST) GetHIMAGELIST(), hIcon);
145 }
146
147 // Replaces a bitmap, optionally passing a mask bitmap.
148 // Note that wxImageList creates new bitmaps, so you may delete
149 // 'bitmap' and 'mask'.
150 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxBitmap& mask)
151 {
152 #ifdef __TWIN32__
153 wxFAIL_MSG("ImageList_Replace not implemented in TWIN32");
154 return FALSE;
155 #else
156 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
157 HBITMAP hBitmap2 = 0;
158 if ( mask.Ok() )
159 hBitmap2 = (HBITMAP) mask.GetHBITMAP();
160 return (ImageList_Replace((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, hBitmap2) != 0);
161 #endif
162 }
163
164 /* Not supported by Win95
165 // Replacing a bitmap, using the specified colour to create the mask bitmap
166 // Note that wxImageList creates new bitmaps, so you may delete
167 // 'bitmap'.
168 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxColour& maskColour)
169 {
170 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
171 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
172 return (bool) ImageList_ReplaceMasked((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, colorRef);
173 }
174 */
175
176 // Replaces a bitmap and mask from an icon.
177 bool wxImageList::Replace(int index, const wxIcon& icon)
178 {
179 HICON hIcon = (HICON) icon.GetHICON();
180 return (ImageList_ReplaceIcon((HIMAGELIST) GetHIMAGELIST(), index, hIcon) != 0);
181 }
182
183 // Removes the image at the given index.
184 bool wxImageList::Remove(int index)
185 {
186 #ifdef __TWIN32__
187 wxFAIL_MSG("ImageList_Replace not implemented in TWIN32");
188 return FALSE;
189 #else
190 return (ImageList_Remove((HIMAGELIST) GetHIMAGELIST(), index) != 0);
191 #endif
192 }
193
194 // Remove all images
195 bool wxImageList::RemoveAll(void)
196 {
197 // TODO: Is this correct?
198 while ( GetImageCount() > 0 )
199 {
200 Remove(0);
201 }
202 return TRUE;
203 }
204
205 // Draws the given image on a dc at the specified position.
206 // If 'solidBackground' is TRUE, Draw sets the image list background
207 // colour to the background colour of the wxDC, to speed up
208 // drawing by eliminating masked drawing where possible.
209 bool wxImageList::Draw(int index, wxDC& dc, int x, int y,
210 int flags, bool solidBackground)
211 {
212 #ifdef __TWIN32__
213 wxFAIL_MSG("ImageList_Replace not implemented in TWIN32");
214 return FALSE;
215 #else
216 HDC hDC = (HDC) dc.GetHDC();
217 if ( !hDC )
218 return FALSE;
219
220 if ( solidBackground )
221 {
222 wxBrush *brush = & dc.GetBackground();
223 if ( brush && brush->Ok())
224 {
225 wxColour col(brush->GetColour());
226 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
227 PALETTERGB(col.Red(), col.Green(), col.Blue()));
228 }
229 else
230 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
231 CLR_NONE);
232 }
233 else
234 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
235 CLR_NONE);
236
237 UINT style = 0;
238 if ( flags & wxIMAGELIST_DRAW_NORMAL )
239 style |= ILD_NORMAL;
240 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
241 style |= ILD_TRANSPARENT;
242 if ( flags & wxIMAGELIST_DRAW_SELECTED )
243 style |= ILD_SELECTED;
244 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
245 style |= ILD_FOCUS;
246
247 return (ImageList_Draw((HIMAGELIST) GetHIMAGELIST(), index, hDC,
248 x, y, style) != 0);
249 #endif
250 }
251
252 #endif
253