Make updating the value in wxEVT_SPINCTRL work in wxMSW
In wxMSW, the value of the control was modified after executing the user-defined wxEVT_SPINCTRL handler which meant that any calls to SetValue() there were effectively ignored. Fix this and add a unit test checking for the described scenario. Closes https://github.com/wxWidgets/wxWidgets/pull/1027 Closes #18187.
This commit is contained in:
committed by
Vadim Zeitlin
parent
facb06a1b8
commit
fd5c62bc41
@@ -244,6 +244,7 @@ void wxSpinCtrl::NormalizeValue()
|
|||||||
|
|
||||||
if ( changed )
|
if ( changed )
|
||||||
{
|
{
|
||||||
|
m_oldValue = value;
|
||||||
SendSpinUpdate(value);
|
SendSpinUpdate(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -666,10 +667,7 @@ void wxSpinCtrl::SendSpinUpdate(int value)
|
|||||||
wxSpinEvent event(wxEVT_SPINCTRL, GetId());
|
wxSpinEvent event(wxEVT_SPINCTRL, GetId());
|
||||||
event.SetEventObject(this);
|
event.SetEventObject(this);
|
||||||
event.SetInt(value);
|
event.SetInt(value);
|
||||||
|
|
||||||
(void)HandleWindowEvent(event);
|
(void)HandleWindowEvent(event);
|
||||||
|
|
||||||
m_oldValue = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
|
bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
|
||||||
@@ -687,7 +685,10 @@ bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
|
|||||||
// might be using 32 bit range.
|
// might be using 32 bit range.
|
||||||
int new_value = GetValue();
|
int new_value = GetValue();
|
||||||
if (m_oldValue != new_value)
|
if (m_oldValue != new_value)
|
||||||
|
{
|
||||||
|
m_oldValue = new_value;
|
||||||
SendSpinUpdate(new_value);
|
SendSpinUpdate(new_value);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -38,6 +38,7 @@ private:
|
|||||||
WXUISIM_TEST( Wrap );
|
WXUISIM_TEST( Wrap );
|
||||||
CPPUNIT_TEST( Range );
|
CPPUNIT_TEST( Range );
|
||||||
CPPUNIT_TEST( Value );
|
CPPUNIT_TEST( Value );
|
||||||
|
WXUISIM_TEST( SetValueInsideEventHandler );
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
void Initial();
|
void Initial();
|
||||||
@@ -46,6 +47,10 @@ private:
|
|||||||
void Wrap();
|
void Wrap();
|
||||||
void Range();
|
void Range();
|
||||||
void Value();
|
void Value();
|
||||||
|
void SetValueInsideEventHandler();
|
||||||
|
|
||||||
|
// Helper event handler for SetValueInsideEventHandler() test.
|
||||||
|
void OnSpinSetValue(wxSpinEvent &e);
|
||||||
|
|
||||||
wxSpinCtrl* m_spin;
|
wxSpinCtrl* m_spin;
|
||||||
|
|
||||||
@@ -224,4 +229,41 @@ void SpinCtrlTestCase::Value()
|
|||||||
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
|
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SpinCtrlTestCase::OnSpinSetValue(wxSpinEvent &e)
|
||||||
|
{
|
||||||
|
// Constrain the value to be in the 1..16 range or 32.
|
||||||
|
int newVal = e.GetValue();
|
||||||
|
|
||||||
|
if ( newVal == 31 )
|
||||||
|
m_spin->SetValue(16);
|
||||||
|
else if ( newVal > 16 )
|
||||||
|
m_spin->SetValue(32);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpinCtrlTestCase::SetValueInsideEventHandler()
|
||||||
|
{
|
||||||
|
#if wxUSE_UIACTIONSIMULATOR
|
||||||
|
m_spin->Bind(wxEVT_SPINCTRL, &SpinCtrlTestCase::OnSpinSetValue, this);
|
||||||
|
|
||||||
|
wxUIActionSimulator sim;
|
||||||
|
|
||||||
|
// run multiple times to make sure there are no issues with keeping old value
|
||||||
|
for ( size_t i = 0; i < 2; i++ )
|
||||||
|
{
|
||||||
|
m_spin->SetFocus();
|
||||||
|
wxYield();
|
||||||
|
|
||||||
|
sim.Char(WXK_DELETE);
|
||||||
|
sim.Char(WXK_DELETE);
|
||||||
|
sim.Text("20");
|
||||||
|
wxYield();
|
||||||
|
|
||||||
|
wxTheApp->GetTopWindow()->SetFocus();
|
||||||
|
wxYield();
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT_EQUAL(32, m_spin->GetValue());
|
||||||
|
}
|
||||||
|
#endif // wxUSE_UIACTIONSIMULATOR
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user