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