]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
cdccdfab | 2 | // Name: src/msw/statbmp.cpp |
2bda0e17 KB |
3 | // Purpose: wxStaticBitmap |
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 | ||
9e3e0821 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__ | |
9e3e0821 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_STATBMP |
28 | ||
cdccdfab | 29 | #include "wx/statbmp.h" |
0c589ad0 | 30 | |
2bda0e17 | 31 | #ifndef WX_PRECOMP |
cdccdfab | 32 | #include "wx/window.h" |
0c589ad0 | 33 | #include "wx/icon.h" |
30724d04 | 34 | #include "wx/dcclient.h" |
2bda0e17 KB |
35 | #endif |
36 | ||
cdccdfab WS |
37 | #include "wx/msw/private.h" |
38 | ||
9dabade2 JS |
39 | #include "wx/sysopt.h" |
40 | ||
2bda0e17 | 41 | #include <stdio.h> |
2bda0e17 | 42 | |
9e3e0821 VZ |
43 | // --------------------------------------------------------------------------- |
44 | // macors | |
45 | // --------------------------------------------------------------------------- | |
46 | ||
bc9fb572 JS |
47 | #if wxUSE_EXTENDED_RTTI |
48 | WX_DEFINE_FLAGS( wxStaticBitmapStyle ) | |
49 | ||
3ff066a4 | 50 | wxBEGIN_FLAGS( wxStaticBitmapStyle ) |
bc9fb572 JS |
51 | // new style border flags, we put them first to |
52 | // use them for streaming out | |
3ff066a4 SC |
53 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
54 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
55 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
56 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
57 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
58 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
57f4f925 | 59 | |
bc9fb572 | 60 | // old style border flags |
3ff066a4 SC |
61 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
62 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
63 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
64 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
65 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 66 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
67 | |
68 | // standard window styles | |
3ff066a4 SC |
69 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
70 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
71 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
72 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 73 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
74 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
75 | wxFLAGS_MEMBER(wxVSCROLL) | |
76 | wxFLAGS_MEMBER(wxHSCROLL) | |
bc9fb572 | 77 | |
3ff066a4 | 78 | wxEND_FLAGS( wxStaticBitmapStyle ) |
bc9fb572 JS |
79 | |
80 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h") | |
81 | ||
3ff066a4 | 82 | wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap) |
af498247 | 83 | wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 | 84 | wxEND_PROPERTIES_TABLE() |
bc9fb572 | 85 | |
3ff066a4 SC |
86 | wxBEGIN_HANDLERS_TABLE(wxStaticBitmap) |
87 | wxEND_HANDLERS_TABLE() | |
bc9fb572 | 88 | |
3ff066a4 | 89 | wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size ) |
bc9fb572 JS |
90 | |
91 | #else | |
4004f41e | 92 | IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl) |
bc9fb572 | 93 | #endif |
2bda0e17 | 94 | |
066f1b7a | 95 | /* |
57f4f925 WS |
96 | TODO PROPERTIES : |
97 | bitmap | |
066f1b7a SC |
98 | */ |
99 | ||
9e3e0821 VZ |
100 | // =========================================================================== |
101 | // implementation | |
102 | // =========================================================================== | |
103 | ||
104 | // --------------------------------------------------------------------------- | |
105 | // wxStaticBitmap | |
106 | // --------------------------------------------------------------------------- | |
2bda0e17 | 107 | |
d8bffc13 MB |
108 | // we may have either bitmap or icon: if a bitmap with mask is passed, we |
109 | // will transform it to an icon ourselves because otherwise the mask will | |
110 | // be ignored by Windows | |
111 | // note that this function will create a new object every time | |
112 | // it is called even if the image needs no conversion | |
113 | ||
d8bffc13 MB |
114 | static wxGDIImage* ConvertImage( const wxGDIImage& bitmap ) |
115 | { | |
116 | bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) ); | |
117 | ||
118 | if( !isIcon ) | |
119 | { | |
120 | wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap), | |
121 | _T("not an icon and not a bitmap?") ); | |
122 | ||
123 | const wxBitmap& bmp = (const wxBitmap&)bitmap; | |
124 | wxMask *mask = bmp.GetMask(); | |
125 | if ( mask && mask->GetMaskBitmap() ) | |
126 | { | |
127 | wxIcon* icon = new wxIcon; | |
128 | icon->CopyFromBitmap(bmp); | |
129 | ||
130 | return icon; | |
131 | } | |
132 | ||
133 | return new wxBitmap( bmp ); | |
134 | } | |
135 | ||
136 | // copying a bitmap is a cheap operation | |
137 | return new wxIcon( (const wxIcon&)bitmap ); | |
138 | } | |
139 | ||
46a5e01e VZ |
140 | bool wxStaticBitmap::Create(wxWindow *parent, |
141 | wxWindowID id, | |
0d0512bd | 142 | const wxGDIImage& bitmap, |
9e3e0821 VZ |
143 | const wxPoint& pos, |
144 | const wxSize& size, | |
145 | long style, | |
146 | const wxString& name) | |
2bda0e17 | 147 | { |
46a5e01e | 148 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
57f4f925 | 149 | return false; |
9e3e0821 | 150 | |
4004f41e VZ |
151 | // we may have either bitmap or icon: if a bitmap with mask is passed, we |
152 | // will transform it to an icon ourselves because otherwise the mask will | |
153 | // be ignored by Windows | |
9e3e0821 | 154 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); |
669f7a11 | 155 | |
5cb598ae | 156 | wxGDIImage *image = ConvertImage( bitmap ); |
d8bffc13 | 157 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); |
9e3e0821 | 158 | |
46a5e01e | 159 | // create the native control |
3a5bcc4d | 160 | if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) ) |
46a5e01e VZ |
161 | { |
162 | // control creation failed | |
3a5bcc4d | 163 | return false; |
46a5e01e | 164 | } |
9e3e0821 | 165 | |
d8bffc13 | 166 | // no need to delete the new image |
46a5e01e | 167 | SetImageNoCopy(image); |
9e3e0821 | 168 | |
9d17ee60 | 169 | // GetBestSize will work properly now, so set the best size if needed |
170acdc9 | 170 | SetInitialSize(size); |
57f4f925 | 171 | |
a73a4ab7 VZ |
172 | // Win9x and 2000 don't draw correctly the images with alpha channel so we |
173 | // need to draw them ourselves and it's easier to just always do it rather | |
174 | // than check if we have an image with alpha or not | |
175 | if ( wxGetWinVersion() <= wxWinVersion_2000 ) | |
176 | { | |
177 | Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBitmap::DoPaintManually)); | |
178 | } | |
179 | ||
57f4f925 | 180 | return true; |
46a5e01e | 181 | } |
9e3e0821 | 182 | |
46a5e01e VZ |
183 | WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const |
184 | { | |
185 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
9e3e0821 | 186 | |
46a5e01e VZ |
187 | // what kind of control are we? |
188 | msStyle |= m_isIcon ? SS_ICON : SS_BITMAP; | |
9e3e0821 | 189 | |
46a5e01e VZ |
190 | // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to |
191 | // fit to its size -- this is unexpected and doesn't happen in other ports | |
42b1fb63 VZ |
192 | // |
193 | // and SS_NOTIFY is necessary to receive mouse events | |
194 | msStyle |= SS_CENTERIMAGE | SS_NOTIFY; | |
46a5e01e VZ |
195 | |
196 | return msStyle; | |
9e3e0821 VZ |
197 | } |
198 | ||
199 | bool wxStaticBitmap::ImageIsOk() const | |
200 | { | |
0d0512bd | 201 | return m_image && m_image->Ok(); |
9e3e0821 VZ |
202 | } |
203 | ||
333c8697 VZ |
204 | wxIcon wxStaticBitmap::GetIcon() const |
205 | { | |
206 | wxCHECK_MSG( m_image, wxIcon(), _T("no image in wxStaticBitmap") ); | |
207 | ||
208 | // we can't ask for an icon if all we have is a bitmap | |
209 | wxCHECK_MSG( m_isIcon, wxIcon(), _T("no icon in this wxStaticBitmap") ); | |
210 | ||
211 | return *(wxIcon *)m_image; | |
212 | } | |
213 | ||
214 | wxBitmap wxStaticBitmap::GetBitmap() const | |
215 | { | |
216 | if ( m_isIcon ) | |
217 | { | |
218 | // don't fail because we might have replaced the bitmap with icon | |
219 | // ourselves internally in ConvertImage() to keep the transparency but | |
220 | // the user code doesn't know about it so it still can use GetBitmap() | |
221 | // to retrieve the bitmap | |
222 | return wxBitmap(GetIcon()); | |
223 | } | |
224 | else // we have a bitmap | |
225 | { | |
226 | wxCHECK_MSG( m_image, wxBitmap(), _T("no image in wxStaticBitmap") ); | |
227 | ||
228 | return *(wxBitmap *)m_image; | |
229 | } | |
230 | } | |
231 | ||
9e3e0821 VZ |
232 | void wxStaticBitmap::Free() |
233 | { | |
0d0512bd | 234 | delete m_image; |
9e3e0821 | 235 | |
0d0512bd | 236 | m_image = NULL; |
2bda0e17 KB |
237 | } |
238 | ||
9d17ee60 RD |
239 | wxSize wxStaticBitmap::DoGetBestSize() const |
240 | { | |
241 | if ( ImageIsOk() ) | |
31582e4e RD |
242 | { |
243 | wxSize best(m_image->GetWidth(), m_image->GetHeight()); | |
244 | CacheBestSize(best); | |
245 | return best; | |
246 | } | |
9d17ee60 RD |
247 | |
248 | // this is completely arbitrary | |
249 | return wxSize(16, 16); | |
250 | } | |
251 | ||
a73a4ab7 VZ |
252 | void wxStaticBitmap::DoPaintManually(wxPaintEvent& WXUNUSED(event)) |
253 | { | |
254 | wxPaintDC dc(this); | |
255 | ||
256 | const wxSize size(GetSize()); | |
257 | const wxBitmap bmp(GetBitmap()); | |
258 | ||
259 | // Clear the background | |
260 | dc.SetBrush(GetBackgroundColour()); | |
261 | dc.SetPen(*wxTRANSPARENT_PEN); | |
262 | dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight()); | |
263 | ||
264 | // Draw the image in the middle | |
265 | dc.DrawBitmap(bmp, | |
266 | (size.GetWidth() - bmp.GetWidth()) / 2, | |
267 | (size.GetHeight() - bmp.GetHeight()) / 2, | |
268 | true /* use mask */); | |
269 | } | |
270 | ||
d8bffc13 | 271 | void wxStaticBitmap::SetImage( const wxGDIImage* image ) |
2bda0e17 | 272 | { |
d8bffc13 MB |
273 | wxGDIImage* convertedImage = ConvertImage( *image ); |
274 | SetImageNoCopy( convertedImage ); | |
9f884528 | 275 | InvalidateBestSize(); |
d8bffc13 | 276 | } |
4004f41e | 277 | |
d8bffc13 MB |
278 | void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image) |
279 | { | |
280 | Free(); | |
4004f41e | 281 | |
d8bffc13 MB |
282 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); |
283 | // the image has already been copied | |
284 | m_image = image; | |
9e3e0821 VZ |
285 | |
286 | int x, y; | |
287 | int w, h; | |
288 | GetPosition(&x, &y); | |
289 | GetSize(&w, &h); | |
9e3e0821 VZ |
290 | |
291 | #ifdef __WIN32__ | |
0d0512bd | 292 | HANDLE handle = (HANDLE)m_image->GetHandle(); |
d8bffc13 MB |
293 | LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ; |
294 | ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) | | |
295 | ( m_isIcon ? SS_ICON : SS_BITMAP ) ); | |
3dece6c4 | 296 | HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE, |
9e3e0821 | 297 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); |
3dece6c4 JS |
298 | // detect if this is still the handle we passed before or |
299 | // if the static-control made a copy of the bitmap! | |
4fe41ce6 | 300 | if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle) |
3dece6c4 JS |
301 | { |
302 | // the static control made a copy and we are responsible for deleting it | |
cdccdfab | 303 | DeleteObject((HGDIOBJ) oldHandle); |
3dece6c4 | 304 | } |
cdccdfab | 305 | m_currentHandle = (WXHANDLE)handle; |
9e3e0821 VZ |
306 | #endif // Win32 |
307 | ||
308 | if ( ImageIsOk() ) | |
309 | { | |
4004f41e VZ |
310 | int width = image->GetWidth(), |
311 | height = image->GetHeight(); | |
9e3e0821 VZ |
312 | if ( width && height ) |
313 | { | |
7c545786 VZ |
314 | w = width; |
315 | h = height; | |
316 | ||
0d0512bd | 317 | ::MoveWindow(GetHwnd(), x, y, width, height, FALSE); |
9e3e0821 VZ |
318 | } |
319 | } | |
320 | ||
0d0512bd VZ |
321 | RECT rect; |
322 | rect.left = x; | |
323 | rect.top = y; | |
324 | rect.right = x + w; | |
325 | rect.bottom = y + h; | |
57f4f925 | 326 | ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE); |
2bda0e17 KB |
327 | } |
328 | ||
1e6feb95 | 329 | #endif // wxUSE_STATBMP |