Fix generation of extraneous wxEVT_SLIDER events in wxMSW

Don't send the event when it's redundant, i.e. doesn't really notify
about the change in the slider value.

Also add a test case for wxEVT_SLIDER and show these events in the
widgets sample.

Closes https://github.com/wxWidgets/wxWidgets/pull/2080

Closes #18929.
This commit is contained in:
PB
2020-10-07 18:18:55 +02:00
committed by Vadim Zeitlin
parent 7ed330a197
commit 952e5f32cd
3 changed files with 48 additions and 5 deletions

View File

@@ -120,6 +120,7 @@ protected:
void OnCheckOrRadioBox(wxCommandEvent& event); void OnCheckOrRadioBox(wxCommandEvent& event);
void OnSlider(wxScrollEvent& event); void OnSlider(wxScrollEvent& event);
void OnSlider(wxCommandEvent& event);
void OnUpdateUIValueButton(wxUpdateUIEvent& event); void OnUpdateUIValueButton(wxUpdateUIEvent& event);
void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event); void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
@@ -230,6 +231,7 @@ wxBEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage)
EVT_UPDATE_UI(SliderPage_CurValueText, SliderWidgetsPage::OnUpdateUICurValueText) EVT_UPDATE_UI(SliderPage_CurValueText, SliderWidgetsPage::OnUpdateUICurValueText)
EVT_COMMAND_SCROLL(SliderPage_Slider, SliderWidgetsPage::OnSlider) EVT_COMMAND_SCROLL(SliderPage_Slider, SliderWidgetsPage::OnSlider)
EVT_SLIDER(SliderPage_Slider, SliderWidgetsPage::OnSlider)
EVT_CHECKBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox) EVT_CHECKBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox) EVT_RADIOBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox)
@@ -845,4 +847,12 @@ void SliderWidgetsPage::OnSlider(wxScrollEvent& event)
event.GetInt()); event.GetInt());
} }
void SliderWidgetsPage::OnSlider(wxCommandEvent& event)
{
static int s_numSliderEvents = 0;
wxLogMessage("Slider event #%d: wxEVT_SLIDER (value = %d)",
s_numSliderEvents++, event.GetInt());
}
#endif // wxUSE_SLIDER #endif // wxUSE_SLIDER

View File

@@ -313,15 +313,28 @@ bool wxSlider::MSWOnScroll(int WXUNUSED(orientation),
SetValue(newPos); SetValue(newPos);
wxScrollEvent event(scrollEvent, m_windowId); wxScrollEvent event(scrollEvent, m_windowId);
bool processed = false;
event.SetPosition(newPos); event.SetPosition(newPos);
event.SetEventObject( this ); event.SetEventObject( this );
HandleWindowEvent(event); processed = HandleWindowEvent(event);
wxCommandEvent cevent( wxEVT_SLIDER, GetId() ); // Do not generate wxEVT_SLIDER when the native scroll message
cevent.SetInt( newPos ); // parameter is SB_ENDSCROLL, which always follows only after
cevent.SetEventObject( this ); // another scroll message which already changed the slider value.
// Therefore, sending wxEVT_SLIDER after SB_ENDSCROLL
// would result in two wxEVT_SLIDER events with the same value.
if ( wParam != SB_ENDSCROLL )
{
wxCommandEvent cevent( wxEVT_SLIDER, GetId() );
return HandleWindowEvent( cevent ); cevent.SetInt( newPos );
cevent.SetEventObject( this );
processed = HandleWindowEvent( cevent );
}
return processed;
} }
void wxSlider::Command (wxCommandEvent & event) void wxSlider::Command (wxCommandEvent & event)

View File

@@ -35,6 +35,7 @@ private:
#ifndef __WXOSX__ #ifndef __WXOSX__
WXUISIM_TEST( PageUpDown ); WXUISIM_TEST( PageUpDown );
WXUISIM_TEST( LineUpDown ); WXUISIM_TEST( LineUpDown );
WXUISIM_TEST( EvtSlider );
WXUISIM_TEST( LinePageSize ); WXUISIM_TEST( LinePageSize );
#endif #endif
CPPUNIT_TEST( Value ); CPPUNIT_TEST( Value );
@@ -47,6 +48,7 @@ private:
void PageUpDown(); void PageUpDown();
void LineUpDown(); void LineUpDown();
void EvtSlider();
void LinePageSize(); void LinePageSize();
void Value(); void Value();
void Range(); void Range();
@@ -125,6 +127,24 @@ void SliderTestCase::LineUpDown()
#endif #endif
} }
void SliderTestCase::EvtSlider()
{
#if wxUSE_UIACTIONSIMULATOR
EventCounter slider(m_slider, wxEVT_SLIDER);
wxUIActionSimulator sim;
wxYield();
m_slider->SetFocus();
sim.Char(WXK_UP);
sim.Char(WXK_DOWN);
wxYield();
CPPUNIT_ASSERT_EQUAL(2, slider.GetCount());
#endif
}
void SliderTestCase::LinePageSize() void SliderTestCase::LinePageSize()
{ {
#if wxUSE_UIACTIONSIMULATOR #if wxUSE_UIACTIONSIMULATOR