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)
|
2023-10-09 19:12:16 -07:00
|
|
|
EVT_KILL_FOCUS(NumberTextCtrl::OnKillFocus)
|
|
|
|
|
EVT_TEXT_ENTER(wxID_ANY, NumberTextCtrl::OnTextEnter)
|
2012-06-29 16:44:26 +02:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
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),
|
2023-10-09 19:12:16 -07:00
|
|
|
minval(minvalue), maxval(maxvalue), lastval(value) {
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07: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),
|
2023-10-09 19:12:16 -07:00
|
|
|
minval(minvalue), maxval(maxvalue), lastval(value) {
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
NumberTextCtrl::~NumberTextCtrl() {
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
void NumberTextCtrl::OnKillFocus(wxFocusEvent &evt) {
|
2015-07-23 10:44:34 -03:00
|
|
|
CheckRange();
|
|
|
|
|
evt.Skip();
|
2016-09-06 18:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
void NumberTextCtrl::OnTextEnter(wxCommandEvent &evt) {
|
2015-07-23 10:44:34 -03:00
|
|
|
CheckRange();
|
|
|
|
|
}
|
2012-06-29 16:44:26 +02:00
|
|
|
|
2023-10-09 19:12:16 -07: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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
long NumberTextCtrl::GetIntValue() {
|
2017-06-14 19:55:08 +02:00
|
|
|
long l;
|
2023-10-09 19:12:16 -07:00
|
|
|
if (GetValue().ToLong(&l)) {
|
2017-06-14 19:55:08 +02:00
|
|
|
return l;
|
2023-10-09 19:12:16 -07:00
|
|
|
}
|
2017-06-14 19:55:08 +02:00
|
|
|
return 0;
|
2015-07-23 10:44:34 -03:00
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
void NumberTextCtrl::SetMinValue(long value) {
|
|
|
|
|
if (value == minval) {
|
2019-06-09 21:00:46 -03:00
|
|
|
return;
|
2023-10-09 19:12:16 -07:00
|
|
|
}
|
2019-06-09 21:00:46 -03:00
|
|
|
minval = value;
|
|
|
|
|
CheckRange();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
void NumberTextCtrl::SetMaxValue(long value) {
|
|
|
|
|
if (value == maxval) {
|
2019-06-09 21:00:46 -03:00
|
|
|
return;
|
2023-10-09 19:12:16 -07:00
|
|
|
}
|
2019-06-09 21:00:46 -03:00
|
|
|
maxval = value;
|
|
|
|
|
CheckRange();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
void NumberTextCtrl::CheckRange() {
|
2017-06-14 19:55:08 +02:00
|
|
|
wxString text = GetValue();
|
|
|
|
|
wxString ntext;
|
|
|
|
|
|
2023-10-09 19:12:16 -07:00
|
|
|
for (size_t s = 0; s < text.size(); ++s) {
|
|
|
|
|
if (text[s] >= '0' && text[s] <= '9') {
|
2017-06-14 19:55:08 +02:00
|
|
|
ntext.Append(text[s]);
|
2023-10-09 19:12:16 -07:00
|
|
|
}
|
2017-06-14 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
// Check that value is in range
|
2017-06-14 19:55:08 +02:00
|
|
|
long v;
|
2023-10-09 19:12:16 -07:00
|
|
|
if (ntext.size() != 0 && ntext.ToLong(&v)) {
|
|
|
|
|
if (v < minval) {
|
2017-06-14 19:55:08 +02:00
|
|
|
v = minval;
|
2023-10-09 19:12:16 -07:00
|
|
|
} else if (v > maxval) {
|
2017-06-14 19:55:08 +02:00
|
|
|
v = maxval;
|
2023-10-09 19:12:16 -07:00
|
|
|
}
|
2017-06-14 19:55:08 +02:00
|
|
|
|
|
|
|
|
ntext.clear();
|
|
|
|
|
ntext << v;
|
|
|
|
|
lastval = v;
|
|
|
|
|
} else {
|
|
|
|
|
ntext.clear();
|
|
|
|
|
ntext << lastval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if there was any change
|
2023-10-09 19:12:16 -07:00
|
|
|
if (ntext != text) {
|
2017-06-14 19:55:08 +02:00
|
|
|
// ChangeValue doesn't generate events
|
|
|
|
|
ChangeValue(ntext);
|
|
|
|
|
}
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|