]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/statbox.cpp
check that the version of __sync_sub_and_fetch that returns a value is supported...
[wxWidgets.git] / src / msw / statbox.cpp
... / ...
CommitLineData
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
45// the values coincide with those in tmschema.h
46#define BP_GROUPBOX 4
47
48#define GBS_NORMAL 1
49
50#define TMT_FONT 210
51
52// ----------------------------------------------------------------------------
53// wxWin macros
54// ----------------------------------------------------------------------------
55
56#if wxUSE_EXTENDED_RTTI
57WX_DEFINE_FLAGS( wxStaticBoxStyle )
58
59wxBEGIN_FLAGS( wxStaticBoxStyle )
60 // new style border flags, we put them first to
61 // use them for streaming out
62 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
63 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
64 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
65 wxFLAGS_MEMBER(wxBORDER_RAISED)
66 wxFLAGS_MEMBER(wxBORDER_STATIC)
67 wxFLAGS_MEMBER(wxBORDER_NONE)
68
69 // old style border flags
70 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
71 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
72 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
73 wxFLAGS_MEMBER(wxRAISED_BORDER)
74 wxFLAGS_MEMBER(wxSTATIC_BORDER)
75 wxFLAGS_MEMBER(wxBORDER)
76
77 // standard window styles
78 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
79 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
80 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
81 wxFLAGS_MEMBER(wxWANTS_CHARS)
82 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
83 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
84 wxFLAGS_MEMBER(wxVSCROLL)
85 wxFLAGS_MEMBER(wxHSCROLL)
86
87wxEND_FLAGS( wxStaticBoxStyle )
88
89IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h")
90
91wxBEGIN_PROPERTIES_TABLE(wxStaticBox)
92 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
93 wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
94/*
95 TODO PROPERTIES :
96 label
97*/
98wxEND_PROPERTIES_TABLE()
99
100wxBEGIN_HANDLERS_TABLE(wxStaticBox)
101wxEND_HANDLERS_TABLE()
102
103wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
104#else
105IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
106#endif
107
108// ============================================================================
109// implementation
110// ============================================================================
111
112// ----------------------------------------------------------------------------
113// wxStaticBox
114// ----------------------------------------------------------------------------
115
116bool wxStaticBox::Create(wxWindow *parent,
117 wxWindowID id,
118 const wxString& label,
119 const wxPoint& pos,
120 const wxSize& size,
121 long style,
122 const wxString& name)
123{
124 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
125 return false;
126
127 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
128 return false;
129
130 // Always use LTR layout. Otherwise, the label would be mirrored.
131 SetLayoutDirection(wxLayout_LeftToRight);
132
133#ifndef __WXWINCE__
134 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
135 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
136#endif // !__WXWINCE__
137
138 return true;
139}
140
141WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
142{
143 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
144
145 // no need for it anymore, must be removed for wxRadioBox child
146 // buttons to be able to repaint themselves
147 styleWin &= ~WS_CLIPCHILDREN;
148
149 if ( exstyle )
150 {
151#ifndef __WXWINCE__
152 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
153 *exstyle = WS_EX_TRANSPARENT;
154 else
155#endif
156 *exstyle = 0;
157 }
158
159 styleWin |= BS_GROUPBOX;
160
161 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
162 {
163 // Make sure label is on the right
164 styleWin |= BS_RIGHT;
165 }
166
167 return styleWin;
168}
169
170wxSize wxStaticBox::DoGetBestSize() const
171{
172 int cx, cy;
173 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
174
175 int wBox;
176 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd)), &wBox, &cy);
177
178 wBox += 3*cx;
179 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
180
181 wxSize best(wBox, hBox);
182 CacheBestSize(best);
183 return best;
184}
185
186void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
187{
188 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
189
190 // need extra space, don't know how much but this seems to be enough
191 *borderTop += GetCharHeight()/3;
192}
193
194// all the hacks below are not necessary for WinCE
195#ifndef __WXWINCE__
196
197WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
198{
199 if ( nMsg == WM_NCHITTEST )
200 {
201 // This code breaks some other processing such as enter/leave tracking
202 // so it's off by default.
203
204 static int s_useHTClient = -1;
205 if (s_useHTClient == -1)
206 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
207 if (s_useHTClient == 1)
208 {
209 int xPos = GET_X_LPARAM(lParam);
210 int yPos = GET_Y_LPARAM(lParam);
211
212 ScreenToClient(&xPos, &yPos);
213
214 // Make sure you can drag by the top of the groupbox, but let
215 // other (enclosed) controls get mouse events also
216 if ( yPos < 10 )
217 return (long)HTCLIENT;
218 }
219 }
220
221 if ( nMsg == WM_PRINTCLIENT )
222 {
223 // we have to process WM_PRINTCLIENT ourselves as otherwise child
224 // windows' background (eg buttons in radio box) would never be drawn
225 // unless we have a parent with non default background
226
227 // so check first if we have one
228 if ( !HandlePrintClient((WXHDC)wParam) )
229 {
230 // no, we don't, erase the background ourselves
231 // (don't use our own) - see PaintBackground for explanation
232 wxBrush brush(GetParent()->GetBackgroundColour());
233 wxFillRect(GetHwnd(), (HDC)wParam, GetHbrushOf(brush));
234 }
235
236 return 0;
237 }
238
239 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
240}
241
242// ----------------------------------------------------------------------------
243// static box drawing
244// ----------------------------------------------------------------------------
245
246/*
247 We draw the static box ourselves because it's the only way to prevent it
248 from flickering horribly on resize (because everything inside the box is
249 erased twice: once when the box itself is repainted and second time when
250 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
251 we used to do and which resulted in other problems.
252 */
253
254// MSWGetRegionWithoutSelf helper: removes the given rectangle from region
255static inline void
256SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
257{
258 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
259 if ( !hrgnRect )
260 {
261 wxLogLastError(_T("CreateRectRgn()"));
262 return;
263 }
264
265 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
266}
267
268void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
269{
270 HRGN hrgn = (HRGN)hRgn;
271
272 // remove the area occupied by the static box borders from the region
273 int borderTop, border;
274 GetBordersForSizer(&borderTop, &border);
275
276 // top
277 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
278
279 // bottom
280 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
281
282 // left
283 SubtractRectFromRgn(hrgn, 0, 0, border, h);
284
285 // right
286 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
287}
288
289WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
290{
291 RECT rc;
292 ::GetWindowRect(GetHwnd(), &rc);
293 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
294 bool foundThis = false;
295
296 // iterate over all child windows (not just wxWindows but all windows)
297 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
298 child;
299 child = ::GetWindow(child, GW_HWNDNEXT) )
300 {
301 if ( ! ::IsWindowVisible(child) )
302 {
303 // if the window isn't visible then it doesn't need clipped
304 continue;
305 }
306
307 LONG style = ::GetWindowLong(child, GWL_STYLE);
308 wxString str(wxGetWindowClass(child));
309 str.UpperCase();
310 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
311 {
312 if ( child == GetHwnd() )
313 foundThis = true;
314
315 // Any static boxes below this one in the Z-order can't be clipped
316 // since if we have the case where a static box with a low Z-order
317 // is nested inside another static box with a high Z-order then the
318 // nested static box would be painted over. Doing it this way
319 // unfortunately results in flicker if the Z-order of nested static
320 // boxes is not inside (lowest) to outside (highest) but at least
321 // they are still shown.
322 if ( foundThis )
323 continue;
324 }
325
326 ::GetWindowRect(child, &rc);
327 if ( ::RectInRegion(hrgn, &rc) )
328 {
329 // need to remove WS_CLIPSIBLINGS from all sibling windows
330 // that are within this staticbox if set
331 if ( style & WS_CLIPSIBLINGS )
332 {
333 style &= ~WS_CLIPSIBLINGS;
334 ::SetWindowLong(child, GWL_STYLE, style);
335
336 // MSDN: "If you have changed certain window data using
337 // SetWindowLong, you must call SetWindowPos to have the
338 // changes take effect."
339 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
340 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
341 SWP_FRAMECHANGED);
342 }
343
344 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
345 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
346 }
347 }
348
349 return (WXHRGN)hrgn;
350}
351
352// helper for OnPaint(): really erase the background, i.e. do it even if we
353// don't have any non default brush for doing it (DoEraseBackground() doesn't
354// do anything in such case)
355void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
356{
357 // note that we do not use the box background colour here, it shouldn't
358 // apply to its interior for several reasons:
359 // 1. wxGTK doesn't do it
360 // 2. controls inside the box don't get correct bg colour because they
361 // are not our children so we'd have some really ugly colour mix if
362 // we did it
363 // 3. this is backwards compatible behaviour and some people rely on it,
364 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
365 wxWindow *parent = GetParent();
366 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
367 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(impl->GetHDC(), GetHWND());
368
369 // if there is no special brush for painting this control, just use the
370 // solid background colour
371 wxBrush brush;
372 if ( !hbr )
373 {
374 brush = wxBrush(parent->GetBackgroundColour());
375 hbr = GetHbrushOf(brush);
376 }
377
378 ::FillRect(GetHdcOf(*impl), &rc, hbr);
379}
380
381void wxStaticBox::PaintForeground(wxDC& dc, const RECT& rc)
382{
383 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
384 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(*impl), 0);
385
386 // when using XP themes, neither setting the text colour nor transparent
387 // background mode doesn't change anything: the static box def window proc
388 // still draws the label in its own colours, so we need to redraw the text
389 // ourselves if we have a non default fg colour
390 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
391 {
392 // draw over the text in default colour in our colour
393 HDC hdc = GetHdcOf(*impl);
394 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
395
396 const bool rtl = wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft;
397 if ( rtl )
398 ::SetTextAlign(hdc, TA_RTLREADING | TA_RIGHT);
399
400 // Get dimensions of the label
401 const wxString label = GetLabel();
402
403 // choose the correct font
404 AutoHFONT font;
405 SelectInHDC selFont;
406 if ( m_hasFont )
407 {
408 selFont.Init(hdc, GetHfontOf(GetFont()));
409 }
410 else // no font set, use the one set by the theme
411 {
412 wxUxThemeHandle hTheme(this, L"BUTTON");
413 if ( hTheme )
414 {
415 // GetThemeFont() expects its parameter to be LOGFONTW and not
416 // LOGFONTA even in ANSI programs and will happily corrupt
417 // memory after the struct end if we pass a LOGFONTA (which is
418 // smaller) to it!
419 LOGFONTW lfw;
420 if ( wxUxThemeEngine::Get()->GetThemeFont
421 (
422 hTheme,
423 hdc,
424 BP_GROUPBOX,
425 GBS_NORMAL,
426 TMT_FONT,
427 (LOGFONT *)&lfw
428 ) == S_OK )
429 {
430#if wxUSE_UNICODE
431 // ok, no conversion necessary
432 const LOGFONT& lf = lfw;
433#else // !wxUSE_UNICODE
434 // most of the fields are the same in LOGFONTA and LOGFONTW
435 LOGFONT lf;
436 memcpy(&lf, &lfw, sizeof(lf));
437
438 // but the face name must be converted
439 WideCharToMultiByte(CP_ACP, 0, lfw.lfFaceName, -1,
440 lf.lfFaceName, sizeof(lf.lfFaceName),
441 NULL, NULL);
442#endif // wxUSE_UNICODE/!wxUSE_UNICODE
443
444 font.Init(lf);
445 if ( font )
446 selFont.Init(hdc, font);
447 }
448 }
449 }
450
451 // Get the font extent
452 int width, height;
453 dc.GetTextExtent(wxStripMenuCodes(label, wxStrip_Mnemonics),
454 &width, &height);
455
456 int x;
457 int y = height;
458
459 // first we need to correctly paint the background of the label
460 // as Windows ignores the brush offset when doing it
461 //
462 // FIXME: value of x is hardcoded as this is what it is on my system,
463 // no idea if it's true everywhere
464 RECT dimensions = {0, 0, 0, y};
465 if ( !rtl )
466 {
467 x = 9;
468 dimensions.left = x;
469 dimensions.right = x + width;
470 }
471 else
472 {
473 x = rc.right - 7;
474 dimensions.left = x - width;
475 dimensions.right = x;
476 }
477
478 // need to adjust the rectangle to cover all the label background
479 dimensions.left -= 2;
480 dimensions.right += 2;
481 dimensions.bottom += 2;
482
483 if ( UseBgCol() )
484 {
485 // our own background colour should be used for the background of
486 // the label: this is consistent with the behaviour under pre-XP
487 // systems (i.e. without visual themes) and generally makes sense
488 wxBrush brush = wxBrush(GetBackgroundColour());
489 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
490 ::FillRect(GetHdcOf(*impl), &dimensions, GetHbrushOf(brush));
491 }
492 else // paint parent background
493 {
494 PaintBackground(dc, dimensions);
495 }
496
497 // now draw the text
498 if ( !rtl )
499 {
500 RECT rc2 = { x, 0, x + width, y };
501 ::DrawText(hdc, label.wx_str(), label.length(), &rc2,
502 DT_SINGLELINE | DT_VCENTER);
503 }
504 else // RTL
505 {
506 RECT rc2 = { x, 0, x - width, y };
507 ::DrawText(hdc, label.wx_str(), label.length(), &rc2,
508 DT_SINGLELINE | DT_VCENTER | DT_RTLREADING);
509 }
510 }
511}
512
513void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
514{
515 RECT rc;
516 ::GetClientRect(GetHwnd(), &rc);
517
518 // draw the entire box in a memory DC
519 wxMemoryDC memdc;
520 wxBitmap bitmap(rc.right, rc.bottom);
521 memdc.SelectObject(bitmap);
522
523 PaintBackground(memdc, rc);
524 PaintForeground(memdc, rc);
525
526 // now only blit the static box border itself, not the interior, to avoid
527 // flicker when background is drawn below
528 //
529 // note that it seems to be faster to do 4 small blits here and then paint
530 // directly into wxPaintDC than painting background in wxMemoryDC and then
531 // blitting everything at once to wxPaintDC, this is why we do it like this
532 wxPaintDC dc(this);
533 int borderTop, border;
534 GetBordersForSizer(&borderTop, &border);
535
536 // top
537 dc.Blit(border, 0, rc.right - border, borderTop,
538 &memdc, border, 0);
539 // bottom
540 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
541 &memdc, border, rc.bottom - border);
542 // left
543 dc.Blit(0, 0, border, rc.bottom,
544 &memdc, 0, 0);
545 // right
546 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
547 &memdc, rc.right - border, 0);
548
549
550 // create the region excluding box children
551 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
552 RECT rcWin;
553 ::GetWindowRect(GetHwnd(), &rcWin);
554 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
555
556 // and also the box itself
557 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
558 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
559 HDCClipper clipToBg(GetHdcOf(*impl), hrgn);
560
561 // paint the inside of the box (excluding box itself and child controls)
562 PaintBackground(dc, rc);
563}
564
565#endif // !__WXWINCE__
566
567#endif // wxUSE_STATBOX