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