-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWAbstractSpinBox.C
More file actions
293 lines (239 loc) · 6.98 KB
/
WAbstractSpinBox.C
File metadata and controls
293 lines (239 loc) · 6.98 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (C) 2011 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Wt/WAbstractSpinBox.h"
#include "Wt/WApplication.h"
#include "Wt/WEnvironment.h"
#include "Wt/WValidator.h"
#include "DomElement.h"
#include <boost/algorithm/string.hpp>
#ifndef WT_DEBUG_JS
#include "js/WSpinBox.min.js"
#endif
namespace Wt {
class SpinBoxValidator : public WValidator
{
public:
SpinBoxValidator(WAbstractSpinBox *spinBox)
: spinBox_(spinBox)
{ }
virtual Result validate(const WT_USTRING& input) const override {
bool valid = spinBox_->parseValue(input);
if (valid)
return spinBox_->validateRange();
else
return Result(ValidationState::Invalid);
}
virtual std::string javaScriptValidate() const override {
return
"new function() { "
"""this.validate = function(t) {"
"" "return " + spinBox_->jsRef() + ".wtObj.validate(t);"
"""};"
"}";
}
private:
WAbstractSpinBox *spinBox_;
};
WAbstractSpinBox::WAbstractSpinBox()
: changed_(false),
valueChangedConnection_(false),
preferNative_(false),
setup_(false),
jsValueChanged_(this, "spinboxValueChanged", true)
{ }
void WAbstractSpinBox::setNativeControl(bool nativeControl)
{
preferNative_ = nativeControl;
}
bool WAbstractSpinBox::nativeControl() const
{
if (preferNative_) {
if (!WLineEdit::inputMask().empty()) {
return false;
}
const WEnvironment& env = WApplication::instance()->environment();
if ((env.agentIsChrome() &&
static_cast<unsigned int>(env.agent()) >=
static_cast<unsigned int>(UserAgent::Chrome5))
|| (env.agentIsSafari() &&
static_cast<unsigned int>(env.agent()) >=
static_cast<unsigned int>(UserAgent::Safari4))
|| (env.agentIsOpera() &&
static_cast<unsigned int>(env.agent()) >=
static_cast<unsigned int>(UserAgent::Opera10)))
return true;
}
return false;
}
void WAbstractSpinBox::setPrefix(const WString& prefix)
{
if (prefix_ != prefix) {
prefix_ = prefix;
setText(textFromValue());
changed_ = true;
repaint();
}
}
void WAbstractSpinBox::setSuffix(const WString& suffix)
{
if (suffix_ != suffix) {
suffix_ = suffix;
setText(textFromValue());
changed_ = true;
repaint();
}
}
void WAbstractSpinBox::render(WFlags<RenderFlag> flags)
{
/*
* In theory we are a bit late here to decide what we want to become:
* somebody could already have asked the domElementType()
*/
if (!setup_ && flags.test(RenderFlag::Full)) {
setup();
}
if (jsValueChanged().needsUpdate(true)) {
WStringStream function;
function << jsRef() << ".wtObj.jsValueChanged=";
if (jsValueChanged().isConnected()) {
function << "function(oldv, v){"
#ifndef WT_TARGET_JAVA
<< "var o=null;var e=null;" << jsValueChanged().createCall({"oldv", "v"}) << "};";
#else // WT_TARGET_JAVA
<< "var o=null;var e=null;" << jsValueChanged().createCall("oldv", "v") << "};";
#endif // WT_TARGET_JAVA
} else {
function << "function() {};";
}
doJavaScript(function.str());
}
WLineEdit::render(flags);
}
void WAbstractSpinBox::connectJavaScript(Wt::EventSignalBase& s,
const std::string& methodName)
{
std::string jsFunction =
"function(obj, event) {"
"""var o = " + jsRef() + ";"
"""if (o && o.wtObj) o.wtObj." + methodName + "(obj, event);"
"}";
s.connect(jsFunction);
}
void WAbstractSpinBox::defineJavaScript()
{
WApplication *app = WApplication::instance();
LOAD_JAVASCRIPT(app, "js/WSpinBox.js", "WSpinBox", wtjs1);
WStringStream ss;
ss << "new " WT_CLASS ".WSpinBox("
<< app->javaScriptClass() << "," << jsRef() << "," << decimals() << ","
<< prefix().jsStringLiteral() << ","
<< suffix().jsStringLiteral() << ","
<< jsMinMaxStep() << ","
<< jsStringLiteral(WLocale::currentLocale().decimalPoint()) << ","
<< jsStringLiteral(WLocale::currentLocale().groupSeparator())
<< ");";
setJavaScriptMember(" WSpinBox", ss.str());
}
void WAbstractSpinBox::setText(const WT_USTRING& text)
{
parseValue(text);
WLineEdit::setText(textFromValue());
}
void WAbstractSpinBox::setFormData(const FormData& formData)
{
WLineEdit::setFormData(formData);
parseValue(text());
}
void WAbstractSpinBox::updateDom(DomElement& element, bool all)
{
if (all || changed_) {
if (!all) {
if (!nativeControl())
doJavaScript(jsRef() + ".wtObj"
".configure("
+ std::to_string(decimals()) + ","
+ prefix().jsStringLiteral() + ","
+ suffix().jsStringLiteral() + ","
+ jsMinMaxStep() + ");");
else
setValidator(std::shared_ptr<WValidator>(createValidator().release()));
}
}
changed_ = false;
WLineEdit::updateDom(element, all);
if (all && nativeControl())
element.setAttribute("type", "number");
}
void WAbstractSpinBox::propagateRenderOk(bool deep)
{
changed_ = false;
WLineEdit::propagateRenderOk(deep);
}
void WAbstractSpinBox::setup()
{
setup_ = true;
bool useNative = nativeControl();
if (!useNative) {
defineJavaScript();
#ifdef WT_CNOR
EventSignalBase& b = mouseMoved();
EventSignalBase& c = keyWentDown();
#endif
connectJavaScript(mouseMoved(), "mouseMove");
connectJavaScript(mouseWentUp(), "mouseUp");
connectJavaScript(mouseWentDown(), "mouseDown");
connectJavaScript(mouseWentOut(), "mouseOut");
connectJavaScript(keyWentDown(), "keyDown");
connectJavaScript(keyWentUp(), "keyUp");
if (!prefix_.empty() || !suffix_.empty())
setValidator(std::shared_ptr<SpinBoxValidator>(new SpinBoxValidator(this)));
}
}
ValidationState WAbstractSpinBox::validate()
{
return WLineEdit::validate();
}
void WAbstractSpinBox::refresh()
{
if (isRendered()) {
doJavaScript
(jsRef() + ".wtObj"
".setLocale("
+ jsStringLiteral(WLocale::currentLocale().decimalPoint()) + ","
+ jsStringLiteral(WLocale::currentLocale().groupSeparator()) + ");");
}
WLineEdit::refresh();
}
int WAbstractSpinBox::boxPadding(Orientation orientation) const
{
if (!nativeControl() && orientation == Orientation::Horizontal)
return WLineEdit::boxPadding(orientation) + 8; // Half since for one side
else
return WLineEdit::boxPadding(orientation);
}
bool WAbstractSpinBox::parseValue(const WT_USTRING& text)
{
std::string textUtf8 = text.toUTF8();
bool valid = true;
if (!nativeControl()) {
valid = false;
std::string prefixUtf8 = prefix_.toUTF8();
std::string suffixUtf8 = suffix_.toUTF8();
if (boost::starts_with(textUtf8, prefixUtf8)) {
textUtf8 = textUtf8.substr(prefixUtf8.length());
if (boost::ends_with(textUtf8, suffixUtf8)) {
textUtf8 = textUtf8.substr(0, textUtf8.length() - suffixUtf8.length());
valid = true;
}
}
}
if (valid)
valid = textUtf8.length() > 0;
if (valid)
valid = parseNumberValue(textUtf8);
return valid;
}
}