]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
minor tab height bug fix
[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 #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 // ----------------------------------------------------------------------------
46 // wxWin macros
47 // ----------------------------------------------------------------------------
48
49 #if wxUSE_EXTENDED_RTTI
50 WX_DEFINE_FLAGS( wxStaticBoxStyle )
51
52 wxBEGIN_FLAGS( wxStaticBoxStyle )
53 // new style border flags, we put them first to
54 // use them for streaming out
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)
61
62 // old style border flags
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)
68 wxFLAGS_MEMBER(wxBORDER)
69
70 // standard window styles
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
74 wxFLAGS_MEMBER(wxWANTS_CHARS)
75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
77 wxFLAGS_MEMBER(wxVSCROLL)
78 wxFLAGS_MEMBER(wxHSCROLL)
79
80 wxEND_FLAGS( wxStaticBoxStyle )
81
82 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h")
83
84 wxBEGIN_PROPERTIES_TABLE(wxStaticBox)
85 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
86 wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
87 /*
88 TODO PROPERTIES :
89 label
90 */
91 wxEND_PROPERTIES_TABLE()
92
93 wxBEGIN_HANDLERS_TABLE(wxStaticBox)
94 wxEND_HANDLERS_TABLE()
95
96 wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
97 #else
98 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
99 #endif
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 // ----------------------------------------------------------------------------
106 // wxStaticBox
107 // ----------------------------------------------------------------------------
108
109 bool 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)
116 {
117 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
118 return false;
119
120 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
121 return false;
122
123 // Always use LTR layout. Otherwise, the label would be mirrored.
124 SetLayoutDirection(wxLayout_LeftToRight);
125
126 #ifndef __WXWINCE__
127 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
128 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
129 #endif // !__WXWINCE__
130
131 return true;
132 }
133
134 wxBorder wxStaticBox::GetDefaultBorder() const
135 {
136 return wxBORDER_NONE;
137 }
138
139 WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
140 {
141 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
142
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
147 if ( exstyle )
148 {
149 #ifndef __WXWINCE__
150 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
151 *exstyle = WS_EX_TRANSPARENT;
152 else
153 #endif
154 *exstyle = 0;
155 }
156
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;
166 }
167
168 wxSize wxStaticBox::DoGetBestSize() const
169 {
170 int cx, cy;
171 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
172
173 int wBox;
174 GetTextExtent(GetLabelText(wxGetWindowText(m_hWnd)), &wBox, &cy);
175
176 wBox += 3*cx;
177 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
178
179 wxSize best(wBox, hBox);
180 CacheBestSize(best);
181 return best;
182 }
183
184 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
185 {
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
193 #ifndef __WXWINCE__
194
195 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
196 {
197 if ( nMsg == WM_NCHITTEST )
198 {
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 {
207 int xPos = GET_X_LPARAM(lParam);
208 int yPos = GET_Y_LPARAM(lParam);
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 }
217 }
218
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
237 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
238 }
239
240 // ----------------------------------------------------------------------------
241 // static box drawing
242 // ----------------------------------------------------------------------------
243
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 */
251
252 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
253 static inline void
254 SubtractRectFromRgn(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
266 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
267 {
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);
273
274 // top
275 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
276
277 // bottom
278 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
279
280 // left
281 SubtractRectFromRgn(hrgn, 0, 0, border, h);
282
283 // right
284 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
285 }
286
287 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
288 {
289 RECT rc;
290 ::GetWindowRect(GetHwnd(), &rc);
291 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
292 bool foundThis = false;
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) )
298 {
299 if ( ! ::IsWindowVisible(child) )
300 {
301 // if the window isn't visible then it doesn't need clipped
302 continue;
303 }
304
305 LONG style = ::GetWindowLong(child, GWL_STYLE);
306 wxString str(wxGetWindowClass(child));
307 str.UpperCase();
308 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
309 {
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;
322 }
323
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 )
330 {
331 style &= ~WS_CLIPSIBLINGS;
332 ::SetWindowLong(child, GWL_STYLE, style);
333
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);
340 }
341
342 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
343 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
344 }
345 }
346
347 return (WXHRGN)hrgn;
348 }
349
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)
353 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
354 {
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();
364 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), GetHWND());
365
366 // if there is no special brush for painting this control, just use the
367 // solid background colour
368 wxBrush brush;
369 if ( !hbr )
370 {
371 brush = wxBrush(parent->GetBackgroundColour());
372 hbr = GetHbrushOf(brush);
373 }
374
375 ::FillRect(GetHdcOf(dc), &rc, hbr);
376 }
377
378 void wxStaticBox::PaintForeground(wxDC& dc, const RECT& rc)
379 {
380 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(dc), 0);
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
386 if ( wxUxThemeEngine::GetIfActive() )
387 {
388 // draw over the text in default colour in our colour
389 dc.SetFont(GetFont());
390
391 HDC hdc = GetHdcOf(dc);
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 }
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
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
426 const wxString label = GetLabel();
427 int width, height;
428 dc.GetTextExtent(wxStripMenuCodes(label, wxStrip_Mnemonics), &width, &height);
429
430 int x;
431 int y = height;
432
433 // first we need to correctly paint the background of the label
434 // as Windows ignores the brush offset when doing it
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 }
448
449 // need to adjust the rectangle to cover all the label background
450 dimensions.left -= 2;
451 dimensions.right += 2;
452 dimensions.bottom += 2;
453 PaintBackground(dc, dimensions);
454
455 // now draw the text
456 if ( !rtl )
457 {
458 RECT rc2 = { x, 0, x + width, y };
459 ::DrawText(hdc, label, label.length(), &rc2,
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 }
468 }
469 }
470
471 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
472 {
473 RECT rc;
474 ::GetClientRect(GetHwnd(), &rc);
475
476 // draw the entire box in a memory DC
477 wxMemoryDC memdc;
478 wxBitmap bitmap(rc.right, rc.bottom);
479 memdc.SelectObject(bitmap);
480
481 PaintBackground(memdc, rc);
482 PaintForeground(memdc, rc);
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);
491 int borderTop, border;
492 GetBordersForSizer(&borderTop, &border);
493
494 // top
495 dc.Blit(border, 0, rc.right - border, borderTop,
496 &memdc, border, 0);
497 // bottom
498 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
499 &memdc, border, rc.bottom - border);
500 // left
501 dc.Blit(0, 0, border, rc.bottom,
502 &memdc, 0, 0);
503 // right
504 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
505 &memdc, rc.right - border, 0);
506
507
508 // create the region excluding box children
509 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
510 RECT rcWin;
511 ::GetWindowRect(GetHwnd(), &rcWin);
512 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
513
514 // and also the box itself
515 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
516 HDCClipper clipToBg(GetHdcOf(dc), hrgn);
517
518 // paint the inside of the box (excluding box itself and child controls)
519 PaintBackground(dc, rc);
520 }
521
522 #endif // !__WXWINCE__
523
524 #endif // wxUSE_STATBOX