diff --git a/apps/shared/Makefile b/apps/shared/Makefile index d221c4c4b..240839f4a 100644 --- a/apps/shared/Makefile +++ b/apps/shared/Makefile @@ -76,6 +76,7 @@ app_objs += $(addprefix apps/shared/,\ tab_table_controller.o\ text_field_delegate.o\ text_field_delegate_app.o\ + text_field_with_extension.o\ toolbox_helpers.o\ values_function_parameter_controller.o\ values_parameter_controller.o\ diff --git a/apps/shared/text_field_with_extension.cpp b/apps/shared/text_field_with_extension.cpp new file mode 100644 index 000000000..93a40b764 --- /dev/null +++ b/apps/shared/text_field_with_extension.cpp @@ -0,0 +1,14 @@ +#include "text_field_with_extension.h" + +namespace Shared { + +void TextFieldWithExtension::willSetCursorLocation(int * location) { + size_t textLength = strlen(text()); + assert(textLength >= m_extensionLength); + size_t maxLocation = textLength - m_extensionLength; + if (*location > (int)maxLocation) { + *location = maxLocation; + } +} + +} diff --git a/apps/shared/text_field_with_extension.h b/apps/shared/text_field_with_extension.h new file mode 100644 index 000000000..18a229de0 --- /dev/null +++ b/apps/shared/text_field_with_extension.h @@ -0,0 +1,32 @@ +#ifndef SHARED_TEXT_FIELD_WITH_EXTENSION_H +#define SHARED_TEXT_FIELD_WITH_EXTENSION_H + +#include + +namespace Shared { + +class TextFieldWithExtension : public TextField { +public: + TextFieldWithExtension(size_t extensionLength, + Responder * parentResponder, + char * textBuffer, + char * draftTextBuffer, + size_t textBufferSize, + TextFieldDelegate * delegate = nullptr, + bool hasTwoBuffers = true, + KDText::FontSize size = KDText::FontSize::Large, + float horizontalAlignment = 0.0f, + float verticalAlignment = 0.5f, + KDColor textColor = KDColorBlack, + KDColor backgroundColor = KDColorWhite) : + TextField(parentResponder, textBuffer, draftTextBuffer, textBufferSize, delegate, hasTwoBuffers, size, horizontalAlignment, verticalAlignment, textColor, backgroundColor), + m_extensionLength(extensionLength) + {} +private: + void willSetCursorLocation(int * location) override; + size_t m_extensionLength; +}; + +} + +#endif diff --git a/escher/include/escher/text_input.h b/escher/include/escher/text_input.h index 95d09f8b1..66ba8a36c 100644 --- a/escher/include/escher/text_input.h +++ b/escher/include/escher/text_input.h @@ -55,6 +55,7 @@ protected: virtual const ContentView * nonEditableContentView() const = 0; private: virtual TextInputDelegate * delegate() = 0; + virtual void willSetCursorLocation(int * location) {} }; #endif diff --git a/escher/src/text_input.cpp b/escher/src/text_input.cpp index 21ec672da..c4c237885 100644 --- a/escher/src/text_input.cpp +++ b/escher/src/text_input.cpp @@ -12,8 +12,8 @@ TextInput::ContentView::ContentView(const KDFont * font) : } void TextInput::ContentView::setCursorLocation(int location) { - int adjustedLocation = location < 0 ? 0 : location; - adjustedLocation = adjustedLocation > (signed int)editedTextLength() ? (signed int)editedTextLength() : adjustedLocation; + assert(location >= 0); + int adjustedLocation = location > (signed int)editedTextLength() ? (signed int)editedTextLength() : location; m_cursorIndex = adjustedLocation; layoutSubviews(); } @@ -84,7 +84,9 @@ void TextInput::scrollToCursor() { } bool TextInput::setCursorLocation(int location) { - contentView()->setCursorLocation(location); + int adjustedLocation = location < 0 ? 0 : location; + willSetCursorLocation(&adjustedLocation); + contentView()->setCursorLocation(adjustedLocation); scrollToCursor(); return true; }