]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
Cured problem introduced by LEAVE/ENTER OnIdle code; bugs in gauge sizing
[wxWidgets.git] / src / msw / statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation "statbox.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/dcclient.h"
25 #include "wx/app.h"
26 #endif
27
28 #include "wx/statbox.h"
29 #include "wx/msw/private.h"
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
33
34 BEGIN_EVENT_TABLE(wxStaticBox, wxControl)
35 EVT_ERASE_BACKGROUND(wxStaticBox::OnEraseBackground)
36 END_EVENT_TABLE()
37
38 #endif
39
40 /*
41 * Group box
42 */
43
44 bool wxStaticBox::Create(wxWindow *parent, const wxWindowID id,
45 const wxString& label,
46 const wxPoint& pos,
47 const wxSize& size,
48 const long style,
49 const wxString& name)
50 {
51 SetName(name);
52
53 if (parent) parent->AddChild(this);
54
55 SetBackgroundColour(parent->GetDefaultBackgroundColour()) ;
56 SetForegroundColour(parent->GetDefaultForegroundColour()) ;
57
58 if ( id == -1 )
59 m_windowId = (int)NewControlId();
60 else
61 m_windowId = id;
62
63 int x = pos.x;
64 int y = pos.y;
65 int width = size.x;
66 int height = size.y;
67
68 m_windowStyle = style;
69
70 long msStyle = BS_GROUPBOX | WS_CHILD | WS_VISIBLE ; // GROUP_FLAGS;
71
72 bool want3D;
73 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
74
75 HWND wx_button =
76 CreateWindowEx(exStyle, "BUTTON", (const char *)label, msStyle,
77 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
78 wxGetInstance(), NULL);
79 #if CTL3D
80 if (want3D)
81 {
82 Ctl3dSubclassCtl(wx_button);
83 m_useCtl3D = TRUE;
84 }
85 #endif
86
87 m_hWnd = (WXHWND)wx_button;
88
89 // Subclass again for purposes of dialog editing mode
90 SubclassWin(GetHWND());
91
92 SetFont(* parent->GetFont());
93
94 SetSize(x, y, width, height);
95 ShowWindow(wx_button, SW_SHOW);
96
97 return TRUE;
98 }
99
100 void wxStaticBox::SetLabel(const wxString& label)
101 {
102 SetWindowText((HWND)m_hWnd, (const char *)label);
103 }
104
105 void wxStaticBox::SetSize(const int x, const int y, const int width, const int height, const int sizeFlags)
106 {
107 int currentX, currentY;
108 GetPosition(&currentX, &currentY);
109
110 int x1 = x;
111 int y1 = y;
112 int w1 = width;
113 int h1 = height;
114
115 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
116 x1 = currentX;
117 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
118 y1 = currentY;
119
120 // If we're prepared to use the existing size, then...
121 if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO))
122 {
123 GetSize(&w1, &h1);
124 }
125
126 char buf[300];
127
128 float current_width;
129
130 int cx;
131 int cy;
132 float cyf;
133
134 HWND button = (HWND)m_hWnd;
135 wxGetCharSize(GetHWND(), &cx, &cy,GetFont());
136
137 GetWindowText(button, buf, 300);
138 GetTextExtent(buf, &current_width, &cyf,NULL,NULL,GetFont());
139 if (w1 < 0)
140 w1 = (int)(current_width + 3*cx) ;
141 if (h1<0)
142 h1 = (int)(cyf*EDIT_CONTROL_FACTOR) ;
143 MoveWindow(button, x1, y1, w1, h1, TRUE);
144
145 #if WXWIN_COMPATIBILITY
146 GetEventHandler()->OldOnSize(width, height);
147 #else
148 wxSizeEvent event(wxSize(width, height), m_windowId);
149 event.eventObject = this;
150 GetEventHandler()->ProcessEvent(event);
151 #endif
152 }
153
154 WXHBRUSH wxStaticBox::OnCtlColor(const WXHDC pDC, const WXHWND pWnd, const WXUINT nCtlColor,
155 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
156 {
157 #if CTL3D
158 if ( m_useCtl3D )
159 {
160 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
161 return (WXHBRUSH) hbrush;
162 }
163 #endif
164
165 if (GetParent()->GetTransparentBackground())
166 SetBkMode((HDC) pDC, TRANSPARENT);
167 else
168 SetBkMode((HDC) pDC, OPAQUE);
169
170 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
171 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
172
173 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
174
175 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
176 // has a zero usage count.
177 // backgroundBrush->RealizeResource();
178 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
179 }
180
181 // Shouldn't erase the whole window, since the static box must only paint its
182 // outline.
183 void wxStaticBox::OnEraseBackground(wxEraseEvent& event)
184 {
185 // If we don't have this (call Default()), we don't paint the background properly.
186 // If we do have this, we seem to overwrite enclosed controls.
187 // Is it the WS_CLIPCHILDREN style that's causing the problems?
188 // Probably - without this style, the background of the window will show through,
189 // so the control doesn't have to paint it. The window background will always be
190 // painted before all other controls, therefore there are no problems with
191 // controls being hidden by the static box.
192 // So, if we could specify wxCLIP_CHILDREN in window, or not, we could optimise painting better.
193 // We would assume wxCLIP_CHILDREN in a frame and a scrolled window, but not in a panel.
194 // Is this too platform-specific?? What else can we do? Not a lot, since we have to pass
195 // this information from arbitrary wxWindow derivatives, and it depends on what you wish to
196 // do with the windows.
197 // Alternatively, just make sure that wxStaticBox is always at the back! There are probably
198 // few other circumstances where it matters about child clipping. But what about painting onto
199 // to panel, inside a groupbox? Doesn't appear, because the box wipes it out.
200 wxWindow *parent = GetParent();
201 if ( parent && parent->GetHWND() && (::GetWindowLong((HWND) parent->GetHWND(), GWL_STYLE) & WS_CLIPCHILDREN) )
202 {
203 // TODO: May in fact need to generate a paint event for inside this
204 // control's rectangle, otherwise all controls are going to be clipped -
205 // ugh.
206 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
207 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
208
209 RECT rect;
210
211 ::GetClientRect((HWND) GetHWND(), &rect);
212 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
213 ::DeleteObject(hBrush);
214 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
215 }
216 else
217 Default();
218 }
219
220 long wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
221 {
222 // TODO: somehow, this has to accept mouse clicks in user interface edit mode,
223 // but not otherwise. Only there is no longer a UI edit mode...
224
225 // It worked before because the message could be processed if not in UI
226 // edit mode. We have to find some way of distinguishing this.
227 // Maybe this class can have an AcceptMouseEvents(bool) function; a sort of
228 // kludge... or, we can search for an active event table entry that will
229 // intercept mouse events, and if one exists (that isn't the default),
230 // skip the code below. Too time consuming though.
231 // Perhaps it's ok to do the default thing *anyway* because the title or edge
232 // of the window may still be active!
233 // if (nMsg == WM_NCHITTEST)
234 // return Default();
235
236 if (nMsg == WM_NCHITTEST)
237 {
238 int xPos = LOWORD(lParam); // horizontal position of cursor
239 int yPos = HIWORD(lParam); // vertical position of cursor
240
241 ScreenToClient(&xPos, &yPos);
242
243 // Make sure you can drag by the top of the groupbox, but let
244 // other (enclosed) controls get mouse events also
245 if (yPos < 10)
246 return (long)HTCLIENT;
247 }
248
249 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
250 }
251