switching back to normal accessors, workaround not needed anymore
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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(__CYGWIN10__))
44 #include <commctrl.h>
45 #endif
46
47 #include <limits.h> // for INT_MIN
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 #if wxUSE_EXTENDED_RTTI
54 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h")
55
56 WX_BEGIN_PROPERTIES_TABLE(wxSpinCtrl)
57 WX_PROPERTY( ValueString , wxString , SetValue , GetValue , ) ;
58 WX_PROPERTY( Value , int , SetValue, GetValue, 0 )
59 WX_PROPERTY( Min , int , SetMin, GetMin, 0 )
60 WX_PROPERTY( Max , int , SetMax, GetMax, 0 )
61 /*
62 TODO PROPERTIES
63 style wxSP_ARROW_KEYS
64 */
65 WX_END_PROPERTIES_TABLE()
66
67 WX_BEGIN_HANDLERS_TABLE(wxSpinCtrl)
68 WX_END_HANDLERS_TABLE()
69
70 WX_CONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle )
71 #else
72 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
73 #endif
74
75 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
76 EVT_CHAR(wxSpinCtrl::OnChar)
77
78 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
79
80 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
81 END_EVENT_TABLE()
82
83 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
84
85 // ----------------------------------------------------------------------------
86 // constants
87 // ----------------------------------------------------------------------------
88
89 // the margin between the up-down control and its buddy (can be arbitrary,
90 // choose what you like - or may be decide during run-time depending on the
91 // font size?)
92 static const int MARGIN_BETWEEN = 1;
93
94 // ============================================================================
95 // implementation
96 // ============================================================================
97
98 wxArraySpins wxSpinCtrl::ms_allSpins;
99
100 // ----------------------------------------------------------------------------
101 // wnd proc for the buddy text ctrl
102 // ----------------------------------------------------------------------------
103
104 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
105 UINT message,
106 WPARAM wParam,
107 LPARAM lParam)
108 {
109 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA);
110
111 // forward some messages (the key and focus ones only so far) to
112 // the spin ctrl
113 switch ( message )
114 {
115 case WM_SETFOCUS:
116 // if the focus comes from the spin control itself, don't set it
117 // back to it -- we don't want to go into an infinite loop
118 if ( wParam == spin->GetHWND() )
119 break;
120 //else: fall through
121
122 case WM_KILLFOCUS:
123 case WM_CHAR:
124 case WM_DEADCHAR:
125 case WM_KEYUP:
126 case WM_KEYDOWN:
127 spin->MSWWindowProc(message, wParam, lParam);
128
129 // The control may have been deleted at this point, so check.
130 if (!(::IsWindow(hwnd) && ((wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA)) == spin))
131 return 0;
132 break;
133
134 case WM_GETDLGCODE:
135 // we want to get WXK_RETURN in order to generate the event for it
136 return DLGC_WANTCHARS;
137 }
138
139 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
140 hwnd, message, wParam, lParam);
141 }
142
143 /* static */
144 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
145 {
146 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong((HWND)hwndBuddy,
147 GWL_USERDATA);
148
149 int i = ms_allSpins.Index(spin);
150
151 if ( i == wxNOT_FOUND )
152 return NULL;
153
154 // sanity check
155 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
156 _T("wxSpinCtrl has incorrect buddy HWND!") );
157
158 return spin;
159 }
160
161 // process a WM_COMMAND generated by the buddy text control
162 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
163 {
164 switch (cmd)
165 {
166 case EN_CHANGE:
167 {
168 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
169 event.SetEventObject(this);
170 wxString val = wxGetWindowText(m_hwndBuddy);
171 event.SetString(val);
172 event.SetInt(GetValue());
173 return GetEventHandler()->ProcessEvent(event);
174 }
175 case EN_SETFOCUS:
176 case EN_KILLFOCUS:
177 {
178 wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
179 : wxEVT_SET_FOCUS,
180 m_windowId);
181 event.SetEventObject( this );
182 return GetEventHandler()->ProcessEvent(event);
183 }
184 default:
185 break;
186 }
187
188 // not processed
189 return FALSE;
190 }
191
192 void wxSpinCtrl::OnChar(wxKeyEvent& event)
193 {
194 switch ( event.GetKeyCode() )
195 {
196 case WXK_RETURN:
197 {
198 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
199 InitCommandEvent(event);
200 wxString val = wxGetWindowText(m_hwndBuddy);
201 event.SetString(val);
202 event.SetInt(GetValue());
203 if ( GetEventHandler()->ProcessEvent(event) )
204 return;
205 break;
206 }
207
208 case WXK_TAB:
209 // always produce navigation event - even if we process TAB
210 // ourselves the fact that we got here means that the user code
211 // decided to skip processing of this TAB - probably to let it
212 // do its default job.
213 {
214 wxNavigationKeyEvent eventNav;
215 eventNav.SetDirection(!event.ShiftDown());
216 eventNav.SetWindowChange(event.ControlDown());
217 eventNav.SetEventObject(this);
218
219 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
220 return;
221 }
222 break;
223 }
224
225 // no, we didn't process it
226 event.Skip();
227 }
228
229 void wxSpinCtrl::OnSetFocus(wxFocusEvent& event)
230 {
231 // when we get focus, give it to our buddy window as it needs it more than
232 // we do
233 ::SetFocus((HWND)m_hwndBuddy);
234
235 event.Skip();
236 }
237
238 // ----------------------------------------------------------------------------
239 // construction
240 // ----------------------------------------------------------------------------
241
242 bool wxSpinCtrl::Create(wxWindow *parent,
243 wxWindowID id,
244 const wxString& value,
245 const wxPoint& pos,
246 const wxSize& size,
247 long style,
248 int min, int max, int initial,
249 const wxString& name)
250 {
251 // before using DoGetBestSize(), have to set style to let the base class
252 // know whether this is a horizontal or vertical control (we're always
253 // vertical)
254 style |= wxSP_VERTICAL;
255
256 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
257 style |= wxBORDER_SUNKEN;
258
259 SetWindowStyle(style);
260
261 WXDWORD exStyle = 0;
262 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
263
264 // calculate the sizes: the size given is the toal size for both controls
265 // and we need to fit them both in the given width (height is the same)
266 wxSize sizeText(size), sizeBtn(size);
267 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
268 if ( sizeText.x <= 0 )
269 {
270 // DEFAULT_ITEM_WIDTH is the default width for the text control
271 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
272 }
273
274 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
275 if ( sizeText.x <= 0 )
276 {
277 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
278 }
279
280 wxPoint posBtn(pos);
281 posBtn.x += sizeText.x + MARGIN_BETWEEN;
282
283 // we must create the text control before the spin button for the purpose
284 // of the dialog navigation: if there is a static text just before the spin
285 // control, activating it by Alt-letter should give focus to the text
286 // control, not the spin and the dialog navigation code will give focus to
287 // the next control (at Windows level), not the one after it
288
289 // create the text window
290
291 m_hwndBuddy = (WXHWND)::CreateWindowEx
292 (
293 exStyle, // sunken border
294 _T("EDIT"), // window class
295 NULL, // no window title
296 msStyle, // style (will be shown later)
297 pos.x, pos.y, // position
298 0, 0, // size (will be set later)
299 GetHwndOf(parent), // parent
300 (HMENU)-1, // control id
301 wxGetInstance(), // app instance
302 NULL // unused client data
303 );
304
305 if ( !m_hwndBuddy )
306 {
307 wxLogLastError(wxT("CreateWindow(buddy text window)"));
308
309 return FALSE;
310 }
311
312
313 // create the spin button
314 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
315 {
316 return FALSE;
317 }
318
319 SetRange(min, max);
320 SetValue(initial);
321
322 // subclass the text ctrl to be able to intercept some events
323 m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC);
324 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this);
325 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc);
326
327 // should have the same font as the other controls
328 SetFont(GetParent()->GetFont());
329
330 // set the size of the text window - can do it only now, because we
331 // couldn't call DoGetBestSize() before as font wasn't set
332 if ( sizeText.y <= 0 )
333 {
334 int cx, cy;
335 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
336
337 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
338 }
339
340 DoMoveWindow(pos.x, pos.y,
341 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
342
343 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
344
345 // associate the text window with the spin button
346 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
347
348 if ( !value.IsEmpty() )
349 {
350 SetValue(value);
351 }
352
353 // do it after finishing with m_hwndBuddy creation to avoid generating
354 // initial wxEVT_COMMAND_TEXT_UPDATED message
355 ms_allSpins.Add(this);
356
357 return TRUE;
358 }
359
360 wxSpinCtrl::~wxSpinCtrl()
361 {
362 ms_allSpins.Remove(this);
363
364 // This removes spurious memory leak reporting
365 if (ms_allSpins.GetCount() == 0)
366 ms_allSpins.Clear();
367
368 // destroy the buddy window because this pointer which wxBuddyTextWndProc
369 // uses will not soon be valid any more
370 ::DestroyWindow(GetBuddyHwnd());
371 }
372
373 // ----------------------------------------------------------------------------
374 // wxTextCtrl-like methods
375 // ----------------------------------------------------------------------------
376
377 void wxSpinCtrl::SetValue(const wxString& text)
378 {
379 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
380 {
381 wxLogLastError(wxT("SetWindowText(buddy)"));
382 }
383 }
384
385 int wxSpinCtrl::GetValue() const
386 {
387 wxString val = wxGetWindowText(m_hwndBuddy);
388
389 long n;
390 if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
391 n = INT_MIN;
392
393 return n;
394 }
395
396 void wxSpinCtrl::SetSelection(long from, long to)
397 {
398 // if from and to are both -1, it means (in wxWindows) that all text should
399 // be selected - translate into Windows convention
400 if ( (from == -1) && (to == -1) )
401 {
402 from = 0;
403 }
404
405 ::SendMessage((HWND)m_hwndBuddy, EM_SETSEL, (WPARAM)from, (LPARAM)to);
406 }
407
408 // ----------------------------------------------------------------------------
409 // forward some methods to subcontrols
410 // ----------------------------------------------------------------------------
411
412 bool wxSpinCtrl::SetFont(const wxFont& font)
413 {
414 if ( !wxWindowBase::SetFont(font) )
415 {
416 // nothing to do
417 return FALSE;
418 }
419
420 WXHANDLE hFont = GetFont().GetResourceHandle();
421 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
422
423 return TRUE;
424 }
425
426 bool wxSpinCtrl::Show(bool show)
427 {
428 if ( !wxControl::Show(show) )
429 {
430 return FALSE;
431 }
432
433 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
434
435 return TRUE;
436 }
437
438 bool wxSpinCtrl::Enable(bool enable)
439 {
440 if ( !wxControl::Enable(enable) )
441 {
442 return FALSE;
443 }
444
445 ::EnableWindow(GetBuddyHwnd(), enable);
446
447 return TRUE;
448 }
449
450 void wxSpinCtrl::SetFocus()
451 {
452 ::SetFocus(GetBuddyHwnd());
453 }
454
455 // ----------------------------------------------------------------------------
456 // event processing
457 // ----------------------------------------------------------------------------
458
459 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
460 {
461 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
462 event.SetEventObject(this);
463 event.SetInt(eventSpin.GetPosition());
464
465 (void)GetEventHandler()->ProcessEvent(event);
466
467 if ( eventSpin.GetSkipped() )
468 {
469 event.Skip();
470 }
471 }
472
473 // ----------------------------------------------------------------------------
474 // size calculations
475 // ----------------------------------------------------------------------------
476
477 wxSize wxSpinCtrl::DoGetBestSize() const
478 {
479 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
480 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
481
482 int y;
483 wxGetCharSize(GetHWND(), NULL, &y, &GetFont());
484 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
485
486 if ( sizeBtn.y < y )
487 {
488 // make the text tall enough
489 sizeBtn.y = y;
490 }
491
492 return sizeBtn;
493 }
494
495 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
496 {
497 int widthBtn = wxSpinButton::DoGetBestSize().x;
498 int widthText = width - widthBtn - MARGIN_BETWEEN;
499 if ( widthText <= 0 )
500 {
501 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
502 }
503
504 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
505 {
506 wxLogLastError(wxT("MoveWindow(buddy)"));
507 }
508
509 x += widthText + MARGIN_BETWEEN;
510 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
511 {
512 wxLogLastError(wxT("MoveWindow"));
513 }
514 }
515
516 // get total size of the control
517 void wxSpinCtrl::DoGetSize(int *x, int *y) const
518 {
519 RECT spinrect, textrect, ctrlrect;
520 GetWindowRect(GetHwnd(), &spinrect);
521 GetWindowRect(GetBuddyHwnd(), &textrect);
522 UnionRect(&ctrlrect,&textrect, &spinrect);
523
524 if ( x )
525 *x = ctrlrect.right - ctrlrect.left;
526 if ( y )
527 *y = ctrlrect.bottom - ctrlrect.top;
528 }
529
530 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
531 {
532 // hack: pretend that our HWND is the text control just for a moment
533 WXHWND hWnd = GetHWND();
534 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
535
536 wxSpinButton::DoGetPosition(x, y);
537
538 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
539 }
540
541 #endif // __WIN95__
542
543 #endif
544 // wxUSE_SPINCTRL
545