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