diff --git a/src/toolbars/SpectralSelectionBar.cpp b/src/toolbars/SpectralSelectionBar.cpp index 713e62a08..fb443452c 100644 --- a/src/toolbars/SpectralSelectionBar.cpp +++ b/src/toolbars/SpectralSelectionBar.cpp @@ -158,12 +158,14 @@ void SpectralSelectionBar::Populate() mLowCtrl = new NumericTextCtrl( NumericConverter::FREQUENCY, this, OnLowID, frequencyFormatName, 0.0); + mLowCtrl->SetInvalidValue(SelectedRegion::UndefinedFrequency); mLowCtrl->SetName(_("Low Frequency:")); mLowCtrl->EnableMenu(); subSizer->Add(mLowCtrl, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5); mHighCtrl = new NumericTextCtrl( NumericConverter::FREQUENCY, this, OnHighID, frequencyFormatName, 0.0); + mHighCtrl->SetInvalidValue(SelectedRegion::UndefinedFrequency); mHighCtrl->SetName(wxString(_("High Frequency:"))); mHighCtrl->EnableMenu(); subSizer->Add(mHighCtrl, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 0); diff --git a/src/widgets/NumericTextCtrl.cpp b/src/widgets/NumericTextCtrl.cpp index a17d65d95..cde2fe674 100644 --- a/src/widgets/NumericTextCtrl.cpp +++ b/src/widgets/NumericTextCtrl.cpp @@ -533,6 +533,7 @@ NumericConverter::NumericConverter(Type type, { ResetMinValue(); ResetMaxValue(); + mInvalidValue = -1.0; mDefaultNdx = 0; @@ -866,7 +867,7 @@ void NumericConverter::ControlsToValue() if (mFields.GetCount() > 0 && mValueString.Mid(mFields[0].pos, 1) == wxChar('-')) { - mValue = -1; + mValue = mInvalidValue; return; } @@ -1169,6 +1170,7 @@ NumericTextCtrl::NumericTextCtrl(NumericConverter::Type type, mAutoPos(autoPos) , mType(type) { + mAllowInvalidValue = false; mDigitBoxW = 10; mDigitBoxH = 16; @@ -1211,6 +1213,7 @@ NumericTextCtrl::~NumericTextCtrl() // Set the focus to the first (left-most) non-zero digit // If all digits are zero, the right-most position is focused +// If all digits are hyphens (invalid), the left-most position is focused void NumericTextCtrl::UpdateAutoFocus() { if (!mAutoPos) @@ -1280,6 +1283,15 @@ void NumericTextCtrl::EnableMenu(bool enable) Fit(); } +void NumericTextCtrl::SetInvalidValue(double invalidValue) +{ + const bool wasInvalid = mAllowInvalidValue && (mValue == mInvalidValue); + mAllowInvalidValue = true; + mInvalidValue = invalidValue; + if (wasInvalid) + SetValue(invalidValue); +} + bool NumericTextCtrl::Layout() { unsigned int i, j; @@ -1596,6 +1608,7 @@ void NumericTextCtrl::OnCaptureKey(wxCommandEvent &event) case WXK_TAB: case WXK_RETURN: case WXK_NUMPAD_ENTER: + case '-': return; default: @@ -1618,6 +1631,7 @@ void NumericTextCtrl::OnKeyUp(wxKeyEvent &event) keyCode -= WXK_NUMPAD0 - '0'; if ((keyCode >= '0' && keyCode <= '9') || + (keyCode == '-') || (keyCode == WXK_BACK) || (keyCode == WXK_UP) || (keyCode == WXK_DOWN)) { @@ -1661,6 +1675,11 @@ void NumericTextCtrl::OnKeyDown(wxKeyEvent &event) Updated(); } + else if (!mReadOnly && keyCode == '-') { + if (mAllowInvalidValue) + SetValue(mInvalidValue); + } + else if (!mReadOnly && keyCode == WXK_BACK) { // Moves left, replaces that char with '0', stays there... mFocusedDigit--; diff --git a/src/widgets/NumericTextCtrl.h b/src/widgets/NumericTextCtrl.h index 87cfec599..9e1e20aaa 100644 --- a/src/widgets/NumericTextCtrl.h +++ b/src/widgets/NumericTextCtrl.h @@ -75,6 +75,7 @@ public: void ResetMinValue(); void SetMaxValue(double maxValue); void ResetMaxValue(); + double GetValue(); wxString GetString(); @@ -101,6 +102,7 @@ protected: double mMinValue; double mMaxValue; + double mInvalidValue; wxString mFormatString; @@ -154,6 +156,12 @@ class NumericTextCtrl: public wxControl, public NumericConverter void SetReadOnly(bool readOnly = true); void EnableMenu(bool enable = true); + // The text control permits typing '-' to make the value invalid only if this + // function has previously been called. + // Maybe you want something other than the default of -1 to indicate the invalid value + // this control returns to the program, so you can specify. + void SetInvalidValue(double invalidValue); + int GetFocusedField() { return mLastField; } int GetFocusedDigit() { return mFocusedDigit; } @@ -207,6 +215,8 @@ private: NumericConverter::Type mType; + bool mAllowInvalidValue; + DECLARE_EVENT_TABLE() };