]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
undid flags values change, it's not as backwards compatible as I thought
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "statbox.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_STATBOX
32
33 #ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/dcclient.h"
36 #endif
37
38 #include "wx/statbox.h"
39 #include "wx/notebook.h"
40 #include "wx/sysopt.h"
41 #include "wx/image.h"
42 #include "wx/dcmemory.h"
43
44 #include "wx/msw/private.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 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 // If any of the ancestors are scrolling windows, style has to be WS_EX_TRANSPARENT
147 // or the static box won't be painted when the window is scrolled. We try
148 // not to do this normally, because we get a lot of flicker.
149 wxWindow* p = GetParent();
150 bool ancestorScrolls = false;
151 while (p && !p->IsTopLevel())
152 {
153 if (p->HasFlag(wxVSCROLL) || GetParent()->HasFlag(wxHSCROLL))
154 {
155 ancestorScrolls = true;
156 break;
157 }
158 p = p->GetParent();
159 }
160 if (ancestorScrolls)
161 *exstyle = WS_EX_TRANSPARENT;
162 else
163 *exstyle = 0;
164 }
165
166 return styleWin | BS_GROUPBOX;
167 }
168
169 wxSize wxStaticBox::DoGetBestSize() const
170 {
171 int cx, cy;
172 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
173
174 int wBox;
175 GetTextExtent(wxGetWindowText(m_hWnd), &wBox, &cy);
176
177 wBox += 3*cx;
178 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
179
180 return wxSize(wBox, hBox);
181 }
182
183 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
184 {
185 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
186
187 // need extra space, don't know how much but this seems to be enough
188 *borderTop += GetCharHeight()/3;
189 }
190
191 // all the hacks below are not necessary for WinCE
192 #ifndef __WXWINCE__
193
194 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
195 {
196 if ( nMsg == WM_NCHITTEST )
197 {
198 // This code breaks some other processing such as enter/leave tracking
199 // so it's off by default.
200
201 static int s_useHTClient = -1;
202 if (s_useHTClient == -1)
203 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
204 if (s_useHTClient == 1)
205 {
206 int xPos = GET_X_LPARAM(lParam);
207 int yPos = GET_Y_LPARAM(lParam);
208
209 ScreenToClient(&xPos, &yPos);
210
211 // Make sure you can drag by the top of the groupbox, but let
212 // other (enclosed) controls get mouse events also
213 if ( yPos < 10 )
214 return (long)HTCLIENT;
215 }
216 }
217
218 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
219 }
220
221 // ----------------------------------------------------------------------------
222 // static box drawing
223 // ----------------------------------------------------------------------------
224
225 /*
226 We draw the static box ourselves because it's the only way to prevent it
227 from flickering horribly on resize (because everything inside the box is
228 erased twice: once when the box itself is repainted and second time when
229 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
230 we used to do and which resulted in other problems.
231 */
232
233 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
234 static inline void
235 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
236 {
237 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
238 if ( !hrgnRect )
239 {
240 wxLogLastError(_T("CreateRectRgn()"));
241 return;
242 }
243
244 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
245 }
246
247 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
248 {
249 HRGN hrgn = (HRGN)hRgn;
250
251 // remove the area occupied by the static box borders from the region
252 int borderTop, border;
253 GetBordersForSizer(&borderTop, &border);
254
255 // top
256 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
257
258 // bottom
259 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
260
261 // left
262 SubtractRectFromRgn(hrgn, 0, 0, border, h);
263
264 // right
265 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
266 }
267
268 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
269 {
270 RECT rc;
271 ::GetWindowRect(GetHwnd(), &rc);
272 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
273
274 // iterate over all child windows (not just wxWindows but all windows)
275 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
276 child;
277 child = ::GetWindow(child, GW_HWNDNEXT) )
278 {
279 wxWindow *childWindow = wxGetWindowFromHWND((WXHWND) child);
280
281 // can't just test for (this != child) here since if a wxStaticBox
282 // overlaps another wxStaticBox then neither are drawn. The overlapping
283 // region will flicker but we shouldn't have overlapping windows anyway.
284 if ( !childWindow || !wxDynamicCast(childWindow, wxStaticBox) )
285 {
286 ::GetWindowRect(child, &rc);
287 if ( ::RectInRegion(hrgn, &rc) )
288 {
289 // need to remove WS_CLIPSIBLINGS from all sibling windows
290 // that are within this staticbox if set
291 LONG style = ::GetWindowLong(child, GWL_STYLE);
292 if ( style & WS_CLIPSIBLINGS )
293 {
294 style &= ~WS_CLIPSIBLINGS;
295 ::SetWindowLong(child, GWL_STYLE, style);
296
297 // MSDN: "If you have changed certain window data using
298 // SetWindowLong, you must call SetWindowPos to have the
299 // changes take effect."
300 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
301 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
302 SWP_FRAMECHANGED);
303 }
304
305 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
306 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
307 }
308 }
309 }
310
311 return (WXHRGN)hrgn;
312 }
313
314 // helper for OnPaint(): really erase the background, i.e. do it even if we
315 // don't have any non default brush for doing it (DoEraseBackground() doesn't
316 // do anything in such case)
317 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
318 {
319 // note that we do not use the box background colour here, it shouldn't
320 // apply to its interior for several reasons:
321 // 1. wxGTK doesn't do it
322 // 2. controls inside the box don't get correct bg colour because they
323 // are not our children so we'd have some really ugly colour mix if
324 // we did it
325 // 3. this is backwards compatible behaviour and some people rely on it,
326 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
327 wxWindow *parent = GetParent();
328 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), this);
329
330 // if there is no special brush for painting this control, just use the
331 // solid background colour
332 wxBrush brush;
333 if ( !hbr )
334 {
335 brush = wxBrush(parent->GetBackgroundColour());
336 hbr = GetHbrushOf(brush);
337 }
338
339 ::FillRect(GetHdcOf(dc), &rc, hbr);
340 }
341
342 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
343 {
344 RECT rc;
345 ::GetClientRect(GetHwnd(), &rc);
346
347 // draw the entire box in a memory DC
348 wxMemoryDC memdc;
349 wxBitmap bitmap(rc.right, rc.bottom);
350 memdc.SelectObject(bitmap);
351
352 PaintBackground(memdc, rc);
353
354 // NB: neither setting the text colour nor transparent background mode
355 // doesn't change anything: the static box def window proc still
356 // draws the label in its own colours, so if we want to have control
357 // over this we really have to draw everything ourselves
358 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(memdc), 0);
359
360
361 // now only blit the static box border itself, not the interior, to avoid
362 // flicker when background is drawn below
363 //
364 // note that it seems to be faster to do 4 small blits here and then paint
365 // directly into wxPaintDC than painting background in wxMemoryDC and then
366 // blitting everything at once to wxPaintDC, this is why we do it like this
367 wxPaintDC dc(this);
368 int borderTop, border;
369 GetBordersForSizer(&borderTop, &border);
370
371 // top
372 dc.Blit(border, 0, rc.right - border, borderTop,
373 &memdc, border, 0);
374 // bottom
375 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
376 &memdc, border, rc.bottom - border);
377 // left
378 dc.Blit(0, 0, border, rc.bottom,
379 &memdc, 0, 0);
380 // right
381 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
382 &memdc, rc.right - border, 0);
383
384
385 // create the region excluding box children
386 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
387 RECT rcWin;
388 ::GetWindowRect(GetHwnd(), &rcWin);
389 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
390
391 // and also the box itself
392 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
393 HDCClipper clipToBg(GetHdcOf(dc), hrgn);
394
395 // paint the inside of the box (excluding box itself and child controls)
396 PaintBackground(dc, rc);
397 }
398
399 #endif // !__WXWINCE__
400
401 #endif // wxUSE_STATBOX
402