]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinctrl.cpp
changed char event handling for ctrl-chars and skipped events
[wxWidgets.git] / src / msw / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 22.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "spinctrlbase.h"
18 #pragma implementation "spinctrl.h"
19 #endif
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #endif
35
36 #if wxUSE_SPINCTRL
37
38 #if defined(__WIN95__)
39
40 #include "wx/spinctrl.h"
41 #include "wx/msw/private.h"
42
43 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
44 #include <commctrl.h>
45 #endif
46
47 #include <limits.h> // for INT_MIN
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
54
55 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
56 EVT_CHAR(wxSpinCtrl::OnChar)
57 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
58 END_EVENT_TABLE()
59
60 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
61
62 // ----------------------------------------------------------------------------
63 // constants
64 // ----------------------------------------------------------------------------
65
66 // the margin between the up-down control and its buddy (can be arbitrary,
67 // choose what you like - or may be decide during run-time depending on the
68 // font size?)
69 static const int MARGIN_BETWEEN = 1;
70
71 // ============================================================================
72 // implementation
73 // ============================================================================
74
75 wxArraySpins wxSpinCtrl::ms_allSpins;
76
77 // ----------------------------------------------------------------------------
78 // wnd proc for the buddy text ctrl
79 // ----------------------------------------------------------------------------
80
81 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
82 UINT message,
83 WPARAM wParam,
84 LPARAM lParam)
85 {
86 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA);
87
88 // forward some messages (the key and focus ones only so far) to
89 // the spin ctrl
90 switch ( message )
91 {
92 case WM_SETFOCUS:
93 case WM_KILLFOCUS:
94 case WM_CHAR:
95 case WM_DEADCHAR:
96 case WM_KEYUP:
97 case WM_KEYDOWN:
98 spin->MSWWindowProc(message, wParam, lParam);
99
100 // The control may have been deleted at this point, so check.
101 if (!(::IsWindow(hwnd) && ((wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA)) == spin))
102 return 0;
103 break;
104
105 case WM_GETDLGCODE:
106 // we want to get WXK_RETURN in order to generate the event for it
107 return DLGC_WANTCHARS;
108 }
109
110 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
111 hwnd, message, wParam, lParam);
112 }
113
114 /* static */
115 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
116 {
117 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong((HWND)hwndBuddy,
118 GWL_USERDATA);
119
120 int i = ms_allSpins.Index(spin);
121
122 if ( i == wxNOT_FOUND )
123 return NULL;
124
125 // sanity check
126 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
127 _T("wxSpinCtrl has incorrect buddy HWND!") );
128
129 return spin;
130 }
131
132 // process a WM_COMMAND generated by the buddy text control
133 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
134 {
135 switch (cmd)
136 {
137 case EN_CHANGE:
138 {
139 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
140 event.SetEventObject(this);
141 wxString val = wxGetWindowText(m_hwndBuddy);
142 event.SetString(val);
143 event.SetInt(GetValue());
144 return GetEventHandler()->ProcessEvent(event);
145 }
146 case EN_SETFOCUS:
147 case EN_KILLFOCUS:
148 {
149 wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
150 : wxEVT_SET_FOCUS,
151 m_windowId);
152 event.SetEventObject( this );
153 return GetEventHandler()->ProcessEvent(event);
154 }
155 default:
156 break;
157 }
158
159 // not processed
160 return FALSE;
161 }
162
163 void wxSpinCtrl::OnChar(wxKeyEvent& event)
164 {
165 switch ( event.KeyCode() )
166 {
167 case WXK_RETURN:
168 {
169 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
170 InitCommandEvent(event);
171 wxString val = wxGetWindowText(m_hwndBuddy);
172 event.SetString(val);
173 event.SetInt(GetValue());
174 if ( GetEventHandler()->ProcessEvent(event) )
175 return;
176 break;
177 }
178
179 case WXK_TAB:
180 // always produce navigation event - even if we process TAB
181 // ourselves the fact that we got here means that the user code
182 // decided to skip processing of this TAB - probably to let it
183 // do its default job.
184 {
185 wxNavigationKeyEvent eventNav;
186 eventNav.SetDirection(!event.ShiftDown());
187 eventNav.SetWindowChange(event.ControlDown());
188 eventNav.SetEventObject(this);
189
190 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
191 return;
192 }
193 break;
194 }
195
196 // no, we didn't process it
197 event.Skip();
198 }
199
200 // ----------------------------------------------------------------------------
201 // construction
202 // ----------------------------------------------------------------------------
203
204 bool wxSpinCtrl::Create(wxWindow *parent,
205 wxWindowID id,
206 const wxString& value,
207 const wxPoint& pos,
208 const wxSize& size,
209 long style,
210 int min, int max, int initial,
211 const wxString& name)
212 {
213 // before using DoGetBestSize(), have to set style to let the base class
214 // know whether this is a horizontal or vertical control (we're always
215 // vertical)
216 style |= wxSP_VERTICAL;
217 SetWindowStyle(style);
218
219 // calculate the sizes: the size given is the toal size for both controls
220 // and we need to fit them both in the given width (height is the same)
221 wxSize sizeText(size), sizeBtn(size);
222 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
223 if ( sizeText.x <= 0 )
224 {
225 // DEFAULT_ITEM_WIDTH is the default width for the text control
226 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
227 }
228
229 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
230 if ( sizeText.x <= 0 )
231 {
232 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
233 }
234
235 wxPoint posBtn(pos);
236 posBtn.x += sizeText.x + MARGIN_BETWEEN;
237
238 // create the spin button
239 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
240 {
241 return FALSE;
242 }
243
244 SetRange(min, max);
245 SetValue(initial);
246
247 bool want3D;
248 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
249 int msStyle = WS_CHILD;
250
251 // Even with extended styles, need to combine with WS_BORDER for them to
252 // look right.
253 if ( want3D || wxStyleHasBorder(style) )
254 msStyle |= WS_BORDER;
255
256 if ( style & wxCLIP_SIBLINGS )
257 msStyle |= WS_CLIPSIBLINGS;
258
259 // create the text window
260 m_hwndBuddy = (WXHWND)::CreateWindowEx
261 (
262 exStyle, // sunken border
263 _T("EDIT"), // window class
264 NULL, // no window title
265 msStyle /* | WS_CLIPSIBLINGS */, // style (will be shown later)
266 pos.x, pos.y, // position
267 0, 0, // size (will be set later)
268 GetHwndOf(parent), // parent
269 (HMENU)-1, // control id
270 wxGetInstance(), // app instance
271 NULL // unused client data
272 );
273
274 if ( !m_hwndBuddy )
275 {
276 wxLogLastError(wxT("CreateWindow(buddy text window)"));
277
278 return FALSE;
279 }
280
281 // subclass the text ctrl to be able to intercept some events
282 m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC);
283 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this);
284 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc);
285
286 // should have the same font as the other controls
287 SetFont(GetParent()->GetFont());
288
289 // set the size of the text window - can do it only now, because we
290 // couldn't call DoGetBestSize() before as font wasn't set
291 if ( sizeText.y <= 0 )
292 {
293 int cx, cy;
294 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
295
296 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
297 }
298
299 DoMoveWindow(pos.x, pos.y,
300 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
301
302 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
303
304 // associate the text window with the spin button
305 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
306
307 if ( !value.IsEmpty() )
308 {
309 SetValue(value);
310 }
311
312 // do it after finishing with m_hwndBuddy creation to avoid generating
313 // initial wxEVT_COMMAND_TEXT_UPDATED message
314 ms_allSpins.Add(this);
315
316 return TRUE;
317 }
318
319 wxSpinCtrl::~wxSpinCtrl()
320 {
321 ms_allSpins.Remove(this);
322
323 // This removes spurious memory leak reporting
324 if (ms_allSpins.GetCount() == 0)
325 ms_allSpins.Clear();
326
327 // destroy the buddy window because this pointer which wxBuddyTextWndProc
328 // uses will not soon be valid any more
329 ::DestroyWindow(GetBuddyHwnd());
330 }
331
332 // ----------------------------------------------------------------------------
333 // wxTextCtrl-like methods
334 // ----------------------------------------------------------------------------
335
336 void wxSpinCtrl::SetValue(const wxString& text)
337 {
338 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
339 {
340 wxLogLastError(wxT("SetWindowText(buddy)"));
341 }
342 }
343
344 int wxSpinCtrl::GetValue() const
345 {
346 wxString val = wxGetWindowText(m_hwndBuddy);
347
348 long n;
349 if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
350 n = INT_MIN;
351
352 return n;
353 }
354
355 // ----------------------------------------------------------------------------
356 // forward some methods to subcontrols
357 // ----------------------------------------------------------------------------
358
359 bool wxSpinCtrl::SetFont(const wxFont& font)
360 {
361 if ( !wxWindowBase::SetFont(font) )
362 {
363 // nothing to do
364 return FALSE;
365 }
366
367 WXHANDLE hFont = GetFont().GetResourceHandle();
368 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
369
370 return TRUE;
371 }
372
373 bool wxSpinCtrl::Show(bool show)
374 {
375 if ( !wxControl::Show(show) )
376 {
377 return FALSE;
378 }
379
380 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
381
382 return TRUE;
383 }
384
385 bool wxSpinCtrl::Enable(bool enable)
386 {
387 if ( !wxControl::Enable(enable) )
388 {
389 return FALSE;
390 }
391
392 ::EnableWindow(GetBuddyHwnd(), enable);
393
394 return TRUE;
395 }
396
397 void wxSpinCtrl::SetFocus()
398 {
399 ::SetFocus(GetBuddyHwnd());
400 }
401
402 // ----------------------------------------------------------------------------
403 // event processing
404 // ----------------------------------------------------------------------------
405
406 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
407 {
408 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
409 event.SetEventObject(this);
410 event.SetInt(eventSpin.GetPosition());
411
412 (void)GetEventHandler()->ProcessEvent(event);
413
414 if ( eventSpin.GetSkipped() )
415 {
416 event.Skip();
417 }
418 }
419
420 // ----------------------------------------------------------------------------
421 // size calculations
422 // ----------------------------------------------------------------------------
423
424 wxSize wxSpinCtrl::DoGetBestSize() const
425 {
426 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
427 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
428
429 int y;
430 wxGetCharSize(GetHWND(), NULL, &y, &GetFont());
431 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
432
433 if ( sizeBtn.y < y )
434 {
435 // make the text tall enough
436 sizeBtn.y = y;
437 }
438
439 return sizeBtn;
440 }
441
442 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
443 {
444 int widthBtn = wxSpinButton::DoGetBestSize().x;
445 int widthText = width - widthBtn - MARGIN_BETWEEN;
446 if ( widthText <= 0 )
447 {
448 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
449 }
450
451 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
452 {
453 wxLogLastError(wxT("MoveWindow(buddy)"));
454 }
455
456 x += widthText + MARGIN_BETWEEN;
457 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
458 {
459 wxLogLastError(wxT("MoveWindow"));
460 }
461 }
462
463 // get total size of the control
464 void wxSpinCtrl::DoGetSize(int *x, int *y) const
465 {
466 RECT spinrect, textrect, ctrlrect;
467 GetWindowRect(GetHwnd(), &spinrect);
468 GetWindowRect(GetBuddyHwnd(), &textrect);
469 UnionRect(&ctrlrect,&textrect, &spinrect);
470
471 if ( x )
472 *x = ctrlrect.right - ctrlrect.left;
473 if ( y )
474 *y = ctrlrect.bottom - ctrlrect.top;
475 }
476
477 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
478 {
479 // hack: pretend that our HWND is the text control just for a moment
480 WXHWND hWnd = GetHWND();
481 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
482
483 wxSpinButton::DoGetPosition(x, y);
484
485 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
486 }
487
488 #endif // __WIN95__
489
490 #endif
491 // wxUSE_SPINCTRL
492