1. wxStaticBox doesn't draw over the underlying controls any more
[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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/dcclient.h"
34 #endif
35
36 #include "wx/statbox.h"
37
38 #include "wx/msw/private.h"
39
40 // ----------------------------------------------------------------------------
41 // wxWin macros
42 // ----------------------------------------------------------------------------
43
44 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
46
47 BEGIN_EVENT_TABLE(wxStaticBox, wxControl)
48 EVT_ERASE_BACKGROUND(wxStaticBox::OnEraseBackground)
49 END_EVENT_TABLE()
50
51 #endif
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 // ----------------------------------------------------------------------------
58 // wxStaticBox
59 // ----------------------------------------------------------------------------
60
61 bool wxStaticBox::Create(wxWindow *parent,
62 wxWindowID id,
63 const wxString& label,
64 const wxPoint& pos,
65 const wxSize& size,
66 long style,
67 const wxString& name)
68 {
69 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
70 return FALSE;
71
72 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX) )
73 return FALSE;
74
75 SetSize(pos.x, pos.y, size.x, size.y);
76
77 return TRUE;
78 }
79
80 wxSize wxStaticBox::DoGetBestSize()
81 {
82 int cx, cy;
83 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
84
85 int wBox;
86 GetTextExtent(wxGetWindowText(m_hWnd), &wBox, &cy);
87
88 wBox += 3*cx;
89 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
90
91 return wxSize(wBox, hBox);
92 }
93
94 WXHBRUSH wxStaticBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
95 WXUINT message,
96 WXWPARAM wParam,
97 WXLPARAM lParam)
98 {
99 #if wxUSE_CTL3D
100 if ( m_useCtl3D )
101 {
102 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
103 return (WXHBRUSH) hbrush;
104 }
105 #endif // wxUSE_CTL3D
106
107 HDC hdc = (HDC)pDC;
108 if (GetParent()->GetTransparentBackground())
109 SetBkMode(hdc, TRANSPARENT);
110 else
111 SetBkMode(hdc, OPAQUE);
112
113 const wxColour& colBack = GetBackgroundColour();
114 ::SetBkColor(hdc, wxColourToRGB(colBack));
115 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
116
117 wxBrush *brush= wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
118
119 return (WXHBRUSH)brush->GetResourceHandle();
120 }
121
122 // VZ: this is probably the most commented function in wxWindows, but I still
123 // don't understand what it does and why. Wouldn't it be better to _never_
124 // erase the background here? What would we lose if we didn't do it?
125 // (FIXME)
126
127 // Shouldn't erase the whole window, since the static box must only paint its
128 // outline.
129 void wxStaticBox::OnEraseBackground(wxEraseEvent& event)
130 {
131 // If we don't have this (call Default()), we don't paint the background properly.
132 // If we do have this, we seem to overwrite enclosed controls.
133 // Is it the WS_CLIPCHILDREN style that's causing the problems?
134 // Probably - without this style, the background of the window will show through,
135 // so the control doesn't have to paint it. The window background will always be
136 // painted before all other controls, therefore there are no problems with
137 // controls being hidden by the static box.
138 // So, if we could specify wxCLIP_CHILDREN in window, or not, we could optimise painting better.
139 // We would assume wxCLIP_CHILDREN in a frame and a scrolled window, but not in a panel.
140 // Is this too platform-specific?? What else can we do? Not a lot, since we have to pass
141 // this information from arbitrary wxWindow derivatives, and it depends on what you wish to
142 // do with the windows.
143 // Alternatively, just make sure that wxStaticBox is always at the back! There are probably
144 // few other circumstances where it matters about child clipping. But what about painting onto
145 // to panel, inside a groupbox? Doesn't appear, because the box wipes it out.
146 wxWindow *parent = GetParent();
147 if ( parent && parent->GetHWND() &&
148 (::GetWindowLong(GetHwndOf(parent), GWL_STYLE) & WS_CLIPCHILDREN) )
149 {
150 // TODO: May in fact need to generate a paint event for inside this
151 // control's rectangle, otherwise all controls are going to be clipped -
152 // ugh.
153
154 // let wxControl::OnEraseBackground() do the job
155 event.Skip();
156 }
157 //else: do *not* call event.Skip() or wxControl will erase the background
158 }
159
160 long wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
161 {
162 if ( nMsg == WM_NCHITTEST)
163 {
164 int xPos = LOWORD(lParam); // horizontal position of cursor
165 int yPos = HIWORD(lParam); // vertical position of cursor
166
167 ScreenToClient(&xPos, &yPos);
168
169 // Make sure you can drag by the top of the groupbox, but let
170 // other (enclosed) controls get mouse events also
171 if (yPos < 10)
172 return (long)HTCLIENT;
173 }
174
175 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
176 }
177