diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index a17c7508ae..166ec25cdb 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -53,6 +53,8 @@ struct TextAtom return String::repeatedString (String::charToString (passwordCharacter), numChars); } + + JUCE_LEAK_DETECTOR (TextAtom) }; //============================================================================== @@ -60,7 +62,7 @@ struct TextAtom class TextEditor::UniformTextSection { public: - UniformTextSection (const String& text, const Font& f, const Colour col, const juce_wchar passwordChar) + UniformTextSection (const String& text, const Font& f, Colour col, juce_wchar passwordChar) : font (f), colour (col) { initialiseAtoms (text, passwordChar); @@ -69,23 +71,10 @@ public: UniformTextSection (const UniformTextSection& other) : font (other.font), colour (other.colour) { - atoms.ensureStorageAllocated (other.atoms.size()); - - for (int i = 0; i < other.atoms.size(); ++i) - atoms.add (new TextAtom (*other.atoms.getUnchecked(i))); + atoms.addCopiesOf (other.atoms); } - ~UniformTextSection() {} // (no need to delete the atoms, as they're explicitly deleted by the caller) - - void clear() - { - for (int i = atoms.size(); --i >= 0;) - delete atoms.getUnchecked (i); - - atoms.clear(); - } - - void append (const UniformTextSection& other, const juce_wchar passwordChar) + void append (UniformTextSection& other, const juce_wchar passwordChar) { if (other.atoms.size() > 0) { @@ -115,6 +104,8 @@ public: atoms.add (other.atoms.getUnchecked(i)); ++i; } + + other.atoms.clear (false); } } @@ -134,9 +125,7 @@ public: for (int j = i; j < atoms.size(); ++j) section2->atoms.add (atoms.getUnchecked (j)); - for (int j = atoms.size(); --j >= i;) - atoms.remove (j); - + atoms.removeRange (i, atoms.size(), false); break; } else if (indexToBreakAt >= index && indexToBreakAt < nextIndex) @@ -156,9 +145,7 @@ public: for (int j = i + 1; j < atoms.size(); ++j) section2->atoms.add (atoms.getUnchecked (j)); - for (int j = atoms.size(); --j > i;) - atoms.remove (j); - + atoms.removeRange (i + 1, atoms.size(), false); break; } @@ -224,7 +211,7 @@ public: //============================================================================== Font font; Colour colour; - Array atoms; + OwnedArray atoms; private: void initialiseAtoms (const String& textToParse, const juce_wchar passwordChar) @@ -274,16 +261,15 @@ private: } } - TextAtom* const atom = new TextAtom(); + TextAtom* const atom = atoms.add (new TextAtom()); + atom->atomText = String (start, numChars); atom->width = font.getStringWidthFloat (atom->getText (passwordChar)); atom->numChars = (uint16) numChars; - - atoms.add (atom); } } - UniformTextSection& operator= (const UniformTextSection& other); + UniformTextSection& operator= (const UniformTextSection&); JUCE_LEAK_DETECTOR (UniformTextSection) }; @@ -291,7 +277,7 @@ private: class TextEditor::Iterator { public: - Iterator (const Array & sectionList, + Iterator (const OwnedArray& sectionList, const float wrapWidth, const juce_wchar passwordChar) : indexInText (0), @@ -685,7 +671,7 @@ public: const UniformTextSection* currentSection; private: - const Array & sections; + const OwnedArray& sections; int sectionIndex, atomIndex; const float wordWrapWidth; const juce_wchar passwordCharacter; @@ -772,22 +758,13 @@ public: const Range rangeToRemove, const int oldCaret, const int newCaret, - const Array & oldSections) + const Array& oldSections) : owner (ed), range (rangeToRemove), oldCaretPos (oldCaret), - newCaretPos (newCaret), - removedSections (oldSections) - { - } - - ~RemoveAction() + newCaretPos (newCaret) { - for (int i = removedSections.size(); --i >= 0;) - { - ScopedPointer section (removedSections.getUnchecked (i)); - section->clear(); - } + removedSections.addArray (oldSections); } bool perform() @@ -816,7 +793,7 @@ private: TextEditor& owner; const Range range; const int oldCaretPos, newCaretPos; - Array removedSections; + OwnedArray removedSections; JUCE_DECLARE_NON_COPYABLE (RemoveAction) }; @@ -967,11 +944,6 @@ TextEditor::~TextEditor() textValue.removeListener (textHolder); textValue.referTo (Value()); - for (int i = 0; i < sections.size(); ++i) - delete sections.getUnchecked(i); - - sections.clear(); - viewport = nullptr; textHolder = nullptr; } @@ -2239,8 +2211,7 @@ void TextEditor::insert (const String& text, } } -void TextEditor::reinsert (const int insertIndex, - const Array & sectionsToInsert) +void TextEditor::reinsert (const int insertIndex, const OwnedArray& sectionsToInsert) { int index = 0; int nextIndex = 0; @@ -2280,9 +2251,7 @@ void TextEditor::reinsert (const int insertIndex, valueTextNeedsUpdating = true; } -void TextEditor::remove (Range range, - UndoManager* const um, - const int caretPositionToMoveTo) +void TextEditor::remove (Range range, UndoManager* const um, const int caretPositionToMoveTo) { if (! range.isEmpty()) { @@ -2315,7 +2284,7 @@ void TextEditor::remove (Range range, if (um != nullptr) { - Array removedSections; + Array removedSections; for (int i = 0; i < sections.size(); ++i) { @@ -2350,9 +2319,7 @@ void TextEditor::remove (Range range, if (remainingRange.getStart() <= index && remainingRange.getEnd() >= nextIndex) { - sections.remove(i); - section->clear(); - delete section; + sections.remove (i); remainingRange.setEnd (remainingRange.getEnd() - (nextIndex - index)); if (remainingRange.isEmpty()) @@ -2533,8 +2500,7 @@ int TextEditor::findWordBreakBefore (const int position) const //============================================================================== -void TextEditor::splitSection (const int sectionIndex, - const int charToSplitAt) +void TextEditor::splitSection (const int sectionIndex, const int charToSplitAt) { jassert (sections[sectionIndex] != nullptr); @@ -2554,7 +2520,6 @@ void TextEditor::coalesceSimilarSections() { s1->append (*s2, passwordCharacter); sections.remove (i + 1); - delete s2; --i; } } diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.h b/modules/juce_gui_basics/widgets/juce_TextEditor.h index f4a9784ba3..2ccbfe001d 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.h +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.h @@ -657,7 +657,7 @@ private: friend class InsertAction; friend class RemoveAction; - ScopedPointer viewport; + ScopedPointer viewport; TextHolderComponent* textHolder; BorderSize borderSize; @@ -683,7 +683,7 @@ private: Font currentFont; mutable int totalNumChars; int caretPosition; - Array sections; + OwnedArray sections; String textToShowWhenEmpty; Colour colourForTextWhenEmpty; juce_wchar passwordCharacter; @@ -707,7 +707,7 @@ private: void splitSection (int sectionIndex, int charToSplitAt); void clearInternal (UndoManager*); void insert (const String&, int insertIndex, const Font&, const Colour, UndoManager*, int newCaretPos); - void reinsert (int insertIndex, const Array &); + void reinsert (int insertIndex, const OwnedArray&); void remove (Range range, UndoManager*, int caretPositionToMoveTo); void getCharPosition (int index, float& x, float& y, float& lineHeight) const; void updateCaretPosition();