Blame view

emulateur/epsilon-nofrendo/poincare/src/layout/grid_layout.cpp 9.19 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  #include "grid_layout.h"
  #include "empty_layout.h"
  #include <poincare/expression_layout_cursor.h>
  #include <poincare/layout_engine.h>
  extern "C" {
  #include <assert.h>
  #include <stdlib.h>
  }
  
  namespace Poincare {
  
  GridLayout::GridLayout(const ExpressionLayout * const * entryLayouts, int numberOfRows, int numberOfColumns, bool cloneOperands) :
    DynamicLayoutHierarchy(entryLayouts, numberOfRows*numberOfColumns, cloneOperands),
    m_numberOfRows(numberOfRows),
    m_numberOfColumns(numberOfColumns)
  {
  }
  
  ExpressionLayout * GridLayout::clone() const {
    GridLayout * layout = new GridLayout(const_cast<ExpressionLayout **>(children()), m_numberOfRows, m_numberOfColumns, true);
    return layout;
  }
  
  ExpressionLayoutCursor GridLayout::cursorLeftOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) {
    // Case: Right. Go to the last entry.
    if (cursor.pointedExpressionLayout() == this
        && cursor.position() == ExpressionLayoutCursor::Position::Right)
    {
      ExpressionLayout * lastChild = editableChild(m_numberOfColumns*m_numberOfRows-1);
      assert(lastChild != nullptr);
      return ExpressionLayoutCursor(lastChild, ExpressionLayoutCursor::Position::Right);
    }
    // Case: The cursor points to a grid's child.
    int childIndex = indexOfChild(cursor.pointedExpressionLayout());
    if (childIndex >- 1 && cursor.position() == ExpressionLayoutCursor::Position::Left) {
      if (childIsLeftOfGrid(childIndex)) {
        // Case: Left of a child on the left of the grid. Go Left of the grid
        return ExpressionLayoutCursor(this, ExpressionLayoutCursor::Position::Left);
      }
      // Case: Left of another child. Go Right of its sibling on the left.
      return ExpressionLayoutCursor(editableChild(childIndex-1), ExpressionLayoutCursor::Position::Right);
    }
    assert(cursor.pointedExpressionLayout() == this);
    // Case: Left. Ask the parent.
    if (m_parent) {
      return m_parent->cursorLeftOf(cursor, shouldRecomputeLayout);
    }
    return ExpressionLayoutCursor();
  }
  
  ExpressionLayoutCursor GridLayout::cursorRightOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) {
    // Case: Left. Go to the first entry.
    if (cursor.pointedExpressionLayout() == this
        && cursor.position() == ExpressionLayoutCursor::Position::Left)
    {
      assert(m_numberOfColumns*m_numberOfRows >= 1);
      ExpressionLayout * firstChild = editableChild(0);
      assert(firstChild != nullptr);
        return ExpressionLayoutCursor(firstChild, ExpressionLayoutCursor::Position::Left);
        }
    // Case: The cursor points to a grid's child.
    int childIndex = indexOfChild(cursor.pointedExpressionLayout());
    if (childIndex >- 1 && cursor.position() == ExpressionLayoutCursor::Position::Right) {
      if (childIsRightOfGrid(childIndex)) {
        // Case: Right of a child on the right of the grid. Go Right of the grid.
        return ExpressionLayoutCursor(this, ExpressionLayoutCursor::Position::Right);
      }
      // Case: Right of another child. Go Left of its sibling on the right.
      return ExpressionLayoutCursor(editableChild(childIndex+1), ExpressionLayoutCursor::Position::Left);
    }
    assert(cursor.pointedExpressionLayout() == this);
    // Case: Right. Ask the parent.
    if (m_parent) {
      return m_parent->cursorRightOf(cursor, shouldRecomputeLayout);
    }
    return ExpressionLayoutCursor();
  }
  
  ExpressionLayoutCursor GridLayout::cursorAbove(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited) {
    /* If the cursor is child that is not on the top row, move it inside its upper
     * neighbour.*/
    int childIndex = m_numberOfColumns;
    while (childIndex < numberOfChildren()) {
      if (cursor.pointedExpressionLayout()->hasAncestor(child(childIndex), true)) {
        return editableChild(childIndex - m_numberOfColumns)->cursorInDescendantsAbove(cursor, shouldRecomputeLayout);
      }
      childIndex++;
    }
    return ExpressionLayout::cursorAbove(cursor, shouldRecomputeLayout, equivalentPositionVisited);
  }
  
  ExpressionLayoutCursor GridLayout::cursorUnder(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited) {
    int childIndex = 0;
    while (childIndex < numberOfChildren() - m_numberOfColumns) {
      if (cursor.pointedExpressionLayout()->hasAncestor(child(childIndex), true)) {
        return editableChild(childIndex + m_numberOfColumns)->cursorInDescendantsUnder(cursor, shouldRecomputeLayout);
      }
      childIndex++;
    }
    return ExpressionLayout::cursorUnder(cursor, shouldRecomputeLayout, equivalentPositionVisited);
  }
  
  void GridLayout::removeChildAtIndex(int index, bool deleteAfterRemoval) {
    ExpressionLayout::removeChildAtIndex(index, deleteAfterRemoval);
  }
  
  KDCoordinate GridLayout::rowBaseline(int i) {
    KDCoordinate rowBaseline = 0;
    for (int j = 0; j < m_numberOfColumns; j++) {
      rowBaseline = max(rowBaseline, editableChild(i*m_numberOfColumns+j)->baseline());
    }
    return rowBaseline;
  }
  
  KDCoordinate GridLayout::rowHeight(int i) {
    KDCoordinate rowHeight = 0;
    KDCoordinate baseline = rowBaseline(i);
    for (int j = 0; j < m_numberOfColumns; j++) {
      rowHeight = max(rowHeight, editableChild(i*m_numberOfColumns+j)->size().height() - editableChild(i*m_numberOfColumns+j)->baseline());
    }
    return baseline+rowHeight;
  }
  
  KDCoordinate GridLayout::height() {
    KDCoordinate totalHeight = 0;
    for (int i = 0; i < m_numberOfRows; i++) {
      totalHeight += rowHeight(i);
    }
    totalHeight += (m_numberOfRows-1)*k_gridEntryMargin;
    return totalHeight;
  }
  
  KDCoordinate GridLayout::columnWidth(int j) {
    KDCoordinate columnWidth = 0;
    for (int i = 0; i < m_numberOfRows; i++) {
      columnWidth = max(columnWidth, editableChild(i*m_numberOfColumns+j)->size().width());
    }
    return columnWidth;
  }
  
  KDCoordinate GridLayout::width() {
    KDCoordinate totalWidth = 0;
    for (int j = 0; j < m_numberOfColumns; j++) {
      totalWidth += columnWidth(j);
    }
    totalWidth += (m_numberOfColumns-1)*k_gridEntryMargin;
    return totalWidth;
  }
  
  void GridLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) {
    // Nothing to do for a simple grid
  }
  
  KDSize GridLayout::computeSize() {
    return KDSize(width(), height());
  }
  
  void GridLayout::computeBaseline() {
    m_baseline = (height()+1)/2;
    m_baselined = true;
  }
  
  KDPoint GridLayout::positionOfChild(ExpressionLayout * child) {
    int rowIndex = 0;
    int columnIndex = 0;
    for (int i = 0; i < m_numberOfRows; i++) {
      for (int j = 0; j < m_numberOfColumns; j++) {
        if (child == editableChild(i*m_numberOfColumns+j)) {
          rowIndex = i;
          columnIndex = j;
          break;
        }
      }
    }
    KDCoordinate x = 0;
    for (int j = 0; j < columnIndex; j++) {
      x += columnWidth(j);
    }
    x += (columnWidth(columnIndex) - child->size().width())/2+ columnIndex * k_gridEntryMargin;
    KDCoordinate y = 0;
    for (int i = 0; i < rowIndex; i++) {
      y += rowHeight(i);
    }
    y += rowBaseline(rowIndex) - child->baseline() + rowIndex * k_gridEntryMargin;
    return KDPoint(x, y);
  }
  
  void GridLayout::addEmptyRow(EmptyLayout::Color color) {
    ExpressionLayout * newChildren[m_numberOfColumns];
    for (int i = 0; i < m_numberOfColumns; i++) {
      newChildren[i] = new EmptyLayout(color);
    }
    addChildrenAtIndex(const_cast<const ExpressionLayout * const *>(const_cast<ExpressionLayout * const *>(newChildren)), m_numberOfColumns, numberOfChildren(), false);
    m_numberOfRows++;
  }
  
  void GridLayout::addEmptyColumn(EmptyLayout::Color color) {
    m_numberOfColumns++;
    for (int i = 0; i < m_numberOfRows; i++) {
      addChildAtIndex(new EmptyLayout(color), i*m_numberOfColumns + m_numberOfColumns-1);
    }
  }
  
  void GridLayout::deleteRowAtIndex(int index) {
    assert(index >= 0 && index < m_numberOfRows);
    for (int i = 0; i < m_numberOfColumns; i++) {
      DynamicLayoutHierarchy::removeChildAtIndex(index * m_numberOfColumns, true);
    }
    m_numberOfRows--;
  }
  
  void GridLayout::deleteColumnAtIndex(int index) {
    assert(index >= 0 && index < m_numberOfColumns);
    for (int i = (m_numberOfRows - 1) * m_numberOfColumns + index; i > -1; i-= m_numberOfColumns) {
      DynamicLayoutHierarchy::removeChildAtIndex(i, true);
    }
    m_numberOfColumns--;
  }
  
  bool GridLayout::childIsLeftOfGrid(int index) const {
    assert(index >= 0 && index < m_numberOfRows*m_numberOfColumns);
    return columnAtChildIndex(index) == 0;
  }
  
  bool GridLayout::childIsRightOfGrid(int index) const {
    assert(index >= 0 && index < m_numberOfRows*m_numberOfColumns);
    return columnAtChildIndex(index) == m_numberOfColumns - 1;
  }
  
  bool GridLayout::childIsTopOfGrid(int index) const {
    assert(index >= 0 && index < m_numberOfRows*m_numberOfColumns);
    return rowAtChildIndex(index) == 0;
  }
  
  bool GridLayout::childIsBottomOfGrid(int index) const {
    assert(index >= 0 && index < m_numberOfRows*m_numberOfColumns);
    return rowAtChildIndex(index) == m_numberOfRows - 1;
  }
  
  int GridLayout::rowAtChildIndex(int index) const {
    assert(index >= 0 && index < m_numberOfRows*m_numberOfColumns);
    return (int)(index / m_numberOfColumns);
  }
  
  int GridLayout::columnAtChildIndex(int index) const {
    assert(index >= 0 && index < m_numberOfRows*m_numberOfColumns);
    return index - m_numberOfColumns * rowAtChildIndex(index);
  }
  
  int GridLayout::indexAtRowColumn(int rowIndex, int columnIndex) const {
    assert(rowIndex >= 0 && rowIndex < m_numberOfRows);
    assert(columnIndex >= 0 && columnIndex < m_numberOfColumns);
    return rowIndex * m_numberOfColumns + columnIndex;
  }
  
  }