diff --git a/src/effects/nyquist/Nyquist.cpp b/src/effects/nyquist/Nyquist.cpp index 6badba124..3a311f7c1 100644 --- a/src/effects/nyquist/Nyquist.cpp +++ b/src/effects/nyquist/Nyquist.cpp @@ -65,6 +65,7 @@ effects from this one class. #include #include #include +#include #include WX_DEFINE_OBJARRAY(NyqControlArray); @@ -785,7 +786,7 @@ bool EffectNyquist::ProcessOne() // decimal separator which may be a comma in some countries. cmd += wxString::Format(wxT("(setf %s %s)\n"), mControls[j].var.c_str(), - Internat::ToString(mControls[j].val).c_str()); + Internat::ToString(mControls[j].val, 14).c_str()); } else if (mControls[j].type == NYQ_CTRL_INT || mControls[j].type == NYQ_CTRL_CHOICE) { @@ -1225,13 +1226,22 @@ NyquistDialog::NyquistDialog(wxWindow * parent, wxWindowID id, wxDefaultPosition, wxSize(60, -1)); item->SetName(ctrl->name); if (ctrl->type == NYQ_CTRL_REAL) { - wxFloatingPointValidator vld(2, &ctrl->val); - vld.SetRange(ctrl->low, ctrl->high); + // > 12 decimal places can cause rounding errors in display. + wxFloatingPointValidator vld(12, &ctrl->val); + vld.SetRange(-FLT_MAX, FLT_MAX); + // Set number of decimal places + if (ctrl->high - ctrl->low < 10) { + vld.SetStyle(wxNUM_VAL_THREE_TRAILING_ZEROES); + } else if (ctrl->high - ctrl->low < 100) { + vld.SetStyle(wxNUM_VAL_TWO_TRAILING_ZEROES); + } else { + vld.SetStyle(wxNUM_VAL_ONE_TRAILING_ZERO); + } item->SetValidator(vld); } else { wxIntegerValidator vld(&ctrl->val); - vld.SetRange(ctrl->low, ctrl->high); + vld.SetRange(INT_MIN, INT_MAX); item->SetValidator(vld); } @@ -1355,7 +1365,9 @@ void NyquistDialog::OnText(wxCommandEvent &event) wxTextCtrl *text = (wxTextCtrl *)FindWindow(ID_NYQ_TEXT + ctrlId); wxASSERT(text); - if (ctrl->type != NYQ_CTRL_STRING) { + if (ctrl->type == NYQ_CTRL_STRING) { + ctrl->valStr = text->GetValue(); + } else { text->GetValidator()->TransferFromWindow(); wxSlider *slider = (wxSlider *)FindWindow(ID_NYQ_SLIDER + ctrlId); wxASSERT(slider); diff --git a/src/widgets/numformatter.cpp b/src/widgets/numformatter.cpp index f7bfae4af..8277231f2 100644 --- a/src/widgets/numformatter.cpp +++ b/src/widgets/numformatter.cpp @@ -32,6 +32,7 @@ #include "wx/intl.h" #include // for setlocale and LC_ALL +#include // ---------------------------------------------------------------------------- // local helpers @@ -222,6 +223,12 @@ wxString wxNumberFormatter::PostProcessIntString(wxString s, int style) wxASSERT_MSG( !(style & Style_NoTrailingZeroes), wxT("Style_NoTrailingZeroes can't be used with integer values") ); + wxASSERT_MSG( !(style & Style_OneTrailingZero), + wxT("Style_OneTrailingZero can't be used with integer values") ); + wxASSERT_MSG( !(style & Style_TwoTrailingZeroes), + wxT("Style_TwoTrailingZeroes can't be used with integer values") ); + wxASSERT_MSG( !(style & Style_ThreeTrailingZeroes), + wxT("Style_ThreeTrailingZeroes can't be used with integer values") ); return s; } @@ -258,9 +265,20 @@ wxString wxNumberFormatter::ToString(double val, int precision, int style) if ( style & Style_WithThousandsSep ) AddThousandsSeparators(s); - if ( style & Style_NoTrailingZeroes ) - RemoveTrailingZeroes(s); + if ( precision != -1 ) + { + if ( style & Style_NoTrailingZeroes ) + RemoveTrailingZeroes(s, 0); + if ( style & Style_OneTrailingZero ) + RemoveTrailingZeroes(s, 1); + + if ( style & Style_TwoTrailingZeroes ) + RemoveTrailingZeroes(s, 2); + + if ( style & Style_ThreeTrailingZeroes ) + RemoveTrailingZeroes(s, 3); + } return s; } @@ -295,21 +313,24 @@ void wxNumberFormatter::AddThousandsSeparators(wxString& s) } } -void wxNumberFormatter::RemoveTrailingZeroes(wxString& s) +void wxNumberFormatter::RemoveTrailingZeroes(wxString& s, size_t retain /* = 0 */) { - const size_t posDecSep = s.find(GetDecimalSeparator()); - wxCHECK_RET( posDecSep != wxString::npos, - wxString::Format(wxT("No decimal separator in \"%s\""), s.c_str()) ); - wxCHECK_RET( posDecSep, wxT("Can't start with decimal separator" )); + const size_t posDecSep = s.find(GetDecimalSeparator()); + wxCHECK_RET( posDecSep != wxString::npos, + wxString::Format(wxT("No decimal separator in \"%s\""), s.c_str()) ); + wxCHECK_RET( posDecSep, wxT("Can't start with decimal separator" )); - // Find the last character to keep. - size_t posLastNonZero = s.find_last_not_of(wxT("0")); + // Find the last character to keep. + size_t posLastCharacterToKeep = s.find_last_not_of(wxT("0")); - // If it's the decimal separator itself, don't keep it neither. - if ( posLastNonZero == posDecSep ) - posLastNonZero--; + // If it's the decimal separator itself, remove it. + if ((posLastCharacterToKeep == posDecSep) && (retain == 0)) { + posLastCharacterToKeep--; + } else if ((posLastCharacterToKeep - posDecSep) < retain) { + posLastCharacterToKeep = retain + posDecSep; + } - s.erase(posLastNonZero + 1); + s.erase(posLastCharacterToKeep + 1); } // ---------------------------------------------------------------------------- diff --git a/src/widgets/numformatter.h b/src/widgets/numformatter.h index ab3a68f52..f32306dd7 100644 --- a/src/widgets/numformatter.h +++ b/src/widgets/numformatter.h @@ -26,9 +26,12 @@ public: // Bit masks for ToString() enum Style { - Style_None = 0x00, - Style_WithThousandsSep = 0x01, - Style_NoTrailingZeroes = 0x02 // Only for floating point numbers + Style_None = 0x00, + Style_WithThousandsSep = 0x01, + Style_NoTrailingZeroes = 0x02, // Only for floating point numbers + Style_OneTrailingZero = 0x04, // Only for floating point numbers + Style_TwoTrailingZeroes = 0x08, // Only for floating point numbers + Style_ThreeTrailingZeroes = 0x10 // Only for floating point numbers }; // Format a number as a string. By default, the thousands separator is @@ -75,7 +78,7 @@ private: // Remove trailing zeroes and, if there is nothing left after it, the // decimal separator itself from a string representing a floating point // number. Also used by ToString(). - static void RemoveTrailingZeroes(wxString& s); + static void RemoveTrailingZeroes(wxString& s, size_t retain = 0); // Remove all thousands separators from a string representing a number. static void RemoveThousandsSeparators(wxString& s); diff --git a/src/widgets/valnum.cpp b/src/widgets/valnum.cpp index 1c2abf47e..2bde5ff2c 100644 --- a/src/widgets/valnum.cpp +++ b/src/widgets/valnum.cpp @@ -52,6 +52,12 @@ int wxNumValidatorBase::GetFormatFlags() const flags |= wxNumberFormatter::Style_WithThousandsSep; if ( m_style & wxNUM_VAL_NO_TRAILING_ZEROES ) flags |= wxNumberFormatter::Style_NoTrailingZeroes; + if ( m_style & wxNUM_VAL_ONE_TRAILING_ZERO ) + flags |= wxNumberFormatter::Style_OneTrailingZero; + if ( m_style & wxNUM_VAL_TWO_TRAILING_ZEROES ) + flags |= wxNumberFormatter::Style_TwoTrailingZeroes; + if ( m_style & wxNUM_VAL_THREE_TRAILING_ZEROES ) + flags |= wxNumberFormatter::Style_ThreeTrailingZeroes; return flags; } @@ -227,6 +233,13 @@ wxIntegerValidatorBase::IsCharOk(const wxString& val, int pos, wxChar ch) const if ( !FromString(GetValueAfterInsertingChar(val, pos, ch), &value) ) return false; + wxString smin = ToString(m_min); + wxString smax = ToString(m_max); + if ( pos < (int) smin.Length() ) + return true; + if ( pos < (int) smax.Length() - 1 ) + return true; + return IsInRange(value); } diff --git a/src/widgets/valnum.h b/src/widgets/valnum.h index 2dae214d3..a049eb3a9 100644 --- a/src/widgets/valnum.h +++ b/src/widgets/valnum.h @@ -27,7 +27,10 @@ enum wxNumValidatorStyle wxNUM_VAL_DEFAULT = 0x0, wxNUM_VAL_THOUSANDS_SEPARATOR = 0x1, wxNUM_VAL_ZERO_AS_BLANK = 0x2, - wxNUM_VAL_NO_TRAILING_ZEROES = 0x4 + wxNUM_VAL_NO_TRAILING_ZEROES = 0x4, + wxNUM_VAL_ONE_TRAILING_ZERO = 0x8, + wxNUM_VAL_TWO_TRAILING_ZEROES = 0x10, + wxNUM_VAL_THREE_TRAILING_ZEROES = 0x20 }; // ---------------------------------------------------------------------------- @@ -265,6 +268,12 @@ protected: { wxASSERT_MSG( !(style & wxNUM_VAL_NO_TRAILING_ZEROES), wxT("This style doesn't make sense for integers.") ); + wxASSERT_MSG( !(style & wxNUM_VAL_ONE_TRAILING_ZERO), + wxT("This style doesn't make sense for integers.") ); + wxASSERT_MSG( !(style & wxNUM_VAL_TWO_TRAILING_ZEROES), + wxT("This style doesn't make sense for integers.") ); + wxASSERT_MSG( !(style & wxNUM_VAL_THREE_TRAILING_ZEROES), + wxT("This style doesn't make sense for integers.") ); } wxIntegerValidatorBase(const wxIntegerValidatorBase& other)