apply patch 1630906 after all as with my simpler fix in the last commit the control...
[wxWidgets.git] / src / msw / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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) 1999-2005 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_SPINCTRL
28
29 #include "wx/spinctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/event.h"
34 #include "wx/textctrl.h"
35 #endif
36
37 #include "wx/msw/private.h"
38
39 #if wxUSE_TOOLTIPS
40 #include "wx/tooltip.h"
41 #endif // wxUSE_TOOLTIPS
42
43 #include <limits.h> // for INT_MIN
44
45 // ----------------------------------------------------------------------------
46 // macros
47 // ----------------------------------------------------------------------------
48
49 #if wxUSE_EXTENDED_RTTI
50 WX_DEFINE_FLAGS( wxSpinCtrlStyle )
51
52 wxBEGIN_FLAGS( wxSpinCtrlStyle )
53 // new style border flags, we put them first to
54 // use them for streaming out
55 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
56 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
57 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
58 wxFLAGS_MEMBER(wxBORDER_RAISED)
59 wxFLAGS_MEMBER(wxBORDER_STATIC)
60 wxFLAGS_MEMBER(wxBORDER_NONE)
61
62 // old style border flags
63 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
64 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
65 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
66 wxFLAGS_MEMBER(wxRAISED_BORDER)
67 wxFLAGS_MEMBER(wxSTATIC_BORDER)
68 wxFLAGS_MEMBER(wxBORDER)
69
70 // standard window styles
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
74 wxFLAGS_MEMBER(wxWANTS_CHARS)
75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
77 wxFLAGS_MEMBER(wxVSCROLL)
78 wxFLAGS_MEMBER(wxHSCROLL)
79
80 wxFLAGS_MEMBER(wxSP_HORIZONTAL)
81 wxFLAGS_MEMBER(wxSP_VERTICAL)
82 wxFLAGS_MEMBER(wxSP_ARROW_KEYS)
83 wxFLAGS_MEMBER(wxSP_WRAP)
84
85 wxEND_FLAGS( wxSpinCtrlStyle )
86
87 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h")
88
89 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl)
90 wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxSpinEvent )
91 wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SPINCTRL_UPDATED , wxCommandEvent )
92 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
93 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
94
95 wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
96 wxPROPERTY( Value , int , SetValue, GetValue, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
97 wxPROPERTY( Min , int , SetMin, GetMin, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
98 wxPROPERTY( Max , int , SetMax, GetMax, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
100 /*
101 TODO PROPERTIES
102 style wxSP_ARROW_KEYS
103 */
104 wxEND_PROPERTIES_TABLE()
105
106 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl)
107 wxEND_HANDLERS_TABLE()
108
109 wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle )
110 #else
111 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
112 #endif
113
114 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
115 EVT_CHAR(wxSpinCtrl::OnChar)
116
117 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
118 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus)
119 EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
120 END_EVENT_TABLE()
121
122 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
123
124 // ----------------------------------------------------------------------------
125 // constants
126 // ----------------------------------------------------------------------------
127
128 // the margin between the up-down control and its buddy (can be arbitrary,
129 // choose what you like - or may be decide during run-time depending on the
130 // font size?)
131 static const int MARGIN_BETWEEN = 1;
132
133 // ============================================================================
134 // implementation
135 // ============================================================================
136
137 wxArraySpins wxSpinCtrl::ms_allSpins;
138
139 // ----------------------------------------------------------------------------
140 // wnd proc for the buddy text ctrl
141 // ----------------------------------------------------------------------------
142
143 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
144 UINT message,
145 WPARAM wParam,
146 LPARAM lParam)
147 {
148 wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData(hwnd);
149
150 // forward some messages (the key and focus ones only so far) to
151 // the spin ctrl
152 switch ( message )
153 {
154 case WM_SETFOCUS:
155 // if the focus comes from the spin control itself, don't set it
156 // back to it -- we don't want to go into an infinite loop
157 if ( (WXHWND)wParam == spin->GetHWND() )
158 break;
159 //else: fall through
160
161 case WM_KILLFOCUS:
162 case WM_CHAR:
163 case WM_DEADCHAR:
164 case WM_KEYUP:
165 case WM_KEYDOWN:
166 spin->MSWWindowProc(message, wParam, lParam);
167
168 // The control may have been deleted at this point, so check.
169 if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin )
170 return 0;
171 break;
172
173 case WM_GETDLGCODE:
174 // we want to get WXK_RETURN in order to generate the event for it
175 return DLGC_WANTCHARS;
176 }
177
178 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
179 hwnd, message, wParam, lParam);
180 }
181
182 /* static */
183 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
184 {
185 wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData((HWND)hwndBuddy);
186
187 int i = ms_allSpins.Index(spin);
188
189 if ( i == wxNOT_FOUND )
190 return NULL;
191
192 // sanity check
193 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
194 _T("wxSpinCtrl has incorrect buddy HWND!") );
195
196 return spin;
197 }
198
199 // process a WM_COMMAND generated by the buddy text control
200 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
201 {
202 if ( cmd == EN_CHANGE )
203 {
204 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
205 event.SetEventObject(this);
206 wxString val = wxGetWindowText(m_hwndBuddy);
207 event.SetString(val);
208 event.SetInt(GetValue());
209 return GetEventHandler()->ProcessEvent(event);
210 }
211
212 // not processed
213 return false;
214 }
215
216 void wxSpinCtrl::OnChar(wxKeyEvent& event)
217 {
218 switch ( event.GetKeyCode() )
219 {
220 case WXK_RETURN:
221 {
222 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
223 InitCommandEvent(event);
224 wxString val = wxGetWindowText(m_hwndBuddy);
225 event.SetString(val);
226 event.SetInt(GetValue());
227 if ( GetEventHandler()->ProcessEvent(event) )
228 return;
229 break;
230 }
231
232 case WXK_TAB:
233 // always produce navigation event - even if we process TAB
234 // ourselves the fact that we got here means that the user code
235 // decided to skip processing of this TAB - probably to let it
236 // do its default job.
237 {
238 wxNavigationKeyEvent eventNav;
239 eventNav.SetDirection(!event.ShiftDown());
240 eventNav.SetWindowChange(event.ControlDown());
241 eventNav.SetEventObject(this);
242
243 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
244 return;
245 }
246 break;
247 }
248
249 // no, we didn't process it
250 event.Skip();
251 }
252
253 void wxSpinCtrl::OnKillFocus(wxFocusEvent& event)
254 {
255 // ensure that a correct value is shown by the control
256 NormalizeValue();
257 event.Skip();
258 }
259
260 void wxSpinCtrl::OnSetFocus(wxFocusEvent& event)
261 {
262 // when we get focus, give it to our buddy window as it needs it more than
263 // we do
264 ::SetFocus((HWND)m_hwndBuddy);
265
266 event.Skip();
267 }
268
269 void wxSpinCtrl::NormalizeValue()
270 {
271 const int value = GetValue();
272 const bool changed = value == m_oldValue;
273
274 // notice that we have to call SetValue() even if the value didn't change
275 // because otherwise we could be left with empty buddy control when value
276 // is 0, see comment in SetValue()
277 SetValue(value);
278
279 if ( changed )
280 {
281 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
282 event.SetEventObject(this);
283 event.SetInt(value);
284 GetEventHandler()->ProcessEvent(event);
285 m_oldValue = value;
286 }
287 }
288
289 // ----------------------------------------------------------------------------
290 // construction
291 // ----------------------------------------------------------------------------
292
293 bool wxSpinCtrl::Create(wxWindow *parent,
294 wxWindowID id,
295 const wxString& value,
296 const wxPoint& pos,
297 const wxSize& size,
298 long style,
299 int min, int max, int initial,
300 const wxString& name)
301 {
302 // before using DoGetBestSize(), have to set style to let the base class
303 // know whether this is a horizontal or vertical control (we're always
304 // vertical)
305 style |= wxSP_VERTICAL;
306
307 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
308 #ifdef __WXWINCE__
309 style |= wxBORDER_SIMPLE;
310 #else
311 style |= wxBORDER_SUNKEN;
312 #endif
313
314 SetWindowStyle(style);
315
316 WXDWORD exStyle = 0;
317 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
318
319 // calculate the sizes: the size given is the toal size for both controls
320 // and we need to fit them both in the given width (height is the same)
321 wxSize sizeText(size), sizeBtn(size);
322 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
323 if ( sizeText.x <= 0 )
324 {
325 // DEFAULT_ITEM_WIDTH is the default width for the text control
326 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
327 }
328
329 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
330 if ( sizeText.x <= 0 )
331 {
332 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
333 }
334
335 wxPoint posBtn(pos);
336 posBtn.x += sizeText.x + MARGIN_BETWEEN;
337
338 // we must create the text control before the spin button for the purpose
339 // of the dialog navigation: if there is a static text just before the spin
340 // control, activating it by Alt-letter should give focus to the text
341 // control, not the spin and the dialog navigation code will give focus to
342 // the next control (at Windows level), not the one after it
343
344 // create the text window
345
346 m_hwndBuddy = (WXHWND)::CreateWindowEx
347 (
348 exStyle, // sunken border
349 _T("EDIT"), // window class
350 NULL, // no window title
351 msStyle, // style (will be shown later)
352 pos.x, pos.y, // position
353 0, 0, // size (will be set later)
354 GetHwndOf(parent), // parent
355 (HMENU)-1, // control id
356 wxGetInstance(), // app instance
357 NULL // unused client data
358 );
359
360 if ( !m_hwndBuddy )
361 {
362 wxLogLastError(wxT("CreateWindow(buddy text window)"));
363
364 return false;
365 }
366
367
368 // create the spin button
369 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
370 {
371 return false;
372 }
373
374 SetRange(min, max);
375 SetValue(initial);
376
377 m_oldValue = initial;
378
379 // subclass the text ctrl to be able to intercept some events
380 wxSetWindowUserData(GetBuddyHwnd(), this);
381 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
382 wxBuddyTextWndProc);
383
384 // set up fonts and colours (This is nomally done in MSWCreateControl)
385 InheritAttributes();
386 if (!m_hasFont)
387 SetFont(GetDefaultAttributes().font);
388
389 // set the size of the text window - can do it only now, because we
390 // couldn't call DoGetBestSize() before as font wasn't set
391 if ( sizeText.y <= 0 )
392 {
393 int cx, cy;
394 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
395
396 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
397 }
398
399 SetInitialSize(size);
400
401 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
402
403 // associate the text window with the spin button
404 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
405
406 if ( !value.empty() )
407 {
408 SetValue(value);
409 }
410
411 // do it after finishing with m_hwndBuddy creation to avoid generating
412 // initial wxEVT_COMMAND_TEXT_UPDATED message
413 ms_allSpins.Add(this);
414
415 return true;
416 }
417
418 wxSpinCtrl::~wxSpinCtrl()
419 {
420 ms_allSpins.Remove(this);
421
422 // This removes spurious memory leak reporting
423 if (ms_allSpins.GetCount() == 0)
424 ms_allSpins.Clear();
425
426 // destroy the buddy window because this pointer which wxBuddyTextWndProc
427 // uses will not soon be valid any more
428 ::DestroyWindow(GetBuddyHwnd());
429 }
430
431 // ----------------------------------------------------------------------------
432 // wxTextCtrl-like methods
433 // ----------------------------------------------------------------------------
434
435 void wxSpinCtrl::SetValue(const wxString& text)
436 {
437 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
438 {
439 wxLogLastError(wxT("SetWindowText(buddy)"));
440 }
441 }
442
443 void wxSpinCtrl::SetValue(int val)
444 {
445 wxSpinButton::SetValue(val);
446
447 // normally setting the value of the spin button is enough as it updates
448 // its buddy control automatically ...
449 if ( wxGetWindowText(m_hwndBuddy).empty() )
450 {
451 // ... but sometimes it doesn't, notably when the value is 0 and the
452 // text control is currently empty, the spin button seems to be happy
453 // to leave it like this, while we really want to always show the
454 // current value in the control, so do it manually
455 ::SetWindowText(GetBuddyHwnd(), wxString::Format(_T("%d"), val));
456 }
457
458 m_oldValue = GetValue();
459 }
460
461 int wxSpinCtrl::GetValue() const
462 {
463 wxString val = wxGetWindowText(m_hwndBuddy);
464
465 long n;
466 if ( (wxSscanf(val, wxT("%ld"), &n) != 1) )
467 n = INT_MIN;
468
469 if ( n < m_min )
470 n = m_min;
471 if ( n > m_max )
472 n = m_max;
473
474 return n;
475 }
476
477 void wxSpinCtrl::SetSelection(long from, long to)
478 {
479 // if from and to are both -1, it means (in wxWidgets) that all text should
480 // be selected - translate into Windows convention
481 if ( (from == -1) && (to == -1) )
482 {
483 from = 0;
484 }
485
486 ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
487 }
488
489 // ----------------------------------------------------------------------------
490 // forward some methods to subcontrols
491 // ----------------------------------------------------------------------------
492
493 bool wxSpinCtrl::SetFont(const wxFont& font)
494 {
495 if ( !wxWindowBase::SetFont(font) )
496 {
497 // nothing to do
498 return false;
499 }
500
501 WXHANDLE hFont = GetFont().GetResourceHandle();
502 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
503
504 return true;
505 }
506
507 bool wxSpinCtrl::Show(bool show)
508 {
509 if ( !wxControl::Show(show) )
510 {
511 return false;
512 }
513
514 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
515
516 return true;
517 }
518
519 bool wxSpinCtrl::Enable(bool enable)
520 {
521 if ( !wxControl::Enable(enable) )
522 {
523 return false;
524 }
525
526 ::EnableWindow(GetBuddyHwnd(), enable);
527
528 return true;
529 }
530
531 void wxSpinCtrl::SetFocus()
532 {
533 ::SetFocus(GetBuddyHwnd());
534 }
535
536 #if wxUSE_TOOLTIPS
537
538 void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
539 {
540 wxSpinButton::DoSetToolTip(tip);
541
542 if ( tip )
543 tip->Add(m_hwndBuddy);
544 }
545
546 #endif // wxUSE_TOOLTIPS
547
548 // ----------------------------------------------------------------------------
549 // event processing
550 // ----------------------------------------------------------------------------
551
552 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
553 {
554 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
555 event.SetEventObject(this);
556 int value = eventSpin.GetPosition();
557 event.SetInt( value );
558
559 if (value != m_oldValue)
560 (void)GetEventHandler()->ProcessEvent(event);
561
562 if ( eventSpin.GetSkipped() )
563 {
564 event.Skip();
565 }
566
567 m_oldValue = value;
568 }
569
570 // ----------------------------------------------------------------------------
571 // size calculations
572 // ----------------------------------------------------------------------------
573
574 wxSize wxSpinCtrl::DoGetBestSize() const
575 {
576 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
577 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
578
579 int y;
580 wxGetCharSize(GetHWND(), NULL, &y, GetFont());
581 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
582
583 // JACS: we should always use the height calculated
584 // from above, because otherwise we'll get a spin control
585 // that's too big. So never use the height calculated
586 // from wxSpinButton::DoGetBestSize().
587
588 // if ( sizeBtn.y < y )
589 {
590 // make the text tall enough
591 sizeBtn.y = y;
592 }
593
594 return sizeBtn;
595 }
596
597 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
598 {
599 int widthBtn = wxSpinButton::DoGetBestSize().x;
600 int widthText = width - widthBtn - MARGIN_BETWEEN;
601 if ( widthText <= 0 )
602 {
603 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
604 }
605
606 // 1) The buddy window
607 DoMoveSibling(m_hwndBuddy, x, y, widthText, height);
608
609 // 2) The button window
610 x += widthText + MARGIN_BETWEEN;
611 wxSpinButton::DoMoveWindow(x, y, widthBtn, height);
612 }
613
614 // get total size of the control
615 void wxSpinCtrl::DoGetSize(int *x, int *y) const
616 {
617 RECT spinrect, textrect, ctrlrect;
618 GetWindowRect(GetHwnd(), &spinrect);
619 GetWindowRect(GetBuddyHwnd(), &textrect);
620 UnionRect(&ctrlrect,&textrect, &spinrect);
621
622 if ( x )
623 *x = ctrlrect.right - ctrlrect.left;
624 if ( y )
625 *y = ctrlrect.bottom - ctrlrect.top;
626 }
627
628 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
629 {
630 // hack: pretend that our HWND is the text control just for a moment
631 WXHWND hWnd = GetHWND();
632 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
633
634 wxSpinButton::DoGetPosition(x, y);
635
636 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
637 }
638
639 #endif // wxUSE_SPINCTRL