]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/statbox.cpp
3 // Purpose: wxStaticBox
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "statbox.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dcclient.h"
38 #include "wx/statbox.h"
39 #include "wx/notebook.h"
40 #include "wx/sysopt.h"
42 #include "wx/dcmemory.h"
44 #include "wx/msw/private.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #if wxUSE_EXTENDED_RTTI
51 WX_DEFINE_FLAGS( wxStaticBoxStyle
)
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
)
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
)
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
)
81 wxEND_FLAGS( wxStaticBoxStyle
)
83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h")
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
92 wxEND_PROPERTIES_TABLE()
94 wxBEGIN_HANDLERS_TABLE(wxStaticBox
)
95 wxEND_HANDLERS_TABLE()
97 wxCONSTRUCTOR_6( wxStaticBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
99 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
)
102 // ============================================================================
104 // ============================================================================
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 bool wxStaticBox::Create(wxWindow
*parent
,
112 const wxString
& label
,
116 const wxString
& name
)
118 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
121 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
125 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
126 #endif // !__WXWINCE__
131 wxBorder
wxStaticBox::GetDefaultBorder() const
133 return wxBORDER_NONE
;
136 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
138 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
140 // no need for it anymore, must be removed for wxRadioBox child
141 // buttons to be able to repaint themselves
142 styleWin
&= ~WS_CLIPCHILDREN
;
148 // If any of the ancestors are scrolling windows, style has to be
149 // WS_EX_TRANSPARENT or the static box won't be painted when the window
150 // is scrolled. We try not to do this normally, because we get a lot of
152 for ( wxWindow
*win
= GetParent(); win
; win
= win
->GetParent() )
154 if ( win
->HasFlag(wxVSCROLL
) || win
->HasFlag(wxHSCROLL
) )
156 *exstyle
= WS_EX_TRANSPARENT
;
160 if ( win
->IsTopLevel() )
165 return styleWin
| BS_GROUPBOX
;
168 wxSize
wxStaticBox::DoGetBestSize() const
171 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
174 GetTextExtent(wxGetWindowText(m_hWnd
), &wBox
, &cy
);
177 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
179 return wxSize(wBox
, hBox
);
182 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
184 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
186 // need extra space, don't know how much but this seems to be enough
187 *borderTop
+= GetCharHeight()/3;
190 // all the hacks below are not necessary for WinCE
193 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
195 if ( nMsg
== WM_NCHITTEST
)
197 // This code breaks some other processing such as enter/leave tracking
198 // so it's off by default.
200 static int s_useHTClient
= -1;
201 if (s_useHTClient
== -1)
202 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
203 if (s_useHTClient
== 1)
205 int xPos
= GET_X_LPARAM(lParam
);
206 int yPos
= GET_Y_LPARAM(lParam
);
208 ScreenToClient(&xPos
, &yPos
);
210 // Make sure you can drag by the top of the groupbox, but let
211 // other (enclosed) controls get mouse events also
213 return (long)HTCLIENT
;
217 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
220 // ----------------------------------------------------------------------------
221 // static box drawing
222 // ----------------------------------------------------------------------------
225 We draw the static box ourselves because it's the only way to prevent it
226 from flickering horribly on resize (because everything inside the box is
227 erased twice: once when the box itself is repainted and second time when
228 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
229 we used to do and which resulted in other problems.
232 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
234 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
236 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
239 wxLogLastError(_T("CreateRectRgn()"));
243 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
246 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
248 HRGN hrgn
= (HRGN
)hRgn
;
250 // remove the area occupied by the static box borders from the region
251 int borderTop
, border
;
252 GetBordersForSizer(&borderTop
, &border
);
255 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
258 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
261 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
264 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
267 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
270 ::GetWindowRect(GetHwnd(), &rc
);
271 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
273 // iterate over all child windows (not just wxWindows but all windows)
274 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
276 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
278 wxWindow
*childWindow
= wxGetWindowFromHWND((WXHWND
) child
);
280 // can't just test for (this != child) here since if a wxStaticBox
281 // overlaps another wxStaticBox then neither are drawn. The overlapping
282 // region will flicker but we shouldn't have overlapping windows anyway.
283 if ( !childWindow
|| !wxDynamicCast(childWindow
, wxStaticBox
) )
285 ::GetWindowRect(child
, &rc
);
286 if ( ::RectInRegion(hrgn
, &rc
) )
288 // need to remove WS_CLIPSIBLINGS from all sibling windows
289 // that are within this staticbox if set
290 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
291 if ( style
& WS_CLIPSIBLINGS
)
293 style
&= ~WS_CLIPSIBLINGS
;
294 ::SetWindowLong(child
, GWL_STYLE
, style
);
296 // MSDN: "If you have changed certain window data using
297 // SetWindowLong, you must call SetWindowPos to have the
298 // changes take effect."
299 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
300 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
304 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
305 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
313 // helper for OnPaint(): really erase the background, i.e. do it even if we
314 // don't have any non default brush for doing it (DoEraseBackground() doesn't
315 // do anything in such case)
316 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
318 // note that we do not use the box background colour here, it shouldn't
319 // apply to its interior for several reasons:
320 // 1. wxGTK doesn't do it
321 // 2. controls inside the box don't get correct bg colour because they
322 // are not our children so we'd have some really ugly colour mix if
324 // 3. this is backwards compatible behaviour and some people rely on it,
325 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
326 wxWindow
*parent
= GetParent();
327 HBRUSH hbr
= (HBRUSH
)parent
->MSWGetBgBrush(dc
.GetHDC(), GetHWND());
329 // if there is no special brush for painting this control, just use the
330 // solid background colour
334 brush
= wxBrush(parent
->GetBackgroundColour());
335 hbr
= GetHbrushOf(brush
);
338 ::FillRect(GetHdcOf(dc
), &rc
, hbr
);
341 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
344 ::GetClientRect(GetHwnd(), &rc
);
346 // draw the entire box in a memory DC
348 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
349 memdc
.SelectObject(bitmap
);
351 PaintBackground(memdc
, rc
);
353 // NB: neither setting the text colour nor transparent background mode
354 // doesn't change anything: the static box def window proc still
355 // draws the label in its own colours, so if we want to have control
356 // over this we really have to draw everything ourselves
357 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(memdc
), 0);
360 // now only blit the static box border itself, not the interior, to avoid
361 // flicker when background is drawn below
363 // note that it seems to be faster to do 4 small blits here and then paint
364 // directly into wxPaintDC than painting background in wxMemoryDC and then
365 // blitting everything at once to wxPaintDC, this is why we do it like this
367 int borderTop
, border
;
368 GetBordersForSizer(&borderTop
, &border
);
371 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
374 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, rc
.bottom
,
375 &memdc
, border
, rc
.bottom
- border
);
377 dc
.Blit(0, 0, border
, rc
.bottom
,
380 dc
.Blit(rc
.right
- border
, 0, rc
.right
, rc
.bottom
,
381 &memdc
, rc
.right
- border
, 0);
384 // create the region excluding box children
385 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
387 ::GetWindowRect(GetHwnd(), &rcWin
);
388 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
390 // and also the box itself
391 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
392 HDCClipper
clipToBg(GetHdcOf(dc
), hrgn
);
394 // paint the inside of the box (excluding box itself and child controls)
395 PaintBackground(dc
, rc
);
398 #endif // !__WXWINCE__
400 #endif // wxUSE_STATBOX