From b3598e17134df04f3d21f16676c6bf55ed19f5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 19 Dec 2017 15:36:47 +0100 Subject: [PATCH] [expression_editor] Insert text char by char, using CharLayout. Change-Id: Id5b146bf875c1cf1d9ac949258d43a5ed415a334 --- apps/expression_editor/controller.cpp | 20 ++++++++++++++++++- apps/expression_editor/controller.h | 1 + poincare/include/poincare/expression_layout.h | 2 +- poincare/src/layout/expression_layout.cpp | 7 +------ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/apps/expression_editor/controller.cpp b/apps/expression_editor/controller.cpp index 4053ad77c..f82229f4e 100644 --- a/apps/expression_editor/controller.cpp +++ b/apps/expression_editor/controller.cpp @@ -1,4 +1,5 @@ #include "controller.h" +#include //TODO move from there. namespace ExpressionEditor { @@ -34,7 +35,8 @@ bool Controller::handleEvent(Ion::Events::Event event) { { returnValue = true; } - else if (event.hasText() && m_expressionLayout->insertLayoutForTextAtCursor(event.text(), &m_cursor)) { + else if (event.hasText()) { + insertTextAtCursor(event.text()); returnValue = true; m_expressionLayout->invalidAllSizesAndPositions(); m_view.layoutSubviews(); @@ -43,4 +45,20 @@ bool Controller::handleEvent(Ion::Events::Event event) { return returnValue; } +void Controller::insertTextAtCursor(const char * text) { + int textLength = strlen(text); + if (textLength <= 0) { + return; + } + Poincare::CharLayout * newChild = nullptr; + for (int i = 0; i < textLength; i++) { + newChild = new Poincare::CharLayout(text[i]); + m_cursor.pointedExpressionLayout()->addBrother(&m_cursor, newChild); + } + assert(newChild != nullptr); + m_cursor.setPointedExpressionLayout(newChild); + m_cursor.setPosition(Poincare::ExpressionLayoutCursor::Position::Right); + m_cursor.setPositionInside(0); +} + } diff --git a/apps/expression_editor/controller.h b/apps/expression_editor/controller.h index d827c35d0..f95e5513b 100644 --- a/apps/expression_editor/controller.h +++ b/apps/expression_editor/controller.h @@ -18,6 +18,7 @@ public: void didBecomeFirstResponder() override; bool handleEvent(Ion::Events::Event event) override; private: + void insertTextAtCursor(const char * text); ExpressionEditorView m_view; Poincare::ExpressionLayout * m_expressionLayout; Poincare::ExpressionLayoutCursor m_cursor; diff --git a/poincare/include/poincare/expression_layout.h b/poincare/include/poincare/expression_layout.h index 91a04f8cc..a9c824f1d 100644 --- a/poincare/include/poincare/expression_layout.h +++ b/poincare/include/poincare/expression_layout.h @@ -45,7 +45,7 @@ public: ExpressionLayout * editableParent() { return m_parent; } bool hasAncestor(const ExpressionLayout * e) const; - bool insertLayoutForTextAtCursor(const char * text, ExpressionLayoutCursor * cursor); + bool insertLayoutAtCursor(ExpressionLayout * newChild, ExpressionLayoutCursor * cursor); void addBrother(ExpressionLayoutCursor * cursor, ExpressionLayout * brother); ExpressionLayout * replaceWith(ExpressionLayout * newChild, bool deleteAfterReplace = true); diff --git a/poincare/src/layout/expression_layout.cpp b/poincare/src/layout/expression_layout.cpp index 5868bce26..f7b52bcf9 100644 --- a/poincare/src/layout/expression_layout.cpp +++ b/poincare/src/layout/expression_layout.cpp @@ -120,15 +120,10 @@ void ExpressionLayout::addBrother(ExpressionLayoutCursor * cursor, ExpressionLay return; } replaceWithJuxtapositionOf(this, brother, false); - //TODO Inside position } -bool ExpressionLayout::insertLayoutForTextAtCursor(const char * text, ExpressionLayoutCursor * cursor) { - EditableStringLayout * newChild = new EditableStringLayout(text, strlen(text)); +bool ExpressionLayout::insertLayoutAtCursor(ExpressionLayout * newChild, ExpressionLayoutCursor * cursor) { cursor->pointedExpressionLayout()->addBrother(cursor, newChild); - cursor->setPointedExpressionLayout(newChild); - cursor->setPosition(ExpressionLayoutCursor::Position::Right); - cursor->setPositionInside(0); return true; }