]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
3a2d88de4c1f630dad43b6e5d5c8aeba46537713
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"
39 #include "wx/notebook.h"
40 #include "wx/sysopt.h"
42 #include "wx/msw/uxtheme.h"
43 #include "wx/msw/private.h"
44 #include "wx/msw/missing.h"
45 #include "wx/msw/dc.h"
47 // the values coincide with those in tmschema.h
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 bool wxStaticBox::Create(wxWindow
*parent
,
68 const wxString
& label
,
74 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
77 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
80 // Always use LTR layout. Otherwise, the label would be mirrored.
81 SetLayoutDirection(wxLayout_LeftToRight
);
84 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
85 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
86 #endif // !__WXWINCE__
91 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
93 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
95 // no need for it anymore, must be removed for wxRadioBox child
96 // buttons to be able to repaint themselves
97 styleWin
&= ~WS_CLIPCHILDREN
;
102 // We may have children inside this static box, so use this style for
103 // TAB navigation to work if we ever use IsDialogMessage() to implement
104 // it (currently we don't because it's too buggy and implement TAB
105 // navigation ourselves, but this could change in the future).
106 *exstyle
|= WS_EX_CONTROLPARENT
;
108 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
109 *exstyle
|= WS_EX_TRANSPARENT
;
113 styleWin
|= BS_GROUPBOX
;
115 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
117 // Make sure label is on the right
118 styleWin
|= BS_RIGHT
;
124 wxSize
wxStaticBox::DoGetBestSize() const
128 // Calculate the size needed by the label
130 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
133 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
136 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
138 // If there is a sizer then the base best size is the sizer's minimum
139 if (GetSizer() != NULL
)
141 wxSize
cm(GetSizer()->CalcMin());
142 best
= ClientToWindowSize(cm
);
143 // adjust for a long label if needed
144 best
.x
= wxMax(best
.x
, wBox
);
146 // otherwise the best size falls back to the label size
149 best
= wxSize(wBox
, hBox
);
154 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
156 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
158 // need extra space, don't know how much but this seems to be enough
159 *borderTop
+= GetCharHeight()/3;
162 // all the hacks below are not necessary for WinCE
165 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
167 if ( nMsg
== WM_NCHITTEST
)
169 // This code breaks some other processing such as enter/leave tracking
170 // so it's off by default.
172 static int s_useHTClient
= -1;
173 if (s_useHTClient
== -1)
174 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
175 if (s_useHTClient
== 1)
177 int xPos
= GET_X_LPARAM(lParam
);
178 int yPos
= GET_Y_LPARAM(lParam
);
180 ScreenToClient(&xPos
, &yPos
);
182 // Make sure you can drag by the top of the groupbox, but let
183 // other (enclosed) controls get mouse events also
185 return (long)HTCLIENT
;
189 if ( nMsg
== WM_PRINTCLIENT
)
191 // we have to process WM_PRINTCLIENT ourselves as otherwise child
192 // windows' background (eg buttons in radio box) would never be drawn
193 // unless we have a parent with non default background
195 // so check first if we have one
196 if ( !HandlePrintClient((WXHDC
)wParam
) )
198 // no, we don't, erase the background ourselves
199 // (don't use our own) - see PaintBackground for explanation
200 wxBrush
brush(GetParent()->GetBackgroundColour());
201 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
207 if ( nMsg
== WM_UPDATEUISTATE
)
209 // DefWindowProc() redraws just the static box text when it gets this
210 // message and it does it using the standard (blue in standard theme)
211 // colour and not our own label colour that we use in PaintForeground()
212 // resulting in the label mysteriously changing the colour when e.g.
213 // "Alt" is pressed anywhere in the window, see #12497.
215 // To avoid this we simply refresh the window forcing our own code
216 // redrawing the label in the correct colour to be called. This is
217 // inefficient but there doesn't seem to be anything else we can do.
219 // Notice that the problem is XP-specific and doesn't arise under later
221 if ( m_hasFgCol
&& wxGetWinVersion() == wxWinVersion_XP
)
225 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
228 // ----------------------------------------------------------------------------
229 // static box drawing
230 // ----------------------------------------------------------------------------
233 We draw the static box ourselves because it's the only way to prevent it
234 from flickering horribly on resize (because everything inside the box is
235 erased twice: once when the box itself is repainted and second time when
236 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
237 we used to do and which resulted in other problems.
240 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
242 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
244 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
247 wxLogLastError(wxT("CreateRectRgn()"));
251 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
254 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
256 HRGN hrgn
= (HRGN
)hRgn
;
258 // remove the area occupied by the static box borders from the region
259 int borderTop
, border
;
260 GetBordersForSizer(&borderTop
, &border
);
263 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
266 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
269 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
272 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
275 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
278 ::GetWindowRect(GetHwnd(), &rc
);
279 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
280 bool foundThis
= false;
282 // iterate over all child windows (not just wxWindows but all windows)
283 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
285 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
287 if ( ! ::IsWindowVisible(child
) )
289 // if the window isn't visible then it doesn't need clipped
293 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
294 wxString
str(wxGetWindowClass(child
));
296 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
298 if ( child
== GetHwnd() )
301 // Any static boxes below this one in the Z-order can't be clipped
302 // since if we have the case where a static box with a low Z-order
303 // is nested inside another static box with a high Z-order then the
304 // nested static box would be painted over. Doing it this way
305 // unfortunately results in flicker if the Z-order of nested static
306 // boxes is not inside (lowest) to outside (highest) but at least
307 // they are still shown.
312 ::GetWindowRect(child
, &rc
);
313 if ( ::RectInRegion(hrgn
, &rc
) )
315 // need to remove WS_CLIPSIBLINGS from all sibling windows
316 // that are within this staticbox if set
317 if ( style
& WS_CLIPSIBLINGS
)
319 style
&= ~WS_CLIPSIBLINGS
;
320 ::SetWindowLong(child
, GWL_STYLE
, style
);
322 // MSDN: "If you have changed certain window data using
323 // SetWindowLong, you must call SetWindowPos to have the
324 // changes take effect."
325 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
326 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
330 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
331 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
338 // helper for OnPaint(): really erase the background, i.e. do it even if we
339 // don't have any non default brush for doing it (DoEraseBackground() doesn't
340 // do anything in such case)
341 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
343 // note that we do not use the box background colour here, it shouldn't
344 // apply to its interior for several reasons:
345 // 1. wxGTK doesn't do it
346 // 2. controls inside the box don't get correct bg colour because they
347 // are not our children so we'd have some really ugly colour mix if
349 // 3. this is backwards compatible behaviour and some people rely on it,
350 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
351 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
352 HBRUSH hbr
= MSWGetBgBrush(impl
->GetHDC());
354 // if there is no special brush for painting this control, just use the
355 // solid background colour
359 brush
= wxBrush(GetParent()->GetBackgroundColour());
360 hbr
= GetHbrushOf(brush
);
363 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
366 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
368 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
369 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(*impl
), 0);
372 // when using XP themes, neither setting the text colour nor transparent
373 // background mode doesn't change anything: the static box def window proc
374 // still draws the label in its own colours, so we need to redraw the text
375 // ourselves if we have a non default fg colour
376 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
378 // draw over the text in default colour in our colour
379 HDC hdc
= GetHdcOf(*impl
);
380 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
382 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
384 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
386 // Get dimensions of the label
387 const wxString label
= GetLabel();
389 // choose the correct font
394 selFont
.Init(hdc
, GetHfontOf(GetFont()));
396 else // no font set, use the one set by the theme
398 wxUxThemeHandle
hTheme(this, L
"BUTTON");
401 wxUxThemeFont themeFont
;
402 if ( wxUxThemeEngine::Get()->GetThemeFont
412 font
.Init(themeFont
.GetLOGFONT());
414 selFont
.Init(hdc
, font
);
419 // Get the font extent
421 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
427 // first we need to correctly paint the background of the label
428 // as Windows ignores the brush offset when doing it
430 // FIXME: value of x is hardcoded as this is what it is on my system,
431 // no idea if it's true everywhere
432 RECT dimensions
= {0, 0, 0, y
};
437 dimensions
.right
= x
+ width
;
442 dimensions
.left
= x
- width
;
443 dimensions
.right
= x
;
446 // need to adjust the rectangle to cover all the label background
447 dimensions
.left
-= 2;
448 dimensions
.right
+= 2;
449 dimensions
.bottom
+= 2;
453 // our own background colour should be used for the background of
454 // the label: this is consistent with the behaviour under pre-XP
455 // systems (i.e. without visual themes) and generally makes sense
456 wxBrush brush
= wxBrush(GetBackgroundColour());
457 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
458 ::FillRect(GetHdcOf(*impl
), &dimensions
, GetHbrushOf(brush
));
460 else // paint parent background
462 PaintBackground(dc
, dimensions
);
465 UINT drawTextFlags
= DT_SINGLELINE
| DT_VCENTER
;
467 // determine the state of UI queues to draw the text correctly under XP
469 static const bool isXPorLater
= wxGetWinVersion() >= wxWinVersion_XP
;
472 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE
, 0, 0) &
475 drawTextFlags
|= DT_HIDEPREFIX
;
482 RECT rc2
= { x
, 0, x
+ width
, y
};
483 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
488 RECT rc2
= { x
, 0, x
- width
, y
};
489 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
490 drawTextFlags
| DT_RTLREADING
);
493 #endif // wxUSE_UXTHEME
496 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
499 ::GetClientRect(GetHwnd(), &rc
);
501 // draw the entire box in a memory DC
503 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
504 memdc
.SelectObject(bitmap
);
506 PaintBackground(memdc
, rc
);
507 PaintForeground(memdc
, rc
);
509 // now only blit the static box border itself, not the interior, to avoid
510 // flicker when background is drawn below
512 // note that it seems to be faster to do 4 small blits here and then paint
513 // directly into wxPaintDC than painting background in wxMemoryDC and then
514 // blitting everything at once to wxPaintDC, this is why we do it like this
516 int borderTop
, border
;
517 GetBordersForSizer(&borderTop
, &border
);
520 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
523 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, border
,
524 &memdc
, border
, rc
.bottom
- border
);
526 dc
.Blit(0, 0, border
, rc
.bottom
,
528 // right (note that upper and bottom right corners were already part of the
529 // first two blits so we shouldn't overwrite them here to avoi flicker)
530 dc
.Blit(rc
.right
- border
, borderTop
,
531 border
, rc
.bottom
- borderTop
- border
,
532 &memdc
, rc
.right
- border
, borderTop
);
535 // create the region excluding box children
536 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
538 ::GetWindowRect(GetHwnd(), &rcWin
);
539 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
541 // and also the box itself
542 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
543 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
544 HDCClipper
clipToBg(GetHdcOf(*impl
), hrgn
);
546 // paint the inside of the box (excluding box itself and child controls)
547 PaintBackground(dc
, rc
);
550 #endif // !__WXWINCE__
552 #endif // wxUSE_STATBOX