Numworks Epsilon  1.4.1
Graphing Calculator Operating System
grid_layout.cpp
Go to the documentation of this file.
1 #include "grid_layout.h"
2 extern "C" {
3 #include <assert.h>
4 #include <stdlib.h>
5 }
6 
7 namespace Poincare {
8 
11  m_numberOfRows(numberOfRows),
12  m_numberOfColumns(numberOfColumns)
13 {
14  m_entryLayouts = new ExpressionLayout *[numberOfColumns*numberOfRows];
15  for (int i = 0; i < m_numberOfRows*m_numberOfColumns; i++) {
16  m_entryLayouts[i] = entryLayouts[i];
17  m_entryLayouts[i]->setParent(this);
18  }
19  m_baseline = (height()+1)/2;
20 }
21 
23  for (int i=0; i<m_numberOfColumns*m_numberOfRows; i++) {
24  delete m_entryLayouts[i];
25  }
26  delete[] m_entryLayouts;
27 }
28 
29 KDCoordinate GridLayout::rowBaseline(int i) {
30  KDCoordinate rowBaseline = 0;
31  for (int j = 0; j < m_numberOfColumns; j++) {
32  rowBaseline = max(rowBaseline, m_entryLayouts[i*m_numberOfColumns+j]->baseline());
33  }
34  return rowBaseline;
35 }
36 
37 
38 KDCoordinate GridLayout::rowHeight(int i) {
39  KDCoordinate rowHeight = 0;
40  KDCoordinate baseline = rowBaseline(i);
41  for (int j = 0; j < m_numberOfColumns; j++) {
42  rowHeight = max(rowHeight, m_entryLayouts[i*m_numberOfColumns+j]->size().height() - m_entryLayouts[i*m_numberOfColumns+j]->baseline());
43  }
44  return baseline+rowHeight;
45 }
46 
47 KDCoordinate GridLayout::height() {
48  KDCoordinate totalHeight = 0;
49  for (int i = 0; i < m_numberOfRows; i++) {
50  totalHeight += rowHeight(i);
51  }
52  totalHeight += (m_numberOfRows-1)*k_gridEntryMargin;
53  return totalHeight;
54 }
55 
56 KDCoordinate GridLayout::columnWidth(int j) {
57  KDCoordinate columnWidth = 0;
58  for (int i = 0; i < m_numberOfRows; i++) {
59  columnWidth = max(columnWidth, m_entryLayouts[i*m_numberOfColumns+j]->size().width());
60  }
61  return columnWidth;
62 }
63 
64 KDCoordinate GridLayout::width() {
65  KDCoordinate totalWidth = 0;
66  for (int j = 0; j < m_numberOfColumns; j++) {
67  totalWidth += columnWidth(j);
68  }
69  totalWidth += (m_numberOfColumns-1)*k_gridEntryMargin;
70  return totalWidth;
71 }
72 
73 void GridLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) {
74  // Nothing to do for a simple grid
75 }
76 
78  return KDSize(width(), height());
79 }
80 
82  if (index < m_numberOfColumns*m_numberOfRows) {
83  return m_entryLayouts[index];
84  }
85  return nullptr;
86 }
87 
89  int rowIndex = 0;
90  int columnIndex = 0;
91  for (int i = 0; i < m_numberOfRows; i++) {
92  for (int j = 0; j < m_numberOfColumns; j++) {
93  if (child == m_entryLayouts[i*m_numberOfColumns+j]) {
94  rowIndex = i;
95  columnIndex = j;
96  break;
97  }
98  }
99  }
100  KDCoordinate x = 0;
101  for (int j = 0; j < columnIndex; j++) {
102  x += columnWidth(j);
103  }
104  x += (columnWidth(columnIndex) - child->size().width())/2+ columnIndex * k_gridEntryMargin;
105  KDCoordinate y = 0;
106  for (int i = 0; i < rowIndex; i++) {
107  y += rowHeight(i);
108  }
109  y += rowBaseline(rowIndex) - child->baseline() + rowIndex * k_gridEntryMargin;
110  return KDPoint(x, y);
111 }
112 
113 }
KDPoint positionOfChild(ExpressionLayout *child) override
Definition: grid_layout.cpp:88
GridLayout(ExpressionLayout **entryLayouts, int numberOfRows, int numberOfColumns)
Definition: grid_layout.cpp:9
int16_t KDCoordinate
Definition: coordinate.h:6
constexpr KDCoordinate width() const
Definition: size.h:10
unsigned short uint16_t
Definition: stdint.h:5
void render(KDContext *ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override
Definition: grid_layout.cpp:73
Definition: point.h:6
ExpressionLayout * child(uint16_t index) override
Definition: grid_layout.cpp:81
Definition: size.h:6
constexpr uint8_t numberOfColumns
Definition: keyboard.h:39
Definition: color.h:6
constexpr uint8_t numberOfRows
Definition: keyboard.h:35
KDSize computeSize() override
Definition: grid_layout.cpp:77
void setParent(ExpressionLayout *parent)