]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
Implement GetValue() and SetValue() and send an event when clicked
[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& 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 int x = pos.x == -1 ? 0 : pos.x,
110 y = pos.y == -1 ? 0 : pos.y,
111 w = size.x == -1 ? 0 : size.x,
112 h = size.y == -1 ? 0 : size.y;
113
114 m_hWnd = (WXHWND)::CreateWindowEx
115 (
116 exstyle, // extended style
117 classname, // the kind of control to create
118 label, // the window name
119 style, // the window style
120 x, y, w, h, // the window position and size
121 GetHwndOf(GetParent()), // parent
122 (HMENU)GetId(), // child id
123 wxGetInstance(), // app instance
124 NULL // creation parameters
125 );
126
127 if ( !m_hWnd )
128 {
129 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
130 wxFAIL_MSG(_T("something is very wrong"));
131
132 return FALSE;
133 }
134
135 #if wxUSE_CTL3D
136 if ( want3D )
137 {
138 Ctl3dSubclassCtl(GetHwnd());
139 m_useCtl3D = TRUE;
140 }
141 #endif // wxUSE_CTL3D
142
143 // install wxWindows window proc for this window
144 SubclassWin(m_hWnd);
145
146 // controls use the same font and colours as their parent dialog by default
147 InheritAttributes();
148
149 // set the size now if no initial size specified
150 if ( w <= 0 || h <= 0 )
151 {
152 SetBestSize(size);
153 }
154
155 return TRUE;
156 }
157
158 wxBorder wxControl::GetDefaultBorder() const
159 {
160 // we want to automatically give controls a sunken style (confusingly,
161 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
162 // which is not sunken at all under Windows XP -- rather, just the default)
163 return wxBORDER_SUNKEN;
164 }
165
166 wxSize wxControl::DoGetBestSize() const
167 {
168 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
169 }
170
171 bool wxControl::ProcessCommand(wxCommandEvent& event)
172 {
173 return GetEventHandler()->ProcessEvent(event);
174 }
175
176 #ifdef __WIN95__
177 bool wxControl::MSWOnNotify(int idCtrl,
178 WXLPARAM lParam,
179 WXLPARAM* result)
180 {
181 wxEventType eventType = wxEVT_NULL;
182
183 NMHDR *hdr = (NMHDR*) lParam;
184 switch ( hdr->code )
185 {
186 case NM_CLICK:
187 eventType = wxEVT_COMMAND_LEFT_CLICK;
188 break;
189
190 case NM_DBLCLK:
191 eventType = wxEVT_COMMAND_LEFT_DCLICK;
192 break;
193
194 case NM_RCLICK:
195 eventType = wxEVT_COMMAND_RIGHT_CLICK;
196 break;
197
198 case NM_RDBLCLK:
199 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
200 break;
201
202 case NM_SETFOCUS:
203 eventType = wxEVT_COMMAND_SET_FOCUS;
204 break;
205
206 case NM_KILLFOCUS:
207 eventType = wxEVT_COMMAND_KILL_FOCUS;
208 break;
209
210 case NM_RETURN:
211 eventType = wxEVT_COMMAND_ENTER;
212 break;
213
214 default:
215 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
216 }
217
218 wxCommandEvent event(wxEVT_NULL, m_windowId);
219 event.SetEventType(eventType);
220 event.SetEventObject(this);
221
222 return GetEventHandler()->ProcessEvent(event);
223 }
224 #endif // Win95
225
226 void wxControl::OnEraseBackground(wxEraseEvent& event)
227 {
228 // notice that this 'dumb' implementation may cause flicker for some of the
229 // controls in which case they should intercept wxEraseEvent and process it
230 // themselves somehow
231
232 RECT rect;
233 ::GetClientRect(GetHwnd(), &rect);
234
235 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
236
237 HDC hdc = GetHdcOf((*event.GetDC()));
238
239 #ifndef __WXWINCE__
240 int mode = ::SetMapMode(hdc, MM_TEXT);
241 #endif
242
243 ::FillRect(hdc, &rect, hBrush);
244 ::DeleteObject(hBrush);
245
246 #ifndef __WXWINCE__
247 ::SetMapMode(hdc, mode);
248 #endif
249 }
250
251 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
252 #if wxUSE_CTL3D
253 WXUINT message,
254 WXWPARAM wParam,
255 WXLPARAM lParam
256 #else
257 WXUINT WXUNUSED(message),
258 WXWPARAM WXUNUSED(wParam),
259 WXLPARAM WXUNUSED(lParam)
260 #endif
261 )
262 {
263 #if wxUSE_CTL3D
264 if ( m_useCtl3D )
265 {
266 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
267 return (WXHBRUSH) hbrush;
268 }
269 #endif // wxUSE_CTL3D
270
271 HDC hdc = (HDC)pDC;
272 wxColour colBack = GetBackgroundColour();
273
274 ::SetBkColor(hdc, wxColourToRGB(colBack));
275 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
276
277 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
278
279 return (WXHBRUSH)brush->GetResourceHandle();
280 }
281
282 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
283 {
284 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
285
286 if ( AcceptsFocus() )
287 {
288 msStyle |= WS_TABSTOP;
289 }
290
291 return msStyle;
292 }
293
294 // ---------------------------------------------------------------------------
295 // global functions
296 // ---------------------------------------------------------------------------
297
298 // Call this repeatedly for several wnds to find the overall size
299 // of the widget.
300 // Call it initially with -1 for all values in rect.
301 // Keep calling for other widgets, and rect will be modified
302 // to calculate largest bounding rectangle.
303 void wxFindMaxSize(WXHWND wnd, RECT *rect)
304 {
305 int left = rect->left;
306 int right = rect->right;
307 int top = rect->top;
308 int bottom = rect->bottom;
309
310 GetWindowRect((HWND) wnd, rect);
311
312 if (left < 0)
313 return;
314
315 if (left < rect->left)
316 rect->left = left;
317
318 if (right > rect->right)
319 rect->right = right;
320
321 if (top < rect->top)
322 rect->top = top;
323
324 if (bottom > rect->bottom)
325 rect->bottom = bottom;
326 }
327
328 #endif // wxUSE_CONTROLS