Applied patch [ 1166587 ] [wxMSW] Removes all flicker from wxStaticBox
[wxWidgets.git] / src / msw / statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/statbox.cpp
3 // Purpose: wxStaticBox
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 "statbox.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_STATBOX
32
33 #ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/dcclient.h"
36 #endif
37
38 #include "wx/statbox.h"
39 #include "wx/notebook.h"
40 #include "wx/sysopt.h"
41 #include "wx/image.h"
42
43 #include "wx/msw/private.h"
44
45 // ----------------------------------------------------------------------------
46 // wxWin macros
47 // ----------------------------------------------------------------------------
48
49 #if wxUSE_EXTENDED_RTTI
50 WX_DEFINE_FLAGS( wxStaticBoxStyle )
51
52 wxBEGIN_FLAGS( wxStaticBoxStyle )
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( wxStaticBoxStyle )
81
82 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h")
83
84 wxBEGIN_PROPERTIES_TABLE(wxStaticBox)
85 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
86 wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
87 /*
88 TODO PROPERTIES :
89 label
90 */
91 wxEND_PROPERTIES_TABLE()
92
93 wxBEGIN_HANDLERS_TABLE(wxStaticBox)
94 wxEND_HANDLERS_TABLE()
95
96 wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
97 #else
98 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
99 #endif
100
101 BEGIN_EVENT_TABLE(wxStaticBox, wxControl)
102 EVT_PAINT(wxStaticBox::OnPaint)
103 END_EVENT_TABLE()
104
105 // ============================================================================
106 // implementation
107 // ============================================================================
108
109 // ----------------------------------------------------------------------------
110 // wxStaticBox
111 // ----------------------------------------------------------------------------
112
113 bool wxStaticBox::Create(wxWindow *parent,
114 wxWindowID id,
115 const wxString& label,
116 const wxPoint& pos,
117 const wxSize& size,
118 long style,
119 const wxString& name)
120 {
121 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
122 return false;
123
124 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
125 return false;
126
127 return true;
128 }
129
130 WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
131 {
132 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
133
134 // no need for it anymore, must be removed for wxRadioBox child
135 // buttons to be able to repaint themselves
136 styleWin &= ~WS_CLIPCHILDREN;
137
138 if ( exstyle )
139 *exstyle = 0;
140
141 return styleWin | BS_GROUPBOX;
142 }
143
144 wxSize wxStaticBox::DoGetBestSize() const
145 {
146 int cx, cy;
147 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
148
149 int wBox;
150 GetTextExtent(wxGetWindowText(m_hWnd), &wBox, &cy);
151
152 wBox += 3*cx;
153 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
154
155 return wxSize(wBox, hBox);
156 }
157
158 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
159 {
160 #ifndef __WXWINCE__
161 if ( nMsg == WM_NCHITTEST )
162 {
163 // This code breaks some other processing such as enter/leave tracking
164 // so it's off by default.
165
166 static int s_useHTClient = -1;
167 if (s_useHTClient == -1)
168 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
169 if (s_useHTClient == 1)
170 {
171 int xPos = LOWORD(lParam); // horizontal position of cursor
172 int yPos = HIWORD(lParam); // vertical position of cursor
173
174 ScreenToClient(&xPos, &yPos);
175
176 // Make sure you can drag by the top of the groupbox, but let
177 // other (enclosed) controls get mouse events also
178 if ( yPos < 10 )
179 return (long)HTCLIENT;
180 }
181 }
182 #endif // !__WXWINCE__
183
184 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
185 }
186
187 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
188 {
189 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
190
191 // if not using correct (but backwards cojmpatible) text metrics
192 // calculations, we need to add some extra margin or otherwise static box
193 // title is clipped
194 #if !wxDIALOG_UNIT_COMPATIBILITY
195 if ( !GetLabel().empty() )
196 *borderTop += GetCharHeight()/3;
197 #endif // !wxDIALOG_UNIT_COMPATIBILITY
198 }
199
200 // rc must be in client coords!
201 void wxStaticBox::MSWClipBoxRegion(HRGN hrgn, const RECT *rc)
202 {
203 HRGN hrgnchild;
204
205 // top
206 hrgnchild = ::CreateRectRgn(0, 0, rc->right, 14);
207 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
208 ::DeleteObject(hrgnchild);
209
210 // bottom
211 hrgnchild = ::CreateRectRgn(0, rc->bottom - 7, rc->right, rc->bottom);
212 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
213 ::DeleteObject(hrgnchild);
214
215 // left
216 hrgnchild = ::CreateRectRgn(0, 0, 7, rc->bottom);
217 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
218 ::DeleteObject(hrgnchild);
219
220 // right
221 hrgnchild = ::CreateRectRgn(rc->right - 7, 0, rc->right, rc->bottom);
222 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
223 ::DeleteObject(hrgnchild);
224 }
225
226 WXHRGN wxStaticBox::MSWCalculateClippingRegion()
227 {
228 RECT rc;
229 ::GetWindowRect(GetHwnd(), &rc);
230 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
231
232 wxWindowList::compatibility_iterator node = GetParent()->GetChildren().GetFirst();
233 while ( node )
234 {
235 wxWindow *child = node->GetData();
236
237 // can't just test for (this != child) here since if a wxStaticBox
238 // overlaps another wxStaticBox then neither are drawn. The overlapping
239 // region will flicker but we shouldn't have overlapping windows anyway.
240 if ( !child->IsKindOf(CLASSINFO(wxStaticBox)) )
241 {
242 ::GetWindowRect(GetHwndOf(child), &rc);
243 if ( RectInRegion(hrgn, &rc) )
244 {
245 // need to remove WS_CLIPSIBLINGS from all sibling windows
246 // that are within this staticbox if set
247 LONG style = ::GetWindowLong(GetHwndOf(child), GWL_STYLE);
248 if ( style & WS_CLIPSIBLINGS )
249 {
250 style &= ~WS_CLIPSIBLINGS;
251 ::SetWindowLong(GetHwndOf(child), GWL_STYLE, style);
252
253 // MSDN: "If you have changed certain window data using
254 // SetWindowLong, you must call SetWindowPos to have the
255 // changes take effect."
256 ::SetWindowPos(GetHwndOf(child), NULL, 0, 0, 0, 0,
257 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
258 SWP_FRAMECHANGED);
259 }
260
261 HRGN hrgnchild = ::CreateRectRgnIndirect(&rc);
262 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
263 ::DeleteObject(hrgnchild);
264 }
265 }
266
267 node = node->GetNext();
268 }
269 ::GetWindowRect(GetHwnd(), &rc);
270 ::OffsetRgn(hrgn, -rc.left, -rc.top);
271
272 return hrgn;
273 }
274
275 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
276 {
277 wxPaintDC dc(this);
278 RECT rc;
279 ::GetClientRect(GetHwnd(), &rc);
280
281 // paint the actual box
282 wxMemoryDC memdc;
283 wxBitmap bitmap(rc.right, rc.bottom);
284 memdc.SelectObject(bitmap);
285
286 // get bg brush
287 WXHBRUSH hbr = DoMSWControlColor(GetHdcOf(memdc), wxNullColour);
288 if ( !hbr )
289 {
290 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
291 hbr = (WXHBRUSH)brush->GetResourceHandle();
292 }
293
294 // draw solid box, but only blit the good bits
295 ::FillRect(GetHdcOf(memdc), &rc, (HBRUSH)hbr);
296 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(memdc), 0);
297
298 // top
299 dc.Blit(7, 0, rc.right - 7, 14, &memdc, 7, 0);
300 // bottom
301 dc.Blit(7, rc.bottom - 7, rc.right - 7, rc.bottom, &memdc, 7, rc.bottom - 7);
302 // left
303 dc.Blit(0, 0, 7, rc.bottom, &memdc, 0, 0);
304 // right
305 dc.Blit(rc.right - 7, 0, rc.right, rc.bottom, &memdc, rc.right - 7, 0);
306
307 // paint the inner
308 HRGN hrgn = (HRGN)MSWCalculateClippingRegion();
309 // now remove the box itself
310 MSWClipBoxRegion(hrgn, &rc);
311
312 hbr = DoMSWControlColor(GetHdcOf(dc), wxNullColour);
313 if ( !hbr )
314 {
315 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
316 hbr = (WXHBRUSH)brush->GetResourceHandle();
317 }
318
319 ::SelectClipRgn(GetHdcOf(dc), hrgn);
320 ::FillRect(GetHdcOf(dc), &rc, (HBRUSH)hbr);
321 ::SelectClipRgn(GetHdcOf(dc), NULL);
322 ::DeleteObject(hrgn);
323 }
324
325 #endif // wxUSE_STATBOX
326