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