Numworks Epsilon  1.4.1
Graphing Calculator Operating System
text_field_delegate_app.cpp
Go to the documentation of this file.
2 #include "../apps_container.h"
3 #include <cmath>
4 #include <string.h>
5 
6 using namespace Poincare;
7 
8 namespace Shared {
9 
10 TextFieldDelegateApp::TextFieldDelegateApp(Container * container, Snapshot * snapshot, ViewController * rootViewController) :
11  ::App(container, snapshot, rootViewController, I18n::Message::Warning),
13 {
14 }
15 
17  return container()->globalContext();
18 }
19 
21  return (AppsContainer *)app()->container();
22 }
23 
24 const char * TextFieldDelegateApp::XNT() {
25  return "X";
26 }
27 
28 const char * TextFieldDelegateApp::privateXNT(TextField * textField) {
29  static constexpr struct { const char *name, *xnt; } sFunctions[] = {
30  { "diff", "x" }, { "int", "x" },
31  { "product", "n" }, { "sum", "n" }
32  };
33  // Let's assume everything before the cursor is nested correctly, which is reasonable if the expression is being entered left-to-right.
34  const char * text = textField->text();
35  size_t location = textField->cursorLocation();
36  unsigned level = 0;
37  while (location >= 1) {
38  location--;
39  switch (text[location]) {
40  case '(':
41  // Check if we are skipping to the next matching '('.
42  if (level) {
43  level--;
44  break;
45  }
46  // Skip over whitespace.
47  while (location >= 1 && text[location-1] == ' ') {
48  location--;
49  }
50  // We found the next innermost function we are currently in.
51  for (size_t i = 0; i < sizeof(sFunctions)/sizeof(sFunctions[0]); i++) {
52  const char * name = sFunctions[i].name;
53  size_t length = strlen(name);
54  if (location >= length && memcmp(&text[location-length], name, length) == 0) {
55  return sFunctions[i].xnt;
56  }
57  }
58  break;
59  case ',':
60  // Commas encountered while skipping to the next matching '(' should be ignored.
61  if (level) {
62  break;
63  }
64  // FALLTHROUGH
65  case ')':
66  // Skip to the next matching '('.
67  level++;
68  break;
69  }
70  }
71  // Fallback to the default
72  return XNT();
73 }
74 
76  return event == Ion::Events::OK || event == Ion::Events::EXE;
77 }
78 
80  if (textField->isEditing() && textField->textFieldShouldFinishEditing(event)) {
81  Expression * exp = Expression::parse(textField->text());
82  if (exp != nullptr) {
83  delete exp;
84  }
85  if (exp == nullptr) {
86  textField->app()->displayWarning(I18n::Message::SyntaxError);
87  return true;
88  }
89  }
90  if (event == Ion::Events::Var) {
91  if (!textField->isEditing()) {
92  textField->setEditing(true);
93  }
94  AppsContainer * appsContainer = (AppsContainer *)textField->app()->container();
95  VariableBoxController * variableBoxController = appsContainer->variableBoxController();
96  variableBoxController->setTextFieldCaller(textField);
98  return true;
99  }
100  if (event == Ion::Events::XNT) {
101  if (!textField->isEditing()) {
102  textField->setEditing(true);
103  }
104  const char * xnt = privateXNT(textField);
105  return textField->handleEventWithText(xnt);
106  }
107  return false;
108 }
109 
111  return container()->mathToolbox();
112 }
113 
114 }
Charge level()
Definition: battery.cpp:20
#define exp(x)
Definition: math.h:176
bool isEditing() const
Definition: text_field.cpp:175
constexpr Event Var
Definition: events.h:74
void setTextFieldCaller(TextField *textField)
Definition: i18n.h:6
void displayModalViewController(ViewController *vc, float verticalAlignment, float horizontalAlignment, KDCoordinate topMargin=0, KDCoordinate leftMargin=0, KDCoordinate bottomMargin=0, KDCoordinate rightMargin=0)
Definition: app.cpp:85
size_t cursorLocation() const
Definition: text_input.h:19
bool textFieldShouldFinishEditing(TextField *textField, Ion::Events::Event event) override
constexpr Event EXE
Definition: events.h:114
bool textFieldShouldFinishEditing(Ion::Events::Event event)
Definition: text_field.h:27
virtual void setEditing(bool isEditing, bool reinitDraftBuffer=true)
Definition: text_field.cpp:196
virtual Poincare::Context * localContext()
VariableBoxController * variableBoxController()
size_t strlen(const char *s)
Definition: strlen.c:3
void displayWarning(I18n::Message warningMessage)
Definition: app.cpp:97
Poincare::Context * globalContext()
constexpr Event XNT
Definition: events.h:73
static constexpr KDCoordinate PopUpTopMargin
Definition: metric.h:23
const char * text() const
Definition: text_input.h:14
MathToolbox * mathToolbox()
Definition: app.h:23
virtual bool textFieldDidReceiveEvent(TextField *textField, Ion::Events::Event event) override
const Container * container() const
Definition: app.cpp:102
bool handleEventWithText(const char *text, bool indenting=false) override
Definition: text_field.cpp:303
LIBA_BEGIN_DECLS int memcmp(const void *s1, const void *s2, size_t n)
Definition: memcmp.c:3
App * app()
Definition: responder.cpp:77
constexpr Event OK
Definition: events.h:65
static constexpr KDCoordinate PopUpRightMargin
Definition: metric.h:22
static constexpr KDCoordinate PopUpLeftMargin
Definition: metric.h:21
Toolbox * toolboxForTextInput(TextInput *textInput) override