simplifications and corrections to background drawing:
[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 #include "wx/dcmemory.h"
43
44 #include "wx/msw/private.h"
45
46 // ----------------------------------------------------------------------------
47 // wxWin macros
48 // ----------------------------------------------------------------------------
49
50 #if wxUSE_EXTENDED_RTTI
51 WX_DEFINE_FLAGS( wxStaticBoxStyle )
52
53 wxBEGIN_FLAGS( wxStaticBoxStyle )
54 // new style border flags, we put them first to
55 // use them for streaming out
56 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
57 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
58 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
59 wxFLAGS_MEMBER(wxBORDER_RAISED)
60 wxFLAGS_MEMBER(wxBORDER_STATIC)
61 wxFLAGS_MEMBER(wxBORDER_NONE)
62
63 // old style border flags
64 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
65 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
66 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
67 wxFLAGS_MEMBER(wxRAISED_BORDER)
68 wxFLAGS_MEMBER(wxSTATIC_BORDER)
69 wxFLAGS_MEMBER(wxBORDER)
70
71 // standard window styles
72 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
73 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
74 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
75 wxFLAGS_MEMBER(wxWANTS_CHARS)
76 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
77 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
78 wxFLAGS_MEMBER(wxVSCROLL)
79 wxFLAGS_MEMBER(wxHSCROLL)
80
81 wxEND_FLAGS( wxStaticBoxStyle )
82
83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h")
84
85 wxBEGIN_PROPERTIES_TABLE(wxStaticBox)
86 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
87 wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
88 /*
89 TODO PROPERTIES :
90 label
91 */
92 wxEND_PROPERTIES_TABLE()
93
94 wxBEGIN_HANDLERS_TABLE(wxStaticBox)
95 wxEND_HANDLERS_TABLE()
96
97 wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
98 #else
99 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
100 #endif
101
102 // ============================================================================
103 // implementation
104 // ============================================================================
105
106 // ----------------------------------------------------------------------------
107 // wxStaticBox
108 // ----------------------------------------------------------------------------
109
110 bool wxStaticBox::Create(wxWindow *parent,
111 wxWindowID id,
112 const wxString& label,
113 const wxPoint& pos,
114 const wxSize& size,
115 long style,
116 const wxString& name)
117 {
118 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
119 return false;
120
121 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
122 return false;
123
124 #ifndef __WXWINCE__
125 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
126 #endif // !__WXWINCE__
127
128 return true;
129 }
130
131 wxBorder wxStaticBox::GetDefaultBorder() const
132 {
133 return wxBORDER_NONE;
134 }
135
136 WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
137 {
138 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
139
140 // no need for it anymore, must be removed for wxRadioBox child
141 // buttons to be able to repaint themselves
142 styleWin &= ~WS_CLIPCHILDREN;
143
144 if ( exstyle )
145 *exstyle = 0;
146
147 return styleWin | BS_GROUPBOX;
148 }
149
150 wxSize wxStaticBox::DoGetBestSize() const
151 {
152 int cx, cy;
153 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
154
155 int wBox;
156 GetTextExtent(wxGetWindowText(m_hWnd), &wBox, &cy);
157
158 wBox += 3*cx;
159 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
160
161 return wxSize(wBox, hBox);
162 }
163
164 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
165 {
166 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
167
168 // need extra space, don't know how much but this seems to be enough
169 *borderTop += GetCharHeight()/3;
170 }
171
172 // all the hacks below are not necessary for WinCE
173 #ifndef __WXWINCE__
174
175 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
176 {
177 if ( nMsg == WM_NCHITTEST )
178 {
179 // This code breaks some other processing such as enter/leave tracking
180 // so it's off by default.
181
182 static int s_useHTClient = -1;
183 if (s_useHTClient == -1)
184 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
185 if (s_useHTClient == 1)
186 {
187 int xPos = GET_X_LPARAM(lParam);
188 int yPos = GET_Y_LPARAM(lParam);
189
190 ScreenToClient(&xPos, &yPos);
191
192 // Make sure you can drag by the top of the groupbox, but let
193 // other (enclosed) controls get mouse events also
194 if ( yPos < 10 )
195 return (long)HTCLIENT;
196 }
197 }
198
199 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
200 }
201
202 // ----------------------------------------------------------------------------
203 // static box drawing
204 // ----------------------------------------------------------------------------
205
206 /*
207 We draw the static box ourselves because it's the only way to prevent it
208 from flickering horribly on resize (because everything inside the box is
209 erased twice: once when the box itself is repainted and second time when
210 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
211 we used to do and which resulted in other problems.
212 */
213
214 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
215 static inline void
216 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
217 {
218 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
219 if ( !hrgnRect )
220 {
221 wxLogLastError(_T("CreateRectRgn()"));
222 return;
223 }
224
225 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
226 }
227
228 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
229 {
230 HRGN hrgn = (HRGN)hRgn;
231
232 // remove the area occupied by the static box borders from the region
233 int borderTop, border;
234 GetBordersForSizer(&borderTop, &border);
235
236 // top
237 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
238
239 // bottom
240 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
241
242 // left
243 SubtractRectFromRgn(hrgn, 0, 0, border, h);
244
245 // right
246 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
247 }
248
249 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
250 {
251 RECT rc;
252 ::GetWindowRect(GetHwnd(), &rc);
253 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
254
255 // iterate over all child windows (not just wxWindows but all windows)
256 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
257 child;
258 child = ::GetWindow(child, GW_HWNDNEXT) )
259 {
260 wxWindow *childWindow = wxGetWindowFromHWND((WXHWND) child);
261
262 // can't just test for (this != child) here since if a wxStaticBox
263 // overlaps another wxStaticBox then neither are drawn. The overlapping
264 // region will flicker but we shouldn't have overlapping windows anyway.
265 if ( !childWindow || !wxDynamicCast(childWindow, wxStaticBox) )
266 {
267 ::GetWindowRect(child, &rc);
268 if ( ::RectInRegion(hrgn, &rc) )
269 {
270 // need to remove WS_CLIPSIBLINGS from all sibling windows
271 // that are within this staticbox if set
272 LONG style = ::GetWindowLong(child, GWL_STYLE);
273 if ( style & WS_CLIPSIBLINGS )
274 {
275 style &= ~WS_CLIPSIBLINGS;
276 ::SetWindowLong(child, GWL_STYLE, style);
277
278 // MSDN: "If you have changed certain window data using
279 // SetWindowLong, you must call SetWindowPos to have the
280 // changes take effect."
281 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
282 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
283 SWP_FRAMECHANGED);
284 }
285
286 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
287 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
288 }
289 }
290 }
291
292 return (WXHRGN)hrgn;
293 }
294
295 // helper for OnPaint(): really erase the background, i.e. do it even if we
296 // don't have any non default brush for doing it (DoEraseBackground() doesn't
297 // do anything in such case)
298 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
299 {
300 // note that we do not use the box background colour here, it shouldn't
301 // apply to its interior for several reasons:
302 // 1. wxGTK doesn't do it
303 // 2. controls inside the box don't get correct bg colour because they
304 // are not our children so we'd have some really ugly colour mix if
305 // we did it
306 // 3. this is backwards compatible behaviour and some people rely on it,
307 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
308 wxWindow *parent = GetParent();
309 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), this);
310
311 // if there is no special brush for painting this control, just use the
312 // solid background colour
313 wxBrush brush;
314 if ( !hbr )
315 {
316 brush = wxBrush(parent->GetBackgroundColour());
317 hbr = GetHbrushOf(brush);
318 }
319
320 ::FillRect(GetHdcOf(dc), &rc, hbr);
321 }
322
323 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
324 {
325 RECT rc;
326 ::GetClientRect(GetHwnd(), &rc);
327
328 // draw the entire box in a memory DC
329 wxMemoryDC memdc;
330 wxBitmap bitmap(rc.right, rc.bottom);
331 memdc.SelectObject(bitmap);
332
333 PaintBackground(memdc, rc);
334
335 // NB: neither setting the text colour nor transparent background mode
336 // doesn't change anything: the static box def window proc still
337 // draws the label in its own colours, so if we want to have control
338 // over this we really have to draw everything ourselves
339 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(memdc), 0);
340
341
342 // now only blit the static box border itself, not the interior, to avoid
343 // flicker when background is drawn below
344 //
345 // note that it seems to be faster to do 4 small blits here and then paint
346 // directly into wxPaintDC than painting background in wxMemoryDC and then
347 // blitting everything at once to wxPaintDC, this is why we do it like this
348 wxPaintDC dc(this);
349 int borderTop, border;
350 GetBordersForSizer(&borderTop, &border);
351
352 // top
353 dc.Blit(border, 0, rc.right - border, borderTop,
354 &memdc, border, 0);
355 // bottom
356 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
357 &memdc, border, rc.bottom - border);
358 // left
359 dc.Blit(0, 0, border, rc.bottom,
360 &memdc, 0, 0);
361 // right
362 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
363 &memdc, rc.right - border, 0);
364
365
366 // create the region excluding box children
367 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
368 RECT rcWin;
369 ::GetWindowRect(GetHwnd(), &rcWin);
370 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
371
372 // and also the box itself
373 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
374 HDCClipper clipToBg(GetHdcOf(dc), hrgn);
375
376 // paint the inside of the box (excluding box itself and child controls)
377 PaintBackground(dc, rc);
378 }
379
380 #endif // !__WXWINCE__
381
382 #endif // wxUSE_STATBOX
383