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