From 3fd4f7a2315ea05eabd1551e6603eb4fae17cdc4 Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 1 Jun 2021 09:17:40 +0100 Subject: [PATCH] TextEditor: Take border size into account when calculating text bounds --- .../widgets/juce_TextEditor.cpp | 27 ++++++++++--------- .../juce_gui_basics/widgets/juce_TextEditor.h | 1 + 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index ffee87e709..a62b82573d 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -1472,14 +1472,20 @@ Rectangle TextEditor::getCaretRectangleFloat() const return { anchor.x, anchor.y, 2.0f, cursorHeight }; } +Point TextEditor::getTextOffset() const noexcept +{ + Iterator i (*this); + auto yOffset = i.getYOffset(); + + return { getLeftIndent() + borderSize.getLeft() - viewport->getViewPositionX(), + roundToInt ((float) getTopIndent() + (float) borderSize.getTop() + yOffset) - viewport->getViewPositionY() }; +} + RectangleList TextEditor::getTextBounds (Range textRange) { RectangleList boundingBox; - Iterator i (*this); - auto yOffset = i.getYOffset(); - - for (auto lineRange : i.getLineRanges()) + for (auto lineRange : Iterator { *this }.getLineRanges()) { auto intersection = lineRange.getIntersectionWith (textRange); @@ -1498,9 +1504,7 @@ RectangleList TextEditor::getTextBounds (Range textRange) } } - boundingBox.offsetAll (getLeftIndent() - viewport->getViewPositionX(), - roundToInt ((float) getTopIndent() + yOffset) - viewport->getViewPositionY()); - + boundingBox.offsetAll (getTextOffset()); return boundingBox; } @@ -1653,10 +1657,10 @@ void TextEditor::moveCaretTo (const int newPosition, const bool isSelecting) int TextEditor::getTextIndexAt (const int x, const int y) const { - Iterator i (*this); + const auto offset = getTextOffset(); - return indexAtPosition ((float) (x + viewport->getViewPositionX() - leftIndent - borderSize.getLeft()), - (float) (y + viewport->getViewPositionY() - topIndent - borderSize.getTop()) - i.getYOffset()); + return indexAtPosition ((float) (x - offset.x), + (float) (y - offset.y)); } void TextEditor::insertTextAtCaret (const String& t) @@ -1746,8 +1750,7 @@ void TextEditor::drawContent (Graphics& g) g.setColour (findColour (highlightColourId).withMultipliedAlpha (hasKeyboardFocus (true) ? 1.0f : 0.5f)); auto boundingBox = getTextBounds (selection); - boundingBox.offsetAll (viewport->getViewPositionX() - leftIndent, - viewport->getViewPositionY() - roundToInt ((float) topIndent + yOffset)); + boundingBox.offsetAll (-getTextOffset()); g.fillPath (boundingBox.toPath(), transform); } diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.h b/modules/juce_gui_basics/widgets/juce_TextEditor.h index 5e973d3adf..98a2991132 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.h +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h @@ -828,6 +828,7 @@ private: bool undoOrRedo (bool shouldUndo); UndoManager* getUndoManager() noexcept; void setSelection (Range) noexcept; + Point getTextOffset() const noexcept; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextEditor) };