diff --git a/apps/math_toolbox.cpp b/apps/math_toolbox.cpp index a91e93daf..a083d5747 100644 --- a/apps/math_toolbox.cpp +++ b/apps/math_toolbox.cpp @@ -115,14 +115,19 @@ void MathToolbox::setSenderAndAction(Responder * sender, Action action) { m_action = action; } -void MathToolbox::actionForEditableExpressionView(void * sender, const char * text) { +void MathToolbox::actionForEditableExpressionView(void * sender, const char * text, bool removeArguments) { EditableExpressionView * expressionLayoutEditorSender = static_cast(sender); - // Replace the arguments with Empty chars. - int textToInsertMaxLength = strlen(text); - char textToInsert[textToInsertMaxLength]; - Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandText(text, textToInsert, textToInsertMaxLength); - // Create the layout - Expression * resultExpression = Expression::parse(textToInsert); + Expression * resultExpression = nullptr; + if (removeArguments) { + // Replace the arguments with Empty chars. + int textToInsertMaxLength = strlen(text); + char textToInsert[textToInsertMaxLength]; + Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandText(text, textToInsert, textToInsertMaxLength); + // Create the layout + resultExpression = Expression::parse(textToInsert); + } else { + resultExpression = Expression::parse(text); + } if (resultExpression == nullptr) { return; } @@ -145,24 +150,30 @@ void MathToolbox::actionForEditableExpressionView(void * sender, const char * te expressionLayoutEditorSender->insertLayoutAtCursor(resultLayout, pointedLayout); } -void MathToolbox::actionForTextField(void * sender, const char * text) { +void MathToolbox::actionForTextField(void * sender, const char * text, bool removeArguments) { TextField * textFieldSender = static_cast(sender); if (!textFieldSender->isEditing()) { textFieldSender->setEditing(true); } - int maxTextToInsertLength = strlen(text); - char textToInsert[maxTextToInsertLength]; - // Translate the message and remove the arguments. - Shared::ToolboxHelpers::TextToInsertForCommandText(text, textToInsert, maxTextToInsertLength); - textFieldSender->insertTextAtLocation(textToInsert, textFieldSender->cursorLocation()); - int newCursorLocation = textFieldSender->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommandText(text); + int newCursorLocation = textFieldSender->cursorLocation(); + if (removeArguments) { + int maxTextToInsertLength = strlen(text); + char textToInsert[maxTextToInsertLength]; + // Translate the message and remove the arguments. + Shared::ToolboxHelpers::TextToInsertForCommandText(text, textToInsert, maxTextToInsertLength); + textFieldSender->insertTextAtLocation(textToInsert, textFieldSender->cursorLocation()); + newCursorLocation+= Shared::ToolboxHelpers::CursorIndexInCommandText(textToInsert); + } else { + textFieldSender->insertTextAtLocation(text, textFieldSender->cursorLocation()); + newCursorLocation+= Shared::ToolboxHelpers::CursorIndexInCommandText(text); + } textFieldSender->setCursorLocation(newCursorLocation); } bool MathToolbox::selectLeaf(ToolboxMessageTree * selectedMessageTree) { ToolboxMessageTree * messageTree = selectedMessageTree; m_selectableTableView.deselectTable(); - m_action(sender(), I18n::translate(messageTree->insertedText())); + m_action(sender(), I18n::translate(messageTree->insertedText()), true); app()->dismissModalViewController(); return true; } diff --git a/apps/math_toolbox.h b/apps/math_toolbox.h index 02be3effa..913706611 100644 --- a/apps/math_toolbox.h +++ b/apps/math_toolbox.h @@ -8,11 +8,11 @@ class MathToolbox : public Toolbox { public: - typedef void (*Action)(void * sender, const char * text); + typedef void (*Action)(void * sender, const char * text, bool removeArguments); MathToolbox(); void setSenderAndAction(Responder * sender, Action action); - static void actionForEditableExpressionView(void * sender, const char * text); - static void actionForTextField(void * sender, const char * text); + static void actionForEditableExpressionView(void * sender, const char * text, bool removeArguments = true); + static void actionForTextField(void * sender, const char * text, bool removeArguments = true); protected: bool selectLeaf(ToolboxMessageTree * selectedMessageTree) override; const ToolboxMessageTree * rootModel() override; diff --git a/apps/sequence/list/sequence_toolbox.cpp b/apps/sequence/list/sequence_toolbox.cpp index f4346311d..3772521c5 100644 --- a/apps/sequence/list/sequence_toolbox.cpp +++ b/apps/sequence/list/sequence_toolbox.cpp @@ -115,7 +115,26 @@ bool SequenceToolbox::selectAddedCell(int selectedRow){ int bufferSize = 10; char buffer[bufferSize]; m_addedCellLayout[selectedRow]->writeTextInBuffer(buffer, bufferSize); - m_action(sender(), buffer); + if (m_action == MathToolbox::actionForTextField) { + // DIRTY. The symbols are layouted using a Subscript VerticalOffsetLayout, + // which serializes into "_{}", but we want parentheses for text fields. We + // thus need to remove any underscores, and changes brackets into + // parentheses. + for (int i = 0; i < bufferSize; i++) { + if (buffer[i] == '{') { + buffer[i] = '('; + } + if (buffer[i] == '}') { + buffer[i] = ')'; + } + if (buffer[i] == '_') { + memmove(&buffer[i], &buffer[i+1], bufferSize - (i+1) + 1); + bufferSize--; + i--; + } + } + } + m_action(sender(), buffer, false); app()->dismissModalViewController(); return true; }