Blame view

build4/epsilon-master/apps/calculation/edit_expression_controller.cpp 7.16 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
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
  #include "edit_expression_controller.h"
  #include "app.h"
  #include "../apps_container.h"
  #include <ion/display.h>
  #include <poincare/preferences.h>
  #include <assert.h>
  
  using namespace Shared;
  using namespace Poincare;
  
  namespace Calculation {
  
  EditExpressionController::ContentView::ContentView(Responder * parentResponder, TableView * subview, TextFieldDelegate * textFieldDelegate, ExpressionLayoutFieldDelegate * expressionLayoutFieldDelegate) :
    View(),
    m_mainView(subview),
    m_layout(new Poincare::HorizontalLayout()),
    m_expressionField(parentResponder, m_textBody, k_bufferLength, m_layout, textFieldDelegate, expressionLayoutFieldDelegate)
  {
    m_textBody[0] = 0;
  }
  
  EditExpressionController::ContentView::~ContentView() {
    delete m_layout;
  }
  
  View * EditExpressionController::ContentView::subviewAtIndex(int index) {
    assert(index >= 0 && index < numberOfSubviews());
    if (index == 0) {
      return m_mainView;
    }
    assert(index == 1);
    return &m_expressionField;
  }
  
  void EditExpressionController::ContentView::layoutSubviews() {
    KDCoordinate inputViewFrameHeight = m_expressionField.minimalSizeForOptimalDisplay().height();
    KDRect mainViewFrame(0, 0, bounds().width(), bounds().height() - inputViewFrameHeight);
    m_mainView->setFrame(mainViewFrame);
    KDRect inputViewFrame(0, bounds().height() - inputViewFrameHeight, bounds().width(), inputViewFrameHeight);
    m_expressionField.setFrame(inputViewFrame);
  }
  
  void EditExpressionController::ContentView::reload() {
    layoutSubviews();
    markRectAsDirty(bounds());
  }
  
  EditExpressionController::EditExpressionController(Responder * parentResponder, HistoryController * historyController, CalculationStore * calculationStore) :
    DynamicViewController(parentResponder),
    m_historyController(historyController),
    m_calculationStore(calculationStore),
    m_inputViewHeightIsMaximal(false)
  {
    m_cacheBuffer[0] = 0;
  }
  
  void EditExpressionController::insertTextBody(const char * text) {
    ((ContentView *)view())->expressionField()->handleEventWithText(text, false, true);
  }
  
  bool EditExpressionController::handleEvent(Ion::Events::Event event) {
    if (event == Ion::Events::Up) {
      if (m_calculationStore->numberOfCalculations() > 0) {
        m_cacheBuffer[0] = 0;
        ((ContentView *)view())->expressionField()->setEditing(false, false);
        app()->setFirstResponder(m_historyController);
      }
      return true;
    }
    return false;
  }
  
  void EditExpressionController::didBecomeFirstResponder() {
    int lastRow = m_calculationStore->numberOfCalculations() > 0 ? m_calculationStore->numberOfCalculations()-1 : 0;
    m_historyController->scrollToCell(0, lastRow);
    ((ContentView *)view())->expressionField()->setEditing(true, false);
    app()->setFirstResponder(((ContentView *)view())->expressionField());
  }
  
  bool EditExpressionController::textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event event) {
    if (textField->isEditing() && textField->textFieldShouldFinishEditing(event) && textField->draftTextLength() == 0 && m_cacheBuffer[0] != 0) {
      return inputViewDidReceiveEvent(event);
    }
    return textFieldDelegateApp()->textFieldDidReceiveEvent(textField, event);
  }
  
  bool EditExpressionController::textFieldDidFinishEditing(::TextField * textField, const char * text, Ion::Events::Event event) {
    return inputViewDidFinishEditing(text, nullptr);
  }
  
  bool EditExpressionController::textFieldDidAbortEditing(::TextField * textField) {
    return inputViewDidAbortEditing(textField->text());
  }
  
  bool EditExpressionController::expressionLayoutFieldDidReceiveEvent(::ExpressionLayoutField * expressionLayoutField, Ion::Events::Event event) {
    if (expressionLayoutField->isEditing() && expressionLayoutField->expressionLayoutFieldShouldFinishEditing(event) && !expressionLayoutField->hasText() && m_calculationStore->numberOfCalculations() > 0) {
      return inputViewDidReceiveEvent(event);
    }
    return expressionFieldDelegateApp()->expressionLayoutFieldDidReceiveEvent(expressionLayoutField, event);
  }
  
  bool EditExpressionController::expressionLayoutFieldDidFinishEditing(::ExpressionLayoutField * expressionLayoutField, ExpressionLayout * layout, Ion::Events::Event event) {
    return inputViewDidFinishEditing(nullptr, layout);
  }
  
  bool EditExpressionController::expressionLayoutFieldDidAbortEditing(::ExpressionLayoutField * expressionLayoutField) {
    return inputViewDidAbortEditing(nullptr);
  }
  
  void EditExpressionController::expressionLayoutFieldDidChangeSize(::ExpressionLayoutField * expressionLayoutField) {
    /* Reload the view only if the ExpressionField height actually changes, i.e.
     * not if the height is already maximal and stays maximal. */
    if (view()) {
      bool newInputViewHeightIsMaximal = static_cast<ContentView *>(view())->expressionField()->heightIsMaximal();
      if (!m_inputViewHeightIsMaximal || !newInputViewHeightIsMaximal) {
        m_inputViewHeightIsMaximal = newInputViewHeightIsMaximal;
        reloadView();
      }
    }
  }
  
  TextFieldDelegateApp * EditExpressionController::textFieldDelegateApp() {
    return (App *)app();
  }
  
  ExpressionFieldDelegateApp * EditExpressionController::expressionFieldDelegateApp() {
    return (App *)app();
  }
  
  View * EditExpressionController::loadView() {
    return new ContentView(this, (TableView *)m_historyController->view(), this, this);
  }
  
  void EditExpressionController::unloadView(View * view) {
    delete view;
  }
  
  void EditExpressionController::reloadView() {
    ((ContentView *)view())->reload();
    m_historyController->reload();
    if (m_historyController->numberOfRows() > 0) {
      ((ContentView *)view())->mainView()->scrollToCell(0, m_historyController->numberOfRows()-1);
    }
  }
  
  bool EditExpressionController::inputViewDidReceiveEvent(Ion::Events::Event event) {
    App * calculationApp = (App *)app();
    /* The input text store in m_cacheBuffer might have beed correct the first
     * time but then be too long when replacing ans in another context */
    if (!calculationApp->textInputIsCorrect(m_cacheBuffer)) {
      return true;
    }
    m_calculationStore->push(m_cacheBuffer, calculationApp->localContext());
    m_historyController->reload();
    ((ContentView *)view())->mainView()->scrollToCell(0, m_historyController->numberOfRows()-1);
    return true;
  }
  
  
  bool EditExpressionController::inputViewDidFinishEditing(const char * text, ExpressionLayout * layout) {
    App * calculationApp = (App *)app();
    if (layout == nullptr) {
      assert(text);
      strlcpy(m_cacheBuffer, text, Calculation::k_printedExpressionSize);
    } else {
      assert(layout);
      layout->writeTextInBuffer(m_cacheBuffer, Calculation::k_printedExpressionSize);
    }
    m_calculationStore->push(m_cacheBuffer, calculationApp->localContext());
    m_historyController->reload();
    ((ContentView *)view())->mainView()->scrollToCell(0, m_historyController->numberOfRows()-1);
    ((ContentView *)view())->expressionField()->setEditing(true);
    ((ContentView *)view())->expressionField()->setText("");
    return true;
  }
  
  bool EditExpressionController::inputViewDidAbortEditing(const char * text) {
    if (text != nullptr) {
      ((ContentView *)view())->expressionField()->setEditing(true, true);
      ((ContentView *)view())->expressionField()->setText(text);
    }
    return false;
  }
  
  void EditExpressionController::viewDidDisappear() {
    DynamicViewController::viewDidDisappear();
    m_historyController->viewDidDisappear();
  }
  
  }