]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
da42478341ff7fda738cd8510ae86d987e91873b
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/statbox.cpp
3 // Purpose: wxStaticBox
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/statbox.h"
32 #include "wx/dcclient.h"
33 #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 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 bool wxStaticBox::Create(wxWindow
*parent
,
67 const wxString
& label
,
73 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
76 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
79 // Always use LTR layout. Otherwise, the label would be mirrored.
80 SetLayoutDirection(wxLayout_LeftToRight
);
83 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
84 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
85 #endif // !__WXWINCE__
90 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
92 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
94 // no need for it anymore, must be removed for wxRadioBox child
95 // buttons to be able to repaint themselves
96 styleWin
&= ~WS_CLIPCHILDREN
;
101 // We may have children inside this static box, so use this style for
102 // TAB navigation to work if we ever use IsDialogMessage() to implement
103 // it (currently we don't because it's too buggy and implement TAB
104 // navigation ourselves, but this could change in the future).
105 *exstyle
|= WS_EX_CONTROLPARENT
;
107 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
108 *exstyle
|= WS_EX_TRANSPARENT
;
112 styleWin
|= BS_GROUPBOX
;
114 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
116 // Make sure label is on the right
117 styleWin
|= BS_RIGHT
;
123 wxSize
wxStaticBox::DoGetBestSize() const
127 // Calculate the size needed by the label
129 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
132 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
135 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
137 // If there is a sizer then the base best size is the sizer's minimum
138 if (GetSizer() != NULL
)
140 wxSize
cm(GetSizer()->CalcMin());
141 best
= ClientToWindowSize(cm
);
142 // adjust for a long label if needed
143 best
.x
= wxMax(best
.x
, wBox
);
145 // otherwise the best size falls back to the label size
148 best
= wxSize(wBox
, hBox
);
153 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
155 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
157 // need extra space, don't know how much but this seems to be enough
158 *borderTop
+= GetCharHeight()/3;
161 // all the hacks below are not necessary for WinCE
164 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
166 if ( nMsg
== WM_NCHITTEST
)
168 // This code breaks some other processing such as enter/leave tracking
169 // so it's off by default.
171 static int s_useHTClient
= -1;
172 if (s_useHTClient
== -1)
173 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
174 if (s_useHTClient
== 1)
176 int xPos
= GET_X_LPARAM(lParam
);
177 int yPos
= GET_Y_LPARAM(lParam
);
179 ScreenToClient(&xPos
, &yPos
);
181 // Make sure you can drag by the top of the groupbox, but let
182 // other (enclosed) controls get mouse events also
184 return (long)HTCLIENT
;
188 if ( nMsg
== WM_PRINTCLIENT
)
190 // we have to process WM_PRINTCLIENT ourselves as otherwise child
191 // windows' background (eg buttons in radio box) would never be drawn
192 // unless we have a parent with non default background
194 // so check first if we have one
195 if ( !HandlePrintClient((WXHDC
)wParam
) )
197 // no, we don't, erase the background ourselves
198 // (don't use our own) - see PaintBackground for explanation
199 wxBrush
brush(GetParent()->GetBackgroundColour());
200 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
206 if ( nMsg
== WM_UPDATEUISTATE
)
208 // DefWindowProc() redraws just the static box text when it gets this
209 // message and it does it using the standard (blue in standard theme)
210 // colour and not our own label colour that we use in PaintForeground()
211 // resulting in the label mysteriously changing the colour when e.g.
212 // "Alt" is pressed anywhere in the window, see #12497.
214 // To avoid this we simply refresh the window forcing our own code
215 // redrawing the label in the correct colour to be called. This is
216 // inefficient but there doesn't seem to be anything else we can do.
218 // Notice that the problem is XP-specific and doesn't arise under later
220 if ( m_hasFgCol
&& wxGetWinVersion() == wxWinVersion_XP
)
224 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
227 // ----------------------------------------------------------------------------
228 // static box drawing
229 // ----------------------------------------------------------------------------
232 We draw the static box ourselves because it's the only way to prevent it
233 from flickering horribly on resize (because everything inside the box is
234 erased twice: once when the box itself is repainted and second time when
235 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
236 we used to do and which resulted in other problems.
239 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
241 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
243 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
246 wxLogLastError(wxT("CreateRectRgn()"));
250 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
253 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
255 HRGN hrgn
= (HRGN
)hRgn
;
257 // remove the area occupied by the static box borders from the region
258 int borderTop
, border
;
259 GetBordersForSizer(&borderTop
, &border
);
262 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
265 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
268 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
271 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
274 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
277 ::GetWindowRect(GetHwnd(), &rc
);
278 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
279 bool foundThis
= false;
281 // iterate over all child windows (not just wxWindows but all windows)
282 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
284 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
286 if ( ! ::IsWindowVisible(child
) )
288 // if the window isn't visible then it doesn't need clipped
292 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
293 wxString
str(wxGetWindowClass(child
));
295 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
297 if ( child
== GetHwnd() )
300 // Any static boxes below this one in the Z-order can't be clipped
301 // since if we have the case where a static box with a low Z-order
302 // is nested inside another static box with a high Z-order then the
303 // nested static box would be painted over. Doing it this way
304 // unfortunately results in flicker if the Z-order of nested static
305 // boxes is not inside (lowest) to outside (highest) but at least
306 // they are still shown.
311 ::GetWindowRect(child
, &rc
);
312 if ( ::RectInRegion(hrgn
, &rc
) )
314 // need to remove WS_CLIPSIBLINGS from all sibling windows
315 // that are within this staticbox if set
316 if ( style
& WS_CLIPSIBLINGS
)
318 style
&= ~WS_CLIPSIBLINGS
;
319 ::SetWindowLong(child
, GWL_STYLE
, style
);
321 // MSDN: "If you have changed certain window data using
322 // SetWindowLong, you must call SetWindowPos to have the
323 // changes take effect."
324 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
325 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
329 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
330 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
337 // helper for OnPaint(): really erase the background, i.e. do it even if we
338 // don't have any non default brush for doing it (DoEraseBackground() doesn't
339 // do anything in such case)
340 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
342 // note that we do not use the box background colour here, it shouldn't
343 // apply to its interior for several reasons:
344 // 1. wxGTK doesn't do it
345 // 2. controls inside the box don't get correct bg colour because they
346 // are not our children so we'd have some really ugly colour mix if
348 // 3. this is backwards compatible behaviour and some people rely on it,
349 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
350 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
351 HBRUSH hbr
= MSWGetBgBrush(impl
->GetHDC());
353 // if there is no special brush for painting this control, just use the
354 // solid background colour
358 brush
= wxBrush(GetParent()->GetBackgroundColour());
359 hbr
= GetHbrushOf(brush
);
362 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
365 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
367 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
368 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(*impl
), 0);
371 // when using XP themes, neither setting the text colour nor transparent
372 // background mode doesn't change anything: the static box def window proc
373 // still draws the label in its own colours, so we need to redraw the text
374 // ourselves if we have a non default fg colour
375 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
377 // draw over the text in default colour in our colour
378 HDC hdc
= GetHdcOf(*impl
);
379 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
381 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
383 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
385 // Get dimensions of the label
386 const wxString label
= GetLabel();
388 // choose the correct font
393 selFont
.Init(hdc
, GetHfontOf(GetFont()));
395 else // no font set, use the one set by the theme
397 wxUxThemeHandle
hTheme(this, L
"BUTTON");
400 wxUxThemeFont themeFont
;
401 if ( wxUxThemeEngine::Get()->GetThemeFont
411 font
.Init(themeFont
.GetLOGFONT());
413 selFont
.Init(hdc
, font
);
418 // Get the font extent
420 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
426 // first we need to correctly paint the background of the label
427 // as Windows ignores the brush offset when doing it
429 // FIXME: value of x is hardcoded as this is what it is on my system,
430 // no idea if it's true everywhere
431 RECT dimensions
= {0, 0, 0, y
};
436 dimensions
.right
= x
+ width
;
441 dimensions
.left
= x
- width
;
442 dimensions
.right
= x
;
445 // need to adjust the rectangle to cover all the label background
446 dimensions
.left
-= 2;
447 dimensions
.right
+= 2;
448 dimensions
.bottom
+= 2;
452 // our own background colour should be used for the background of
453 // the label: this is consistent with the behaviour under pre-XP
454 // systems (i.e. without visual themes) and generally makes sense
455 wxBrush brush
= wxBrush(GetBackgroundColour());
456 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
457 ::FillRect(GetHdcOf(*impl
), &dimensions
, GetHbrushOf(brush
));
459 else // paint parent background
461 PaintBackground(dc
, dimensions
);
464 UINT drawTextFlags
= DT_SINGLELINE
| DT_VCENTER
;
466 // determine the state of UI queues to draw the text correctly under XP
468 static const bool isXPorLater
= wxGetWinVersion() >= wxWinVersion_XP
;
471 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE
, 0, 0) &
474 drawTextFlags
|= DT_HIDEPREFIX
;
481 RECT rc2
= { x
, 0, x
+ width
, y
};
482 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
487 RECT rc2
= { x
, 0, x
- width
, y
};
488 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
489 drawTextFlags
| DT_RTLREADING
);
492 #endif // wxUSE_UXTHEME
495 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
498 ::GetClientRect(GetHwnd(), &rc
);
500 // draw the entire box in a memory DC
502 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
503 memdc
.SelectObject(bitmap
);
505 PaintBackground(memdc
, rc
);
506 PaintForeground(memdc
, rc
);
508 // now only blit the static box border itself, not the interior, to avoid
509 // flicker when background is drawn below
511 // note that it seems to be faster to do 4 small blits here and then paint
512 // directly into wxPaintDC than painting background in wxMemoryDC and then
513 // blitting everything at once to wxPaintDC, this is why we do it like this
515 int borderTop
, border
;
516 GetBordersForSizer(&borderTop
, &border
);
519 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
522 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, border
,
523 &memdc
, border
, rc
.bottom
- border
);
525 dc
.Blit(0, 0, border
, rc
.bottom
,
527 // right (note that upper and bottom right corners were already part of the
528 // first two blits so we shouldn't overwrite them here to avoi flicker)
529 dc
.Blit(rc
.right
- border
, borderTop
,
530 border
, rc
.bottom
- borderTop
- border
,
531 &memdc
, rc
.right
- border
, borderTop
);
534 // create the region excluding box children
535 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
537 ::GetWindowRect(GetHwnd(), &rcWin
);
538 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
540 // and also the box itself
541 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
542 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
543 HDCClipper
clipToBg(GetHdcOf(*impl
), hrgn
);
545 // paint the inside of the box (excluding box itself and child controls)
546 PaintBackground(dc
, rc
);
549 #endif // !__WXWINCE__
551 #endif // wxUSE_STATBOX