X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/44cc96a80c09c8572e1315be66d29f6d610cbf78..d58526d55433162cef8ad2ed7f422689862c3019:/src/richtext/richtextbuffer.cpp diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index dc5a764031..82c529fa11 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -28,6 +28,7 @@ #include "wx/module.h" #endif +#include "wx/settings.h" #include "wx/filename.h" #include "wx/clipbrd.h" #include "wx/wfstream.h" @@ -47,8 +48,57 @@ WX_DEFINE_LIST(wxRichTextLineList) // Switch off if the platform doesn't like it for some reason #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1 +// Use GetPartialTextExtents for platforms that support it natively +#define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1 + const wxChar wxRichTextLineBreakChar = (wxChar) 29; +// Helpers for efficiency + +inline void wxCheckSetFont(wxDC& dc, const wxFont& font) +{ +#if 0 + const wxFont& font1 = dc.GetFont(); + if (font1.IsOk() && font.IsOk()) + { + if (font1.GetPointSize() == font.GetPointSize() && + font1.GetFamily() == font.GetFamily() && + font1.GetStyle() == font.GetStyle() && + font1.GetWeight() == font.GetWeight() && + font1.GetUnderlined() == font.GetUnderlined() && + font1.GetFamily() == font.GetFamily() && + font1.GetFaceName() == font.GetFaceName()) + return; + } +#endif + dc.SetFont(font); +} + +inline void wxCheckSetPen(wxDC& dc, const wxPen& pen) +{ + const wxPen& pen1 = dc.GetPen(); + if (pen1.IsOk() && pen.IsOk()) + { + if (pen1.GetWidth() == pen.GetWidth() && + pen1.GetStyle() == pen.GetStyle() && + pen1.GetColour() == pen.GetColour()) + return; + } + dc.SetPen(pen); +} + +inline void wxCheckSetBrush(wxDC& dc, const wxBrush& brush) +{ + const wxBrush& brush1 = dc.GetBrush(); + if (brush1.IsOk() && brush.IsOk()) + { + if (brush1.GetStyle() == brush.GetStyle() && + brush1.GetColour() == brush.GetColour()) + return; + } + dc.SetBrush(brush); +} + /*! * wxRichTextObject * This is the base for drawable objects. @@ -265,7 +315,8 @@ int wxRichTextCompositeObject::HitTest(wxDC& dc, const wxPoint& pt, long& textPo node = node->GetNext(); } - return wxRICHTEXT_HITTEST_NONE; + textPosition = GetRange().GetEnd()-1; + return wxRICHTEXT_HITTEST_AFTER|wxRICHTEXT_HITTEST_OUTSIDE; } /// Finds the absolute position and row height for the given character position @@ -377,26 +428,31 @@ wxString wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange& range } /// Recursively merge all pieces that can be merged. -bool wxRichTextCompositeObject::Defragment() +bool wxRichTextCompositeObject::Defragment(const wxRichTextRange& range) { wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); while (node) { wxRichTextObject* child = node->GetData(); - wxRichTextCompositeObject* composite = wxDynamicCast(child, wxRichTextCompositeObject); - if (composite) - composite->Defragment(); - - if (node->GetNext()) + if (range == wxRICHTEXT_ALL || !child->GetRange().IsOutside(range)) { - wxRichTextObject* nextChild = node->GetNext()->GetData(); - if (child->CanMerge(nextChild) && child->Merge(nextChild)) + wxRichTextCompositeObject* composite = wxDynamicCast(child, wxRichTextCompositeObject); + if (composite) + composite->Defragment(); + + if (node->GetNext()) { - nextChild->Dereference(); - m_children.Erase(node->GetNext()); + wxRichTextObject* nextChild = node->GetNext()->GetData(); + if (child->CanMerge(nextChild) && child->Merge(nextChild)) + { + nextChild->Dereference(); + m_children.Erase(node->GetNext()); - // Don't set node -- we'll see if we can merge again with the next - // child. + // Don't set node -- we'll see if we can merge again with the next + // child. + } + else + node = node->GetNext(); } else node = node->GetNext(); @@ -465,13 +521,13 @@ bool wxRichTextBox::Layout(wxDC& dc, const wxRect& rect, int style) } /// Get/set the size for the given range. Assume only has one child. -bool wxRichTextBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const +bool wxRichTextBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position, wxArrayInt* partialExtents) const { wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); if (node) { wxRichTextObject* child = node->GetData(); - return child->GetRangeSize(range, size, descent, dc, flags, position); + return child->GetRangeSize(range, size, descent, dc, flags, position, partialExtents); } else return false; @@ -536,7 +592,7 @@ bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, const wxRichTextRange& range, // Skip } else - child->Draw(dc, range, selectionRange, childRect, descent, style); + child->Draw(dc, range, selectionRange, rect, descent, style); } node = node->GetNext(); @@ -594,7 +650,7 @@ bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int styl if (invalidRange == wxRICHTEXT_ALL) layoutAll = true; else // If we know what range is affected, start laying out from that point on. - if (invalidRange.GetStart() > GetRange().GetStart()) + if (invalidRange.GetStart() >= GetRange().GetStart()) { wxRichTextParagraph* firstParagraph = GetParagraphAtPosition(invalidRange.GetStart()); if (firstParagraph) @@ -603,10 +659,13 @@ bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int styl wxRichTextObjectList::compatibility_iterator previousNode; if ( firstNode ) previousNode = firstNode->GetPrevious(); - if (firstNode && previousNode) + if (firstNode) { - wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph); - availableSpace.y = previousParagraph->GetPosition().y + previousParagraph->GetCachedSize().y; + if (previousNode) + { + wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph); + availableSpace.y = previousParagraph->GetPosition().y + previousParagraph->GetCachedSize().y; + } // Now we're going to start iterating from the first affected paragraph. node = firstNode; @@ -693,7 +752,7 @@ void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox& obj) } /// Get/set the size for the given range. -bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const +bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position, wxArrayInt* WXUNUSED(partialExtents)) const { wxSize sz; @@ -801,25 +860,29 @@ wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool c wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); while (node) { - // child is a paragraph - wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); - wxASSERT (child != NULL); - - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) + wxRichTextObject* obj = (wxRichTextObject*) node->GetData(); + if (obj->GetRange().Contains(pos)) { - wxRichTextLine* line = node2->GetData(); + // child is a paragraph + wxRichTextParagraph* child = wxDynamicCast(obj, wxRichTextParagraph); + wxASSERT (child != NULL); - wxRichTextRange range = line->GetAbsoluteRange(); + wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); + while (node2) + { + wxRichTextLine* line = node2->GetData(); - if (range.Contains(pos) || + wxRichTextRange range = line->GetAbsoluteRange(); - // If the position is end-of-paragraph, then return the last line of - // of the paragraph. - (range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd())) - return line; + if (range.Contains(pos) || - node2 = node2->GetNext(); + // If the position is end-of-paragraph, then return the last line of + // of the paragraph. + ((range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd()))) + return line; + + node2 = node2->GetNext(); + } } node = node->GetNext(); @@ -912,7 +975,17 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraph(const wxString& text, wxTextAttr defaultCharStyle; wxTextAttr defaultParaStyle; - wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle); + // If the default style is a named paragraph style, don't apply any character formatting + // to the initial text string. + if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet()) + { + wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName()); + if (def) + defaultParaStyle = def->GetStyleMergedWithBase(GetStyleSheet()); + } + else + wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle); + wxTextAttr* pStyle = paraStyle ? paraStyle : (wxTextAttr*) & defaultParaStyle; wxTextAttr* cStyle = & defaultCharStyle; @@ -935,7 +1008,17 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text wxTextAttr defaultCharStyle; wxTextAttr defaultParaStyle; - wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle); + + // If the default style is a named paragraph style, don't apply any character formatting + // to the initial text string. + if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet()) + { + wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName()); + if (def) + defaultParaStyle = def->GetStyleMergedWithBase(GetStyleSheet()); + } + else + wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle); wxTextAttr* pStyle = paraStyle ? paraStyle : (wxTextAttr*) & defaultParaStyle; wxTextAttr* cStyle = & defaultCharStyle; @@ -960,15 +1043,18 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text wxChar ch = text[i]; if (ch == wxT('\n') || ch == wxT('\r')) { - wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData(); - plainText->SetText(line); + if (i != (len-1)) + { + wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData(); + plainText->SetText(line); - para = new wxRichTextParagraph(wxEmptyString, this, pStyle, cStyle); + para = new wxRichTextParagraph(wxEmptyString, this, pStyle, cStyle); - AppendChild(para); + AppendChild(para); - lastPara = para; - line = wxEmptyString; + lastPara = para; + line = wxEmptyString; + } } else line += ch; @@ -998,7 +1084,17 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddImage(const wxImage& image, wxT wxTextAttr defaultCharStyle; wxTextAttr defaultParaStyle; - wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle); + + // If the default style is a named paragraph style, don't apply any character formatting + // to the initial text string. + if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet()) + { + wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName()); + if (def) + defaultParaStyle = def->GetStyleMergedWithBase(GetStyleSheet()); + } + else + wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle); wxTextAttr* pStyle = paraStyle ? paraStyle : (wxTextAttr*) & defaultParaStyle; wxTextAttr* cStyle = & defaultCharStyle; @@ -1026,6 +1122,8 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag wxRichTextParagraph* para = GetParagraphAtPosition(position); if (para) { + wxTextAttrEx originalAttr = para->GetAttributes(); + wxRichTextObjectList::compatibility_iterator node = m_children.Find(para); // Now split at this position, returning the object to insert the new @@ -1046,11 +1144,6 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph); wxASSERT (firstPara != NULL); - // Apply the new paragraph attributes to the existing paragraph - wxTextAttr attr(para->GetAttributes()); - wxRichTextApplyStyle(attr, firstPara->GetAttributes()); - para->SetAttributes(attr); - wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst(); while (objectNode) { @@ -1098,7 +1191,19 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph); wxASSERT(firstPara != NULL); + if (!(fragment.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE)) + para->SetAttributes(firstPara->GetAttributes()); + + // Save empty paragraph attributes for appending later + // These are character attributes deliberately set for a new paragraph. Without this, + // we couldn't pass default attributes when appending a new paragraph. + wxTextAttrEx emptyParagraphAttributes; + wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst(); + + if (objectNode && firstPara->GetChildren().GetCount() == 1 && objectNode->GetData()->IsEmpty()) + emptyParagraphAttributes = objectNode->GetData()->GetAttributes(); + while (objectNode) { wxRichTextObject* newObj = objectNode->GetData()->Clone(); @@ -1118,50 +1223,57 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst()->GetNext(); wxRichTextParagraph* finalPara = para; + bool needExtraPara = (!i || !fragment.GetPartialParagraph()); + // If there was only one paragraph, we need to insert a new one. - if (!i) + while (i) { - finalPara = new wxRichTextParagraph; + wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph); + wxASSERT( para != NULL ); - // TODO: These attributes should come from the subsequent paragraph - // when originally deleted, since the subsequent para takes on - // the previous para's attributes. - finalPara->SetAttributes(firstPara->GetAttributes()); + finalPara = (wxRichTextParagraph*) para->Clone(); if (nextParagraph) InsertChild(finalPara, nextParagraph); else AppendChild(finalPara); + + i = i->GetNext(); } - else while (i) - { - wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph); - wxASSERT( para != NULL ); - finalPara = (wxRichTextParagraph*) para->Clone(); + // If there was only one paragraph, or we have full paragraphs in our fragment, + // we need to insert a new one. + if (needExtraPara) + { + finalPara = new wxRichTextParagraph; if (nextParagraph) InsertChild(finalPara, nextParagraph); else AppendChild(finalPara); - - i = i->GetNext(); } // 4. Add back the remaining content. if (finalPara) { - finalPara->MoveFromList(savedObjects); + if (nextObject) + finalPara->MoveFromList(savedObjects); // Ensure there's at least one object if (finalPara->GetChildCount() == 0) { wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString); + text->SetAttributes(emptyParagraphAttributes); finalPara->AppendChild(text); } } + if ((fragment.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE) && firstPara) + finalPara->SetAttributes(firstPara->GetAttributes()); + else if (finalPara && finalPara != para) + finalPara->SetAttributes(originalAttr); + return true; } } @@ -1345,6 +1457,7 @@ bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range) { wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); + wxRichTextParagraph* firstPara = NULL; while (node) { wxRichTextParagraph* obj = wxDynamicCast(node->GetData(), wxRichTextParagraph); @@ -1359,58 +1472,82 @@ bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range) // Deletes the content of this object within the given range obj->DeleteRange(range); + wxRichTextRange thisRange = obj->GetRange(); + wxTextAttrEx thisAttr = obj->GetAttributes(); + // If the whole paragraph is within the range to delete, // delete the whole thing. - if (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd()) + if (range.GetStart() <= thisRange.GetStart() && range.GetEnd() >= thisRange.GetEnd()) { // Delete the whole object RemoveChild(obj, true); + obj = NULL; } + else if (!firstPara) + firstPara = obj; + // If the range includes the paragraph end, we need to join this // and the next paragraph. - else if (range.Contains(obj->GetRange().GetEnd())) + if (range.GetEnd() <= thisRange.GetEnd()) { // We need to move the objects from the next paragraph // to this paragraph - if (next) + wxRichTextParagraph* nextParagraph = NULL; + if ((range.GetEnd() < thisRange.GetEnd()) && obj) + nextParagraph = obj; + else { - wxRichTextParagraph* nextParagraph = wxDynamicCast(next->GetData(), wxRichTextParagraph); - next = next->GetNext(); - if (nextParagraph) - { - // Delete the stuff we need to delete - nextParagraph->DeleteRange(range); + // We're ending at the end of the paragraph, so merge the _next_ paragraph. + if (next) + nextParagraph = wxDynamicCast(next->GetData(), wxRichTextParagraph); + } - // Move the objects to the previous para - wxRichTextObjectList::compatibility_iterator node1 = nextParagraph->GetChildren().GetFirst(); + bool applyFinalParagraphStyle = firstPara && nextParagraph && nextParagraph != firstPara; - while (node1) - { - wxRichTextObject* obj1 = node1->GetData(); + wxTextAttrEx nextParaAttr; + if (applyFinalParagraphStyle) + { + // Special case when deleting the end of a paragraph - use _this_ paragraph's style, + // not the next one. + if (range.GetStart() == range.GetEnd() && range.GetStart() == thisRange.GetEnd()) + nextParaAttr = thisAttr; + else + nextParaAttr = nextParagraph->GetAttributes(); + } - // If the object is empty, optimise it out - if (obj1->IsEmpty()) - { - delete obj1; - } - else - { - obj->AppendChild(obj1); - } + if (firstPara && nextParagraph && firstPara != nextParagraph) + { + // Move the objects to the previous para + wxRichTextObjectList::compatibility_iterator node1 = nextParagraph->GetChildren().GetFirst(); - wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext(); - nextParagraph->GetChildren().Erase(node1); + while (node1) + { + wxRichTextObject* obj1 = node1->GetData(); - node1 = next1; - } + firstPara->AppendChild(obj1); - // Delete the paragraph - RemoveChild(nextParagraph, true); + wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext(); + nextParagraph->GetChildren().Erase(node1); + node1 = next1; } + + // Delete the paragraph + RemoveChild(nextParagraph, true); + } + + // Avoid empty paragraphs + if (firstPara && firstPara->GetChildren().GetCount() == 0) + { + wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString); + firstPara->AppendChild(text); } + if (applyFinalParagraphStyle) + firstPara->SetAttributes(nextParaAttr); + + return true; } } @@ -1662,8 +1799,7 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const // we only want the paragraphs to hold this character style, then we _don't_ want to // apply the character style. So we need to be able to choose. - // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd()) - if (!parasOnly && characterStyle && range.GetStart() != newPara->GetRange().GetEnd()) + if (!parasOnly && (characterStyle|charactersOnly) && range.GetStart() != newPara->GetRange().GetEnd()) { wxRichTextRange childRange(range); childRange.LimitTo(newPara->GetRange()); @@ -1686,7 +1822,7 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const splitPoint ++; // Find last object - if (splitPoint == newPara->GetRange().GetEnd() || splitPoint == (newPara->GetRange().GetEnd() - 1)) + if (splitPoint == newPara->GetRange().GetEnd()) lastObject = newPara->GetChildren().GetLast()->GetData(); else // lastObject is set as a side-effect of splitting. It's @@ -1812,11 +1948,14 @@ static bool wxHasStyle(long flags, long style) /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of /// content. -bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const wxTextAttr& style, long& multipleStyleAttributes, int& multipleTextEffectAttributes) +bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const wxTextAttr& style, long& multipleStyleAttributes, int& multipleTextEffectAttributes, int& absentStyleAttributes, int& absentTextEffectAttributes) { + absentStyleAttributes |= (~style.GetFlags() & wxTEXT_ATTR_ALL); + absentTextEffectAttributes |= (~style.GetTextEffectFlags() & 0xFFFF); + if (style.HasFont()) { - if (style.HasFontSize() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_SIZE)) + if (style.HasFontSize() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_FONT_SIZE)) { if (currentStyle.HasFontSize()) { @@ -1833,7 +1972,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasFontItalic() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_ITALIC)) + if (style.HasFontItalic() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_FONT_ITALIC)) { if (currentStyle.HasFontItalic()) { @@ -1850,7 +1989,24 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasFontWeight() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_WEIGHT)) + if (style.HasFontFamily() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_FONT_FAMILY)) + { + if (currentStyle.HasFontFamily()) + { + if (currentStyle.GetFontFamily() != style.GetFontFamily()) + { + // Clash of style - mark as such + multipleStyleAttributes |= wxTEXT_ATTR_FONT_FAMILY; + currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_FONT_FAMILY); + } + } + else + { + currentStyle.SetFontFamily(style.GetFontFamily()); + } + } + + if (style.HasFontWeight() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_FONT_WEIGHT)) { if (currentStyle.HasFontWeight()) { @@ -1867,7 +2023,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_FACE)) + if (style.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_FONT_FACE)) { if (currentStyle.HasFontFaceName()) { @@ -1887,7 +2043,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_UNDERLINE)) + if (style.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_FONT_UNDERLINE)) { if (currentStyle.HasFontUnderlined()) { @@ -1905,7 +2061,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasTextColour() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_TEXT_COLOUR)) + if (style.HasTextColour() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_TEXT_COLOUR)) { if (currentStyle.HasTextColour()) { @@ -1920,7 +2076,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetTextColour(style.GetTextColour()); } - if (style.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BACKGROUND_COLOUR)) + if (style.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_BACKGROUND_COLOUR)) { if (currentStyle.HasBackgroundColour()) { @@ -1935,7 +2091,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetBackgroundColour(style.GetBackgroundColour()); } - if (style.HasAlignment() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_ALIGNMENT)) + if (style.HasAlignment() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_ALIGNMENT)) { if (currentStyle.HasAlignment()) { @@ -1950,7 +2106,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetAlignment(style.GetAlignment()); } - if (style.HasTabs() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_TABS)) + if (style.HasTabs() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_TABS)) { if (currentStyle.HasTabs()) { @@ -1965,7 +2121,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetTabs(style.GetTabs()); } - if (style.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_LEFT_INDENT)) + if (style.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_LEFT_INDENT)) { if (currentStyle.HasLeftIndent()) { @@ -1980,7 +2136,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent()); } - if (style.HasRightIndent() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_RIGHT_INDENT)) + if (style.HasRightIndent() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_RIGHT_INDENT)) { if (currentStyle.HasRightIndent()) { @@ -1995,7 +2151,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetRightIndent(style.GetRightIndent()); } - if (style.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_PARA_SPACING_AFTER)) + if (style.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_PARA_SPACING_AFTER)) { if (currentStyle.HasParagraphSpacingAfter()) { @@ -2010,7 +2166,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter()); } - if (style.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_PARA_SPACING_BEFORE)) + if (style.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_PARA_SPACING_BEFORE)) { if (currentStyle.HasParagraphSpacingBefore()) { @@ -2025,7 +2181,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore()); } - if (style.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_LINE_SPACING)) + if (style.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_LINE_SPACING)) { if (currentStyle.HasLineSpacing()) { @@ -2040,7 +2196,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetLineSpacing(style.GetLineSpacing()); } - if (style.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_CHARACTER_STYLE_NAME)) + if (style.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_CHARACTER_STYLE_NAME)) { if (currentStyle.HasCharacterStyleName()) { @@ -2055,7 +2211,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetCharacterStyleName(style.GetCharacterStyleName()); } - if (style.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME)) + if (style.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME)) { if (currentStyle.HasParagraphStyleName()) { @@ -2070,7 +2226,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetParagraphStyleName(style.GetParagraphStyleName()); } - if (style.HasListStyleName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_LIST_STYLE_NAME)) + if (style.HasListStyleName() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_LIST_STYLE_NAME)) { if (currentStyle.HasListStyleName()) { @@ -2085,7 +2241,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetListStyleName(style.GetListStyleName()); } - if (style.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_STYLE)) + if (style.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_BULLET_STYLE)) { if (currentStyle.HasBulletStyle()) { @@ -2100,7 +2256,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetBulletStyle(style.GetBulletStyle()); } - if (style.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_NUMBER)) + if (style.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_BULLET_NUMBER)) { if (currentStyle.HasBulletNumber()) { @@ -2115,7 +2271,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetBulletNumber(style.GetBulletNumber()); } - if (style.HasBulletText() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_TEXT)) + if (style.HasBulletText() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_BULLET_TEXT)) { if (currentStyle.HasBulletText()) { @@ -2133,7 +2289,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasBulletName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_NAME)) + if (style.HasBulletName() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_BULLET_NAME)) { if (currentStyle.HasBulletName()) { @@ -2150,7 +2306,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasURL() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_URL)) + if (style.HasURL() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_URL)) { if (currentStyle.HasURL()) { @@ -2167,13 +2323,17 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const } } - if (style.HasTextEffects() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_EFFECTS)) + if (style.HasTextEffects() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_EFFECTS)) { if (currentStyle.HasTextEffects()) { // We need to find the bits in the new style that are different: // just look at those bits that are specified by the new style. + // We need to remove the bits and flags that are not common between current style + // and new style. In so doing we need to take account of the styles absent from one or more of the + // previous styles. + int currentRelevantTextEffects = currentStyle.GetTextEffects() & style.GetTextEffectFlags(); int newRelevantTextEffects = style.GetTextEffects() & style.GetTextEffectFlags(); @@ -2192,9 +2352,17 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr& currentStyle, const currentStyle.SetTextEffects(style.GetTextEffects()); currentStyle.SetTextEffectFlags(style.GetTextEffectFlags()); } + + // Mask out the flags and values that cannot be common because they were absent in one or more objecrs + // that we've looked at so far + currentStyle.SetTextEffects(currentStyle.GetTextEffects() & ~absentTextEffectAttributes); + currentStyle.SetTextEffectFlags(currentStyle.GetTextEffectFlags() & ~absentTextEffectAttributes); + + if (currentStyle.GetTextEffectFlags() == 0) + currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_EFFECTS); } - if (style.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_OUTLINE_LEVEL)) + if (style.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes|absentStyleAttributes, wxTEXT_ATTR_OUTLINE_LEVEL)) { if (currentStyle.HasOutlineLevel()) { @@ -2224,6 +2392,11 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range long multipleStyleAttributes = 0; int multipleTextEffectAttributes = 0; + int absentStyleAttributesPara = 0; + int absentStyleAttributesChar = 0; + int absentTextEffectAttributesPara = 0; + int absentTextEffectAttributesChar = 0; + wxRichTextObjectList::compatibility_iterator node = GetChildren().GetFirst(); while (node) { @@ -2234,7 +2407,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range { wxTextAttr paraStyle = para->GetCombinedAttributes(); - CollectStyle(style, paraStyle, multipleStyleAttributes, multipleTextEffectAttributes); + CollectStyle(style, paraStyle, multipleStyleAttributes, multipleTextEffectAttributes, absentStyleAttributesPara, absentTextEffectAttributesPara); } else { @@ -2244,7 +2417,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range // First collect paragraph attributes only wxTextAttr paraStyle = para->GetCombinedAttributes(); paraStyle.SetFlags(paraStyle.GetFlags() & wxTEXT_ATTR_PARAGRAPH); - CollectStyle(style, paraStyle, multipleStyleAttributes, multipleTextEffectAttributes); + CollectStyle(style, paraStyle, multipleStyleAttributes, multipleTextEffectAttributes, absentStyleAttributesPara, absentTextEffectAttributesPara); wxRichTextObjectList::compatibility_iterator childNode = para->GetChildren().GetFirst(); @@ -2258,7 +2431,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range // Now collect character attributes only childStyle.SetFlags(childStyle.GetFlags() & wxTEXT_ATTR_CHARACTER); - CollectStyle(style, childStyle, multipleStyleAttributes, multipleTextEffectAttributes); + CollectStyle(style, childStyle, multipleStyleAttributes, multipleTextEffectAttributes, absentStyleAttributesChar, absentTextEffectAttributesChar); } childNode = childNode->GetNext(); @@ -2372,6 +2545,15 @@ void wxRichTextParagraphLayoutBox::Reset() { Clear(); + wxRichTextBuffer* buffer = wxDynamicCast(this, wxRichTextBuffer); + if (buffer && GetRichTextCtrl()) + { + wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, GetRichTextCtrl()->GetId()); + event.SetEventObject(GetRichTextCtrl()); + + buffer->SendEvent(event, true); + } + AddParagraph(wxEmptyString); Invalidate(wxRICHTEXT_ALL); @@ -2427,6 +2609,29 @@ bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSh int foundCount = 0; + wxRichTextAttr attr(GetBasicStyle()); + if (GetBasicStyle().HasParagraphStyleName()) + { + wxRichTextParagraphStyleDefinition* paraDef = styleSheet->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName()); + if (paraDef) + { + attr.Apply(paraDef->GetStyleMergedWithBase(styleSheet)); + SetBasicStyle(attr); + foundCount ++; + } + } + + if (GetBasicStyle().HasCharacterStyleName()) + { + wxRichTextCharacterStyleDefinition* charDef = styleSheet->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName()); + if (charDef) + { + attr.Apply(charDef->GetStyleMergedWithBase(styleSheet)); + SetBasicStyle(attr); + foundCount ++; + } + } + wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); while (node) { @@ -2649,7 +2854,7 @@ bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange& range, co bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0); // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0); -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL bool specifyLevel = ((flags & wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL) != 0); #endif @@ -2943,7 +3148,7 @@ wxRichTextParagraph::~wxRichTextParagraph() } /// Draw the item -bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int WXUNUSED(descent), int style) +bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int WXUNUSED(descent), int style) { wxTextAttr attr = GetCombinedAttributes(); @@ -2968,7 +3173,7 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR } // Get line height from first line, if any - wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : (wxRichTextLine*) NULL; + wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : NULL; wxPoint linePos; int lineHeight wxDUMMY_INITIALIZE(0); @@ -2985,7 +3190,7 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR else font = (*wxNORMAL_FONT); - dc.SetFont(font); + wxCheckSetFont(dc, font); lineHeight = dc.GetCharHeight(); linePos = GetPosition(); @@ -3022,40 +3227,56 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR wxRichTextLine* line = node->GetData(); wxRichTextRange lineRange = line->GetAbsoluteRange(); - int maxDescent = line->GetDescent(); - // Lines are specified relative to the paragraph wxPoint linePosition = line->GetPosition() + GetPosition(); - wxPoint objectPosition = linePosition; - // Loop through objects until we get to the one within range - wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst(); - while (node2) + // Don't draw if off the screen + if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) != 0) || ((linePosition.y + line->GetSize().y) >= rect.y && linePosition.y <= rect.y + rect.height)) { - wxRichTextObject* child = node2->GetData(); + wxPoint objectPosition = linePosition; + int maxDescent = line->GetDescent(); - if (!child->GetRange().IsOutside(lineRange) && !lineRange.IsOutside(range)) + // Loop through objects until we get to the one within range + wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst(); + + int i = 0; + while (node2) { - // Draw this part of the line at the correct position - wxRichTextRange objectRange(child->GetRange()); - objectRange.LimitTo(lineRange); + wxRichTextObject* child = node2->GetData(); - wxSize objectSize; - int descent = 0; - child->GetRangeSize(objectRange, objectSize, descent, dc, wxRICHTEXT_UNFORMATTED, objectPosition); + if (child->GetRange().GetLength() > 0 && !child->GetRange().IsOutside(lineRange) && !lineRange.IsOutside(range)) + { + // Draw this part of the line at the correct position + wxRichTextRange objectRange(child->GetRange()); + objectRange.LimitTo(lineRange); - // Use the child object's width, but the whole line's height - wxRect childRect(objectPosition, wxSize(objectSize.x, line->GetSize().y)); - child->Draw(dc, objectRange, selectionRange, childRect, maxDescent, style); + wxSize objectSize; +#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS + if (i < (int) line->GetObjectSizes().GetCount()) + { + objectSize.x = line->GetObjectSizes()[(size_t) i]; + } + else +#endif + { + int descent = 0; + child->GetRangeSize(objectRange, objectSize, descent, dc, wxRICHTEXT_UNFORMATTED, objectPosition); + } - objectPosition.x += objectSize.x; - } - else if (child->GetRange().GetStart() > lineRange.GetEnd()) - // Can break out of inner loop now since we've passed this line's range - break; + // Use the child object's width, but the whole line's height + wxRect childRect(objectPosition, wxSize(objectSize.x, line->GetSize().y)); + child->Draw(dc, objectRange, selectionRange, childRect, maxDescent, style); - node2 = node2->GetNext(); + objectPosition.x += objectSize.x; + i ++; + } + else if (child->GetRange().GetStart() > lineRange.GetEnd()) + // Can break out of inner loop now since we've passed this line's range + break; + + node2 = node2->GetNext(); + } } node = node->GetNext(); @@ -3064,6 +3285,25 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR return true; } +// Get the range width using partial extents calculated for the whole paragraph. +static int wxRichTextGetRangeWidth(const wxRichTextParagraph& para, const wxRichTextRange& range, const wxArrayInt& partialExtents) +{ + wxASSERT(partialExtents.GetCount() >= (size_t) range.GetLength()); + + if (partialExtents.GetCount() < (size_t) range.GetLength()) + return 0; + + int leftMostPos = 0; + if (range.GetStart() - para.GetRange().GetStart() > 0) + leftMostPos = partialExtents[range.GetStart() - para.GetRange().GetStart() - 1]; + + int rightMostPos = partialExtents[range.GetEnd() - para.GetRange().GetStart()]; + + int w = rightMostPos - leftMostPos; + + return w; +} + /// Lay the item out bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) { @@ -3084,7 +3324,7 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) if (attr.GetLineSpacing() != 10 && GetBuffer()) { wxFont font(GetBuffer()->GetFontTable().FindFont(attr)); - dc.SetFont(font); + wxCheckSetFont(dc, font); lineSpacing = (ConvertTenthsMMToPixels(dc, dc.GetCharHeight()) * attr.GetLineSpacing())/10; } @@ -3119,17 +3359,48 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) int lineCount = 0; + wxRichTextObjectList::compatibility_iterator node; + +#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS + wxUnusedVar(style); + wxArrayInt partialExtents; + + wxSize paraSize; + int paraDescent; + + // This calculates the partial text extents + GetRangeSize(GetRange(), paraSize, paraDescent, dc, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_CACHE_SIZE, wxPoint(0,0), & partialExtents); +#else + node = m_children.GetFirst(); + while (node) + { + wxRichTextObject* child = node->GetData(); + + child->SetCachedSize(wxDefaultSize); + child->Layout(dc, rect, style); + + node = node->GetNext(); + } + +#endif + // Split up lines // We may need to go back to a previous child, in which case create the new line, // find the child corresponding to the start position of the string, and // continue. - wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); + node = m_children.GetFirst(); while (node) { wxRichTextObject* child = node->GetData(); + if (child->GetRange().GetLength() == 0) + { + node = node->GetNext(); + continue; + } + // If this is e.g. a composite text box, it will need to be laid out itself. // But if just a text fragment or image, for example, this will // do nothing. NB: won't we need to set the position after layout? @@ -3137,9 +3408,6 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) // can't tell the position until the size is determined. So possibly introduce // another layout phase. - // TODO: can't this be called only once per child? - child->Layout(dc, rect, style); - // Available width depends on whether we're on the first or subsequent lines int availableSpaceForText = (lineCount == 0 ? availableTextSpaceFirstLine : availableTextSpaceSubsequentLines); @@ -3165,7 +3433,15 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) childDescent = child->GetDescent(); } else + { +#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS + // Get height only, then the width using the partial extents + GetRangeSize(wxRichTextRange(lastEndPos+1, lastPosToUse), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_HEIGHT_ONLY); + childSize.x = wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos+1, lastPosToUse), partialExtents); +#else GetRangeSize(wxRichTextRange(lastEndPos+1, lastPosToUse), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED, rect.GetPosition()); +#endif + } // Cases: // 1) There was a line break BEFORE the natural break @@ -3179,7 +3455,7 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) // Find a place to wrap. This may walk back to previous children, // for example if a word spans several objects. - if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos+1, child->GetRange().GetEnd()), dc, availableSpaceForText, wrapPosition)) + if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos+1, child->GetRange().GetEnd()), dc, availableSpaceForText, wrapPosition, & partialExtents)) { // If the function failed, just cut it off at the end of this child. wrapPosition = child->GetRange().GetEnd(); @@ -3194,7 +3470,19 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) // Let's find the actual size of the current line now wxSize actualSize; wxRichTextRange actualRange(lastCompletedEndPos+1, wrapPosition); + + /// Use previous descent, not the wrapping descent we just found, since this may be too big + /// for the fragment we're about to add. + childDescent = maxDescent; + +#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS + // Get height only, then the width using the partial extents + GetRangeSize(actualRange, actualSize, childDescent, dc, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_HEIGHT_ONLY); + actualSize.x = wxRichTextGetRangeWidth(*this, actualRange, partialExtents); +#else GetRangeSize(actualRange, actualSize, childDescent, dc, wxRICHTEXT_UNFORMATTED); +#endif + currentWidth = actualSize.x; lineHeight = wxMax(lineHeight, actualSize.y); maxDescent = wxMax(childDescent, maxDescent); @@ -3264,7 +3552,7 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) if (lineHeight == 0 && GetBuffer()) { wxFont font(GetBuffer()->GetFontTable().FindFont(attr)); - dc.SetFont(font); + wxCheckSetFont(dc, font); lineHeight = dc.GetCharHeight(); } if (maxDescent == 0) @@ -3284,17 +3572,59 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) ClearUnusedLines(lineCount); // Apply styles to wrapped lines - ApplyParagraphStyle(attr, rect); + ApplyParagraphStyle(attr, rect, dc); SetCachedSize(wxSize(maxWidth, currentPosition.y + spaceBeforePara + spaceAfterPara)); m_dirty = false; +#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS +#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING + // Use the text extents to calculate the size of each fragment in each line + wxRichTextLineList::compatibility_iterator lineNode = m_cachedLines.GetFirst(); + while (lineNode) + { + wxRichTextLine* line = lineNode->GetData(); + wxRichTextRange lineRange = line->GetAbsoluteRange(); + + // Loop through objects until we get to the one within range + wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst(); + + while (node2) + { + wxRichTextObject* child = node2->GetData(); + + if (child->GetRange().GetLength() > 0 && !child->GetRange().IsOutside(lineRange)) + { + wxRichTextRange rangeToUse = lineRange; + rangeToUse.LimitTo(child->GetRange()); + + // Find the size of the child from the text extents, and store in an array + // for drawing later + int left = 0; + if (rangeToUse.GetStart() > GetRange().GetStart()) + left = partialExtents[(rangeToUse.GetStart()-1) - GetRange().GetStart()]; + int right = partialExtents[rangeToUse.GetEnd() - GetRange().GetStart()]; + int sz = right - left; + line->GetObjectSizes().Add(sz); + } + else if (child->GetRange().GetStart() > lineRange.GetEnd()) + // Can break out of inner loop now since we've passed this line's range + break; + + node2 = node2->GetNext(); + } + + lineNode = lineNode->GetNext(); + } +#endif +#endif + return true; } /// Apply paragraph styles, such as centering, to wrapped lines -void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr& attr, const wxRect& rect) +void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr& attr, const wxRect& rect, wxDC& dc) { if (!attr.HasAlignment()) return; @@ -3310,12 +3640,18 @@ void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr& attr, const wxRe // centering, right-justification if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE) { - pos.x = (rect.GetWidth() - size.x)/2 + pos.x; + int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent()); + pos.x = (rect.GetWidth() - (pos.x - rect.x) - rightIndent - size.x)/2 + pos.x; + // Lines are relative to the paragraph position + pos.x -= GetPosition().x; line->SetPosition(pos); } else if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT) { - pos.x = pos.x + rect.GetWidth() - size.x; + int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent()); + pos.x = rect.x + rect.GetWidth() - size.x - rightIndent; + // Lines are relative to the paragraph position + pos.x -= GetPosition().x; line->SetPosition(pos); } @@ -3406,7 +3742,7 @@ void wxRichTextParagraph::ClearLines() /// Get/set the object size for the given range. Returns false if the range /// is invalid for this object. -bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const +bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position, wxArrayInt* partialExtents) const { if (!range.IsWithin(GetRange())) return false; @@ -3418,9 +3754,17 @@ bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& siz wxSize sz; + wxArrayInt childExtents; + wxArrayInt* p; + if (partialExtents) + p = & childExtents; + else + p = NULL; + wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); while (node) { + wxRichTextObject* child = node->GetData(); if (!child->GetRange().IsOutside(range)) { @@ -3430,12 +3774,47 @@ bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& siz rangeToUse.LimitTo(child->GetRange()); int childDescent = 0; - if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, wxPoint(position.x + sz.x, position.y))) + // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size, + // but it's only going to be used after caching has taken place. + if ((flags & wxRICHTEXT_HEIGHT_ONLY) && child->GetCachedSize().y != 0) + { + childDescent = child->GetDescent(); + childSize = child->GetCachedSize(); + + sz.y = wxMax(sz.y, childSize.y); + sz.x += childSize.x; + descent = wxMax(descent, childDescent); + } + else if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, wxPoint(position.x + sz.x, position.y), p)) { sz.y = wxMax(sz.y, childSize.y); sz.x += childSize.x; descent = wxMax(descent, childDescent); + + if ((flags & wxRICHTEXT_CACHE_SIZE) && (rangeToUse == child->GetRange())) + { + child->SetCachedSize(childSize); + child->SetDescent(childDescent); + } + + if (partialExtents) + { + int lastSize; + if (partialExtents->GetCount() > 0) + lastSize = (*partialExtents)[partialExtents->GetCount()-1]; + else + lastSize = 0; + + size_t i; + for (i = 0; i < childExtents.GetCount(); i++) + { + partialExtents->Add(childExtents[i] + lastSize); + } + } } + + if (p) + p->Clear(); } node = node->GetNext(); @@ -3604,7 +3983,7 @@ int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition wxSize lineSize = line->GetSize(); wxRichTextRange lineRange = line->GetAbsoluteRange(); - if (pt.y >= linePos.y && pt.y <= linePos.y + lineSize.y) + if (pt.y <= linePos.y + lineSize.y) { if (pt.x < linePos.x) { @@ -3618,9 +3997,42 @@ int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition } else { - long i; - int lastX = linePos.x; - for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++) +#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS + wxArrayInt partialExtents; + + wxSize paraSize; + int paraDescent; + + // This calculates the partial text extents + GetRangeSize(lineRange, paraSize, paraDescent, dc, wxRICHTEXT_UNFORMATTED, wxPoint(0,0), & partialExtents); + + int lastX = linePos.x; + size_t i; + for (i = 0; i < partialExtents.GetCount(); i++) + { + int nextX = partialExtents[i] + linePos.x; + + if (pt.x >= lastX && pt.x <= nextX) + { + textPosition = i + lineRange.GetStart(); // minus 1? + + // So now we know it's between i-1 and i. + // Let's see if we can be more precise about + // which side of the position it's on. + + int midPoint = (nextX - lastX)/2 + lastX; + if (pt.x >= midPoint) + return wxRICHTEXT_HITTEST_AFTER; + else + return wxRICHTEXT_HITTEST_BEFORE; + } + + lastX = nextX; + } +#else + long i; + int lastX = linePos.x; + for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++) { wxSize childSize; int descent = 0; @@ -3650,6 +4062,7 @@ int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition lastX = nextX; } } +#endif } } @@ -3788,7 +4201,9 @@ bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTex text += textObj->GetTextForRange(range); } else - return true; + { + text += wxT(" "); + } } node = node->GetNext(); @@ -3808,7 +4223,9 @@ bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTex text = textObj->GetTextForRange(range) + text; } else - return true; + { + text = wxT(" ") + text; + } } node = node->GetPrevious(); @@ -3819,21 +4236,85 @@ bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTex } /// Find a suitable wrap position. -bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition) +bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition, wxArrayInt* partialExtents) { + if (range.GetLength() <= 0) + return false; + // Find the first position where the line exceeds the available space. wxSize sz; - long i; long breakPosition = range.GetEnd(); - for (i = range.GetStart(); i <= range.GetEnd(); i++) + +#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS + if (partialExtents && partialExtents->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline { - int descent = 0; - GetRangeSize(wxRichTextRange(range.GetStart(), i), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + int widthBefore; + + if (range.GetStart() > GetRange().GetStart()) + widthBefore = (*partialExtents)[range.GetStart() - GetRange().GetStart() - 1]; + else + widthBefore = 0; - if (sz.x > availableSpace) + size_t i; + for (i = (size_t) range.GetStart(); i <= (size_t) range.GetEnd(); i++) { - breakPosition = i-1; - break; + int widthFromStartOfThisRange = (*partialExtents)[i - GetRange().GetStart()] - widthBefore; + + if (widthFromStartOfThisRange > availableSpace) + { + breakPosition = i-1; + break; + } + } + } + else +#endif + { + // Binary chop for speed + long minPos = range.GetStart(); + long maxPos = range.GetEnd(); + while (true) + { + if (minPos == maxPos) + { + int descent = 0; + GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + + if (sz.x > availableSpace) + breakPosition = minPos - 1; + break; + } + else if ((maxPos - minPos) == 1) + { + int descent = 0; + GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + + if (sz.x > availableSpace) + breakPosition = minPos - 1; + else + { + GetRangeSize(wxRichTextRange(range.GetStart(), maxPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + if (sz.x > availableSpace) + breakPosition = maxPos-1; + } + break; + } + else + { + long nextPos = minPos + ((maxPos - minPos) / 2); + + int descent = 0; + GetRangeSize(wxRichTextRange(range.GetStart(), nextPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + + if (sz.x > availableSpace) + { + maxPos = nextPos; + } + else + { + minPos = nextPos; + } + } } } @@ -4057,12 +4538,18 @@ void wxRichTextLine::Init(wxRichTextParagraph* parent) m_pos = wxPoint(0, 0); m_size = wxSize(0, 0); m_descent = 0; +#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING + m_objectSizes.Clear(); +#endif } /// Copy void wxRichTextLine::Copy(const wxRichTextLine& obj) { m_range = obj.m_range; +#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING + m_objectSizes = obj.m_objectSizes; +#endif } /// Get the absolute object position @@ -4114,22 +4601,50 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR wxString str = m_text; wxString toRemove = wxRichTextLineBreakChar; str.Replace(toRemove, wxT(" ")); + if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)) + str.MakeUpper(); long len = range.GetLength(); wxString stringChunk = str.Mid(range.GetStart() - offset, (size_t) len); - if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)) - stringChunk.MakeUpper(); - - int charHeight = dc.GetCharHeight(); - - int x = rect.x; - int y = rect.y + (rect.height - charHeight - (descent - m_descent)); // Test for the optimized situations where all is selected, or none // is selected. - wxFont font(GetBuffer()->GetFontTable().FindFont(textAttr)); - dc.SetFont(font); + wxFont textFont(GetBuffer()->GetFontTable().FindFont(textAttr)); + wxCheckSetFont(dc, textFont); + int charHeight = dc.GetCharHeight(); + + int x, y; + if ( textFont.Ok() ) + { + if ( textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) ) + { + double size = static_cast(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR; + textFont.SetPointSize( static_cast(size) ); + x = rect.x; + y = rect.y; + wxCheckSetFont(dc, textFont); + } + else if ( textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) ) + { + double size = static_cast(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR; + textFont.SetPointSize( static_cast(size) ); + x = rect.x; + int sub_height = static_cast( static_cast(charHeight) / wxSCRIPT_MUL_FACTOR); + y = rect.y + (rect.height - sub_height + (descent - m_descent)); + wxCheckSetFont(dc, textFont); + } + else + { + x = rect.x; + y = rect.y + (rect.height - charHeight - (descent - m_descent)); + } + } + else + { + x = rect.x; + y = rect.y + (rect.height - charHeight - (descent - m_descent)); + } // (a) All selected. if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd()) @@ -4147,7 +4662,7 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR // (c) Part selected, part not // Let's draw unselected chunk, selected chunk, then unselected chunk. - dc.SetBackgroundMode(wxTRANSPARENT); + dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); // 1. Initial unselected chunk, if any, up until start of selection. if (selectionRange.GetStart() > range.GetStart() && selectionRange.GetStart() <= range.GetEnd()) @@ -4258,10 +4773,13 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con if (selected) { - dc.SetBrush(*wxBLACK_BRUSH); - dc.SetPen(*wxBLACK_PEN); - dc.SetTextForeground(*wxWHITE); - dc.SetBackgroundMode(wxTRANSPARENT); + wxColour highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); + wxColour highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT)); + + wxCheckSetBrush(dc, wxBrush(highlightColour)); + wxCheckSetPen(dc, wxPen(highlightColour)); + dc.SetTextForeground(highlightTextColour); + dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); } else { @@ -4269,11 +4787,11 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con if (attr.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR) && attr.GetBackgroundColour().IsOk()) { - dc.SetBackgroundMode(wxSOLID); + dc.SetBackgroundMode(wxBRUSHSTYLE_SOLID); dc.SetTextBackground(attr.GetBackgroundColour()); } else - dc.SetBackgroundMode(wxTRANSPARENT); + dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); } while (hasTabs) @@ -4312,9 +4830,9 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)) { wxPen oldPen = dc.GetPen(); - dc.SetPen(wxPen(attr.GetTextColour(), 1)); + wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1)); dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5)); - dc.SetPen(oldPen); + wxCheckSetPen(dc, oldPen); } x = nextTabPos; @@ -4336,9 +4854,9 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)) { wxPen oldPen = dc.GetPen(); - dc.SetPen(wxPen(attr.GetTextColour(), 1)); + wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1)); dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5)); - dc.SetPen(oldPen); + wxCheckSetPen(dc, oldPen); } x += w; @@ -4350,7 +4868,9 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con /// Lay the item out bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style)) { - GetRangeSize(GetRange(), m_size, m_descent, dc, 0, wxPoint(0, 0)); + // Only lay out if we haven't already cached the size + if (m_size.x == -1) + GetRangeSize(GetRange(), m_size, m_descent, dc, 0, wxPoint(0, 0)); return true; } @@ -4365,7 +4885,7 @@ void wxRichTextPlainText::Copy(const wxRichTextPlainText& obj) /// Get/set the object size for the given range. Returns false if the range /// is invalid for this object. -bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int WXUNUSED(flags), wxPoint position) const +bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int WXUNUSED(flags), wxPoint position, wxArrayInt* partialExtents) const { if (!range.IsWithin(GetRange())) return false; @@ -4379,9 +4899,26 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz // of line breaks - and we don't need it, since we'll calculate size within // formatted text by doing it in chunks according to the line ranges + bool bScript(false); wxFont font(GetBuffer()->GetFontTable().FindFont(textAttr)); - dc.SetFont(font); + if (font.Ok()) + { + if ( textAttr.HasTextEffects() && ( (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) + || (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) ) ) + { + wxFont textFont = font; + double size = static_cast(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR; + textFont.SetPointSize( static_cast(size) ); + wxCheckSetFont(dc, textFont); + bScript = true; + } + else + { + wxCheckSetFont(dc, font); + } + } + bool haveDescent = false; int startPos = range.GetStart() - GetRange().GetStart(); long len = range.GetLength(); @@ -4418,13 +4955,40 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz while (stringChunk.Find(wxT('\t')) >= 0) { + int absoluteWidth = 0; + // the string has a tab // break up the string at the Tab wxString stringFragment = stringChunk.BeforeFirst(wxT('\t')); stringChunk = stringChunk.AfterFirst(wxT('\t')); - dc.GetTextExtent(stringFragment, & w, & h); - width += w; - int absoluteWidth = width + position.x; + + if (partialExtents) + { + int oldWidth; + if (partialExtents->GetCount() > 0) + oldWidth = (*partialExtents)[partialExtents->GetCount()-1]; + else + oldWidth = 0; + + // Add these partial extents + wxArrayInt p; + dc.GetPartialTextExtents(stringFragment, p); + size_t j; + for (j = 0; j < p.GetCount(); j++) + partialExtents->Add(oldWidth + p[j]); + + if (partialExtents->GetCount() > 0) + absoluteWidth = (*partialExtents)[(*partialExtents).GetCount()-1] + position.x; + else + absoluteWidth = position.x; + } + else + { + dc.GetTextExtent(stringFragment, & w, & h); + width += w; + absoluteWidth = width + position.x; + haveDescent = true; + } bool notFound = true; for (int i = 0; i < tabCount && notFound; ++i) @@ -4444,13 +5008,58 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz notFound = false; width = nextTabPos - position.x; + + if (partialExtents) + partialExtents->Add(width); } } } } - dc.GetTextExtent(stringChunk, & w, & h, & descent); - width += w; - size = wxSize(width, dc.GetCharHeight()); + + if (!stringChunk.IsEmpty()) + { + if (partialExtents) + { + int oldWidth; + if (partialExtents->GetCount() > 0) + oldWidth = (*partialExtents)[partialExtents->GetCount()-1]; + else + oldWidth = 0; + + // Add these partial extents + wxArrayInt p; + dc.GetPartialTextExtents(stringChunk, p); + size_t j; + for (j = 0; j < p.GetCount(); j++) + partialExtents->Add(oldWidth + p[j]); + } + else + { + dc.GetTextExtent(stringChunk, & w, & h, & descent); + width += w; + haveDescent = true; + } + } + + if (partialExtents) + { + int charHeight = dc.GetCharHeight(); + if ((*partialExtents).GetCount() > 0) + w = (*partialExtents)[partialExtents->GetCount()-1]; + else + w = 0; + size = wxSize(w, charHeight); + } + else + { + size = wxSize(width, dc.GetCharHeight()); + } + + if (!haveDescent) + dc.GetTextExtent(wxT("X"), & w, & h, & descent); + + if ( bScript ) + dc.SetFont(font); return true; } @@ -4535,6 +5144,7 @@ bool wxRichTextPlainText::Merge(wxRichTextObject* object) if (textObject) { m_text += textObject->GetText(); + wxRichTextApplyStyle(m_attributes, textObject->GetAttributes()); return true; } else @@ -4667,21 +5277,23 @@ bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagr action->GetNewParagraphs() = paragraphs; - if (p) + if (p && !p->IsDefault()) { - wxRichTextObjectList::compatibility_iterator node = m_children.GetLast(); - while (node) + for (wxRichTextObjectList::compatibility_iterator node = action->GetNewParagraphs().GetChildren().GetFirst(); node; node = node->GetNext()) { - wxRichTextParagraph* obj = (wxRichTextParagraph*) node->GetData(); - obj->SetAttributes(*p); - node = node->GetPrevious(); + wxRichTextObject* child = node->GetData(); + child->SetAttributes(*p); } } action->SetPosition(pos); + wxRichTextRange range = wxRichTextRange(pos, pos + paragraphs.GetRange().GetEnd() - 1); + if (!paragraphs.GetPartialParagraph()) + range.SetEnd(range.GetEnd()+1); + // Set the range we'll need to delete in Undo - action->SetRange(wxRichTextRange(pos, pos + paragraphs.GetRange().GetEnd() - 1)); + action->SetRange(range); SubmitAction(action); @@ -4746,13 +5358,53 @@ bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int action->GetNewParagraphs().AppendChild(newPara); action->GetNewParagraphs().UpdateRanges(); action->GetNewParagraphs().SetPartialParagraph(false); - action->SetPosition(pos); + wxRichTextParagraph* para = GetParagraphAtPosition(pos, false); + long pos1 = pos; if (p) newPara->SetAttributes(*p); + if (flags & wxRICHTEXT_INSERT_INTERACTIVE) + { + if (para && para->GetRange().GetEnd() == pos) + pos1 ++; + + // Now see if we need to number the paragraph. + if (newPara->GetAttributes().HasBulletNumber()) + { + wxRichTextAttr numberingAttr; + if (FindNextParagraphNumber(para, numberingAttr)) + wxRichTextApplyStyle(newPara->GetAttributes(), (const wxRichTextAttr&) numberingAttr); + } + } + + action->SetPosition(pos); + + // Use the default character style + // Use the default character style + if (!GetDefaultStyle().IsDefault() && newPara->GetChildren().GetFirst()) + { + // Check whether the default style merely reflects the paragraph/basic style, + // in which case don't apply it. + wxTextAttrEx defaultStyle(GetDefaultStyle()); + wxTextAttrEx toApply; + if (para) + { + wxRichTextAttr combinedAttr = para->GetCombinedAttributes(); + wxTextAttrEx newAttr; + // This filters out attributes that are accounted for by the current + // paragraph/basic style + wxRichTextApplyStyle(toApply, defaultStyle, & combinedAttr); + } + else + toApply = defaultStyle; + + if (!toApply.IsDefault()) + newPara->GetChildren().GetFirst()->GetData()->SetAttributes(toApply); + } + // Set the range we'll need to delete in Undo - action->SetRange(wxRichTextRange(pos, pos)); + action->SetRange(wxRichTextRange(pos1, pos1)); SubmitAction(action); @@ -4813,7 +5465,8 @@ wxTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPositio wxRichTextParagraphStyleDefinition* paraDef = GetStyleSheet()->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName()); if (paraDef) { - if (!paraDef->GetNextStyle().IsEmpty()) + // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style. + if (para->GetRange().GetEnd() == pos && !paraDef->GetNextStyle().IsEmpty()) { wxRichTextParagraphStyleDefinition* nextParaDef = GetStyleSheet()->FindParagraphStyle(paraDef->GetNextStyle()); if (nextParaDef) @@ -4831,6 +5484,25 @@ wxTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPositio } } } + + // Also apply list style if present + if (lookUpNewParaStyle && !para->GetAttributes().GetListStyleName().IsEmpty() && GetStyleSheet()) + { + wxRichTextListStyleDefinition* listDef = GetStyleSheet()->FindListStyle(para->GetAttributes().GetListStyleName()); + if (listDef) + { + int thisIndent = para->GetAttributes().GetLeftIndent(); + int thisLevel = para->GetAttributes().HasOutlineLevel() ? para->GetAttributes().GetOutlineLevel() : listDef->FindLevelForIndent(thisIndent); + + // Apply the overall list style, and item style for this level + wxRichTextAttr listStyle(listDef->GetCombinedStyleForLevel(thisLevel, GetStyleSheet())); + wxRichTextApplyStyle(attr, listStyle); + attr.SetOutlineLevel(thisLevel); + if (para->GetAttributes().HasBulletNumber()) + attr.SetBulletNumber(para->GetAttributes().GetBulletNumber()); + } + } + if (!foundAttributes) { attr = para->GetAttributes(); @@ -4843,14 +5515,6 @@ wxTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPositio attr.SetFlags(flags); } - // Now see if we need to number the paragraph. - if (attr.HasBulletStyle()) - { - wxTextAttr numberingAttr; - if (FindNextParagraphNumber(para, numberingAttr)) - wxRichTextApplyStyle(attr, (const wxTextAttr&) numberingAttr); - } - return attr; } else @@ -4870,22 +5534,18 @@ bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, wxRichT // Copy the fragment that we'll need to restore in Undo CopyFragment(range, action->GetOldParagraphs()); - // Special case: if there is only one (non-partial) paragraph, - // we must save the *next* paragraph's style, because that - // is the style we must apply when inserting the content back - // when undoing the delete. (This is because we're merging the - // paragraph with the previous paragraph and throwing away - // the style, and we need to restore it.) - if (!action->GetOldParagraphs().GetPartialParagraph() && action->GetOldParagraphs().GetChildCount() == 1) + // See if we're deleting a paragraph marker, in which case we need to + // make a note not to copy the attributes from the 2nd paragraph to the 1st. + if (range.GetStart() == range.GetEnd()) { - wxRichTextParagraph* lastPara = GetParagraphAtPosition(range.GetStart()); - if (lastPara) + wxRichTextParagraph* para = GetParagraphAtPosition(range.GetStart()); + if (para && para->GetRange().GetEnd() == range.GetEnd()) { - wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetEnd()+1); - if (nextPara) + wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetStart()+1); + if (nextPara && nextPara != para) { - wxRichTextParagraph* para = (wxRichTextParagraph*) action->GetOldParagraphs().GetChild(0); - para->SetAttributes(nextPara->GetAttributes()); + action->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara->GetAttributes()); + action->GetOldParagraphs().GetAttributes().SetFlags(action->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE); } } } @@ -4903,7 +5563,7 @@ bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName) wxASSERT(m_batchedCommand == NULL); if (m_batchedCommand) { - GetCommandProcessor()->Submit(m_batchedCommand); + GetCommandProcessor()->Store(m_batchedCommand); } m_batchedCommand = new wxRichTextCommand(cmdName); } @@ -4923,7 +5583,7 @@ bool wxRichTextBuffer::EndBatchUndo() if (m_batchedCommandDepth == 0) { - GetCommandProcessor()->Submit(m_batchedCommand); + GetCommandProcessor()->Store(m_batchedCommand); m_batchedCommand = NULL; } @@ -4934,7 +5594,15 @@ bool wxRichTextBuffer::EndBatchUndo() bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action) { if (BatchingUndo() && m_batchedCommand && !SuppressingUndo()) + { + wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName()); + cmd->AddAction(action); + cmd->Do(); + cmd->GetActions().Clear(); + delete cmd; + m_batchedCommand->AddAction(action); + } else { wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName()); @@ -4976,8 +5644,6 @@ bool wxRichTextBuffer::BeginStyle(const wxTextAttr& style) SetDefaultStyle(newStyle); - // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize()); - return true; } @@ -5020,7 +5686,7 @@ void wxRichTextBuffer::ClearStyleStack() bool wxRichTextBuffer::BeginBold() { wxTextAttr attr; - attr.SetFontWeight(wxBOLD); + attr.SetFontWeight(wxFONTWEIGHT_BOLD); return BeginStyle(attr); } @@ -5029,7 +5695,7 @@ bool wxRichTextBuffer::BeginBold() bool wxRichTextBuffer::BeginItalic() { wxTextAttr attr; - attr.SetFontStyle(wxITALIC); + attr.SetFontStyle(wxFONTSTYLE_ITALIC); return BeginStyle(attr); } @@ -5257,14 +5923,15 @@ bool wxRichTextBuffer::RemoveHandler(const wxString& name) } /// Finds a handler by filename or, if supplied, type -wxRichTextFileHandler *wxRichTextBuffer::FindHandlerFilenameOrType(const wxString& filename, int imageType) +wxRichTextFileHandler *wxRichTextBuffer::FindHandlerFilenameOrType(const wxString& filename, + wxRichTextFileType imageType) { if (imageType != wxRICHTEXT_TYPE_ANY) return FindHandler(imageType); else if (!filename.IsEmpty()) { wxString path, file, ext; - wxSplitPath(filename, & path, & file, & ext); + wxFileName::SplitPath(filename, & path, & file, & ext); return FindHandler(ext, imageType); } else @@ -5287,7 +5954,7 @@ wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& name) } /// Finds a handler by extension and type -wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, int type) +wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, wxRichTextFileType type) { wxList::compatibility_iterator node = sm_handlers.GetFirst(); while (node) @@ -5302,7 +5969,7 @@ wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, } /// Finds a handler by type -wxRichTextFileHandler* wxRichTextBuffer::FindHandler(int type) +wxRichTextFileHandler* wxRichTextBuffer::FindHandler(wxRichTextFileType type) { wxList::compatibility_iterator node = sm_handlers.GetFirst(); while (node) @@ -5346,7 +6013,7 @@ wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* t while (node) { wxRichTextFileHandler* handler = (wxRichTextFileHandler*) node->GetData(); - if (handler->IsVisible() && ((save && handler->CanSave()) || !save && handler->CanLoad())) + if (handler->IsVisible() && ((save && handler->CanSave()) || (!save && handler->CanLoad()))) { if (combine) { @@ -5380,7 +6047,7 @@ wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* t } /// Load a file -bool wxRichTextBuffer::LoadFile(const wxString& filename, int type) +bool wxRichTextBuffer::LoadFile(const wxString& filename, wxRichTextFileType type) { wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type); if (handler) @@ -5396,7 +6063,7 @@ bool wxRichTextBuffer::LoadFile(const wxString& filename, int type) } /// Save a file -bool wxRichTextBuffer::SaveFile(const wxString& filename, int type) +bool wxRichTextBuffer::SaveFile(const wxString& filename, wxRichTextFileType type) { wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type); if (handler) @@ -5409,7 +6076,7 @@ bool wxRichTextBuffer::SaveFile(const wxString& filename, int type) } /// Load from a stream -bool wxRichTextBuffer::LoadFile(wxInputStream& stream, int type) +bool wxRichTextBuffer::LoadFile(wxInputStream& stream, wxRichTextFileType type) { wxRichTextFileHandler* handler = FindHandler(type); if (handler) @@ -5425,7 +6092,7 @@ bool wxRichTextBuffer::LoadFile(wxInputStream& stream, int type) } /// Save to a stream -bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, int type) +bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, wxRichTextFileType type) { wxRichTextFileHandler* handler = FindHandler(type); if (handler) @@ -5499,7 +6166,9 @@ bool wxRichTextBuffer::PasteFromClipboard(long position) wxRichTextBuffer* richTextBuffer = data.GetRichTextBuffer(); if (richTextBuffer) { - InsertParagraphsWithUndo(position+1, *richTextBuffer, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); + InsertParagraphsWithUndo(position+1, *richTextBuffer, GetRichTextCtrl(), 0); + if (GetRichTextCtrl()) + GetRichTextCtrl()->ShowPosition(position + richTextBuffer->GetRange().GetEnd()); delete richTextBuffer; } } @@ -5508,9 +6177,23 @@ bool wxRichTextBuffer::PasteFromClipboard(long position) wxTextDataObject data; wxTheClipboard->GetData(data); wxString text(data.GetText()); - text.Replace(_T("\r\n"), _T("\n")); +#ifdef __WXMSW__ + wxString text2; + text2.Alloc(text.Length()+1); + size_t i; + for (i = 0; i < text.Length(); i++) + { + wxChar ch = text[i]; + if (ch != wxT('\r')) + text2 += ch; + } +#else + wxString text2 = text; +#endif + InsertTextWithUndo(position+1, text2, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); - InsertTextWithUndo(position+1, text, GetRichTextCtrl()); + if (GetRichTextCtrl()) + GetRichTextCtrl()->ShowPosition(position + text2.Length()); success = true; } @@ -5528,10 +6211,10 @@ bool wxRichTextBuffer::PasteFromClipboard(long position) if (action->GetNewParagraphs().GetChildCount() == 1) action->GetNewParagraphs().SetPartialParagraph(true); - action->SetPosition(position); + action->SetPosition(position+1); // Set the range we'll need to delete in Undo - action->SetRange(wxRichTextRange(position, position)); + action->SetRange(wxRichTextRange(position+1, position+1)); SubmitAction(action); @@ -5672,13 +6355,13 @@ bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, w { if (bulletAttr.GetTextColour().Ok()) { - dc.SetPen(wxPen(bulletAttr.GetTextColour())); - dc.SetBrush(wxBrush(bulletAttr.GetTextColour())); + wxCheckSetPen(dc, wxPen(bulletAttr.GetTextColour())); + wxCheckSetBrush(dc, wxBrush(bulletAttr.GetTextColour())); } else { - dc.SetPen(*wxBLACK_PEN); - dc.SetBrush(*wxBLACK_BRUSH); + wxCheckSetPen(dc, *wxBLACK_PEN); + wxCheckSetBrush(dc, *wxBLACK_BRUSH); } wxFont font; @@ -5689,7 +6372,7 @@ bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, w else font = (*wxNORMAL_FONT); - dc.SetFont(font); + wxCheckSetFont(dc, font); int charHeight = dc.GetCharHeight(); @@ -5763,12 +6446,12 @@ bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph* paragraph, wxDC& else font = (*wxNORMAL_FONT); - dc.SetFont(font); + wxCheckSetFont(dc, font); if (attr.GetTextColour().Ok()) dc.SetTextForeground(attr.GetTextColour()); - dc.SetBackgroundMode(wxTRANSPARENT); + dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); int charHeight = dc.GetCharHeight(); wxCoord tw, th; @@ -5930,6 +6613,58 @@ wxRichTextAction::~wxRichTextAction() { } +void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt& optimizationLineCharPositions, wxArrayInt& optimizationLineYPositions) +{ + // Store a list of line start character and y positions so we can figure out which area + // we need to refresh + +#if wxRICHTEXT_USE_OPTIMIZED_DRAWING + // NOTE: we're assuming that the buffer is laid out correctly at this point. + // If we had several actions, which only invalidate and leave layout until the + // paint handler is called, then this might not be true. So we may need to switch + // optimisation on only when we're simply adding text and not simultaneously + // deleting a selection, for example. Or, we make sure the buffer is laid out correctly + // first, but of course this means we'll be doing it twice. + if (!m_buffer->GetDirty() && m_ctrl) // can only do optimisation if the buffer is already laid out correctly + { + wxSize clientSize = m_ctrl->GetClientSize(); + wxPoint firstVisiblePt = m_ctrl->GetFirstVisiblePoint(); + int lastY = firstVisiblePt.y + clientSize.y; + + wxRichTextParagraph* para = m_buffer->GetParagraphAtPosition(GetRange().GetStart()); + wxRichTextObjectList::compatibility_iterator node = m_buffer->GetChildren().Find(para); + while (node) + { + wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData(); + wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); + while (node2) + { + wxRichTextLine* line = node2->GetData(); + wxPoint pt = line->GetAbsolutePosition(); + wxRichTextRange range = line->GetAbsoluteRange(); + + if (pt.y > lastY) + { + node2 = wxRichTextLineList::compatibility_iterator(); + node = wxRichTextObjectList::compatibility_iterator(); + } + else if (range.GetStart() > GetPosition() && pt.y >= firstVisiblePt.y) + { + optimizationLineCharPositions.Add(range.GetStart()); + optimizationLineYPositions.Add(pt.y); + } + + if (node2) + node2 = node2->GetNext(); + } + + if (node) + node = node->GetNext(); + } + } +#endif +} + bool wxRichTextAction::Do() { m_buffer->Modify(true); @@ -5944,54 +6679,12 @@ bool wxRichTextAction::Do() wxArrayInt optimizationLineYPositions; #if wxRICHTEXT_USE_OPTIMIZED_DRAWING - // NOTE: we're assuming that the buffer is laid out correctly at this point. - // If we had several actions, which only invalidate and leave layout until the - // paint handler is called, then this might not be true. So we may need to switch - // optimisation on only when we're simply adding text and not simultaneously - // deleting a selection, for example. Or, we make sure the buffer is laid out correctly - // first, but of course this means we'll be doing it twice. - if (!m_buffer->GetDirty() && m_ctrl) // can only do optimisation if the buffer is already laid out correctly - { - wxSize clientSize = m_ctrl->GetClientSize(); - wxPoint firstVisiblePt = m_ctrl->GetFirstVisiblePoint(); - int lastY = firstVisiblePt.y + clientSize.y; - - wxRichTextParagraph* para = m_buffer->GetParagraphAtPosition(GetPosition()); - wxRichTextObjectList::compatibility_iterator node = m_buffer->GetChildren().Find(para); - while (node) - { - wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData(); - wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); - while (node2) - { - wxRichTextLine* line = node2->GetData(); - wxPoint pt = line->GetAbsolutePosition(); - wxRichTextRange range = line->GetAbsoluteRange(); - - if (pt.y > lastY) - { - node2 = wxRichTextLineList::compatibility_iterator(); - node = wxRichTextObjectList::compatibility_iterator(); - } - else if (range.GetStart() > GetPosition() && pt.y >= firstVisiblePt.y) - { - optimizationLineCharPositions.Add(range.GetStart()); - optimizationLineYPositions.Add(pt.y); - } - - if (node2) - node2 = node2->GetNext(); - } - - if (node) - node = node->GetNext(); - } - } + CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions); #endif - m_buffer->InsertFragment(GetPosition(), m_newParagraphs); + m_buffer->InsertFragment(GetRange().GetStart(), m_newParagraphs); m_buffer->UpdateRanges(); - m_buffer->Invalidate(GetRange()); + m_buffer->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd())); long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength(); @@ -6011,10 +6704,7 @@ bool wxRichTextAction::Do() newCaretPosition = wxMin(newCaretPosition, (m_buffer->GetRange().GetEnd()-1)); - if (optimizationLineCharPositions.GetCount() > 0) - UpdateAppearance(newCaretPosition, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions); - else - UpdateAppearance(newCaretPosition, true /* send update event */); + UpdateAppearance(newCaretPosition, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */); wxRichTextEvent cmdEvent( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, @@ -6029,11 +6719,22 @@ bool wxRichTextAction::Do() } case wxRICHTEXT_DELETE: { + wxArrayInt optimizationLineCharPositions; + wxArrayInt optimizationLineYPositions; + +#if wxRICHTEXT_USE_OPTIMIZED_DRAWING + CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions); +#endif + m_buffer->DeleteRange(GetRange()); m_buffer->UpdateRanges(); m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); - UpdateAppearance(GetRange().GetStart()-1, true /* send update event */); + long caretPos = GetRange().GetStart()-1; + if (caretPos >= m_buffer->GetRange().GetEnd()) + caretPos --; + + UpdateAppearance(caretPos, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */); wxRichTextEvent cmdEvent( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, @@ -6079,13 +6780,20 @@ bool wxRichTextAction::Undo() { case wxRICHTEXT_INSERT: { + wxArrayInt optimizationLineCharPositions; + wxArrayInt optimizationLineYPositions; + +#if wxRICHTEXT_USE_OPTIMIZED_DRAWING + CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions); +#endif + m_buffer->DeleteRange(GetRange()); m_buffer->UpdateRanges(); m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); long newCaretPosition = GetPosition() - 1; - UpdateAppearance(newCaretPosition, true /* send update event */); + UpdateAppearance(newCaretPosition, true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */); wxRichTextEvent cmdEvent( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, @@ -6100,11 +6808,18 @@ bool wxRichTextAction::Undo() } case wxRICHTEXT_DELETE: { + wxArrayInt optimizationLineCharPositions; + wxArrayInt optimizationLineYPositions; + +#if wxRICHTEXT_USE_OPTIMIZED_DRAWING + CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions); +#endif + m_buffer->InsertFragment(GetRange().GetStart(), m_oldParagraphs); m_buffer->UpdateRanges(); m_buffer->Invalidate(GetRange()); - UpdateAppearance(GetPosition(), true /* send update event */); + UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */); wxRichTextEvent cmdEvent( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, @@ -6143,7 +6858,7 @@ bool wxRichTextAction::Undo() } /// Update the control appearance -void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent, wxArrayInt* optimizationLineCharPositions, wxArrayInt* optimizationLineYPositions) +void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent, wxArrayInt* optimizationLineCharPositions, wxArrayInt* optimizationLineYPositions, bool isDoCmd) { if (m_ctrl) { @@ -6151,11 +6866,10 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent if (!m_ctrl->IsFrozen()) { m_ctrl->LayoutContent(); - m_ctrl->PositionCaret(); #if wxRICHTEXT_USE_OPTIMIZED_DRAWING // Find refresh rectangle if we are in a position to optimise refresh - if (m_cmdId == wxRICHTEXT_INSERT && optimizationLineCharPositions && optimizationLineCharPositions->GetCount() > 0) + if ((m_cmdId == wxRICHTEXT_INSERT || m_cmdId == wxRICHTEXT_DELETE) && optimizationLineCharPositions) { size_t i; @@ -6166,17 +6880,57 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent int firstY = 0; int lastY = firstVisiblePt.y + clientSize.y; - bool foundStart = false; bool foundEnd = false; // position offset - how many characters were inserted int positionOffset = GetRange().GetLength(); + // Determine whether this is Do or Undo, and adjust positionOffset accordingly + if ((m_cmdId == wxRICHTEXT_DELETE && isDoCmd) || (m_cmdId == wxRICHTEXT_INSERT && !isDoCmd)) + positionOffset = - positionOffset; + // find the first line which is being drawn at the same position as it was // before. Since we're talking about a simple insertion, we can assume // that the rest of the window does not need to be redrawn. wxRichTextParagraph* para = m_buffer->GetParagraphAtPosition(GetPosition()); + if (para) + { + // Find line containing GetPosition(). + wxRichTextLine* line = NULL; + wxRichTextLineList::compatibility_iterator node2 = para->GetLines().GetFirst(); + while (node2) + { + wxRichTextLine* l = node2->GetData(); + wxRichTextRange range = l->GetAbsoluteRange(); + if (range.Contains(GetRange().GetStart()-1)) + { + line = l; + break; + } + node2 = node2->GetNext(); + } + + if (line) + { + // Step back a couple of lines to where we can be sure of reformatting correctly + wxRichTextLineList::compatibility_iterator lineNode = para->GetLines().Find(line); + if (lineNode) + { + lineNode = lineNode->GetPrevious(); + if (lineNode) + { + line = (wxRichTextLine*) lineNode->GetData(); + lineNode = lineNode->GetPrevious(); + if (lineNode) + line = (wxRichTextLine*) lineNode->GetData(); + } + } + + firstY = line->GetAbsolutePosition().y; + } + } + wxRichTextObjectList::compatibility_iterator node = m_buffer->GetChildren().Find(para); while (node) { @@ -6196,14 +6950,19 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent node2 = wxRichTextLineList::compatibility_iterator(); node = wxRichTextObjectList::compatibility_iterator(); } - else + // Detect last line in the buffer + else if (!node2->GetNext() && para->GetRange().Contains(m_buffer->GetRange().GetEnd())) { - if (!foundStart) - { - firstY = pt.y - firstVisiblePt.y; - foundStart = true; - } + foundEnd = true; + lastY = pt.y + line->GetSize().y; + node2 = wxRichTextLineList::compatibility_iterator(); + node = wxRichTextObjectList::compatibility_iterator(); + + break; + } + else + { // search for this line being at the same position as before for (i = 0; i < optimizationLineCharPositions->GetCount(); i++) { @@ -6212,7 +6971,8 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent { // Stop, we're now the same as we were foundEnd = true; - lastY = pt.y - firstVisiblePt.y; + + lastY = pt.y; node2 = wxRichTextLineList::compatibility_iterator(); node = wxRichTextObjectList::compatibility_iterator(); @@ -6230,22 +6990,21 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent node = node->GetNext(); } - if (!foundStart) - firstY = firstVisiblePt.y; + firstY = wxMax(firstVisiblePt.y, firstY); if (!foundEnd) lastY = firstVisiblePt.y + clientSize.y; - wxRect rect(firstVisiblePt.x, firstY, firstVisiblePt.x + clientSize.x, lastY - firstY); + // Convert to device coordinates + wxRect rect(m_ctrl->GetPhysicalPoint(wxPoint(firstVisiblePt.x, firstY)), wxSize(clientSize.x, lastY - firstY)); m_ctrl->RefreshRect(rect); - - // TODO: we need to make sure that lines are only drawn if in the update region. The rect - // passed to Draw is currently used in different ways (to pass the position the content should - // be drawn at as well as the relevant region). } else #endif m_ctrl->Refresh(false); +#if wxRICHTEXT_USE_OWN_CARET + m_ctrl->PositionCaret(); +#endif if (sendUpdateEvent) wxTextCtrl::SendTextUpdatedEvent(m_ctrl); } @@ -6336,10 +7095,11 @@ bool wxRichTextImage::LoadFromBlock() /// Make block from the wxImage bool wxRichTextImage::MakeBlock() { - if (m_imageBlock.GetImageType() == wxBITMAP_TYPE_ANY || m_imageBlock.GetImageType() == -1) - m_imageBlock.SetImageType(wxBITMAP_TYPE_PNG); + wxBitmapType type = m_imageBlock.GetImageType(); + if ( type == wxBITMAP_TYPE_ANY || type == wxBITMAP_TYPE_INVALID ) + m_imageBlock.SetImageType(type = wxBITMAP_TYPE_PNG); - m_imageBlock.MakeImageBlock(m_image, m_imageBlock.GetImageType()); + m_imageBlock.MakeImageBlock(m_image, type); return m_imageBlock.Ok(); } @@ -6363,8 +7123,8 @@ bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichT if (selectionRange.Contains(range.GetStart())) { - dc.SetBrush(*wxBLACK_BRUSH); - dc.SetPen(*wxBLACK_PEN); + wxCheckSetBrush(dc, *wxBLACK_BRUSH); + wxCheckSetPen(dc, *wxBLACK_PEN); dc.SetLogicalFunction(wxINVERT); dc.DrawRectangle(rect); dc.SetLogicalFunction(wxCOPY); @@ -6390,11 +7150,22 @@ bool wxRichTextImage::Layout(wxDC& WXUNUSED(dc), const wxRect& rect, int WXUNUSE /// Get/set the object size for the given range. Returns false if the range /// is invalid for this object. -bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& WXUNUSED(dc), int WXUNUSED(flags), wxPoint WXUNUSED(position)) const +bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& WXUNUSED(dc), int WXUNUSED(flags), wxPoint WXUNUSED(position), wxArrayInt* partialExtents) const { if (!range.IsWithin(GetRange())) return false; + if (!m_image.Ok()) + ((wxRichTextImage*) this)->LoadFromBlock(); + + if (partialExtents) + { + if (m_image.Ok()) + partialExtents->Add(m_image.GetWidth()); + else + partialExtents->Add(0); + } + if (!m_image.Ok()) return false; @@ -6558,7 +7329,7 @@ bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& f bool wxRichTextFileHandler::CanHandle(const wxString& filename) const { wxString path, file, ext; - wxSplitPath(filename, & path, & file, & ext); + wxFileName::SplitPath(filename, & path, & file, & ext); return (ext.Lower() == GetExtension()); } @@ -6648,7 +7419,7 @@ void wxRichTextImageBlock::Init() { m_data = NULL; m_dataSize = 0; - m_imageType = -1; + m_imageType = wxBITMAP_TYPE_INVALID; } void wxRichTextImageBlock::Clear() @@ -6656,7 +7427,7 @@ void wxRichTextImageBlock::Clear() delete[] m_data; m_data = NULL; m_dataSize = 0; - m_imageType = -1; + m_imageType = wxBITMAP_TYPE_INVALID; } @@ -6666,24 +7437,23 @@ void wxRichTextImageBlock::Clear() // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to // load the image a 2nd time. -bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG) +bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, wxBitmapType imageType, + wxImage& image, bool convertToJPEG) { m_imageType = imageType; wxString filenameToRead(filename); bool removeFile = false; - if (imageType == -1) + if (imageType == wxBITMAP_TYPE_INVALID) return false; // Could not determine image type if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG) { - wxString tempFile; - bool success = wxGetTempFileName(_("image"), tempFile) ; - - wxASSERT(success); + wxString tempFile = + wxFileName::CreateTempFileName(_("image")); - wxUnusedVar(success); + wxASSERT(!tempFile.IsEmpty()); image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG); filenameToRead = tempFile; @@ -6710,19 +7480,16 @@ bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, int imageTyp // Make an image block from the wxImage in the given // format. -bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, int imageType, int quality) +bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, wxBitmapType imageType, int quality) { m_imageType = imageType; image.SetOption(wxT("quality"), quality); - if (imageType == -1) + if (imageType == wxBITMAP_TYPE_INVALID) return false; // Could not determine image type - wxString tempFile; - bool success = wxGetTempFileName(_("image"), tempFile) ; - - wxASSERT(success); - wxUnusedVar(success); + wxString tempFile = wxFileName::CreateTempFileName(_("image")) ; + wxASSERT(!tempFile.IsEmpty()); if (!image.SaveFile(tempFile, m_imageType)) { @@ -6789,9 +7556,8 @@ bool wxRichTextImageBlock::Load(wxImage& image) wxMemoryInputStream mstream(m_data, m_dataSize); bool success = image.LoadFile(mstream, GetImageType()); #else - wxString tempFile; - bool success = wxGetTempFileName(_("image"), tempFile) ; - wxASSERT(success); + wxString tempFile = wxFileName::CreateTempFileName(_("image")); + wxASSERT(!tempFile.IsEmpty()); if (!WriteBlock(tempFile, m_data, m_dataSize)) { @@ -6838,14 +7604,17 @@ bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream) } // Read data in hex from a stream -bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, int imageType) +bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, wxBitmapType imageType) { int dataSize = length/2; if (m_data) delete[] m_data; - wxChar str[2]; + // create a null terminated temporary string: + char str[3]; + str[2] = '\0'; + m_data = new unsigned char[dataSize]; int i; for (i = 0; i < dataSize; i ++) @@ -7035,7 +7804,7 @@ bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len), const void *buf) * Manages quick access to a pool of fonts for rendering rich text */ -WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxFont, wxRichTextFontTableHashMap); +WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont, wxRichTextFontTableHashMap, class WXDLLIMPEXP_RICHTEXT); class wxRichTextFontTableData: public wxObjectRefData { @@ -7070,10 +7839,10 @@ IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable, wxObject) wxRichTextFontTable::wxRichTextFontTable() { m_refData = new wxRichTextFontTableData; - m_refData->IncRef(); } wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable& table) + : wxObject() { (*this) = table; }