]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
Copied gsockmsw.c revision 1.20
[wxWidgets.git] / src / msw / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/control.cpp
3 // Purpose: wxControl class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "control.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 #if wxUSE_CONTROLS
24
25 #ifndef WX_PRECOMP
26 #include "wx/event.h"
27 #include "wx/app.h"
28 #include "wx/dcclient.h"
29 #include "wx/log.h"
30 #endif
31
32 #include "wx/control.h"
33
34 #include "wx/msw/private.h"
35
36 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
37 #include <commctrl.h>
38 #endif
39
40 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
41
42 BEGIN_EVENT_TABLE(wxControl, wxWindow)
43 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
44 END_EVENT_TABLE()
45
46 // Item members
47 wxControl::wxControl()
48 {
49 }
50
51 wxControl::~wxControl()
52 {
53 m_isBeingDeleted = TRUE;
54 }
55
56
57 bool wxControl::Create(wxWindow *parent,
58 wxWindowID id,
59 const wxPoint& pos,
60 const wxSize& size,
61 long style,
62 const wxValidator& wxVALIDATOR_PARAM(validator),
63 const wxString& name)
64 {
65 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
66 return FALSE;
67
68 #if wxUSE_VALIDATORS
69 SetValidator(validator);
70 #endif
71
72 return TRUE;
73 }
74
75 bool wxControl::MSWCreateControl(const wxChar *classname,
76 const wxString& label,
77 const wxPoint& pos,
78 const wxSize& size)
79 {
80 WXDWORD exstyle;
81 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle);
82
83 return MSWCreateControl(classname, msStyle, pos, size, label, exstyle);
84 }
85
86 bool wxControl::MSWCreateControl(const wxChar *classname,
87 WXDWORD style,
88 const wxPoint& pos,
89 const wxSize& size,
90 const wxString& label,
91 WXDWORD exstyle)
92 {
93 // if no extended style given, determine it ourselves
94 if ( exstyle == (WXDWORD)-1 )
95 {
96 exstyle = 0;
97 (void) MSWGetStyle(GetWindowStyle(), &exstyle);
98 }
99
100 // all controls should have this style
101 style |= WS_CHILD;
102
103 // create the control visible if it's currently shown for wxWindows
104 if ( m_isShown )
105 {
106 style |= WS_VISIBLE;
107 }
108
109 // choose the position for the control
110 int x = pos.x == -1 ? 0 : pos.x,
111 y = pos.y == -1 ? 0 : pos.y,
112 w = size.x == -1 ? 0 : size.x,
113 h = size.y == -1 ? 0 : size.y;
114
115 // ... and adjust it to account for ap ossible parent frames toolbar
116 AdjustForParentClientOrigin(x, y);
117
118 m_hWnd = (WXHWND)::CreateWindowEx
119 (
120 exstyle, // extended style
121 classname, // the kind of control to create
122 label, // the window name
123 style, // the window style
124 x, y, w, h, // the window position and size
125 GetHwndOf(GetParent()), // parent
126 (HMENU)GetId(), // child id
127 wxGetInstance(), // app instance
128 NULL // creation parameters
129 );
130
131 if ( !m_hWnd )
132 {
133 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
134 wxFAIL_MSG(_T("something is very wrong"));
135
136 return FALSE;
137 }
138
139 #if wxUSE_CTL3D
140 if ( want3D )
141 {
142 Ctl3dSubclassCtl(GetHwnd());
143 m_useCtl3D = TRUE;
144 }
145 #endif // wxUSE_CTL3D
146
147 // install wxWindows window proc for this window
148 SubclassWin(m_hWnd);
149
150 // controls use the same font and colours as their parent dialog by default
151 InheritAttributes();
152
153 // set the size now if no initial size specified
154 if ( w <= 0 || h <= 0 )
155 {
156 SetBestSize(size);
157 }
158
159 return TRUE;
160 }
161
162 wxBorder wxControl::GetDefaultBorder() const
163 {
164 // we want to automatically give controls a sunken style (confusingly,
165 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
166 // which is not sunken at all under Windows XP -- rather, just the default)
167 return wxBORDER_SUNKEN;
168 }
169
170 wxSize wxControl::DoGetBestSize() const
171 {
172 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
173 }
174
175 bool wxControl::ProcessCommand(wxCommandEvent& event)
176 {
177 return GetEventHandler()->ProcessEvent(event);
178 }
179
180 #ifdef __WIN95__
181 bool wxControl::MSWOnNotify(int idCtrl,
182 WXLPARAM lParam,
183 WXLPARAM* result)
184 {
185 wxEventType eventType = wxEVT_NULL;
186
187 NMHDR *hdr = (NMHDR*) lParam;
188 switch ( hdr->code )
189 {
190 case NM_CLICK:
191 eventType = wxEVT_COMMAND_LEFT_CLICK;
192 break;
193
194 case NM_DBLCLK:
195 eventType = wxEVT_COMMAND_LEFT_DCLICK;
196 break;
197
198 case NM_RCLICK:
199 eventType = wxEVT_COMMAND_RIGHT_CLICK;
200 break;
201
202 case NM_RDBLCLK:
203 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
204 break;
205
206 case NM_SETFOCUS:
207 eventType = wxEVT_COMMAND_SET_FOCUS;
208 break;
209
210 case NM_KILLFOCUS:
211 eventType = wxEVT_COMMAND_KILL_FOCUS;
212 break;
213
214 case NM_RETURN:
215 eventType = wxEVT_COMMAND_ENTER;
216 break;
217
218 default:
219 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
220 }
221
222 wxCommandEvent event(wxEVT_NULL, m_windowId);
223 event.SetEventType(eventType);
224 event.SetEventObject(this);
225
226 return GetEventHandler()->ProcessEvent(event);
227 }
228 #endif // Win95
229
230 void wxControl::OnEraseBackground(wxEraseEvent& event)
231 {
232 // notice that this 'dumb' implementation may cause flicker for some of the
233 // controls in which case they should intercept wxEraseEvent and process it
234 // themselves somehow
235
236 RECT rect;
237 ::GetClientRect(GetHwnd(), &rect);
238
239 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
240
241 HDC hdc = GetHdcOf((*event.GetDC()));
242
243 #ifndef __WXWINCE__
244 int mode = ::SetMapMode(hdc, MM_TEXT);
245 #endif
246
247 ::FillRect(hdc, &rect, hBrush);
248 ::DeleteObject(hBrush);
249
250 #ifndef __WXWINCE__
251 ::SetMapMode(hdc, mode);
252 #endif
253 }
254
255 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
256 #if wxUSE_CTL3D
257 WXUINT message,
258 WXWPARAM wParam,
259 WXLPARAM lParam
260 #else
261 WXUINT WXUNUSED(message),
262 WXWPARAM WXUNUSED(wParam),
263 WXLPARAM WXUNUSED(lParam)
264 #endif
265 )
266 {
267 #if wxUSE_CTL3D
268 if ( m_useCtl3D )
269 {
270 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
271 return (WXHBRUSH) hbrush;
272 }
273 #endif // wxUSE_CTL3D
274
275 HDC hdc = (HDC)pDC;
276 wxColour colBack = GetBackgroundColour();
277
278 ::SetBkColor(hdc, wxColourToRGB(colBack));
279 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
280
281 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
282
283 return (WXHBRUSH)brush->GetResourceHandle();
284 }
285
286 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
287 {
288 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
289
290 if ( AcceptsFocus() )
291 {
292 msStyle |= WS_TABSTOP;
293 }
294
295 return msStyle;
296 }
297
298 // ---------------------------------------------------------------------------
299 // global functions
300 // ---------------------------------------------------------------------------
301
302 // Call this repeatedly for several wnds to find the overall size
303 // of the widget.
304 // Call it initially with -1 for all values in rect.
305 // Keep calling for other widgets, and rect will be modified
306 // to calculate largest bounding rectangle.
307 void wxFindMaxSize(WXHWND wnd, RECT *rect)
308 {
309 int left = rect->left;
310 int right = rect->right;
311 int top = rect->top;
312 int bottom = rect->bottom;
313
314 GetWindowRect((HWND) wnd, rect);
315
316 if (left < 0)
317 return;
318
319 if (left < rect->left)
320 rect->left = left;
321
322 if (right > rect->right)
323 rect->right = right;
324
325 if (top < rect->top)
326 rect->top = top;
327
328 if (bottom > rect->bottom)
329 rect->bottom = bottom;
330 }
331
332 #endif // wxUSE_CONTROLS