fixed wxStaticBox::SetBackgroundColour(): this should change background for the label...
[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
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 *exstyle = 0;
146
147 return styleWin | BS_GROUPBOX;
148 }
149
150 wxSize wxStaticBox::DoGetBestSize() const
151 {
152 int cx, cy;
153 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
154
155 int wBox;
156 GetTextExtent(wxGetWindowText(m_hWnd), &wBox, &cy);
157
158 wBox += 3*cx;
159 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
160
161 return wxSize(wBox, hBox);
162 }
163
164 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
165 {
166 #ifndef __WXWINCE__
167 if ( nMsg == WM_NCHITTEST )
168 {
169 // This code breaks some other processing such as enter/leave tracking
170 // so it's off by default.
171
172 static int s_useHTClient = -1;
173 if (s_useHTClient == -1)
174 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
175 if (s_useHTClient == 1)
176 {
177 int xPos = LOWORD(lParam); // horizontal position of cursor
178 int yPos = HIWORD(lParam); // vertical position of cursor
179
180 ScreenToClient(&xPos, &yPos);
181
182 // Make sure you can drag by the top of the groupbox, but let
183 // other (enclosed) controls get mouse events also
184 if ( yPos < 10 )
185 return (long)HTCLIENT;
186 }
187 }
188 #endif // !__WXWINCE__
189
190 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
191 }
192
193 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
194 {
195 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
196
197 // if not using correct (but backwards cojmpatible) text metrics
198 // calculations, we need to add some extra margin or otherwise static box
199 // title is clipped
200 #if !wxDIALOG_UNIT_COMPATIBILITY
201 *borderTop += GetCharHeight()/3;
202 #endif // !wxDIALOG_UNIT_COMPATIBILITY
203 }
204
205 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
206 static inline void
207 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
208 {
209 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
210 if ( !hrgnRect )
211 {
212 wxLogLastError(_T("CreateRectRgn()"));
213 return;
214 }
215
216 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
217 }
218
219 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
220 {
221 HRGN hrgn = (HRGN)hRgn;
222
223 // remove the area occupied by the static box borders from the region
224 int borderTop, border;
225 GetBordersForSizer(&borderTop, &border);
226
227 // top
228 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
229
230 // bottom
231 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
232
233 // left
234 SubtractRectFromRgn(hrgn, 0, 0, border, h);
235
236 // right
237 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
238 }
239
240 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
241 {
242 RECT rc;
243 ::GetWindowRect(GetHwnd(), &rc);
244 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
245
246 // iterate over all child windows (not just wxWindows but all windows)
247 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
248 child;
249 child = ::GetWindow(child, GW_HWNDNEXT) )
250 {
251 wxWindow *childWindow = wxGetWindowFromHWND((WXHWND) child);
252
253 // can't just test for (this != child) here since if a wxStaticBox
254 // overlaps another wxStaticBox then neither are drawn. The overlapping
255 // region will flicker but we shouldn't have overlapping windows anyway.
256 if ( !childWindow || !wxDynamicCast(childWindow, wxStaticBox) )
257 {
258 ::GetWindowRect(child, &rc);
259 if ( ::RectInRegion(hrgn, &rc) )
260 {
261 // need to remove WS_CLIPSIBLINGS from all sibling windows
262 // that are within this staticbox if set
263 LONG style = ::GetWindowLong(child, GWL_STYLE);
264 if ( style & WS_CLIPSIBLINGS )
265 {
266 style &= ~WS_CLIPSIBLINGS;
267 ::SetWindowLong(child, GWL_STYLE, style);
268
269 // MSDN: "If you have changed certain window data using
270 // SetWindowLong, you must call SetWindowPos to have the
271 // changes take effect."
272 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
273 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
274 SWP_FRAMECHANGED);
275 }
276
277 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
278 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
279 }
280 }
281 }
282
283 return (WXHRGN)hrgn;
284 }
285
286 // helper for OnPaint()
287 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
288 {
289 // note that static box should be transparent, so it should show its
290 // parents colour, not its own
291 wxWindow * const parent = GetParent();
292
293 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC());
294 if ( !hbr )
295 {
296 wxBrush *brush =
297 wxTheBrushList->FindOrCreateBrush(parent->GetBackgroundColour());
298 if ( brush )
299 hbr = GetHbrushOf(*brush);
300 }
301
302 if ( hbr )
303 ::FillRect(GetHdcOf(dc), &rc, hbr);
304 }
305
306 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
307 {
308 wxPaintDC dc(this);
309 RECT rc;
310 ::GetClientRect(GetHwnd(), &rc);
311
312 // draw the entire box in a memory DC, but only blit the bits not redrawn
313 // either by our children windows nor by FillRect() painting the background
314 // below
315 wxMemoryDC memdc;
316 wxBitmap bitmap(rc.right, rc.bottom);
317 memdc.SelectObject(bitmap);
318
319 PaintBackground(memdc, rc);
320 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(memdc), 0);
321
322 int borderTop, border;
323 GetBordersForSizer(&borderTop, &border);
324
325 // top
326 dc.Blit(border, 0, rc.right - border, borderTop,
327 &memdc, border, 0);
328 // bottom
329 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
330 &memdc, border, rc.bottom - border);
331 // left
332 dc.Blit(0, 0, border, rc.bottom,
333 &memdc, 0, 0);
334 // right
335 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
336 &memdc, rc.right - border, 0);
337
338 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
339 RECT rcWin;
340 ::GetWindowRect(GetHwnd(), &rcWin);
341 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
342
343
344 // now remove the box itself
345 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
346
347 // and paint the inside of the box (excluding child controls)
348 ::SelectClipRgn(GetHdcOf(dc), hrgn);
349 PaintBackground(dc, rc);
350 ::SelectClipRgn(GetHdcOf(dc), NULL);
351 }
352
353 #endif // wxUSE_STATBOX
354