]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbox.cpp
proper braces avoiding potential dangling else resulting from expansion
[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"
18717b27 36 #include "wx/sizer.h"
2bda0e17
KB
37#endif
38
3ad70d31 39#include "wx/notebook.h"
9dabade2 40#include "wx/sysopt.h"
2bda0e17 41
72a55896 42#include "wx/msw/uxtheme.h"
3f2711d5 43#include "wx/msw/private.h"
7212d155 44#include "wx/msw/missing.h"
74052fe8 45#include "wx/msw/dc.h"
3f2711d5 46
95fab76b
VZ
47// the values coincide with those in tmschema.h
48#define BP_GROUPBOX 4
49
50#define GBS_NORMAL 1
51
52#define TMT_FONT 210
53
3f2711d5
VZ
54// ----------------------------------------------------------------------------
55// wxWin macros
56// ----------------------------------------------------------------------------
57
3f2711d5
VZ
58// ============================================================================
59// implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// wxStaticBox
64// ----------------------------------------------------------------------------
65
66bool wxStaticBox::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxString& label,
69 const wxPoint& pos,
70 const wxSize& size,
71 long style,
72 const wxString& name)
2bda0e17 73{
11b6a93b 74 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
57f4f925 75 return false;
2bda0e17 76
e31e6ea7 77 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
57f4f925 78 return false;
2bda0e17 79
cf072997
VZ
80 // Always use LTR layout. Otherwise, the label would be mirrored.
81 SetLayoutDirection(wxLayout_LeftToRight);
82
9f769708 83#ifndef __WXWINCE__
5bf4788a
JS
84 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
85 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
c3732409 86#endif // !__WXWINCE__
3b6e5fb3 87
57f4f925 88 return true;
2bda0e17
KB
89}
90
e31e6ea7
VZ
91WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
92{
93 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
94
eba99da4
JS
95 // no need for it anymore, must be removed for wxRadioBox child
96 // buttons to be able to repaint themselves
97 styleWin &= ~WS_CLIPCHILDREN;
98
e31e6ea7 99 if ( exstyle )
5bf4788a 100 {
a92e7f30 101#ifndef __WXWINCE__
5bf4788a
JS
102 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
103 *exstyle = WS_EX_TRANSPARENT;
104 else
a92e7f30 105#endif
5bf4788a
JS
106 *exstyle = 0;
107 }
a83b5b74 108
cf072997
VZ
109 styleWin |= BS_GROUPBOX;
110
111 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
112 {
113 // Make sure label is on the right
114 styleWin |= BS_RIGHT;
115 }
116
117 return styleWin;
e31e6ea7
VZ
118}
119
f68586e5 120wxSize wxStaticBox::DoGetBestSize() const
2bda0e17 121{
d18eb918
RD
122 wxSize best;
123
124 // Calculate the size needed by the label
4438caf4 125 int cx, cy;
7a5e53ab 126 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
2bda0e17 127
4438caf4 128 int wBox;
32cd189d 129 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd)), &wBox, &cy);
2bda0e17 130
4438caf4
VZ
131 wBox += 3*cx;
132 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
2bda0e17 133
d18eb918
RD
134 // If there is a sizer then the base best size is the sizer's minimum
135 if (GetSizer() != NULL)
136 {
137 wxSize cm(GetSizer()->CalcMin());
138 best = ClientToWindowSize(cm);
139 // adjust for a long label if needed
140 best.x = wxMax(best.x, wBox);
141 }
142 // otherwise the best size falls back to the label size
143 else
144 {
145 best = wxSize(wBox, hBox);
146 }
31582e4e 147 return best;
4438caf4 148}
2bda0e17 149
c3732409 150void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
6aa01033 151{
c3732409
VZ
152 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
153
154 // need extra space, don't know how much but this seems to be enough
155 *borderTop += GetCharHeight()/3;
156}
157
158// all the hacks below are not necessary for WinCE
abf912c5 159#ifndef __WXWINCE__
c3732409
VZ
160
161WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
162{
58385af0 163 if ( nMsg == WM_NCHITTEST )
6aa01033 164 {
58385af0
VZ
165 // This code breaks some other processing such as enter/leave tracking
166 // so it's off by default.
167
168 static int s_useHTClient = -1;
169 if (s_useHTClient == -1)
170 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
171 if (s_useHTClient == 1)
172 {
c3732409
VZ
173 int xPos = GET_X_LPARAM(lParam);
174 int yPos = GET_Y_LPARAM(lParam);
58385af0
VZ
175
176 ScreenToClient(&xPos, &yPos);
177
178 // Make sure you can drag by the top of the groupbox, but let
179 // other (enclosed) controls get mouse events also
180 if ( yPos < 10 )
181 return (long)HTCLIENT;
182 }
6aa01033
JS
183 }
184
9705fbe9
VZ
185 if ( nMsg == WM_PRINTCLIENT )
186 {
187 // we have to process WM_PRINTCLIENT ourselves as otherwise child
188 // windows' background (eg buttons in radio box) would never be drawn
189 // unless we have a parent with non default background
190
191 // so check first if we have one
192 if ( !HandlePrintClient((WXHDC)wParam) )
193 {
194 // no, we don't, erase the background ourselves
195 // (don't use our own) - see PaintBackground for explanation
196 wxBrush brush(GetParent()->GetBackgroundColour());
197 wxFillRect(GetHwnd(), (HDC)wParam, GetHbrushOf(brush));
198 }
199
200 return 0;
201 }
202
2af006eb
VZ
203 if ( nMsg == WM_UPDATEUISTATE )
204 {
205 // DefWindowProc() redraws just the static box text when it gets this
206 // message and it does it using the standard (blue in standard theme)
207 // colour and not our own label colour that we use in PaintForeground()
208 // resulting in the label mysteriously changing the colour when e.g.
209 // "Alt" is pressed anywhere in the window, see #12497.
210 //
211 // To avoid this we simply refresh the window forcing our own code
212 // redrawing the label in the correct colour to be called. This is
213 // inefficient but there doesn't seem to be anything else we can do.
214 //
215 // Notice that the problem is XP-specific and doesn't arise under later
216 // systems.
217 if ( m_hasFgCol && wxGetWinVersion() == wxWinVersion_XP )
218 Refresh();
219 }
220
6aa01033
JS
221 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
222}
223
c3732409
VZ
224// ----------------------------------------------------------------------------
225// static box drawing
226// ----------------------------------------------------------------------------
58385af0 227
c3732409
VZ
228/*
229 We draw the static box ourselves because it's the only way to prevent it
230 from flickering horribly on resize (because everything inside the box is
231 erased twice: once when the box itself is repainted and second time when
232 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
233 we used to do and which resulted in other problems.
234 */
58385af0 235
3b6e5fb3
VZ
236// MSWGetRegionWithoutSelf helper: removes the given rectangle from region
237static inline void
238SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
239{
240 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
241 if ( !hrgnRect )
242 {
9a83f860 243 wxLogLastError(wxT("CreateRectRgn()"));
3b6e5fb3
VZ
244 return;
245 }
246
247 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
248}
249
250void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
eba99da4 251{
3b6e5fb3
VZ
252 HRGN hrgn = (HRGN)hRgn;
253
254 // remove the area occupied by the static box borders from the region
255 int borderTop, border;
256 GetBordersForSizer(&borderTop, &border);
eba99da4
JS
257
258 // top
3b6e5fb3 259 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
eba99da4
JS
260
261 // bottom
3b6e5fb3 262 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
eba99da4
JS
263
264 // left
3b6e5fb3 265 SubtractRectFromRgn(hrgn, 0, 0, border, h);
eba99da4
JS
266
267 // right
3b6e5fb3 268 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
eba99da4
JS
269}
270
3b6e5fb3 271WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
eba99da4
JS
272{
273 RECT rc;
274 ::GetWindowRect(GetHwnd(), &rc);
275 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
cec46079 276 bool foundThis = false;
3b6e5fb3
VZ
277
278 // iterate over all child windows (not just wxWindows but all windows)
279 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
280 child;
281 child = ::GetWindow(child, GW_HWNDNEXT) )
eba99da4 282 {
4249e334
RD
283 if ( ! ::IsWindowVisible(child) )
284 {
285 // if the window isn't visible then it doesn't need clipped
286 continue;
287 }
9705fbe9 288
4249e334
RD
289 LONG style = ::GetWindowLong(child, GWL_STYLE);
290 wxString str(wxGetWindowClass(child));
291 str.UpperCase();
5c17f6d3 292 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
eba99da4 293 {
cec46079
JS
294 if ( child == GetHwnd() )
295 foundThis = true;
296
297 // Any static boxes below this one in the Z-order can't be clipped
298 // since if we have the case where a static box with a low Z-order
299 // is nested inside another static box with a high Z-order then the
300 // nested static box would be painted over. Doing it this way
301 // unfortunately results in flicker if the Z-order of nested static
302 // boxes is not inside (lowest) to outside (highest) but at least
303 // they are still shown.
304 if ( foundThis )
305 continue;
4249e334 306 }
9705fbe9 307
4249e334
RD
308 ::GetWindowRect(child, &rc);
309 if ( ::RectInRegion(hrgn, &rc) )
310 {
311 // need to remove WS_CLIPSIBLINGS from all sibling windows
312 // that are within this staticbox if set
313 if ( style & WS_CLIPSIBLINGS )
eba99da4 314 {
4249e334
RD
315 style &= ~WS_CLIPSIBLINGS;
316 ::SetWindowLong(child, GWL_STYLE, style);
9705fbe9 317
4249e334
RD
318 // MSDN: "If you have changed certain window data using
319 // SetWindowLong, you must call SetWindowPos to have the
320 // changes take effect."
321 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
322 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
323 SWP_FRAMECHANGED);
eba99da4 324 }
4249e334
RD
325
326 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
327 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
eba99da4 328 }
eba99da4 329 }
eba99da4 330
3b6e5fb3
VZ
331 return (WXHRGN)hrgn;
332}
333
c3732409
VZ
334// helper for OnPaint(): really erase the background, i.e. do it even if we
335// don't have any non default brush for doing it (DoEraseBackground() doesn't
336// do anything in such case)
3b6e5fb3
VZ
337void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
338{
c3732409
VZ
339 // note that we do not use the box background colour here, it shouldn't
340 // apply to its interior for several reasons:
341 // 1. wxGTK doesn't do it
342 // 2. controls inside the box don't get correct bg colour because they
343 // are not our children so we'd have some really ugly colour mix if
344 // we did it
345 // 3. this is backwards compatible behaviour and some people rely on it,
346 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
888dde65 347 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
ebfee179 348 HBRUSH hbr = MSWGetBgBrush(impl->GetHDC());
c3732409
VZ
349
350 // if there is no special brush for painting this control, just use the
351 // solid background colour
352 wxBrush brush;
3b6e5fb3
VZ
353 if ( !hbr )
354 {
ebfee179 355 brush = wxBrush(GetParent()->GetBackgroundColour());
c3732409 356 hbr = GetHbrushOf(brush);
3b6e5fb3
VZ
357 }
358
888dde65 359 ::FillRect(GetHdcOf(*impl), &rc, hbr);
eba99da4
JS
360}
361
cf072997 362void wxStaticBox::PaintForeground(wxDC& dc, const RECT& rc)
21c6080d 363{
888dde65
RR
364 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
365 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(*impl), 0);
72a55896
VZ
366
367 // when using XP themes, neither setting the text colour nor transparent
368 // background mode doesn't change anything: the static box def window proc
369 // still draws the label in its own colours, so we need to redraw the text
370 // ourselves if we have a non default fg colour
95fab76b 371 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
72a55896
VZ
372 {
373 // draw over the text in default colour in our colour
888dde65 374 HDC hdc = GetHdcOf(*impl);
95fab76b 375 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
cf072997
VZ
376
377 const bool rtl = wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft;
378 if ( rtl )
379 ::SetTextAlign(hdc, TA_RTLREADING | TA_RIGHT);
380
381 // Get dimensions of the label
24e35cf8 382 const wxString label = GetLabel();
24e35cf8 383
95fab76b
VZ
384 // choose the correct font
385 AutoHFONT font;
386 SelectInHDC selFont;
387 if ( m_hasFont )
388 {
389 selFont.Init(hdc, GetHfontOf(GetFont()));
390 }
391 else // no font set, use the one set by the theme
392 {
393 wxUxThemeHandle hTheme(this, L"BUTTON");
394 if ( hTheme )
395 {
3120eccf 396 wxUxThemeFont themeFont;
95fab76b
VZ
397 if ( wxUxThemeEngine::Get()->GetThemeFont
398 (
399 hTheme,
400 hdc,
401 BP_GROUPBOX,
402 GBS_NORMAL,
403 TMT_FONT,
3120eccf 404 themeFont.GetPtr()
95fab76b
VZ
405 ) == S_OK )
406 {
3120eccf 407 font.Init(themeFont.GetLOGFONT());
95fab76b
VZ
408 if ( font )
409 selFont.Init(hdc, font);
410 }
411 }
412 }
413
441dfa1d
VZ
414 // Get the font extent
415 int width, height;
416 dc.GetTextExtent(wxStripMenuCodes(label, wxStrip_Mnemonics),
417 &width, &height);
418
419 int x;
420 int y = height;
421
422 // first we need to correctly paint the background of the label
423 // as Windows ignores the brush offset when doing it
424 //
425 // FIXME: value of x is hardcoded as this is what it is on my system,
426 // no idea if it's true everywhere
427 RECT dimensions = {0, 0, 0, y};
428 if ( !rtl )
429 {
430 x = 9;
431 dimensions.left = x;
432 dimensions.right = x + width;
433 }
434 else
435 {
436 x = rc.right - 7;
437 dimensions.left = x - width;
438 dimensions.right = x;
439 }
440
441 // need to adjust the rectangle to cover all the label background
442 dimensions.left -= 2;
443 dimensions.right += 2;
444 dimensions.bottom += 2;
445
b8ec69e8
VZ
446 if ( UseBgCol() )
447 {
448 // our own background colour should be used for the background of
449 // the label: this is consistent with the behaviour under pre-XP
450 // systems (i.e. without visual themes) and generally makes sense
451 wxBrush brush = wxBrush(GetBackgroundColour());
888dde65
RR
452 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
453 ::FillRect(GetHdcOf(*impl), &dimensions, GetHbrushOf(brush));
b8ec69e8
VZ
454 }
455 else // paint parent background
456 {
457 PaintBackground(dc, dimensions);
458 }
441dfa1d 459
b4607e89
VZ
460 UINT drawTextFlags = DT_SINGLELINE | DT_VCENTER;
461
462 // determine the state of UI queues to draw the text correctly under XP
463 // and later systems
464 static const bool isXPorLater = wxGetWinVersion() >= wxWinVersion_XP;
465 if ( isXPorLater )
466 {
467 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE, 0, 0) &
468 UISF_HIDEACCEL )
469 {
470 drawTextFlags |= DT_HIDEPREFIX;
471 }
472 }
473
24e35cf8 474 // now draw the text
cf072997
VZ
475 if ( !rtl )
476 {
ffabb347 477 RECT rc2 = { x, 0, x + width, y };
017dc06b 478 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
b4607e89 479 drawTextFlags);
cf072997
VZ
480 }
481 else // RTL
482 {
483 RECT rc2 = { x, 0, x - width, y };
017dc06b 484 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
b4607e89 485 drawTextFlags | DT_RTLREADING);
cf072997 486 }
72a55896 487 }
21c6080d
JS
488}
489
eba99da4
JS
490void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
491{
eba99da4
JS
492 RECT rc;
493 ::GetClientRect(GetHwnd(), &rc);
494
c3732409 495 // draw the entire box in a memory DC
eba99da4
JS
496 wxMemoryDC memdc;
497 wxBitmap bitmap(rc.right, rc.bottom);
498 memdc.SelectObject(bitmap);
499
3b6e5fb3 500 PaintBackground(memdc, rc);
21c6080d 501 PaintForeground(memdc, rc);
c3732409
VZ
502
503 // now only blit the static box border itself, not the interior, to avoid
504 // flicker when background is drawn below
505 //
506 // note that it seems to be faster to do 4 small blits here and then paint
507 // directly into wxPaintDC than painting background in wxMemoryDC and then
508 // blitting everything at once to wxPaintDC, this is why we do it like this
509 wxPaintDC dc(this);
3b6e5fb3
VZ
510 int borderTop, border;
511 GetBordersForSizer(&borderTop, &border);
512
eba99da4 513 // top
3b6e5fb3
VZ
514 dc.Blit(border, 0, rc.right - border, borderTop,
515 &memdc, border, 0);
eba99da4 516 // bottom
1d1b4862 517 dc.Blit(border, rc.bottom - border, rc.right - border, border,
3b6e5fb3 518 &memdc, border, rc.bottom - border);
eba99da4 519 // left
3b6e5fb3
VZ
520 dc.Blit(0, 0, border, rc.bottom,
521 &memdc, 0, 0);
1d1b4862
VZ
522 // right (note that upper and bottom right corners were already part of the
523 // first two blits so we shouldn't overwrite them here to avoi flicker)
524 dc.Blit(rc.right - border, borderTop,
525 border, rc.bottom - borderTop - border,
526 &memdc, rc.right - border, borderTop);
eba99da4 527
c3732409
VZ
528
529 // create the region excluding box children
3b6e5fb3
VZ
530 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
531 RECT rcWin;
532 ::GetWindowRect(GetHwnd(), &rcWin);
533 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
eba99da4 534
c3732409 535 // and also the box itself
3b6e5fb3 536 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
888dde65
RR
537 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
538 HDCClipper clipToBg(GetHdcOf(*impl), hrgn);
3b6e5fb3 539
c3732409 540 // paint the inside of the box (excluding box itself and child controls)
3b6e5fb3 541 PaintBackground(dc, rc);
eba99da4
JS
542}
543
c3732409
VZ
544#endif // !__WXWINCE__
545
5ad24e6d
RD
546
547wxPoint wxStaticBox::GetClientAreaOrigin() const
548{
549 // See: http://msdn.microsoft.com/en-us/library/aa511279.aspx
550 wxPoint pt = ConvertDialogToPixels(wxPoint(6,11));
551 return pt;
552}
553
554
555void wxStaticBox::DoGetClientSize(int *width, int *height) const
556{
557 // See: http://msdn.microsoft.com/en-us/library/aa511279.aspx
558 wxPoint lr = ConvertDialogToPixels(wxPoint(6,7));
559 wxPoint ul = GetClientAreaOrigin();
560 wxSize sz = GetSize();
561
562 if (width)
563 *width = sz.x - ul.x - lr.x;
564 if (height)
565 *height = sz.y - ul.y - lr.x;
566}
567
1e6feb95 568#endif // wxUSE_STATBOX