]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
Added licence/copyright information
[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 #include "wx/sysopt.h"
44
45 #include "wx/msw/private.h"
46 #include "wx/msw/missing.h"
47
48 // ----------------------------------------------------------------------------
49 // wxWin macros
50 // ----------------------------------------------------------------------------
51
52 #if wxUSE_EXTENDED_RTTI
53 WX_DEFINE_FLAGS( wxStaticBoxStyle )
54
55 wxBEGIN_FLAGS( wxStaticBoxStyle )
56 // new style border flags, we put them first to
57 // use them for streaming out
58 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
59 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
60 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
61 wxFLAGS_MEMBER(wxBORDER_RAISED)
62 wxFLAGS_MEMBER(wxBORDER_STATIC)
63 wxFLAGS_MEMBER(wxBORDER_NONE)
64
65 // old style border flags
66 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
67 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
68 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
69 wxFLAGS_MEMBER(wxRAISED_BORDER)
70 wxFLAGS_MEMBER(wxSTATIC_BORDER)
71 wxFLAGS_MEMBER(wxBORDER)
72
73 // standard window styles
74 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
75 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
76 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
77 wxFLAGS_MEMBER(wxWANTS_CHARS)
78 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
79 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
80 wxFLAGS_MEMBER(wxVSCROLL)
81 wxFLAGS_MEMBER(wxHSCROLL)
82
83 wxEND_FLAGS( wxStaticBoxStyle )
84
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h")
86
87 wxBEGIN_PROPERTIES_TABLE(wxStaticBox)
88 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
89 wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
90 /*
91 TODO PROPERTIES :
92 label
93 */
94 wxEND_PROPERTIES_TABLE()
95
96 wxBEGIN_HANDLERS_TABLE(wxStaticBox)
97 wxEND_HANDLERS_TABLE()
98
99 wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
100 #else
101 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
102 #endif
103
104 // ============================================================================
105 // implementation
106 // ============================================================================
107
108 // ----------------------------------------------------------------------------
109 // wxStaticBox
110 // ----------------------------------------------------------------------------
111
112 bool wxStaticBox::Create(wxWindow *parent,
113 wxWindowID id,
114 const wxString& label,
115 const wxPoint& pos,
116 const wxSize& size,
117 long style,
118 const wxString& name)
119 {
120 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
121 return false;
122
123 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
124 return false;
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 if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint")))
150 *exstyle = WS_EX_TRANSPARENT;
151 else
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(wxGetWindowText(m_hWnd), &wBox, &cy);
165
166 wBox += 3*cx;
167 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
168
169 return wxSize(wBox, hBox);
170 }
171
172 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
173 {
174 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
175
176 // need extra space, don't know how much but this seems to be enough
177 *borderTop += GetCharHeight()/3;
178 }
179
180 // all the hacks below are not necessary for WinCE
181 #ifndef __WXWINCE__
182
183 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
184 {
185 if ( nMsg == WM_NCHITTEST )
186 {
187 // This code breaks some other processing such as enter/leave tracking
188 // so it's off by default.
189
190 static int s_useHTClient = -1;
191 if (s_useHTClient == -1)
192 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
193 if (s_useHTClient == 1)
194 {
195 int xPos = GET_X_LPARAM(lParam);
196 int yPos = GET_Y_LPARAM(lParam);
197
198 ScreenToClient(&xPos, &yPos);
199
200 // Make sure you can drag by the top of the groupbox, but let
201 // other (enclosed) controls get mouse events also
202 if ( yPos < 10 )
203 return (long)HTCLIENT;
204 }
205 }
206
207 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
208 }
209
210 // ----------------------------------------------------------------------------
211 // static box drawing
212 // ----------------------------------------------------------------------------
213
214 /*
215 We draw the static box ourselves because it's the only way to prevent it
216 from flickering horribly on resize (because everything inside the box is
217 erased twice: once when the box itself is repainted and second time when
218 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
219 we used to do and which resulted in other problems.
220 */
221
222 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
223 static inline void
224 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
225 {
226 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
227 if ( !hrgnRect )
228 {
229 wxLogLastError(_T("CreateRectRgn()"));
230 return;
231 }
232
233 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
234 }
235
236 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
237 {
238 HRGN hrgn = (HRGN)hRgn;
239
240 // remove the area occupied by the static box borders from the region
241 int borderTop, border;
242 GetBordersForSizer(&borderTop, &border);
243
244 // top
245 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
246
247 // bottom
248 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
249
250 // left
251 SubtractRectFromRgn(hrgn, 0, 0, border, h);
252
253 // right
254 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
255 }
256
257 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
258 {
259 RECT rc;
260 ::GetWindowRect(GetHwnd(), &rc);
261 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
262
263 // iterate over all child windows (not just wxWindows but all windows)
264 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
265 child;
266 child = ::GetWindow(child, GW_HWNDNEXT) )
267 {
268 if ( ! ::IsWindowVisible(child) )
269 {
270 // if the window isn't visible then it doesn't need clipped
271 continue;
272 }
273
274 LONG style = ::GetWindowLong(child, GWL_STYLE);
275 wxString str(wxGetWindowClass(child));
276 str.UpperCase();
277 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) == BS_GROUPBOX )
278 {
279 // Don't clip any static boxes, not just this one. This will
280 // result in flicker in overlapping static boxes, but at least
281 // they will all be drawn correctly and we shouldn't have
282 // overlapping windows anyway.
283 continue;
284 }
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 if ( style & WS_CLIPSIBLINGS )
292 {
293 style &= ~WS_CLIPSIBLINGS;
294 ::SetWindowLong(child, GWL_STYLE, style);
295
296 // MSDN: "If you have changed certain window data using
297 // SetWindowLong, you must call SetWindowPos to have the
298 // changes take effect."
299 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
300 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
301 SWP_FRAMECHANGED);
302 }
303
304 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
305 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
306 }
307 }
308
309 return (WXHRGN)hrgn;
310 }
311
312 // helper for OnPaint(): really erase the background, i.e. do it even if we
313 // don't have any non default brush for doing it (DoEraseBackground() doesn't
314 // do anything in such case)
315 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
316 {
317 // note that we do not use the box background colour here, it shouldn't
318 // apply to its interior for several reasons:
319 // 1. wxGTK doesn't do it
320 // 2. controls inside the box don't get correct bg colour because they
321 // are not our children so we'd have some really ugly colour mix if
322 // we did it
323 // 3. this is backwards compatible behaviour and some people rely on it,
324 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
325 wxWindow *parent = GetParent();
326 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), GetHWND());
327
328 // if there is no special brush for painting this control, just use the
329 // solid background colour
330 wxBrush brush;
331 if ( !hbr )
332 {
333 brush = wxBrush(parent->GetBackgroundColour());
334 hbr = GetHbrushOf(brush);
335 }
336
337 ::FillRect(GetHdcOf(dc), &rc, hbr);
338 }
339
340 void wxStaticBox::PaintForeground(wxDC& dc, const RECT& WXUNUSED(rc))
341 {
342 // NB: neither setting the text colour nor transparent background mode
343 // doesn't change anything: the static box def window proc still
344 // draws the label in its own colours, so if we want to have control
345 // over this we really have to draw everything ourselves
346 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(dc), 0);
347 }
348
349 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
350 {
351 RECT rc;
352 ::GetClientRect(GetHwnd(), &rc);
353
354 // draw the entire box in a memory DC
355 wxMemoryDC memdc;
356 wxBitmap bitmap(rc.right, rc.bottom);
357 memdc.SelectObject(bitmap);
358
359 PaintBackground(memdc, rc);
360 PaintForeground(memdc, rc);
361
362 // now only blit the static box border itself, not the interior, to avoid
363 // flicker when background is drawn below
364 //
365 // note that it seems to be faster to do 4 small blits here and then paint
366 // directly into wxPaintDC than painting background in wxMemoryDC and then
367 // blitting everything at once to wxPaintDC, this is why we do it like this
368 wxPaintDC dc(this);
369 int borderTop, border;
370 GetBordersForSizer(&borderTop, &border);
371
372 // top
373 dc.Blit(border, 0, rc.right - border, borderTop,
374 &memdc, border, 0);
375 // bottom
376 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
377 &memdc, border, rc.bottom - border);
378 // left
379 dc.Blit(0, 0, border, rc.bottom,
380 &memdc, 0, 0);
381 // right
382 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
383 &memdc, rc.right - border, 0);
384
385
386 // create the region excluding box children
387 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
388 RECT rcWin;
389 ::GetWindowRect(GetHwnd(), &rcWin);
390 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
391
392 // and also the box itself
393 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
394 HDCClipper clipToBg(GetHdcOf(dc), hrgn);
395
396 // paint the inside of the box (excluding box itself and child controls)
397 PaintBackground(dc, rc);
398 }
399
400 #endif // !__WXWINCE__
401
402 #endif // wxUSE_STATBOX
403