Refactor text to/from double conversion in wxSpinCtrlGenericBase.
The code always used ToDouble() and Format("%g") which was a bit strange for integer-valued wxSpinCtrl. Move the conversions to their own virtual functions for clarity, perhaps correctness and, especially, flexibility as they will be overridden in wxSpinCtrl soon. Also move wxSpinCtrlGenericBase::m_format to wxSpinCtrlDouble as the base class really doesn't need it at all. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72412 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -129,6 +129,10 @@ protected:
|
||||
// Send the correct event type
|
||||
virtual void DoSendEvent() = 0;
|
||||
|
||||
// Convert the text to/from the corresponding value.
|
||||
virtual bool DoTextToValue(const wxString& text, double *val) = 0;
|
||||
virtual wxString DoValueToText(double val) = 0;
|
||||
|
||||
// check if the value is in range
|
||||
bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
|
||||
|
||||
@@ -141,7 +145,6 @@ protected:
|
||||
double m_max;
|
||||
double m_increment;
|
||||
bool m_snap_to_ticks;
|
||||
wxString m_format;
|
||||
|
||||
int m_spin_value;
|
||||
|
||||
@@ -299,6 +302,8 @@ public:
|
||||
protected:
|
||||
virtual void DoSendEvent();
|
||||
|
||||
virtual bool DoTextToValue(const wxString& text, double *val);
|
||||
virtual wxString DoValueToText(double val);
|
||||
DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
|
||||
};
|
||||
|
||||
@@ -311,7 +316,7 @@ protected:
|
||||
class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
|
||||
{
|
||||
public:
|
||||
wxSpinCtrlDouble() : m_digits(0) { }
|
||||
wxSpinCtrlDouble() { Init(); }
|
||||
wxSpinCtrlDouble(wxWindow *parent,
|
||||
wxWindowID id = wxID_ANY,
|
||||
const wxString& value = wxEmptyString,
|
||||
@@ -322,7 +327,8 @@ public:
|
||||
double inc = 1,
|
||||
const wxString& name = wxT("wxSpinCtrlDouble"))
|
||||
{
|
||||
m_digits = 0;
|
||||
Init();
|
||||
|
||||
Create(parent, id, value, pos, size, style,
|
||||
min, max, initial, inc, name);
|
||||
}
|
||||
@@ -360,8 +366,21 @@ public:
|
||||
protected:
|
||||
virtual void DoSendEvent();
|
||||
|
||||
virtual bool DoTextToValue(const wxString& text, double *val);
|
||||
virtual wxString DoValueToText(double val);
|
||||
|
||||
unsigned m_digits;
|
||||
|
||||
private:
|
||||
// Common part of all ctors.
|
||||
void Init()
|
||||
{
|
||||
m_digits = 0;
|
||||
m_format = wxS("%g");
|
||||
}
|
||||
|
||||
wxString m_format;
|
||||
|
||||
DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
|
||||
};
|
||||
|
||||
|
@@ -159,7 +159,6 @@ void wxSpinCtrlGenericBase::Init()
|
||||
m_max = 100;
|
||||
m_increment = 1;
|
||||
m_snap_to_ticks = false;
|
||||
m_format = wxS("%g");
|
||||
|
||||
m_spin_value = 0;
|
||||
|
||||
@@ -207,10 +206,10 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
|
||||
if ( !value.empty() )
|
||||
{
|
||||
double d;
|
||||
if ( value.ToDouble(&d) )
|
||||
if ( DoTextToValue(value, &d) )
|
||||
{
|
||||
m_value = d;
|
||||
m_textCtrl->SetValue(wxString::Format(m_format, m_value));
|
||||
m_textCtrl->SetValue(DoValueToText(m_value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -448,7 +447,7 @@ bool wxSpinCtrlGenericBase::SyncSpinToText()
|
||||
return false;
|
||||
|
||||
double textValue;
|
||||
if ( m_textCtrl->GetValue().ToDouble(&textValue) )
|
||||
if ( DoTextToValue(m_textCtrl->GetValue(), &textValue) )
|
||||
{
|
||||
if (textValue > m_max)
|
||||
textValue = m_max;
|
||||
@@ -476,7 +475,7 @@ void wxSpinCtrlGenericBase::SetValue(const wxString& text)
|
||||
wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") );
|
||||
|
||||
double val;
|
||||
if ( text.ToDouble(&val) && InRange(val) )
|
||||
if ( DoTextToValue(text, &val) && InRange(val) )
|
||||
{
|
||||
DoSetValue(val);
|
||||
}
|
||||
@@ -508,12 +507,12 @@ bool wxSpinCtrlGenericBase::DoSetValue(double val)
|
||||
}
|
||||
}
|
||||
|
||||
wxString str(wxString::Format(m_format.c_str(), val));
|
||||
wxString str(DoValueToText(val));
|
||||
|
||||
if ((val != m_value) || (str != m_textCtrl->GetValue()))
|
||||
{
|
||||
if ( !DoTextToValue(str, &m_value ) ) // wysiwyg for textctrl
|
||||
m_value = val;
|
||||
str.ToDouble( &m_value ); // wysiwyg for textctrl
|
||||
m_textCtrl->SetValue( str );
|
||||
m_textCtrl->SetInsertionPointEnd();
|
||||
m_textCtrl->DiscardEdits();
|
||||
@@ -572,6 +571,22 @@ void wxSpinCtrl::DoSendEvent()
|
||||
GetEventHandler()->ProcessEvent( event );
|
||||
}
|
||||
|
||||
bool wxSpinCtrl::DoTextToValue(const wxString& text, double *val)
|
||||
{
|
||||
long lval;
|
||||
if ( !text.ToLong(&lval) )
|
||||
return false;
|
||||
|
||||
*val = static_cast<double>(lval);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
wxString wxSpinCtrl::DoValueToText(double val)
|
||||
{
|
||||
return wxString::Format("%ld", static_cast<long>(val));
|
||||
}
|
||||
|
||||
#endif // !wxHAS_NATIVE_SPINCTRL
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -589,6 +604,16 @@ void wxSpinCtrlDouble::DoSendEvent()
|
||||
GetEventHandler()->ProcessEvent( event );
|
||||
}
|
||||
|
||||
bool wxSpinCtrlDouble::DoTextToValue(const wxString& text, double *val)
|
||||
{
|
||||
return text.ToDouble(val);
|
||||
}
|
||||
|
||||
wxString wxSpinCtrlDouble::DoValueToText(double val)
|
||||
{
|
||||
return wxString::Format(m_format, val);
|
||||
}
|
||||
|
||||
void wxSpinCtrlDouble::SetDigits(unsigned digits)
|
||||
{
|
||||
wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" );
|
||||
|
Reference in New Issue
Block a user