diff --git a/samples/widgets/slider.cpp b/samples/widgets/slider.cpp index 32bf2fad60..4fbda64f11 100644 --- a/samples/widgets/slider.cpp +++ b/samples/widgets/slider.cpp @@ -120,6 +120,7 @@ protected: void OnCheckOrRadioBox(wxCommandEvent& event); void OnSlider(wxScrollEvent& event); + void OnSlider(wxCommandEvent& event); void OnUpdateUIValueButton(wxUpdateUIEvent& event); void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event); @@ -230,6 +231,7 @@ wxBEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage) EVT_UPDATE_UI(SliderPage_CurValueText, SliderWidgetsPage::OnUpdateUICurValueText) EVT_COMMAND_SCROLL(SliderPage_Slider, SliderWidgetsPage::OnSlider) + EVT_SLIDER(SliderPage_Slider, SliderWidgetsPage::OnSlider) EVT_CHECKBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox) EVT_RADIOBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox) @@ -845,4 +847,12 @@ void SliderWidgetsPage::OnSlider(wxScrollEvent& event) 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 diff --git a/src/msw/slider.cpp b/src/msw/slider.cpp index d859ea5d55..3cb247965d 100644 --- a/src/msw/slider.cpp +++ b/src/msw/slider.cpp @@ -313,15 +313,28 @@ bool wxSlider::MSWOnScroll(int WXUNUSED(orientation), SetValue(newPos); wxScrollEvent event(scrollEvent, m_windowId); + bool processed = false; + event.SetPosition(newPos); event.SetEventObject( this ); - HandleWindowEvent(event); + processed = HandleWindowEvent(event); - wxCommandEvent cevent( wxEVT_SLIDER, GetId() ); - cevent.SetInt( newPos ); - cevent.SetEventObject( this ); + // Do not generate wxEVT_SLIDER when the native scroll message + // parameter is SB_ENDSCROLL, which always follows only after + // 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) diff --git a/tests/controls/slidertest.cpp b/tests/controls/slidertest.cpp index 9ecc97327c..aac59a2bca 100644 --- a/tests/controls/slidertest.cpp +++ b/tests/controls/slidertest.cpp @@ -35,6 +35,7 @@ private: #ifndef __WXOSX__ WXUISIM_TEST( PageUpDown ); WXUISIM_TEST( LineUpDown ); + WXUISIM_TEST( EvtSlider ); WXUISIM_TEST( LinePageSize ); #endif CPPUNIT_TEST( Value ); @@ -47,6 +48,7 @@ private: void PageUpDown(); void LineUpDown(); + void EvtSlider(); void LinePageSize(); void Value(); void Range(); @@ -125,6 +127,24 @@ void SliderTestCase::LineUpDown() #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() { #if wxUSE_UIACTIONSIMULATOR