Skip to content

Commit

Permalink
Fix #103
Browse files Browse the repository at this point in the history
The NumberTextCtrl does not delete the selected text on Linux as
expected. Now  the range check is performed after lost focus or pressed
enter key.
  • Loading branch information
Mignari committed Jul 23, 2015
1 parent b0f32fd commit b17411c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
80 changes: 38 additions & 42 deletions source/numbertextctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
// $Id: numbertextctrl.hpp 280 2010-02-14 23:46:31Z admin $

#include "main.h"

#include "numbertextctrl.h"

BEGIN_EVENT_TABLE(NumberTextCtrl, wxTextCtrl)
EVT_TEXT(wxID_ANY, NumberTextCtrl::OnEnterText)
EVT_KILL_FOCUS(NumberTextCtrl::OnKillFocus)
EVT_TEXT_ENTER(wxID_ANY, NumberTextCtrl::OnTextEnter)
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, wxDefaultValidator, name),
wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name),
minval(minvalue), maxval(maxvalue), lastval(value)
{
}
Expand All @@ -38,7 +38,7 @@ 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, wxDefaultValidator, name),
wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name),
minval(minvalue), maxval(maxvalue), lastval(value)
{
}
Expand All @@ -47,67 +47,63 @@ NumberTextCtrl::~NumberTextCtrl()
{
}

void NumberTextCtrl::OnEnterText(wxCommandEvent& evt)
void NumberTextCtrl::OnKillFocus(wxFocusEvent& evt)
{
CheckRange();
evt.Skip();
}

void NumberTextCtrl::OnTextEnter(wxCommandEvent& evt)
{
CheckRange();
}

void NumberTextCtrl::SetIntValue(long v)
{
wxString sv;
sv << v;
// Will generate events
SetValue(sv);
}

long NumberTextCtrl::GetIntValue()
{
long l;
if(GetValue().ToLong(&l))
return l;
return 0;
}

void NumberTextCtrl::CheckRange()
{
//printf("%d\n", (int)GetInsertionPoint());
wxString text = GetValue();
wxString ntext;

int p = GetInsertionPoint();
if(text.size() && text[0] == wxT('-'))
ntext.Append(wxT('-'));

for(size_t s = 0; s < text.size(); ++s)
{
if(text[s] >= '0' && text[s] <= '9')
for(size_t s = 0; s < text.size(); ++s) {
if (text[s] >= '0' && text[s] <= '9')
ntext.Append(text[s]);
else
--p;
}

// Check that value is in range
long v;
if(ntext.ToLong(&v))
{
if(v < minval)
if(ntext.size() != 0 && ntext.ToLong(&v)) {
if (v < minval)
v = minval;
else if(v > maxval)
v = maxval;

ntext.clear();
ntext << v;
lastval = v;
}
else
{
} else {
ntext.clear();
ntext << lastval;
}

// Check if there was any change
if(ntext != text)
{
// User input was invalid, change the text to the cleaned one.

if(ntext != text) {
// ChangeValue doesn't generate events
ChangeValue(ntext);
SetInsertionPoint(p);
}
}

void NumberTextCtrl::SetIntValue(long v)
{
wxString sv;
sv << v;
// Will generate events
SetValue(sv);
}

long NumberTextCtrl::GetIntValue()
{
long l;
if(GetValue().ToLong(&l))
return l;
return 0;
}

6 changes: 5 additions & 1 deletion source/numbertextctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ class NumberTextCtrl : public wxTextCtrl
const wxPoint& pos = wxDefaultPosition, const wxSize& sz = wxDefaultSize);
~NumberTextCtrl();

void OnEnterText(wxCommandEvent&);
void OnKillFocus(wxFocusEvent&);
void OnTextEnter(wxCommandEvent&);

long GetIntValue();
void SetIntValue(long v);

protected:
void CheckRange();

long minval, maxval, lastval;
DECLARE_EVENT_TABLE();
};
Expand Down
6 changes: 3 additions & 3 deletions source/positionctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
PositionCtrl::PositionCtrl(wxWindow* parent, const wxString& label, long x, long y, long z, long maxx, long maxy, long maxz) :
wxStaticBoxSizer(wxHORIZONTAL, parent, label)
{
x_field = newd NumberTextCtrl(parent, wxID_ANY, x, 0, maxx, wxWANTS_CHARS, wxT("X"), wxDefaultPosition, wxSize(60, 20));
x_field = newd NumberTextCtrl(parent, wxID_ANY, x, 0, maxx, wxTE_PROCESS_ENTER, wxT("X"), wxDefaultPosition, wxSize(60, 20));
x_field->Bind(wxEVT_TEXT_PASTE, &PositionCtrl::OnClipboardText, this);
Add(x_field, 2, wxEXPAND | wxLEFT | wxBOTTOM, 5);

y_field = newd NumberTextCtrl(parent, wxID_ANY, y, 0, maxy, wxWANTS_CHARS, wxT("Y"), wxDefaultPosition, wxSize(60, 20));
y_field = newd NumberTextCtrl(parent, wxID_ANY, y, 0, maxy, wxTE_PROCESS_ENTER, wxT("Y"), wxDefaultPosition, wxSize(60, 20));
y_field->Bind(wxEVT_TEXT_PASTE, &PositionCtrl::OnClipboardText, this);
Add(y_field, 2, wxEXPAND | wxLEFT | wxBOTTOM, 5);

z_field = newd NumberTextCtrl(parent, wxID_ANY, z, 0, maxz, wxWANTS_CHARS, wxT("Z"), wxDefaultPosition, wxSize(35, 20));
z_field = newd NumberTextCtrl(parent, wxID_ANY, z, 0, maxz, wxTE_PROCESS_ENTER, wxT("Z"), wxDefaultPosition, wxSize(35, 20));
z_field->Bind(wxEVT_TEXT_PASTE, &PositionCtrl::OnClipboardText, this);
Add(z_field, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5);
}
Expand Down

0 comments on commit b17411c

Please sign in to comment.