Numworks Epsilon  1.4.1
Graphing Calculator Operating System
horizontal_layout.cpp
Go to the documentation of this file.
1 extern "C" {
2 #include <assert.h>
3 #include <kandinsky.h>
4 #include <stdlib.h>
5 }
6 #include "horizontal_layout.h"
7 #include "string_layout.h"
8 
9 namespace Poincare {
10 
11 HorizontalLayout::HorizontalLayout(ExpressionLayout ** children_layouts, int number_of_children) :
12  ExpressionLayout(), m_number_of_children(number_of_children) {
13  assert(number_of_children > 0);
14  m_children_layouts = new ExpressionLayout *[number_of_children];
15  for (int i=0; i<m_number_of_children; i++) {
16  m_children_layouts[i] = children_layouts[i];
17  m_children_layouts[i]->setParent(this);
18  if (m_children_layouts[i]->baseline() > m_baseline) {
19  m_baseline = m_children_layouts[i]->baseline();
20  }
21  }
22 }
23 
25  for (int i=0; i<m_number_of_children; i++) {
26  delete m_children_layouts[i];
27  }
28  delete[] m_children_layouts;
29 }
30 
31 void HorizontalLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) {
32 }
33 
35  KDCoordinate totalWidth = 0;
36  int i = 0;
37  KDCoordinate max_under_baseline = 0;
38  KDCoordinate max_above_baseline = 0;
39  while (ExpressionLayout * c = child(i++)) {
40  KDSize childSize = c->size();
41  totalWidth += childSize.width();
42  if (childSize.height() - c->baseline() > max_under_baseline) {
43  max_under_baseline = childSize.height() - c->baseline() ;
44  }
45  if (c->baseline() > max_above_baseline) {
46  max_above_baseline = c->baseline();
47  }
48  }
49  return KDSize(totalWidth, max_under_baseline + max_above_baseline);
50 }
51 
53  assert(index <= (unsigned int) m_number_of_children);
54  if (index < (unsigned int) m_number_of_children) {
55  return m_children_layouts[index];
56  } else {
57  return nullptr;
58  }
59 }
60 
62  KDCoordinate x = 0;
63  KDCoordinate y = 0;
64  uint16_t index = 0;
65  for (int i=0;i<m_number_of_children;i++) {
66  if (m_children_layouts[i] == child) {
67  index = i;
68  break;
69  }
70  }
71  if (index > 0) {
72  ExpressionLayout * previousChild = m_children_layouts[index-1];
73  assert(previousChild != nullptr);
74  x = previousChild->origin().x() + previousChild->size().width();
75  }
76  y = m_baseline - child->baseline();
77  return KDPoint(x, y);
78 }
79 
80 }
#define assert(e)
Definition: assert.h:9
int16_t KDCoordinate
Definition: coordinate.h:6
constexpr KDCoordinate width() const
Definition: size.h:10
unsigned short uint16_t
Definition: stdint.h:5
Definition: point.h:6
Definition: size.h:6
c(generic_all_nodes)
HorizontalLayout(ExpressionLayout **layouts, int number_of_children)
void render(KDContext *ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override
Definition: color.h:6
ExpressionLayout * child(uint16_t index) override
KDCoordinate x() const
Definition: point.h:10
void setParent(ExpressionLayout *parent)
KDPoint positionOfChild(ExpressionLayout *child) override
constexpr KDCoordinate height() const
Definition: size.h:11