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
/**
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
// 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;
}
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();
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