]>
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"
43 #include "wx/sysopt.h"
45 #include "wx/msw/uxtheme.h"
46 #include "wx/msw/private.h"
47 #include "wx/msw/missing.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxStaticBoxStyle
)
56 wxBEGIN_FLAGS( wxStaticBoxStyle
)
57 // new style border flags, we put them first to
58 // use them for streaming out
59 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
60 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
61 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
62 wxFLAGS_MEMBER(wxBORDER_RAISED
)
63 wxFLAGS_MEMBER(wxBORDER_STATIC
)
64 wxFLAGS_MEMBER(wxBORDER_NONE
)
66 // old style border flags
67 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
68 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
69 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
70 wxFLAGS_MEMBER(wxRAISED_BORDER
)
71 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
72 wxFLAGS_MEMBER(wxBORDER
)
74 // standard window styles
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
77 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
78 wxFLAGS_MEMBER(wxWANTS_CHARS
)
79 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
80 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
81 wxFLAGS_MEMBER(wxVSCROLL
)
82 wxFLAGS_MEMBER(wxHSCROLL
)
84 wxEND_FLAGS( wxStaticBoxStyle
)
86 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h")
88 wxBEGIN_PROPERTIES_TABLE(wxStaticBox
)
89 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
90 wxPROPERTY_FLAGS( WindowStyle
, wxStaticBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
95 wxEND_PROPERTIES_TABLE()
97 wxBEGIN_HANDLERS_TABLE(wxStaticBox
)
98 wxEND_HANDLERS_TABLE()
100 wxCONSTRUCTOR_6( wxStaticBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
102 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
)
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 bool wxStaticBox::Create(wxWindow
*parent
,
115 const wxString
& label
,
119 const wxString
& name
)
121 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
124 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
128 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
129 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
130 #endif // !__WXWINCE__
135 wxBorder
wxStaticBox::GetDefaultBorder() const
137 return wxBORDER_NONE
;
140 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
142 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
144 // no need for it anymore, must be removed for wxRadioBox child
145 // buttons to be able to repaint themselves
146 styleWin
&= ~WS_CLIPCHILDREN
;
151 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
152 *exstyle
= WS_EX_TRANSPARENT
;
158 return styleWin
| BS_GROUPBOX
;
161 wxSize
wxStaticBox::DoGetBestSize() const
164 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
167 GetTextExtent(wxGetWindowText(m_hWnd
), &wBox
, &cy
);
170 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
172 wxSize
best(wBox
, hBox
);
177 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
179 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
181 // need extra space, don't know how much but this seems to be enough
182 *borderTop
+= GetCharHeight()/3;
185 // all the hacks below are not necessary for WinCE
188 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
190 if ( nMsg
== WM_NCHITTEST
)
192 // This code breaks some other processing such as enter/leave tracking
193 // so it's off by default.
195 static int s_useHTClient
= -1;
196 if (s_useHTClient
== -1)
197 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
198 if (s_useHTClient
== 1)
200 int xPos
= GET_X_LPARAM(lParam
);
201 int yPos
= GET_Y_LPARAM(lParam
);
203 ScreenToClient(&xPos
, &yPos
);
205 // Make sure you can drag by the top of the groupbox, but let
206 // other (enclosed) controls get mouse events also
208 return (long)HTCLIENT
;
212 if ( nMsg
== WM_PRINTCLIENT
)
214 // we have to process WM_PRINTCLIENT ourselves as otherwise child
215 // windows' background (eg buttons in radio box) would never be drawn
216 // unless we have a parent with non default background
218 // so check first if we have one
219 if ( !HandlePrintClient((WXHDC
)wParam
) )
221 // no, we don't, erase the background ourselves
222 // (don't use our own) - see PaintBackground for explanation
223 wxBrush
brush(GetParent()->GetBackgroundColour());
224 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
230 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
233 // ----------------------------------------------------------------------------
234 // static box drawing
235 // ----------------------------------------------------------------------------
238 We draw the static box ourselves because it's the only way to prevent it
239 from flickering horribly on resize (because everything inside the box is
240 erased twice: once when the box itself is repainted and second time when
241 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
242 we used to do and which resulted in other problems.
245 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
247 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
249 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
252 wxLogLastError(_T("CreateRectRgn()"));
256 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
259 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
261 HRGN hrgn
= (HRGN
)hRgn
;
263 // remove the area occupied by the static box borders from the region
264 int borderTop
, border
;
265 GetBordersForSizer(&borderTop
, &border
);
268 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
271 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
274 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
277 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
280 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
283 ::GetWindowRect(GetHwnd(), &rc
);
284 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
285 bool foundThis
= false;
287 // iterate over all child windows (not just wxWindows but all windows)
288 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
290 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
292 if ( ! ::IsWindowVisible(child
) )
294 // if the window isn't visible then it doesn't need clipped
298 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
299 wxString
str(wxGetWindowClass(child
));
301 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
303 if ( child
== GetHwnd() )
306 // Any static boxes below this one in the Z-order can't be clipped
307 // since if we have the case where a static box with a low Z-order
308 // is nested inside another static box with a high Z-order then the
309 // nested static box would be painted over. Doing it this way
310 // unfortunately results in flicker if the Z-order of nested static
311 // boxes is not inside (lowest) to outside (highest) but at least
312 // they are still shown.
317 ::GetWindowRect(child
, &rc
);
318 if ( ::RectInRegion(hrgn
, &rc
) )
320 // need to remove WS_CLIPSIBLINGS from all sibling windows
321 // that are within this staticbox if set
322 if ( style
& WS_CLIPSIBLINGS
)
324 style
&= ~WS_CLIPSIBLINGS
;
325 ::SetWindowLong(child
, GWL_STYLE
, style
);
327 // MSDN: "If you have changed certain window data using
328 // SetWindowLong, you must call SetWindowPos to have the
329 // changes take effect."
330 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
331 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
335 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
336 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
343 // helper for OnPaint(): really erase the background, i.e. do it even if we
344 // don't have any non default brush for doing it (DoEraseBackground() doesn't
345 // do anything in such case)
346 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
348 // note that we do not use the box background colour here, it shouldn't
349 // apply to its interior for several reasons:
350 // 1. wxGTK doesn't do it
351 // 2. controls inside the box don't get correct bg colour because they
352 // are not our children so we'd have some really ugly colour mix if
354 // 3. this is backwards compatible behaviour and some people rely on it,
355 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
356 wxWindow
*parent
= GetParent();
357 HBRUSH hbr
= (HBRUSH
)parent
->MSWGetBgBrush(dc
.GetHDC(), GetHWND());
359 // if there is no special brush for painting this control, just use the
360 // solid background colour
364 brush
= wxBrush(parent
->GetBackgroundColour());
365 hbr
= GetHbrushOf(brush
);
368 ::FillRect(GetHdcOf(dc
), &rc
, hbr
);
371 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& WXUNUSED(rc
))
373 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(dc
), 0);
375 // when using XP themes, neither setting the text colour nor transparent
376 // background mode doesn't change anything: the static box def window proc
377 // still draws the label in its own colours, so we need to redraw the text
378 // ourselves if we have a non default fg colour
379 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
381 // draw over the text in default colour in our colour
382 dc
.SetFont(GetFont());
384 HDC hdc
= GetHdcOf(dc
);
385 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
387 // FIXME: value of x is hardcoded as this is what it is on my system,
388 // no idea if it's true everywhere
389 const int y
= dc
.GetCharHeight();
393 RECT rc
= { x
, 0, GetSize().x
- x
, y
};
395 const wxString label
= GetLabel();
396 ::DrawText(hdc
, label
, label
.length(), &rc
, DT_SINGLELINE
| DT_VCENTER
);
400 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
403 ::GetClientRect(GetHwnd(), &rc
);
405 // draw the entire box in a memory DC
407 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
408 memdc
.SelectObject(bitmap
);
410 PaintBackground(memdc
, rc
);
411 PaintForeground(memdc
, rc
);
413 // now only blit the static box border itself, not the interior, to avoid
414 // flicker when background is drawn below
416 // note that it seems to be faster to do 4 small blits here and then paint
417 // directly into wxPaintDC than painting background in wxMemoryDC and then
418 // blitting everything at once to wxPaintDC, this is why we do it like this
420 int borderTop
, border
;
421 GetBordersForSizer(&borderTop
, &border
);
424 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
427 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, rc
.bottom
,
428 &memdc
, border
, rc
.bottom
- border
);
430 dc
.Blit(0, 0, border
, rc
.bottom
,
433 dc
.Blit(rc
.right
- border
, 0, rc
.right
, rc
.bottom
,
434 &memdc
, rc
.right
- border
, 0);
437 // create the region excluding box children
438 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
440 ::GetWindowRect(GetHwnd(), &rcWin
);
441 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
443 // and also the box itself
444 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
445 HDCClipper
clipToBg(GetHdcOf(dc
), hrgn
);
447 // paint the inside of the box (excluding box itself and child controls)
448 PaintBackground(dc
, rc
);
451 #endif // !__WXWINCE__
453 #endif // wxUSE_STATBOX