Browse Source

Refactored some TextEditor allocation.

tags/2021-05-28
jules 11 years ago
parent
commit
542c4bc8eb
2 changed files with 27 additions and 62 deletions
  1. +24
    -59
      modules/juce_gui_basics/widgets/juce_TextEditor.cpp
  2. +3
    -3
      modules/juce_gui_basics/widgets/juce_TextEditor.h

+ 24
- 59
modules/juce_gui_basics/widgets/juce_TextEditor.cpp View File

@@ -53,6 +53,8 @@ struct TextAtom
return String::repeatedString (String::charToString (passwordCharacter), numChars); return String::repeatedString (String::charToString (passwordCharacter), numChars);
} }
JUCE_LEAK_DETECTOR (TextAtom)
}; };
//============================================================================== //==============================================================================
@@ -60,7 +62,7 @@ struct TextAtom
class TextEditor::UniformTextSection class TextEditor::UniformTextSection
{ {
public: 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) : font (f), colour (col)
{ {
initialiseAtoms (text, passwordChar); initialiseAtoms (text, passwordChar);
@@ -69,23 +71,10 @@ public:
UniformTextSection (const UniformTextSection& other) UniformTextSection (const UniformTextSection& other)
: font (other.font), colour (other.colour) : 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) if (other.atoms.size() > 0)
{ {
@@ -115,6 +104,8 @@ public:
atoms.add (other.atoms.getUnchecked(i)); atoms.add (other.atoms.getUnchecked(i));
++i; ++i;
} }
other.atoms.clear (false);
} }
} }
@@ -134,9 +125,7 @@ public:
for (int j = i; j < atoms.size(); ++j) for (int j = i; j < atoms.size(); ++j)
section2->atoms.add (atoms.getUnchecked (j)); section2->atoms.add (atoms.getUnchecked (j));
for (int j = atoms.size(); --j >= i;)
atoms.remove (j);
atoms.removeRange (i, atoms.size(), false);
break; break;
} }
else if (indexToBreakAt >= index && indexToBreakAt < nextIndex) else if (indexToBreakAt >= index && indexToBreakAt < nextIndex)
@@ -156,9 +145,7 @@ public:
for (int j = i + 1; j < atoms.size(); ++j) for (int j = i + 1; j < atoms.size(); ++j)
section2->atoms.add (atoms.getUnchecked (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; break;
} }
@@ -224,7 +211,7 @@ public:
//============================================================================== //==============================================================================
Font font; Font font;
Colour colour; Colour colour;
Array <TextAtom*> atoms;
OwnedArray<TextAtom> atoms;
private: private:
void initialiseAtoms (const String& textToParse, const juce_wchar passwordChar) 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->atomText = String (start, numChars);
atom->width = font.getStringWidthFloat (atom->getText (passwordChar)); atom->width = font.getStringWidthFloat (atom->getText (passwordChar));
atom->numChars = (uint16) numChars; atom->numChars = (uint16) numChars;
atoms.add (atom);
} }
} }
UniformTextSection& operator= (const UniformTextSection& other);
UniformTextSection& operator= (const UniformTextSection&);
JUCE_LEAK_DETECTOR (UniformTextSection) JUCE_LEAK_DETECTOR (UniformTextSection)
}; };
@@ -291,7 +277,7 @@ private:
class TextEditor::Iterator class TextEditor::Iterator
{ {
public: public:
Iterator (const Array <UniformTextSection*>& sectionList,
Iterator (const OwnedArray<UniformTextSection>& sectionList,
const float wrapWidth, const float wrapWidth,
const juce_wchar passwordChar) const juce_wchar passwordChar)
: indexInText (0), : indexInText (0),
@@ -685,7 +671,7 @@ public:
const UniformTextSection* currentSection; const UniformTextSection* currentSection;
private: private:
const Array <UniformTextSection*>& sections;
const OwnedArray<UniformTextSection>& sections;
int sectionIndex, atomIndex; int sectionIndex, atomIndex;
const float wordWrapWidth; const float wordWrapWidth;
const juce_wchar passwordCharacter; const juce_wchar passwordCharacter;
@@ -772,22 +758,13 @@ public:
const Range<int> rangeToRemove, const Range<int> rangeToRemove,
const int oldCaret, const int oldCaret,
const int newCaret, const int newCaret,
const Array <UniformTextSection*>& oldSections)
const Array<UniformTextSection*>& oldSections)
: owner (ed), : owner (ed),
range (rangeToRemove), range (rangeToRemove),
oldCaretPos (oldCaret), oldCaretPos (oldCaret),
newCaretPos (newCaret),
removedSections (oldSections)
{
}
~RemoveAction()
newCaretPos (newCaret)
{ {
for (int i = removedSections.size(); --i >= 0;)
{
ScopedPointer<UniformTextSection> section (removedSections.getUnchecked (i));
section->clear();
}
removedSections.addArray (oldSections);
} }
bool perform() bool perform()
@@ -816,7 +793,7 @@ private:
TextEditor& owner; TextEditor& owner;
const Range<int> range; const Range<int> range;
const int oldCaretPos, newCaretPos; const int oldCaretPos, newCaretPos;
Array <UniformTextSection*> removedSections;
OwnedArray<UniformTextSection> removedSections;
JUCE_DECLARE_NON_COPYABLE (RemoveAction) JUCE_DECLARE_NON_COPYABLE (RemoveAction)
}; };
@@ -967,11 +944,6 @@ TextEditor::~TextEditor()
textValue.removeListener (textHolder); textValue.removeListener (textHolder);
textValue.referTo (Value()); textValue.referTo (Value());
for (int i = 0; i < sections.size(); ++i)
delete sections.getUnchecked(i);
sections.clear();
viewport = nullptr; viewport = nullptr;
textHolder = nullptr; textHolder = nullptr;
} }
@@ -2239,8 +2211,7 @@ void TextEditor::insert (const String& text,
} }
} }
void TextEditor::reinsert (const int insertIndex,
const Array <UniformTextSection*>& sectionsToInsert)
void TextEditor::reinsert (const int insertIndex, const OwnedArray<UniformTextSection>& sectionsToInsert)
{ {
int index = 0; int index = 0;
int nextIndex = 0; int nextIndex = 0;
@@ -2280,9 +2251,7 @@ void TextEditor::reinsert (const int insertIndex,
valueTextNeedsUpdating = true; valueTextNeedsUpdating = true;
} }
void TextEditor::remove (Range<int> range,
UndoManager* const um,
const int caretPositionToMoveTo)
void TextEditor::remove (Range<int> range, UndoManager* const um, const int caretPositionToMoveTo)
{ {
if (! range.isEmpty()) if (! range.isEmpty())
{ {
@@ -2315,7 +2284,7 @@ void TextEditor::remove (Range<int> range,
if (um != nullptr) if (um != nullptr)
{ {
Array <UniformTextSection*> removedSections;
Array<UniformTextSection*> removedSections;
for (int i = 0; i < sections.size(); ++i) for (int i = 0; i < sections.size(); ++i)
{ {
@@ -2350,9 +2319,7 @@ void TextEditor::remove (Range<int> range,
if (remainingRange.getStart() <= index && remainingRange.getEnd() >= nextIndex) if (remainingRange.getStart() <= index && remainingRange.getEnd() >= nextIndex)
{ {
sections.remove(i);
section->clear();
delete section;
sections.remove (i);
remainingRange.setEnd (remainingRange.getEnd() - (nextIndex - index)); remainingRange.setEnd (remainingRange.getEnd() - (nextIndex - index));
if (remainingRange.isEmpty()) 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); jassert (sections[sectionIndex] != nullptr);
@@ -2554,7 +2520,6 @@ void TextEditor::coalesceSimilarSections()
{ {
s1->append (*s2, passwordCharacter); s1->append (*s2, passwordCharacter);
sections.remove (i + 1); sections.remove (i + 1);
delete s2;
--i; --i;
} }
} }


+ 3
- 3
modules/juce_gui_basics/widgets/juce_TextEditor.h View File

@@ -657,7 +657,7 @@ private:
friend class InsertAction; friend class InsertAction;
friend class RemoveAction; friend class RemoveAction;
ScopedPointer <Viewport> viewport;
ScopedPointer<Viewport> viewport;
TextHolderComponent* textHolder; TextHolderComponent* textHolder;
BorderSize<int> borderSize; BorderSize<int> borderSize;
@@ -683,7 +683,7 @@ private:
Font currentFont; Font currentFont;
mutable int totalNumChars; mutable int totalNumChars;
int caretPosition; int caretPosition;
Array <UniformTextSection*> sections;
OwnedArray<UniformTextSection> sections;
String textToShowWhenEmpty; String textToShowWhenEmpty;
Colour colourForTextWhenEmpty; Colour colourForTextWhenEmpty;
juce_wchar passwordCharacter; juce_wchar passwordCharacter;
@@ -707,7 +707,7 @@ private:
void splitSection (int sectionIndex, int charToSplitAt); void splitSection (int sectionIndex, int charToSplitAt);
void clearInternal (UndoManager*); void clearInternal (UndoManager*);
void insert (const String&, int insertIndex, const Font&, const Colour, UndoManager*, int newCaretPos); void insert (const String&, int insertIndex, const Font&, const Colour, UndoManager*, int newCaretPos);
void reinsert (int insertIndex, const Array <UniformTextSection*>&);
void reinsert (int insertIndex, const OwnedArray<UniformTextSection>&);
void remove (Range<int> range, UndoManager*, int caretPositionToMoveTo); void remove (Range<int> range, UndoManager*, int caretPositionToMoveTo);
void getCharPosition (int index, float& x, float& y, float& lineHeight) const; void getCharPosition (int index, float& x, float& y, float& lineHeight) const;
void updateCaretPosition(); void updateCaretPosition();


Loading…
Cancel
Save