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