]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
Simplify and rationalize wxGDIPlusContext class constructors.
[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 int cx, cy;
122 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
123
124 int wBox;
125 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd)), &wBox, &cy);
126
127 wBox += 3*cx;
128 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
129
130 wxSize best(wBox, hBox);
131 CacheBestSize(best);
132 return best;
133 }
134
135 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
136 {
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
144 #ifndef __WXWINCE__
145
146 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
147 {
148 if ( nMsg == WM_NCHITTEST )
149 {
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 {
158 int xPos = GET_X_LPARAM(lParam);
159 int yPos = GET_Y_LPARAM(lParam);
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 }
168 }
169
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
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
206 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
207 }
208
209 // ----------------------------------------------------------------------------
210 // static box drawing
211 // ----------------------------------------------------------------------------
212
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 */
220
221 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
222 static inline void
223 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
224 {
225 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
226 if ( !hrgnRect )
227 {
228 wxLogLastError(wxT("CreateRectRgn()"));
229 return;
230 }
231
232 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
233 }
234
235 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
236 {
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);
242
243 // top
244 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
245
246 // bottom
247 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
248
249 // left
250 SubtractRectFromRgn(hrgn, 0, 0, border, h);
251
252 // right
253 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
254 }
255
256 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
257 {
258 RECT rc;
259 ::GetWindowRect(GetHwnd(), &rc);
260 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
261 bool foundThis = false;
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) )
267 {
268 if ( ! ::IsWindowVisible(child) )
269 {
270 // if the window isn't visible then it doesn't need clipped
271 continue;
272 }
273
274 LONG style = ::GetWindowLong(child, GWL_STYLE);
275 wxString str(wxGetWindowClass(child));
276 str.UpperCase();
277 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
278 {
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;
291 }
292
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 )
299 {
300 style &= ~WS_CLIPSIBLINGS;
301 ::SetWindowLong(child, GWL_STYLE, style);
302
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);
309 }
310
311 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
312 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
313 }
314 }
315
316 return (WXHRGN)hrgn;
317 }
318
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)
322 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
323 {
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
332 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
333 HBRUSH hbr = MSWGetBgBrush(impl->GetHDC());
334
335 // if there is no special brush for painting this control, just use the
336 // solid background colour
337 wxBrush brush;
338 if ( !hbr )
339 {
340 brush = wxBrush(GetParent()->GetBackgroundColour());
341 hbr = GetHbrushOf(brush);
342 }
343
344 ::FillRect(GetHdcOf(*impl), &rc, hbr);
345 }
346
347 void wxStaticBox::PaintForeground(wxDC& dc, const RECT& rc)
348 {
349 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
350 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(*impl), 0);
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
356 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
357 {
358 // draw over the text in default colour in our colour
359 HDC hdc = GetHdcOf(*impl);
360 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
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
367 const wxString label = GetLabel();
368
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 {
381 wxUxThemeFont themeFont;
382 if ( wxUxThemeEngine::Get()->GetThemeFont
383 (
384 hTheme,
385 hdc,
386 BP_GROUPBOX,
387 GBS_NORMAL,
388 TMT_FONT,
389 themeFont.GetPtr()
390 ) == S_OK )
391 {
392 font.Init(themeFont.GetLOGFONT());
393 if ( font )
394 selFont.Init(hdc, font);
395 }
396 }
397 }
398
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
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());
437 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
438 ::FillRect(GetHdcOf(*impl), &dimensions, GetHbrushOf(brush));
439 }
440 else // paint parent background
441 {
442 PaintBackground(dc, dimensions);
443 }
444
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
459 // now draw the text
460 if ( !rtl )
461 {
462 RECT rc2 = { x, 0, x + width, y };
463 ::DrawText(hdc, label.wx_str(), label.length(), &rc2,
464 drawTextFlags);
465 }
466 else // RTL
467 {
468 RECT rc2 = { x, 0, x - width, y };
469 ::DrawText(hdc, label.wx_str(), label.length(), &rc2,
470 drawTextFlags | DT_RTLREADING);
471 }
472 }
473 }
474
475 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
476 {
477 RECT rc;
478 ::GetClientRect(GetHwnd(), &rc);
479
480 // draw the entire box in a memory DC
481 wxMemoryDC memdc;
482 wxBitmap bitmap(rc.right, rc.bottom);
483 memdc.SelectObject(bitmap);
484
485 PaintBackground(memdc, rc);
486 PaintForeground(memdc, rc);
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);
495 int borderTop, border;
496 GetBordersForSizer(&borderTop, &border);
497
498 // top
499 dc.Blit(border, 0, rc.right - border, borderTop,
500 &memdc, border, 0);
501 // bottom
502 dc.Blit(border, rc.bottom - border, rc.right - border, border,
503 &memdc, border, rc.bottom - border);
504 // left
505 dc.Blit(0, 0, border, rc.bottom,
506 &memdc, 0, 0);
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);
512
513
514 // create the region excluding box children
515 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
516 RECT rcWin;
517 ::GetWindowRect(GetHwnd(), &rcWin);
518 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
519
520 // and also the box itself
521 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
522 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
523 HDCClipper clipToBg(GetHdcOf(*impl), hrgn);
524
525 // paint the inside of the box (excluding box itself and child controls)
526 PaintBackground(dc, rc);
527 }
528
529 #endif // !__WXWINCE__
530
531 #endif // wxUSE_STATBOX