diff --git a/apps/toolbox_controller.cpp b/apps/toolbox_controller.cpp index 89a90298a..3f506e1f9 100644 --- a/apps/toolbox_controller.cpp +++ b/apps/toolbox_controller.cpp @@ -58,16 +58,16 @@ bool ToolboxController::selectLeaf(Node * selectedNode){ m_listViewController.deselectTable(); ToolboxNode * node = (ToolboxNode *)selectedNode; const char * editedText = node->label(); - m_textFieldCaller->appendText(editedText); - int cursorPosition = 0; + m_textFieldCaller->insertTextAtLocation(editedText, m_textFieldCaller->cursorLocation()); + int cursorDelta = 0; int editedTextLength = strlen(editedText); for (int i = 0; i < editedTextLength; i++) { if (editedText[i] == '(') { - cursorPosition = i + 1 - editedTextLength; + cursorDelta = i + 1; break; } } - m_textFieldCaller->moveCursor(cursorPosition); + m_textFieldCaller->setCursorLocation(m_textFieldCaller->cursorLocation()+cursorDelta); app()->dismissModalViewController(); return true; } diff --git a/apps/variable_box_controller.cpp b/apps/variable_box_controller.cpp index b61b3c65c..6c59883c4 100644 --- a/apps/variable_box_controller.cpp +++ b/apps/variable_box_controller.cpp @@ -93,7 +93,8 @@ Node * VariableBoxController::nodeModel() { bool VariableBoxController::selectLeaf(Node * selectedNode){ m_listViewController.deselectTable(); const char * editedText = selectedNode->label(); - m_textFieldCaller->appendText(editedText); + m_textFieldCaller->insertTextAtLocation(editedText, m_textFieldCaller->cursorLocation()); + m_textFieldCaller->setCursorLocation(m_textFieldCaller->cursorLocation() + strlen(editedText)); app()->dismissModalViewController(); return true; } diff --git a/escher/include/escher/text_field.h b/escher/include/escher/text_field.h index d1f3e52fa..e7696c8bf 100644 --- a/escher/include/escher/text_field.h +++ b/escher/include/escher/text_field.h @@ -19,8 +19,9 @@ public: /* If the text to be appended is too long to be added without overflowing the * buffer, nothing is done (not even adding few letters from the text to reach * the maximum buffer capacity. */ - void appendText(const char * text); - void moveCursor(int delta); + int cursorLocation() const; + void setCursorLocation(int location); + void insertTextAtLocation(const char * text, int location); KDSize minimalSizeForOptimalDisplay() override; protected: void reload(); @@ -29,7 +30,7 @@ protected: #endif char * m_textBuffer; size_t m_currentTextLength; - size_t m_currentCursorPosition; + size_t m_currentCursorLocation; private: size_t m_textBufferSize; TextFieldDelegate * m_delegate; diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index 98d50fb20..18b9b4ce3 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -6,7 +6,7 @@ TextField::TextField(Responder * parentResponder, char * textBuffer, size_t text Responder(parentResponder), m_textBuffer(textBuffer), m_currentTextLength(0), - m_currentCursorPosition(0), + m_currentCursorLocation(0), m_textBufferSize(textBufferSize), m_delegate(delegate) { @@ -40,23 +40,23 @@ bool TextField::handleEvent(Ion::Events::Event event) { } } if (event == Ion::Events::Left) { - if (m_currentCursorPosition > 0) { - m_currentCursorPosition--; + if (m_currentCursorLocation > 0) { + m_currentCursorLocation--; } return true; } if (event == Ion::Events::Right) { - if (m_currentCursorPosition < m_currentTextLength) { - m_currentCursorPosition++; + if (m_currentCursorLocation < m_currentTextLength) { + m_currentCursorLocation++; } return true; } if (event == Ion::Events::Backspace) { - if (m_currentCursorPosition > 0) { + if (m_currentCursorLocation > 0) { reload(); m_currentTextLength--; - m_currentCursorPosition--; - for (int k = m_currentCursorPosition; k < m_currentTextLength; k ++) { + m_currentCursorLocation--; + for (int k = m_currentCursorLocation; k < m_currentTextLength; k ++) { m_textBuffer[k] = m_textBuffer[k+1]; } m_textBuffer[m_currentTextLength] = 0; @@ -66,11 +66,11 @@ bool TextField::handleEvent(Ion::Events::Event event) { 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_currentCursorPosition; k--) { + for (int k = m_currentTextLength; k > m_currentCursorLocation; k--) { m_textBuffer[k] = m_textBuffer[k-1]; } m_textBuffer[++m_currentTextLength] = 0; - m_textBuffer[m_currentCursorPosition++] = event.text()[0]; + m_textBuffer[m_currentCursorLocation++] = event.text()[0]; reload(); } return true; @@ -89,32 +89,35 @@ int TextField::textLength() const { void TextField::setText(const char * text) { strlcpy(m_textBuffer, text, m_textBufferSize); - m_currentCursorPosition = strlen(text); - m_currentTextLength = m_currentCursorPosition; + m_currentCursorLocation = strlen(text); + m_currentTextLength = m_currentCursorLocation; reload(); } -void TextField::appendText(const char * text) { +int TextField::cursorLocation() const{ + return m_currentCursorLocation; +} + +void TextField::setCursorLocation(int location) { + location = location < 0 ? 0 : location; + location = location > m_currentTextLength ? m_currentTextLength : location; + m_currentCursorLocation = location; +} + +void TextField::insertTextAtLocation(const char * text, int location) { int textSize = strlen(text); if (m_currentTextLength + textSize > m_textBufferSize) { return; } - for (int k = m_currentTextLength; k >= m_currentCursorPosition && k >= 0; k--) { + for (int k = m_currentTextLength; k >= location && k >= 0; k--) { m_textBuffer[k+textSize] = m_textBuffer[k]; } - strlcpy(&m_textBuffer[m_currentCursorPosition], text, textSize); - m_currentCursorPosition += textSize; - m_textBuffer[m_currentCursorPosition-1] = text[textSize-1]; + strlcpy(&m_textBuffer[location], text, textSize); + m_textBuffer[location+textSize-1] = text[textSize-1]; m_currentTextLength += textSize; reload(); } -void TextField::moveCursor(int position) { - assert(m_currentCursorPosition + position <= m_currentTextLength); - assert(m_currentCursorPosition + position >= 0); - m_currentCursorPosition += position; -} - KDSize TextField::minimalSizeForOptimalDisplay() { KDSize textSize = KDText::stringSize(m_textBuffer); return KDSize(0, textSize.height());