]>
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" |
2bda0e17 KB |
34 | #endif |
35 | ||
cdccdfab WS |
36 | #include "wx/msw/private.h" |
37 | ||
9dabade2 JS |
38 | #include "wx/sysopt.h" |
39 | ||
2bda0e17 | 40 | #include <stdio.h> |
2bda0e17 | 41 | |
9e3e0821 VZ |
42 | // --------------------------------------------------------------------------- |
43 | // macors | |
44 | // --------------------------------------------------------------------------- | |
45 | ||
bc9fb572 JS |
46 | #if wxUSE_EXTENDED_RTTI |
47 | WX_DEFINE_FLAGS( wxStaticBitmapStyle ) | |
48 | ||
3ff066a4 | 49 | wxBEGIN_FLAGS( wxStaticBitmapStyle ) |
bc9fb572 JS |
50 | // new style border flags, we put them first to |
51 | // use them for streaming out | |
3ff066a4 SC |
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) | |
57f4f925 | 58 | |
bc9fb572 | 59 | // old style border flags |
3ff066a4 SC |
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) | |
cb0afb26 | 65 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
66 | |
67 | // standard window styles | |
3ff066a4 SC |
68 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
69 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
70 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
71 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 72 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
73 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
74 | wxFLAGS_MEMBER(wxVSCROLL) | |
75 | wxFLAGS_MEMBER(wxHSCROLL) | |
bc9fb572 | 76 | |
3ff066a4 | 77 | wxEND_FLAGS( wxStaticBitmapStyle ) |
bc9fb572 JS |
78 | |
79 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h") | |
80 | ||
3ff066a4 | 81 | wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap) |
af498247 | 82 | wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 | 83 | wxEND_PROPERTIES_TABLE() |
bc9fb572 | 84 | |
3ff066a4 SC |
85 | wxBEGIN_HANDLERS_TABLE(wxStaticBitmap) |
86 | wxEND_HANDLERS_TABLE() | |
bc9fb572 | 87 | |
3ff066a4 | 88 | wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size ) |
bc9fb572 JS |
89 | |
90 | #else | |
4004f41e | 91 | IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl) |
bc9fb572 | 92 | #endif |
2bda0e17 | 93 | |
066f1b7a | 94 | /* |
57f4f925 WS |
95 | TODO PROPERTIES : |
96 | bitmap | |
066f1b7a SC |
97 | */ |
98 | ||
9e3e0821 VZ |
99 | // =========================================================================== |
100 | // implementation | |
101 | // =========================================================================== | |
102 | ||
103 | // --------------------------------------------------------------------------- | |
104 | // wxStaticBitmap | |
105 | // --------------------------------------------------------------------------- | |
2bda0e17 | 106 | |
d8bffc13 MB |
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 | ||
d8bffc13 MB |
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 | ||
46a5e01e VZ |
139 | bool wxStaticBitmap::Create(wxWindow *parent, |
140 | wxWindowID id, | |
0d0512bd | 141 | const wxGDIImage& bitmap, |
9e3e0821 VZ |
142 | const wxPoint& pos, |
143 | const wxSize& size, | |
144 | long style, | |
145 | const wxString& name) | |
2bda0e17 | 146 | { |
46a5e01e | 147 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
57f4f925 | 148 | return false; |
9e3e0821 | 149 | |
4004f41e VZ |
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 | |
9e3e0821 | 153 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); |
669f7a11 | 154 | |
5cb598ae | 155 | wxGDIImage *image = ConvertImage( bitmap ); |
d8bffc13 | 156 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); |
9e3e0821 | 157 | |
46a5e01e | 158 | // create the native control |
3a5bcc4d | 159 | if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) ) |
46a5e01e VZ |
160 | { |
161 | // control creation failed | |
3a5bcc4d | 162 | return false; |
46a5e01e | 163 | } |
9e3e0821 | 164 | |
d8bffc13 | 165 | // no need to delete the new image |
46a5e01e | 166 | SetImageNoCopy(image); |
9e3e0821 | 167 | |
9d17ee60 | 168 | // GetBestSize will work properly now, so set the best size if needed |
170acdc9 | 169 | SetInitialSize(size); |
57f4f925 WS |
170 | |
171 | return true; | |
46a5e01e | 172 | } |
9e3e0821 | 173 | |
46a5e01e VZ |
174 | WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const |
175 | { | |
176 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
9e3e0821 | 177 | |
46a5e01e VZ |
178 | // what kind of control are we? |
179 | msStyle |= m_isIcon ? SS_ICON : SS_BITMAP; | |
9e3e0821 | 180 | |
46a5e01e VZ |
181 | // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to |
182 | // fit to its size -- this is unexpected and doesn't happen in other ports | |
42b1fb63 VZ |
183 | // |
184 | // and SS_NOTIFY is necessary to receive mouse events | |
185 | msStyle |= SS_CENTERIMAGE | SS_NOTIFY; | |
46a5e01e VZ |
186 | |
187 | return msStyle; | |
9e3e0821 VZ |
188 | } |
189 | ||
190 | bool wxStaticBitmap::ImageIsOk() const | |
191 | { | |
0d0512bd | 192 | return m_image && m_image->Ok(); |
9e3e0821 VZ |
193 | } |
194 | ||
333c8697 VZ |
195 | wxIcon wxStaticBitmap::GetIcon() const |
196 | { | |
197 | wxCHECK_MSG( m_image, wxIcon(), _T("no image in wxStaticBitmap") ); | |
198 | ||
199 | // we can't ask for an icon if all we have is a bitmap | |
200 | wxCHECK_MSG( m_isIcon, wxIcon(), _T("no icon in this wxStaticBitmap") ); | |
201 | ||
202 | return *(wxIcon *)m_image; | |
203 | } | |
204 | ||
205 | wxBitmap wxStaticBitmap::GetBitmap() const | |
206 | { | |
207 | if ( m_isIcon ) | |
208 | { | |
209 | // don't fail because we might have replaced the bitmap with icon | |
210 | // ourselves internally in ConvertImage() to keep the transparency but | |
211 | // the user code doesn't know about it so it still can use GetBitmap() | |
212 | // to retrieve the bitmap | |
213 | return wxBitmap(GetIcon()); | |
214 | } | |
215 | else // we have a bitmap | |
216 | { | |
217 | wxCHECK_MSG( m_image, wxBitmap(), _T("no image in wxStaticBitmap") ); | |
218 | ||
219 | return *(wxBitmap *)m_image; | |
220 | } | |
221 | } | |
222 | ||
9e3e0821 VZ |
223 | void wxStaticBitmap::Free() |
224 | { | |
0d0512bd | 225 | delete m_image; |
9e3e0821 | 226 | |
0d0512bd | 227 | m_image = NULL; |
2bda0e17 KB |
228 | } |
229 | ||
9d17ee60 RD |
230 | wxSize wxStaticBitmap::DoGetBestSize() const |
231 | { | |
232 | if ( ImageIsOk() ) | |
31582e4e RD |
233 | { |
234 | wxSize best(m_image->GetWidth(), m_image->GetHeight()); | |
235 | CacheBestSize(best); | |
236 | return best; | |
237 | } | |
9d17ee60 RD |
238 | |
239 | // this is completely arbitrary | |
240 | return wxSize(16, 16); | |
241 | } | |
242 | ||
d8bffc13 | 243 | void wxStaticBitmap::SetImage( const wxGDIImage* image ) |
2bda0e17 | 244 | { |
d8bffc13 MB |
245 | wxGDIImage* convertedImage = ConvertImage( *image ); |
246 | SetImageNoCopy( convertedImage ); | |
9f884528 | 247 | InvalidateBestSize(); |
d8bffc13 | 248 | } |
4004f41e | 249 | |
d8bffc13 MB |
250 | void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image) |
251 | { | |
252 | Free(); | |
4004f41e | 253 | |
d8bffc13 MB |
254 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); |
255 | // the image has already been copied | |
256 | m_image = image; | |
9e3e0821 VZ |
257 | |
258 | int x, y; | |
259 | int w, h; | |
260 | GetPosition(&x, &y); | |
261 | GetSize(&w, &h); | |
9e3e0821 VZ |
262 | |
263 | #ifdef __WIN32__ | |
0d0512bd | 264 | HANDLE handle = (HANDLE)m_image->GetHandle(); |
d8bffc13 MB |
265 | LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ; |
266 | ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) | | |
267 | ( m_isIcon ? SS_ICON : SS_BITMAP ) ); | |
3dece6c4 | 268 | HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE, |
9e3e0821 | 269 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); |
3dece6c4 JS |
270 | // detect if this is still the handle we passed before or |
271 | // if the static-control made a copy of the bitmap! | |
4fe41ce6 | 272 | if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle) |
3dece6c4 JS |
273 | { |
274 | // the static control made a copy and we are responsible for deleting it | |
cdccdfab | 275 | DeleteObject((HGDIOBJ) oldHandle); |
3dece6c4 | 276 | } |
cdccdfab | 277 | m_currentHandle = (WXHANDLE)handle; |
9e3e0821 VZ |
278 | #endif // Win32 |
279 | ||
280 | if ( ImageIsOk() ) | |
281 | { | |
4004f41e VZ |
282 | int width = image->GetWidth(), |
283 | height = image->GetHeight(); | |
9e3e0821 VZ |
284 | if ( width && height ) |
285 | { | |
7c545786 VZ |
286 | w = width; |
287 | h = height; | |
288 | ||
0d0512bd | 289 | ::MoveWindow(GetHwnd(), x, y, width, height, FALSE); |
9e3e0821 VZ |
290 | } |
291 | } | |
292 | ||
0d0512bd VZ |
293 | RECT rect; |
294 | rect.left = x; | |
295 | rect.top = y; | |
296 | rect.right = x + w; | |
297 | rect.bottom = y + h; | |
57f4f925 | 298 | ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE); |
2bda0e17 KB |
299 | } |
300 | ||
1e6feb95 | 301 | #endif // wxUSE_STATBMP |