]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
42fec06f706bf3d497b040b47268847e213b8c6f
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "statbox.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dcclient.h"
38 #include "wx/statbox.h"
39 #include "wx/notebook.h"
40 #include "wx/sysopt.h"
42 #include "wx/dcmemory.h"
43 #include "wx/sysopt.h"
45 #include "wx/msw/private.h"
46 #include "wx/msw/missing.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #if wxUSE_EXTENDED_RTTI
53 WX_DEFINE_FLAGS( wxStaticBoxStyle
)
55 wxBEGIN_FLAGS( wxStaticBoxStyle
)
56 // new style border flags, we put them first to
57 // use them for streaming out
58 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
59 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
60 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
61 wxFLAGS_MEMBER(wxBORDER_RAISED
)
62 wxFLAGS_MEMBER(wxBORDER_STATIC
)
63 wxFLAGS_MEMBER(wxBORDER_NONE
)
65 // old style border flags
66 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
67 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
68 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
69 wxFLAGS_MEMBER(wxRAISED_BORDER
)
70 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
71 wxFLAGS_MEMBER(wxBORDER
)
73 // standard window styles
74 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
75 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
76 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
77 wxFLAGS_MEMBER(wxWANTS_CHARS
)
78 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
79 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
80 wxFLAGS_MEMBER(wxVSCROLL
)
81 wxFLAGS_MEMBER(wxHSCROLL
)
83 wxEND_FLAGS( wxStaticBoxStyle
)
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h")
87 wxBEGIN_PROPERTIES_TABLE(wxStaticBox
)
88 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
89 wxPROPERTY_FLAGS( WindowStyle
, wxStaticBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
94 wxEND_PROPERTIES_TABLE()
96 wxBEGIN_HANDLERS_TABLE(wxStaticBox
)
97 wxEND_HANDLERS_TABLE()
99 wxCONSTRUCTOR_6( wxStaticBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
101 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
)
104 // ============================================================================
106 // ============================================================================
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 bool wxStaticBox::Create(wxWindow
*parent
,
114 const wxString
& label
,
118 const wxString
& name
)
120 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
123 if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) )
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 return styleWin
| BS_GROUPBOX
;
160 wxSize
wxStaticBox::DoGetBestSize() const
163 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
166 GetTextExtent(wxGetWindowText(m_hWnd
), &wBox
, &cy
);
169 int hBox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
171 wxSize
best(wBox
, hBox
);
176 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const
178 wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
);
180 // need extra space, don't know how much but this seems to be enough
181 *borderTop
+= GetCharHeight()/3;
184 // all the hacks below are not necessary for WinCE
187 WXLRESULT
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
189 if ( nMsg
== WM_NCHITTEST
)
191 // This code breaks some other processing such as enter/leave tracking
192 // so it's off by default.
194 static int s_useHTClient
= -1;
195 if (s_useHTClient
== -1)
196 s_useHTClient
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
197 if (s_useHTClient
== 1)
199 int xPos
= GET_X_LPARAM(lParam
);
200 int yPos
= GET_Y_LPARAM(lParam
);
202 ScreenToClient(&xPos
, &yPos
);
204 // Make sure you can drag by the top of the groupbox, but let
205 // other (enclosed) controls get mouse events also
207 return (long)HTCLIENT
;
211 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
214 // ----------------------------------------------------------------------------
215 // static box drawing
216 // ----------------------------------------------------------------------------
219 We draw the static box ourselves because it's the only way to prevent it
220 from flickering horribly on resize (because everything inside the box is
221 erased twice: once when the box itself is repainted and second time when
222 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
223 we used to do and which resulted in other problems.
226 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
228 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
)
230 AutoHRGN
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
));
233 wxLogLastError(_T("CreateRectRgn()"));
237 ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
);
240 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
)
242 HRGN hrgn
= (HRGN
)hRgn
;
244 // remove the area occupied by the static box borders from the region
245 int borderTop
, border
;
246 GetBordersForSizer(&borderTop
, &border
);
249 SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
);
252 SubtractRectFromRgn(hrgn
, 0, h
- border
, w
, h
);
255 SubtractRectFromRgn(hrgn
, 0, 0, border
, h
);
258 SubtractRectFromRgn(hrgn
, w
- border
, 0, w
, h
);
261 WXHRGN
wxStaticBox::MSWGetRegionWithoutChildren()
264 ::GetWindowRect(GetHwnd(), &rc
);
265 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
267 // iterate over all child windows (not just wxWindows but all windows)
268 for ( HWND child
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
);
270 child
= ::GetWindow(child
, GW_HWNDNEXT
) )
272 if ( ! ::IsWindowVisible(child
) )
274 // if the window isn't visible then it doesn't need clipped
278 LONG style
= ::GetWindowLong(child
, GWL_STYLE
);
279 wxString
str(wxGetWindowClass(child
));
281 if ( str
== wxT("BUTTON") && (style
& BS_GROUPBOX
) == BS_GROUPBOX
)
283 // Don't clip any static boxes, not just this one. This will
284 // result in flicker in overlapping static boxes, but at least
285 // they will all be drawn correctly and we shouldn't have
286 // overlapping windows anyway.
290 ::GetWindowRect(child
, &rc
);
291 if ( ::RectInRegion(hrgn
, &rc
) )
293 // need to remove WS_CLIPSIBLINGS from all sibling windows
294 // that are within this staticbox if set
295 if ( style
& WS_CLIPSIBLINGS
)
297 style
&= ~WS_CLIPSIBLINGS
;
298 ::SetWindowLong(child
, GWL_STYLE
, style
);
300 // MSDN: "If you have changed certain window data using
301 // SetWindowLong, you must call SetWindowPos to have the
302 // changes take effect."
303 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0,
304 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
308 AutoHRGN
hrgnChild(::CreateRectRgnIndirect(&rc
));
309 ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
);
316 // helper for OnPaint(): really erase the background, i.e. do it even if we
317 // don't have any non default brush for doing it (DoEraseBackground() doesn't
318 // do anything in such case)
319 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
)
321 // note that we do not use the box background colour here, it shouldn't
322 // apply to its interior for several reasons:
323 // 1. wxGTK doesn't do it
324 // 2. controls inside the box don't get correct bg colour because they
325 // are not our children so we'd have some really ugly colour mix if
327 // 3. this is backwards compatible behaviour and some people rely on it,
328 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
329 wxWindow
*parent
= GetParent();
330 HBRUSH hbr
= (HBRUSH
)parent
->MSWGetBgBrush(dc
.GetHDC(), GetHWND());
332 // if there is no special brush for painting this control, just use the
333 // solid background colour
337 brush
= wxBrush(parent
->GetBackgroundColour());
338 hbr
= GetHbrushOf(brush
);
341 ::FillRect(GetHdcOf(dc
), &rc
, hbr
);
344 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& WXUNUSED(rc
))
346 // NB: neither setting the text colour nor transparent background mode
347 // doesn't change anything: the static box def window proc still
348 // draws the label in its own colours, so if we want to have control
349 // over this we really have to draw everything ourselves
350 MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(dc
), 0);
353 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
356 ::GetClientRect(GetHwnd(), &rc
);
358 // draw the entire box in a memory DC
360 wxBitmap
bitmap(rc
.right
, rc
.bottom
);
361 memdc
.SelectObject(bitmap
);
363 PaintBackground(memdc
, rc
);
364 PaintForeground(memdc
, rc
);
366 // now only blit the static box border itself, not the interior, to avoid
367 // flicker when background is drawn below
369 // note that it seems to be faster to do 4 small blits here and then paint
370 // directly into wxPaintDC than painting background in wxMemoryDC and then
371 // blitting everything at once to wxPaintDC, this is why we do it like this
373 int borderTop
, border
;
374 GetBordersForSizer(&borderTop
, &border
);
377 dc
.Blit(border
, 0, rc
.right
- border
, borderTop
,
380 dc
.Blit(border
, rc
.bottom
- border
, rc
.right
- border
, rc
.bottom
,
381 &memdc
, border
, rc
.bottom
- border
);
383 dc
.Blit(0, 0, border
, rc
.bottom
,
386 dc
.Blit(rc
.right
- border
, 0, rc
.right
, rc
.bottom
,
387 &memdc
, rc
.right
- border
, 0);
390 // create the region excluding box children
391 AutoHRGN
hrgn((HRGN
)MSWGetRegionWithoutChildren());
393 ::GetWindowRect(GetHwnd(), &rcWin
);
394 ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
);
396 // and also the box itself
397 MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
);
398 HDCClipper
clipToBg(GetHdcOf(dc
), hrgn
);
400 // paint the inside of the box (excluding box itself and child controls)
401 PaintBackground(dc
, rc
);
404 #endif // !__WXWINCE__
406 #endif // wxUSE_STATBOX