mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-22 07:10:40 +01:00
[poincare] EditableStringLayout.
Change-Id: Ie985874c216881a722d9c6807c007bc17b25d4f0
This commit is contained in:
@@ -87,6 +87,7 @@ objs += $(addprefix poincare/src/layout/,\
|
||||
bracket_layout.o\
|
||||
condensed_sum_layout.o\
|
||||
conjugate_layout.o\
|
||||
editable_string_layout.o\
|
||||
expression_layout.o\
|
||||
fraction_layout.o\
|
||||
grid_layout.o\
|
||||
|
||||
50
poincare/src/layout/editable_string_layout.cpp
Normal file
50
poincare/src/layout/editable_string_layout.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "editable_string_layout.h"
|
||||
#include <poincare/expression_layout_cursor.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
bool EditableStringLayout::moveLeft(ExpressionLayoutCursor * cursor) {
|
||||
assert(cursor->pointedExpressionLayout() == this);
|
||||
// Case: Right.
|
||||
// Go before the last char.
|
||||
if (cursor->position() == ExpressionLayoutCursor::Position::Right) {
|
||||
size_t stringLength = strlen(m_string);
|
||||
if (stringLength > 1) {
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Inside);
|
||||
cursor->setPositionInside(stringLength - 1);
|
||||
return true;
|
||||
}
|
||||
if (stringLength == 1) {
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
|
||||
return true;
|
||||
}
|
||||
assert(stringLength == 0);
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
|
||||
if (m_parent) {
|
||||
return m_parent->moveLeft(cursor);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// Case: Inside.
|
||||
// Go one char left.
|
||||
if (cursor->position() == ExpressionLayoutCursor::Position::Inside) {
|
||||
int cursorIndex = cursor->positionInside();
|
||||
assert(cursorIndex > 0 && cursorIndex < strlen(m_string));
|
||||
if (cursorIndex == 1) {
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
|
||||
return true;
|
||||
}
|
||||
cursor->setPositionInside(cursorIndex - 1);
|
||||
return true;
|
||||
}
|
||||
// Case: Left.
|
||||
// Ask the parent.
|
||||
if (m_parent) {
|
||||
return m_parent->moveLeft(cursor);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
17
poincare/src/layout/editable_string_layout.h
Normal file
17
poincare/src/layout/editable_string_layout.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef POINCARE_EDITABLE_STRING_LAYOUT_H
|
||||
#define POINCARE_EDITABLE_STRING_LAYOUT_H
|
||||
|
||||
#include "string_layout.h"
|
||||
#include <string.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
class EditableStringLayout : public StringLayout {
|
||||
public:
|
||||
using StringLayout::StringLayout;
|
||||
bool moveLeft(ExpressionLayoutCursor * cursor) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -25,37 +25,13 @@ char * StringLayout::text() {
|
||||
}
|
||||
|
||||
bool StringLayout::moveLeft(ExpressionLayoutCursor * cursor) {
|
||||
assert(cursor->pointedExpressionLayout() == this);
|
||||
// A StringLayout is not editable, and the cursor cannot go inside it.
|
||||
assert(cursor->pointedExpressionLayout() == this);
|
||||
// Case: Right.
|
||||
// Go before the last char.
|
||||
// Go Left.
|
||||
if (cursor->position() == ExpressionLayoutCursor::Position::Right) {
|
||||
size_t stringLength = strlen(m_string);
|
||||
if (stringLength > 1) {
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Inside);
|
||||
cursor->setPositionInside(stringLength - 1);
|
||||
return true;
|
||||
}
|
||||
if (stringLength == 1) {
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
|
||||
return true;
|
||||
}
|
||||
assert(stringLength == 0);
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
|
||||
if (m_parent) {
|
||||
return m_parent->moveLeft(cursor);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// Case: Inside.
|
||||
// Go one char left.
|
||||
if (cursor->position() == ExpressionLayoutCursor::Position::Inside) {
|
||||
int cursorIndex = cursor->positionInside();
|
||||
assert(cursorIndex > 0 && cursorIndex < strlen(m_string));
|
||||
if (cursorIndex == 1) {
|
||||
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
|
||||
return true;
|
||||
}
|
||||
cursor->setPositionInside(cursorIndex - 1);
|
||||
return true;
|
||||
}
|
||||
// Case: Left.
|
||||
|
||||
@@ -24,8 +24,8 @@ protected:
|
||||
KDSize computeSize() override;
|
||||
ExpressionLayout * child(uint16_t index) override;
|
||||
KDPoint positionOfChild(ExpressionLayout * child) override;
|
||||
private:
|
||||
char * m_string;
|
||||
private:
|
||||
KDText::FontSize m_fontSize;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user