]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
3f2711d5 | 2 | // Name: msw/statbox.cpp |
2bda0e17 KB |
3 | // Purpose: wxStaticBox |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3f2711d5 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
3f2711d5 | 21 | #pragma implementation "statbox.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
3f2711d5 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_STATBOX |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
3f2711d5 VZ |
34 | #include "wx/app.h" |
35 | #include "wx/dcclient.h" | |
2bda0e17 KB |
36 | #endif |
37 | ||
38 | #include "wx/statbox.h" | |
3ad70d31 | 39 | #include "wx/notebook.h" |
9dabade2 | 40 | #include "wx/sysopt.h" |
eba99da4 | 41 | #include "wx/image.h" |
a4c46c19 | 42 | #include "wx/dcmemory.h" |
2bda0e17 | 43 | |
3f2711d5 | 44 | #include "wx/msw/private.h" |
72093cd8 | 45 | #include "wx/msw/missing.h" |
3f2711d5 VZ |
46 | |
47 | // ---------------------------------------------------------------------------- | |
48 | // wxWin macros | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51741307 | 51 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
52 | WX_DEFINE_FLAGS( wxStaticBoxStyle ) |
53 | ||
3ff066a4 | 54 | wxBEGIN_FLAGS( wxStaticBoxStyle ) |
bc9fb572 JS |
55 | // new style border flags, we put them first to |
56 | // use them for streaming out | |
3ff066a4 SC |
57 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
58 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
59 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
60 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
61 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
62 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
57f4f925 | 63 | |
bc9fb572 | 64 | // old style border flags |
3ff066a4 SC |
65 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
66 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
67 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
68 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
69 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 70 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
71 | |
72 | // standard window styles | |
3ff066a4 SC |
73 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
74 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
75 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
76 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 77 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
78 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
79 | wxFLAGS_MEMBER(wxVSCROLL) | |
80 | wxFLAGS_MEMBER(wxHSCROLL) | |
bc9fb572 | 81 | |
3ff066a4 | 82 | wxEND_FLAGS( wxStaticBoxStyle ) |
bc9fb572 | 83 | |
51741307 SC |
84 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h") |
85 | ||
3ff066a4 | 86 | wxBEGIN_PROPERTIES_TABLE(wxStaticBox) |
57f4f925 | 87 | wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
af498247 | 88 | wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
066f1b7a | 89 | /* |
57f4f925 WS |
90 | TODO PROPERTIES : |
91 | label | |
066f1b7a | 92 | */ |
3ff066a4 | 93 | wxEND_PROPERTIES_TABLE() |
51741307 | 94 | |
3ff066a4 SC |
95 | wxBEGIN_HANDLERS_TABLE(wxStaticBox) |
96 | wxEND_HANDLERS_TABLE() | |
51741307 | 97 | |
57f4f925 | 98 | wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
51741307 SC |
99 | #else |
100 | IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl) | |
101 | #endif | |
2bda0e17 | 102 | |
3f2711d5 VZ |
103 | // ============================================================================ |
104 | // implementation | |
105 | // ============================================================================ | |
106 | ||
107 | // ---------------------------------------------------------------------------- | |
108 | // wxStaticBox | |
109 | // ---------------------------------------------------------------------------- | |
110 | ||
111 | bool wxStaticBox::Create(wxWindow *parent, | |
112 | wxWindowID id, | |
113 | const wxString& label, | |
114 | const wxPoint& pos, | |
115 | const wxSize& size, | |
116 | long style, | |
117 | const wxString& name) | |
2bda0e17 | 118 | { |
11b6a93b | 119 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
57f4f925 | 120 | return false; |
2bda0e17 | 121 | |
e31e6ea7 | 122 | if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) ) |
57f4f925 | 123 | return false; |
2bda0e17 | 124 | |
9f769708 | 125 | #ifndef __WXWINCE__ |
3b6e5fb3 | 126 | Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint)); |
c3732409 | 127 | #endif // !__WXWINCE__ |
3b6e5fb3 | 128 | |
57f4f925 | 129 | return true; |
2bda0e17 KB |
130 | } |
131 | ||
9f769708 JS |
132 | wxBorder wxStaticBox::GetDefaultBorder() const |
133 | { | |
134 | return wxBORDER_NONE; | |
135 | } | |
136 | ||
e31e6ea7 VZ |
137 | WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const |
138 | { | |
139 | long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle); | |
140 | ||
eba99da4 JS |
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 | ||
e31e6ea7 | 145 | if ( exstyle ) |
a83b5b74 VZ |
146 | *exstyle = 0; |
147 | ||
e31e6ea7 VZ |
148 | return styleWin | BS_GROUPBOX; |
149 | } | |
150 | ||
f68586e5 | 151 | wxSize wxStaticBox::DoGetBestSize() const |
2bda0e17 | 152 | { |
4438caf4 | 153 | int cx, cy; |
7a5e53ab | 154 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
2bda0e17 | 155 | |
4438caf4 VZ |
156 | int wBox; |
157 | GetTextExtent(wxGetWindowText(m_hWnd), &wBox, &cy); | |
2bda0e17 | 158 | |
4438caf4 VZ |
159 | wBox += 3*cx; |
160 | int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
2bda0e17 | 161 | |
4438caf4 VZ |
162 | return wxSize(wBox, hBox); |
163 | } | |
2bda0e17 | 164 | |
c3732409 | 165 | void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const |
6aa01033 | 166 | { |
c3732409 VZ |
167 | wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther); |
168 | ||
169 | // need extra space, don't know how much but this seems to be enough | |
170 | *borderTop += GetCharHeight()/3; | |
171 | } | |
172 | ||
173 | // all the hacks below are not necessary for WinCE | |
abf912c5 | 174 | #ifndef __WXWINCE__ |
c3732409 VZ |
175 | |
176 | WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
177 | { | |
58385af0 | 178 | if ( nMsg == WM_NCHITTEST ) |
6aa01033 | 179 | { |
58385af0 VZ |
180 | // This code breaks some other processing such as enter/leave tracking |
181 | // so it's off by default. | |
182 | ||
183 | static int s_useHTClient = -1; | |
184 | if (s_useHTClient == -1) | |
185 | s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient")); | |
186 | if (s_useHTClient == 1) | |
187 | { | |
c3732409 VZ |
188 | int xPos = GET_X_LPARAM(lParam); |
189 | int yPos = GET_Y_LPARAM(lParam); | |
58385af0 VZ |
190 | |
191 | ScreenToClient(&xPos, &yPos); | |
192 | ||
193 | // Make sure you can drag by the top of the groupbox, but let | |
194 | // other (enclosed) controls get mouse events also | |
195 | if ( yPos < 10 ) | |
196 | return (long)HTCLIENT; | |
197 | } | |
6aa01033 JS |
198 | } |
199 | ||
200 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
201 | } | |
202 | ||
c3732409 VZ |
203 | // ---------------------------------------------------------------------------- |
204 | // static box drawing | |
205 | // ---------------------------------------------------------------------------- | |
58385af0 | 206 | |
c3732409 VZ |
207 | /* |
208 | We draw the static box ourselves because it's the only way to prevent it | |
209 | from flickering horribly on resize (because everything inside the box is | |
210 | erased twice: once when the box itself is repainted and second time when | |
211 | the control inside it is repainted) without using WS_EX_TRANSPARENT style as | |
212 | we used to do and which resulted in other problems. | |
213 | */ | |
58385af0 | 214 | |
3b6e5fb3 VZ |
215 | // MSWGetRegionWithoutSelf helper: removes the given rectangle from region |
216 | static inline void | |
217 | SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom) | |
218 | { | |
219 | AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom)); | |
220 | if ( !hrgnRect ) | |
221 | { | |
222 | wxLogLastError(_T("CreateRectRgn()")); | |
223 | return; | |
224 | } | |
225 | ||
226 | ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF); | |
227 | } | |
228 | ||
229 | void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h) | |
eba99da4 | 230 | { |
3b6e5fb3 VZ |
231 | HRGN hrgn = (HRGN)hRgn; |
232 | ||
233 | // remove the area occupied by the static box borders from the region | |
234 | int borderTop, border; | |
235 | GetBordersForSizer(&borderTop, &border); | |
eba99da4 JS |
236 | |
237 | // top | |
3b6e5fb3 | 238 | SubtractRectFromRgn(hrgn, 0, 0, w, borderTop); |
eba99da4 JS |
239 | |
240 | // bottom | |
3b6e5fb3 | 241 | SubtractRectFromRgn(hrgn, 0, h - border, w, h); |
eba99da4 JS |
242 | |
243 | // left | |
3b6e5fb3 | 244 | SubtractRectFromRgn(hrgn, 0, 0, border, h); |
eba99da4 JS |
245 | |
246 | // right | |
3b6e5fb3 | 247 | SubtractRectFromRgn(hrgn, w - border, 0, w, h); |
eba99da4 JS |
248 | } |
249 | ||
3b6e5fb3 | 250 | WXHRGN wxStaticBox::MSWGetRegionWithoutChildren() |
eba99da4 JS |
251 | { |
252 | RECT rc; | |
253 | ::GetWindowRect(GetHwnd(), &rc); | |
254 | HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1); | |
3b6e5fb3 VZ |
255 | |
256 | // iterate over all child windows (not just wxWindows but all windows) | |
257 | for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD); | |
258 | child; | |
259 | child = ::GetWindow(child, GW_HWNDNEXT) ) | |
eba99da4 | 260 | { |
3b6e5fb3 | 261 | wxWindow *childWindow = wxGetWindowFromHWND((WXHWND) child); |
eba99da4 JS |
262 | |
263 | // can't just test for (this != child) here since if a wxStaticBox | |
264 | // overlaps another wxStaticBox then neither are drawn. The overlapping | |
265 | // region will flicker but we shouldn't have overlapping windows anyway. | |
3b6e5fb3 | 266 | if ( !childWindow || !wxDynamicCast(childWindow, wxStaticBox) ) |
eba99da4 | 267 | { |
54c5498d | 268 | ::GetWindowRect(child, &rc); |
3b6e5fb3 | 269 | if ( ::RectInRegion(hrgn, &rc) ) |
eba99da4 JS |
270 | { |
271 | // need to remove WS_CLIPSIBLINGS from all sibling windows | |
272 | // that are within this staticbox if set | |
54c5498d | 273 | LONG style = ::GetWindowLong(child, GWL_STYLE); |
eba99da4 JS |
274 | if ( style & WS_CLIPSIBLINGS ) |
275 | { | |
276 | style &= ~WS_CLIPSIBLINGS; | |
54c5498d | 277 | ::SetWindowLong(child, GWL_STYLE, style); |
eba99da4 JS |
278 | |
279 | // MSDN: "If you have changed certain window data using | |
280 | // SetWindowLong, you must call SetWindowPos to have the | |
281 | // changes take effect." | |
54c5498d | 282 | ::SetWindowPos(child, NULL, 0, 0, 0, 0, |
eba99da4 JS |
283 | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | |
284 | SWP_FRAMECHANGED); | |
285 | } | |
286 | ||
3b6e5fb3 VZ |
287 | AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc)); |
288 | ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF); | |
eba99da4 JS |
289 | } |
290 | } | |
eba99da4 | 291 | } |
eba99da4 | 292 | |
3b6e5fb3 VZ |
293 | return (WXHRGN)hrgn; |
294 | } | |
295 | ||
c3732409 VZ |
296 | // helper for OnPaint(): really erase the background, i.e. do it even if we |
297 | // don't have any non default brush for doing it (DoEraseBackground() doesn't | |
298 | // do anything in such case) | |
3b6e5fb3 VZ |
299 | void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc) |
300 | { | |
c3732409 VZ |
301 | // note that we do not use the box background colour here, it shouldn't |
302 | // apply to its interior for several reasons: | |
303 | // 1. wxGTK doesn't do it | |
304 | // 2. controls inside the box don't get correct bg colour because they | |
305 | // are not our children so we'd have some really ugly colour mix if | |
306 | // we did it | |
307 | // 3. this is backwards compatible behaviour and some people rely on it, | |
308 | // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es | |
309 | wxWindow *parent = GetParent(); | |
bcb8ba61 | 310 | HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), GetHWND()); |
c3732409 VZ |
311 | |
312 | // if there is no special brush for painting this control, just use the | |
313 | // solid background colour | |
314 | wxBrush brush; | |
3b6e5fb3 VZ |
315 | if ( !hbr ) |
316 | { | |
c3732409 VZ |
317 | brush = wxBrush(parent->GetBackgroundColour()); |
318 | hbr = GetHbrushOf(brush); | |
3b6e5fb3 VZ |
319 | } |
320 | ||
c3732409 | 321 | ::FillRect(GetHdcOf(dc), &rc, hbr); |
eba99da4 JS |
322 | } |
323 | ||
324 | void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
325 | { | |
eba99da4 JS |
326 | RECT rc; |
327 | ::GetClientRect(GetHwnd(), &rc); | |
328 | ||
c3732409 | 329 | // draw the entire box in a memory DC |
eba99da4 JS |
330 | wxMemoryDC memdc; |
331 | wxBitmap bitmap(rc.right, rc.bottom); | |
332 | memdc.SelectObject(bitmap); | |
333 | ||
3b6e5fb3 | 334 | PaintBackground(memdc, rc); |
c3732409 VZ |
335 | |
336 | // NB: neither setting the text colour nor transparent background mode | |
337 | // doesn't change anything: the static box def window proc still | |
338 | // draws the label in its own colours, so if we want to have control | |
339 | // over this we really have to draw everything ourselves | |
eba99da4 JS |
340 | MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(memdc), 0); |
341 | ||
c3732409 VZ |
342 | |
343 | // now only blit the static box border itself, not the interior, to avoid | |
344 | // flicker when background is drawn below | |
345 | // | |
346 | // note that it seems to be faster to do 4 small blits here and then paint | |
347 | // directly into wxPaintDC than painting background in wxMemoryDC and then | |
348 | // blitting everything at once to wxPaintDC, this is why we do it like this | |
349 | wxPaintDC dc(this); | |
3b6e5fb3 VZ |
350 | int borderTop, border; |
351 | GetBordersForSizer(&borderTop, &border); | |
352 | ||
eba99da4 | 353 | // top |
3b6e5fb3 VZ |
354 | dc.Blit(border, 0, rc.right - border, borderTop, |
355 | &memdc, border, 0); | |
eba99da4 | 356 | // bottom |
3b6e5fb3 VZ |
357 | dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom, |
358 | &memdc, border, rc.bottom - border); | |
eba99da4 | 359 | // left |
3b6e5fb3 VZ |
360 | dc.Blit(0, 0, border, rc.bottom, |
361 | &memdc, 0, 0); | |
eba99da4 | 362 | // right |
3b6e5fb3 VZ |
363 | dc.Blit(rc.right - border, 0, rc.right, rc.bottom, |
364 | &memdc, rc.right - border, 0); | |
eba99da4 | 365 | |
c3732409 VZ |
366 | |
367 | // create the region excluding box children | |
3b6e5fb3 VZ |
368 | AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren()); |
369 | RECT rcWin; | |
370 | ::GetWindowRect(GetHwnd(), &rcWin); | |
371 | ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top); | |
eba99da4 | 372 | |
c3732409 | 373 | // and also the box itself |
3b6e5fb3 | 374 | MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom); |
c3732409 | 375 | HDCClipper clipToBg(GetHdcOf(dc), hrgn); |
3b6e5fb3 | 376 | |
c3732409 | 377 | // paint the inside of the box (excluding box itself and child controls) |
3b6e5fb3 | 378 | PaintBackground(dc, rc); |
eba99da4 JS |
379 | } |
380 | ||
c3732409 VZ |
381 | #endif // !__WXWINCE__ |
382 | ||
1e6feb95 | 383 | #endif // wxUSE_STATBOX |
6aa01033 | 384 |