X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e191ee875133b476d005f2b35eec43e8281dbac2..77a1771911ed222ba6861ead327c3735fc16a0b1:/src/richtext/richtextbuffer.cpp?ds=sidebyside diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index cf57ce9b69..3f714632a4 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -16,29 +16,29 @@ #pragma hdrstop #endif +#if wxUSE_RICHTEXT + +#include "wx/richtext/richtextbuffer.h" + #ifndef WX_PRECOMP #include "wx/wx.h" #endif -#include "wx/image.h" - -#if wxUSE_RICHTEXT - #include "wx/filename.h" #include "wx/clipbrd.h" +#include "wx/dataobj.h" #include "wx/wfstream.h" #include "wx/module.h" #include "wx/mstream.h" #include "wx/sstream.h" -#include "wx/richtext/richtextbuffer.h" #include "wx/richtext/richtextctrl.h" #include "wx/richtext/richtextstyles.h" #include "wx/listimpl.cpp" -WX_DEFINE_LIST(wxRichTextObjectList); -WX_DEFINE_LIST(wxRichTextLineList); +WX_DEFINE_LIST(wxRichTextObjectList) +WX_DEFINE_LIST(wxRichTextLineList) /*! * wxRichTextObject @@ -174,9 +174,10 @@ bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject* child, bool delete wxRichTextObjectList::compatibility_iterator node = m_children.Find(child); if (node) { + wxRichTextObject* obj = node->GetData(); + m_children.Erase(node); if (deleteChild) - delete node->GetData(); - delete node; + delete obj; return true; } @@ -195,7 +196,7 @@ bool wxRichTextCompositeObject::DeleteChildren() child->Dereference(); // Only delete if reference count is zero node = node->GetNext(); - delete oldNode; + m_children.Erase(oldNode); } return true; @@ -368,7 +369,7 @@ bool wxRichTextCompositeObject::Defragment() if (child->CanMerge(nextChild) && child->Merge(nextChild)) { nextChild->Dereference(); - delete node->GetNext(); + m_children.Erase(node->GetNext()); // Don't set node -- we'll see if we can merge again with the next // child. @@ -516,7 +517,31 @@ bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, const wxRichTextRange& range, /// Lay the item out bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int style) { - wxRect availableSpace(rect.x + m_leftMargin, + wxRect availableSpace; + bool formatRect = (style & wxRICHTEXT_LAYOUT_SPECIFIED_RECT) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT; + + // If only laying out a specific area, the passed rect has a different meaning: + // the visible part of the buffer. + if (formatRect) + { + availableSpace = wxRect(0 + m_leftMargin, + 0 + m_topMargin, + rect.width - m_leftMargin - m_rightMargin, + rect.height); + + // Invalidate the part of the buffer from the first visible line + // to the end. If other parts of the buffer are currently invalid, + // then they too will be taken into account if they are above + // the visible point. + long startPos = 0; + wxRichTextLine* line = GetLineAtYPosition(rect.y); + if (line) + startPos = line->GetAbsoluteRange().GetStart(); + + Invalidate(wxRichTextRange(startPos, GetRange().GetEnd())); + } + else + availableSpace = wxRect(rect.x + m_leftMargin, rect.y + m_topMargin, rect.width - m_leftMargin - m_rightMargin, rect.height - m_topMargin - m_bottomMargin); @@ -524,13 +549,13 @@ bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int styl int maxWidth = 0; wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); - + bool layoutAll = true; // Get invalid range, rounding to paragraph start/end. wxRichTextRange invalidRange = GetInvalidRange(true); - if (invalidRange == wxRICHTEXT_NONE) + if (invalidRange == wxRICHTEXT_NONE && !formatRect) return true; if (invalidRange == wxRICHTEXT_ALL) @@ -542,7 +567,9 @@ bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int styl if (firstParagraph) { wxRichTextObjectList::compatibility_iterator firstNode = m_children.Find(firstParagraph); - wxRichTextObjectList::compatibility_iterator previousNode = firstNode ? firstNode->GetPrevious() : (wxRichTextObjectList::compatibility_iterator) NULL; + wxRichTextObjectList::compatibility_iterator previousNode; + if ( firstNode ) + previousNode = firstNode->GetPrevious(); if (firstNode && previousNode) { wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph); @@ -556,21 +583,33 @@ bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int styl } } + // A way to force speedy rest-of-buffer layout (the 'else' below) + bool forceQuickLayout = false; + while (node) { // Assume this box only contains paragraphs wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); - wxASSERT (child != NULL); + wxCHECK_MSG( child, false, _T("Unknown object in layout") ); // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines - if (child && (layoutAll || child->GetLines().GetCount() == 0 || !child->GetRange().IsOutside(invalidRange))) + if ( !forceQuickLayout && + (layoutAll || + child->GetLines().IsEmpty() || + !child->GetRange().IsOutside(invalidRange)) ) { child->Layout(dc, availableSpace, style); // Layout must set the cached size availableSpace.y += child->GetCachedSize().y; maxWidth = wxMax(maxWidth, child->GetCachedSize().x); + + // If we're just formatting the visible part of the buffer, + // and we're now past the bottom of the window, start quick + // layout. + if (formatRect && child->GetPosition().y > rect.GetBottom()) + forceQuickLayout = true; } else { @@ -622,8 +661,8 @@ bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wx { wxSize sz; - wxRichTextObjectList::compatibility_iterator startPara = NULL; - wxRichTextObjectList::compatibility_iterator endPara = NULL; + wxRichTextObjectList::compatibility_iterator startPara = wxRichTextObjectList::compatibility_iterator(); + wxRichTextObjectList::compatibility_iterator endPara = wxRichTextObjectList::compatibility_iterator(); // First find the first paragraph whose starting position is within the range. wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); @@ -1096,8 +1135,6 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextFragm return true; } - - return false; } /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'. @@ -1317,7 +1354,7 @@ bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range) } wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext(); - delete node1; + nextParagraph->GetChildren().Erase(node1); node1 = next1; } @@ -1557,8 +1594,8 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxRichTextObjectList::compatibility_iterator firstNode = newPara->GetChildren().Find(firstObject); wxRichTextObjectList::compatibility_iterator lastNode = newPara->GetChildren().Find(lastObject); - wxASSERT(firstNode != NULL); - wxASSERT(lastNode != NULL); + wxASSERT(firstNode); + wxASSERT(lastNode); wxRichTextObjectList::compatibility_iterator node2 = firstNode; @@ -1747,7 +1784,7 @@ void wxRichTextParagraphLayoutBox::Reset() void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange& invalidRange) { SetDirty(true); - + if (invalidRange == wxRICHTEXT_ALL) { m_invalidRange = wxRICHTEXT_ALL; @@ -1757,7 +1794,7 @@ void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange& invalidRang // Already invalidating everything if (m_invalidRange == wxRICHTEXT_ALL) return; - + if ((invalidRange.GetStart() < m_invalidRange.GetStart()) || m_invalidRange.GetStart() == -1) m_invalidRange.SetStart(invalidRange.GetStart()); if (invalidRange.GetEnd() > m_invalidRange.GetEnd()) @@ -1769,9 +1806,9 @@ wxRichTextRange wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagrap { if (m_invalidRange == wxRICHTEXT_ALL || m_invalidRange == wxRICHTEXT_NONE) return m_invalidRange; - + wxRichTextRange range = m_invalidRange; - + if (wholeParagraphs) { wxRichTextParagraph* para1 = GetParagraphAtPosition(range.GetStart()); @@ -1949,6 +1986,8 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& WXUNUSED(range), /// Lay the item out bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) { + // ClearLines(); + // Increase the size of the paragraph due to spacing int spaceBeforePara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingBefore()); int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter()); @@ -2066,7 +2105,7 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) // Add a new line wxRichTextLine* line = AllocateLine(lineCount); - + // Set relative range so we won't have to change line ranges when paragraphs are moved line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart())); line->SetPosition(currentPosition); @@ -2192,7 +2231,7 @@ void wxRichTextParagraph::ApplyParagraphStyle(const wxRect& rect) bool wxRichTextParagraph::InsertText(long pos, const wxString& text) { wxRichTextObject* childToUse = NULL; - wxRichTextObjectList::compatibility_iterator nodeToUse = NULL; + wxRichTextObjectList::compatibility_iterator nodeToUse = wxRichTextObjectList::compatibility_iterator(); wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); while (node) @@ -2538,7 +2577,12 @@ wxRichTextObject* wxRichTextParagraph::SplitAt(long pos, wxRichTextObject** prev if (pos == child->GetRange().GetStart()) { if (previousObject) - *previousObject = child; + { + if (node->GetPrevious()) + *previousObject = node->GetPrevious()->GetData(); + else + *previousObject = NULL; + } return child; } @@ -2598,7 +2642,7 @@ void wxRichTextParagraph::MoveToList(wxRichTextObject* obj, wxList& list) /// Add content back from list void wxRichTextParagraph::MoveFromList(wxList& list) { - for (wxNode* node = list.GetFirst(); node; node = node->GetNext()) + for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext()) { AppendChild((wxRichTextObject*) node->GetData()); } @@ -3368,15 +3412,15 @@ bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx& style) /// End the style bool wxRichTextBuffer::EndStyle() { - if (m_attributeStack.GetFirst() == NULL) + if (!m_attributeStack.GetFirst()) { wxLogDebug(_("Too many EndStyle calls!")); return false; } - wxNode* node = m_attributeStack.GetLast(); + wxList::compatibility_iterator node = m_attributeStack.GetLast(); wxTextAttrEx* attr = (wxTextAttrEx*)node->GetData(); - delete node; + m_attributeStack.Erase(node); SetDefaultStyle(*attr); @@ -3395,7 +3439,7 @@ bool wxRichTextBuffer::EndAllStyles() /// Clear the style stack void wxRichTextBuffer::ClearStyleStack() { - for (wxNode* node = m_attributeStack.GetFirst(); node; node = node->GetNext()) + for (wxList::compatibility_iterator node = m_attributeStack.GetFirst(); node; node = node->GetNext()) delete (wxTextAttrEx*) node->GetData(); m_attributeStack.Clear(); } @@ -3783,12 +3827,16 @@ bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, int type) bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range) { bool success = false; +#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ wxString text = GetTextForRange(range); - if (wxTheClipboard->Open()) + if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open()) { success = wxTheClipboard->SetData(new wxTextDataObject(text)); wxTheClipboard->Close(); } +#else + wxUnusedVar(range); +#endif return success; } @@ -3796,6 +3844,7 @@ bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range) bool wxRichTextBuffer::PasteFromClipboard(long position) { bool success = false; +#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ if (CanPasteFromClipboard()) { if (wxTheClipboard->Open()) @@ -3805,6 +3854,7 @@ bool wxRichTextBuffer::PasteFromClipboard(long position) wxTextDataObject data; wxTheClipboard->GetData(data); wxString text(data.GetText()); + text.Replace(_T("\r\n"), _T("\n")); InsertTextWithUndo(position+1, text, GetRichTextCtrl()); @@ -3836,6 +3886,9 @@ bool wxRichTextBuffer::PasteFromClipboard(long position) wxTheClipboard->Close(); } } +#else + wxUnusedVar(position); +#endif return success; } @@ -3843,7 +3896,8 @@ bool wxRichTextBuffer::PasteFromClipboard(long position) bool wxRichTextBuffer::CanPasteFromClipboard() const { bool canPaste = false; - if (wxTheClipboard->Open()) +#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ + if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open()) { if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_BITMAP)) { @@ -3851,6 +3905,7 @@ bool wxRichTextBuffer::CanPasteFromClipboard() const } wxTheClipboard->Close(); } +#endif return canPaste; } @@ -3912,7 +3967,7 @@ void wxRichTextCommand::AddAction(wxRichTextAction* action) bool wxRichTextCommand::Do() { - for (wxNode* node = m_actions.GetFirst(); node; node = node->GetNext()) + for (wxList::compatibility_iterator node = m_actions.GetFirst(); node; node = node->GetNext()) { wxRichTextAction* action = (wxRichTextAction*) node->GetData(); action->Do(); @@ -3923,7 +3978,7 @@ bool wxRichTextCommand::Do() bool wxRichTextCommand::Undo() { - for (wxNode* node = m_actions.GetLast(); node; node = node->GetPrevious()) + for (wxList::compatibility_iterator node = m_actions.GetLast(); node; node = node->GetPrevious()) { wxRichTextAction* action = (wxRichTextAction*) node->GetData(); action->Undo(); @@ -4061,9 +4116,9 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent m_ctrl->SetCaretPosition(caretPosition); if (!m_ctrl->IsFrozen()) { - m_ctrl->Layout(); + m_ctrl->LayoutContent(); m_ctrl->PositionCaret(); - m_ctrl->Refresh(); + m_ctrl->Refresh(false); if (sendUpdateEvent) m_ctrl->SendUpdateEvent(); @@ -4319,23 +4374,23 @@ bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, i (attr1.GetRightIndent() != attr2.GetRightIndent())) return false; - if ((flags && wxTEXT_ATTR_PARA_SPACING_AFTER) && + if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) && (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter())) return false; - if ((flags && wxTEXT_ATTR_PARA_SPACING_BEFORE) && + if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) && (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore())) return false; - if ((flags && wxTEXT_ATTR_LINE_SPACING) && + if ((flags & wxTEXT_ATTR_LINE_SPACING) && (attr1.GetLineSpacing() != attr2.GetLineSpacing())) return false; - if ((flags && wxTEXT_ATTR_CHARACTER_STYLE_NAME) && + if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) && (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName())) return false; - if ((flags && wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) && + if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) && (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName())) return false; @@ -4401,23 +4456,23 @@ bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2, (attr1.GetRightIndent() != attr2.GetRightIndent())) return false; - if ((flags && wxTEXT_ATTR_PARA_SPACING_AFTER) && + if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) && (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter())) return false; - if ((flags && wxTEXT_ATTR_PARA_SPACING_BEFORE) && + if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) && (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore())) return false; - if ((flags && wxTEXT_ATTR_LINE_SPACING) && + if ((flags & wxTEXT_ATTR_LINE_SPACING) && (attr1.GetLineSpacing() != attr2.GetLineSpacing())) return false; - if ((flags && wxTEXT_ATTR_CHARACTER_STYLE_NAME) && + if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) && (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName())) return false; - if ((flags && wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) && + if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) && (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName())) return false; @@ -4736,6 +4791,9 @@ void wxRichTextAttr::CopyTo(wxTextAttrEx& attr) const wxFont wxRichTextAttr::CreateFont() const { wxFont font(m_fontSize, wxDEFAULT, m_fontStyle, m_fontWeight, m_fontUnderlined, m_fontFaceName); +#ifdef __WXMAC__ + font.SetNoAntiAliasing(true); +#endif return font; } @@ -4816,8 +4874,8 @@ bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& f wxFFileInputStream stream(filename); if (stream.Ok()) return LoadFile(buffer, stream); - else - return false; + + return false; } bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename) @@ -4825,8 +4883,8 @@ bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& f wxFFileOutputStream stream(filename); if (stream.Ok()) return SaveFile(buffer, stream); - else - return false; + + return false; } #endif // wxUSE_STREAMS @@ -4859,13 +4917,16 @@ bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStr { int ch = stream.GetC(); - if (ch == 10 && lastCh != 13) - str += wxT('\n'); + if (!stream.Eof()) + { + if (ch == 10 && lastCh != 13) + str += wxT('\n'); - if (ch > 0 && ch != 10) - str += wxChar(ch); + if (ch > 0 && ch != 10) + str += wxChar(ch); - lastCh = ch; + lastCh = ch; + } } buffer->Clear(); @@ -4922,8 +4983,7 @@ void wxRichTextImageBlock::Init() void wxRichTextImageBlock::Clear() { - if (m_data) - delete m_data; + delete[] m_data; m_data = NULL; m_dataSize = 0; m_imageType = -1; @@ -5157,4 +5217,3 @@ bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* b #endif // wxUSE_RICHTEXT -