Blame view

build1/epsilon-master/escher/src/expression_field.cpp 4.21 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
  #include <escher/expression_field.h>
  #include <poincare/preferences.h>
  #include <assert.h>
  
  ExpressionField::ExpressionField(Responder * parentResponder, char * textBuffer, int textBufferLength, Poincare::ExpressionLayout * layout, TextFieldDelegate * textFieldDelegate, ExpressionLayoutFieldDelegate * expressionLayoutFieldDelegate) :
    Responder(parentResponder),
    View(),
    m_textField(parentResponder, textBuffer, textBuffer, textBufferLength, textFieldDelegate, false, KDText::FontSize::Large, 0.0f, 0.5f, KDColorBlack, KDColorWhite),
    m_expressionLayoutField(parentResponder, layout, expressionLayoutFieldDelegate),
    m_textBuffer(textBuffer),
    m_textBufferLength(textBufferLength)
  {
    // Initialize text field
    m_textField.setMargins(0, k_horizontalMargin, 0, k_horizontalMargin);
    m_textField.setBackgroundColor(KDColorWhite);
    m_textField.setColorsBackground(true);
    // Initialize layout field
    m_expressionLayoutField.setMargins(k_verticalMargin, k_horizontalMargin, k_verticalMargin, k_horizontalMargin);
    m_expressionLayoutField.setBackgroundColor(KDColorWhite);
    m_expressionLayoutField.setColorsBackground(true);
  }
  
  void ExpressionField::setEditing(bool isEditing, bool reinitDraftBuffer) {
    if (editionIsInTextField()) {
      m_textField.setEditing(isEditing, reinitDraftBuffer);
    } else {
      if (reinitDraftBuffer) {
        m_expressionLayoutField.clearLayout();
      }
      m_expressionLayoutField.setEditing(isEditing);
    }
  }
  
  bool ExpressionField::isEditing() const {
    return editionIsInTextField() ? m_textField.isEditing() : m_expressionLayoutField.isEditing();
  }
  
  const char * ExpressionField::text() {
    if (!editionIsInTextField()) {
      m_expressionLayoutField.writeTextInBuffer(m_textBuffer, m_textBufferLength);
    }
    return m_textBuffer;
  }
  
  void ExpressionField::setText(const char * text) {
    if (editionIsInTextField()) {
      m_textField.setText(text);
    } else {
      m_expressionLayoutField.clearLayout();
      m_expressionLayoutField.handleEventWithText(text, false, true);
    }
  }
  
  View * ExpressionField::subviewAtIndex(int index) {
    assert(index == 0);
    if (editionIsInTextField()) {
      return &m_textField;
    }
    return &m_expressionLayoutField;
  }
  
  void ExpressionField::layoutSubviews() {
    KDRect inputViewFrame(0, k_separatorThickness, bounds().width(), bounds().height() - k_separatorThickness);
    if (editionIsInTextField()) {
      m_textField.setFrame(inputViewFrame);
      m_expressionLayoutField.setFrame(KDRectZero);
      return;
    }
    m_expressionLayoutField.setFrame(inputViewFrame);
    m_textField.setFrame(KDRectZero);
  }
  
  void ExpressionField::reload() {
    layoutSubviews();
    markRectAsDirty(bounds());
  }
  
  void ExpressionField::drawRect(KDContext * ctx, KDRect rect) const {
    // Draw the separator
    ctx->fillRect(KDRect(0, 0, bounds().width(), k_separatorThickness), Palette::GreyMiddle);
  }
  
  bool ExpressionField::handleEvent(Ion::Events::Event event) {
    return editionIsInTextField() ? m_textField.handleEvent(event) : m_expressionLayoutField.handleEvent(event);
  }
  
  KDSize ExpressionField::minimalSizeForOptimalDisplay() const {
    return KDSize(0, inputViewHeight());
  }
  
  bool ExpressionField::editionIsInTextField() const {
    return Poincare::Preferences::sharedPreferences()->editionMode() == Poincare::Preferences::EditionMode::Edition1D;
  }
  
  bool ExpressionField::isEmpty() const {
    return editionIsInTextField() ? (m_textField.draftTextLength() == 0) : !m_expressionLayoutField.hasText();
  }
  
  bool ExpressionField::heightIsMaximal() const {
    return inputViewHeight() == k_separatorThickness + maximalHeight();
  }
  
  bool ExpressionField::handleEventWithText(const char * text, bool indentation, bool forceCursorRightOfText) {
    if (editionIsInTextField()) {
      return m_textField.handleEventWithText(text, indentation, forceCursorRightOfText);
    } else {
      return m_expressionLayoutField.handleEventWithText(text, indentation, forceCursorRightOfText);
    }
  }
  
  KDCoordinate ExpressionField::inputViewHeight() const {
    return k_separatorThickness
      + (editionIsInTextField() ? k_textFieldHeight :
          min(maximalHeight(),
            max(k_textFieldHeight, m_expressionLayoutField.minimalSizeForOptimalDisplay().height() + 2*k_verticalMargin )));
  }
  
  KDCoordinate ExpressionField::maximalHeight() const {
    return 0.6*Ion::Display::Height;
  }