Numworks Epsilon  1.4.1
Graphing Calculator Operating System
sequence_toolbox.cpp
Go to the documentation of this file.
1 #include "sequence_toolbox.h"
2 #include "../sequence_store.h"
3 #include "../../../poincare/src/layout/baseline_relative_layout.h"
4 #include "../../../poincare/src/layout/string_layout.h"
5 #include <assert.h>
6 
7 using namespace Poincare;
8 
9 namespace Sequence {
10 
11 SequenceToolbox::SequenceToolbox() :
12  MathToolbox(),
13  m_addedCellLayout{},
14  m_numberOfAddedCells(0)
15 {
16 }
17 
19  for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
20  if (m_addedCellLayout[i]) {
21  delete m_addedCellLayout[i];
22  m_addedCellLayout[i] = nullptr;
23  }
24  }
25 }
26 
28  if (selectedRow() < m_numberOfAddedCells && stackDepth() == 0) {
29  if (event == Ion::Events::OK || event == Ion::Events::EXE) {
30  return selectAddedCell(selectedRow());
31  }
32  return false;
33  }
34  return MathToolbox::handleEventForRow(event, mathToolboxIndex(selectedRow()));
35 }
36 
38  if (stackDepth() == 0) {
39  return MathToolbox::numberOfRows()+m_numberOfAddedCells;
40  }
42 }
43 
45  assert(type < 3);
46  assert(index >= 0);
48  if (type == 2) {
49  return &m_addedCells[index];
50  }
51  return MathToolbox::reusableCell(index, type);
52 }
53 
55  if (typeAtLocation(0, index) != 2) {
56  MathToolbox::willDisplayCellForIndex(cell, mathToolboxIndex(index));
57  }
58 }
59 
61  if (stackDepth() == 0 && j < m_numberOfAddedCells) {
62  return 2;
63  }
64  return MathToolbox::typeAtLocation(i,mathToolboxIndex(j));
65 }
66 
67 void SequenceToolbox::setExtraCells(const char * sequenceName, int recurrenceDepth) {
68  for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
69  if (m_addedCellLayout[i]) {
70  delete m_addedCellLayout[i];
71  m_addedCellLayout[i] = nullptr;
72  }
73  }
74  /* If recurrenceDepth < 0, the user is setting the initial conditions so we
75  * do not want to add any cell in the toolbox. */
76  if (recurrenceDepth < 0) {
77  m_numberOfAddedCells = 0;
78  return;
79  }
80  /* The cells added reprensent the sequence at smaller ranks than its depth
81  * and the other sequence at ranks smaller or equal to the depth, ie:
82  * if the sequence is u(n+1), we add cells u(n), v(n), v(n+1).
83  * There is a special case for double recurrent sequences because we do not
84  * want to parse symbols u(n+2) or v(n+2). */
85  m_numberOfAddedCells = recurrenceDepth == 2 ? 2*recurrenceDepth : 2*recurrenceDepth+1;
86  int sequenceIndex = sequenceName == SequenceStore::k_sequenceNames[0] ? 0 : 1;
87  const char * otherSequenceName = SequenceStore::k_sequenceNames[1-sequenceIndex];
88  for (int j = 0; j < recurrenceDepth; j++) {
89  const char * indice = j == 0 ? "n" : "n+1";
90  m_addedCellLayout[j] = new BaselineRelativeLayout(new StringLayout(sequenceName, 1, KDText::FontSize::Large), new StringLayout(indice, strlen(indice), KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
91  m_addedCellLayout[j+recurrenceDepth] = new BaselineRelativeLayout(new StringLayout(otherSequenceName, 1, KDText::FontSize::Large), new StringLayout(indice, strlen(indice), KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
92  }
93  if (recurrenceDepth < 2) {
94  const char * indice = recurrenceDepth == 0 ? "n" : (recurrenceDepth == 1 ? "n+1" : "n+2");
95  m_addedCellLayout[2*recurrenceDepth] = new BaselineRelativeLayout(new StringLayout(otherSequenceName, 1, KDText::FontSize::Large), new StringLayout(indice, strlen(indice), KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
96  }
97  for (int index = 0; index < k_maxNumberOfDisplayedRows; index++) {
98  m_addedCells[index].setExpression(m_addedCellLayout[index]);
99  }
100 }
101 
102 bool SequenceToolbox::selectAddedCell(int selectedRow){
103  char buffer[10];
104  BaselineRelativeLayout * layout = (BaselineRelativeLayout *)m_addedCellLayout[selectedRow];
105  StringLayout * nameLayout = (StringLayout *)layout->baseLayout();
106  StringLayout * subscriptLayout = (StringLayout *)layout->indiceLayout();
107  int currentChar = 0;
108  strlcpy(buffer, nameLayout->text(), strlen(nameLayout->text())+1);
109  currentChar += strlen(nameLayout->text());
110  buffer[currentChar++] = '(';
111  strlcpy(buffer+currentChar, subscriptLayout->text(), strlen(subscriptLayout->text())+1);
112  currentChar += strlen(subscriptLayout->text());
113  buffer[currentChar++] = ')';
114  buffer[currentChar] = 0;
115  sender()->handleEventWithText(buffer);
117  return true;
118 }
119 
120 int SequenceToolbox::mathToolboxIndex(int index) {
121  int indexMathToolbox = index;
122  if (stackDepth() == 0) {
123  indexMathToolbox = index - m_numberOfAddedCells;
124  }
125  return indexMathToolbox;
126 }
127 
128 }
bool handleEventForRow(Ion::Events::Event event, int selectedRow)
Definition: toolbox.cpp:191
#define assert(e)
Definition: assert.h:9
int numberOfRows() override
Definition: toolbox.cpp:128
int typeAtLocation(int i, int j) override
Definition: toolbox.cpp:179
constexpr Event EXE
Definition: events.h:114
void willDisplayCellForIndex(HighlightCell *cell, int index) override
Definition: toolbox.cpp:149
size_t strlcpy(char *dst, const char *src, size_t len)
Definition: strlcpy.c:3
void setExtraCells(const char *sequenceName, int recurrenceDepth)
int typeAtLocation(int i, int j) override
static constexpr int k_maxNumberOfDisplayedRows
Definition: math_toolbox.h:18
size_t strlen(const char *s)
Definition: strlen.c:3
bool handleEvent(Ion::Events::Event event) override
void setExpression(Poincare::ExpressionLayout *expressionLayout)
int stackDepth()
Definition: toolbox.cpp:187
TextField * sender() override
bool handleEventWithText(const char *text, bool indenting=false) override
Definition: text_field.cpp:303
Definition: app.cpp:7
HighlightCell * reusableCell(int index, int type) override
Definition: toolbox.cpp:135
HighlightCell * reusableCell(int index, int type) override
static constexpr const char * k_sequenceNames[MaxNumberOfSequences]
App * app()
Definition: responder.cpp:77
void dismissModalViewController()
Definition: app.cpp:93
constexpr Event OK
Definition: events.h:65
void willDisplayCellForIndex(HighlightCell *cell, int index) override