diff --git a/escher/include/escher/text_input.h b/escher/include/escher/text_input.h index 933fe1bc0..5da71452a 100644 --- a/escher/include/escher/text_input.h +++ b/escher/include/escher/text_input.h @@ -23,26 +23,36 @@ protected: ContentView(const KDFont * font) : View(), m_cursorView(), + m_font(font), m_selectionStart(nullptr), m_selectionEnd(nullptr), - m_font(font), m_cursorLocation(nullptr) {} + + // Font void setFont(const KDFont * font); const KDFont * font() const { return m_font; } + + // Cursor location const char * cursorLocation() const { assert(m_cursorLocation != nullptr); return m_cursorLocation; } void setCursorLocation(const char * cursorLocation); + KDRect cursorRect(); + + // Virtual text get/add/remove virtual const char * text() const = 0; virtual bool insertTextAtLocation(const char * text, const char * location) = 0; virtual bool removePreviousGlyph() = 0; virtual bool removeEndOfLine() = 0; - KDRect cursorRect(); + + // Selection const char * selectionStart() const { return m_selectionStart; } const char * selectionEnd() const { return m_selectionEnd; } void addSelection(const char * left, const char * right); bool resetSelection(); // returns true if the selection was indeed reset bool selectionIsEmpty() const; virtual size_t deleteSelection() = 0; + + // Reload void reloadRectFromPosition(const char * position, bool includeFollowingLines = false); protected: virtual void layoutSubviews(bool force = false) override; @@ -50,9 +60,9 @@ protected: virtual KDRect glyphFrameAtPosition(const char * buffer, const char * position) const = 0; virtual KDRect dirtyRectFromPosition(const char * position, bool includeFollowingLines) const; TextCursorView m_cursorView; + const KDFont * m_font; const char * m_selectionStart; const char * m_selectionEnd; - const KDFont * m_font; const char * m_cursorLocation; private: int numberOfSubviews() const override { return 1; } diff --git a/escher/src/text_input.cpp b/escher/src/text_input.cpp index bdb4f0ecb..a3e483aec 100644 --- a/escher/src/text_input.cpp +++ b/escher/src/text_input.cpp @@ -8,6 +8,11 @@ static inline const char * minCharPointer(const char * x, const char * y) { return x < y ? x : y; } static inline const char * maxCharPointer(const char * x, const char * y) { return x > y ? x : y; } +void TextInput::ContentView::setFont(const KDFont * font) { + m_font = font; + markRectAsDirty(bounds()); +} + void TextInput::ContentView::setCursorLocation(const char * location) { assert(location != nullptr); assert(location >= editedText()); @@ -16,11 +21,6 @@ void TextInput::ContentView::setCursorLocation(const char * location) { layoutSubviews(); } -void TextInput::ContentView::setFont(const KDFont * font) { - m_font = font; - markRectAsDirty(bounds()); -} - KDRect TextInput::ContentView::cursorRect() { return glyphFrameAtPosition(editedText(), m_cursorLocation); } @@ -75,14 +75,14 @@ bool TextInput::ContentView::selectionIsEmpty() const { return m_selectionStart == nullptr; } -void TextInput::ContentView::layoutSubviews(bool force) { - m_cursorView.setFrame(cursorRect(), force); -} - void TextInput::ContentView::reloadRectFromPosition(const char * position, bool includeFollowingLines) { markRectAsDirty(dirtyRectFromPosition(position, includeFollowingLines)); } +void TextInput::ContentView::layoutSubviews(bool force) { + m_cursorView.setFrame(cursorRect(), force); +} + void TextInput::ContentView::reloadRectFromAndToPositions(const char * start, const char * end) { if (start == end) { return; @@ -123,6 +123,15 @@ bool TextInput::removePreviousGlyph() { return true; } +bool TextInput::setCursorLocation(const char * location) { + assert(location != nullptr); + const char * adjustedLocation = maxCharPointer(location, text()); + willSetCursorLocation(&adjustedLocation); + contentView()->setCursorLocation(adjustedLocation); + scrollToCursor(); + return true; +} + void TextInput::scrollToCursor() { /* Technically, we do not need to overscroll in text input. However, we should * layout the scroll view before calling scrollToContentRect (in case the size @@ -147,15 +156,6 @@ void TextInput::deleteSelection() { contentView()->reloadRectFromPosition(previousSelectionStart, true); } -bool TextInput::setCursorLocation(const char * location) { - assert(location != nullptr); - const char * adjustedLocation = maxCharPointer(location, text()); - willSetCursorLocation(&adjustedLocation); - contentView()->setCursorLocation(adjustedLocation); - scrollToCursor(); - return true; -} - bool TextInput::insertTextAtLocation(const char * text, const char * location) { if (contentView()->insertTextAtLocation(text, location)) { /* We layout the scrollable view before scrolling to cursor because the