]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbox.cpp
remove the search for parent with wxTAB_TRAVERSAL style in SetDefaultStyle(): it...
[wxWidgets.git] / src / msw / statbox.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
f38924e8 2// Name: src/msw/statbox.cpp
2bda0e17
KB
3// Purpose: wxStaticBox
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
3f2711d5
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
3f2711d5 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_STATBOX
28
f38924e8
WS
29#include "wx/statbox.h"
30
2bda0e17 31#ifndef WX_PRECOMP
3f2711d5
VZ
32 #include "wx/app.h"
33 #include "wx/dcclient.h"
f38924e8 34 #include "wx/dcmemory.h"
155ecd4c 35 #include "wx/image.h"
2bda0e17
KB
36#endif
37
3ad70d31 38#include "wx/notebook.h"
9dabade2 39#include "wx/sysopt.h"
2bda0e17 40
72a55896 41#include "wx/msw/uxtheme.h"
3f2711d5 42#include "wx/msw/private.h"
72093cd8 43#include "wx/msw/missing.h"
3f2711d5
VZ
44
45// ----------------------------------------------------------------------------
46// wxWin macros
47// ----------------------------------------------------------------------------
48
51741307 49#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
50WX_DEFINE_FLAGS( wxStaticBoxStyle )
51
3ff066a4 52wxBEGIN_FLAGS( wxStaticBoxStyle )
bc9fb572
JS
53 // new style border flags, we put them first to
54 // use them for streaming out
3ff066a4
SC
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)
57f4f925 61
bc9fb572 62 // old style border flags
3ff066a4
SC
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)
cb0afb26 68 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
69
70 // standard window styles
3ff066a4
SC
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
74 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
77 wxFLAGS_MEMBER(wxVSCROLL)
78 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 79
3ff066a4 80wxEND_FLAGS( wxStaticBoxStyle )
bc9fb572 81
51741307
SC
82IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h")
83
3ff066a4 84wxBEGIN_PROPERTIES_TABLE(wxStaticBox)
57f4f925 85 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 86 wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
066f1b7a 87/*
57f4f925
WS
88 TODO PROPERTIES :
89 label
066f1b7a 90*/
3ff066a4 91wxEND_PROPERTIES_TABLE()
51741307 92
3ff066a4
SC
93wxBEGIN_HANDLERS_TABLE(wxStaticBox)
94wxEND_HANDLERS_TABLE()
51741307 95
57f4f925 96wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
51741307
SC
97#else
98IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
99#endif
2bda0e17 100
3f2711d5
VZ
101// ============================================================================
102// implementation
103// ============================================================================
104
105// ----------------------------------------------------------------------------
106// wxStaticBox
107// ----------------------------------------------------------------------------
108
109bool wxStaticBox::Create(wxWindow *parent,
110 wxWindowID id,
111 const wxString& label,
112 const wxPoint& pos,
113 const wxSize& size,
114 long style,
115 const wxString& name)
2bda0e17 116{
11b6a93b 117 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
57f4f925 118 return false;
2bda0e17 119
e31e6ea7 120 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
57f4f925 121 return false;
2bda0e17 122
9f769708 123#ifndef __WXWINCE__
5bf4788a
JS
124 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
125 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
c3732409 126#endif // !__WXWINCE__
3b6e5fb3 127
57f4f925 128 return true;
2bda0e17
KB
129}
130
9f769708
JS
131wxBorder wxStaticBox::GetDefaultBorder() const
132{
133 return wxBORDER_NONE;
134}
135
e31e6ea7
VZ
136WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
137{
138 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
139
eba99da4
JS
140 // no need for it anymore, must be removed for wxRadioBox child
141 // buttons to be able to repaint themselves
142 styleWin &= ~WS_CLIPCHILDREN;
143
e31e6ea7 144 if ( exstyle )
5bf4788a 145 {
a92e7f30 146#ifndef __WXWINCE__
5bf4788a
JS
147 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
148 *exstyle = WS_EX_TRANSPARENT;
149 else
a92e7f30 150#endif
5bf4788a
JS
151 *exstyle = 0;
152 }
a83b5b74 153
e31e6ea7
VZ
154 return styleWin | BS_GROUPBOX;
155}
156
f68586e5 157wxSize wxStaticBox::DoGetBestSize() const
2bda0e17 158{
4438caf4 159 int cx, cy;
7a5e53ab 160 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
2bda0e17 161
4438caf4 162 int wBox;
9f8130d6 163 GetTextExtent(wxStripMenuCodes(wxGetWindowText(m_hWnd)), &wBox, &cy);
2bda0e17 164
4438caf4
VZ
165 wBox += 3*cx;
166 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
2bda0e17 167
31582e4e
RD
168 wxSize best(wBox, hBox);
169 CacheBestSize(best);
170 return best;
4438caf4 171}
2bda0e17 172
c3732409 173void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
6aa01033 174{
c3732409
VZ
175 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
176
177 // need extra space, don't know how much but this seems to be enough
178 *borderTop += GetCharHeight()/3;
179}
180
181// all the hacks below are not necessary for WinCE
abf912c5 182#ifndef __WXWINCE__
c3732409
VZ
183
184WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
185{
58385af0 186 if ( nMsg == WM_NCHITTEST )
6aa01033 187 {
58385af0
VZ
188 // This code breaks some other processing such as enter/leave tracking
189 // so it's off by default.
190
191 static int s_useHTClient = -1;
192 if (s_useHTClient == -1)
193 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
194 if (s_useHTClient == 1)
195 {
c3732409
VZ
196 int xPos = GET_X_LPARAM(lParam);
197 int yPos = GET_Y_LPARAM(lParam);
58385af0
VZ
198
199 ScreenToClient(&xPos, &yPos);
200
201 // Make sure you can drag by the top of the groupbox, but let
202 // other (enclosed) controls get mouse events also
203 if ( yPos < 10 )
204 return (long)HTCLIENT;
205 }
6aa01033
JS
206 }
207
9705fbe9
VZ
208 if ( nMsg == WM_PRINTCLIENT )
209 {
210 // we have to process WM_PRINTCLIENT ourselves as otherwise child
211 // windows' background (eg buttons in radio box) would never be drawn
212 // unless we have a parent with non default background
213
214 // so check first if we have one
215 if ( !HandlePrintClient((WXHDC)wParam) )
216 {
217 // no, we don't, erase the background ourselves
218 // (don't use our own) - see PaintBackground for explanation
219 wxBrush brush(GetParent()->GetBackgroundColour());
220 wxFillRect(GetHwnd(), (HDC)wParam, GetHbrushOf(brush));
221 }
222
223 return 0;
224 }
225
6aa01033
JS
226 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
227}
228
c3732409
VZ
229// ----------------------------------------------------------------------------
230// static box drawing
231// ----------------------------------------------------------------------------
58385af0 232
c3732409
VZ
233/*
234 We draw the static box ourselves because it's the only way to prevent it
235 from flickering horribly on resize (because everything inside the box is
236 erased twice: once when the box itself is repainted and second time when
237 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
238 we used to do and which resulted in other problems.
239 */
58385af0 240
3b6e5fb3
VZ
241// MSWGetRegionWithoutSelf helper: removes the given rectangle from region
242static inline void
243SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
244{
245 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
246 if ( !hrgnRect )
247 {
248 wxLogLastError(_T("CreateRectRgn()"));
249 return;
250 }
251
252 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
253}
254
255void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
eba99da4 256{
3b6e5fb3
VZ
257 HRGN hrgn = (HRGN)hRgn;
258
259 // remove the area occupied by the static box borders from the region
260 int borderTop, border;
261 GetBordersForSizer(&borderTop, &border);
eba99da4
JS
262
263 // top
3b6e5fb3 264 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
eba99da4
JS
265
266 // bottom
3b6e5fb3 267 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
eba99da4
JS
268
269 // left
3b6e5fb3 270 SubtractRectFromRgn(hrgn, 0, 0, border, h);
eba99da4
JS
271
272 // right
3b6e5fb3 273 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
eba99da4
JS
274}
275
3b6e5fb3 276WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
eba99da4
JS
277{
278 RECT rc;
279 ::GetWindowRect(GetHwnd(), &rc);
280 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
cec46079 281 bool foundThis = false;
3b6e5fb3
VZ
282
283 // iterate over all child windows (not just wxWindows but all windows)
284 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
285 child;
286 child = ::GetWindow(child, GW_HWNDNEXT) )
eba99da4 287 {
4249e334
RD
288 if ( ! ::IsWindowVisible(child) )
289 {
290 // if the window isn't visible then it doesn't need clipped
291 continue;
292 }
9705fbe9 293
4249e334
RD
294 LONG style = ::GetWindowLong(child, GWL_STYLE);
295 wxString str(wxGetWindowClass(child));
296 str.UpperCase();
5c17f6d3 297 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
eba99da4 298 {
cec46079
JS
299 if ( child == GetHwnd() )
300 foundThis = true;
301
302 // Any static boxes below this one in the Z-order can't be clipped
303 // since if we have the case where a static box with a low Z-order
304 // is nested inside another static box with a high Z-order then the
305 // nested static box would be painted over. Doing it this way
306 // unfortunately results in flicker if the Z-order of nested static
307 // boxes is not inside (lowest) to outside (highest) but at least
308 // they are still shown.
309 if ( foundThis )
310 continue;
4249e334 311 }
9705fbe9 312
4249e334
RD
313 ::GetWindowRect(child, &rc);
314 if ( ::RectInRegion(hrgn, &rc) )
315 {
316 // need to remove WS_CLIPSIBLINGS from all sibling windows
317 // that are within this staticbox if set
318 if ( style & WS_CLIPSIBLINGS )
eba99da4 319 {
4249e334
RD
320 style &= ~WS_CLIPSIBLINGS;
321 ::SetWindowLong(child, GWL_STYLE, style);
9705fbe9 322
4249e334
RD
323 // MSDN: "If you have changed certain window data using
324 // SetWindowLong, you must call SetWindowPos to have the
325 // changes take effect."
326 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
327 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
328 SWP_FRAMECHANGED);
eba99da4 329 }
4249e334
RD
330
331 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
332 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
eba99da4 333 }
eba99da4 334 }
eba99da4 335
3b6e5fb3
VZ
336 return (WXHRGN)hrgn;
337}
338
c3732409
VZ
339// helper for OnPaint(): really erase the background, i.e. do it even if we
340// don't have any non default brush for doing it (DoEraseBackground() doesn't
341// do anything in such case)
3b6e5fb3
VZ
342void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
343{
c3732409
VZ
344 // note that we do not use the box background colour here, it shouldn't
345 // apply to its interior for several reasons:
346 // 1. wxGTK doesn't do it
347 // 2. controls inside the box don't get correct bg colour because they
348 // are not our children so we'd have some really ugly colour mix if
349 // we did it
350 // 3. this is backwards compatible behaviour and some people rely on it,
351 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
352 wxWindow *parent = GetParent();
bcb8ba61 353 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), GetHWND());
c3732409
VZ
354
355 // if there is no special brush for painting this control, just use the
356 // solid background colour
357 wxBrush brush;
3b6e5fb3
VZ
358 if ( !hbr )
359 {
c3732409
VZ
360 brush = wxBrush(parent->GetBackgroundColour());
361 hbr = GetHbrushOf(brush);
3b6e5fb3
VZ
362 }
363
c3732409 364 ::FillRect(GetHdcOf(dc), &rc, hbr);
eba99da4
JS
365}
366
21c6080d
JS
367void wxStaticBox::PaintForeground(wxDC& dc, const RECT& WXUNUSED(rc))
368{
21c6080d 369 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(dc), 0);
72a55896
VZ
370
371 // when using XP themes, neither setting the text colour nor transparent
372 // background mode doesn't change anything: the static box def window proc
373 // still draws the label in its own colours, so we need to redraw the text
374 // ourselves if we have a non default fg colour
375 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
376 {
377 // draw over the text in default colour in our colour
378 dc.SetFont(GetFont());
379
380 HDC hdc = GetHdcOf(dc);
381 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
382
383 // FIXME: value of x is hardcoded as this is what it is on my system,
384 // no idea if it's true everywhere
385 const int y = dc.GetCharHeight();
386 const int x = 9;
387
388 // TODO: RTL?
1d6e5768 389 RECT rc = { x, 0, GetSize().x, y };
72a55896
VZ
390
391 const wxString label = GetLabel();
392 ::DrawText(hdc, label, label.length(), &rc, DT_SINGLELINE | DT_VCENTER);
393 }
21c6080d
JS
394}
395
eba99da4
JS
396void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
397{
eba99da4
JS
398 RECT rc;
399 ::GetClientRect(GetHwnd(), &rc);
400
c3732409 401 // draw the entire box in a memory DC
eba99da4
JS
402 wxMemoryDC memdc;
403 wxBitmap bitmap(rc.right, rc.bottom);
404 memdc.SelectObject(bitmap);
405
3b6e5fb3 406 PaintBackground(memdc, rc);
21c6080d 407 PaintForeground(memdc, rc);
c3732409
VZ
408
409 // now only blit the static box border itself, not the interior, to avoid
410 // flicker when background is drawn below
411 //
412 // note that it seems to be faster to do 4 small blits here and then paint
413 // directly into wxPaintDC than painting background in wxMemoryDC and then
414 // blitting everything at once to wxPaintDC, this is why we do it like this
415 wxPaintDC dc(this);
3b6e5fb3
VZ
416 int borderTop, border;
417 GetBordersForSizer(&borderTop, &border);
418
eba99da4 419 // top
3b6e5fb3
VZ
420 dc.Blit(border, 0, rc.right - border, borderTop,
421 &memdc, border, 0);
eba99da4 422 // bottom
3b6e5fb3
VZ
423 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
424 &memdc, border, rc.bottom - border);
eba99da4 425 // left
3b6e5fb3
VZ
426 dc.Blit(0, 0, border, rc.bottom,
427 &memdc, 0, 0);
eba99da4 428 // right
3b6e5fb3
VZ
429 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
430 &memdc, rc.right - border, 0);
eba99da4 431
c3732409
VZ
432
433 // create the region excluding box children
3b6e5fb3
VZ
434 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
435 RECT rcWin;
436 ::GetWindowRect(GetHwnd(), &rcWin);
437 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
eba99da4 438
c3732409 439 // and also the box itself
3b6e5fb3 440 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
c3732409 441 HDCClipper clipToBg(GetHdcOf(dc), hrgn);
3b6e5fb3 442
c3732409 443 // paint the inside of the box (excluding box itself and child controls)
3b6e5fb3 444 PaintBackground(dc, rc);
eba99da4
JS
445}
446
c3732409
VZ
447#endif // !__WXWINCE__
448
1e6feb95 449#endif // wxUSE_STATBOX