From d73f9bdd741524c8fc7aa5a337f1ee3bc83914e7 Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 13 Oct 2017 10:41:21 +0100 Subject: [PATCH] Fix to TextEditor caret positioning --- .../widgets/juce_TextEditor.cpp | 19 ++++++++++++------- .../juce_gui_basics/widgets/juce_TextEditor.h | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index c781a0381e..e3d436b756 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -1338,12 +1338,17 @@ void TextEditor::scrollEditorToPositionCaret (const int desiredCaretX, } Rectangle TextEditor::getCaretRectangle() +{ + return getCaretRectangleFloat().getSmallestIntegerContainer(); +} + +Rectangle TextEditor::getCaretRectangleFloat() const { Point anchor; auto cursorHeight = currentFont.getHeight(); // (in case the text is empty and the call below doesn't set this value) getCharPosition (caretPosition, anchor, cursorHeight); - return { roundToInt (anchor.x), roundToInt (anchor.y), 2, roundToInt (cursorHeight) }; + return { anchor.x, anchor.y, 2.0f, cursorHeight }; } //============================================================================== @@ -1837,7 +1842,7 @@ bool TextEditor::moveCaretUp (bool selecting) if (! isMultiLine()) return moveCaretToStartOfLine (selecting); - auto caretPos = getCaretRectangle().toFloat(); + auto caretPos = getCaretRectangleFloat(); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - 1.0f), selecting); } @@ -1846,7 +1851,7 @@ bool TextEditor::moveCaretDown (bool selecting) if (! isMultiLine()) return moveCaretToEndOfLine (selecting); - auto caretPos = getCaretRectangle().toFloat(); + auto caretPos = getCaretRectangleFloat(); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + 1.0f), selecting); } @@ -1855,7 +1860,7 @@ bool TextEditor::pageUp (bool selecting) if (! isMultiLine()) return moveCaretToStartOfLine (selecting); - auto caretPos = getCaretRectangle().toFloat(); + auto caretPos = getCaretRectangleFloat(); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - viewport->getViewHeight()), selecting); } @@ -1864,7 +1869,7 @@ bool TextEditor::pageDown (bool selecting) if (! isMultiLine()) return moveCaretToEndOfLine (selecting); - auto caretPos = getCaretRectangle().toFloat(); + auto caretPos = getCaretRectangleFloat(); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + viewport->getViewHeight()), selecting); } @@ -1892,7 +1897,7 @@ bool TextEditor::moveCaretToTop (bool selecting) bool TextEditor::moveCaretToStartOfLine (bool selecting) { - auto caretPos = getCaretRectangle().toFloat(); + auto caretPos = getCaretRectangleFloat(); return moveCaretWithTransaction (indexAtPosition (0.0f, caretPos.getY()), selecting); } @@ -1903,7 +1908,7 @@ bool TextEditor::moveCaretToEnd (bool selecting) bool TextEditor::moveCaretToEndOfLine (bool selecting) { - auto caretPos = getCaretRectangle().toFloat(); + auto caretPos = getCaretRectangleFloat(); return moveCaretWithTransaction (indexAtPosition ((float) textHolder->getWidth(), caretPos.getY()), selecting); } diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.h b/modules/juce_gui_basics/widgets/juce_TextEditor.h index 3afae595e2..a8d2a17ce9 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.h +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h @@ -749,6 +749,7 @@ private: void reinsert (int insertIndex, const OwnedArray&); void remove (Range, UndoManager*, int caretPositionToMoveTo); void getCharPosition (int index, Point&, float& lineHeight) const; + Rectangle getCaretRectangleFloat() const; void updateCaretPosition(); void updateValueFromText(); void textWasChangedByValue();