Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: position copy/paste clipboard #89

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading