From: Vadim Zeitlin Date: Wed, 9 May 2012 14:24:20 +0000 (+0000) Subject: Correct the initial value setting in wxMSW wxSpinCtrl. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/6e36db5ecaca60f633fa3321b30feae863eaeecd Correct the initial value setting in wxMSW wxSpinCtrl. Always use value argument for the text control contents and also override the initial numeric value with it if it's numeric. This seems to be the only consistent thing to do, so document this behaviour and add a unit test checking for it. Closes #13589. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71387 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/interface/wx/spinctrl.h b/interface/wx/spinctrl.h index 7a735a1d61..fac812cb30 100644 --- a/interface/wx/spinctrl.h +++ b/interface/wx/spinctrl.h @@ -62,6 +62,13 @@ public: /** Constructor, creating and showing a spin control. + If @a value is non-empty, it will be shown in the text entry part of + the control and if it has numeric value, the initial numeric value of + the control, as returned by GetValue() will also be determined by it + instead of by @a initial. Hence, it only makes sense to specify @a + initial if @a value is a non-empty string not convertible to a number, + otherwise @a initial is simply ignored. + @param parent Parent window. Must not be @NULL. @param value diff --git a/src/msw/spinctrl.cpp b/src/msw/spinctrl.cpp index 71c1221f49..d5b95d4115 100644 --- a/src/msw/spinctrl.cpp +++ b/src/msw/spinctrl.cpp @@ -389,22 +389,25 @@ bool wxSpinCtrl::Create(wxWindow *parent, // associate the text window with the spin button (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); + // If the initial text value is actually a number, it overrides the + // "initial" argument specified later. + long initialFromText; + if ( value.ToLong(&initialFromText) ) + initial = initialFromText; + SetValue(initial); + m_oldValue = initial; + // Set the range in the native control SetRange(min, max); - // If necessary, set the textual value. Don't do it if it's the same as the - // numeric value though. - if ( value != wxString::Format("%d", initial) ) - { + // Also set the text part of the control if it was specified independently + // but don't generate an event for this, it would be unexpected. + m_blockEvent = true; + if ( !value.empty() ) SetValue(value); - m_oldValue = (int) wxAtol(value); - } - else - { - m_oldValue = initial; - } + m_blockEvent = false; return true; } diff --git a/tests/controls/spinctrltest.cpp b/tests/controls/spinctrltest.cpp index f6801f4b0f..5b677f7571 100644 --- a/tests/controls/spinctrltest.cpp +++ b/tests/controls/spinctrltest.cpp @@ -33,12 +33,14 @@ public: private: CPPUNIT_TEST_SUITE( SpinCtrlTestCase ); + CPPUNIT_TEST( Initial ); WXUISIM_TEST( Arrows ); WXUISIM_TEST( Wrap ); CPPUNIT_TEST( Range ); CPPUNIT_TEST( Value ); CPPUNIT_TEST_SUITE_END(); + void Initial(); void Arrows(); void Wrap(); void Range(); @@ -65,6 +67,28 @@ void SpinCtrlTestCase::tearDown() wxDELETE(m_spin); } +void SpinCtrlTestCase::Initial() +{ + // Initial value is defined by "initial" argument which is 0 by default. + CPPUNIT_ASSERT_EQUAL( 0, m_spin->GetValue() ); + + wxWindow* const parent = m_spin->GetParent(); + + // Recreate the control with another "initial" to check this. + delete m_spin; + m_spin = new wxSpinCtrl(parent, wxID_ANY, "", + wxDefaultPosition, wxDefaultSize, 0, + 0, 100, 17); + CPPUNIT_ASSERT_EQUAL( 17, m_spin->GetValue() ); + + // But if the text string is specified, it takes precedence. + delete m_spin; + m_spin = new wxSpinCtrl(parent, wxID_ANY, "99", + wxDefaultPosition, wxDefaultSize, 0, + 0, 100, 17); + CPPUNIT_ASSERT_EQUAL( 99, m_spin->GetValue() ); +} + void SpinCtrlTestCase::Arrows() { #if wxUSE_UIACTIONSIMULATOR