Patch #1203934: enable best size caching for MSW widgets
[wxWidgets.git] / src / msw / statbmp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "statbmp.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_STATBMP
32
33 #include "wx/window.h"
34 #include "wx/msw/private.h"
35
36 #ifndef WX_PRECOMP
37 #include "wx/icon.h"
38 #include "wx/statbmp.h"
39 #endif
40
41 #include "wx/sysopt.h"
42
43 #include <stdio.h>
44
45 // ---------------------------------------------------------------------------
46 // macors
47 // ---------------------------------------------------------------------------
48
49 #if wxUSE_EXTENDED_RTTI
50 WX_DEFINE_FLAGS( wxStaticBitmapStyle )
51
52 wxBEGIN_FLAGS( wxStaticBitmapStyle )
53 // new style border flags, we put them first to
54 // use them for streaming out
55 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
56 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
57 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
58 wxFLAGS_MEMBER(wxBORDER_RAISED)
59 wxFLAGS_MEMBER(wxBORDER_STATIC)
60 wxFLAGS_MEMBER(wxBORDER_NONE)
61
62 // old style border flags
63 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
64 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
65 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
66 wxFLAGS_MEMBER(wxRAISED_BORDER)
67 wxFLAGS_MEMBER(wxSTATIC_BORDER)
68 wxFLAGS_MEMBER(wxBORDER)
69
70 // standard window styles
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
74 wxFLAGS_MEMBER(wxWANTS_CHARS)
75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
77 wxFLAGS_MEMBER(wxVSCROLL)
78 wxFLAGS_MEMBER(wxHSCROLL)
79
80 wxEND_FLAGS( wxStaticBitmapStyle )
81
82 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h")
83
84 wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap)
85 wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
86 wxEND_PROPERTIES_TABLE()
87
88 wxBEGIN_HANDLERS_TABLE(wxStaticBitmap)
89 wxEND_HANDLERS_TABLE()
90
91 wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size )
92
93 #else
94 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
95 #endif
96
97 /*
98 TODO PROPERTIES :
99 bitmap
100 */
101
102 // ===========================================================================
103 // implementation
104 // ===========================================================================
105
106 // ---------------------------------------------------------------------------
107 // wxStaticBitmap
108 // ---------------------------------------------------------------------------
109
110 // we may have either bitmap or icon: if a bitmap with mask is passed, we
111 // will transform it to an icon ourselves because otherwise the mask will
112 // be ignored by Windows
113 // note that this function will create a new object every time
114 // it is called even if the image needs no conversion
115
116 static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
117 {
118 bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) );
119
120 if( !isIcon )
121 {
122 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
123 _T("not an icon and not a bitmap?") );
124
125 const wxBitmap& bmp = (const wxBitmap&)bitmap;
126 wxMask *mask = bmp.GetMask();
127 if ( mask && mask->GetMaskBitmap() )
128 {
129 wxIcon* icon = new wxIcon;
130 icon->CopyFromBitmap(bmp);
131
132 return icon;
133 }
134
135 return new wxBitmap( bmp );
136 }
137
138 // copying a bitmap is a cheap operation
139 return new wxIcon( (const wxIcon&)bitmap );
140 }
141
142 bool wxStaticBitmap::Create(wxWindow *parent,
143 wxWindowID id,
144 const wxGDIImage& bitmap,
145 const wxPoint& pos,
146 const wxSize& size,
147 long style,
148 const wxString& name)
149 {
150 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
151 return false;
152
153 // we may have either bitmap or icon: if a bitmap with mask is passed, we
154 // will transform it to an icon ourselves because otherwise the mask will
155 // be ignored by Windows
156 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
157
158 wxGDIImage *image = ConvertImage( bitmap );
159 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
160
161 // create the native control
162 if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) )
163 {
164 // control creation failed
165 return false;
166 }
167
168 // no need to delete the new image
169 SetImageNoCopy(image);
170
171 // GetBestSize will work properly now, so set the best size if needed
172 SetBestSize(size);
173
174 return true;
175 }
176
177 wxBorder wxStaticBitmap::GetDefaultBorder() const
178 {
179 return wxBORDER_NONE;
180 }
181
182 WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
183 {
184 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
185
186 // what kind of control are we?
187 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
188
189 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
190 // fit to its size -- this is unexpected and doesn't happen in other ports
191 msStyle |= SS_CENTERIMAGE;
192
193 return msStyle;
194 }
195
196 bool wxStaticBitmap::ImageIsOk() const
197 {
198 return m_image && m_image->Ok();
199 }
200
201 void wxStaticBitmap::Free()
202 {
203 delete m_image;
204
205 m_image = NULL;
206 }
207
208 wxSize wxStaticBitmap::DoGetBestSize() const
209 {
210 if ( ImageIsOk() )
211 {
212 wxSize best(m_image->GetWidth(), m_image->GetHeight());
213 CacheBestSize(best);
214 return best;
215 }
216
217 // this is completely arbitrary
218 return wxSize(16, 16);
219 }
220
221 void wxStaticBitmap::SetImage( const wxGDIImage* image )
222 {
223 wxGDIImage* convertedImage = ConvertImage( *image );
224 SetImageNoCopy( convertedImage );
225 InvalidateBestSize();
226 }
227
228 void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
229 {
230 Free();
231
232 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
233 // the image has already been copied
234 m_image = image;
235
236 int x, y;
237 int w, h;
238 GetPosition(&x, &y);
239 GetSize(&w, &h);
240
241 #ifdef __WIN32__
242 HANDLE handle = (HANDLE)m_image->GetHandle();
243 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
244 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
245 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
246 ::SendMessage(GetHwnd(), STM_SETIMAGE,
247 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
248 #endif // Win32
249
250 if ( ImageIsOk() )
251 {
252 int width = image->GetWidth(),
253 height = image->GetHeight();
254 if ( width && height )
255 {
256 w = width;
257 h = height;
258
259 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
260 }
261 }
262
263 RECT rect;
264 rect.left = x;
265 rect.top = y;
266 rect.right = x + w;
267 rect.bottom = y + h;
268 ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
269 }
270
271 WXLRESULT wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
272 WXWPARAM wParam,
273 WXLPARAM lParam)
274 {
275 #ifndef __WXWINCE__
276 static int s_useHTClient = -1;
277 if (s_useHTClient == -1)
278 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbitmap.htclient"));
279 if (s_useHTClient == 1)
280 {
281 // Ensure that static items get messages. Some controls don't like this
282 // message to be intercepted (e.g. RichEdit), hence the tests.
283 // Also, this code breaks some other processing such as enter/leave tracking
284 // so it's off by default.
285
286 if ( nMsg == WM_NCHITTEST )
287 return (long)HTCLIENT;
288 }
289 #endif
290
291 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
292 }
293
294 #endif // wxUSE_STATBMP
295