-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWAbstractSpinBox.C
More file actions
226 lines (178 loc) · 5.01 KB
/
WAbstractSpinBox.C
File metadata and controls
226 lines (178 loc) · 5.01 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
/*
* Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Wt/WAbstractSpinBox"
#include "Wt/WApplication"
#include "Wt/WEnvironment"
#include "Wt/WValidator"
#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(WT_USTRING& input) const {
return spinBox_->parseValue(input) ? Result(Valid) : Result(Invalid);
}
virtual std::string javaScriptValidate() const {
return "jQuery.data(" + spinBox_->jsRef() + ", 'obj');";
}
private:
WAbstractSpinBox *spinBox_;
};
WAbstractSpinBox::WAbstractSpinBox(WContainerWidget *parent)
: WLineEdit(parent),
changed_(false),
valueChangedConnection_(false),
preferNative_(false),
setup_(false)
{
setJavaScriptMember("_a", "0");
}
void WAbstractSpinBox::setNativeControl(bool nativeControl)
{
preferNative_ = nativeControl;
}
bool WAbstractSpinBox::nativeControl() const
{
if (preferNative_) {
const WEnvironment& env = WApplication::instance()->environment();
if ((env.agentIsChrome() && env.agent() >= WEnvironment::Chrome5)
|| (env.agentIsSafari() && env.agent() >= WEnvironment::Safari4)
|| (env.agentIsOpera() && env.agent() >= WEnvironment::Opera10))
return true;
}
return false;
}
void WAbstractSpinBox::setPrefix(const WString& prefix)
{
prefix_ = prefix;
}
void WAbstractSpinBox::setSuffix(const WString& suffix)
{
suffix_ = suffix;
}
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 & RenderFull) {
setup_ = true;
bool useNative = nativeControl();
setup(useNative);
}
WLineEdit::render(flags);
}
void WAbstractSpinBox::connectJavaScript(Wt::EventSignalBase& s,
const std::string& methodName)
{
std::string jsFunction =
"function(obj, event) {"
"""var o = jQuery.data(" + jsRef() + ", 'obj');"
"""if (o) o." + methodName + "(obj, event);"
"}";
s.connect(jsFunction);
}
void WAbstractSpinBox::defineJavaScript()
{
WApplication *app = WApplication::instance();
LOAD_JAVASCRIPT(app, "js/WSpinBox.js", "WSpinBox", wtjs1);
std::string jsObj = "new " WT_CLASS ".WSpinBox("
+ app->javaScriptClass() + "," + jsRef() + ","
+ boost::lexical_cast<std::string>(decimals()) + ","
+ prefix().jsStringLiteral() + ","
+ suffix().jsStringLiteral() + ","
+ jsMinMaxStep() + ");";
setJavaScriptMember("_a", "0;" + jsObj);
}
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())
WApplication::instance()->doJavaScript
("jQuery.data(" + jsRef() + ", 'obj')"
".update(" + jsMinMaxStep() + ","
+ boost::lexical_cast<std::string>(decimals()) + ");");
else
setValidator(createValidator());
}
}
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(bool useNative)
{
if (useNative) {
setValidator(createValidator());
} else {
defineJavaScript();
addStyleClass("Wt-spinbox");
#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");
setValidator(new SpinBoxValidator(this));
}
}
int WAbstractSpinBox::boxPadding(Orientation orientation) const
{
if (!nativeControl() && 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 = parseNumberValue(textUtf8);
return valid;
}
}