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