]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/statbox.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcmemory.h"
38 #include "wx/notebook.h"
39 #include "wx/sysopt.h"
41 #include "wx/msw/uxtheme.h"
42 #include "wx/msw/private.h"
43 #include "wx/msw/missing.h"
45 // the values coincide with those in tmschema.h
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 #if wxUSE_EXTENDED_RTTI
57 WX_DEFINE_FLAGS( wxStaticBoxStyle
)
59 wxBEGIN_FLAGS( wxStaticBoxStyle
)
60 // new style border flags, we put them first to
61 // use them for streaming out
62 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
63 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
64 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
65 wxFLAGS_MEMBER(wxBORDER_RAISED
)
66 wxFLAGS_MEMBER(wxBORDER_STATIC
)
67 wxFLAGS_MEMBER(wxBORDER_NONE
)
69 // old style border flags
70 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
71 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
72 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
73 wxFLAGS_MEMBER(wxRAISED_BORDER
)
74 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
75 wxFLAGS_MEMBER(wxBORDER
)
77 // standard window styles
78 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
79 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
80 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
81 wxFLAGS_MEMBER(wxWANTS_CHARS
)
82 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
83 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
84 wxFLAGS_MEMBER(wxVSCROLL
)
85 wxFLAGS_MEMBER(wxHSCROLL
)
87 wxEND_FLAGS( wxStaticBoxStyle
)
89 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h")
91 wxBEGIN_PROPERTIES_TABLE(wxStaticBox
)
92 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
93 wxPROPERTY_FLAGS( WindowStyle
, wxStaticBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
98 wxEND_PROPERTIES_TABLE()
100 wxBEGIN_HANDLERS_TABLE(wxStaticBox
)
101 wxEND_HANDLERS_TABLE()
103 wxCONSTRUCTOR_6( wxStaticBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
105 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
)
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 bool wxStaticBox::Create(wxWindow
*parent
,
118 const wxString
& label
,
122 const wxString
& name
)
124 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
127 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
130 // Always use LTR layout. Otherwise, the label would be mirrored.
131 SetLayoutDirection(wxLayout_LeftToRight
);
134 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
135 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
136 #endif // !__WXWINCE__
141 wxBorder
wxStaticBox::GetDefaultBorder() const
143 return wxBORDER_NONE
;
146 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
148 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
150 // no need for it anymore, must be removed for wxRadioBox child
151 // buttons to be able to repaint themselves
152 styleWin
&= ~WS_CLIPCHILDREN
;
157 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
158 *exstyle
= WS_EX_TRANSPARENT
;
164 styleWin
|= BS_GROUPBOX
;
166 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
168 // Make sure label is on the right
169 styleWin
|= BS_RIGHT
;
175 wxSize
wxStaticBox::DoGetBestSize() const
178 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
181 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
184 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
186 wxSize
best(wBox
, hBox
);
191 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
193 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
195 // need extra space, don't know how much but this seems to be enough
196 *borderTop
+= GetCharHeight()/3;
199 // all the hacks below are not necessary for WinCE
202 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
204 if ( nMsg
== WM_NCHITTEST
)
206 // This code breaks some other processing such as enter/leave tracking
207 // so it's off by default.
209 static int s_useHTClient
= -1;
210 if (s_useHTClient
== -1)
211 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
212 if (s_useHTClient
== 1)
214 int xPos
= GET_X_LPARAM(lParam
);
215 int yPos
= GET_Y_LPARAM(lParam
);
217 ScreenToClient(&xPos
, &yPos
);
219 // Make sure you can drag by the top of the groupbox, but let
220 // other (enclosed) controls get mouse events also
222 return (long)HTCLIENT
;
226 if ( nMsg
== WM_PRINTCLIENT
)
228 // we have to process WM_PRINTCLIENT ourselves as otherwise child
229 // windows' background (eg buttons in radio box) would never be drawn
230 // unless we have a parent with non default background
232 // so check first if we have one
233 if ( !HandlePrintClient((WXHDC
)wParam
) )
235 // no, we don't, erase the background ourselves
236 // (don't use our own) - see PaintBackground for explanation
237 wxBrush
brush(GetParent()->GetBackgroundColour());
238 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
244 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
247 // ----------------------------------------------------------------------------
248 // static box drawing
249 // ----------------------------------------------------------------------------
252 We draw the static box ourselves because it's the only way to prevent it
253 from flickering horribly on resize (because everything inside the box is
254 erased twice: once when the box itself is repainted and second time when
255 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
256 we used to do and which resulted in other problems.
259 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
261 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
263 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
266 wxLogLastError(_T("CreateRectRgn()"));
270 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
273 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
275 HRGN hrgn
= (HRGN
)hRgn
;
277 // remove the area occupied by the static box borders from the region
278 int borderTop
, border
;
279 GetBordersForSizer(&borderTop
, &border
);
282 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
285 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
288 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
291 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
294 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
297 ::GetWindowRect(GetHwnd(), &rc
);
298 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
299 bool foundThis
= false;
301 // iterate over all child windows (not just wxWindows but all windows)
302 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
304 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
306 if ( ! ::IsWindowVisible(child
) )
308 // if the window isn't visible then it doesn't need clipped
312 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
313 wxString
str(wxGetWindowClass(child
));
315 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
317 if ( child
== GetHwnd() )
320 // Any static boxes below this one in the Z-order can't be clipped
321 // since if we have the case where a static box with a low Z-order
322 // is nested inside another static box with a high Z-order then the
323 // nested static box would be painted over. Doing it this way
324 // unfortunately results in flicker if the Z-order of nested static
325 // boxes is not inside (lowest) to outside (highest) but at least
326 // they are still shown.
331 ::GetWindowRect(child
, &rc
);
332 if ( ::RectInRegion(hrgn
, &rc
) )
334 // need to remove WS_CLIPSIBLINGS from all sibling windows
335 // that are within this staticbox if set
336 if ( style
& WS_CLIPSIBLINGS
)
338 style
&= ~WS_CLIPSIBLINGS
;
339 ::SetWindowLong(child
, GWL_STYLE
, style
);
341 // MSDN: "If you have changed certain window data using
342 // SetWindowLong, you must call SetWindowPos to have the
343 // changes take effect."
344 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
345 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
349 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
350 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
357 // helper for OnPaint(): really erase the background, i.e. do it even if we
358 // don't have any non default brush for doing it (DoEraseBackground() doesn't
359 // do anything in such case)
360 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
362 // note that we do not use the box background colour here, it shouldn't
363 // apply to its interior for several reasons:
364 // 1. wxGTK doesn't do it
365 // 2. controls inside the box don't get correct bg colour because they
366 // are not our children so we'd have some really ugly colour mix if
368 // 3. this is backwards compatible behaviour and some people rely on it,
369 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
370 wxWindow
*parent
= GetParent();
371 HBRUSH hbr
= (HBRUSH
)parent
->MSWGetBgBrush(dc
.GetHDC(), GetHWND());
373 // if there is no special brush for painting this control, just use the
374 // solid background colour
378 brush
= wxBrush(parent
->GetBackgroundColour());
379 hbr
= GetHbrushOf(brush
);
382 ::FillRect(GetHdcOf(dc
), &rc
, hbr
);
385 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
387 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(dc
), 0);
389 // when using XP themes, neither setting the text colour nor transparent
390 // background mode doesn't change anything: the static box def window proc
391 // still draws the label in its own colours, so we need to redraw the text
392 // ourselves if we have a non default fg colour
393 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
395 // draw over the text in default colour in our colour
396 HDC hdc
= GetHdcOf(dc
);
397 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
399 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
401 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
403 // Get dimensions of the label
404 const wxString label
= GetLabel();
406 // choose the correct font
411 selFont
.Init(hdc
, GetHfontOf(GetFont()));
413 else // no font set, use the one set by the theme
415 wxUxThemeHandle
hTheme(this, L
"BUTTON");
418 // GetThemeFont() expects its parameter to be LOGFONTW and not
419 // LOGFONTA even in ANSI programs and will happily corrupt
420 // memory after the struct end if we pass a LOGFONTA (which is
423 if ( wxUxThemeEngine::Get()->GetThemeFont
434 // ok, no conversion necessary
435 const LOGFONT
& lf
= lfw
;
436 #else // !wxUSE_UNICODE
437 // most of the fields are the same in LOGFONTA and LOGFONTW
439 memcpy(&lf
, &lfw
, sizeof(lf
));
441 // but the face name must be converted
442 WideCharToMultiByte(CP_ACP
, 0, lfw
.lfFaceName
, -1,
443 lf
.lfFaceName
, sizeof(lf
.lfFaceName
),
445 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
449 selFont
.Init(hdc
, font
);
454 // Get the font extent
456 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
462 // first we need to correctly paint the background of the label
463 // as Windows ignores the brush offset when doing it
465 // FIXME: value of x is hardcoded as this is what it is on my system,
466 // no idea if it's true everywhere
467 RECT dimensions
= {0, 0, 0, y
};
472 dimensions
.right
= x
+ width
;
477 dimensions
.left
= x
- width
;
478 dimensions
.right
= x
;
481 // need to adjust the rectangle to cover all the label background
482 dimensions
.left
-= 2;
483 dimensions
.right
+= 2;
484 dimensions
.bottom
+= 2;
488 // our own background colour should be used for the background of
489 // the label: this is consistent with the behaviour under pre-XP
490 // systems (i.e. without visual themes) and generally makes sense
491 wxBrush brush
= wxBrush(GetBackgroundColour());
492 ::FillRect(GetHdcOf(dc
), &dimensions
, GetHbrushOf(brush
));
494 else // paint parent background
496 PaintBackground(dc
, dimensions
);
502 RECT rc2
= { x
, 0, x
+ width
, y
};
503 ::DrawText(hdc
, label
, label
.length(), &rc2
,
504 DT_SINGLELINE
| DT_VCENTER
);
508 RECT rc2
= { x
, 0, x
- width
, y
};
509 ::DrawText(hdc
, label
, label
.length(), &rc2
,
510 DT_SINGLELINE
| DT_VCENTER
| DT_RTLREADING
);
515 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
518 ::GetClientRect(GetHwnd(), &rc
);
520 // draw the entire box in a memory DC
522 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
523 memdc
.SelectObject(bitmap
);
525 PaintBackground(memdc
, rc
);
526 PaintForeground(memdc
, rc
);
528 // now only blit the static box border itself, not the interior, to avoid
529 // flicker when background is drawn below
531 // note that it seems to be faster to do 4 small blits here and then paint
532 // directly into wxPaintDC than painting background in wxMemoryDC and then
533 // blitting everything at once to wxPaintDC, this is why we do it like this
535 int borderTop
, border
;
536 GetBordersForSizer(&borderTop
, &border
);
539 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
542 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, rc
.bottom
,
543 &memdc
, border
, rc
.bottom
- border
);
545 dc
.Blit(0, 0, border
, rc
.bottom
,
548 dc
.Blit(rc
.right
- border
, 0, rc
.right
, rc
.bottom
,
549 &memdc
, rc
.right
- border
, 0);
552 // create the region excluding box children
553 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
555 ::GetWindowRect(GetHwnd(), &rcWin
);
556 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
558 // and also the box itself
559 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
560 HDCClipper
clipToBg(GetHdcOf(dc
), hrgn
);
562 // paint the inside of the box (excluding box itself and child controls)
563 PaintBackground(dc
, rc
);
566 #endif // !__WXWINCE__
568 #endif // wxUSE_STATBOX