After the changes of r53758 wxMSW didn't generate any wxEVT_TEXT events but
this was still the case for the generic version (and hence for
wxSpinCtrlDouble under MSW too) and wasn't documented.
Fix all versions to avoid sending events for programmatic actions, add unit
tests checking this behaviour and document it.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74631
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
virtual void DoEnable(bool enable);
#endif // __WXMSW__
virtual void DoEnable(bool enable);
#endif // __WXMSW__
+ enum SendEvent
+ {
+ SendEvent_None,
+ SendEvent_Text
+ };
+
// generic double valued functions
double DoGetValue() const { return m_value; }
// generic double valued functions
double DoGetValue() const { return m_value; }
- bool DoSetValue(double val);
+ bool DoSetValue(double val, SendEvent sendEvent);
void DoSetRange(double min_val, double max_val);
void DoSetIncrement(double inc);
void DoSetRange(double min_val, double max_val);
void DoSetIncrement(double inc);
// can also change the text control if its value is invalid
//
// return true if our value has changed
// can also change the text control if its value is invalid
//
// return true if our value has changed
+ bool SyncSpinToText(SendEvent sendEvent);
// Send the correct event type
virtual void DoSendEvent() = 0;
// Send the correct event type
virtual void DoSendEvent() = 0;
bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
wxDefaultValidator, name);
bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
wxDefaultValidator, name);
+ DoSetValue(initial, SendEvent_None);
- bool DoSetValue(double val)
+ bool DoSetValue(double val, SendEvent sendEvent)
- wxTextCtrl::SetValue(wxString::Format(m_format.c_str(), val));
+ wxString str(wxString::Format(m_format, val));
+ switch ( sendEvent )
+ {
+ case SendEvent_None:
+ wxTextCtrl::ChangeValue(str);
+ break;
+
+ case SendEvent_Text:
+ wxTextCtrl::SetValue(str);
+ break;
+ }
+
return true;
}
void DoSetRange(double min_val, double max_val)
return true;
}
void DoSetRange(double min_val, double max_val)
// operations
void SetValue(const wxString& value)
{ wxSpinCtrlGenericBase::SetValue(value); }
// operations
void SetValue(const wxString& value)
{ wxSpinCtrlGenericBase::SetValue(value); }
- void SetValue( int value ) { DoSetValue(value); }
+ void SetValue( int value ) { DoSetValue(value, SendEvent_None); }
void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
void SetIncrement(int inc) { DoSetIncrement(inc); }
void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
void SetIncrement(int inc) { DoSetIncrement(inc); }
// operations
void SetValue(const wxString& value)
{ wxSpinCtrlGenericBase::SetValue(value); }
// operations
void SetValue(const wxString& value)
{ wxSpinCtrlGenericBase::SetValue(value); }
- void SetValue(double value) { DoSetValue(value); }
+ void SetValue(double value) { DoSetValue(value, SendEvent_None); }
void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
void SetIncrement(double inc) { DoSetIncrement(inc); }
void SetDigits(unsigned digits);
void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
void SetIncrement(double inc) { DoSetIncrement(inc); }
void SetDigits(unsigned digits);
virtual void SetSelection(long from, long to);
/**
virtual void SetSelection(long from, long to);
/**
- Sets the value of the spin control. Use the variant using int instead.
+ Sets the value of the spin control.
+
+ It is recommended to use the overload taking an integer value instead.
+
+ Notice that, unlike wxTextCtrl::SetValue(), but like most of the other
+ setter methods in wxWidgets, calling this method does not generate any
+ events as events are only generated for the user actions.
*/
virtual void SetValue(const wxString& text);
/**
Sets the value of the spin control.
*/
virtual void SetValue(const wxString& text);
/**
Sets the value of the spin control.
+
+ Calling this method doesn't generate any @c wxEVT_SPINCTRL events.
*/
void SetValue(int value);
};
*/
void SetValue(int value);
};
void SetRange(double minVal, double maxVal);
/**
void SetRange(double minVal, double maxVal);
/**
- Sets the value of the spin control. Use the variant using double instead.
+ Sets the value of the spin control.
+
+ It is recommended to use the overload taking a double value instead.
+
+ Notice that, unlike wxTextCtrl::SetValue(), but like most of the other
+ setter methods in wxWidgets, calling this method does not generate any
+ events as events are only generated for the user actions.
*/
virtual void SetValue(const wxString& text);
/**
Sets the value of the spin control.
*/
virtual void SetValue(const wxString& text);
/**
Sets the value of the spin control.
+
+ Calling this method doesn't generate any @c wxEVT_SPINCTRLDOUBLE events.
*/
void SetValue(double value);
};
*/
void SetValue(double value);
};
// Sync the textctrl since the user expects that the button will modify
// what they see in the textctrl.
// Sync the textctrl since the user expects that the button will modify
// what they see in the textctrl.
+ SyncSpinToText(SendEvent_None);
int spin_value = event.GetPosition();
double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
int spin_value = event.GetPosition();
double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
m_spin_value = spin_value;
m_spin_value = spin_value;
- if ( DoSetValue(value) )
+ // Notify about the change in wxTextCtrl too.
+ if ( DoSetValue(value, SendEvent_Text) )
DoSendEvent();
}
void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent& event)
{
DoSendEvent();
}
void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent& event)
{
+ SyncSpinToText(SendEvent_Text);
DoSendEvent();
event.Skip();
DoSendEvent();
event.Skip();
value = AdjustToFitInRange(value);
value = AdjustToFitInRange(value);
+ SyncSpinToText(SendEvent_None);
- if ( DoSetValue(value) )
+ // No need to send event, it was already generated by wxTextCtrl itself.
+ if ( DoSetValue(value, SendEvent_None) )
// Textctrl functions
// ----------------------------------------------------------------------------
// Textctrl functions
// ----------------------------------------------------------------------------
-bool wxSpinCtrlGenericBase::SyncSpinToText()
+bool wxSpinCtrlGenericBase::SyncSpinToText(SendEvent sendEvent)
{
if ( !m_textCtrl || !m_textCtrl->IsModified() )
return false;
{
if ( !m_textCtrl || !m_textCtrl->IsModified() )
return false;
// we must always set the value here, even if it's equal to m_value, as
// otherwise we could be left with an out of range value when leaving the
// text control and the current value is already m_max for example
// we must always set the value here, even if it's equal to m_value, as
// otherwise we could be left with an out of range value when leaving the
// text control and the current value is already m_max for example
- return DoSetValue(textValue);
+ return DoSetValue(textValue, sendEvent);
}
// ----------------------------------------------------------------------------
}
// ----------------------------------------------------------------------------
double val;
if ( DoTextToValue(text, &val) && InRange(val) )
{
double val;
if ( DoTextToValue(text, &val) && InRange(val) )
{
+ DoSetValue(val, SendEvent_None);
}
else // not a number at all or out of range
{
}
else // not a number at all or out of range
{
- m_textCtrl->SetValue(text);
+ m_textCtrl->ChangeValue(text);
m_textCtrl->SelectAll();
}
}
m_textCtrl->SelectAll();
}
}
-bool wxSpinCtrlGenericBase::DoSetValue(double val)
+bool wxSpinCtrlGenericBase::DoSetValue(double val, SendEvent sendEvent)
{
wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") );
{
wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") );
{
if ( !DoTextToValue(str, &m_value ) ) // wysiwyg for textctrl
m_value = val;
{
if ( !DoTextToValue(str, &m_value ) ) // wysiwyg for textctrl
m_value = val;
- m_textCtrl->SetValue( str );
+
+ switch ( sendEvent )
+ {
+ case SendEvent_None:
+ m_textCtrl->ChangeValue(str);
+ break;
+
+ case SendEvent_Text:
+ m_textCtrl->SetValue(str);
+ break;
+ }
+
m_textCtrl->SelectAll();
m_textCtrl->DiscardEdits();
return true;
m_textCtrl->SelectAll();
m_textCtrl->DiscardEdits();
return true;
{
m_min = min;
if ( m_value < m_min )
{
m_min = min;
if ( m_value < m_min )
+ DoSetValue(m_min, SendEvent_None);
m_max = max;
if ( m_value > m_max )
m_max = max;
if ( m_value > m_max )
+ DoSetValue(m_max, SendEvent_None);
}
void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
}
void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
{
m_snap_to_ticks = snap_to_ticks;
void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
{
m_snap_to_ticks = snap_to_ticks;
+ DoSetValue(m_value, SendEvent_None);
}
void wxSpinCtrlGenericBase::SetSelection(long from, long to)
}
void wxSpinCtrlGenericBase::SetSelection(long from, long to)
// ... but DoValueToText() after doing it.
if ( hasValidVal )
// ... but DoValueToText() after doing it.
if ( hasValidVal )
- m_textCtrl->SetValue(DoValueToText(val));
+ m_textCtrl->ChangeValue(DoValueToText(val));
m_format.Printf(wxT("%%0.%ulf"), digits);
m_format.Printf(wxT("%%0.%ulf"), digits);
+ DoSetValue(m_value, SendEvent_None);
}
#endif // wxUSE_SPINBTN
}
#endif // wxUSE_SPINBTN
{
wxCHECK_RET( m_widget, "invalid spin button" );
{
wxCHECK_RET( m_widget, "invalid spin button" );
gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
}
#endif // wxUSE_SPINCTRL
}
#endif // wxUSE_SPINCTRL
delete m_spin;
m_spin = new wxSpinCtrlDouble;
delete m_spin;
m_spin = new wxSpinCtrlDouble;
- EventCounter updated(m_spin, wxEVT_SPINCTRLDOUBLE);
+ EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
+ EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->Create(parent, wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0., 100., 17.);
m_spin->Create(parent, wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0., 100., 17.);
- CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
}
void SpinCtrlDoubleTestCase::Arrows()
}
void SpinCtrlDoubleTestCase::Arrows()
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetMin());
CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetMax());
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetMin());
CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetMax());
+ // Test that the value is adjusted to be inside the new valid range but
+ // that this doesn't result in any events (as this is not something done by
+ // the user).
+ {
+ EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
+ EventCounter updatedText(m_spin, wxEVT_TEXT);
+
+ m_spin->SetRange(1., 10.);
+ CPPUNIT_ASSERT_EQUAL(1., m_spin->GetValue());
+
+ CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
+ }
+
+ //Test negative ranges
m_spin->SetRange(-10.0, 10.0);
CPPUNIT_ASSERT_EQUAL(-10.0, m_spin->GetMin());
m_spin->SetRange(-10.0, 10.0);
CPPUNIT_ASSERT_EQUAL(-10.0, m_spin->GetMin());
void SpinCtrlDoubleTestCase::Value()
{
void SpinCtrlDoubleTestCase::Value()
{
+ EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
+ EventCounter updatedText(m_spin, wxEVT_TEXT);
+
m_spin->SetDigits(2);
m_spin->SetIncrement(0.1);
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
m_spin->SetValue(50.0);
m_spin->SetDigits(2);
m_spin->SetIncrement(0.1);
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
m_spin->SetValue(50.0);
CPPUNIT_ASSERT_EQUAL(50.0, m_spin->GetValue());
m_spin->SetValue(49.1);
CPPUNIT_ASSERT_EQUAL(50.0, m_spin->GetValue());
m_spin->SetValue(49.1);
CPPUNIT_ASSERT_EQUAL(49.1, m_spin->GetValue());
CPPUNIT_ASSERT_EQUAL(49.1, m_spin->GetValue());
+
+ // Calling SetValue() shouldn't have generated any events.
+ CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
}
void SpinCtrlDoubleTestCase::Increment()
}
void SpinCtrlDoubleTestCase::Increment()
delete m_spin;
m_spin = new wxSpinCtrl;
delete m_spin;
m_spin = new wxSpinCtrl;
- EventCounter updated(m_spin, wxEVT_SPINCTRL);
+ EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
+ EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->Create(parent, wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0, 100, 17);
m_spin->Create(parent, wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0, 100, 17);
- CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
}
void SpinCtrlTestCase::Arrows()
}
void SpinCtrlTestCase::Arrows()
// that this doesn't result in any events (as this is not something done by
// the user).
{
// that this doesn't result in any events (as this is not something done by
// the user).
{
- EventCounter updated(m_spin, wxEVT_SPINCTRL);
+ EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
+ EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->SetRange(1, 10);
CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
m_spin->SetRange(1, 10);
CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
- CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
+
+ CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
void SpinCtrlTestCase::Value()
{
void SpinCtrlTestCase::Value()
{
+ EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
+ EventCounter updatedText(m_spin, wxEVT_TEXT);
+
CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
m_spin->SetValue(50);
CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
m_spin->SetValue(50);
CPPUNIT_ASSERT_EQUAL(50, m_spin->GetValue());
m_spin->SetValue(-10);
CPPUNIT_ASSERT_EQUAL(50, m_spin->GetValue());
m_spin->SetValue(-10);
CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
m_spin->SetValue(110);
CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
m_spin->SetValue(110);
CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
+
+ // Calling SetValue() shouldn't have generated any events.
+ CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
+ CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());