From 4ed7e06373a87ab26724fba2e38504ef401adc22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 14 Nov 2016 17:47:46 +0100 Subject: [PATCH] [apps][escher] Fix insering several letters when clicking on SIN e. g. Change-Id: If71400b22f65878a94090fed7f3c68433a10506c --- apps/graph/list/list_controller.cpp | 21 ++++++++++++------- apps/graph/list/list_controller.h | 2 +- apps/graph/values/values_controller.cpp | 19 +++++++++-------- apps/graph/values/values_controller.h | 2 +- .../values/values_parameter_controller.cpp | 18 +++++++++------- .../values/values_parameter_controller.h | 2 +- escher/include/escher/input_view_controller.h | 2 +- escher/src/input_view_controller.cpp | 3 ++- escher/src/text_field.cpp | 12 +++-------- 9 files changed, 42 insertions(+), 39 deletions(-) diff --git a/apps/graph/list/list_controller.cpp b/apps/graph/list/list_controller.cpp index 48fc067ca..9c9d9d056 100644 --- a/apps/graph/list/list_controller.cpp +++ b/apps/graph/list/list_controller.cpp @@ -111,17 +111,19 @@ void ListController::configureFunction(Function * function) { stack->push(&m_parameterController); } -void ListController::editExpression(FunctionExpressionView * functionCell, bool overwrite, char initialDigit) { +void ListController::editExpression(FunctionExpressionView * functionCell, const char * initialText) { char initialTextContent[255]; - if (overwrite) { - initialTextContent[0] = initialDigit; - initialTextContent[1] = 0; + int cursorDelta = 0; + if (initialText) { + strlcpy(initialTextContent, initialText, sizeof(initialTextContent)); + cursorDelta = strlen(initialText) > 1 ? -1 : 0; } else { strlcpy(initialTextContent, functionCell->function()->text(), sizeof(initialTextContent)); } + int cursorLocation = strlen(initialTextContent) + cursorDelta; App * myApp = (App *)app(); InputViewController * inputController = myApp->inputViewController(); - inputController->edit(this, initialTextContent, functionCell, + inputController->edit(this, initialTextContent, cursorLocation, functionCell, [](void * context, void * sender){ FunctionExpressionView * myCell = (FunctionExpressionView *) context; Function * myFunction = myCell->function(); @@ -144,12 +146,15 @@ bool ListController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::OK) { return handleEnter(); } + if (event == Ion::Events::XNT && m_selectableTableView.selectedColumn() == 1){ + FunctionExpressionView * functionCell = (FunctionExpressionView *)(m_selectableTableView.cellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow())); + editExpression(functionCell, "x"); + } if (!event.hasText() || m_selectableTableView.selectedColumn() == 0) { return false; } FunctionExpressionView * functionCell = (FunctionExpressionView *)(m_selectableTableView.cellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow())); - // FIXME: Only first character handled! - editExpression(functionCell, true, event.text()[0]); + editExpression(functionCell, event.text()); return true; } @@ -175,7 +180,7 @@ bool ListController::handleEnter() { return false; } FunctionExpressionView * functionCell = (FunctionExpressionView *)(m_selectableTableView.cellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow())); - editExpression(functionCell, false); + editExpression(functionCell); return true; } default: diff --git a/apps/graph/list/list_controller.h b/apps/graph/list/list_controller.h index 546f0f68f..a4c50bb88 100644 --- a/apps/graph/list/list_controller.h +++ b/apps/graph/list/list_controller.h @@ -31,7 +31,7 @@ public: int reusableCellCount(int type) override; int typeAtLocation(int i, int j) override; void configureFunction(Function * function); - void editExpression(FunctionExpressionView * functionCell, bool overwrite, char initialDigit = 0); + void editExpression(FunctionExpressionView * functionCell, const char * initialText = nullptr); private: static constexpr KDCoordinate k_verticalFunctionMargin = 50-12; diff --git a/apps/graph/values/values_controller.cpp b/apps/graph/values/values_controller.cpp index 9b2a062c9..d50b8ad51 100644 --- a/apps/graph/values/values_controller.cpp +++ b/apps/graph/values/values_controller.cpp @@ -230,7 +230,7 @@ bool ValuesController::handleEvent(Ion::Events::Event event) { return true; } if (activeColumn() == 0) { - editValue(false); + editValue(); return true; } return false; @@ -241,8 +241,7 @@ bool ValuesController::handleEvent(Ion::Events::Event event) { } if (event.hasText()) { if (activeColumn() == 0 && activeRow() > 0) { - // FIXME: Only first character! - editValue(true, event.text()[0]); + editValue(event.text()); return true; } return false; @@ -270,14 +269,15 @@ void ValuesController::configureDerivativeFunction() { stack->push(&m_derivativeParameterController); } -void ValuesController::editValue(bool overwrite, char initialDigit) { +void ValuesController::editValue(const char * initialText) { /* This code assumes that the active cell remains the one which is edited * until the invocation is performed. This could lead to concurrency issue in * other cases. */ - char initialTextContent[Constant::FloatBufferSizeInScientificMode]; - if (overwrite) { - initialTextContent[0] = initialDigit; - initialTextContent[1] = 0; + char initialTextContent[255]; + int cursorDelta = 0; + if (initialText) { + strlcpy(initialTextContent, initialText, sizeof(initialTextContent)); + cursorDelta = strlen(initialText) > 1 ? -1 : 0; } else { if (activeRow() > m_interval.numberOfElements()) { initialTextContent[0] = 0; @@ -285,9 +285,10 @@ void ValuesController::editValue(bool overwrite, char initialDigit) { Float(m_interval.element(activeRow()-1)).convertFloatToText(initialTextContent, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); } } + int cursorLocation = strlen(initialTextContent) + cursorDelta; App * myApp = (App *)app(); InputViewController * inputController = myApp->inputViewController(); - inputController->edit(this, initialTextContent, this, + inputController->edit(this, initialTextContent, cursorLocation, this, [](void * context, void * sender){ ValuesController * valuesController = (ValuesController *)context; int activeRow = valuesController->activeRow(); diff --git a/apps/graph/values/values_controller.h b/apps/graph/values/values_controller.h index 79760e5a6..69633069d 100644 --- a/apps/graph/values/values_controller.h +++ b/apps/graph/values/values_controller.h @@ -22,7 +22,7 @@ public: int activeColumn(); Interval * interval(); ValueCell * abscisseCellAtRow(int rowIndex); - void editValue(bool overwrite, char initialDigit = 0); + void editValue(const char * initialText = nullptr); const char * title() const override; bool handleEvent(Ion::Events::Event event) override; diff --git a/apps/graph/values/values_parameter_controller.cpp b/apps/graph/values/values_parameter_controller.cpp index 62d2b23e6..0929e6687 100644 --- a/apps/graph/values/values_parameter_controller.cpp +++ b/apps/graph/values/values_parameter_controller.cpp @@ -78,31 +78,33 @@ void ValuesParameterController::setIntervalParameterAtIndex(int parameterIndex, bool ValuesParameterController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::OK) { - editInterval(false); + editInterval(); return true; } if (event.hasText()) { - editInterval(true, event.text()[0]); // FIXME: only first char + editInterval(event.text()); return true; } return false; } -void ValuesParameterController::editInterval(bool overwrite, char initialDigit) { +void ValuesParameterController::editInterval(const char * initialText) { /* This code assumes that the active cell remains the one which is edited * until the invocation is performed. This could lead to concurrency issue in * other cases. */ - char initialTextContent[Constant::FloatBufferSizeInScientificMode]; - if (overwrite) { - initialTextContent[0] = initialDigit; - initialTextContent[1] = 0; + char initialTextContent[255]; + int cursorDelta = 0; + if (initialText) { + strlcpy(initialTextContent, initialText, sizeof(initialTextContent)); + cursorDelta = strlen(initialText) > 1 ? -1 : 0; } else { TextMenuListCell * textMenuListCell = (TextMenuListCell *)reusableCell(activeCell()); strlcpy(initialTextContent, textMenuListCell->accessoryText(), sizeof(initialTextContent)); } + int cursorLocation = strlen(initialTextContent) + cursorDelta; App * myApp = (App *)app(); InputViewController * inputController = myApp->inputViewController(); - inputController->edit(this, initialTextContent, this, + inputController->edit(this, initialTextContent, cursorLocation, this, [](void * context, void * sender){ ValuesParameterController * valuesParameterController = (ValuesParameterController *)context; int activeCell = valuesParameterController->activeCell(); diff --git a/apps/graph/values/values_parameter_controller.h b/apps/graph/values/values_parameter_controller.h index 2aa5c8f70..c0e8e2e08 100644 --- a/apps/graph/values/values_parameter_controller.h +++ b/apps/graph/values/values_parameter_controller.h @@ -10,7 +10,7 @@ public: ValuesParameterController(Responder * parentResponder, Interval * interval); Interval * interval(); int activeCell(); - void editInterval(bool overwrite, char initialDigit = 0); + void editInterval(const char * initialText = nullptr); void setIntervalParameterAtIndex(int parameterIndex, float f); View * view() override; diff --git a/escher/include/escher/input_view_controller.h b/escher/include/escher/input_view_controller.h index b624ef3c0..63f944cc3 100644 --- a/escher/include/escher/input_view_controller.h +++ b/escher/include/escher/input_view_controller.h @@ -11,7 +11,7 @@ public: InputViewController(Responder * parentResponder, ViewController * child, TextFieldDelegate * textFieldDelegate = nullptr); const char * title() const override; bool handleEvent(Ion::Events::Event event) override; - void edit(Responder * caller, const char * initialContent, void * context, Invocation::Action successAction, Invocation::Action failureAction); + void edit(Responder * caller, const char * initialContent, int cursorPosition, void * context, Invocation::Action successAction, Invocation::Action failureAction); const char * textBody(); private: class TextFieldController : public ViewController { diff --git a/escher/src/input_view_controller.cpp b/escher/src/input_view_controller.cpp index 12d8bca1d..0b81670db 100644 --- a/escher/src/input_view_controller.cpp +++ b/escher/src/input_view_controller.cpp @@ -62,9 +62,10 @@ bool InputViewController::handleEvent(Ion::Events::Event event) { return false; } -void InputViewController::edit(Responder * caller, const char * initialContent, void * context, Invocation::Action successAction, Invocation::Action failureAction) { +void InputViewController::edit(Responder * caller, const char * initialContent, int cursorPosition, void * context, Invocation::Action successAction, Invocation::Action failureAction) { m_successAction = Invocation(successAction, context); m_failureAction = Invocation(failureAction, context); setTextBody(initialContent); + m_textFieldController.textField()->setCursorLocation(cursorPosition); showInput(); } diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index 18b9b4ce3..cf8ab531c 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -64,15 +64,9 @@ bool TextField::handleEvent(Ion::Events::Event event) { return true; } if (event.hasText()) { - // FIXME: Only inserting the first letter! - if (m_currentTextLength == 0 || m_currentTextLength-1 < m_textBufferSize) { - for (int k = m_currentTextLength; k > m_currentCursorLocation; k--) { - m_textBuffer[k] = m_textBuffer[k-1]; - } - m_textBuffer[++m_currentTextLength] = 0; - m_textBuffer[m_currentCursorLocation++] = event.text()[0]; - reload(); - } + insertTextAtLocation(event.text(), cursorLocation()); + int cursorDelta = strlen(event.text()) > 1 ? -1 : 0; + setCursorLocation(cursorLocation() + strlen(event.text()) + cursorDelta); return true; } return false;