]>
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"
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 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
103 *exstyle
= WS_EX_TRANSPARENT
;
109 styleWin
|= BS_GROUPBOX
;
111 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
113 // Make sure label is on the right
114 styleWin
|= BS_RIGHT
;
120 wxSize
wxStaticBox::DoGetBestSize() const
124 // Calculate the size needed by the label
126 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
129 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
132 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
134 // If there is a sizer then the base best size is the sizer's minimum
135 if (GetSizer() != NULL
)
137 wxSize
cm(GetSizer()->CalcMin());
138 best
= ClientToWindowSize(cm
);
139 // adjust for a long label if needed
140 best
.x
= wxMax(best
.x
, wBox
);
142 // otherwise the best size falls back to the label size
145 best
= wxSize(wBox
, hBox
);
150 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
152 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
154 // need extra space, don't know how much but this seems to be enough
155 *borderTop
+= GetCharHeight()/3;
158 // all the hacks below are not necessary for WinCE
161 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
163 if ( nMsg
== WM_NCHITTEST
)
165 // This code breaks some other processing such as enter/leave tracking
166 // so it's off by default.
168 static int s_useHTClient
= -1;
169 if (s_useHTClient
== -1)
170 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
171 if (s_useHTClient
== 1)
173 int xPos
= GET_X_LPARAM(lParam
);
174 int yPos
= GET_Y_LPARAM(lParam
);
176 ScreenToClient(&xPos
, &yPos
);
178 // Make sure you can drag by the top of the groupbox, but let
179 // other (enclosed) controls get mouse events also
181 return (long)HTCLIENT
;
185 if ( nMsg
== WM_PRINTCLIENT
)
187 // we have to process WM_PRINTCLIENT ourselves as otherwise child
188 // windows' background (eg buttons in radio box) would never be drawn
189 // unless we have a parent with non default background
191 // so check first if we have one
192 if ( !HandlePrintClient((WXHDC
)wParam
) )
194 // no, we don't, erase the background ourselves
195 // (don't use our own) - see PaintBackground for explanation
196 wxBrush
brush(GetParent()->GetBackgroundColour());
197 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
203 if ( nMsg
== WM_UPDATEUISTATE
)
205 // DefWindowProc() redraws just the static box text when it gets this
206 // message and it does it using the standard (blue in standard theme)
207 // colour and not our own label colour that we use in PaintForeground()
208 // resulting in the label mysteriously changing the colour when e.g.
209 // "Alt" is pressed anywhere in the window, see #12497.
211 // To avoid this we simply refresh the window forcing our own code
212 // redrawing the label in the correct colour to be called. This is
213 // inefficient but there doesn't seem to be anything else we can do.
215 // Notice that the problem is XP-specific and doesn't arise under later
217 if ( m_hasFgCol
&& wxGetWinVersion() == wxWinVersion_XP
)
221 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
224 // ----------------------------------------------------------------------------
225 // static box drawing
226 // ----------------------------------------------------------------------------
229 We draw the static box ourselves because it's the only way to prevent it
230 from flickering horribly on resize (because everything inside the box is
231 erased twice: once when the box itself is repainted and second time when
232 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
233 we used to do and which resulted in other problems.
236 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
238 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
240 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
243 wxLogLastError(wxT("CreateRectRgn()"));
247 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
250 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
252 HRGN hrgn
= (HRGN
)hRgn
;
254 // remove the area occupied by the static box borders from the region
255 int borderTop
, border
;
256 GetBordersForSizer(&borderTop
, &border
);
259 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
262 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
265 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
268 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
271 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
274 ::GetWindowRect(GetHwnd(), &rc
);
275 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
276 bool foundThis
= false;
278 // iterate over all child windows (not just wxWindows but all windows)
279 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
281 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
283 if ( ! ::IsWindowVisible(child
) )
285 // if the window isn't visible then it doesn't need clipped
289 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
290 wxString
str(wxGetWindowClass(child
));
292 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
294 if ( child
== GetHwnd() )
297 // Any static boxes below this one in the Z-order can't be clipped
298 // since if we have the case where a static box with a low Z-order
299 // is nested inside another static box with a high Z-order then the
300 // nested static box would be painted over. Doing it this way
301 // unfortunately results in flicker if the Z-order of nested static
302 // boxes is not inside (lowest) to outside (highest) but at least
303 // they are still shown.
308 ::GetWindowRect(child
, &rc
);
309 if ( ::RectInRegion(hrgn
, &rc
) )
311 // need to remove WS_CLIPSIBLINGS from all sibling windows
312 // that are within this staticbox if set
313 if ( style
& WS_CLIPSIBLINGS
)
315 style
&= ~WS_CLIPSIBLINGS
;
316 ::SetWindowLong(child
, GWL_STYLE
, style
);
318 // MSDN: "If you have changed certain window data using
319 // SetWindowLong, you must call SetWindowPos to have the
320 // changes take effect."
321 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
322 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
326 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
327 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
334 // helper for OnPaint(): really erase the background, i.e. do it even if we
335 // don't have any non default brush for doing it (DoEraseBackground() doesn't
336 // do anything in such case)
337 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
339 // note that we do not use the box background colour here, it shouldn't
340 // apply to its interior for several reasons:
341 // 1. wxGTK doesn't do it
342 // 2. controls inside the box don't get correct bg colour because they
343 // are not our children so we'd have some really ugly colour mix if
345 // 3. this is backwards compatible behaviour and some people rely on it,
346 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
347 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
348 HBRUSH hbr
= MSWGetBgBrush(impl
->GetHDC());
350 // if there is no special brush for painting this control, just use the
351 // solid background colour
355 brush
= wxBrush(GetParent()->GetBackgroundColour());
356 hbr
= GetHbrushOf(brush
);
359 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
362 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
364 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
365 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(*impl
), 0);
368 // when using XP themes, neither setting the text colour nor transparent
369 // background mode doesn't change anything: the static box def window proc
370 // still draws the label in its own colours, so we need to redraw the text
371 // ourselves if we have a non default fg colour
372 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
374 // draw over the text in default colour in our colour
375 HDC hdc
= GetHdcOf(*impl
);
376 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
378 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
380 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
382 // Get dimensions of the label
383 const wxString label
= GetLabel();
385 // choose the correct font
390 selFont
.Init(hdc
, GetHfontOf(GetFont()));
392 else // no font set, use the one set by the theme
394 wxUxThemeHandle
hTheme(this, L
"BUTTON");
397 wxUxThemeFont themeFont
;
398 if ( wxUxThemeEngine::Get()->GetThemeFont
408 font
.Init(themeFont
.GetLOGFONT());
410 selFont
.Init(hdc
, font
);
415 // Get the font extent
417 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
423 // first we need to correctly paint the background of the label
424 // as Windows ignores the brush offset when doing it
426 // FIXME: value of x is hardcoded as this is what it is on my system,
427 // no idea if it's true everywhere
428 RECT dimensions
= {0, 0, 0, y
};
433 dimensions
.right
= x
+ width
;
438 dimensions
.left
= x
- width
;
439 dimensions
.right
= x
;
442 // need to adjust the rectangle to cover all the label background
443 dimensions
.left
-= 2;
444 dimensions
.right
+= 2;
445 dimensions
.bottom
+= 2;
449 // our own background colour should be used for the background of
450 // the label: this is consistent with the behaviour under pre-XP
451 // systems (i.e. without visual themes) and generally makes sense
452 wxBrush brush
= wxBrush(GetBackgroundColour());
453 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
454 ::FillRect(GetHdcOf(*impl
), &dimensions
, GetHbrushOf(brush
));
456 else // paint parent background
458 PaintBackground(dc
, dimensions
);
461 UINT drawTextFlags
= DT_SINGLELINE
| DT_VCENTER
;
463 // determine the state of UI queues to draw the text correctly under XP
465 static const bool isXPorLater
= wxGetWinVersion() >= wxWinVersion_XP
;
468 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE
, 0, 0) &
471 drawTextFlags
|= DT_HIDEPREFIX
;
478 RECT rc2
= { x
, 0, x
+ width
, y
};
479 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
484 RECT rc2
= { x
, 0, x
- width
, y
};
485 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
486 drawTextFlags
| DT_RTLREADING
);
489 #endif // wxUSE_UXTHEME
492 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
495 ::GetClientRect(GetHwnd(), &rc
);
497 // draw the entire box in a memory DC
499 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
500 memdc
.SelectObject(bitmap
);
502 PaintBackground(memdc
, rc
);
503 PaintForeground(memdc
, rc
);
505 // now only blit the static box border itself, not the interior, to avoid
506 // flicker when background is drawn below
508 // note that it seems to be faster to do 4 small blits here and then paint
509 // directly into wxPaintDC than painting background in wxMemoryDC and then
510 // blitting everything at once to wxPaintDC, this is why we do it like this
512 int borderTop
, border
;
513 GetBordersForSizer(&borderTop
, &border
);
516 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
519 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, border
,
520 &memdc
, border
, rc
.bottom
- border
);
522 dc
.Blit(0, 0, border
, rc
.bottom
,
524 // right (note that upper and bottom right corners were already part of the
525 // first two blits so we shouldn't overwrite them here to avoi flicker)
526 dc
.Blit(rc
.right
- border
, borderTop
,
527 border
, rc
.bottom
- borderTop
- border
,
528 &memdc
, rc
.right
- border
, borderTop
);
531 // create the region excluding box children
532 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
534 ::GetWindowRect(GetHwnd(), &rcWin
);
535 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
537 // and also the box itself
538 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
539 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
540 HDCClipper
clipToBg(GetHdcOf(*impl
), hrgn
);
542 // paint the inside of the box (excluding box itself and child controls)
543 PaintBackground(dc
, rc
);
546 #endif // !__WXWINCE__
548 #endif // wxUSE_STATBOX