]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
Fix command line parsing in media player sample.
[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 // when using XP themes, neither setting the text colour nor transparent
368 // background mode doesn't change anything: the static box def window proc
369 // still draws the label in its own colours, so we need to redraw the text
370 // ourselves if we have a non default fg colour
371 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
372 {
373 // draw over the text in default colour in our colour
374 HDC hdc = GetHdcOf(*impl);
375 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
376
377 const bool rtl = wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft;
378 if ( rtl )
379 ::SetTextAlign(hdc, TA_RTLREADING | TA_RIGHT);
380
381 // Get dimensions of the label
382 const wxString label = GetLabel();
383
384 // choose the correct font
385 AutoHFONT font;
386 SelectInHDC selFont;
387 if ( m_hasFont )
388 {
389 selFont.Init(hdc, GetHfontOf(GetFont()));
390 }
391 else // no font set, use the one set by the theme
392 {
393 wxUxThemeHandle hTheme(this, L"BUTTON");
394 if ( hTheme )
395 {
396 wxUxThemeFont themeFont;
397 if ( wxUxThemeEngine::Get()->GetThemeFont
398 (
399 hTheme,
400 hdc,
401 BP_GROUPBOX,
402 GBS_NORMAL,
403 TMT_FONT,
404 themeFont.GetPtr()
405 ) == S_OK )
406 {
407 font.Init(themeFont.GetLOGFONT());
408 if ( font )
409 selFont.Init(hdc, font);
410 }
411 }
412 }
413
414 // Get the font extent
415 int width, height;
416 dc.GetTextExtent(wxStripMenuCodes(label, wxStrip_Mnemonics),
417 &width, &height);
418
419 int x;
420 int y = height;
421
422 // first we need to correctly paint the background of the label
423 // as Windows ignores the brush offset when doing it
424 //
425 // FIXME: value of x is hardcoded as this is what it is on my system,
426 // no idea if it's true everywhere
427 RECT dimensions = {0, 0, 0, y};
428 if ( !rtl )
429 {
430 x = 9;
431 dimensions.left = x;
432 dimensions.right = x + width;
433 }
434 else
435 {
436 x = rc.right - 7;
437 dimensions.left = x - width;
438 dimensions.right = x;
439 }
440
441 // need to adjust the rectangle to cover all the label background
442 dimensions.left -= 2;
443 dimensions.right += 2;
444 dimensions.bottom += 2;
445
446 if ( UseBgCol() )
447 {
448 // our own background colour should be used for the background of
449 // the label: this is consistent with the behaviour under pre-XP
450 // systems (i.e. without visual themes) and generally makes sense
451 wxBrush brush = wxBrush(GetBackgroundColour());
452 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
453 ::FillRect(GetHdcOf(*impl), &dimensions, GetHbrushOf(brush));
454 }
455 else // paint parent background
456 {
457 PaintBackground(dc, dimensions);
458 }
459
460 UINT drawTextFlags = DT_SINGLELINE | DT_VCENTER;
461
462 // determine the state of UI queues to draw the text correctly under XP
463 // and later systems
464 static const bool isXPorLater = wxGetWinVersion() >= wxWinVersion_XP;
465 if ( isXPorLater )
466 {
467 if ( ::SendMessage(GetHwnd(), WM_QUERYUISTATE, 0, 0) &
468 UISF_HIDEACCEL )
469 {
470 drawTextFlags |= DT_HIDEPREFIX;
471 }
472 }
473
474 // now draw the text
475 if ( !rtl )
476 {
477 RECT rc2 = { x, 0, x + width, y };
478 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
479 drawTextFlags);
480 }
481 else // RTL
482 {
483 RECT rc2 = { x, 0, x - width, y };
484 ::DrawText(hdc, label.t_str(), label.length(), &rc2,
485 drawTextFlags | DT_RTLREADING);
486 }
487 }
488 }
489
490 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
491 {
492 RECT rc;
493 ::GetClientRect(GetHwnd(), &rc);
494
495 // draw the entire box in a memory DC
496 wxMemoryDC memdc;
497 wxBitmap bitmap(rc.right, rc.bottom);
498 memdc.SelectObject(bitmap);
499
500 PaintBackground(memdc, rc);
501 PaintForeground(memdc, rc);
502
503 // now only blit the static box border itself, not the interior, to avoid
504 // flicker when background is drawn below
505 //
506 // note that it seems to be faster to do 4 small blits here and then paint
507 // directly into wxPaintDC than painting background in wxMemoryDC and then
508 // blitting everything at once to wxPaintDC, this is why we do it like this
509 wxPaintDC dc(this);
510 int borderTop, border;
511 GetBordersForSizer(&borderTop, &border);
512
513 // top
514 dc.Blit(border, 0, rc.right - border, borderTop,
515 &memdc, border, 0);
516 // bottom
517 dc.Blit(border, rc.bottom - border, rc.right - border, border,
518 &memdc, border, rc.bottom - border);
519 // left
520 dc.Blit(0, 0, border, rc.bottom,
521 &memdc, 0, 0);
522 // right (note that upper and bottom right corners were already part of the
523 // first two blits so we shouldn't overwrite them here to avoi flicker)
524 dc.Blit(rc.right - border, borderTop,
525 border, rc.bottom - borderTop - border,
526 &memdc, rc.right - border, borderTop);
527
528
529 // create the region excluding box children
530 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
531 RECT rcWin;
532 ::GetWindowRect(GetHwnd(), &rcWin);
533 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
534
535 // and also the box itself
536 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
537 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
538 HDCClipper clipToBg(GetHdcOf(*impl), hrgn);
539
540 // paint the inside of the box (excluding box itself and child controls)
541 PaintBackground(dc, rc);
542 }
543
544 #endif // !__WXWINCE__
545
546
547 wxPoint wxStaticBox::GetClientAreaOrigin() const
548 {
549 // See: http://msdn.microsoft.com/en-us/library/aa511279.aspx
550 wxPoint pt = ConvertDialogToPixels(wxPoint(6,11));
551 return pt;
552 }
553
554
555 void wxStaticBox::DoGetClientSize(int *width, int *height) const
556 {
557 // See: http://msdn.microsoft.com/en-us/library/aa511279.aspx
558 wxPoint lr = ConvertDialogToPixels(wxPoint(6,7));
559 wxPoint ul = GetClientAreaOrigin();
560 wxSize sz = GetSize();
561
562 if (width)
563 *width = sz.x - ul.x - lr.x;
564 if (height)
565 *height = sz.y - ul.y - lr.x;
566 }
567
568 #endif // wxUSE_STATBOX