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