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