Blame view

build2/epsilon-master/apps/shared/expression_field_delegate_app.cpp 2.98 KB
6663b6c9   adorian   projet complet av...
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
  #include "expression_field_delegate_app.h"
  #include <escher.h>
  #include "../i18n.h"
  #include "../apps_container.h"
  
  using namespace Poincare;
  
  namespace Shared {
  
  ExpressionFieldDelegateApp::ExpressionFieldDelegateApp(Container * container, Snapshot * snapshot, ViewController * rootViewController) :
    TextFieldDelegateApp(container, snapshot, rootViewController),
    ExpressionLayoutFieldDelegate()
  {
  }
  
  char ExpressionFieldDelegateApp::privateXNT(ExpressionLayoutField * expressionLayoutField) {
    char xntCharFromLayout = expressionLayoutField->XNTChar();
    if (xntCharFromLayout != Ion::Charset::Empty) {
      return xntCharFromLayout;
    }
    return XNT()[0];
  }
  
  bool ExpressionFieldDelegateApp::expressionLayoutFieldShouldFinishEditing(ExpressionLayoutField * expressionLayoutField, Ion::Events::Event event) {
    return event == Ion::Events::OK || event == Ion::Events::EXE;
  }
  
  bool ExpressionFieldDelegateApp::expressionLayoutFieldDidReceiveEvent(ExpressionLayoutField * expressionLayoutField, Ion::Events::Event event) {
    if (expressionLayoutField->isEditing() && expressionLayoutField->expressionLayoutFieldShouldFinishEditing(event)) {
      if (!expressionLayoutField->hasText()) {
        expressionLayoutField->app()->displayWarning(I18n::Message::SyntaxError);
        return true;
      }
      char buffer[TextField::maxBufferSize()];
      int bufferSize = TextField::maxBufferSize();
      int length = expressionLayoutField->writeTextInBuffer(buffer, bufferSize);
      Expression * exp = Expression::parse(buffer);
      if (exp != nullptr) {
        delete exp;
      }
      if (length >= bufferSize-1) {
        /* If the buffer is totally full, it is VERY likely that writeTextInBuffer
         * escaped before printing utterly the expression. */
        displayWarning(I18n::Message::SyntaxError);
        return true;
      }
      if (exp == nullptr) {
        expressionLayoutField->app()->displayWarning(I18n::Message::SyntaxError);
        return true;
      }
    }
    if (event == Ion::Events::Var) {
      if (!expressionLayoutField->isEditing()) {
        expressionLayoutField->setEditing(true);
      }
      AppsContainer * appsContainer = (AppsContainer *)expressionLayoutField->app()->container();
      VariableBoxController * variableBoxController = appsContainer->variableBoxController();
      variableBoxController->setSender(expressionLayoutField);
      expressionLayoutField->app()->displayModalViewController(variableBoxController, 0.f, 0.f, Metric::PopUpTopMargin, Metric::PopUpLeftMargin, 0, Metric::PopUpRightMargin);
      return true;
    }
    if (event == Ion::Events::XNT) {
      if (!expressionLayoutField->isEditing()) {
        expressionLayoutField->setEditing(true);
      }
      const char xnt[2] = {privateXNT(expressionLayoutField), 0};
      return expressionLayoutField->handleEventWithText(xnt);
    }
    return false;
  }
  
  Toolbox * ExpressionFieldDelegateApp::toolboxForExpressionLayoutField(ExpressionLayoutField * expressionLayoutField) {
    Toolbox * toolbox = container()->mathToolbox();
    toolbox->setSender(expressionLayoutField);
    return toolbox;
  }
  
  }