-
Notifications
You must be signed in to change notification settings - Fork 224
/
numbertextctrl.cpp
126 lines (109 loc) · 2.98 KB
/
numbertextctrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor is free software: you can redistribute it and/or modify
// 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.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "numbertextctrl.h"
BEGIN_EVENT_TABLE(NumberTextCtrl, wxTextCtrl)
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, wxTextValidator(wxFILTER_NONE), name),
minval(minvalue), maxval(maxvalue), lastval(value)
{
////
}
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_NONE), name),
minval(minvalue), maxval(maxvalue), lastval(value)
{
////
}
NumberTextCtrl::~NumberTextCtrl()
{
////
}
void NumberTextCtrl::OnKillFocus(wxFocusEvent& evt)
{
CheckRange();
evt.Skip();
}
void NumberTextCtrl::OnTextEnter(wxCommandEvent& evt)
{
CheckRange();
}
void NumberTextCtrl::SetIntValue(long value)
{
wxString sv;
sv << value;
// Will generate events
SetValue(sv);
}
long NumberTextCtrl::GetIntValue()
{
long l;
if(GetValue().ToLong(&l))
return l;
return 0;
}
void NumberTextCtrl::SetMinValue(long value)
{
if(value == minval)
return;
minval = value;
CheckRange();
}
void NumberTextCtrl::SetMaxValue(long value)
{
if(value == maxval)
return;
maxval = value;
CheckRange();
}
void NumberTextCtrl::CheckRange()
{
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]);
}
// 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;
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);
}
}