]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbox.cpp
Make the modal print preview window behave like a modal dialog.
[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 366
ed72e195 367#if wxUSE_UXTHEME
72a55896
VZ
368 // when using XP themes, neither setting the text colour nor transparent
369 // background mode doesn't change anything: the static box def window proc
370 // still draws the label in its own colours, so we need to redraw the text
371 // ourselves if we have a non default fg colour
95fab76b 372 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
72a55896
VZ
373 {
374 // draw over the text in default colour in our colour
888dde65 375 HDC hdc = GetHdcOf(*impl);
95fab76b 376 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
cf072997
VZ
377
378 const bool rtl = wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft;
379 if ( rtl )
380 ::SetTextAlign(hdc, TA_RTLREADING | TA_RIGHT);
381
382 // Get dimensions of the label
24e35cf8 383 const wxString label = GetLabel();
24e35cf8 384
95fab76b
VZ
385 // choose the correct font
386 AutoHFONT font;
387 SelectInHDC selFont;
388 if ( m_hasFont )
389 {
390 selFont.Init(hdc, GetHfontOf(GetFont()));
391 }
392 else // no font set, use the one set by the theme
393 {
394 wxUxThemeHandle hTheme(this, L"BUTTON");
395 if ( hTheme )
396 {
3120eccf 397 wxUxThemeFont themeFont;
95fab76b
VZ
398 if ( wxUxThemeEngine::Get()->GetThemeFont
399 (
400 hTheme,
401 hdc,
402 BP_GROUPBOX,
403 GBS_NORMAL,
404 TMT_FONT,
3120eccf 405 themeFont.GetPtr()
95fab76b
VZ
406 ) == S_OK )
407 {
3120eccf 408 font.Init(themeFont.GetLOGFONT());
95fab76b
VZ
409 if ( font )
410 selFont.Init(hdc, font);
411 }
412 }
413 }
414
441dfa1d
VZ
415 // Get the font extent
416 int width, height;
417 dc.GetTextExtent(wxStripMenuCodes(label, wxStrip_Mnemonics),
418 &width, &height);
419
420 int x;
421 int y = height;
422
423 // first we need to correctly paint the background of the label
424 // as Windows ignores the brush offset when doing it
425 //
426 // FIXME: value of x is hardcoded as this is what it is on my system,
427 // no idea if it's true everywhere
428 RECT dimensions = {0, 0, 0, y};
429 if ( !rtl )
430 {
431 x = 9;
432 dimensions.left = x;
433 dimensions.right = x + width;
434 }
435 else
436 {
437 x = rc.right - 7;
438 dimensions.left = x - width;
439 dimensions.right = x;
440 }
441
442 // need to adjust the rectangle to cover all the label background
443 dimensions.left -= 2;
444 dimensions.right += 2;
445 dimensions.bottom += 2;
446
b8ec69e8
VZ
447 if ( UseBgCol() )
448 {
449 // our own background colour should be used for the background of
450 // the label: this is consistent with the behaviour under pre-XP
451 // systems (i.e. without visual themes) and generally makes sense
452 wxBrush brush = wxBrush(GetBackgroundColour());
888dde65
RR
453 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
454 ::FillRect(GetHdcOf(*impl), &dimensions, GetHbrushOf(brush));
b8ec69e8
VZ
455 }
456 else // paint parent background
457 {
458 PaintBackground(dc, dimensions);
459 }
441dfa1d 460
b4607e89
VZ
461 UINT drawTextFlags = DT_SINGLELINE | DT_VCENTER;
462
463 // determine the state of UI queues to draw the text correctly under XP
464 // and later systems
465 static const bool isXPorLater = wxGetWinVersion() >= wxWinVersion_XP;
466 if ( isXPorLater )
467 {
468 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE, 0, 0) &
469 UISF_HIDEACCEL )
470 {
471 drawTextFlags |= DT_HIDEPREFIX;
472 }
473 }
474
24e35cf8 475 // now draw the text
cf072997
VZ
476 if ( !rtl )
477 {
ffabb347 478 RECT rc2 = { x, 0, x + width, y };
017dc06b 479 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
b4607e89 480 drawTextFlags);
cf072997
VZ
481 }
482 else // RTL
483 {
484 RECT rc2 = { x, 0, x - width, y };
017dc06b 485 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
b4607e89 486 drawTextFlags | DT_RTLREADING);
cf072997 487 }
72a55896 488 }
ed72e195 489#endif // wxUSE_UXTHEME
21c6080d
JS
490}
491
eba99da4
JS
492void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
493{
eba99da4
JS
494 RECT rc;
495 ::GetClientRect(GetHwnd(), &rc);
496
c3732409 497 // draw the entire box in a memory DC
eba99da4
JS
498 wxMemoryDC memdc;
499 wxBitmap bitmap(rc.right, rc.bottom);
500 memdc.SelectObject(bitmap);
501
3b6e5fb3 502 PaintBackground(memdc, rc);
21c6080d 503 PaintForeground(memdc, rc);
c3732409
VZ
504
505 // now only blit the static box border itself, not the interior, to avoid
506 // flicker when background is drawn below
507 //
508 // note that it seems to be faster to do 4 small blits here and then paint
509 // directly into wxPaintDC than painting background in wxMemoryDC and then
510 // blitting everything at once to wxPaintDC, this is why we do it like this
511 wxPaintDC dc(this);
3b6e5fb3
VZ
512 int borderTop, border;
513 GetBordersForSizer(&borderTop, &border);
514
eba99da4 515 // top
3b6e5fb3
VZ
516 dc.Blit(border, 0, rc.right - border, borderTop,
517 &memdc, border, 0);
eba99da4 518 // bottom
1d1b4862 519 dc.Blit(border, rc.bottom - border, rc.right - border, border,
3b6e5fb3 520 &memdc, border, rc.bottom - border);
eba99da4 521 // left
3b6e5fb3
VZ
522 dc.Blit(0, 0, border, rc.bottom,
523 &memdc, 0, 0);
1d1b4862
VZ
524 // right (note that upper and bottom right corners were already part of the
525 // first two blits so we shouldn't overwrite them here to avoi flicker)
526 dc.Blit(rc.right - border, borderTop,
527 border, rc.bottom - borderTop - border,
528 &memdc, rc.right - border, borderTop);
eba99da4 529
c3732409
VZ
530
531 // create the region excluding box children
3b6e5fb3
VZ
532 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
533 RECT rcWin;
534 ::GetWindowRect(GetHwnd(), &rcWin);
535 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
eba99da4 536
c3732409 537 // and also the box itself
3b6e5fb3 538 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
888dde65
RR
539 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
540 HDCClipper clipToBg(GetHdcOf(*impl), hrgn);
3b6e5fb3 541
c3732409 542 // paint the inside of the box (excluding box itself and child controls)
3b6e5fb3 543 PaintBackground(dc, rc);
eba99da4
JS
544}
545
c3732409
VZ
546#endif // !__WXWINCE__
547
1e6feb95 548#endif // wxUSE_STATBOX