Numworks Epsilon  1.4.1
Graphing Calculator Operating System
app.cpp
Go to the documentation of this file.
1 #include "app.h"
2 #include "../apps_container.h"
3 #include "calculation_icon.h"
4 #include "../i18n.h"
5 
6 using namespace Poincare;
7 
8 using namespace Shared;
9 
10 namespace Calculation {
11 
12 I18n::Message App::Descriptor::name() {
13  return I18n::Message::CalculApp;
14 }
15 
16 I18n::Message App::Descriptor::upperName() {
17  return I18n::Message::CalculAppCapital;
18 }
19 
21  return ImageStore::CalculationIcon;
22 }
23 
25  return new App(container, this);
26 }
27 
29  m_calculationStore.deleteAll();
30 }
31 
33  static Descriptor descriptor;
34  return &descriptor;
35 }
36 
37 CalculationStore * App::Snapshot::calculationStore() {
38  return &m_calculationStore;
39 }
40 
41 void App::Snapshot::tidy() {
42  m_calculationStore.tidy();
43 }
44 
45 App::App(Container * container, Snapshot * snapshot) :
46  TextFieldDelegateApp(container, snapshot, &m_editExpressionController),
47  m_historyController(&m_editExpressionController, snapshot->calculationStore()),
48  m_editExpressionController(&m_modalViewController, &m_historyController, snapshot->calculationStore())
49 {
50 }
51 
53  if ((event == Ion::Events::Var || event == Ion::Events::XNT) && TextFieldDelegateApp::textFieldDidReceiveEvent(textField, event)) {
54  return true;
55  }
56  if (textField->isEditing() && textField->textFieldShouldFinishEditing(event)) {
57  if (textField->text()[0] == 0) {
58  return true;
59  }
60  if (!textInputIsCorrect(textField->text())) {
61  displayWarning(I18n::Message::SyntaxError);
62  return true;
63  }
64  }
65  return false;
66 }
67 
68 bool App::textInputIsCorrect(const char * text) {
69  /* Here, we check that the expression entered by the user can be printed with
70  * less than k_printedExpressionLength characters. Otherwise, we prevent the
71  * user from adding this expression to the calculation store. */
72  Expression * exp = Expression::parse(text);
73  if (exp == nullptr) {
74  return false;
75  }
76  Expression::ReplaceSymbolWithExpression(&exp, Symbol::SpecialSymbols::Ans, static_cast<Snapshot *>(snapshot())->calculationStore()->ansExpression(localContext()));
78  int length = exp->writeTextInBuffer(buffer, sizeof(buffer));
79  delete exp;
80  /* if the buffer is totally full, it is VERY likely that writeTextInBuffer
81  * escaped before printing utterly the expression. */
82  if (length >= Calculation::k_printedExpressionSize-1) {
83  return false;
84  }
85  return true;
86 }
87 
88 const char * App::XNT() {
89  return "x";
90 }
91 
92 }
#define exp(x)
Definition: math.h:176
bool isEditing() const
Definition: text_field.cpp:175
constexpr Event Var
Definition: events.h:74
Snapshot * snapshot()
Definition: app.cpp:41
bool textFieldDidReceiveEvent(::TextField *textField, Ion::Events::Event event) override
Definition: app.cpp:52
Definition: image.h:6
bool textFieldShouldFinishEditing(Ion::Events::Event event)
Definition: text_field.h:27
App(Container *container, Snapshot *snapshot, ViewController *rootViewController, I18n::Message warningMessage=(I18n::Message) 0)
Definition: app.cpp:30
virtual Poincare::Context * localContext()
virtual void reset()
Definition: app.cpp:24
void displayWarning(I18n::Message warningMessage)
Definition: app.cpp:97
constexpr Event Ans
Definition: events.h:113
const char * XNT() override
Definition: app.cpp:88
virtual App * unpack(Container *container)=0
constexpr Event XNT
Definition: events.h:73
const char * text() const
Definition: text_input.h:14
virtual const Image * icon()
Definition: app.cpp:15
virtual I18n::Message upperName()
Definition: app.cpp:11
virtual Descriptor * descriptor()=0
bool textInputIsCorrect(const char *text)
Definition: app.cpp:68
virtual I18n::Message name()
Definition: app.cpp:7
static constexpr int k_printedExpressionSize
Definition: calculation.h:35