* Implemented BestSize cache
[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 <stdio.h>
42
43 // ---------------------------------------------------------------------------
44 // macors
45 // ---------------------------------------------------------------------------
46
47 #if wxUSE_EXTENDED_RTTI
48 WX_DEFINE_FLAGS( wxStaticBitmapStyle )
49
50 wxBEGIN_FLAGS( wxStaticBitmapStyle )
51 // new style border flags, we put them first to
52 // use them for streaming out
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)
59
60 // old style border flags
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)
66 wxFLAGS_MEMBER(wxBORDER)
67
68 // standard window styles
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
72 wxFLAGS_MEMBER(wxWANTS_CHARS)
73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
75 wxFLAGS_MEMBER(wxVSCROLL)
76 wxFLAGS_MEMBER(wxHSCROLL)
77
78 wxEND_FLAGS( wxStaticBitmapStyle )
79
80 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h")
81
82 wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap)
83 wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
84 wxEND_PROPERTIES_TABLE()
85
86 wxBEGIN_HANDLERS_TABLE(wxStaticBitmap)
87 wxEND_HANDLERS_TABLE()
88
89 wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size )
90
91 #else
92 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
93 #endif
94
95 /*
96 TODO PROPERTIES :
97 bitmap
98 */
99
100 // ===========================================================================
101 // implementation
102 // ===========================================================================
103
104 // ---------------------------------------------------------------------------
105 // wxStaticBitmap
106 // ---------------------------------------------------------------------------
107
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
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
140 bool wxStaticBitmap::Create(wxWindow *parent,
141 wxWindowID id,
142 const wxGDIImage& bitmap,
143 const wxPoint& pos,
144 const wxSize& size,
145 long style,
146 const wxString& name)
147 {
148 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
149 return FALSE;
150
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
154 wxGDIImage *image = (wxGDIImage *)NULL;
155 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
156
157 image = ConvertImage( bitmap );
158 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
159
160 // create the native control
161 if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) )
162 {
163 // control creation failed
164 return false;
165 }
166
167 // no need to delete the new image
168 SetImageNoCopy(image);
169
170 // GetBestSize will work properly now, so set the best size if needed
171 SetBestSize(size);
172
173 return TRUE;
174 }
175
176 wxBorder wxStaticBitmap::GetDefaultBorder() const
177 {
178 return wxBORDER_NONE;
179 }
180
181 WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
182 {
183 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
184
185 // what kind of control are we?
186 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
187
188 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
189 // fit to its size -- this is unexpected and doesn't happen in other ports
190 msStyle |= SS_CENTERIMAGE;
191
192 return msStyle;
193 }
194
195 bool wxStaticBitmap::ImageIsOk() const
196 {
197 return m_image && m_image->Ok();
198 }
199
200 void wxStaticBitmap::Free()
201 {
202 delete m_image;
203
204 m_image = NULL;
205 }
206
207 wxSize wxStaticBitmap::DoGetBestSize() const
208 {
209 if ( ImageIsOk() )
210 return wxSize(m_image->GetWidth(), m_image->GetHeight());
211
212 // this is completely arbitrary
213 return wxSize(16, 16);
214 }
215
216 void wxStaticBitmap::SetImage( const wxGDIImage* image )
217 {
218 wxGDIImage* convertedImage = ConvertImage( *image );
219 SetImageNoCopy( convertedImage );
220 InvalidateBestSize();
221 }
222
223 void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
224 {
225 Free();
226
227 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
228 // the image has already been copied
229 m_image = image;
230
231 int x, y;
232 int w, h;
233 GetPosition(&x, &y);
234 GetSize(&w, &h);
235
236 #ifdef __WIN32__
237 HANDLE handle = (HANDLE)m_image->GetHandle();
238 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
239 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
240 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
241 ::SendMessage(GetHwnd(), STM_SETIMAGE,
242 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
243 #endif // Win32
244
245 if ( ImageIsOk() )
246 {
247 int width = image->GetWidth(),
248 height = image->GetHeight();
249 if ( width && height )
250 {
251 w = width;
252 h = height;
253
254 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
255 }
256 }
257
258 RECT rect;
259 rect.left = x;
260 rect.top = y;
261 rect.right = x + w;
262 rect.bottom = y + h;
263 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
264 }
265
266 // We need this or the control can never be moved e.g. in Dialog Editor.
267 WXLRESULT wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
268 WXWPARAM wParam,
269 WXLPARAM lParam)
270 {
271 #ifndef __WXWINCE__
272 // Ensure that static items get messages. Some controls don't like this
273 // message to be intercepted (e.g. RichEdit), hence the tests.
274 if ( nMsg == WM_NCHITTEST )
275 return (long)HTCLIENT;
276 #endif
277
278 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
279 }
280
281 #endif // wxUSE_STATBMP