]>
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
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")))
85 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
87 // Our OnPaint() completely erases our background, so don't do it in
88 // WM_ERASEBKGND too to avoid flicker.
89 SetBackgroundStyle(wxBG_STYLE_PAINT
);
91 #endif // !__WXWINCE__
96 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
98 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
100 // no need for it anymore, must be removed for wxRadioBox child
101 // buttons to be able to repaint themselves
102 styleWin
&= ~WS_CLIPCHILDREN
;
107 // We may have children inside this static box, so use this style for
108 // TAB navigation to work if we ever use IsDialogMessage() to implement
109 // it (currently we don't because it's too buggy and implement TAB
110 // navigation ourselves, but this could change in the future).
111 *exstyle
|= WS_EX_CONTROLPARENT
;
113 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
114 *exstyle
|= WS_EX_TRANSPARENT
;
118 styleWin
|= BS_GROUPBOX
;
120 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
122 // Make sure label is on the right
123 styleWin
|= BS_RIGHT
;
129 wxSize
wxStaticBox::DoGetBestSize() const
133 // Calculate the size needed by the label
135 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
138 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
141 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
143 // If there is a sizer then the base best size is the sizer's minimum
144 if (GetSizer() != NULL
)
146 wxSize
cm(GetSizer()->CalcMin());
147 best
= ClientToWindowSize(cm
);
148 // adjust for a long label if needed
149 best
.x
= wxMax(best
.x
, wBox
);
151 // otherwise the best size falls back to the label size
154 best
= wxSize(wBox
, hBox
);
159 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
161 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
163 // need extra space, don't know how much but this seems to be enough
164 *borderTop
+= GetCharHeight()/3;
167 // all the hacks below are not necessary for WinCE
170 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
172 if ( nMsg
== WM_NCHITTEST
)
174 // This code breaks some other processing such as enter/leave tracking
175 // so it's off by default.
177 static int s_useHTClient
= -1;
178 if (s_useHTClient
== -1)
179 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
180 if (s_useHTClient
== 1)
182 int xPos
= GET_X_LPARAM(lParam
);
183 int yPos
= GET_Y_LPARAM(lParam
);
185 ScreenToClient(&xPos
, &yPos
);
187 // Make sure you can drag by the top of the groupbox, but let
188 // other (enclosed) controls get mouse events also
190 return (long)HTCLIENT
;
194 if ( nMsg
== WM_PRINTCLIENT
)
196 // we have to process WM_PRINTCLIENT ourselves as otherwise child
197 // windows' background (eg buttons in radio box) would never be drawn
198 // unless we have a parent with non default background
200 // so check first if we have one
201 if ( !HandlePrintClient((WXHDC
)wParam
) )
203 // no, we don't, erase the background ourselves
204 // (don't use our own) - see PaintBackground for explanation
205 wxBrush
brush(GetParent()->GetBackgroundColour());
206 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
212 if ( nMsg
== WM_UPDATEUISTATE
)
214 // DefWindowProc() redraws just the static box text when it gets this
215 // message and it does it using the standard (blue in standard theme)
216 // colour and not our own label colour that we use in PaintForeground()
217 // resulting in the label mysteriously changing the colour when e.g.
218 // "Alt" is pressed anywhere in the window, see #12497.
220 // To avoid this we simply refresh the window forcing our own code
221 // redrawing the label in the correct colour to be called. This is
222 // inefficient but there doesn't seem to be anything else we can do.
224 // Notice that the problem is XP-specific and doesn't arise under later
226 if ( m_hasFgCol
&& wxGetWinVersion() == wxWinVersion_XP
)
230 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
233 // ----------------------------------------------------------------------------
234 // static box drawing
235 // ----------------------------------------------------------------------------
238 We draw the static box ourselves because it's the only way to prevent it
239 from flickering horribly on resize (because everything inside the box is
240 erased twice: once when the box itself is repainted and second time when
241 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
242 we used to do and which resulted in other problems.
245 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
247 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
249 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
252 wxLogLastError(wxT("CreateRectRgn()"));
256 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
259 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
261 HRGN hrgn
= (HRGN
)hRgn
;
263 // remove the area occupied by the static box borders from the region
264 int borderTop
, border
;
265 GetBordersForSizer(&borderTop
, &border
);
268 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
271 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
274 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
277 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
280 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
283 ::GetWindowRect(GetHwnd(), &rc
);
284 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
285 bool foundThis
= false;
287 // Iterate over all sibling windows as in the old wxWidgets API the
288 // controls appearing inside the static box were created as its siblings
289 // and not children. This is now deprecated but should still work.
291 // Also notice that we must iterate over all windows, not just all
292 // wxWindows, as there may be composite windows etc.
294 for ( child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
296 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
298 if ( ! ::IsWindowVisible(child
) )
300 // if the window isn't visible then it doesn't need clipped
304 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
305 wxString
str(wxGetWindowClass(child
));
307 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
309 if ( child
== GetHwnd() )
312 // Any static boxes below this one in the Z-order can't be clipped
313 // since if we have the case where a static box with a low Z-order
314 // is nested inside another static box with a high Z-order then the
315 // nested static box would be painted over. Doing it this way
316 // unfortunately results in flicker if the Z-order of nested static
317 // boxes is not inside (lowest) to outside (highest) but at least
318 // they are still shown.
323 ::GetWindowRect(child
, &rc
);
324 if ( ::RectInRegion(hrgn
, &rc
) )
326 // need to remove WS_CLIPSIBLINGS from all sibling windows
327 // that are within this staticbox if set
328 if ( style
& WS_CLIPSIBLINGS
)
330 style
&= ~WS_CLIPSIBLINGS
;
331 ::SetWindowLong(child
, GWL_STYLE
, style
);
333 // MSDN: "If you have changed certain window data using
334 // SetWindowLong, you must call SetWindowPos to have the
335 // changes take effect."
336 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
337 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
341 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
342 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
346 // Also iterate over all children of the static box, we need to clip them
348 for ( child
= ::GetWindow(GetHwnd(), GW_CHILD
);
350 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
352 if ( !::IsWindowVisible(child
) )
354 // if the window isn't visible then it doesn't need clipped
358 ::GetWindowRect(child
, &rc
);
359 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
360 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
366 // helper for OnPaint(): really erase the background, i.e. do it even if we
367 // don't have any non default brush for doing it (DoEraseBackground() doesn't
368 // do anything in such case)
369 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
371 // note that we do not use the box background colour here, it shouldn't
372 // apply to its interior for several reasons:
373 // 1. wxGTK doesn't do it
374 // 2. controls inside the box don't get correct bg colour because they
375 // are not our children so we'd have some really ugly colour mix if
377 // 3. this is backwards compatible behaviour and some people rely on it,
378 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
379 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
380 HBRUSH hbr
= MSWGetBgBrush(impl
->GetHDC());
382 // if there is no special brush for painting this control, just use the
383 // solid background colour
387 brush
= wxBrush(GetParent()->GetBackgroundColour());
388 hbr
= GetHbrushOf(brush
);
391 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
394 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
396 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
397 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(*impl
), 0);
400 // when using XP themes, neither setting the text colour nor transparent
401 // background mode doesn't change anything: the static box def window proc
402 // still draws the label in its own colours, so we need to redraw the text
403 // ourselves if we have a non default fg colour
404 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
406 // draw over the text in default colour in our colour
407 HDC hdc
= GetHdcOf(*impl
);
408 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
410 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
412 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
414 // Get dimensions of the label
415 const wxString label
= GetLabel();
417 // choose the correct font
422 selFont
.Init(hdc
, GetHfontOf(GetFont()));
424 else // no font set, use the one set by the theme
426 wxUxThemeHandle
hTheme(this, L
"BUTTON");
429 wxUxThemeFont themeFont
;
430 if ( wxUxThemeEngine::Get()->GetThemeFont
440 font
.Init(themeFont
.GetLOGFONT());
442 selFont
.Init(hdc
, font
);
447 // Get the font extent
449 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
455 // first we need to correctly paint the background of the label
456 // as Windows ignores the brush offset when doing it
458 // FIXME: value of x is hardcoded as this is what it is on my system,
459 // no idea if it's true everywhere
460 RECT dimensions
= {0, 0, 0, y
};
465 dimensions
.right
= x
+ width
;
470 dimensions
.left
= x
- width
;
471 dimensions
.right
= x
;
474 // need to adjust the rectangle to cover all the label background
475 dimensions
.left
-= 2;
476 dimensions
.right
+= 2;
477 dimensions
.bottom
+= 2;
481 // our own background colour should be used for the background of
482 // the label: this is consistent with the behaviour under pre-XP
483 // systems (i.e. without visual themes) and generally makes sense
484 wxBrush brush
= wxBrush(GetBackgroundColour());
485 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
486 ::FillRect(GetHdcOf(*impl
), &dimensions
, GetHbrushOf(brush
));
488 else // paint parent background
490 PaintBackground(dc
, dimensions
);
493 UINT drawTextFlags
= DT_SINGLELINE
| DT_VCENTER
;
495 // determine the state of UI queues to draw the text correctly under XP
497 static const bool isXPorLater
= wxGetWinVersion() >= wxWinVersion_XP
;
500 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE
, 0, 0) &
503 drawTextFlags
|= DT_HIDEPREFIX
;
510 RECT rc2
= { x
, 0, x
+ width
, y
};
511 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
516 RECT rc2
= { x
, 0, x
- width
, y
};
517 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
518 drawTextFlags
| DT_RTLREADING
);
521 #endif // wxUSE_UXTHEME
524 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
527 ::GetClientRect(GetHwnd(), &rc
);
529 // draw the entire box in a memory DC
531 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
532 memdc
.SelectObject(bitmap
);
534 PaintBackground(memdc
, rc
);
535 PaintForeground(memdc
, rc
);
537 // now only blit the static box border itself, not the interior, to avoid
538 // flicker when background is drawn below
540 // note that it seems to be faster to do 4 small blits here and then paint
541 // directly into wxPaintDC than painting background in wxMemoryDC and then
542 // blitting everything at once to wxPaintDC, this is why we do it like this
544 int borderTop
, border
;
545 GetBordersForSizer(&borderTop
, &border
);
548 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
551 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, border
,
552 &memdc
, border
, rc
.bottom
- border
);
554 dc
.Blit(0, 0, border
, rc
.bottom
,
556 // right (note that upper and bottom right corners were already part of the
557 // first two blits so we shouldn't overwrite them here to avoi flicker)
558 dc
.Blit(rc
.right
- border
, borderTop
,
559 border
, rc
.bottom
- borderTop
- border
,
560 &memdc
, rc
.right
- border
, borderTop
);
563 // create the region excluding box children
564 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
566 ::GetWindowRect(GetHwnd(), &rcWin
);
567 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
569 // and also the box itself
570 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
571 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
572 HDCClipper
clipToBg(GetHdcOf(*impl
), hrgn
);
574 // paint the inside of the box (excluding box itself and child controls)
575 PaintBackground(dc
, rc
);
578 #endif // !__WXWINCE__
580 #endif // wxUSE_STATBOX