]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
wxHtmlHelpWindow: Fix contents panel syncing with view.
[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 #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 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
102 *exstyle = WS_EX_TRANSPARENT;
103 else
104 #endif
105 *exstyle = 0;
106 }
107
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;
117 }
118
119 wxSize wxStaticBox::DoGetBestSize() const
120 {
121 wxSize best;
122
123 // Calculate the size needed by the label
124 int cx, cy;
125 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
126
127 int wBox;
128 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd)), &wBox, &cy);
129
130 wBox += 3*cx;
131 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
132
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 }
146 return best;
147 }
148
149 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
150 {
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
158 #ifndef __WXWINCE__
159
160 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
161 {
162 if ( nMsg == WM_NCHITTEST )
163 {
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 {
172 int xPos = GET_X_LPARAM(lParam);
173 int yPos = GET_Y_LPARAM(lParam);
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 }
182 }
183
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
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
220 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
221 }
222
223 // ----------------------------------------------------------------------------
224 // static box drawing
225 // ----------------------------------------------------------------------------
226
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 */
234
235 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
236 static inline void
237 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
238 {
239 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
240 if ( !hrgnRect )
241 {
242 wxLogLastError(wxT("CreateRectRgn()"));
243 return;
244 }
245
246 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
247 }
248
249 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
250 {
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);
256
257 // top
258 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
259
260 // bottom
261 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
262
263 // left
264 SubtractRectFromRgn(hrgn, 0, 0, border, h);
265
266 // right
267 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
268 }
269
270 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
271 {
272 RECT rc;
273 ::GetWindowRect(GetHwnd(), &rc);
274 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
275 bool foundThis = false;
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) )
281 {
282 if ( ! ::IsWindowVisible(child) )
283 {
284 // if the window isn't visible then it doesn't need clipped
285 continue;
286 }
287
288 LONG style = ::GetWindowLong(child, GWL_STYLE);
289 wxString str(wxGetWindowClass(child));
290 str.UpperCase();
291 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
292 {
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;
305 }
306
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 )
313 {
314 style &= ~WS_CLIPSIBLINGS;
315 ::SetWindowLong(child, GWL_STYLE, style);
316
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);
323 }
324
325 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
326 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
327 }
328 }
329
330 return (WXHRGN)hrgn;
331 }
332
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)
336 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
337 {
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
346 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
347 HBRUSH hbr = MSWGetBgBrush(impl->GetHDC());
348
349 // if there is no special brush for painting this control, just use the
350 // solid background colour
351 wxBrush brush;
352 if ( !hbr )
353 {
354 brush = wxBrush(GetParent()->GetBackgroundColour());
355 hbr = GetHbrushOf(brush);
356 }
357
358 ::FillRect(GetHdcOf(*impl), &rc, hbr);
359 }
360
361 void wxStaticBox::PaintForeground(wxDC& dc, const RECT& rc)
362 {
363 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
364 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(*impl), 0);
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
370 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
371 {
372 // draw over the text in default colour in our colour
373 HDC hdc = GetHdcOf(*impl);
374 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
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
381 const wxString label = GetLabel();
382
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 {
395 wxUxThemeFont themeFont;
396 if ( wxUxThemeEngine::Get()->GetThemeFont
397 (
398 hTheme,
399 hdc,
400 BP_GROUPBOX,
401 GBS_NORMAL,
402 TMT_FONT,
403 themeFont.GetPtr()
404 ) == S_OK )
405 {
406 font.Init(themeFont.GetLOGFONT());
407 if ( font )
408 selFont.Init(hdc, font);
409 }
410 }
411 }
412
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
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());
451 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
452 ::FillRect(GetHdcOf(*impl), &dimensions, GetHbrushOf(brush));
453 }
454 else // paint parent background
455 {
456 PaintBackground(dc, dimensions);
457 }
458
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
473 // now draw the text
474 if ( !rtl )
475 {
476 RECT rc2 = { x, 0, x + width, y };
477 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
478 drawTextFlags);
479 }
480 else // RTL
481 {
482 RECT rc2 = { x, 0, x - width, y };
483 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
484 drawTextFlags | DT_RTLREADING);
485 }
486 }
487 }
488
489 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
490 {
491 RECT rc;
492 ::GetClientRect(GetHwnd(), &rc);
493
494 // draw the entire box in a memory DC
495 wxMemoryDC memdc;
496 wxBitmap bitmap(rc.right, rc.bottom);
497 memdc.SelectObject(bitmap);
498
499 PaintBackground(memdc, rc);
500 PaintForeground(memdc, rc);
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);
509 int borderTop, border;
510 GetBordersForSizer(&borderTop, &border);
511
512 // top
513 dc.Blit(border, 0, rc.right - border, borderTop,
514 &memdc, border, 0);
515 // bottom
516 dc.Blit(border, rc.bottom - border, rc.right - border, border,
517 &memdc, border, rc.bottom - border);
518 // left
519 dc.Blit(0, 0, border, rc.bottom,
520 &memdc, 0, 0);
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);
526
527
528 // create the region excluding box children
529 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
530 RECT rcWin;
531 ::GetWindowRect(GetHwnd(), &rcWin);
532 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
533
534 // and also the box itself
535 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
536 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
537 HDCClipper clipToBg(GetHdcOf(*impl), hrgn);
538
539 // paint the inside of the box (excluding box itself and child controls)
540 PaintBackground(dc, rc);
541 }
542
543 #endif // !__WXWINCE__
544
545
546 wxPoint 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
554 void 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
567 #endif // wxUSE_STATBOX