diff --git a/source/numbertextctrl.cpp b/source/numbertextctrl.cpp index bbd4ca2f..51004bc4 100644 --- a/source/numbertextctrl.cpp +++ b/source/numbertextctrl.cpp @@ -21,27 +21,29 @@ BEGIN_EVENT_TABLE(NumberTextCtrl, wxTextCtrl) EVT_KILL_FOCUS(NumberTextCtrl::OnKillFocus) EVT_TEXT_ENTER(wxID_ANY, NumberTextCtrl::OnTextEnter) +EVT_TEXT(wxID_ANY, NumberTextCtrl::EnsureOnlyNumbers) END_EVENT_TABLE() -NumberTextCtrl::NumberTextCtrl(wxWindow* parent, wxWindowID id, long value, long minvalue, long maxvalue, const wxPoint &pos, const wxSize &sz, long style, const wxString &name) : - wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name), - minval(minvalue), maxval(maxvalue), lastval(value) { - //// +void NumberTextCtrl::OnKillFocus(wxFocusEvent &evt) { + CheckRange(); + evt.Skip(); } -NumberTextCtrl::NumberTextCtrl(wxWindow* parent, wxWindowID id, long value, long minvalue, long maxvalue, long style, const wxString &name, const wxPoint &pos, const wxSize &sz) : - wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name), - minval(minvalue), maxval(maxvalue), lastval(value) { - //// -} +wxString NumberTextCtrl::TextFilterDigits(const wxString &text) { + wxString newText; + for (size_t position = 0; position < text.size(); ++position) { + if (text[position] >= '0' && text[position] <= '9') { + newText.Append(text[position]); + } + } -NumberTextCtrl::~NumberTextCtrl() { - //// + return newText; } -void NumberTextCtrl::OnKillFocus(wxFocusEvent &evt) { - CheckRange(); - evt.Skip(); +void NumberTextCtrl::EnsureOnlyNumbers(wxCommandEvent &evt) { + const auto newText = TextFilterDigits(GetValue().ToStdString()); + + ChangeValue(newText); } void NumberTextCtrl::OnTextEnter(wxCommandEvent &evt) { @@ -49,66 +51,56 @@ void NumberTextCtrl::OnTextEnter(wxCommandEvent &evt) { } void NumberTextCtrl::SetIntValue(long value) { - wxString sv; - sv << value; // Will generate events - SetValue(sv); + SetValue(wxString::Format("%i", value)); } long NumberTextCtrl::GetIntValue() { long l; - if (GetValue().ToLong(&l)) { - return l; - } - return 0; + return GetValue().ToLong(&l) ? l : 0; } void NumberTextCtrl::SetMinValue(long value) { - if (value == minval) { + if (value == minValue) { return; } - minval = value; + minValue = value; CheckRange(); } void NumberTextCtrl::SetMaxValue(long value) { - if (value == maxval) { + if (value == maxValue) { return; } - maxval = value; + maxValue = value; CheckRange(); } void NumberTextCtrl::CheckRange() { auto text = GetValue().ToStdString(); - wxString ntext; - for (size_t s = 0; s < text.size(); ++s) { - if (text[s] >= '0' && text[s] <= '9') { - ntext.Append(text[s]); - } - } + auto newText = TextFilterDigits(text); // Check that value is in range long v; - if (ntext.size() != 0 && ntext.ToLong(&v)) { - if (v < minval) { - v = minval; - } else if (v > maxval) { - v = maxval; + if (newText.size() != 0 && newText.ToLong(&v)) { + if (v < minValue) { + v = minValue; + } else if (v > maxValue) { + v = maxValue; } - ntext.clear(); - ntext << v; - lastval = v; + newText.clear(); + newText = wxString::Format("%i", v); + lastValue = v; } else { - ntext.clear(); - ntext << lastval; + newText.clear(); + newText = wxString::Format("%i", lastValue); } // Check if there was any change - if (ntext != text) { + if (newText != text) { // ChangeValue doesn't generate events - ChangeValue(ntext); + ChangeValue(newText); } } diff --git a/source/numbertextctrl.h b/source/numbertextctrl.h index 8db42ba0..e848eb3f 100644 --- a/source/numbertextctrl.h +++ b/source/numbertextctrl.h @@ -21,12 +21,21 @@ // Text ctrl that only allows number input class NumberTextCtrl : public wxTextCtrl { public: - NumberTextCtrl(wxWindow* parent, wxWindowID id = wxID_ANY, long value = 0, long minvalue = 0, long maxvalue = 100, const wxPoint &pos = wxDefaultPosition, const wxSize &sz = wxDefaultSize, long style = 0, const wxString &name = wxTextCtrlNameStr); - NumberTextCtrl(wxWindow* parent, wxWindowID id = wxID_ANY, long value = 0, long minvalue = 0, long maxvalue = 100, long style = 0, const wxString &name = wxTextCtrlNameStr, const wxPoint &pos = wxDefaultPosition, const wxSize &sz = wxDefaultSize); - ~NumberTextCtrl(); + // Please avoid using wxTextValidator rather than wxFILTER_NONE, because it prevents the event clipboard paste to be fired. + NumberTextCtrl(wxWindow* parent, wxWindowID id, long value, long minValue, long maxValue, const wxPoint &pos, const wxSize &sz, long style, const wxString &name) : + wxTextCtrl(parent, id, wxString::Format("%i", value), pos, sz, style, wxTextValidator(wxFILTER_NONE), name), + minValue(minValue), maxValue(maxValue), lastValue(value) { } + // Please avoid using wxTextValidator rather than wxFILTER_NONE, because it prevents the event clipboard paste to be fired. + NumberTextCtrl(wxWindow* parent, wxWindowID id, long value, long minValue, long maxValue, long style, const wxString &name, const wxPoint &pos, const wxSize &sz) : + wxTextCtrl(parent, id, wxString::Format("%i", value), pos, sz, style, wxTextValidator(wxFILTER_NONE), name), + minValue(minValue), maxValue(maxValue), lastValue(value) { } + ~NumberTextCtrl() = default; void OnKillFocus(wxFocusEvent &); void OnTextEnter(wxCommandEvent &); + void EnsureOnlyNumbers(wxCommandEvent &evt); + + wxString TextFilterDigits(const wxString &string); long GetIntValue(); void SetIntValue(long value); @@ -37,7 +46,7 @@ class NumberTextCtrl : public wxTextCtrl { protected: void CheckRange(); - long minval, maxval, lastval; + long minValue, maxValue, lastValue; DECLARE_EVENT_TABLE(); };