]> git.saurik.com Git - wxWidgets.git/commitdiff
Refactor text to/from double conversion in wxSpinCtrlGenericBase.
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 30 Aug 2012 20:23:49 +0000 (20:23 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 30 Aug 2012 20:23:49 +0000 (20:23 +0000)
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

include/wx/generic/spinctlg.h
src/generic/spinctlg.cpp

index a8dad8de5c8bf0797f2403a58e3e9ec16783f3d4..4492bedef0b9b9f2f448ed4c4bf4c86027beebc9 100644 (file)
@@ -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)
 };
 
index 9a2a5eb4e4d30773d38a1ce28e3eab6cb40c58ec..5f6bc425122e1dc6779d52e660a3217b2e87659f 100644 (file)
@@ -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()))
     {
-        m_value = val;
-        str.ToDouble( &m_value );    // wysiwyg for textctrl
+        if ( !DoTextToValue(str, &m_value ) )    // wysiwyg for textctrl
+            m_value = val;
         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" );