]>
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/window.h" | |
33 | #include "wx/icon.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/msw/private.h" | |
37 | ||
38 | #include "wx/sysopt.h" | |
39 | ||
40 | #include <stdio.h> | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | // macors | |
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | #if wxUSE_EXTENDED_RTTI | |
47 | WX_DEFINE_FLAGS( wxStaticBitmapStyle ) | |
48 | ||
49 | wxBEGIN_FLAGS( wxStaticBitmapStyle ) | |
50 | // new style border flags, we put them first to | |
51 | // use them for streaming out | |
52 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
53 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
54 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
55 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
56 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
57 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
58 | ||
59 | // old style border flags | |
60 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
61 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
62 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
63 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
64 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
65 | wxFLAGS_MEMBER(wxBORDER) | |
66 | ||
67 | // standard window styles | |
68 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
69 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
70 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
71 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
72 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
73 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
74 | wxFLAGS_MEMBER(wxVSCROLL) | |
75 | wxFLAGS_MEMBER(wxHSCROLL) | |
76 | ||
77 | wxEND_FLAGS( wxStaticBitmapStyle ) | |
78 | ||
79 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h") | |
80 | ||
81 | wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap) | |
82 | wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
83 | wxEND_PROPERTIES_TABLE() | |
84 | ||
85 | wxBEGIN_HANDLERS_TABLE(wxStaticBitmap) | |
86 | wxEND_HANDLERS_TABLE() | |
87 | ||
88 | wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size ) | |
89 | ||
90 | #else | |
91 | IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl) | |
92 | #endif | |
93 | ||
94 | /* | |
95 | TODO PROPERTIES : | |
96 | bitmap | |
97 | */ | |
98 | ||
99 | // =========================================================================== | |
100 | // implementation | |
101 | // =========================================================================== | |
102 | ||
103 | // --------------------------------------------------------------------------- | |
104 | // wxStaticBitmap | |
105 | // --------------------------------------------------------------------------- | |
106 | ||
107 | // we may have either bitmap or icon: if a bitmap with mask is passed, we | |
108 | // will transform it to an icon ourselves because otherwise the mask will | |
109 | // be ignored by Windows | |
110 | // note that this function will create a new object every time | |
111 | // it is called even if the image needs no conversion | |
112 | ||
113 | static wxGDIImage* ConvertImage( const wxGDIImage& bitmap ) | |
114 | { | |
115 | bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) ); | |
116 | ||
117 | if( !isIcon ) | |
118 | { | |
119 | wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap), | |
120 | _T("not an icon and not a bitmap?") ); | |
121 | ||
122 | const wxBitmap& bmp = (const wxBitmap&)bitmap; | |
123 | wxMask *mask = bmp.GetMask(); | |
124 | if ( mask && mask->GetMaskBitmap() ) | |
125 | { | |
126 | wxIcon* icon = new wxIcon; | |
127 | icon->CopyFromBitmap(bmp); | |
128 | ||
129 | return icon; | |
130 | } | |
131 | ||
132 | return new wxBitmap( bmp ); | |
133 | } | |
134 | ||
135 | // copying a bitmap is a cheap operation | |
136 | return new wxIcon( (const wxIcon&)bitmap ); | |
137 | } | |
138 | ||
139 | bool wxStaticBitmap::Create(wxWindow *parent, | |
140 | wxWindowID id, | |
141 | const wxGDIImage& bitmap, | |
142 | const wxPoint& pos, | |
143 | const wxSize& size, | |
144 | long style, | |
145 | const wxString& name) | |
146 | { | |
147 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) | |
148 | return false; | |
149 | ||
150 | // we may have either bitmap or icon: if a bitmap with mask is passed, we | |
151 | // will transform it to an icon ourselves because otherwise the mask will | |
152 | // be ignored by Windows | |
153 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); | |
154 | ||
155 | wxGDIImage *image = ConvertImage( bitmap ); | |
156 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); | |
157 | ||
158 | // create the native control | |
159 | if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) ) | |
160 | { | |
161 | // control creation failed | |
162 | return false; | |
163 | } | |
164 | ||
165 | // no need to delete the new image | |
166 | SetImageNoCopy(image); | |
167 | ||
168 | // GetBestSize will work properly now, so set the best size if needed | |
169 | SetBestSize(size); | |
170 | ||
171 | return true; | |
172 | } | |
173 | ||
174 | wxBorder wxStaticBitmap::GetDefaultBorder() const | |
175 | { | |
176 | return wxBORDER_NONE; | |
177 | } | |
178 | ||
179 | WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const | |
180 | { | |
181 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
182 | ||
183 | // what kind of control are we? | |
184 | msStyle |= m_isIcon ? SS_ICON : SS_BITMAP; | |
185 | ||
186 | // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to | |
187 | // fit to its size -- this is unexpected and doesn't happen in other ports | |
188 | // | |
189 | // and SS_NOTIFY is necessary to receive mouse events | |
190 | msStyle |= SS_CENTERIMAGE | SS_NOTIFY; | |
191 | ||
192 | return msStyle; | |
193 | } | |
194 | ||
195 | bool wxStaticBitmap::ImageIsOk() const | |
196 | { | |
197 | return m_image && m_image->Ok(); | |
198 | } | |
199 | ||
200 | wxIcon wxStaticBitmap::GetIcon() const | |
201 | { | |
202 | wxCHECK_MSG( m_image, wxIcon(), _T("no image in wxStaticBitmap") ); | |
203 | ||
204 | // we can't ask for an icon if all we have is a bitmap | |
205 | wxCHECK_MSG( m_isIcon, wxIcon(), _T("no icon in this wxStaticBitmap") ); | |
206 | ||
207 | return *(wxIcon *)m_image; | |
208 | } | |
209 | ||
210 | wxBitmap wxStaticBitmap::GetBitmap() const | |
211 | { | |
212 | if ( m_isIcon ) | |
213 | { | |
214 | // don't fail because we might have replaced the bitmap with icon | |
215 | // ourselves internally in ConvertImage() to keep the transparency but | |
216 | // the user code doesn't know about it so it still can use GetBitmap() | |
217 | // to retrieve the bitmap | |
218 | return wxBitmap(GetIcon()); | |
219 | } | |
220 | else // we have a bitmap | |
221 | { | |
222 | wxCHECK_MSG( m_image, wxBitmap(), _T("no image in wxStaticBitmap") ); | |
223 | ||
224 | return *(wxBitmap *)m_image; | |
225 | } | |
226 | } | |
227 | ||
228 | void wxStaticBitmap::Free() | |
229 | { | |
230 | delete m_image; | |
231 | ||
232 | m_image = NULL; | |
233 | } | |
234 | ||
235 | wxSize wxStaticBitmap::DoGetBestSize() const | |
236 | { | |
237 | if ( ImageIsOk() ) | |
238 | { | |
239 | wxSize best(m_image->GetWidth(), m_image->GetHeight()); | |
240 | CacheBestSize(best); | |
241 | return best; | |
242 | } | |
243 | ||
244 | // this is completely arbitrary | |
245 | return wxSize(16, 16); | |
246 | } | |
247 | ||
248 | void wxStaticBitmap::SetImage( const wxGDIImage* image ) | |
249 | { | |
250 | wxGDIImage* convertedImage = ConvertImage( *image ); | |
251 | SetImageNoCopy( convertedImage ); | |
252 | InvalidateBestSize(); | |
253 | } | |
254 | ||
255 | void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image) | |
256 | { | |
257 | Free(); | |
258 | ||
259 | m_isIcon = image->IsKindOf( CLASSINFO(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 |