app.cpp
2.57 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
#include "app.h"
#include "../apps_container.h"
#include "calculation_icon.h"
#include "../i18n.h"
using namespace Poincare;
using namespace Shared;
namespace Calculation {
I18n::Message App::Descriptor::name() {
return I18n::Message::CalculApp;
}
I18n::Message App::Descriptor::upperName() {
return I18n::Message::CalculAppCapital;
}
const Image * App::Descriptor::icon() {
return ImageStore::CalculationIcon;
}
App * App::Snapshot::unpack(Container * container) {
return new App(container, this);
}
void App::Snapshot::reset() {
m_calculationStore.deleteAll();
}
App::Descriptor * App::Snapshot::descriptor() {
static Descriptor descriptor;
return &descriptor;
}
CalculationStore * App::Snapshot::calculationStore() {
return &m_calculationStore;
}
void App::Snapshot::tidy() {
m_calculationStore.tidy();
}
App::App(Container * container, Snapshot * snapshot) :
TextFieldDelegateApp(container, snapshot, &m_editExpressionController),
m_historyController(&m_editExpressionController, snapshot->calculationStore()),
m_editExpressionController(&m_modalViewController, &m_historyController, snapshot->calculationStore())
{
}
bool App::textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event event) {
if ((event == Ion::Events::Var || event == Ion::Events::XNT) && TextFieldDelegateApp::textFieldDidReceiveEvent(textField, event)) {
return true;
}
if (textField->isEditing() && textField->textFieldShouldFinishEditing(event)) {
if (textField->text()[0] == 0) {
return true;
}
if (!textInputIsCorrect(textField->text())) {
displayWarning(I18n::Message::SyntaxError);
return true;
}
}
return false;
}
bool App::textInputIsCorrect(const char * text) {
/* Here, we check that the expression entered by the user can be printed with
* less than k_printedExpressionLength characters. Otherwise, we prevent the
* user from adding this expression to the calculation store. */
Expression * exp = Expression::parse(text);
if (exp == nullptr) {
return false;
}
Expression::ReplaceSymbolWithExpression(&exp, Symbol::SpecialSymbols::Ans, static_cast<Snapshot *>(snapshot())->calculationStore()->ansExpression(localContext()));
char buffer[Calculation::k_printedExpressionSize];
int length = exp->writeTextInBuffer(buffer, sizeof(buffer));
delete exp;
/* if the buffer is totally full, it is VERY likely that writeTextInBuffer
* escaped before printing utterly the expression. */
if (length >= Calculation::k_printedExpressionSize-1) {
return false;
}
return true;
}
const char * App::XNT() {
return "x";
}
}