2012-06-29 16:44:26 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
// This file is part of Remere's Map Editor
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2020-07-30 11:42:28 -03:00
|
|
|
// Remere's Map Editor is free software: you can redistribute it and/or modify
|
2012-06-29 16:44:26 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
2020-07-30 11:42:28 -03:00
|
|
|
// Remere's Map Editor is distributed in the hope that it will be useful,
|
2012-06-29 16:44:26 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2020-07-30 11:42:28 -03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-29 16:44:26 +02:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-07-30 11:42:28 -03:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-06-29 16:44:26 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include "numbertextctrl.h"
|
|
|
|
|
|
2017-06-14 19:55:08 +02:00
|
|
|
BEGIN_EVENT_TABLE(NumberTextCtrl, wxTextCtrl)
|
2015-07-23 10:44:34 -03:00
|
|
|
EVT_KILL_FOCUS(NumberTextCtrl::OnKillFocus)
|
|
|
|
|
EVT_TEXT_ENTER(wxID_ANY, NumberTextCtrl::OnTextEnter)
|
2012-06-29 16:44:26 +02:00
|
|
|
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) :
|
2017-06-14 19:55:08 +02:00
|
|
|
wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name),
|
2015-01-21 08:31:57 -03:00
|
|
|
minval(minvalue), maxval(maxvalue), lastval(value)
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberTextCtrl::NumberTextCtrl(wxWindow* parent, wxWindowID id,
|
|
|
|
|
long value, long minvalue, long maxvalue,
|
|
|
|
|
long style, const wxString& name,
|
|
|
|
|
const wxPoint& pos, const wxSize& sz) :
|
2017-06-14 19:55:08 +02:00
|
|
|
wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name),
|
2015-01-21 08:31:57 -03:00
|
|
|
minval(minvalue), maxval(maxvalue), lastval(value)
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberTextCtrl::~NumberTextCtrl()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-23 10:44:34 -03:00
|
|
|
void NumberTextCtrl::OnKillFocus(wxFocusEvent& evt)
|
|
|
|
|
{
|
|
|
|
|
CheckRange();
|
|
|
|
|
evt.Skip();
|
2016-09-06 18:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-23 10:44:34 -03:00
|
|
|
void NumberTextCtrl::OnTextEnter(wxCommandEvent& evt)
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
2015-07-23 10:44:34 -03:00
|
|
|
CheckRange();
|
|
|
|
|
}
|
2012-06-29 16:44:26 +02:00
|
|
|
|
2019-06-09 21:00:46 -03:00
|
|
|
void NumberTextCtrl::SetIntValue(long value)
|
2015-07-23 10:44:34 -03:00
|
|
|
{
|
|
|
|
|
wxString sv;
|
2019-06-09 21:00:46 -03:00
|
|
|
sv << value;
|
2015-07-23 10:44:34 -03:00
|
|
|
// Will generate events
|
|
|
|
|
SetValue(sv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long NumberTextCtrl::GetIntValue()
|
|
|
|
|
{
|
2017-06-14 19:55:08 +02:00
|
|
|
long l;
|
|
|
|
|
if(GetValue().ToLong(&l))
|
|
|
|
|
return l;
|
|
|
|
|
return 0;
|
2015-07-23 10:44:34 -03:00
|
|
|
}
|
|
|
|
|
|
2019-06-09 21:00:46 -03:00
|
|
|
void NumberTextCtrl::SetMinValue(long value)
|
|
|
|
|
{
|
|
|
|
|
if(value == minval)
|
|
|
|
|
return;
|
|
|
|
|
minval = value;
|
|
|
|
|
CheckRange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NumberTextCtrl::SetMaxValue(long value)
|
|
|
|
|
{
|
|
|
|
|
if(value == maxval)
|
|
|
|
|
return;
|
|
|
|
|
maxval = value;
|
|
|
|
|
CheckRange();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-23 10:44:34 -03:00
|
|
|
void NumberTextCtrl::CheckRange()
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
2017-06-14 19:55:08 +02:00
|
|
|
wxString text = GetValue();
|
|
|
|
|
wxString ntext;
|
|
|
|
|
|
|
|
|
|
for(size_t s = 0; s < text.size(); ++s) {
|
|
|
|
|
if(text[s] >= '0' && text[s] <= '9')
|
|
|
|
|
ntext.Append(text[s]);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
// Check that value is in range
|
2017-06-14 19:55:08 +02:00
|
|
|
long v;
|
|
|
|
|
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 {
|
|
|
|
|
ntext.clear();
|
|
|
|
|
ntext << lastval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if there was any change
|
|
|
|
|
if(ntext != text) {
|
|
|
|
|
// ChangeValue doesn't generate events
|
|
|
|
|
ChangeValue(ntext);
|
|
|
|
|
}
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|