]>
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"
32 #include "wx/msw/missing.h"
34 #include "wx/dcclient.h"
35 #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"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #if wxUSE_EXTENDED_RTTI
50 WX_DEFINE_FLAGS( wxStaticBoxStyle
)
52 wxBEGIN_FLAGS( wxStaticBoxStyle
)
53 // new style border flags, we put them first to
54 // use them for streaming out
55 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
56 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
57 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
58 wxFLAGS_MEMBER(wxBORDER_RAISED
)
59 wxFLAGS_MEMBER(wxBORDER_STATIC
)
60 wxFLAGS_MEMBER(wxBORDER_NONE
)
62 // old style border flags
63 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
64 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
65 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
66 wxFLAGS_MEMBER(wxRAISED_BORDER
)
67 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
68 wxFLAGS_MEMBER(wxBORDER
)
70 // standard window styles
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
74 wxFLAGS_MEMBER(wxWANTS_CHARS
)
75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
77 wxFLAGS_MEMBER(wxVSCROLL
)
78 wxFLAGS_MEMBER(wxHSCROLL
)
80 wxEND_FLAGS( wxStaticBoxStyle
)
82 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h")
84 wxBEGIN_PROPERTIES_TABLE(wxStaticBox
)
85 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
86 wxPROPERTY_FLAGS( WindowStyle
, wxStaticBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
91 wxEND_PROPERTIES_TABLE()
93 wxBEGIN_HANDLERS_TABLE(wxStaticBox
)
94 wxEND_HANDLERS_TABLE()
96 wxCONSTRUCTOR_6( wxStaticBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
98 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
)
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 bool wxStaticBox::Create(wxWindow
*parent
,
111 const wxString
& label
,
115 const wxString
& name
)
117 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
120 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
123 // Always use LTR layout. Otherwise, the label would be mirrored.
124 SetLayoutDirection(wxLayout_LeftToRight
);
127 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
128 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
));
129 #endif // !__WXWINCE__
134 wxBorder
wxStaticBox::GetDefaultBorder() const
136 return wxBORDER_NONE
;
139 WXDWORD
wxStaticBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
141 long styleWin
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
);
143 // no need for it anymore, must be removed for wxRadioBox child
144 // buttons to be able to repaint themselves
145 styleWin
&= ~WS_CLIPCHILDREN
;
150 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
151 *exstyle
= WS_EX_TRANSPARENT
;
157 styleWin
|= BS_GROUPBOX
;
159 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
161 // Make sure label is on the right
162 styleWin
|= BS_RIGHT
;
168 wxSize
wxStaticBox::DoGetBestSize() const
171 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
174 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd
)), &wBox
, &cy
);
177 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
179 wxSize
best(wBox
, hBox
);
184 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
186 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
188 // need extra space, don't know how much but this seems to be enough
189 *borderTop
+= GetCharHeight()/3;
192 // all the hacks below are not necessary for WinCE
195 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
197 if ( nMsg
== WM_NCHITTEST
)
199 // This code breaks some other processing such as enter/leave tracking
200 // so it's off by default.
202 static int s_useHTClient
= -1;
203 if (s_useHTClient
== -1)
204 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
205 if (s_useHTClient
== 1)
207 int xPos
= GET_X_LPARAM(lParam
);
208 int yPos
= GET_Y_LPARAM(lParam
);
210 ScreenToClient(&xPos
, &yPos
);
212 // Make sure you can drag by the top of the groupbox, but let
213 // other (enclosed) controls get mouse events also
215 return (long)HTCLIENT
;
219 if ( nMsg
== WM_PRINTCLIENT
)
221 // we have to process WM_PRINTCLIENT ourselves as otherwise child
222 // windows' background (eg buttons in radio box) would never be drawn
223 // unless we have a parent with non default background
225 // so check first if we have one
226 if ( !HandlePrintClient((WXHDC
)wParam
) )
228 // no, we don't, erase the background ourselves
229 // (don't use our own) - see PaintBackground for explanation
230 wxBrush
brush(GetParent()->GetBackgroundColour());
231 wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
));
237 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
240 // ----------------------------------------------------------------------------
241 // static box drawing
242 // ----------------------------------------------------------------------------
245 We draw the static box ourselves because it's the only way to prevent it
246 from flickering horribly on resize (because everything inside the box is
247 erased twice: once when the box itself is repainted and second time when
248 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
249 we used to do and which resulted in other problems.
252 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
254 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
256 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
259 wxLogLastError(_T("CreateRectRgn()"));
263 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
266 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
268 HRGN hrgn
= (HRGN
)hRgn
;
270 // remove the area occupied by the static box borders from the region
271 int borderTop
, border
;
272 GetBordersForSizer(&borderTop
, &border
);
275 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
278 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
281 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
284 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
287 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
290 ::GetWindowRect(GetHwnd(), &rc
);
291 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
292 bool foundThis
= false;
294 // iterate over all child windows (not just wxWindows but all windows)
295 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
297 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
299 if ( ! ::IsWindowVisible(child
) )
301 // if the window isn't visible then it doesn't need clipped
305 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
306 wxString
str(wxGetWindowClass(child
));
308 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
310 if ( child
== GetHwnd() )
313 // Any static boxes below this one in the Z-order can't be clipped
314 // since if we have the case where a static box with a low Z-order
315 // is nested inside another static box with a high Z-order then the
316 // nested static box would be painted over. Doing it this way
317 // unfortunately results in flicker if the Z-order of nested static
318 // boxes is not inside (lowest) to outside (highest) but at least
319 // they are still shown.
324 ::GetWindowRect(child
, &rc
);
325 if ( ::RectInRegion(hrgn
, &rc
) )
327 // need to remove WS_CLIPSIBLINGS from all sibling windows
328 // that are within this staticbox if set
329 if ( style
& WS_CLIPSIBLINGS
)
331 style
&= ~WS_CLIPSIBLINGS
;
332 ::SetWindowLong(child
, GWL_STYLE
, style
);
334 // MSDN: "If you have changed certain window data using
335 // SetWindowLong, you must call SetWindowPos to have the
336 // changes take effect."
337 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
338 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
342 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
343 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
350 // helper for OnPaint(): really erase the background, i.e. do it even if we
351 // don't have any non default brush for doing it (DoEraseBackground() doesn't
352 // do anything in such case)
353 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
355 // note that we do not use the box background colour here, it shouldn't
356 // apply to its interior for several reasons:
357 // 1. wxGTK doesn't do it
358 // 2. controls inside the box don't get correct bg colour because they
359 // are not our children so we'd have some really ugly colour mix if
361 // 3. this is backwards compatible behaviour and some people rely on it,
362 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
363 wxWindow
*parent
= GetParent();
364 HBRUSH hbr
= (HBRUSH
)parent
->MSWGetBgBrush(dc
.GetHDC(), GetHWND());
366 // if there is no special brush for painting this control, just use the
367 // solid background colour
371 brush
= wxBrush(parent
->GetBackgroundColour());
372 hbr
= GetHbrushOf(brush
);
375 ::FillRect(GetHdcOf(dc
), &rc
, hbr
);
378 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& rc
)
380 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(dc
), 0);
382 // when using XP themes, neither setting the text colour nor transparent
383 // background mode doesn't change anything: the static box def window proc
384 // still draws the label in its own colours, so we need to redraw the text
385 // ourselves if we have a non default fg colour
386 if ( wxUxThemeEngine::GetIfActive() )
388 // draw over the text in default colour in our colour
389 dc
.SetFont(GetFont());
391 HDC hdc
= GetHdcOf(dc
);
394 ::SetTextColor(hdc
, GetForegroundColour().GetPixel());
398 wxUxThemeHandle
hTheme(this, L
"BUTTON");
402 wxUxThemeEngine::Get()->GetThemeColor(
406 3803 /* TMT_TEXTCOLOR */,
409 ::SetTextColor(hdc
, col
);
413 // can't open the theme - default to blue
414 ::SetTextColor(hdc
, 0x00D54600);
418 // FIXME: value of x is hardcoded as this is what it is on my system,
419 // no idea if it's true everywhere
421 const bool rtl
= wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
;
423 ::SetTextAlign(hdc
, TA_RTLREADING
| TA_RIGHT
);
425 // Get dimensions of the label
426 const wxString label
= GetLabel();
428 dc
.GetTextExtent(wxStripMenuCodes(label
, wxStrip_Mnemonics
), &width
, &height
);
433 // first we need to correctly paint the background of the label
434 // as Windows ignores the brush offset when doing it
435 RECT dimensions
= {0, 0, 0, y
};
440 dimensions
.right
= x
+ width
;
445 dimensions
.left
= x
- width
;
446 dimensions
.right
= x
;
449 // need to adjust the rectangle to cover all the label background
450 dimensions
.left
-= 2;
451 dimensions
.right
+= 2;
452 dimensions
.bottom
+= 2;
453 PaintBackground(dc
, dimensions
);
458 ::DrawText(hdc
, label
, label
.length(), &dimensions
,
459 DT_SINGLELINE
| DT_VCENTER
);
463 RECT rc2
= { x
, 0, x
- width
, y
};
464 ::DrawText(hdc
, label
, label
.length(), &rc2
,
465 DT_SINGLELINE
| DT_VCENTER
| DT_RTLREADING
);
470 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
473 ::GetClientRect(GetHwnd(), &rc
);
475 // draw the entire box in a memory DC
477 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
478 memdc
.SelectObject(bitmap
);
480 PaintBackground(memdc
, rc
);
481 PaintForeground(memdc
, rc
);
483 // now only blit the static box border itself, not the interior, to avoid
484 // flicker when background is drawn below
486 // note that it seems to be faster to do 4 small blits here and then paint
487 // directly into wxPaintDC than painting background in wxMemoryDC and then
488 // blitting everything at once to wxPaintDC, this is why we do it like this
490 int borderTop
, border
;
491 GetBordersForSizer(&borderTop
, &border
);
494 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
497 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, rc
.bottom
,
498 &memdc
, border
, rc
.bottom
- border
);
500 dc
.Blit(0, 0, border
, rc
.bottom
,
503 dc
.Blit(rc
.right
- border
, 0, rc
.right
, rc
.bottom
,
504 &memdc
, rc
.right
- border
, 0);
507 // create the region excluding box children
508 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
510 ::GetWindowRect(GetHwnd(), &rcWin
);
511 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
513 // and also the box itself
514 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
515 HDCClipper
clipToBg(GetHdcOf(dc
), hrgn
);
517 // paint the inside of the box (excluding box itself and child controls)
518 PaintBackground(dc
, rc
);
521 #endif // !__WXWINCE__
523 #endif // wxUSE_STATBOX