/
WDoubleSpinBox.C
159 lines (129 loc) · 3.24 KB
/
WDoubleSpinBox.C
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
/*
* Copyright (C) 2010 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <Wt/WDoubleSpinBox>
#include <Wt/WDoubleValidator>
#include "DomElement.h"
#include "WebUtils.h"
namespace Wt {
WDoubleSpinBox::WDoubleSpinBox(WContainerWidget *parent)
: WAbstractSpinBox(parent),
value_(-1),
min_(0.0),
max_(99.99),
step_(1.0),
precision_(2)
{
setValue(0.0);
}
void WDoubleSpinBox::setValue(double value)
{
if (value_ != value) {
value_ = value;
setText(textFromValue());
}
}
void WDoubleSpinBox::setMinimum(double minimum)
{
min_ = minimum;
changed_ = true;
repaint(RepaintInnerHtml);
}
void WDoubleSpinBox::setMaximum(double maximum)
{
max_ = maximum;
changed_ = true;
repaint(RepaintInnerHtml);
}
void WDoubleSpinBox::setRange(double minimum, double maximum)
{
min_ = minimum;
max_ = maximum;
changed_ = true;
repaint(RepaintInnerHtml);
}
void WDoubleSpinBox::setSingleStep(double step)
{
step_ = step;
changed_ = true;
repaint(RepaintInnerHtml);
}
int WDoubleSpinBox::decimals() const
{
return precision_;
}
void WDoubleSpinBox::setDecimals(int decimals)
{
precision_ = decimals;
setText(textFromValue());
}
std::string WDoubleSpinBox::jsMinMaxStep() const
{
return boost::lexical_cast<std::string>(min_) + ","
+ boost::lexical_cast<std::string>(max_) + ","
+ boost::lexical_cast<std::string>(step_);
}
void WDoubleSpinBox::updateDom(DomElement& element, bool all)
{
if (all || changed_) {
if (nativeControl()) {
element.setAttribute("min", boost::lexical_cast<std::string>(min_));
element.setAttribute("max", boost::lexical_cast<std::string>(max_));
element.setAttribute("step", boost::lexical_cast<std::string>(step_));
} else {
/* Make sure the JavaScript validator is loaded */
WDoubleValidator v;
v.javaScriptValidate();
}
}
WAbstractSpinBox::updateDom(element, all);
}
void WDoubleSpinBox::signalConnectionsChanged()
{
if (valueChanged_.isConnected() && !valueChangedConnection_) {
valueChangedConnection_ = true;
changed().connect(this, &WDoubleSpinBox::onChange);
}
}
void WDoubleSpinBox::onChange()
{
valueChanged_.emit(value());
}
WValidator *WDoubleSpinBox::createValidator()
{
WDoubleValidator *validator = new WDoubleValidator();
validator->setRange(min_, max_);
return validator;
}
WString WDoubleSpinBox::textFromValue() const
{
#ifndef WT_TARGET_JAVA
// can't use round_str, because (1) precision is only a hint, and
// (2) precision is limited to values < 7
std::stringstream ss;
ss.precision(precision_);
ss << std::fixed << std::showpoint << value_;
std::string result = ss.str();
#else
char buf[30];
std::string result = Utils::round_str(value_, precision_, buf);
#endif // WT_TARGET_JAVA
if (!nativeControl())
result = prefix().toUTF8() + result + suffix().toUTF8();
return WString::fromUTF8(result);
}
bool WDoubleSpinBox::parseNumberValue(const std::string& text)
{
try {
char buf[30];
std::string currentV = Utils::round_str(value_, precision_, buf);
if (currentV != text) // to prevent loss of precision
value_ = boost::lexical_cast<double>(text);
return true;
} catch (boost::bad_lexical_cast &e) {
return false;
}
}
}