From 8aa5c55f61b56663d158c10b13c326cc7cedefb0 Mon Sep 17 00:00:00 2001 From: Mehmet Soyturk Date: Tue, 2 Nov 2021 10:15:13 +0300 Subject: [PATCH] Change wxRichTextParagraph::m_cachedLines data type to vector Now wxRichTextParagraph::m_cachedLines is stored as a wxVector instead of a wxList. See #2523, closes #2577. --- docs/changes.txt | 3 + include/wx/richtext/richtextbuffer.h | 7 +- interface/wx/richtext/richtextbuffer.h | 6 +- src/richtext/richtextbuffer.cpp | 163 ++++++++++++------------- src/richtext/richtextprint.cpp | 10 +- 5 files changed, 93 insertions(+), 96 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index c8bc869e05..73fba50a6a 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -139,6 +139,9 @@ Changes in behaviour not resulting in compilation errors distinguishing them from the drive letters, even for single letter network share name. +- wxRichTextParagraph::GetLines() now returns const wxVector& + instead of wxList&. + Changes in behaviour which may result in build errors ----------------------------------------------------- diff --git a/include/wx/richtext/richtextbuffer.h b/include/wx/richtext/richtextbuffer.h index 84a1fd7002..4037a64551 100644 --- a/include/wx/richtext/richtextbuffer.h +++ b/include/wx/richtext/richtextbuffer.h @@ -4475,7 +4475,7 @@ protected: #endif }; -WX_DECLARE_LIST_WITH_DECL( wxRichTextLine, wxRichTextLineList , class WXDLLIMPEXP_RICHTEXT ); +typedef wxVector wxRichTextLineVector; /** @class wxRichTextParagraph @@ -4528,7 +4528,7 @@ public: /** Returns the cached lines. */ - wxRichTextLineList& GetLines() { return m_cachedLines; } + const wxRichTextLineVector& GetLines() const { return m_cachedLines; } // Operations @@ -4653,8 +4653,7 @@ public: protected: // The lines that make up the wrapped paragraph - wxRichTextLineList m_cachedLines; - wxVector m_cachedLinesVect; + wxRichTextLineVector m_cachedLines; // Whether the paragraph is impacted by floating objects from above int m_impactedByFloatingObjects; diff --git a/interface/wx/richtext/richtextbuffer.h b/interface/wx/richtext/richtextbuffer.h index a3858bf680..0d43e184c7 100644 --- a/interface/wx/richtext/richtextbuffer.h +++ b/interface/wx/richtext/richtextbuffer.h @@ -4305,7 +4305,7 @@ protected: #endif }; -class wxRichTextLineList; +typedef wxVector wxRichTextLineVector; /** @class wxRichTextParagraph @@ -4357,7 +4357,7 @@ public: /** Returns the cached lines. */ - wxRichTextLineList& GetLines() { return m_cachedLines; } + const wxRichTextLineVector& GetLines() const { return m_cachedLines; } // Operations @@ -4482,7 +4482,7 @@ public: protected: // The lines that make up the wrapped paragraph - wxRichTextLineList m_cachedLines; + wxRichTextLineVector m_cachedLines; // Whether the paragraph is impacted by floating objects from above int m_impactedByFloatingObjects; diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index 8640aa67a6..9d18c1f294 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -46,7 +46,6 @@ #include "wx/arrimpl.cpp" WX_DEFINE_LIST(wxRichTextObjectList) -WX_DEFINE_LIST(wxRichTextLineList) // Switch off if the platform doesn't like it for some reason #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1 @@ -2280,7 +2279,7 @@ bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, wxRichTextDrawingContext& co // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines if ( !forceQuickLayout && (layoutAll || - child->GetLines().IsEmpty() || + child->GetLines().empty() || !child->GetRange().IsOutside(invalidRange)) ) { // Lays out the object first with a given amount of space, and then if no width was specified in attr, @@ -2353,7 +2352,7 @@ bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, wxRichTextDrawingContext& co wxRichTextParagraph* nodeChild = wxDynamicCast(node->GetData(), wxRichTextParagraph); if (nodeChild) { - if (nodeChild->GetLines().GetCount() == 0) + if (nodeChild->GetLines().empty()) { nodeChild->SetImpactedByFloatingObjects(-1); @@ -2618,10 +2617,10 @@ wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool c if (child) { - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextLineVector::const_iterator it = child->GetLines().begin(); + while (it != child->GetLines().end()) { - wxRichTextLine* line = node2->GetData(); + wxRichTextLine* line = *it; wxRichTextRange range = line->GetAbsoluteRange(); @@ -2632,7 +2631,7 @@ wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool c ((range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd()))) return line; - node2 = node2->GetNext(); + ++it; } } } @@ -2658,17 +2657,17 @@ wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y) const if (child) { - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextLineVector::const_iterator it = child->GetLines().begin(); + while (it != child->GetLines().end()) { - wxRichTextLine* line = node2->GetData(); + wxRichTextLine* line = *it; wxRect rect(line->GetRect()); if (y <= rect.GetBottom()) return line; - node2 = node2->GetNext(); + ++it; } } @@ -2695,7 +2694,7 @@ int wxRichTextParagraphLayoutBox::GetLineCount() const // wxASSERT (child != NULL); if (child) - count += child->GetLines().GetCount(); + count += child->GetLines().size(); node = node->GetNext(); } @@ -3160,10 +3159,10 @@ long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos, bool caretPosi { if (child->GetRange().Contains(pos)) { - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextLineVector::const_iterator it = child->GetLines().begin(); + while (it != child->GetLines().end()) { - wxRichTextLine* line = node2->GetData(); + wxRichTextLine* line = *it; wxRichTextRange lineRange = line->GetAbsoluteRange(); if (lineRange.Contains(pos) || pos == lineRange.GetStart()) @@ -3179,14 +3178,14 @@ long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos, bool caretPosi lineCount ++; - node2 = node2->GetNext(); + ++it; } // If we didn't find it in the lines, it must be // the last position of the paragraph. So return the last line. return lineCount-1; } else - lineCount += child->GetLines().GetCount(); + lineCount += child->GetLines().size(); } node = node->GetNext(); @@ -3209,23 +3208,23 @@ wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long l if (child) { - if (lineNumber < (int) (child->GetLines().GetCount() + lineCount)) + if (lineNumber < (int) (child->GetLines().size() + lineCount)) { - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextLineVector::const_iterator it = child->GetLines().begin(); + while (it != child->GetLines().end()) { - wxRichTextLine* line = node2->GetData(); + wxRichTextLine* line = *it; if (lineCount == lineNumber) return line; lineCount ++; - node2 = node2->GetNext(); + ++it; } } else - lineCount += child->GetLines().GetCount(); + lineCount += child->GetLines().size(); } node = node->GetNext(); @@ -4836,7 +4835,7 @@ bool wxRichTextParagraph::Draw(wxDC& dc, wxRichTextDrawingContext& context, cons wxRichTextAttr bulletAttr(attr); // Get line height from first line, if any - wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : NULL; + wxRichTextLine* line = m_cachedLines.empty() ? NULL : m_cachedLines[0]; wxPoint linePos; int lineHeight wxDUMMY_INITIALIZE(0); @@ -4884,10 +4883,10 @@ bool wxRichTextParagraph::Draw(wxDC& dc, wxRichTextDrawingContext& context, cons // Draw the range for each line, one object at a time. - wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); - while (node) + wxRichTextLineVector::const_iterator it = m_cachedLines.begin(); + while (it != m_cachedLines.end()) { - wxRichTextLine* line = node->GetData(); + wxRichTextLine* line = *it; wxRichTextRange lineRange = line->GetAbsoluteRange(); // Lines are specified relative to the paragraph @@ -4950,7 +4949,7 @@ bool wxRichTextParagraph::Draw(wxDC& dc, wxRichTextDrawingContext& context, cons } } - node = node->GetNext(); + ++it; } return true; @@ -5696,8 +5695,10 @@ void wxRichTextParagraph::Copy(const wxRichTextParagraph& obj) /// Clear the cached lines void wxRichTextParagraph::ClearLines() { - WX_CLEAR_LIST(wxRichTextLineList, m_cachedLines); - m_cachedLinesVect.clear(); + size_t cachedLineCount = m_cachedLines.size(); + for (size_t i = 0; i < cachedLineCount; i++) + delete m_cachedLines[i]; + m_cachedLines.clear(); } /// Get/set the object size for the given range. Returns false if the range @@ -5877,10 +5878,10 @@ bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& siz // (so we can assume that getting the unformatted size for a fragment // within a line is the actual size) - wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); - while (node) + wxRichTextLineVector::const_iterator it = m_cachedLines.begin(); + while (it != m_cachedLines.end()) { - wxRichTextLine* line = node->GetData(); + wxRichTextLine* line = *it; wxRichTextRange lineRange = line->GetAbsoluteRange(); if (!lineRange.IsOutside(range)) { @@ -5931,7 +5932,7 @@ bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& siz sz.y += maxLineHeight; sz.x = wxMax(sz.x, maxLineWidth); } - node = node->GetNext(); + ++it; } size = sz; } @@ -5984,10 +5985,10 @@ bool wxRichTextParagraph::FindPosition(wxDC& dc, wxRichTextDrawingContext& conte if (index < GetRange().GetStart() || index > GetRange().GetEnd()) return false; - wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); - while (node) + wxRichTextLineVector::const_iterator it = m_cachedLines.begin(); + while (it != m_cachedLines.end()) { - wxRichTextLine* line = node->GetData(); + wxRichTextLine* line = *it; wxRichTextRange lineRange = line->GetAbsoluteRange(); if (index >= lineRange.GetStart() && index <= lineRange.GetEnd()) { @@ -5996,9 +5997,12 @@ bool wxRichTextParagraph::FindPosition(wxDC& dc, wxRichTextDrawingContext& conte // thing. if (index == lineRange.GetEnd() && forceLineStart) { - if (node->GetNext()) + wxRichTextLineVector::const_iterator itNext = it; + ++itNext; + + if (itNext != m_cachedLines.end()) { - wxRichTextLine* nextLine = node->GetNext()->GetData(); + wxRichTextLine* nextLine = *itNext; *height = nextLine->GetSize().y; pt = nextLine->GetAbsolutePosition(); return true; @@ -6025,7 +6029,7 @@ bool wxRichTextParagraph::FindPosition(wxDC& dc, wxRichTextDrawingContext& conte } - node = node->GetNext(); + ++it; } return false; @@ -6076,10 +6080,10 @@ int wxRichTextParagraph::HitTest(wxDC& dc, wxRichTextDrawingContext& context, co wxPoint paraPos = GetPosition(); - wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); - while (node) + wxRichTextLineVector::const_iterator it = m_cachedLines.begin(); + while (it != m_cachedLines.end()) { - wxRichTextLine* line = node->GetData(); + wxRichTextLine* line = *it; wxPoint linePos = paraPos + line->GetPosition(); wxSize lineSize = line->GetSize(); wxRichTextRange lineRange = line->GetAbsoluteRange(); @@ -6177,7 +6181,7 @@ int wxRichTextParagraph::HitTest(wxDC& dc, wxRichTextDrawingContext& context, co } } - node = node->GetNext(); + ++it; } return wxRICHTEXT_HITTEST_NONE; @@ -6528,18 +6532,16 @@ wxString wxRichTextParagraph::GetBulletText() /// Allocate or reuse a line object wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos) { - if (pos < (int) m_cachedLines.GetCount()) + if (pos < (int) m_cachedLines.size()) { - wxASSERT(m_cachedLinesVect.size() == m_cachedLines.GetCount()); - wxRichTextLine* line = m_cachedLinesVect[pos]; + wxRichTextLine* line = m_cachedLines[pos]; line->Init(this); return line; } else { wxRichTextLine* line = new wxRichTextLine(this); - m_cachedLines.Append(line); - m_cachedLinesVect.push_back(line); + m_cachedLines.push_back(line); return line; } } @@ -6547,18 +6549,14 @@ wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos) /// Clear remaining unused line objects, if any bool wxRichTextParagraph::ClearUnusedLines(int lineCount) { - int cachedLineCount = m_cachedLines.GetCount(); - if ((int) cachedLineCount > lineCount) + size_t cachedLineCount = m_cachedLines.size(); + + if ((size_t)lineCount < cachedLineCount) { - for (int i = 0; i < (int) (cachedLineCount - lineCount); i ++) - { - wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetLast(); - wxRichTextLine* line = node->GetData(); - m_cachedLines.Erase(node); - delete line; - } + for (size_t i = lineCount; i < cachedLineCount; i++) + delete m_cachedLines[i]; + m_cachedLines.resize(lineCount); } - m_cachedLinesVect.resize(lineCount); return true; } @@ -11836,20 +11834,21 @@ void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt& optimizationLin wxRichTextParagraph* para = container->GetParagraphAtPosition(GetRange().GetStart()); wxRichTextObjectList::compatibility_iterator firstNode = container->GetChildren().Find(para); wxRichTextObjectList::compatibility_iterator node = firstNode; - while (node) + bool stop = false; + + while (!stop && node) { wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData(); - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextLineVector::const_iterator it = child->GetLines().begin(); + while (!stop && it != child->GetLines().end()) { - wxRichTextLine* line = node2->GetData(); + wxRichTextLine* line = *it; wxPoint pt = line->GetAbsolutePosition(); wxRichTextRange range = line->GetAbsoluteRange(); if (pt.y > lastY) { - node2 = wxRichTextLineList::compatibility_iterator(); - node = wxRichTextObjectList::compatibility_iterator(); + stop = true; } else if (range.GetStart() > GetPosition() && pt.y >= firstVisiblePt.y) { @@ -11857,12 +11856,10 @@ void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt& optimizationLin optimizationLineYPositions.Add(pt.y); } - if (node2) - node2 = node2->GetNext(); + ++it; } - if (node) - node = node->GetNext(); + node = node->GetNext(); } if (wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0) @@ -12313,26 +12310,28 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent wxRichTextObjectList::compatibility_iterator firstNode = container->GetChildren().Find(para); wxRichTextObjectList::compatibility_iterator node = firstNode; wxRichTextObjectList::compatibility_iterator lastNode = wxRichTextObjectList::compatibility_iterator(); - while (node) + bool stop = false; + + while (!stop && node) { wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData(); - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextLineVector::const_iterator it = child->GetLines().begin(); + while (!stop && it != child->GetLines().end()) { - wxRichTextLine* line = node2->GetData(); + wxRichTextLine* line = *it; wxPoint pt = line->GetAbsolutePosition(); wxRichTextRange range = line->GetAbsoluteRange(); + const bool isLastLine = it + 1 == child->GetLines().end(); // we want to find the first line that is in the same position // as before. This will mean we're at the end of the changed text. if (pt.y > lastY) // going past the end of the window, no more info { - node2 = wxRichTextLineList::compatibility_iterator(); - node = wxRichTextObjectList::compatibility_iterator(); + stop = true; } // Detect last line in the buffer - else if (!node2->GetNext() && para->GetRange().Contains(container->GetOwnRange().GetEnd())) + else if (isLastLine && para->GetRange().Contains(container->GetOwnRange().GetEnd())) { // If deleting text, make sure we refresh below as well as above if (positionOffset >= 0) @@ -12343,8 +12342,7 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent lastNode = node; - node2 = wxRichTextLineList::compatibility_iterator(); - node = wxRichTextObjectList::compatibility_iterator(); + stop = true; break; } @@ -12363,20 +12361,17 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent lastNode = node; - node2 = wxRichTextLineList::compatibility_iterator(); - node = wxRichTextObjectList::compatibility_iterator(); + stop = true; break; } } } - if (node2) - node2 = node2->GetNext(); + ++it; } - if (node) - node = node->GetNext(); + node = node->GetNext(); } firstY = wxMax(firstVisiblePt.y, firstY); diff --git a/src/richtext/richtextprint.cpp b/src/richtext/richtextprint.cpp index ae75eed322..9c7b5ee496 100644 --- a/src/richtext/richtextprint.cpp +++ b/src/richtext/richtextprint.cpp @@ -80,13 +80,13 @@ void wxRichTextPrintout::OnPreparePrinting() wxASSERT (child != NULL); if (child) { - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextLineVector::const_iterator it = child->GetLines().begin(); + while (it != child->GetLines().end()) { - wxRichTextLine* line = node2->GetData(); + wxRichTextLine* line = *it; int lineY = child->GetPosition().y + line->GetPosition().y - yOffset; - bool hasHardPageBreak = ((node2 == child->GetLines().GetFirst()) && child->GetAttributes().HasPageBreak()); + bool hasHardPageBreak = ((it == child->GetLines().begin()) && child->GetAttributes().HasPageBreak()); // Break the page if either we're going off the bottom, or this paragraph specifies // an explicit page break @@ -134,7 +134,7 @@ void wxRichTextPrintout::OnPreparePrinting() lastLine = line; - node2 = node2->GetNext(); + ++it; } }