/////////////////////////////////////////////////////////////////////////////
-// Name: msw/spinctrl.cpp
+// Name: src/msw/spinctrl.cpp
// Purpose: wxSpinCtrl class implementation for Win32
// Author: Vadim Zeitlin
// Modified by:
#pragma hdrstop
#endif
-#ifndef WX_PRECOMP
- #include "wx/wx.h"
-#endif
-
#if wxUSE_SPINCTRL
#include "wx/spinctrl.h"
+
+#ifndef WX_PRECOMP
+ #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
+ #include "wx/event.h"
+ #include "wx/textctrl.h"
+#endif
+
#include "wx/msw/private.h"
-#include "wx/msw/wrapcctl.h"
#if wxUSE_TOOLTIPS
#include "wx/tooltip.h"
IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
#endif
+//pmg EVT_KILL_FOCUS
BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
EVT_CHAR(wxSpinCtrl::OnChar)
EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
-
+ EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus)
EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
END_EVENT_TABLE()
// process a WM_COMMAND generated by the buddy text control
bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
{
- switch (cmd)
+ if ( cmd == EN_CHANGE )
{
- case EN_CHANGE:
- {
- wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
- event.SetEventObject(this);
- wxString val = wxGetWindowText(m_hwndBuddy);
- event.SetString(val);
- event.SetInt(GetValue());
- return GetEventHandler()->ProcessEvent(event);
- }
- case EN_SETFOCUS:
- case EN_KILLFOCUS:
- {
- wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
- : wxEVT_SET_FOCUS,
- m_windowId);
- event.SetEventObject( this );
- return GetEventHandler()->ProcessEvent(event);
- }
- default:
- break;
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
+ event.SetEventObject(this);
+ wxString val = wxGetWindowText(m_hwndBuddy);
+ event.SetString(val);
+ event.SetInt(GetValue());
+ return GetEventHandler()->ProcessEvent(event);
}
// not processed
event.Skip();
}
+void wxSpinCtrl::OnKillFocus(wxFocusEvent& event)
+{
+ // ensure that a correct value is shown by the control
+ NormalizeValue();
+ event.Skip();
+}
+
void wxSpinCtrl::OnSetFocus(wxFocusEvent& event)
{
// when we get focus, give it to our buddy window as it needs it more than
event.Skip();
}
+void wxSpinCtrl::NormalizeValue()
+{
+ int value = GetValue();
+ SetValue( value );
+ if (value != m_oldValue)
+ {
+ wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId() );
+ event.SetEventObject( this );
+ event.SetInt( value );
+ GetEventHandler()->ProcessEvent( event );
+ m_oldValue = value;
+ }
+}
+
// ----------------------------------------------------------------------------
// construction
// ----------------------------------------------------------------------------
SetRange(min, max);
SetValue(initial);
+
+ m_oldValue = initial;
// subclass the text ctrl to be able to intercept some events
wxSetWindowUserData(GetBuddyHwnd(), this);
sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
}
- SetBestSize(size);
+ SetInitialSize(size);
(void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
}
}
+void wxSpinCtrl::SetValue(int val)
+{
+ wxSpinButton::SetValue(val);
+
+ // normally setting the value of the spin button is enough as it updates
+ // its buddy control automatically ...
+ if ( wxGetWindowText(m_hwndBuddy).empty() )
+ {
+ // ... but sometimes it doesn't, notably when the value is 0 and the
+ // text control is currently empty, the spin button seems to be happy
+ // to leave it like this, while we really want to always show the
+ // current value in the control, so do it manually
+ ::SetWindowText(GetBuddyHwnd(), wxString::Format(_T("%d"), val));
+ }
+
+ m_oldValue = GetValue();
+}
+
int wxSpinCtrl::GetValue() const
{
wxString val = wxGetWindowText(m_hwndBuddy);
long n;
- if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
+ if ( (wxSscanf(val, wxT("%ld"), &n) != 1) )
n = INT_MIN;
- if (n < m_min) n = m_min;
- if (n > m_max) n = m_max;
+ if ( n < m_min )
+ n = m_min;
+ if ( n > m_max )
+ n = m_max;
return n;
}
{
wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
event.SetEventObject(this);
- event.SetInt(eventSpin.GetPosition());
-
- (void)GetEventHandler()->ProcessEvent(event);
+ int value = eventSpin.GetPosition();
+ event.SetInt( value );
+
+ if (value != m_oldValue)
+ (void)GetEventHandler()->ProcessEvent(event);
if ( eventSpin.GetSkipped() )
{
event.Skip();
}
+
+ m_oldValue = value;
}
// ----------------------------------------------------------------------------
}
#endif // wxUSE_SPINCTRL
-