Files
Upsilon/apps/sequence/sequence_toolbox.cpp
Émilie Feral f2512b2968 [apps/sequence] Temporary implementation of sequence toolbox
Change-Id: I59c703c4c50cd523d49af9a558a7fc3b0f360bc6
2017-02-20 10:51:58 +01:00

94 lines
3.0 KiB
C++

#include "sequence_toolbox.h"
#include "../../poincare/src/layout/baseline_relative_layout.h"
#include "../../poincare/src/layout/string_layout.h"
#include <assert.h>
using namespace Poincare;
namespace Sequence {
SequenceToolbox::SequenceToolbox() :
MathToolbox(),
m_numberOfAddedCells(0)
{
}
bool SequenceToolbox::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::OK && stackDepth() == 0) {
int selectedRow = m_selectableTableView.selectedRow();
if (selectedRow < m_numberOfAddedCells) {
return selectAddedCell(selectedRow);
}
}
return MathToolbox::handleEvent(event);
}
int SequenceToolbox::numberOfRows() {
if (stackDepth() == 0) {
return MathToolbox::numberOfRows()+m_numberOfAddedCells;
}
return MathToolbox::numberOfRows();
}
TableViewCell * SequenceToolbox::reusableCell(int index, int type) {
assert(type < 3);
assert(index >= 0);
assert(index < k_maxNumberOfDisplayedRows);
if (type == 2) {
return &m_addedCells[index];
}
return MathToolbox::reusableCell(index, type);
}
void SequenceToolbox::willDisplayCellForIndex(TableViewCell * cell, int index) {
if (typeAtLocation(0, index) == 2) {
ExpressionMenuListCell * myCell = (ExpressionMenuListCell *)cell;
myCell->setExpression(m_addedCellLayout[index]);
return;
} else {
MathToolbox::willDisplayCellForIndex(cell, index);
}
}
KDCoordinate SequenceToolbox::rowHeight(int j) {
if (typeAtLocation(0, j) == 2) {
return k_addedRowHeight;
}
return MathToolbox::rowHeight(j);
}
int SequenceToolbox::typeAtLocation(int i, int j) {
if (stackDepth() == 0 && j < m_numberOfAddedCells) {
return 2;
}
return MathToolbox::typeAtLocation(i,j);
}
void SequenceToolbox::addCells(int numberOfAddedCells, char ** cellNames, char ** cellSubscript) {
m_numberOfAddedCells = numberOfAddedCells;
for (int i = 0; i < numberOfAddedCells; i++) {
m_addedCellLayout[i] = new BaselineRelativeLayout(new StringLayout(cellNames[i], 1, KDText::FontSize::Large), new StringLayout(cellSubscript[i], strlen(cellSubscript[i]), KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
}
}
bool SequenceToolbox::selectAddedCell(int selectedRow){
char buffer[10];
BaselineRelativeLayout * layout = (BaselineRelativeLayout *)m_addedCellLayout[selectedRow];
StringLayout * nameLayout = (StringLayout *)layout->baseLayout();
StringLayout * subscriptLayout = (StringLayout *)layout->indiceLayout();
int currentChar = 0;
strlcpy(buffer, nameLayout->text(), strlen(nameLayout->text())+1);
currentChar += strlen(nameLayout->text());
buffer[currentChar++] = '(';
strlcpy(buffer+currentChar, subscriptLayout->text(), strlen(subscriptLayout->text())+1);
currentChar += strlen(subscriptLayout->text());
buffer[currentChar++] = ')';
buffer[currentChar] = 0;
sender()->insertTextAtLocation(buffer, sender()->cursorLocation());
sender()->setCursorLocation(sender()->cursorLocation()+currentChar);
app()->dismissModalViewController();
return true;
}
}