]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
Checkable items in wxToolMenuBarTool supported.
[wxWidgets.git] / src / msw / statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/dcclient.h"
32 #endif
33
34 #include "wx/statbox.h"
35 #include "wx/notebook.h"
36 #include "wx/sysopt.h"
37 #include "wx/image.h"
38 #include "wx/dcmemory.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 #ifndef __WXWINCE__
124 if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
125 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
126 #endif // !__WXWINCE__
127
128 return true;
129 }
130
131 wxBorder wxStaticBox::GetDefaultBorder() const
132 {
133 return wxBORDER_NONE;
134 }
135
136 WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
137 {
138 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
139
140 // no need for it anymore, must be removed for wxRadioBox child
141 // buttons to be able to repaint themselves
142 styleWin &= ~WS_CLIPCHILDREN;
143
144 if ( exstyle )
145 {
146 #ifndef __WXWINCE__
147 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
148 *exstyle = WS_EX_TRANSPARENT;
149 else
150 #endif
151 *exstyle = 0;
152 }
153
154 return styleWin | BS_GROUPBOX;
155 }
156
157 wxSize wxStaticBox::DoGetBestSize() const
158 {
159 int cx, cy;
160 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
161
162 int wBox;
163 GetTextExtent(wxStripMenuCodes(wxGetWindowText(m_hWnd)), &wBox, &cy);
164
165 wBox += 3*cx;
166 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
167
168 wxSize best(wBox, hBox);
169 CacheBestSize(best);
170 return best;
171 }
172
173 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
174 {
175 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
176
177 // need extra space, don't know how much but this seems to be enough
178 *borderTop += GetCharHeight()/3;
179 }
180
181 // all the hacks below are not necessary for WinCE
182 #ifndef __WXWINCE__
183
184 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
185 {
186 if ( nMsg == WM_NCHITTEST )
187 {
188 // This code breaks some other processing such as enter/leave tracking
189 // so it's off by default.
190
191 static int s_useHTClient = -1;
192 if (s_useHTClient == -1)
193 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
194 if (s_useHTClient == 1)
195 {
196 int xPos = GET_X_LPARAM(lParam);
197 int yPos = GET_Y_LPARAM(lParam);
198
199 ScreenToClient(&xPos, &yPos);
200
201 // Make sure you can drag by the top of the groupbox, but let
202 // other (enclosed) controls get mouse events also
203 if ( yPos < 10 )
204 return (long)HTCLIENT;
205 }
206 }
207
208 if ( nMsg == WM_PRINTCLIENT )
209 {
210 // we have to process WM_PRINTCLIENT ourselves as otherwise child
211 // windows' background (eg buttons in radio box) would never be drawn
212 // unless we have a parent with non default background
213
214 // so check first if we have one
215 if ( !HandlePrintClient((WXHDC)wParam) )
216 {
217 // no, we don't, erase the background ourselves
218 // (don't use our own) - see PaintBackground for explanation
219 wxBrush brush(GetParent()->GetBackgroundColour());
220 wxFillRect(GetHwnd(), (HDC)wParam, GetHbrushOf(brush));
221 }
222
223 return 0;
224 }
225
226 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
227 }
228
229 // ----------------------------------------------------------------------------
230 // static box drawing
231 // ----------------------------------------------------------------------------
232
233 /*
234 We draw the static box ourselves because it's the only way to prevent it
235 from flickering horribly on resize (because everything inside the box is
236 erased twice: once when the box itself is repainted and second time when
237 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
238 we used to do and which resulted in other problems.
239 */
240
241 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
242 static inline void
243 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
244 {
245 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
246 if ( !hrgnRect )
247 {
248 wxLogLastError(_T("CreateRectRgn()"));
249 return;
250 }
251
252 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
253 }
254
255 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
256 {
257 HRGN hrgn = (HRGN)hRgn;
258
259 // remove the area occupied by the static box borders from the region
260 int borderTop, border;
261 GetBordersForSizer(&borderTop, &border);
262
263 // top
264 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
265
266 // bottom
267 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
268
269 // left
270 SubtractRectFromRgn(hrgn, 0, 0, border, h);
271
272 // right
273 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
274 }
275
276 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
277 {
278 RECT rc;
279 ::GetWindowRect(GetHwnd(), &rc);
280 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
281 bool foundThis = false;
282
283 // iterate over all child windows (not just wxWindows but all windows)
284 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
285 child;
286 child = ::GetWindow(child, GW_HWNDNEXT) )
287 {
288 if ( ! ::IsWindowVisible(child) )
289 {
290 // if the window isn't visible then it doesn't need clipped
291 continue;
292 }
293
294 LONG style = ::GetWindowLong(child, GWL_STYLE);
295 wxString str(wxGetWindowClass(child));
296 str.UpperCase();
297 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
298 {
299 if ( child == GetHwnd() )
300 foundThis = true;
301
302 // Any static boxes below this one in the Z-order can't be clipped
303 // since if we have the case where a static box with a low Z-order
304 // is nested inside another static box with a high Z-order then the
305 // nested static box would be painted over. Doing it this way
306 // unfortunately results in flicker if the Z-order of nested static
307 // boxes is not inside (lowest) to outside (highest) but at least
308 // they are still shown.
309 if ( foundThis )
310 continue;
311 }
312
313 ::GetWindowRect(child, &rc);
314 if ( ::RectInRegion(hrgn, &rc) )
315 {
316 // need to remove WS_CLIPSIBLINGS from all sibling windows
317 // that are within this staticbox if set
318 if ( style & WS_CLIPSIBLINGS )
319 {
320 style &= ~WS_CLIPSIBLINGS;
321 ::SetWindowLong(child, GWL_STYLE, style);
322
323 // MSDN: "If you have changed certain window data using
324 // SetWindowLong, you must call SetWindowPos to have the
325 // changes take effect."
326 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
327 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
328 SWP_FRAMECHANGED);
329 }
330
331 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
332 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
333 }
334 }
335
336 return (WXHRGN)hrgn;
337 }
338
339 // helper for OnPaint(): really erase the background, i.e. do it even if we
340 // don't have any non default brush for doing it (DoEraseBackground() doesn't
341 // do anything in such case)
342 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
343 {
344 // note that we do not use the box background colour here, it shouldn't
345 // apply to its interior for several reasons:
346 // 1. wxGTK doesn't do it
347 // 2. controls inside the box don't get correct bg colour because they
348 // are not our children so we'd have some really ugly colour mix if
349 // we did it
350 // 3. this is backwards compatible behaviour and some people rely on it,
351 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
352 wxWindow *parent = GetParent();
353 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), GetHWND());
354
355 // if there is no special brush for painting this control, just use the
356 // solid background colour
357 wxBrush brush;
358 if ( !hbr )
359 {
360 brush = wxBrush(parent->GetBackgroundColour());
361 hbr = GetHbrushOf(brush);
362 }
363
364 ::FillRect(GetHdcOf(dc), &rc, hbr);
365 }
366
367 void wxStaticBox::PaintForeground(wxDC& dc, const RECT& WXUNUSED(rc))
368 {
369 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(dc), 0);
370
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
375 if ( m_hasFgCol && wxUxThemeEngine::GetIfActive() )
376 {
377 // draw over the text in default colour in our colour
378 dc.SetFont(GetFont());
379
380 HDC hdc = GetHdcOf(dc);
381 ::SetTextColor(hdc, GetForegroundColour().GetPixel());
382
383 // FIXME: value of x is hardcoded as this is what it is on my system,
384 // no idea if it's true everywhere
385 const int y = dc.GetCharHeight();
386 const int x = 9;
387
388 // TODO: RTL?
389 RECT rc = { x, 0, GetSize().x - x, y };
390
391 const wxString label = GetLabel();
392 ::DrawText(hdc, label, label.length(), &rc, DT_SINGLELINE | DT_VCENTER);
393 }
394 }
395
396 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
397 {
398 RECT rc;
399 ::GetClientRect(GetHwnd(), &rc);
400
401 // draw the entire box in a memory DC
402 wxMemoryDC memdc;
403 wxBitmap bitmap(rc.right, rc.bottom);
404 memdc.SelectObject(bitmap);
405
406 PaintBackground(memdc, rc);
407 PaintForeground(memdc, rc);
408
409 // now only blit the static box border itself, not the interior, to avoid
410 // flicker when background is drawn below
411 //
412 // note that it seems to be faster to do 4 small blits here and then paint
413 // directly into wxPaintDC than painting background in wxMemoryDC and then
414 // blitting everything at once to wxPaintDC, this is why we do it like this
415 wxPaintDC dc(this);
416 int borderTop, border;
417 GetBordersForSizer(&borderTop, &border);
418
419 // top
420 dc.Blit(border, 0, rc.right - border, borderTop,
421 &memdc, border, 0);
422 // bottom
423 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
424 &memdc, border, rc.bottom - border);
425 // left
426 dc.Blit(0, 0, border, rc.bottom,
427 &memdc, 0, 0);
428 // right
429 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
430 &memdc, rc.right - border, 0);
431
432
433 // create the region excluding box children
434 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
435 RECT rcWin;
436 ::GetWindowRect(GetHwnd(), &rcWin);
437 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
438
439 // and also the box itself
440 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
441 HDCClipper clipToBg(GetHdcOf(dc), hrgn);
442
443 // paint the inside of the box (excluding box itself and child controls)
444 PaintBackground(dc, rc);
445 }
446
447 #endif // !__WXWINCE__
448
449 #endif // wxUSE_STATBOX
450