[apps/math_toolbox] Handle const char * when selecting a leaf.

Else, there would be a problem with sequence_toolbox, where not all
toolbox leaves are message trees.

Change-Id: Ib4968aad37f6a835b1ea4d77efac2ae1bd19c7ce
This commit is contained in:
Léa Saviot
2018-01-19 16:24:05 +01:00
parent 2cb8956665
commit f3e6b10410
6 changed files with 31 additions and 30 deletions

View File

@@ -47,7 +47,7 @@ App::App(Container * container, Snapshot * snapshot) :
// insertText() also moves the cursor. We need to re-move it to the
// position we want (which is after the first parenthesis or before the
// first point).
int deltaCursorLocation = - textArea->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommand(text);
int deltaCursorLocation = - textArea->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommandText(text);
// WARNING: This is a dirty and only works because the cursor location we
// want is always on the first line of the text we insert. Because of the
// auto indentation, it would be difficult to compute the wanted cursor
@@ -59,7 +59,7 @@ App::App(Container * container, Snapshot * snapshot) :
if (!textField->isEditing()) {
textField->setEditing(true);
}
int newCursorLocation = textField->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommand(text);
int newCursorLocation = textField->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommandText(text);
if (textField->insertTextAtLocation(text, textField->cursorLocation())) {
textField->setCursorLocation(newCursorLocation);
}}),

View File

@@ -128,13 +128,13 @@ void VariableBoxController::ContentViewController::insertTextInCaller(const char
}
int previousCursorLocation = m_textFieldCaller->cursorLocation();
m_textFieldCaller->insertTextAtLocation(commandBuffer, previousCursorLocation);
m_textFieldCaller->setCursorLocation(previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommand(commandBuffer));
m_textFieldCaller->setCursorLocation(previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommandText(commandBuffer));
return;
}
if (m_textAreaCaller != nullptr) {
int previousCursorLocation = m_textAreaCaller->cursorLocation();
m_textAreaCaller->insertText(commandBuffer);
int deltaCursorLocation = - m_textAreaCaller->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommand(commandBuffer);
int deltaCursorLocation = - m_textAreaCaller->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommandText(commandBuffer);
m_textAreaCaller->moveCursor(deltaCursorLocation);
}
}

View File

@@ -115,15 +115,14 @@ void MathToolbox::setSenderAndAction(Responder * sender, Action action) {
m_action = action;
}
void MathToolbox::actionForEditableExpressionView(void * sender, ToolboxMessageTree * messageTree) {
void MathToolbox::actionForEditableExpressionView(void * sender, const char * text) {
EditableExpressionView * expressionLayoutEditorSender = static_cast<EditableExpressionView *>(sender);
// Translate the message and replace the arguments with Empty chars.
const char * textToInsert = I18n::translate(messageTree->insertedText());
int strippedTextToInsertMaxLength = strlen(textToInsert);
char strippedTextToInsert[strlen(textToInsert)];
Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandMessage(messageTree->insertedText(), strippedTextToInsert, strippedTextToInsertMaxLength);
// 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(strippedTextToInsert);
Expression * resultExpression = Expression::parse(textToInsert);
if (resultExpression == nullptr) {
return;
}
@@ -146,25 +145,24 @@ void MathToolbox::actionForEditableExpressionView(void * sender, ToolboxMessageT
expressionLayoutEditorSender->insertLayoutAtCursor(resultLayout, pointedLayout);
}
void MathToolbox::actionForTextField(void * sender, ToolboxMessageTree * messageTree) {
void MathToolbox::actionForTextField(void * sender, const char * text) {
TextField * textFieldSender = static_cast<TextField *>(sender);
if (!textFieldSender->isEditing()) {
textFieldSender->setEditing(true);
}
const char * textToInsert = I18n::translate(messageTree->insertedText());
int textToInsertLength = strlen(textToInsert);
char strippedTextToInsert[textToInsertLength];
int maxTextToInsertLength = strlen(text);
char textToInsert[maxTextToInsertLength];
// Translate the message and remove the arguments.
Shared::ToolboxHelpers::TextToInsertForCommandMessage(messageTree->insertedText(), strippedTextToInsert, textToInsertLength);
textFieldSender->insertTextAtLocation(strippedTextToInsert, textFieldSender->cursorLocation());
int newCursorLocation = textFieldSender->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommand(strippedTextToInsert);
Shared::ToolboxHelpers::TextToInsertForCommandText(text, textToInsert, maxTextToInsertLength);
textFieldSender->insertTextAtLocation(textToInsert, textFieldSender->cursorLocation());
int newCursorLocation = textFieldSender->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommandText(text);
textFieldSender->setCursorLocation(newCursorLocation);
}
bool MathToolbox::selectLeaf(ToolboxMessageTree * selectedMessageTree) {
ToolboxMessageTree * messageTree = selectedMessageTree;
m_selectableTableView.deselectTable();
m_action(sender(), messageTree);
m_action(sender(), I18n::translate(messageTree->insertedText()));
app()->dismissModalViewController();
return true;
}

View File

@@ -8,11 +8,11 @@
class MathToolbox : public Toolbox {
public:
typedef void (*Action)(void * sender, ToolboxMessageTree * messageTree);
typedef void (*Action)(void * sender, const char * text);
MathToolbox();
void setSenderAndAction(Responder * sender, Action action);
static void actionForEditableExpressionView(void * sender, ToolboxMessageTree * messageTree);
static void actionForTextField(void * sender, ToolboxMessageTree * messageTree);
static void actionForEditableExpressionView(void * sender, const char * text);
static void actionForTextField(void * sender, const char * text);
protected:
bool selectLeaf(ToolboxMessageTree * selectedMessageTree) override;
const ToolboxMessageTree * rootModel() override;

View File

@@ -7,7 +7,7 @@
namespace Shared {
namespace ToolboxHelpers {
int CursorIndexInCommand(const char * text) {
int CursorIndexInCommandText(const char * text) {
for (size_t i = 0; i < strlen(text); i++) {
if (text[i] == '(' || text[i] == '\'') {
return i + 1;
@@ -20,8 +20,7 @@ int CursorIndexInCommand(const char * text) {
}
void TextToInsertForCommandMessage(I18n::Message message, char * buffer, int bufferSize) {
const char * messageText = I18n::translate(message);
TextToInsertForCommandText(messageText, buffer, bufferSize);
TextToInsertForCommandText(I18n::translate(message), buffer, bufferSize);
}
void TextToInsertForCommandText(const char * command, char * buffer, int bufferSize) {
@@ -58,7 +57,11 @@ void TextToInsertForCommandText(const char * command, char * buffer, int bufferS
}
void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer, int bufferSize) {
if (message == I18n::Message::MatrixCommandWithArg) {
TextToParseIntoLayoutForCommandText(I18n::translate(message), buffer, bufferSize);
}
void TextToParseIntoLayoutForCommandText(const char * command, char * buffer, int bufferSize) {
if (command == I18n::translate(I18n::Message::MatrixCommandWithArg)) {
assert(bufferSize >= 6);
// Handle a new matrix command.
buffer[0] = '[';
@@ -69,8 +72,7 @@ void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer
buffer[5] = 0;
return;
}
const char * messageText = I18n::translate(message);
TextToInsertForCommandText(messageText, buffer, bufferSize);
TextToInsertForCommandText(command, buffer, bufferSize);
size_t bufferLength = strlen(buffer);
for (size_t i = 0; i < bufferLength; i++) {
if (buffer[i] == '(' || buffer[i] == '[' || buffer[i] == ',') {

View File

@@ -6,8 +6,8 @@
namespace Shared {
namespace ToolboxHelpers {
int CursorIndexInCommand(const char * text);
/* Returns the index of the cursor position in a Command, which is the smallest
int CursorIndexInCommandText(const char * text);
/* Returns the index of the cursor position in a command, which is the smallest
* index between :
* - After the first open parenthesis
* - The end of the text */
@@ -18,6 +18,7 @@ void TextToInsertForCommandText(const char * command, char * buffer, int bufferS
/* Removes the arguments from a command:
* - Removes text between parentheses or brackets, except commas */
void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer, int bufferSize);
void TextToParseIntoLayoutForCommandText(const char * command, char * buffer, int bufferSize);
/* Removes the arguments from a command and replaces them with empty chars. */
}