Browse Source

Fix to TextEditor caret positioning

tags/2021-05-28
jules 8 years ago
parent
commit
d73f9bdd74
2 changed files with 13 additions and 7 deletions
  1. +12
    -7
      modules/juce_gui_basics/widgets/juce_TextEditor.cpp
  2. +1
    -0
      modules/juce_gui_basics/widgets/juce_TextEditor.h

+ 12
- 7
modules/juce_gui_basics/widgets/juce_TextEditor.cpp View File

@@ -1338,12 +1338,17 @@ void TextEditor::scrollEditorToPositionCaret (const int desiredCaretX,
} }
Rectangle<int> TextEditor::getCaretRectangle() Rectangle<int> TextEditor::getCaretRectangle()
{
return getCaretRectangleFloat().getSmallestIntegerContainer();
}
Rectangle<float> TextEditor::getCaretRectangleFloat() const
{ {
Point<float> anchor; Point<float> anchor;
auto cursorHeight = currentFont.getHeight(); // (in case the text is empty and the call below doesn't set this value) auto cursorHeight = currentFont.getHeight(); // (in case the text is empty and the call below doesn't set this value)
getCharPosition (caretPosition, anchor, cursorHeight); 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()) if (! isMultiLine())
return moveCaretToStartOfLine (selecting); return moveCaretToStartOfLine (selecting);
auto caretPos = getCaretRectangle().toFloat();
auto caretPos = getCaretRectangleFloat();
return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - 1.0f), selecting); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - 1.0f), selecting);
} }
@@ -1846,7 +1851,7 @@ bool TextEditor::moveCaretDown (bool selecting)
if (! isMultiLine()) if (! isMultiLine())
return moveCaretToEndOfLine (selecting); return moveCaretToEndOfLine (selecting);
auto caretPos = getCaretRectangle().toFloat();
auto caretPos = getCaretRectangleFloat();
return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + 1.0f), selecting); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + 1.0f), selecting);
} }
@@ -1855,7 +1860,7 @@ bool TextEditor::pageUp (bool selecting)
if (! isMultiLine()) if (! isMultiLine())
return moveCaretToStartOfLine (selecting); return moveCaretToStartOfLine (selecting);
auto caretPos = getCaretRectangle().toFloat();
auto caretPos = getCaretRectangleFloat();
return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - viewport->getViewHeight()), selecting); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getY() - viewport->getViewHeight()), selecting);
} }
@@ -1864,7 +1869,7 @@ bool TextEditor::pageDown (bool selecting)
if (! isMultiLine()) if (! isMultiLine())
return moveCaretToEndOfLine (selecting); return moveCaretToEndOfLine (selecting);
auto caretPos = getCaretRectangle().toFloat();
auto caretPos = getCaretRectangleFloat();
return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + viewport->getViewHeight()), selecting); return moveCaretWithTransaction (indexAtPosition (caretPos.getX(), caretPos.getBottom() + viewport->getViewHeight()), selecting);
} }
@@ -1892,7 +1897,7 @@ bool TextEditor::moveCaretToTop (bool selecting)
bool TextEditor::moveCaretToStartOfLine (bool selecting) bool TextEditor::moveCaretToStartOfLine (bool selecting)
{ {
auto caretPos = getCaretRectangle().toFloat();
auto caretPos = getCaretRectangleFloat();
return moveCaretWithTransaction (indexAtPosition (0.0f, caretPos.getY()), selecting); return moveCaretWithTransaction (indexAtPosition (0.0f, caretPos.getY()), selecting);
} }
@@ -1903,7 +1908,7 @@ bool TextEditor::moveCaretToEnd (bool selecting)
bool TextEditor::moveCaretToEndOfLine (bool selecting) bool TextEditor::moveCaretToEndOfLine (bool selecting)
{ {
auto caretPos = getCaretRectangle().toFloat();
auto caretPos = getCaretRectangleFloat();
return moveCaretWithTransaction (indexAtPosition ((float) textHolder->getWidth(), caretPos.getY()), selecting); return moveCaretWithTransaction (indexAtPosition ((float) textHolder->getWidth(), caretPos.getY()), selecting);
} }


+ 1
- 0
modules/juce_gui_basics/widgets/juce_TextEditor.h View File

@@ -749,6 +749,7 @@ private:
void reinsert (int insertIndex, const OwnedArray<UniformTextSection>&); void reinsert (int insertIndex, const OwnedArray<UniformTextSection>&);
void remove (Range<int>, UndoManager*, int caretPositionToMoveTo); void remove (Range<int>, UndoManager*, int caretPositionToMoveTo);
void getCharPosition (int index, Point<float>&, float& lineHeight) const; void getCharPosition (int index, Point<float>&, float& lineHeight) const;
Rectangle<float> getCaretRectangleFloat() const;
void updateCaretPosition(); void updateCaretPosition();
void updateValueFromText(); void updateValueFromText();
void textWasChangedByValue(); void textWasChangedByValue();


Loading…
Cancel
Save