]>
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"
44 #include "wx/msw/dc.h"
46 // the values coincide with those in tmschema.h
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 #if wxUSE_EXTENDED_RTTI
58 WX_DEFINE_FLAGS( wxStaticBoxStyle
)
60 wxBEGIN_FLAGS( wxStaticBoxStyle
)
61 // new style border flags, we put them first to
62 // use them for streaming out
63 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
64 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
65 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
66 wxFLAGS_MEMBER(wxBORDER_RAISED
)
67 wxFLAGS_MEMBER(wxBORDER_STATIC
)
68 wxFLAGS_MEMBER(wxBORDER_NONE
)
70 // old style border flags
71 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
72 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
73 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
74 wxFLAGS_MEMBER(wxRAISED_BORDER
)
75 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
76 wxFLAGS_MEMBER(wxBORDER
)
78 // standard window styles
79 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
80 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
81 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
82 wxFLAGS_MEMBER(wxWANTS_CHARS
)
83 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
84 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
85 wxFLAGS_MEMBER(wxVSCROLL
)
86 wxFLAGS_MEMBER(wxHSCROLL
)
88 wxEND_FLAGS( wxStaticBoxStyle
)
90 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h")
92 wxBEGIN_PROPERTIES_TABLE(wxStaticBox
)
93 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
94 wxPROPERTY_FLAGS( WindowStyle
, wxStaticBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
99 wxEND_PROPERTIES_TABLE()
101 wxBEGIN_HANDLERS_TABLE(wxStaticBox
)
102 wxEND_HANDLERS_TABLE()
104 wxCONSTRUCTOR_6( wxStaticBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
106 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
)
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 bool wxStaticBox::Create(wxWindow
*parent
,
119 const wxString
& label
,
123 const wxString
& name
)
125 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
128 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
131 // Always use LTR layout. Otherwise, the label would be mirrored.
132 SetLayoutDirection(wxLayout_LeftToRight
);
135 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
136 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
137 #endif // !__WXWINCE__
142 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
144 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
146 // no need for it anymore, must be removed for wxRadioBox child
147 // buttons to be able to repaint themselves
148 styleWin
&= ~WS_CLIPCHILDREN
;
153 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
154 *exstyle
= WS_EX_TRANSPARENT
;
160 styleWin
|= BS_GROUPBOX
;
162 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
164 // Make sure label is on the right
165 styleWin
|= BS_RIGHT
;
171 wxSize
wxStaticBox::DoGetBestSize() const
174 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
177 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
180 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
182 wxSize
best(wBox
, hBox
);
187 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
189 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
191 // need extra space, don't know how much but this seems to be enough
192 *borderTop
+= GetCharHeight()/3;
195 // all the hacks below are not necessary for WinCE
198 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
200 if ( nMsg
== WM_NCHITTEST
)
202 // This code breaks some other processing such as enter/leave tracking
203 // so it's off by default.
205 static int s_useHTClient
= -1;
206 if (s_useHTClient
== -1)
207 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
208 if (s_useHTClient
== 1)
210 int xPos
= GET_X_LPARAM(lParam
);
211 int yPos
= GET_Y_LPARAM(lParam
);
213 ScreenToClient(&xPos
, &yPos
);
215 // Make sure you can drag by the top of the groupbox, but let
216 // other (enclosed) controls get mouse events also
218 return (long)HTCLIENT
;
222 if ( nMsg
== WM_PRINTCLIENT
)
224 // we have to process WM_PRINTCLIENT ourselves as otherwise child
225 // windows' background (eg buttons in radio box) would never be drawn
226 // unless we have a parent with non default background
228 // so check first if we have one
229 if ( !HandlePrintClient((WXHDC
)wParam
) )
231 // no, we don't, erase the background ourselves
232 // (don't use our own) - see PaintBackground for explanation
233 wxBrush
brush(GetParent()->GetBackgroundColour());
234 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
240 if ( nMsg
== WM_UPDATEUISTATE
)
242 // DefWindowProc() redraws just the static box text when it gets this
243 // message and it does it using the standard (blue in standard theme)
244 // colour and not our own label colour that we use in PaintForeground()
245 // resulting in the label mysteriously changing the colour when e.g.
246 // "Alt" is pressed anywhere in the window, see #12497.
248 // To avoid this we simply refresh the window forcing our own code
249 // redrawing the label in the correct colour to be called. This is
250 // inefficient but there doesn't seem to be anything else we can do.
252 // Notice that the problem is XP-specific and doesn't arise under later
254 if ( m_hasFgCol
&& wxGetWinVersion() == wxWinVersion_XP
)
258 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
261 // ----------------------------------------------------------------------------
262 // static box drawing
263 // ----------------------------------------------------------------------------
266 We draw the static box ourselves because it's the only way to prevent it
267 from flickering horribly on resize (because everything inside the box is
268 erased twice: once when the box itself is repainted and second time when
269 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
270 we used to do and which resulted in other problems.
273 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
275 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
277 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
280 wxLogLastError(wxT("CreateRectRgn()"));
284 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
287 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
289 HRGN hrgn
= (HRGN
)hRgn
;
291 // remove the area occupied by the static box borders from the region
292 int borderTop
, border
;
293 GetBordersForSizer(&borderTop
, &border
);
296 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
299 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
302 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
305 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
308 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
311 ::GetWindowRect(GetHwnd(), &rc
);
312 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
313 bool foundThis
= false;
315 // iterate over all child windows (not just wxWindows but all windows)
316 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
318 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
320 if ( ! ::IsWindowVisible(child
) )
322 // if the window isn't visible then it doesn't need clipped
326 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
327 wxString
str(wxGetWindowClass(child
));
329 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
331 if ( child
== GetHwnd() )
334 // Any static boxes below this one in the Z-order can't be clipped
335 // since if we have the case where a static box with a low Z-order
336 // is nested inside another static box with a high Z-order then the
337 // nested static box would be painted over. Doing it this way
338 // unfortunately results in flicker if the Z-order of nested static
339 // boxes is not inside (lowest) to outside (highest) but at least
340 // they are still shown.
345 ::GetWindowRect(child
, &rc
);
346 if ( ::RectInRegion(hrgn
, &rc
) )
348 // need to remove WS_CLIPSIBLINGS from all sibling windows
349 // that are within this staticbox if set
350 if ( style
& WS_CLIPSIBLINGS
)
352 style
&= ~WS_CLIPSIBLINGS
;
353 ::SetWindowLong(child
, GWL_STYLE
, style
);
355 // MSDN: "If you have changed certain window data using
356 // SetWindowLong, you must call SetWindowPos to have the
357 // changes take effect."
358 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
359 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
363 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
364 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
371 // helper for OnPaint(): really erase the background, i.e. do it even if we
372 // don't have any non default brush for doing it (DoEraseBackground() doesn't
373 // do anything in such case)
374 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
376 // note that we do not use the box background colour here, it shouldn't
377 // apply to its interior for several reasons:
378 // 1. wxGTK doesn't do it
379 // 2. controls inside the box don't get correct bg colour because they
380 // are not our children so we'd have some really ugly colour mix if
382 // 3. this is backwards compatible behaviour and some people rely on it,
383 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
384 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
385 HBRUSH hbr
= MSWGetBgBrush(impl
->GetHDC());
387 // if there is no special brush for painting this control, just use the
388 // solid background colour
392 brush
= wxBrush(GetParent()->GetBackgroundColour());
393 hbr
= GetHbrushOf(brush
);
396 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
399 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
401 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
402 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(*impl
), 0);
404 // when using XP themes, neither setting the text colour nor transparent
405 // background mode doesn't change anything: the static box def window proc
406 // still draws the label in its own colours, so we need to redraw the text
407 // ourselves if we have a non default fg colour
408 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
410 // draw over the text in default colour in our colour
411 HDC hdc
= GetHdcOf(*impl
);
412 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
414 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
416 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
418 // Get dimensions of the label
419 const wxString label
= GetLabel();
421 // choose the correct font
426 selFont
.Init(hdc
, GetHfontOf(GetFont()));
428 else // no font set, use the one set by the theme
430 wxUxThemeHandle
hTheme(this, L
"BUTTON");
433 // GetThemeFont() expects its parameter to be LOGFONTW and not
434 // LOGFONTA even in ANSI programs and will happily corrupt
435 // memory after the struct end if we pass a LOGFONTA (which is
438 if ( wxUxThemeEngine::Get()->GetThemeFont
449 // ok, no conversion necessary
450 const LOGFONT
& lf
= lfw
;
451 #else // !wxUSE_UNICODE
452 // most of the fields are the same in LOGFONTA and LOGFONTW
454 memcpy(&lf
, &lfw
, sizeof(lf
));
456 // but the face name must be converted
457 WideCharToMultiByte(CP_ACP
, 0, lfw
.lfFaceName
, -1,
458 lf
.lfFaceName
, sizeof(lf
.lfFaceName
),
460 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
464 selFont
.Init(hdc
, font
);
469 // Get the font extent
471 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
477 // first we need to correctly paint the background of the label
478 // as Windows ignores the brush offset when doing it
480 // FIXME: value of x is hardcoded as this is what it is on my system,
481 // no idea if it's true everywhere
482 RECT dimensions
= {0, 0, 0, y
};
487 dimensions
.right
= x
+ width
;
492 dimensions
.left
= x
- width
;
493 dimensions
.right
= x
;
496 // need to adjust the rectangle to cover all the label background
497 dimensions
.left
-= 2;
498 dimensions
.right
+= 2;
499 dimensions
.bottom
+= 2;
503 // our own background colour should be used for the background of
504 // the label: this is consistent with the behaviour under pre-XP
505 // systems (i.e. without visual themes) and generally makes sense
506 wxBrush brush
= wxBrush(GetBackgroundColour());
507 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
508 ::FillRect(GetHdcOf(*impl
), &dimensions
, GetHbrushOf(brush
));
510 else // paint parent background
512 PaintBackground(dc
, dimensions
);
515 UINT drawTextFlags
= DT_SINGLELINE
| DT_VCENTER
;
517 // determine the state of UI queues to draw the text correctly under XP
519 static const bool isXPorLater
= wxGetWinVersion() >= wxWinVersion_XP
;
522 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE
, 0, 0) &
525 drawTextFlags
|= DT_HIDEPREFIX
;
532 RECT rc2
= { x
, 0, x
+ width
, y
};
533 ::DrawText(hdc
, label
.wx_str(), label
.length(), &rc2
,
538 RECT rc2
= { x
, 0, x
- width
, y
};
539 ::DrawText(hdc
, label
.wx_str(), label
.length(), &rc2
,
540 drawTextFlags
| DT_RTLREADING
);
545 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
548 ::GetClientRect(GetHwnd(), &rc
);
550 // draw the entire box in a memory DC
552 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
553 memdc
.SelectObject(bitmap
);
555 PaintBackground(memdc
, rc
);
556 PaintForeground(memdc
, rc
);
558 // now only blit the static box border itself, not the interior, to avoid
559 // flicker when background is drawn below
561 // note that it seems to be faster to do 4 small blits here and then paint
562 // directly into wxPaintDC than painting background in wxMemoryDC and then
563 // blitting everything at once to wxPaintDC, this is why we do it like this
565 int borderTop
, border
;
566 GetBordersForSizer(&borderTop
, &border
);
569 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
572 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, border
,
573 &memdc
, border
, rc
.bottom
- border
);
575 dc
.Blit(0, 0, border
, rc
.bottom
,
577 // right (note that upper and bottom right corners were already part of the
578 // first two blits so we shouldn't overwrite them here to avoi flicker)
579 dc
.Blit(rc
.right
- border
, borderTop
,
580 border
, rc
.bottom
- borderTop
- border
,
581 &memdc
, rc
.right
- border
, borderTop
);
584 // create the region excluding box children
585 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
587 ::GetWindowRect(GetHwnd(), &rcWin
);
588 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
590 // and also the box itself
591 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
592 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
593 HDCClipper
clipToBg(GetHdcOf(*impl
), hrgn
);
595 // paint the inside of the box (excluding box itself and child controls)
596 PaintBackground(dc
, rc
);
599 #endif // !__WXWINCE__
601 #endif // wxUSE_STATBOX