1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#include "main.h"
19#include "numbertextctrl.h"
20
21BEGIN_EVENT_TABLE(NumberTextCtrl, wxTextCtrl)
22EVT_KILL_FOCUS(NumberTextCtrl::OnKillFocus)
23EVT_TEXT_ENTER(wxID_ANY, NumberTextCtrl::OnTextEnter)
24END_EVENT_TABLE()
25
26NumberTextCtrl::NumberTextCtrl(wxWindow* parent, wxWindowID id, long value, long minvalue, long maxvalue, const wxPoint& pos, const wxSize& sz, long style, const wxString& name) :
27 wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name),
28 minval(minvalue), maxval(maxvalue), lastval(value) {
29 ////
30}
31
32NumberTextCtrl::NumberTextCtrl(wxWindow* parent, wxWindowID id, long value, long minvalue, long maxvalue, long style, const wxString& name, const wxPoint& pos, const wxSize& sz) :
33 wxTextCtrl(parent, id, (wxString() << value), pos, sz, style, wxTextValidator(wxFILTER_NUMERIC), name),
34 minval(minvalue), maxval(maxvalue), lastval(value) {
35 ////
36}
37
38NumberTextCtrl::~NumberTextCtrl() {
39 ////
40}
41
42void NumberTextCtrl::OnKillFocus(wxFocusEvent& evt) {
43 CheckRange();
44 evt.Skip();
45}
46
47void NumberTextCtrl::OnTextEnter(wxCommandEvent& evt) {
48 CheckRange();
49}
50
51void NumberTextCtrl::SetIntValue(long value) {
52 wxString sv;
53 sv << value;
54 // Will generate events
55 SetValue(sv);
56}
57
58long NumberTextCtrl::GetIntValue() {
59 long l;
60 if (GetValue().ToLong(&l)) {
61 return l;
62 }
63 return 0;
64}
65
66void NumberTextCtrl::SetMinValue(long value) {
67 if (value == minval) {
68 return;
69 }
70 minval = value;
71 CheckRange();
72}
73
74void NumberTextCtrl::SetMaxValue(long value) {
75 if (value == maxval) {
76 return;
77 }
78 maxval = value;
79 CheckRange();
80}
81
82void NumberTextCtrl::CheckRange() {
83 wxString text = GetValue();
84 wxString ntext;
85
86 for (size_t s = 0; s < text.size(); ++s) {
87 if (text[s] >= '0' && text[s] <= '9') {
88 ntext.Append(text[s]);
89 }
90 }
91
92 // Check that value is in range
93 long v;
94 if (ntext.size() != 0 && ntext.ToLong(&v)) {
95 if (v < minval) {
96 v = minval;
97 } else if (v > maxval) {
98 v = maxval;
99 }
100
101 ntext.clear();
102 ntext << v;
103 lastval = v;
104 } else {
105 ntext.clear();
106 ntext << lastval;
107 }
108
109 // Check if there was any change
110 if (ntext != text) {
111 // ChangeValue doesn't generate events
112 ChangeValue(ntext);
113 }
114}
115