forked from ennorehling/csmapfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.cpp
More file actions
272 lines (218 loc) · 7.21 KB
/
Copy pathcalc.cpp
File metadata and controls
272 lines (218 loc) · 7.21 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
#include <sstream>
#include "fxkeys.h"
#include "main.h"
#include "fxhelper.h"
#include "calc.h"
#include "symbols.h"
#include "tinyjs/TinyJS.h"
#include "tinyjs/TinyJS_MathFunctions.h"
#include "tinyjs/TinyJS_Functions.h"
// *********************************************************************************************************
// *** 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()
{
}
void FXCalculator::mapfiles(std::list<datafile> *f)
{
files = 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 *state = (datafile::SelectionState*)ptr;
// connected to a datafile list?
if (!files)
return 0;
bool needUpdate = false;
if (selection.fileChange != state->fileChange)
{
selection.fileChange = state->fileChange;
selection.map = state->map;
selection.activefaction = state->activefaction;
needUpdate = true;
}
if (selection.selChange != state->selChange)
{
selection.selChange = state->selChange;
selection.selected = state->selected;
selection.region = state->region;
selection.faction = state->faction;
selection.building = state->building;
selection.ship = state->ship;
selection.unit = state->unit;
needUpdate = true;
}
if (needUpdate)
{
onChanged(this, 0, NULL);
}
return 1;
}
long FXCalculator::onChanged(FXObject*, FXSelector, void*)
{
FXString exp = formula->getText();
bool changed = false;
for (int pos = exp.find_first_of("\t\r\n"); pos != -1; pos = exp.find_first_of("\t\r\n"))
{
changed = true;
exp.replace(pos, 1, " ");
}
if (changed)
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->sendRouteCmds("", 1);
result->setText(text);
longresult->setText("");
if (text.length())
{
if (secondline->shown())
{
secondline->hide();
recalc();
}
return 1;
}
}
std::string resultstr;
CTinyJS s;
registerFunctions(&s);
registerMathFunctions(&s);
try {
std::string code(exp.text());
resultstr = s.evaluate(code);
}
catch (CScriptException *e) {
resultstr.assign(e->text.c_str());
delete e;
}
/*
Ruby::Value val = Ruby::Evaluate( utf2iso(exp).text() );
resultstr = val.to_s();
for (std::string::iterator it = resultstr.begin(); it != resultstr.end(); it++)
if (*it == '\n' || *it == '\r' || *it == '\t')
*it = ' ';
*/
if (resultstr.size() > 15)
{
result->setText("");
longresult->setText( FXString(resultstr.c_str()) );
secondline->show();
}
else
{
result->setText( FXString(resultstr.c_str()) );
secondline->hide();
}
recalc();
return 1;
}