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