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