Skip to content

Commit

Permalink
fix: position copy/paste clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed Aug 7, 2024
1 parent 639c26a commit 676949d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
78 changes: 35 additions & 43 deletions source/numbertextctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,94 +21,86 @@
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) {
CheckRange();
}

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);
}
}
17 changes: 13 additions & 4 deletions source/numbertextctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -37,7 +46,7 @@ class NumberTextCtrl : public wxTextCtrl {
protected:
void CheckRange();

long minval, maxval, lastval;
long minValue, maxValue, lastValue;
DECLARE_EVENT_TABLE();
};

Expand Down

0 comments on commit 676949d

Please sign in to comment.