Refresh wxMSW wxStaticBitmap when its size changes.
[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 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STATBOX
28
29 #include "wx/statbox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcmemory.h"
35 #include "wx/image.h"
36 #include "wx/sizer.h"
37 #endif
38
39 #include "wx/notebook.h"
40 #include "wx/sysopt.h"
41
42 #include "wx/msw/uxtheme.h"
43 #include "wx/msw/private.h"
44 #include "wx/msw/missing.h"
45 #include "wx/msw/dc.h"
46
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
54 // ----------------------------------------------------------------------------
55 // wxWin macros
56 // ----------------------------------------------------------------------------
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // wxStaticBox
64 // ----------------------------------------------------------------------------
65
66 bool 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)
73 {
74 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
75 return false;
76
77 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
78 return false;
79
80 // Always use LTR layout. Otherwise, the label would be mirrored.
81 SetLayoutDirection(wxLayout_LeftToRight);
82
83 #ifndef __WXWINCE__
84 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
85 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
86 #endif // !__WXWINCE__
87
88 return true;
89 }
90
91 WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
92 {
93 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
94
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
99 if ( exstyle )
100 {
101 #ifndef __WXWINCE__
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
108 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
109 *exstyle |= WS_EX_TRANSPARENT;
110 #endif
111 }
112
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;
122 }
123
124 wxSize wxStaticBox::DoGetBestSize() const
125 {
126 wxSize best;
127
128 // Calculate the size needed by the label
129 int cx, cy;
130 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
131
132 int wBox;
133 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd)), &wBox, &cy);
134
135 wBox += 3*cx;
136 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
137
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 }
151 return best;
152 }
153
154 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
155 {
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
163 #ifndef __WXWINCE__
164
165 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
166 {
167 if ( nMsg == WM_NCHITTEST )
168 {
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 {
177 int xPos = GET_X_LPARAM(lParam);
178 int yPos = GET_Y_LPARAM(lParam);
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 }
187 }
188
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
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
225 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
226 }
227
228 // ----------------------------------------------------------------------------
229 // static box drawing
230 // ----------------------------------------------------------------------------
231
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 */
239
240 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
241 static inline void
242 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
243 {
244 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
245 if ( !hrgnRect )
246 {
247 wxLogLastError(wxT("CreateRectRgn()"));
248 return;
249 }
250
251 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
252 }
253
254 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
255 {
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);
261
262 // top
263 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
264
265 // bottom
266 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
267
268 // left
269 SubtractRectFromRgn(hrgn, 0, 0, border, h);
270
271 // right
272 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
273 }
274
275 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
276 {
277 RECT rc;
278 ::GetWindowRect(GetHwnd(), &rc);
279 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
280 bool foundThis = false;
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) )
286 {
287 if ( ! ::IsWindowVisible(child) )
288 {
289 // if the window isn't visible then it doesn't need clipped
290 continue;
291 }
292
293 LONG style = ::GetWindowLong(child, GWL_STYLE);
294 wxString str(wxGetWindowClass(child));
295 str.UpperCase();
296 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
297 {
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;
310 }
311
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 )
318 {
319 style &= ~WS_CLIPSIBLINGS;
320 ::SetWindowLong(child, GWL_STYLE, style);
321
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);
328 }
329
330 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
331 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
332 }
333 }
334
335 return (WXHRGN)hrgn;
336 }
337
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)
341 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
342 {
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
351 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
352 HBRUSH hbr = MSWGetBgBrush(impl->GetHDC());
353
354 // if there is no special brush for painting this control, just use the
355 // solid background colour
356 wxBrush brush;
357 if ( !hbr )
358 {
359 brush = wxBrush(GetParent()->GetBackgroundColour());
360 hbr = GetHbrushOf(brush);
361 }
362
363 ::FillRect(GetHdcOf(*impl), &rc, hbr);
364 }
365
366 void wxStaticBox::PaintForeground(wxDC& dc, const RECT& rc)
367 {
368 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
369 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(*impl), 0);
370
371 #if wxUSE_UXTHEME
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
376 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
377 {
378 // draw over the text in default colour in our colour
379 HDC hdc = GetHdcOf(*impl);
380 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
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
387 const wxString label = GetLabel();
388
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 {
401 wxUxThemeFont themeFont;
402 if ( wxUxThemeEngine::Get()->GetThemeFont
403 (
404 hTheme,
405 hdc,
406 BP_GROUPBOX,
407 GBS_NORMAL,
408 TMT_FONT,
409 themeFont.GetPtr()
410 ) == S_OK )
411 {
412 font.Init(themeFont.GetLOGFONT());
413 if ( font )
414 selFont.Init(hdc, font);
415 }
416 }
417 }
418
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
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());
457 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
458 ::FillRect(GetHdcOf(*impl), &dimensions, GetHbrushOf(brush));
459 }
460 else // paint parent background
461 {
462 PaintBackground(dc, dimensions);
463 }
464
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
479 // now draw the text
480 if ( !rtl )
481 {
482 RECT rc2 = { x, 0, x + width, y };
483 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
484 drawTextFlags);
485 }
486 else // RTL
487 {
488 RECT rc2 = { x, 0, x - width, y };
489 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
490 drawTextFlags | DT_RTLREADING);
491 }
492 }
493 #endif // wxUSE_UXTHEME
494 }
495
496 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
497 {
498 RECT rc;
499 ::GetClientRect(GetHwnd(), &rc);
500
501 // draw the entire box in a memory DC
502 wxMemoryDC memdc;
503 wxBitmap bitmap(rc.right, rc.bottom);
504 memdc.SelectObject(bitmap);
505
506 PaintBackground(memdc, rc);
507 PaintForeground(memdc, rc);
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);
516 int borderTop, border;
517 GetBordersForSizer(&borderTop, &border);
518
519 // top
520 dc.Blit(border, 0, rc.right - border, borderTop,
521 &memdc, border, 0);
522 // bottom
523 dc.Blit(border, rc.bottom - border, rc.right - border, border,
524 &memdc, border, rc.bottom - border);
525 // left
526 dc.Blit(0, 0, border, rc.bottom,
527 &memdc, 0, 0);
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);
533
534
535 // create the region excluding box children
536 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
537 RECT rcWin;
538 ::GetWindowRect(GetHwnd(), &rcWin);
539 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
540
541 // and also the box itself
542 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
543 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
544 HDCClipper clipToBg(GetHdcOf(*impl), hrgn);
545
546 // paint the inside of the box (excluding box itself and child controls)
547 PaintBackground(dc, rc);
548 }
549
550 #endif // !__WXWINCE__
551
552 #endif // wxUSE_STATBOX