]>
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
123 // Calculate the size needed by the label
125 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
128 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
131 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
133 // If there is a sizer then the base best size is the sizer's minimum
134 if (GetSizer() != NULL
)
136 wxSize
cm(GetSizer()->CalcMin());
137 best
= ClientToWindowSize(cm
);
138 // adjust for a long label if needed
139 best
.x
= wxMax(best
.x
, wBox
);
141 // otherwise the best size falls back to the label size
144 best
= wxSize(wBox
, hBox
);
149 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
151 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
153 // need extra space, don't know how much but this seems to be enough
154 *borderTop
+= GetCharHeight()/3;
157 // all the hacks below are not necessary for WinCE
160 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
162 if ( nMsg
== WM_NCHITTEST
)
164 // This code breaks some other processing such as enter/leave tracking
165 // so it's off by default.
167 static int s_useHTClient
= -1;
168 if (s_useHTClient
== -1)
169 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
170 if (s_useHTClient
== 1)
172 int xPos
= GET_X_LPARAM(lParam
);
173 int yPos
= GET_Y_LPARAM(lParam
);
175 ScreenToClient(&xPos
, &yPos
);
177 // Make sure you can drag by the top of the groupbox, but let
178 // other (enclosed) controls get mouse events also
180 return (long)HTCLIENT
;
184 if ( nMsg
== WM_PRINTCLIENT
)
186 // we have to process WM_PRINTCLIENT ourselves as otherwise child
187 // windows' background (eg buttons in radio box) would never be drawn
188 // unless we have a parent with non default background
190 // so check first if we have one
191 if ( !HandlePrintClient((WXHDC
)wParam
) )
193 // no, we don't, erase the background ourselves
194 // (don't use our own) - see PaintBackground for explanation
195 wxBrush
brush(GetParent()->GetBackgroundColour());
196 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
202 if ( nMsg
== WM_UPDATEUISTATE
)
204 // DefWindowProc() redraws just the static box text when it gets this
205 // message and it does it using the standard (blue in standard theme)
206 // colour and not our own label colour that we use in PaintForeground()
207 // resulting in the label mysteriously changing the colour when e.g.
208 // "Alt" is pressed anywhere in the window, see #12497.
210 // To avoid this we simply refresh the window forcing our own code
211 // redrawing the label in the correct colour to be called. This is
212 // inefficient but there doesn't seem to be anything else we can do.
214 // Notice that the problem is XP-specific and doesn't arise under later
216 if ( m_hasFgCol
&& wxGetWinVersion() == wxWinVersion_XP
)
220 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
223 // ----------------------------------------------------------------------------
224 // static box drawing
225 // ----------------------------------------------------------------------------
228 We draw the static box ourselves because it's the only way to prevent it
229 from flickering horribly on resize (because everything inside the box is
230 erased twice: once when the box itself is repainted and second time when
231 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
232 we used to do and which resulted in other problems.
235 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
237 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
239 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
242 wxLogLastError(wxT("CreateRectRgn()"));
246 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
249 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
251 HRGN hrgn
= (HRGN
)hRgn
;
253 // remove the area occupied by the static box borders from the region
254 int borderTop
, border
;
255 GetBordersForSizer(&borderTop
, &border
);
258 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
261 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
264 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
267 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
270 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
273 ::GetWindowRect(GetHwnd(), &rc
);
274 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
275 bool foundThis
= false;
277 // iterate over all child windows (not just wxWindows but all windows)
278 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
280 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
282 if ( ! ::IsWindowVisible(child
) )
284 // if the window isn't visible then it doesn't need clipped
288 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
289 wxString
str(wxGetWindowClass(child
));
291 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
293 if ( child
== GetHwnd() )
296 // Any static boxes below this one in the Z-order can't be clipped
297 // since if we have the case where a static box with a low Z-order
298 // is nested inside another static box with a high Z-order then the
299 // nested static box would be painted over. Doing it this way
300 // unfortunately results in flicker if the Z-order of nested static
301 // boxes is not inside (lowest) to outside (highest) but at least
302 // they are still shown.
307 ::GetWindowRect(child
, &rc
);
308 if ( ::RectInRegion(hrgn
, &rc
) )
310 // need to remove WS_CLIPSIBLINGS from all sibling windows
311 // that are within this staticbox if set
312 if ( style
& WS_CLIPSIBLINGS
)
314 style
&= ~WS_CLIPSIBLINGS
;
315 ::SetWindowLong(child
, GWL_STYLE
, style
);
317 // MSDN: "If you have changed certain window data using
318 // SetWindowLong, you must call SetWindowPos to have the
319 // changes take effect."
320 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
321 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
325 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
326 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
333 // helper for OnPaint(): really erase the background, i.e. do it even if we
334 // don't have any non default brush for doing it (DoEraseBackground() doesn't
335 // do anything in such case)
336 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
338 // note that we do not use the box background colour here, it shouldn't
339 // apply to its interior for several reasons:
340 // 1. wxGTK doesn't do it
341 // 2. controls inside the box don't get correct bg colour because they
342 // are not our children so we'd have some really ugly colour mix if
344 // 3. this is backwards compatible behaviour and some people rely on it,
345 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
346 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
347 HBRUSH hbr
= MSWGetBgBrush(impl
->GetHDC());
349 // if there is no special brush for painting this control, just use the
350 // solid background colour
354 brush
= wxBrush(GetParent()->GetBackgroundColour());
355 hbr
= GetHbrushOf(brush
);
358 ::FillRect(GetHdcOf(*impl
), &rc
, hbr
);
361 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
363 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
364 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(*impl
), 0);
366 // when using XP themes, neither setting the text colour nor transparent
367 // background mode doesn't change anything: the static box def window proc
368 // still draws the label in its own colours, so we need to redraw the text
369 // ourselves if we have a non default fg colour
370 if ( m_hasFgCol
&& wxUxThemeEngine::GetIfActive() )
372 // draw over the text in default colour in our colour
373 HDC hdc
= GetHdcOf(*impl
);
374 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
376 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
378 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
380 // Get dimensions of the label
381 const wxString label
= GetLabel();
383 // choose the correct font
388 selFont
.Init(hdc
, GetHfontOf(GetFont()));
390 else // no font set, use the one set by the theme
392 wxUxThemeHandle
hTheme(this, L
"BUTTON");
395 wxUxThemeFont themeFont
;
396 if ( wxUxThemeEngine::Get()->GetThemeFont
406 font
.Init(themeFont
.GetLOGFONT());
408 selFont
.Init(hdc
, font
);
413 // Get the font extent
415 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
),
421 // first we need to correctly paint the background of the label
422 // as Windows ignores the brush offset when doing it
424 // FIXME: value of x is hardcoded as this is what it is on my system,
425 // no idea if it's true everywhere
426 RECT dimensions
= {0, 0, 0, y
};
431 dimensions
.right
= x
+ width
;
436 dimensions
.left
= x
- width
;
437 dimensions
.right
= x
;
440 // need to adjust the rectangle to cover all the label background
441 dimensions
.left
-= 2;
442 dimensions
.right
+= 2;
443 dimensions
.bottom
+= 2;
447 // our own background colour should be used for the background of
448 // the label: this is consistent with the behaviour under pre-XP
449 // systems (i.e. without visual themes) and generally makes sense
450 wxBrush brush
= wxBrush(GetBackgroundColour());
451 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
452 ::FillRect(GetHdcOf(*impl
), &dimensions
, GetHbrushOf(brush
));
454 else // paint parent background
456 PaintBackground(dc
, dimensions
);
459 UINT drawTextFlags
= DT_SINGLELINE
| DT_VCENTER
;
461 // determine the state of UI queues to draw the text correctly under XP
463 static const bool isXPorLater
= wxGetWinVersion() >= wxWinVersion_XP
;
466 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE
, 0, 0) &
469 drawTextFlags
|= DT_HIDEPREFIX
;
476 RECT rc2
= { x
, 0, x
+ width
, y
};
477 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
482 RECT rc2
= { x
, 0, x
- width
, y
};
483 ::DrawText(hdc
, label
.t_str(), label
.length(), &rc2
,
484 drawTextFlags
| DT_RTLREADING
);
489 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
492 ::GetClientRect(GetHwnd(), &rc
);
494 // draw the entire box in a memory DC
496 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
497 memdc
.SelectObject(bitmap
);
499 PaintBackground(memdc
, rc
);
500 PaintForeground(memdc
, rc
);
502 // now only blit the static box border itself, not the interior, to avoid
503 // flicker when background is drawn below
505 // note that it seems to be faster to do 4 small blits here and then paint
506 // directly into wxPaintDC than painting background in wxMemoryDC and then
507 // blitting everything at once to wxPaintDC, this is why we do it like this
509 int borderTop
, border
;
510 GetBordersForSizer(&borderTop
, &border
);
513 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
516 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, border
,
517 &memdc
, border
, rc
.bottom
- border
);
519 dc
.Blit(0, 0, border
, rc
.bottom
,
521 // right (note that upper and bottom right corners were already part of the
522 // first two blits so we shouldn't overwrite them here to avoi flicker)
523 dc
.Blit(rc
.right
- border
, borderTop
,
524 border
, rc
.bottom
- borderTop
- border
,
525 &memdc
, rc
.right
- border
, borderTop
);
528 // create the region excluding box children
529 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
531 ::GetWindowRect(GetHwnd(), &rcWin
);
532 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
534 // and also the box itself
535 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
536 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
537 HDCClipper
clipToBg(GetHdcOf(*impl
), hrgn
);
539 // paint the inside of the box (excluding box itself and child controls)
540 PaintBackground(dc
, rc
);
543 #endif // !__WXWINCE__
546 wxPoint
wxStaticBox::GetClientAreaOrigin() const
548 // See: http://msdn.microsoft.com/en-us/library/aa511279.aspx
549 wxPoint pt
= ConvertDialogToPixels(wxPoint(6,11));
554 void wxStaticBox::DoGetClientSize(int *width
, int *height
) const
556 // See: http://msdn.microsoft.com/en-us/library/aa511279.aspx
557 wxPoint lr
= ConvertDialogToPixels(wxPoint(6,7));
558 wxPoint ul
= GetClientAreaOrigin();
559 wxSize sz
= GetSize();
562 *width
= sz
.x
- ul
.x
- lr
.x
;
564 *height
= sz
.y
- ul
.y
- lr
.x
;
567 #endif // wxUSE_STATBOX