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