]>
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 // ============================================================================
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 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
102 *exstyle
= WS_EX_TRANSPARENT
;
108 styleWin
|= BS_GROUPBOX
;
110 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
112 // Make sure label is on the right
113 styleWin
|= BS_RIGHT
;
119 wxSize
wxStaticBox::DoGetBestSize() const
122 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
125 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
128 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
130 wxSize
best(wBox
, hBox
);
135 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
137 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
139 // need extra space, don't know how much but this seems to be enough
140 *borderTop
+= GetCharHeight()/3;
143 // all the hacks below are not necessary for WinCE
146 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
148 if ( nMsg
== WM_NCHITTEST
)
150 // This code breaks some other processing such as enter/leave tracking
151 // so it's off by default.
153 static int s_useHTClient
= -1;
154 if (s_useHTClient
== -1)
155 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
156 if (s_useHTClient
== 1)
158 int xPos
= GET_X_LPARAM(lParam
);
159 int yPos
= GET_Y_LPARAM(lParam
);
161 ScreenToClient(&xPos
, &yPos
);
163 // Make sure you can drag by the top of the groupbox, but let
164 // other (enclosed) controls get mouse events also
166 return (long)HTCLIENT
;
170 if ( nMsg
== WM_PRINTCLIENT
)
172 // we have to process WM_PRINTCLIENT ourselves as otherwise child
173 // windows' background (eg buttons in radio box) would never be drawn
174 // unless we have a parent with non default background
176 // so check first if we have one
177 if ( !HandlePrintClient((WXHDC
)wParam
) )
179 // no, we don't, erase the background ourselves
180 // (don't use our own) - see PaintBackground for explanation
181 wxBrush
brush(GetParent()->GetBackgroundColour());
182 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
188 if ( nMsg
== WM_UPDATEUISTATE
)
190 // DefWindowProc() redraws just the static box text when it gets this
191 // message and it does it using the standard (blue in standard theme)
192 // colour and not our own label colour that we use in PaintForeground()
193 // resulting in the label mysteriously changing the colour when e.g.
194 // "Alt" is pressed anywhere in the window, see #12497.
196 // To avoid this we simply refresh the window forcing our own code
197 // redrawing the label in the correct colour to be called. This is
198 // inefficient but there doesn't seem to be anything else we can do.
200 // Notice that the problem is XP-specific and doesn't arise under later
202 if ( m_hasFgCol
&& wxGetWinVersion() == wxWinVersion_XP
)
206 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
209 // ----------------------------------------------------------------------------
210 // static box drawing
211 // ----------------------------------------------------------------------------
214 We draw the static box ourselves because it's the only way to prevent it
215 from flickering horribly on resize (because everything inside the box is
216 erased twice: once when the box itself is repainted and second time when
217 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
218 we used to do and which resulted in other problems.
221 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
223 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
225 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
228 wxLogLastError(wxT("CreateRectRgn()"));
232 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
235 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
237 HRGN hrgn
= (HRGN
)hRgn
;
239 // remove the area occupied by the static box borders from the region
240 int borderTop
, border
;
241 GetBordersForSizer(&borderTop
, &border
);
244 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
247 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
250 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
253 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
256 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
259 ::GetWindowRect(GetHwnd(), &rc
);
260 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
261 bool foundThis
= false;
263 // iterate over all child windows (not just wxWindows but all windows)
264 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
266 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
268 if ( ! ::IsWindowVisible(child
) )
270 // if the window isn't visible then it doesn't need clipped
274 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
275 wxString
str(wxGetWindowClass(child
));
277 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
279 if ( child
== GetHwnd() )
282 // Any static boxes below this one in the Z-order can't be clipped
283 // since if we have the case where a static box with a low Z-order
284 // is nested inside another static box with a high Z-order then the
285 // nested static box would be painted over. Doing it this way
286 // unfortunately results in flicker if the Z-order of nested static
287 // boxes is not inside (lowest) to outside (highest) but at least
288 // they are still shown.
293 ::GetWindowRect(child
, &rc
);
294 if ( ::RectInRegion(hrgn
, &rc
) )
296 // need to remove WS_CLIPSIBLINGS from all sibling windows
297 // that are within this staticbox if set
298 if ( style
& WS_CLIPSIBLINGS
)
300 style
&= ~WS_CLIPSIBLINGS
;
301 ::SetWindowLong(child
, GWL_STYLE
, style
);
303 // MSDN: "If you have changed certain window data using
304 // SetWindowLong, you must call SetWindowPos to have the
305 // changes take effect."
306 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
307 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
311 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
312 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
319 // helper for OnPaint(): really erase the background, i.e. do it even if we
320 // don't have any non default brush for doing it (DoEraseBackground() doesn't
321 // do anything in such case)
322 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
324 // note that we do not use the box background colour here, it shouldn't
325 // apply to its interior for several reasons:
326 // 1. wxGTK doesn't do it
327 // 2. controls inside the box don't get correct bg colour because they
328 // are not our children so we'd have some really ugly colour mix if
330 // 3. this is backwards compatible behaviour and some people rely on it,
331 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
332 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
333 HBRUSH hbr
= MSWGetBgBrush(impl
->GetHDC());
335 // if there is no special brush for painting this control, just use the
336 // solid background colour
340 brush
= wxBrush(GetParent()->GetBackgroundColour());
341 hbr
= GetHbrushOf(brush
);
344 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
347 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
349 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
350 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(*impl
), 0);
352 // when using XP themes, neither setting the text colour nor transparent
353 // background mode doesn't change anything: the static box def window proc
354 // still draws the label in its own colours, so we need to redraw the text
355 // ourselves if we have a non default fg colour
356 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
358 // draw over the text in default colour in our colour
359 HDC hdc
= GetHdcOf(*impl
);
360 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
362 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
364 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
366 // Get dimensions of the label
367 const wxString label
= GetLabel();
369 // choose the correct font
374 selFont
.Init(hdc
, GetHfontOf(GetFont()));
376 else // no font set, use the one set by the theme
378 wxUxThemeHandle
hTheme(this, L
"BUTTON");
381 wxUxThemeFont themeFont
;
382 if ( wxUxThemeEngine::Get()->GetThemeFont
392 font
.Init(themeFont
.GetLOGFONT());
394 selFont
.Init(hdc
, font
);
399 // Get the font extent
401 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
407 // first we need to correctly paint the background of the label
408 // as Windows ignores the brush offset when doing it
410 // FIXME: value of x is hardcoded as this is what it is on my system,
411 // no idea if it's true everywhere
412 RECT dimensions
= {0, 0, 0, y
};
417 dimensions
.right
= x
+ width
;
422 dimensions
.left
= x
- width
;
423 dimensions
.right
= x
;
426 // need to adjust the rectangle to cover all the label background
427 dimensions
.left
-= 2;
428 dimensions
.right
+= 2;
429 dimensions
.bottom
+= 2;
433 // our own background colour should be used for the background of
434 // the label: this is consistent with the behaviour under pre-XP
435 // systems (i.e. without visual themes) and generally makes sense
436 wxBrush brush
= wxBrush(GetBackgroundColour());
437 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
438 ::FillRect(GetHdcOf(*impl
), &dimensions
, GetHbrushOf(brush
));
440 else // paint parent background
442 PaintBackground(dc
, dimensions
);
445 UINT drawTextFlags
= DT_SINGLELINE
| DT_VCENTER
;
447 // determine the state of UI queues to draw the text correctly under XP
449 static const bool isXPorLater
= wxGetWinVersion() >= wxWinVersion_XP
;
452 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE
, 0, 0) &
455 drawTextFlags
|= DT_HIDEPREFIX
;
462 RECT rc2
= { x
, 0, x
+ width
, y
};
463 ::DrawText(hdc
, label
.wx_str(), label
.length(), &rc2
,
468 RECT rc2
= { x
, 0, x
- width
, y
};
469 ::DrawText(hdc
, label
.wx_str(), label
.length(), &rc2
,
470 drawTextFlags
| DT_RTLREADING
);
475 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
478 ::GetClientRect(GetHwnd(), &rc
);
480 // draw the entire box in a memory DC
482 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
483 memdc
.SelectObject(bitmap
);
485 PaintBackground(memdc
, rc
);
486 PaintForeground(memdc
, rc
);
488 // now only blit the static box border itself, not the interior, to avoid
489 // flicker when background is drawn below
491 // note that it seems to be faster to do 4 small blits here and then paint
492 // directly into wxPaintDC than painting background in wxMemoryDC and then
493 // blitting everything at once to wxPaintDC, this is why we do it like this
495 int borderTop
, border
;
496 GetBordersForSizer(&borderTop
, &border
);
499 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
502 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, border
,
503 &memdc
, border
, rc
.bottom
- border
);
505 dc
.Blit(0, 0, border
, rc
.bottom
,
507 // right (note that upper and bottom right corners were already part of the
508 // first two blits so we shouldn't overwrite them here to avoi flicker)
509 dc
.Blit(rc
.right
- border
, borderTop
,
510 border
, rc
.bottom
- borderTop
- border
,
511 &memdc
, rc
.right
- border
, borderTop
);
514 // create the region excluding box children
515 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
517 ::GetWindowRect(GetHwnd(), &rcWin
);
518 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
520 // and also the box itself
521 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
522 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
523 HDCClipper
clipToBg(GetHdcOf(*impl
), hrgn
);
525 // paint the inside of the box (excluding box itself and child controls)
526 PaintBackground(dc
, rc
);
529 #endif // !__WXWINCE__
531 #endif // wxUSE_STATBOX