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