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