]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/statbmp.cpp | |
3 | // Purpose: wxStaticBitmap | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_STATBMP | |
28 | ||
29 | #include "wx/statbmp.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/app.h" | |
33 | #include "wx/window.h" | |
34 | #include "wx/icon.h" | |
35 | #include "wx/dcclient.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/msw/private.h" | |
39 | ||
40 | #include "wx/sysopt.h" | |
41 | ||
42 | #include <stdio.h> | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | // macros | |
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | wxBEGIN_EVENT_TABLE(wxStaticBitmap, wxStaticBitmapBase) | |
49 | EVT_SIZE(wxStaticBitmap::WXHandleSize) | |
50 | wxEND_EVENT_TABLE() | |
51 | ||
52 | // =========================================================================== | |
53 | // implementation | |
54 | // =========================================================================== | |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | // wxStaticBitmap | |
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | // we may have either bitmap or icon: if a bitmap with mask is passed, we | |
61 | // will transform it to an icon ourselves because otherwise the mask will | |
62 | // be ignored by Windows | |
63 | // note that this function will create a new object every time | |
64 | // it is called even if the image needs no conversion | |
65 | ||
66 | static wxGDIImage* ConvertImage( const wxGDIImage& bitmap ) | |
67 | { | |
68 | bool isIcon = bitmap.IsKindOf( wxCLASSINFO(wxIcon) ); | |
69 | ||
70 | if( !isIcon ) | |
71 | { | |
72 | wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap), | |
73 | wxT("not an icon and not a bitmap?") ); | |
74 | ||
75 | const wxBitmap& bmp = (const wxBitmap&)bitmap; | |
76 | wxMask *mask = bmp.GetMask(); | |
77 | if ( mask && mask->GetMaskBitmap() ) | |
78 | { | |
79 | wxIcon* icon = new wxIcon; | |
80 | icon->CopyFromBitmap(bmp); | |
81 | ||
82 | return icon; | |
83 | } | |
84 | ||
85 | return new wxBitmap( bmp ); | |
86 | } | |
87 | ||
88 | // copying a bitmap is a cheap operation | |
89 | return new wxIcon( (const wxIcon&)bitmap ); | |
90 | } | |
91 | ||
92 | bool wxStaticBitmap::Create(wxWindow *parent, | |
93 | wxWindowID id, | |
94 | const wxGDIImage& bitmap, | |
95 | const wxPoint& pos, | |
96 | const wxSize& size, | |
97 | long style, | |
98 | const wxString& name) | |
99 | { | |
100 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) | |
101 | return false; | |
102 | ||
103 | // we may have either bitmap or icon: if a bitmap with mask is passed, we | |
104 | // will transform it to an icon ourselves because otherwise the mask will | |
105 | // be ignored by Windows | |
106 | m_isIcon = bitmap.IsKindOf(wxCLASSINFO(wxIcon)); | |
107 | ||
108 | wxGDIImage *image = ConvertImage( bitmap ); | |
109 | m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) ); | |
110 | ||
111 | // create the native control | |
112 | if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString, pos, size) ) | |
113 | { | |
114 | // control creation failed | |
115 | return false; | |
116 | } | |
117 | ||
118 | // no need to delete the new image | |
119 | SetImageNoCopy(image); | |
120 | ||
121 | // GetBestSize will work properly now, so set the best size if needed | |
122 | SetInitialSize(size); | |
123 | ||
124 | // painting manually is reported not to work under Windows CE (see #10093), | |
125 | // so don't do it there even if this probably means that alpha is not | |
126 | // supported there -- but at least bitmaps without alpha appear correctly | |
127 | #ifndef __WXWINCE__ | |
128 | // Windows versions before XP (and even XP if the application has no | |
129 | // manifest and so the old comctl32.dll is used) don't draw correctly the | |
130 | // images with alpha channel so we need to draw them ourselves and it's | |
131 | // easier to just always do it rather than check if we have an image with | |
132 | // alpha or not | |
133 | if ( wxTheApp->GetComCtl32Version() < 600 ) | |
134 | { | |
135 | Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBitmap::DoPaintManually)); | |
136 | } | |
137 | #endif // !__WXWINCE__ | |
138 | ||
139 | return true; | |
140 | } | |
141 | ||
142 | WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const | |
143 | { | |
144 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
145 | ||
146 | // what kind of control are we? | |
147 | msStyle |= m_isIcon ? SS_ICON : SS_BITMAP; | |
148 | ||
149 | // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to | |
150 | // fit to its size -- this is unexpected and doesn't happen in other ports | |
151 | // | |
152 | // and SS_NOTIFY is necessary to receive mouse events | |
153 | msStyle |= SS_CENTERIMAGE | SS_NOTIFY; | |
154 | ||
155 | return msStyle; | |
156 | } | |
157 | ||
158 | bool wxStaticBitmap::ImageIsOk() const | |
159 | { | |
160 | return m_image && m_image->IsOk(); | |
161 | } | |
162 | ||
163 | wxIcon wxStaticBitmap::GetIcon() const | |
164 | { | |
165 | wxCHECK_MSG( m_image, wxIcon(), wxT("no image in wxStaticBitmap") ); | |
166 | ||
167 | // we can't ask for an icon if all we have is a bitmap | |
168 | wxCHECK_MSG( m_isIcon, wxIcon(), wxT("no icon in this wxStaticBitmap") ); | |
169 | ||
170 | return *(wxIcon *)m_image; | |
171 | } | |
172 | ||
173 | wxBitmap wxStaticBitmap::GetBitmap() const | |
174 | { | |
175 | if ( m_isIcon ) | |
176 | { | |
177 | // don't fail because we might have replaced the bitmap with icon | |
178 | // ourselves internally in ConvertImage() to keep the transparency but | |
179 | // the user code doesn't know about it so it still can use GetBitmap() | |
180 | // to retrieve the bitmap | |
181 | return wxBitmap(GetIcon()); | |
182 | } | |
183 | else // we have a bitmap | |
184 | { | |
185 | wxCHECK_MSG( m_image, wxBitmap(), wxT("no image in wxStaticBitmap") ); | |
186 | ||
187 | return *(wxBitmap *)m_image; | |
188 | } | |
189 | } | |
190 | ||
191 | void wxStaticBitmap::Free() | |
192 | { | |
193 | wxDELETE(m_image); | |
194 | } | |
195 | ||
196 | wxSize wxStaticBitmap::DoGetBestClientSize() const | |
197 | { | |
198 | wxSize size; | |
199 | if ( ImageIsOk() ) | |
200 | { | |
201 | size = m_image->GetSize(); | |
202 | } | |
203 | else // No image yet | |
204 | { | |
205 | // this is completely arbitrary | |
206 | size.x = | |
207 | size.y = 16; | |
208 | } | |
209 | ||
210 | return size; | |
211 | } | |
212 | ||
213 | void wxStaticBitmap::WXHandleSize(wxSizeEvent& event) | |
214 | { | |
215 | // Invalidate everything when our size changes as the image position (it's | |
216 | // drawn centred in the window client area) changes. | |
217 | Refresh(); | |
218 | ||
219 | event.Skip(); | |
220 | } | |
221 | ||
222 | #ifndef __WXWINCE__ | |
223 | ||
224 | void wxStaticBitmap::DoPaintManually(wxPaintEvent& WXUNUSED(event)) | |
225 | { | |
226 | wxPaintDC dc(this); | |
227 | ||
228 | const wxSize size(GetSize()); | |
229 | const wxBitmap bmp(GetBitmap()); | |
230 | ||
231 | // Clear the background: notice that we're supposed to be transparent, so | |
232 | // use the parent background colour if we don't have our own instead of | |
233 | // falling back to the default | |
234 | const wxWindow *win = UseBgCol() ? this : GetParent(); | |
235 | dc.SetBrush(win->GetBackgroundColour()); | |
236 | dc.SetPen(*wxTRANSPARENT_PEN); | |
237 | dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight()); | |
238 | ||
239 | // Draw the image in the middle | |
240 | dc.DrawBitmap(bmp, | |
241 | (size.GetWidth() - bmp.GetWidth()) / 2, | |
242 | (size.GetHeight() - bmp.GetHeight()) / 2, | |
243 | true /* use mask */); | |
244 | } | |
245 | ||
246 | #endif // !__WXWINCE__ | |
247 | ||
248 | void wxStaticBitmap::SetImage( const wxGDIImage* image ) | |
249 | { | |
250 | wxGDIImage* convertedImage = ConvertImage( *image ); | |
251 | SetImageNoCopy( convertedImage ); | |
252 | } | |
253 | ||
254 | void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image) | |
255 | { | |
256 | Free(); | |
257 | InvalidateBestSize(); | |
258 | ||
259 | m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) ); | |
260 | // the image has already been copied | |
261 | m_image = image; | |
262 | ||
263 | int x, y; | |
264 | int w, h; | |
265 | GetPosition(&x, &y); | |
266 | GetSize(&w, &h); | |
267 | ||
268 | #ifdef __WIN32__ | |
269 | HANDLE handle = (HANDLE)m_image->GetHandle(); | |
270 | LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ; | |
271 | ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) | | |
272 | ( m_isIcon ? SS_ICON : SS_BITMAP ) ); | |
273 | HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE, | |
274 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); | |
275 | // detect if this is still the handle we passed before or | |
276 | // if the static-control made a copy of the bitmap! | |
277 | if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle) | |
278 | { | |
279 | // the static control made a copy and we are responsible for deleting it | |
280 | DeleteObject((HGDIOBJ) oldHandle); | |
281 | } | |
282 | m_currentHandle = (WXHANDLE)handle; | |
283 | #endif // Win32 | |
284 | ||
285 | if ( ImageIsOk() ) | |
286 | { | |
287 | int width = image->GetWidth(), | |
288 | height = image->GetHeight(); | |
289 | if ( width && height ) | |
290 | { | |
291 | w = width; | |
292 | h = height; | |
293 | ||
294 | ::MoveWindow(GetHwnd(), x, y, width, height, FALSE); | |
295 | } | |
296 | } | |
297 | ||
298 | RECT rect; | |
299 | rect.left = x; | |
300 | rect.top = y; | |
301 | rect.right = x + w; | |
302 | rect.bottom = y + h; | |
303 | ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE); | |
304 | } | |
305 | ||
306 | #endif // wxUSE_STATBMP |