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