-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.cpp
281 lines (233 loc) · 7.4 KB
/
calc.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
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
#include <sstream>
#include "fxkeys.h"
#include "fxhelper.h"
#include "calc.h"
#include "symbols.h"
#undef USE_CEVAL
#ifdef USE_CEVAL
#define CEVAL_STOICAL
#include "ceval.h"
#else
#include "cparse/shunting-yard.h"
#include "cparse/shunting-yard-exceptions.h"
#endif
// *********************************************************************************************************
// *** FXCalculator implementation
FXDEFMAP(FXCalculator) MessageMap[]=
{
//________Message_Type_____________________ID_______________Message_Handler_______
FXMAPFUNC(SEL_COMMAND, FXCalculator::ID_TOGGLESHOWN, FXCalculator::onToggleShown),
FXMAPFUNC(SEL_UPDATE, FXCalculator::ID_TOGGLESHOWN, FXCalculator::onUpdateShown),
FXMAPFUNC(SEL_FOCUSIN, 0, FXCalculator::onFocusIn),
FXMAPFUNC(SEL_FOCUS_NEXT, 0, FXCalculator::onFocusNext),
FXMAPFUNC(SEL_FOCUS_DOWN, 0, FXCalculator::onFocusNext),
FXMAPFUNC(SEL_FOCUS_RIGHT, 0, FXCalculator::onFocusNext),
FXMAPFUNC(SEL_FOCUS_PREV, 0, FXCalculator::onFocusPrev),
FXMAPFUNC(SEL_FOCUS_UP, 0, FXCalculator::onFocusPrev),
FXMAPFUNC(SEL_FOCUS_LEFT, 0, FXCalculator::onFocusPrev),
FXMAPFUNC(SEL_COMMAND, FXCalculator::ID_UPDATE, FXCalculator::onMapChange),
FXMAPFUNC(SEL_CHANGED, FXCalculator::ID_FORMULA, FXCalculator::onChanged)
};
FXIMPLEMENT(FXCalculator,FXHorizontalFrame,MessageMap, ARRAYNUMBER(MessageMap))
FXCalculator::FXCalculator(FXComposite* p, FXObject* tgt,FXSelector sel, FXuint opts, FXint x,FXint y,FXint w,FXint h)
: FXHorizontalFrame(p, opts, x,y,w,h, 3,3,2,2, 0,0), map(NULL)
{
// set target etc.
setTarget(tgt);
setSelector(sel);
// set layout
setFrameStyle(FRAME_LINE);
setBorderColor(getApp()->getShadowColor());
// close button icon
if (p && p->getParent()) {
if (p->getParent()->getParent())
{
closeIcon = new FXGIFIcon(getApp(), data::small_x, FXRGB(255, 255, 255), IMAGE_ALPHACOLOR);
new FXButton(this,
L"\t\tTaschenrechnerleiste schliessen",
closeIcon, this, FXCalculator::ID_CLOSE, BUTTON_TOOLBAR);
}
}
new FXLabel(this, "&Rechner ", 0, LAYOUT_CENTER_Y);
FXVerticalFrame *frame = new FXVerticalFrame(this, LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0);
firstline = new FXHorizontalFrame(frame, LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0);
formula = new FXTextField(firstline, 10, this,ID_FORMULA, LAYOUT_FILL_X|LAYOUT_FILL_Y);
formula->setHelpText("Taschenrechner");
result = new FXTextField(firstline, 13, NULL,0, TEXTFIELD_READONLY|JUSTIFY_RIGHT|LAYOUT_FILL_Y);
result->setBackColor(getBackColor());
// second line
secondline = new FXHorizontalFrame(frame, LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0);
secondline->hide();
longresult = new FXTextField(secondline, 13, NULL,0, TEXTFIELD_READONLY|LAYOUT_FILL_X|LAYOUT_FILL_Y);
longresult->setBackColor(getBackColor());
}
void FXCalculator::create()
{
FXHorizontalFrame::create();
}
FXCalculator::~FXCalculator()
{
delete closeIcon;
}
void FXCalculator::setMapFile(std::shared_ptr<datafile>& f) {
mapFile = f;
}
void FXCalculator::connectMap(FXCSMap* map_)
{
map = map_;
}
long FXCalculator::onToggleShown(FXObject*, FXSelector, void*)
{
if (shown())
hide();
else
show();
recalc();
return 1;
}
long FXCalculator::onUpdateShown(FXObject* sender, FXSelector, void*)
{
sender->handle(this, FXSEL(SEL_COMMAND, shown()?ID_CHECK:ID_UNCHECK), NULL);
return 1;
}
long FXCalculator::onCloseButton(FXObject*, FXSelector, void*)
{
if (shown())
{
hide();
recalc();
}
return 1;
}
long FXCalculator::onFocusIn(FXObject* sender, FXSelector sel, void* ptr)
{
long res = FXHorizontalFrame::onFocusIn(sender, sel, ptr);
if (formula->hasFocus())
formula->selectAll();
else if (!getFocus())
formula->setFocus();
return res;
}
long FXCalculator::onFocusNext(FXObject* sender, FXSelector sel, void* ptr)
{
if (!getFocus())
return formula->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr);
else
return 0;
}
long FXCalculator::onFocusPrev(FXObject* sender, FXSelector sel, void* ptr)
{
if (!getFocus())
return formula->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr);
else
return 0;
}
long FXCalculator::onMapChange(FXObject* /*sender*/, FXSelector, void* ptr)
{
datafile::SelectionState *pstate = (datafile::SelectionState*)ptr;
if (selection.fileChange != pstate->fileChange || selection.selChange != pstate->selChange)
{
selection = *pstate;
onChanged(this, 0, NULL);
}
return 1;
}
static FXString format_double(double v) {
FXString x = FXStringFormat("%lf", v);
FXint pos = x.length();
while (--pos > 0) {
if (x[pos] != '0') break;
}
if (x[pos] == '.') --pos;
x.trunc(pos + 1);
return x;
}
#ifdef USE_CEVAL
static FXString evaluate(const char* expr)
{
if (expr && expr[0]) {
double f = ceval_result(expr);
if (!isnan(f)) {
return format_double(f);
}
}
return FXString::null;
}
#else
static FXString evaluate(const char* expr)
{
if (expr && expr[0]) {
cparse::TokenMap vars;
cparse::calculator cl;
try {
cl.compile(expr);
cparse::packToken pt = cl.eval(vars);
if (pt->type == cparse::tokType::INT) {
FXlong value = pt.asInt();
return FXStringVal(value, 10);
}
else if (pt->type == cparse::tokType::REAL) {
double value = pt.asDouble();
return format_double(value);
}
std::string result = pt.str();
return FXString(result.c_str());
}
catch (...) {
return FXString("Fehler");
}
}
return FXString::null;
}
#endif
long FXCalculator::onChanged(FXObject*, FXSelector, void*)
{
FXString text = formula->getText();
FXString exp = text.trimBegin();
if (exp != text) {
formula->setText(exp);
}
// if ROUTE or NACH, send to map
if (map)
{
FXString text;
if (exp.before(' ').upper() == "NACH" || exp.before(' ').upper() == "ROUTE")
{
int length = map->sendRouteCmds(exp, 1);
text = "Regionen: " + FXStringVal(length);
}
else
map->clearRoute(1);
result->setText(text);
longresult->setText("");
if (text.length())
{
if (secondline->shown())
{
secondline->hide();
recalc();
}
return 1;
}
}
// HACK: https://bugs.eressea.de/view.php?id=2962
// https://github.com/cparse/cparse/issues/104
FXint pos = 0;
while ((pos = exp.find(':', pos)) >= 0) {
exp.erase(pos);
}
FXString resultstr = evaluate(exp.text());
if (resultstr.length() > 15)
{
result->setText("");
longresult->setText(resultstr);
secondline->show();
}
else
{
result->setText(resultstr);
secondline->hide();
}
recalc();
return 1;
}