]> git.saurik.com Git - wxWidgets.git/blobdiff - src/richtext/richtextbuffer.cpp
changed ds[pw] files to use CRLF eol style and not native one (this helps if you...
[wxWidgets.git] / src / richtext / richtextbuffer.cpp
index a73d8825d0dffb963881a781fce11831cb0b9233..b77bca81bfd2412846053d31e1170295d0848665 100644 (file)
 WX_DEFINE_LIST(wxRichTextObjectList)
 WX_DEFINE_LIST(wxRichTextLineList)
 
+// Switch off if the platform doesn't like it for some reason
+#define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
+
+const wxChar wxRichTextLineBreakChar = (wxChar) 29;
+
 /*!
  * wxRichTextObject
  * This is the base for drawable objects.
@@ -82,12 +87,6 @@ void wxRichTextObject::Copy(const wxRichTextObject& obj)
     m_range = obj.m_range;
     m_attributes = obj.m_attributes;
     m_descent = obj.m_descent;
-/*
-    if (!m_attributes.GetFont().Ok())
-        wxLogDebug(wxT("No font!"));
-    if (!obj.m_attributes.GetFont().Ok())
-        wxLogDebug(wxT("Parent has no font!"));
-*/
 }
 
 void wxRichTextObject::SetMargins(int margin)
@@ -526,7 +525,12 @@ bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, const wxRichTextRange& range,
         {
             wxRect childRect(child->GetPosition(), child->GetCachedSize());
 
-            if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetTop() > rect.GetBottom() || childRect.GetBottom() < rect.GetTop())
+            if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetTop() > rect.GetBottom())
+            {
+                // Stop drawing
+                break;
+            }
+            else if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetBottom() < rect.GetTop())
             {
                 // Skip
             }
@@ -899,21 +903,18 @@ wxSize wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos, bool caretP
 /// Convenience function to add a paragraph of text
 wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraph(const wxString& text, wxTextAttrEx* paraStyle)
 {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     // Don't use the base style, just the default style, and the base style will
-    // be combined at display time
-    wxTextAttrEx style(GetDefaultStyle());
-#else
-    wxTextAttrEx style(GetAttributes());
+    // be combined at display time.
+    // Divide into paragraph and character styles.
 
-    // Apply default style. If the style has no attributes set,
-    // then the attributes will remain the 'basic style' (i.e. the
-    // layout box's style).
-    wxRichTextApplyStyle(style, GetDefaultStyle());
-#endif
-    wxRichTextParagraph* para = new wxRichTextParagraph(text, this, & style);
-    if (paraStyle)
-        para->SetAttributes(*paraStyle);
+    wxTextAttrEx defaultCharStyle;
+    wxTextAttrEx defaultParaStyle;
+
+    wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle);
+    wxTextAttrEx* pStyle = paraStyle ? paraStyle : (wxTextAttrEx*) & defaultParaStyle;
+    wxTextAttrEx* cStyle = & defaultCharStyle;
+
+    wxRichTextParagraph* para = new wxRichTextParagraph(text, this, pStyle, cStyle);
 
     AppendChild(para);
 
@@ -926,24 +927,16 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraph(const wxString& text,
 /// Adds multiple paragraphs, based on newlines.
 wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text, wxTextAttrEx* paraStyle)
 {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     // Don't use the base style, just the default style, and the base style will
-    // be combined at display time
-    wxTextAttrEx style(GetDefaultStyle());
-#else
-    wxTextAttrEx style(GetAttributes());
+    // be combined at display time.
+    // Divide into paragraph and character styles.
 
-    //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
-    //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
+    wxTextAttrEx defaultCharStyle;
+    wxTextAttrEx defaultParaStyle;
+    wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle);
 
-    // Apply default style. If the style has no attributes set,
-    // then the attributes will remain the 'basic style' (i.e. the
-    // layout box's style).
-    wxRichTextApplyStyle(style, GetDefaultStyle());
-
-    //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
-    //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
-#endif
+    wxTextAttrEx* pStyle = paraStyle ? paraStyle : (wxTextAttrEx*) & defaultParaStyle;
+    wxTextAttrEx* cStyle = & defaultCharStyle;
 
     wxRichTextParagraph* firstPara = NULL;
     wxRichTextParagraph* lastPara = NULL;
@@ -953,9 +946,7 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text
     size_t i = 0;
     size_t len = text.length();
     wxString line;
-    wxRichTextParagraph* para = new wxRichTextParagraph(wxEmptyString, this, & style);
-    if (paraStyle)
-        para->SetAttributes(*paraStyle);
+    wxRichTextParagraph* para = new wxRichTextParagraph(wxEmptyString, this, pStyle, cStyle);
 
     AppendChild(para);
 
@@ -970,15 +961,10 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text
             wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData();
             plainText->SetText(line);
 
-            para = new wxRichTextParagraph(wxEmptyString, this, & style);
-            if (paraStyle)
-                para->SetAttributes(*paraStyle);
+            para = new wxRichTextParagraph(wxEmptyString, this, pStyle, cStyle);
 
             AppendChild(para);
 
-            //if (!firstPara)
-            //    firstPara = para;
-
             lastPara = para;
             line = wxEmptyString;
         }
@@ -994,18 +980,6 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text
         plainText->SetText(line);
     }
 
-/*
-    if (firstPara)
-        range.SetStart(firstPara->GetRange().GetStart());
-    else if (lastPara)
-        range.SetStart(lastPara->GetRange().GetStart());
-
-    if (lastPara)
-        range.SetEnd(lastPara->GetRange().GetEnd());
-    else if (firstPara)
-        range.SetEnd(firstPara->GetRange().GetEnd());
-*/
-
     UpdateRanges();
 
     SetDirty(false);
@@ -1016,25 +990,20 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text
 /// Convenience function to add an image
 wxRichTextRange wxRichTextParagraphLayoutBox::AddImage(const wxImage& image, wxTextAttrEx* paraStyle)
 {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     // Don't use the base style, just the default style, and the base style will
-    // be combined at display time
-    wxTextAttrEx style(GetDefaultStyle());
-#else
-    wxTextAttrEx style(GetAttributes());
+    // be combined at display time.
+    // Divide into paragraph and character styles.
 
-    // Apply default style. If the style has no attributes set,
-    // then the attributes will remain the 'basic style' (i.e. the
-    // layout box's style).
-    wxRichTextApplyStyle(style, GetDefaultStyle());
-#endif
+    wxTextAttrEx defaultCharStyle;
+    wxTextAttrEx defaultParaStyle;
+    wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle);
 
-    wxRichTextParagraph* para = new wxRichTextParagraph(this, & style);
-    AppendChild(para);
-    para->AppendChild(new wxRichTextImage(image, this));
+    wxTextAttrEx* pStyle = paraStyle ? paraStyle : (wxTextAttrEx*) & defaultParaStyle;
+    wxTextAttrEx* cStyle = & defaultCharStyle;
 
-    if (paraStyle)
-        para->SetAttributes(*paraStyle);
+    wxRichTextParagraph* para = new wxRichTextParagraph(this, pStyle);
+    AppendChild(para);
+    para->AppendChild(new wxRichTextImage(image, this, cStyle));
 
     UpdateRanges();
     SetDirty(true);
@@ -1046,8 +1015,6 @@ wxRichTextRange wxRichTextParagraphLayoutBox::AddImage(const wxImage& image, wxT
 /// Insert fragment into this box at the given position. If partialParagraph is true,
 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
 /// marker.
-/// TODO: if fragment is inserted inside styled fragment, must apply that style to
-/// to the data (if it has a default style, anyway).
 
 bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParagraphLayoutBox& fragment)
 {
@@ -1077,6 +1044,11 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag
             wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph);
             wxASSERT (firstPara != NULL);
 
+            // Apply the new paragraph attributes to the existing paragraph
+            wxTextAttrEx attr(para->GetAttributes());
+            wxRichTextApplyStyle(attr, firstPara->GetAttributes());
+            para->SetAttributes(attr);
+
             wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst();
             while (objectNode)
             {
@@ -1183,9 +1155,6 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag
                 if (finalPara->GetChildCount() == 0)
                 {
                     wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString);
-#if !wxRICHTEXT_USE_DYNAMIC_STYLES
-                    text->SetAttributes(finalPara->GetAttributes());
-#endif
 
                     finalPara->AppendChild(text);
                 }
@@ -1460,8 +1429,6 @@ wxString wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange& ra
         wxRichTextObject* child = node->GetData();
         if (!child->GetRange().IsOutside(range))
         {
-//            if (lineCount > 0)
-//                text += wxT("\n");
             wxRichTextRange childRange = range;
             childRange.LimitTo(child->GetRange());
 
@@ -1469,7 +1436,7 @@ wxString wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange& ra
 
             text += childText;
 
-            if (childRange.GetEnd() == child->GetRange().GetEnd())
+            if ((childRange.GetEnd() == child->GetRange().GetEnd()) && node->GetNext())
                 text += wxT("\n");
 
             lineCount ++;
@@ -1591,11 +1558,30 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const
     bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
     bool parasOnly = ((flags & wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY) != 0);
     bool charactersOnly = ((flags & wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY) != 0);
+    bool resetExistingStyle = ((flags & wxRICHTEXT_SETSTYLE_RESET) != 0);
+    bool removeStyle = ((flags & wxRICHTEXT_SETSTYLE_REMOVE) != 0);
+
+    // Apply paragraph style first, if any
+    wxRichTextAttr wholeStyle(style);
+
+    if (!removeStyle && wholeStyle.HasParagraphStyleName() && GetStyleSheet())
+    {
+        wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(wholeStyle.GetParagraphStyleName());
+        if (def)
+            wxRichTextApplyStyle(wholeStyle, def->GetStyleMergedWithBase(GetStyleSheet()));
+    }
 
     // Limit the attributes to be set to the content to only character attributes.
-    wxRichTextAttr characterAttributes(style);
+    wxRichTextAttr characterAttributes(wholeStyle);
     characterAttributes.SetFlags(characterAttributes.GetFlags() & (wxTEXT_ATTR_CHARACTER));
 
+    if (!removeStyle && characterAttributes.HasCharacterStyleName() && GetStyleSheet())
+    {
+        wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterAttributes.GetCharacterStyleName());
+        if (def)
+            wxRichTextApplyStyle(characterAttributes, def->GetStyleMergedWithBase(GetStyleSheet()));
+    }
+
     // If we are associated with a control, make undoable; otherwise, apply immediately
     // to the data.
 
@@ -1639,21 +1625,32 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const
                 else
                     newPara = para;
 
-                if (paragraphStyle && !charactersOnly)
+                // If we're specifying paragraphs only, then we really mean character formatting
+                // to be included in the paragraph style
+                if ((paragraphStyle || parasOnly) && !charactersOnly)
                 {
-                    if (applyMinimal)
+                    if (removeStyle)
                     {
-                        // Only apply attributes that will make a difference to the combined
-                        // style as seen on the display
-                        wxRichTextAttr combinedAttr(para->GetCombinedAttributes());
-                        wxRichTextApplyStyle(newPara->GetAttributes(), style, & combinedAttr);
+                        // Removes the given style from the paragraph
+                        wxRichTextRemoveStyle(newPara->GetAttributes(), style);
                     }
+                    else if (resetExistingStyle)
+                        newPara->GetAttributes() = wholeStyle;
                     else
-                        wxRichTextApplyStyle(newPara->GetAttributes(), style);
+                    {
+                        if (applyMinimal)
+                        {
+                            // Only apply attributes that will make a difference to the combined
+                            // style as seen on the display
+                            wxRichTextAttr combinedAttr(para->GetCombinedAttributes());
+                            wxRichTextApplyStyle(newPara->GetAttributes(), wholeStyle, & combinedAttr);
+                        }
+                        else
+                            wxRichTextApplyStyle(newPara->GetAttributes(), wholeStyle);
+                    }
                 }
 
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
-                // If applying paragraph styles dynamically, don't change the text objects' attributes
+                // When applying paragraph styles dynamically, don't change the text objects' attributes
                 // since they will computed as needed. Only apply the character styling if it's _only_
                 // character styling. This policy is subject to change and might be put under user control.
 
@@ -1665,9 +1662,6 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const
 
                 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
                 if (!parasOnly && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
-#else
-                if (characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
-#endif
                 {
                     wxRichTextRange childRange(range);
                     childRange.LimitTo(newPara->GetRange());
@@ -1715,15 +1709,25 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const
                     {
                         wxRichTextObject* child = node2->GetData();
 
-                        if (applyMinimal)
+                        if (removeStyle)
                         {
-                            // Only apply attributes that will make a difference to the combined
-                            // style as seen on the display
-                            wxRichTextAttr combinedAttr(newPara->GetCombinedAttributes(child->GetAttributes()));
-                            wxRichTextApplyStyle(child->GetAttributes(), characterAttributes, & combinedAttr);
+                            // Removes the given style from the paragraph
+                            wxRichTextRemoveStyle(child->GetAttributes(), style);
                         }
+                        else if (resetExistingStyle)
+                            child->GetAttributes() = characterAttributes;
                         else
-                            wxRichTextApplyStyle(child->GetAttributes(), characterAttributes);
+                        {
+                            if (applyMinimal)
+                            {
+                                // Only apply attributes that will make a difference to the combined
+                                // style as seen on the display
+                                wxRichTextAttr combinedAttr(newPara->GetCombinedAttributes(child->GetAttributes()));
+                                wxRichTextApplyStyle(child->GetAttributes(), characterAttributes, & combinedAttr);
+                            }
+                            else
+                                wxRichTextApplyStyle(child->GetAttributes(), characterAttributes);
+                        }
 
                         if (node2 == lastNode)
                             break;
@@ -1799,7 +1803,6 @@ bool wxRichTextParagraphLayoutBox::DoGetStyle(long position, wxTextAttrEx& style
         obj = GetParagraphAtPosition(position);
         if (obj)
         {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
             if (combineStyles)
             {
                 // Start with the base style
@@ -1810,9 +1813,7 @@ bool wxRichTextParagraphLayoutBox::DoGetStyle(long position, wxTextAttrEx& style
             }
             else
                 style = obj->GetAttributes();
-#else
-            style = obj->GetAttributes();
-#endif
+
             return true;
         }
     }
@@ -1821,7 +1822,6 @@ bool wxRichTextParagraphLayoutBox::DoGetStyle(long position, wxTextAttrEx& style
         obj = GetLeafObjectAtPosition(position);
         if (obj)
         {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
             if (combineStyles)
             {
                 wxRichTextParagraph* para = wxDynamicCast(obj->GetParent(), wxRichTextParagraph);
@@ -1829,9 +1829,7 @@ bool wxRichTextParagraphLayoutBox::DoGetStyle(long position, wxTextAttrEx& style
             }
             else
                 style = obj->GetAttributes();
-#else
-            style = obj->GetAttributes();
-#endif
+
             return true;
         }
     }
@@ -1845,13 +1843,13 @@ 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(wxTextAttrEx& currentStyle, const wxTextAttrEx& style, long& multipleStyleAttributes)
+bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, const wxTextAttrEx& style, long& multipleStyleAttributes, int& multipleTextEffectAttributes)
 {
     if (style.HasFont())
     {
-        if (style.HasSize() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_SIZE))
+        if (style.HasFontSize() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_SIZE))
         {
-            if (currentStyle.GetFont().Ok() && currentStyle.HasSize())
+            if (currentStyle.GetFont().Ok() && currentStyle.HasFontSize())
             {
                 if (currentStyle.GetFont().GetPointSize() != style.GetFont().GetPointSize())
                 {
@@ -1872,9 +1870,9 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
             }
         }
 
-        if (style.HasItalic() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_ITALIC))
+        if (style.HasFontItalic() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_ITALIC))
         {
-            if (currentStyle.GetFont().Ok() && currentStyle.HasItalic())
+            if (currentStyle.GetFont().Ok() && currentStyle.HasFontItalic())
             {
                 if (currentStyle.GetFont().GetStyle() != style.GetFont().GetStyle())
                 {
@@ -1894,9 +1892,9 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
             }
         }
 
-        if (style.HasWeight() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_WEIGHT))
+        if (style.HasFontWeight() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_WEIGHT))
         {
-            if (currentStyle.GetFont().Ok() && currentStyle.HasWeight())
+            if (currentStyle.GetFont().Ok() && currentStyle.HasFontWeight())
             {
                 if (currentStyle.GetFont().GetWeight() != style.GetFont().GetWeight())
                 {
@@ -1916,9 +1914,9 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
             }
         }
 
-        if (style.HasFaceName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_FACE))
+        if (style.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_FACE))
         {
-            if (currentStyle.GetFont().Ok() && currentStyle.HasFaceName())
+            if (currentStyle.GetFont().Ok() && currentStyle.HasFontFaceName())
             {
                 wxString faceName1(currentStyle.GetFont().GetFaceName());
                 wxString faceName2(style.GetFont().GetFaceName());
@@ -1941,9 +1939,9 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
             }
         }
 
-        if (style.HasUnderlined() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_UNDERLINE))
+        if (style.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_UNDERLINE))
         {
-            if (currentStyle.GetFont().Ok() && currentStyle.HasUnderlined())
+            if (currentStyle.GetFont().Ok() && currentStyle.HasFontUnderlined())
             {
                 if (currentStyle.GetFont().GetUnderlined() != style.GetFont().GetUnderlined())
                 {
@@ -2058,7 +2056,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasParagraphSpacingAfter())
         {
-            if (currentStyle.HasParagraphSpacingAfter() != style.HasParagraphSpacingAfter())
+            if (currentStyle.GetParagraphSpacingAfter() != style.GetParagraphSpacingAfter())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_PARA_SPACING_AFTER;
@@ -2073,7 +2071,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasParagraphSpacingBefore())
         {
-            if (currentStyle.HasParagraphSpacingBefore() != style.HasParagraphSpacingBefore())
+            if (currentStyle.GetParagraphSpacingBefore() != style.GetParagraphSpacingBefore())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_PARA_SPACING_BEFORE;
@@ -2088,7 +2086,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasLineSpacing())
         {
-            if (currentStyle.HasLineSpacing() != style.HasLineSpacing())
+            if (currentStyle.GetLineSpacing() != style.GetLineSpacing())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_LINE_SPACING;
@@ -2103,7 +2101,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasCharacterStyleName())
         {
-            if (currentStyle.HasCharacterStyleName() != style.HasCharacterStyleName())
+            if (currentStyle.GetCharacterStyleName() != style.GetCharacterStyleName())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_CHARACTER_STYLE_NAME;
@@ -2118,7 +2116,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasParagraphStyleName())
         {
-            if (currentStyle.HasParagraphStyleName() != style.HasParagraphStyleName())
+            if (currentStyle.GetParagraphStyleName() != style.GetParagraphStyleName())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME;
@@ -2133,7 +2131,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasListStyleName())
         {
-            if (currentStyle.HasListStyleName() != style.HasListStyleName())
+            if (currentStyle.GetListStyleName() != style.GetListStyleName())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_LIST_STYLE_NAME;
@@ -2148,7 +2146,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasBulletStyle())
         {
-            if (currentStyle.HasBulletStyle() != style.HasBulletStyle())
+            if (currentStyle.GetBulletStyle() != style.GetBulletStyle())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_STYLE;
@@ -2163,7 +2161,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasBulletNumber())
         {
-            if (currentStyle.HasBulletNumber() != style.HasBulletNumber())
+            if (currentStyle.GetBulletNumber() != style.GetBulletNumber())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_NUMBER;
@@ -2178,7 +2176,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasBulletText())
         {
-            if (currentStyle.HasBulletText() != style.HasBulletText())
+            if (currentStyle.GetBulletText() != style.GetBulletText())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_TEXT;
@@ -2196,7 +2194,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasBulletName())
         {
-            if (currentStyle.HasBulletName() != style.HasBulletName())
+            if (currentStyle.GetBulletName() != style.GetBulletName())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_NAME;
@@ -2213,7 +2211,7 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
     {
         if (currentStyle.HasURL())
         {
-            if (currentStyle.HasURL() != style.HasURL())
+            if (currentStyle.GetURL() != style.GetURL())
             {
                 // Clash of style - mark as such
                 multipleStyleAttributes |= wxTEXT_ATTR_URL;
@@ -2226,6 +2224,48 @@ bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, cons
         }
     }
 
+    if (style.HasTextEffects() && !wxHasStyle(multipleStyleAttributes, 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.
+
+            int currentRelevantTextEffects = currentStyle.GetTextEffects() & style.GetTextEffectFlags();
+            int newRelevantTextEffects = style.GetTextEffects() & style.GetTextEffectFlags();
+
+            if (currentRelevantTextEffects != newRelevantTextEffects)
+            {
+                // Find the text effects that were different, using XOR
+                int differentEffects = currentRelevantTextEffects ^ newRelevantTextEffects;
+
+                // Clash of style - mark as such
+                multipleTextEffectAttributes |= differentEffects;
+                currentStyle.SetTextEffectFlags(currentStyle.GetTextEffectFlags() & ~differentEffects);
+            }
+        }
+        else
+        {
+            currentStyle.SetTextEffects(style.GetTextEffects());
+            currentStyle.SetTextEffectFlags(style.GetTextEffectFlags());
+        }
+    }
+
+    if (style.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_OUTLINE_LEVEL))
+    {
+        if (currentStyle.HasOutlineLevel())
+        {
+            if (currentStyle.GetOutlineLevel() != style.GetOutlineLevel())
+            {
+                // Clash of style - mark as such
+                multipleStyleAttributes |= wxTEXT_ATTR_OUTLINE_LEVEL;
+                currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL);
+            }
+        }
+        else
+            currentStyle.SetOutlineLevel(style.GetOutlineLevel());
+    }
+
     return true;
 }
 
@@ -2239,6 +2279,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range
 
     // The attributes that aren't valid because of multiple styles within the range
     long multipleStyleAttributes = 0;
+    int multipleTextEffectAttributes = 0;
 
     wxRichTextObjectList::compatibility_iterator node = GetChildren().GetFirst();
     while (node)
@@ -2250,7 +2291,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range
             {
                 wxTextAttrEx paraStyle = para->GetCombinedAttributes();
 
-                CollectStyle(style, paraStyle, multipleStyleAttributes);
+                CollectStyle(style, paraStyle, multipleStyleAttributes, multipleTextEffectAttributes);
             }
             else
             {
@@ -2260,7 +2301,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range
                 // First collect paragraph attributes only
                 wxTextAttrEx paraStyle = para->GetCombinedAttributes();
                 paraStyle.SetFlags(paraStyle.GetFlags() & wxTEXT_ATTR_PARAGRAPH);
-                CollectStyle(style, paraStyle, multipleStyleAttributes);
+                CollectStyle(style, paraStyle, multipleStyleAttributes, multipleTextEffectAttributes);
 
                 wxRichTextObjectList::compatibility_iterator childNode = para->GetChildren().GetFirst();
 
@@ -2274,7 +2315,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range
                         // Now collect character attributes only
                         childStyle.SetFlags(childStyle.GetFlags() & wxTEXT_ATTR_CHARACTER);
 
-                        CollectStyle(style, childStyle, multipleStyleAttributes);
+                        CollectStyle(style, childStyle, multipleStyleAttributes, multipleTextEffectAttributes);
                     }
 
                     childNode = childNode->GetNext();
@@ -2289,19 +2330,7 @@ bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range
 /// Set default style
 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx& style)
 {
-    // I don't think the default style should be combined with the previous
-    // default style.
     m_defaultAttributes = style;
-
-#if 0
-    // keep the old attributes if the new style doesn't specify them unless the
-    // new style is empty - then reset m_defaultStyle (as there is no other way
-    // to do it)
-    if ( style.IsDefault() )
-        m_defaultAttributes = style;
-    else
-        m_defaultAttributes = wxTextAttrEx::CombineEx(style, m_defaultAttributes, NULL);
-#endif
     return true;
 }
 
@@ -2336,11 +2365,8 @@ bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange&
                     if (!child->GetRange().IsOutside(range) && child->IsKindOf(CLASSINFO(wxRichTextPlainText)))
                     {
                         foundCount ++;
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
                         wxTextAttrEx textAttr = para->GetCombinedAttributes(child->GetAttributes());
-#else
-                        const wxTextAttrEx& textAttr = child->GetAttributes();
-#endif
+
                         if (wxTextAttrEqPartial(textAttr, style, style.GetFlags()))
                             matchingCount ++;
                     }
@@ -2385,14 +2411,10 @@ bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange&
 
             if (!para->GetRange().IsOutside(range))
             {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
                 wxTextAttrEx textAttr = GetAttributes();
                 // Apply the paragraph style
                 wxRichTextApplyStyle(textAttr, para->GetAttributes());
 
-#else
-                const wxTextAttrEx& textAttr = para->GetAttributes();
-#endif
                 foundCount ++;
                 if (wxTextAttrEqPartial(textAttr, style, style.GetFlags()))
                     matchingCount ++;
@@ -2420,6 +2442,8 @@ void wxRichTextParagraphLayoutBox::Reset()
     Clear();
 
     AddParagraph(wxEmptyString);
+
+    Invalidate(wxRICHTEXT_ALL);
 }
 
 /// Invalidate the buffer. With no argument, invalidates whole buffer.
@@ -2497,13 +2521,13 @@ bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSh
                 wxRichTextListStyleDefinition* listDef = styleSheet->FindListStyle(para->GetAttributes().GetListStyleName());
                 if (paraDef && !listDef)
                 {
-                    para->GetAttributes() = paraDef->GetStyle();
+                    para->GetAttributes() = paraDef->GetStyleMergedWithBase(styleSheet);
                     foundCount ++;
                 }
                 else if (listDef && !paraDef)
                 {
                     // Set overall style defined for the list style definition
-                    para->GetAttributes() = listDef->GetStyle();
+                    para->GetAttributes() = listDef->GetStyleMergedWithBase(styleSheet);
 
                     // Apply the style for this level
                     wxRichTextApplyStyle(para->GetAttributes(), * listDef->GetLevelAttributes(listDef->FindLevelForIndent(currentIndent)));
@@ -2512,7 +2536,7 @@ bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSh
                 else if (listDef && paraDef)
                 {
                     // Combines overall list style, style for level, and paragraph style
-                    para->GetAttributes() = listDef->CombineWithParagraphStyle(currentIndent, paraDef->GetStyle());
+                    para->GetAttributes() = listDef->CombineWithParagraphStyle(currentIndent, paraDef->GetStyleMergedWithBase(styleSheet));
                     foundCount ++;
                 }
             }
@@ -2523,7 +2547,7 @@ bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSh
                 wxRichTextListStyleDefinition* listDef = styleSheet->FindListStyle(para->GetAttributes().GetListStyleName());
 
                 // Overall list definition style
-                para->GetAttributes() = listDef->GetStyle();
+                para->GetAttributes() = listDef->GetStyleMergedWithBase(styleSheet);
 
                 // Style for this level
                 wxRichTextApplyStyle(para->GetAttributes(), * listDef->GetLevelAttributes(listDef->FindLevelForIndent(currentIndent)));
@@ -2535,7 +2559,7 @@ bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSh
                 wxRichTextParagraphStyleDefinition* def = styleSheet->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
                 if (def)
                 {
-                    para->GetAttributes() = def->GetStyle();
+                    para->GetAttributes() = def->GetStyleMergedWithBase(styleSheet);
                     foundCount ++;
                 }
             }
@@ -2549,6 +2573,8 @@ bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSh
 /// Set list style
 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
 {
+    wxRichTextStyleSheet* styleSheet = GetStyleSheet();
+
     bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
     // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
     bool specifyLevel = ((flags & wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL) != 0);
@@ -2616,7 +2642,7 @@ bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange& range, wx
                     // Renumbering will need to be done when we promote/demote a paragraph.
 
                     // Apply the overall list style, and item style for this level
-                    wxTextAttrEx listStyle(def->GetCombinedStyleForLevel(thisLevel));
+                    wxTextAttrEx listStyle(def->GetCombinedStyleForLevel(thisLevel, styleSheet));
                     wxRichTextApplyStyle(newPara->GetAttributes(), listStyle);
 
                     // Now we need to do numbering
@@ -2639,13 +2665,12 @@ bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange& range, wx
                     // Eliminate the main list-related attributes
                     newPara->GetAttributes().SetFlags(newPara->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT & ~wxTEXT_ATTR_BULLET_STYLE & ~wxTEXT_ATTR_BULLET_NUMBER & ~wxTEXT_ATTR_BULLET_TEXT & wxTEXT_ATTR_LIST_STYLE_NAME);
 
-                    wxRichTextStyleSheet* styleSheet = GetStyleSheet();
                     if (styleSheet && !newPara->GetAttributes().GetParagraphStyleName().IsEmpty())
                     {
                         wxRichTextParagraphStyleDefinition* def = styleSheet->FindParagraphStyle(newPara->GetAttributes().GetParagraphStyleName());
                         if (def)
                         {
-                            newPara->GetAttributes() = def->GetStyle();
+                            newPara->GetAttributes() = def->GetStyleMergedWithBase(styleSheet);
                         }
                     }
                 }
@@ -2689,9 +2714,13 @@ bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange& range, wxRi
 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange& range, const wxRichTextRange& promotionRange, int promoteBy,
                                                 wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
 {
+    wxRichTextStyleSheet* styleSheet = GetStyleSheet();
+
     bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
     // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
+#ifdef __WXDEBUG__
     bool specifyLevel = ((flags & wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL) != 0);
+#endif
 
     bool renumber = ((flags & wxRICHTEXT_SETSTYLE_RENUMBER) != 0);
 
@@ -2764,10 +2793,8 @@ bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange& range, co
                 wxRichTextListStyleDefinition* defToUse = def;
                 if (!defToUse)
                 {
-                    wxRichTextStyleSheet* sheet = GetStyleSheet();
-
-                    if (sheet && !newPara->GetAttributes().GetListStyleName().IsEmpty())
-                        defToUse = sheet->FindListStyle(newPara->GetAttributes().GetListStyleName());
+                    if (styleSheet && !newPara->GetAttributes().GetListStyleName().IsEmpty())
+                        defToUse = styleSheet->FindListStyle(newPara->GetAttributes().GetListStyleName());
                 }
 
                 if (defToUse)
@@ -2790,7 +2817,7 @@ bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange& range, co
                     }
 
                     // Apply the overall list style, and item style for this level
-                    wxTextAttrEx listStyle(defToUse->GetCombinedStyleForLevel(thisLevel));
+                    wxTextAttrEx listStyle(defToUse->GetCombinedStyleForLevel(thisLevel, styleSheet));
                     wxRichTextApplyStyle(newPara->GetAttributes(), listStyle);
 
                     // OK, we've (re)applied the style, now let's get the numbering right.
@@ -2903,25 +2930,18 @@ bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy, const wxRichTextRa
 /// position of the paragraph that it had to start looking from.
 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph* previousParagraph, wxRichTextAttr& attr) const
 {
-#if 0
-    wxRichTextObjectList::compatibility_iterator node = m_children.Find(previousParagraph);
-    
-    if (!node)
-        return false;
-#endif
-    
     if (!previousParagraph->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE) || previousParagraph->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE)
         return false;
-    
-    wxRichTextStyleSheet* sheet = GetStyleSheet();
-    if (sheet && !previousParagraph->GetAttributes().GetListStyleName().IsEmpty())
+
+    wxRichTextStyleSheet* styleSheet = GetStyleSheet();
+    if (styleSheet && !previousParagraph->GetAttributes().GetListStyleName().IsEmpty())
     {
-        wxRichTextListStyleDefinition* def = sheet->FindListStyle(previousParagraph->GetAttributes().GetListStyleName());
+        wxRichTextListStyleDefinition* def = styleSheet->FindListStyle(previousParagraph->GetAttributes().GetListStyleName());
         if (def)
         {
             // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
             // int thisLevel = def->FindLevelForIndent(thisIndent);
-            
+
             bool isOutline = (previousParagraph->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE) != 0;
 
             attr.SetFlags(previousParagraph->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER|wxTEXT_ATTR_BULLET_TEXT|wxTEXT_ATTR_BULLET_NAME));
@@ -2929,10 +2949,10 @@ bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph*
                 attr.SetBulletName(previousParagraph->GetAttributes().GetBulletName());
             attr.SetBulletStyle(previousParagraph->GetAttributes().GetBulletStyle());
             attr.SetListStyleName(previousParagraph->GetAttributes().GetListStyleName());
-            
+
             int nextNumber = previousParagraph->GetAttributes().GetBulletNumber() + 1;
             attr.SetBulletNumber(nextNumber);
-            
+
             if (isOutline)
             {
                 wxString text = previousParagraph->GetAttributes().GetBulletText();
@@ -2951,7 +2971,7 @@ bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph*
                     attr.SetBulletText(text);
                 }
             }
-            
+
             return true;
         }
         else
@@ -2973,21 +2993,17 @@ wxArrayInt wxRichTextParagraph::sm_defaultTabs;
 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject* parent, wxTextAttrEx* style):
     wxRichTextBox(parent)
 {
-    if (parent && !style)
-        SetAttributes(parent->GetAttributes());
     if (style)
         SetAttributes(*style);
 }
 
-wxRichTextParagraph::wxRichTextParagraph(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style):
+wxRichTextParagraph::wxRichTextParagraph(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* paraStyle, wxTextAttrEx* charStyle):
     wxRichTextBox(parent)
 {
-    if (parent && !style)
-        SetAttributes(parent->GetAttributes());
-    if (style)
-        SetAttributes(*style);
+    if (paraStyle)
+        SetAttributes(*paraStyle);
 
-    AppendChild(new wxRichTextPlainText(text, this));
+    AppendChild(new wxRichTextPlainText(text, this, charStyle));
 }
 
 wxRichTextParagraph::~wxRichTextParagraph()
@@ -2998,11 +3014,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)
 {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     wxTextAttrEx attr = GetCombinedAttributes();
-#else
-    const wxTextAttrEx& attr = GetAttributes();
-#endif
 
     // Draw the bullet, if any
     if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
@@ -3014,6 +3026,16 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
 
             wxTextAttrEx bulletAttr(GetCombinedAttributes());
 
+            // Combine with the font of the first piece of content, if one is specified
+            if (GetChildren().GetCount() > 0)
+            {
+                wxRichTextObject* firstObj = (wxRichTextObject*) GetChildren().GetFirst()->GetData();
+                if (firstObj->GetAttributes().HasFont())
+                {
+                    wxRichTextApplyStyle(bulletAttr, firstObj->GetAttributes());
+                }
+            }
+
             // Get line height from first line, if any
             wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : (wxRichTextLine*) NULL;
 
@@ -3046,15 +3068,15 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
                 if (wxRichTextBuffer::GetRenderer())
                     wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc, bulletAttr, bulletRect);
             }
-            else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD)                
-            {                
+            else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD)
+            {
                 if (wxRichTextBuffer::GetRenderer())
                     wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc, bulletAttr, bulletRect);
             }
             else
             {
                 wxString bulletText = GetBulletText();
-                
+
                 if (!bulletText.empty() && wxRichTextBuffer::GetRenderer())
                     wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc, bulletAttr, bulletRect, bulletText);
             }
@@ -3081,7 +3103,7 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
         while (node2)
         {
             wxRichTextObject* child = node2->GetData();
-            
+
             if (!child->GetRange().IsOutside(lineRange) && !lineRange.IsOutside(range))
             {
                 // Draw this part of the line at the correct position
@@ -3114,11 +3136,7 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
 /// Lay the item out
 bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style)
 {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     wxTextAttrEx attr = GetCombinedAttributes();
-#else
-    const wxTextAttrEx& attr = GetAttributes();
-#endif
 
     // ClearLines();
 
@@ -3132,7 +3150,7 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style)
     int lineSpacing = 0;
 
     // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
-    if (attr.GetLineSpacing() > 10 && attr.GetFont().Ok())
+    if (attr.GetLineSpacing() != 10 && attr.GetFont().Ok())
     {
         dc.SetFont(attr.GetFont());
         lineSpacing = (ConvertTenthsMMToPixels(dc, dc.GetCharHeight()) * attr.GetLineSpacing())/10;
@@ -3187,6 +3205,7 @@ 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
@@ -3198,17 +3217,31 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style)
         // and found a suitable point some way into the child. So get the size for the fragment
         // if necessary.
 
+        long nextBreakPos = GetFirstLineBreakPosition(lastEndPos+1);
+        long lastPosToUse = child->GetRange().GetEnd();
+        bool lineBreakInThisObject = (nextBreakPos > -1 && nextBreakPos <= child->GetRange().GetEnd());
+
+        if (lineBreakInThisObject)
+            lastPosToUse = nextBreakPos;
+
         wxSize childSize;
         int childDescent = 0;
-        if (lastEndPos == child->GetRange().GetStart() - 1)
+
+        if ((nextBreakPos == -1) && (lastEndPos == child->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
         {
             childSize = child->GetCachedSize();
             childDescent = child->GetDescent();
         }
         else
-            GetRangeSize(wxRichTextRange(lastEndPos+1, child->GetRange().GetEnd()), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED,rect.GetPosition());
+            GetRangeSize(wxRichTextRange(lastEndPos+1, lastPosToUse), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED, rect.GetPosition());
+
+        // Cases:
+        // 1) There was a line break BEFORE the natural break
+        // 2) There was a line break AFTER the natural break
+        // 3) The child still fits (carry on)
 
-        if (childSize.x + currentWidth > availableSpaceForText)
+        if ((lineBreakInThisObject && (childSize.x + currentWidth <= availableSpaceForText)) ||
+            (childSize.x + currentWidth > availableSpaceForText))
         {
             long wrapPosition = 0;
 
@@ -3465,7 +3498,7 @@ bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& siz
                 rangeToUse.LimitTo(child->GetRange());
                 int childDescent = 0;
 
-                if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
+                if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, wxPoint(position.x + sz.x, position.y)))
                 {
                     sz.y = wxMax(sz.y, childSize.y);
                     sz.x += childSize.x;
@@ -3510,7 +3543,7 @@ bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& siz
 
                         wxSize childSize;
                         int childDescent = 0;
-                        if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
+                        if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, wxPoint(position.x + sz.x, position.y)))
                         {
                             lineSize.y = wxMax(lineSize.y, childSize.y);
                             lineSize.x += childSize.x;
@@ -3644,12 +3677,12 @@ int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition
             if (pt.x < linePos.x)
             {
                 textPosition = lineRange.GetStart();
-                return wxRICHTEXT_HITTEST_BEFORE;
+                return wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_OUTSIDE;
             }
             else if (pt.x >= (linePos.x + lineSize.x))
             {
                 textPosition = lineRange.GetEnd();
-                return wxRICHTEXT_HITTEST_AFTER;
+                return wxRICHTEXT_HITTEST_AFTER|wxRICHTEXT_HITTEST_OUTSIDE;
             }
             else
             {
@@ -3878,11 +3911,21 @@ bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& d
     wxString plainText;
     if (GetContiguousPlainText(plainText, wxRichTextRange(range.GetStart(), breakPosition), false))
     {
-        int spacePos = plainText.Find(wxT(' '), true);
-        if (spacePos != wxNOT_FOUND)
+        int newLinePos = plainText.Find(wxRichTextLineBreakChar);
+        if (newLinePos != wxNOT_FOUND)
+        {
+            breakPosition = wxMax(0, range.GetStart() + newLinePos);
+        }
+        else
         {
-            int positionsFromEndOfString = plainText.length() - spacePos - 1;
-            breakPosition = breakPosition - positionsFromEndOfString;
+            int spacePos = plainText.Find(wxT(' '), true);
+            int tabPos = plainText.Find(wxT('\t'), true);
+            int pos = wxMax(spacePos, tabPos);
+            if (pos != wxNOT_FOUND)
+            {
+                int positionsFromEndOfString = plainText.length() - pos - 1;
+                breakPosition = breakPosition - positionsFromEndOfString;
+            }
         }
     }
 
@@ -3928,7 +3971,7 @@ wxString wxRichTextParagraph::GetBulletText()
     {
         text = GetAttributes().GetBulletText();
     }
-    
+
     if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE)
     {
         // The outline style relies on the text being computed statically,
@@ -4040,6 +4083,27 @@ void wxRichTextParagraph::ClearDefaultTabs()
     sm_defaultTabs.Clear();
 }
 
+/// Get the first position from pos that has a line break character.
+long wxRichTextParagraph::GetFirstLineBreakPosition(long pos)
+{
+    wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
+    while (node)
+    {
+        wxRichTextObject* obj = node->GetData();
+        if (pos >= obj->GetRange().GetStart() && pos <= obj->GetRange().GetEnd())
+        {
+            wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
+            if (textObj)
+            {
+                long breakPos = textObj->GetFirstLineBreakPosition(pos);
+                if (breakPos > -1)
+                    return breakPos;
+            }
+        }
+        node = node->GetNext();
+    }
+    return -1;
+}
 
 /*!
  * wxRichTextLine
@@ -4093,8 +4157,6 @@ IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText, wxRichTextObject)
 wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style):
     wxRichTextObject(parent)
 {
-    if (parent && !style)
-        SetAttributes(parent->GetAttributes());
     if (style)
         SetAttributes(*style);
 
@@ -4103,22 +4165,28 @@ wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject*
 
 #define USE_KERNING_FIX 1
 
+// If insufficient tabs are defined, this is the tab width used
+#define WIDTH_FOR_DEFAULT_TABS 50
+
 /// Draw the item
 bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int WXUNUSED(style))
 {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
     wxASSERT (para != NULL);
 
     wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
-#else
-    wxTextAttrEx textAttr(GetAttributes());
-#endif
 
     int offset = GetRange().GetStart();
 
+    // Replace line break characters with spaces
+    wxString str = m_text;
+    wxString toRemove = wxRichTextLineBreakChar;
+    str.Replace(toRemove, wxT(" "));
+
     long len = range.GetLength();
-    wxString stringChunk = m_text.Mid(range.GetStart() - offset, (size_t) len);
+    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();
 
@@ -4157,7 +4225,7 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
             int fragmentLen = s1 - r1 + 1;
             if (fragmentLen < 0)
                 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 - offset), (int)fragmentLen);
-            wxString stringFragment = m_text.Mid(r1 - offset, fragmentLen);
+            wxString stringFragment = str.Mid(r1 - offset, fragmentLen);
 
             DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
 
@@ -4165,8 +4233,8 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
             if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
             {
                 // Compensate for kerning difference
-                wxString stringFragment2(m_text.Mid(r1 - offset, fragmentLen+1));
-                wxString stringFragment3(m_text.Mid(r1 - offset + fragmentLen, 1));
+                wxString stringFragment2(str.Mid(r1 - offset, fragmentLen+1));
+                wxString stringFragment3(str.Mid(r1 - offset + fragmentLen, 1));
 
                 wxCoord w1, h1, w2, h2, w3, h3;
                 dc.GetTextExtent(stringFragment,  & w1, & h1);
@@ -4188,7 +4256,7 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
             int fragmentLen = s2 - s1 + 1;
             if (fragmentLen < 0)
                 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 - offset), (int)fragmentLen);
-            wxString stringFragment = m_text.Mid(s1 - offset, fragmentLen);
+            wxString stringFragment = str.Mid(s1 - offset, fragmentLen);
 
             DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, true);
 
@@ -4196,8 +4264,8 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
             if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
             {
                 // Compensate for kerning difference
-                wxString stringFragment2(m_text.Mid(s1 - offset, fragmentLen+1));
-                wxString stringFragment3(m_text.Mid(s1 - offset + fragmentLen, 1));
+                wxString stringFragment2(str.Mid(s1 - offset, fragmentLen+1));
+                wxString stringFragment3(str.Mid(s1 - offset + fragmentLen, 1));
 
                 wxCoord w1, h1, w2, h2, w3, h3;
                 dc.GetTextExtent(stringFragment,  & w1, & h1);
@@ -4219,7 +4287,7 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
             int fragmentLen = r2 - s2 + 1;
             if (fragmentLen < 0)
                 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 - offset), (int)fragmentLen);
-            wxString stringFragment = m_text.Mid(s2 - offset, fragmentLen);
+            wxString stringFragment = str.Mid(s2 - offset, fragmentLen);
 
             DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
         }
@@ -4266,7 +4334,14 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, c
     else
     {
         dc.SetTextForeground(attr.GetTextColour());
-        dc.SetBackgroundMode(wxTRANSPARENT);
+
+        if (attr.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR) && attr.GetBackgroundColour().IsOk())
+        {
+            dc.SetBackgroundMode(wxSOLID);
+            dc.SetTextBackground(attr.GetBackgroundColour());
+        }
+        else
+            dc.SetBackgroundMode(wxTRANSPARENT);
     }
 
     while (hasTabs)
@@ -4281,8 +4356,18 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, c
         for (int i = 0; i < tabCount && not_found; ++i)
         {
             nextTabPos = tabArray.Item(i);
-            if (nextTabPos > tabPos)
+
+            // Find the next tab position.
+            // Even if we're at the end of the tab array, we must still draw the chunk.
+
+            if (nextTabPos > tabPos || (i == (tabCount - 1)))
             {
+                if (nextTabPos <= tabPos)
+                {
+                    int defaultTabWidth = ConvertTenthsMMToPixels(dc, WIDTH_FOR_DEFAULT_TABS);
+                    nextTabPos = tabPos + defaultTabWidth;
+                }
+
                 not_found = false;
                 if (selected)
                 {
@@ -4291,6 +4376,15 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, c
                     dc.DrawRectangle(selRect);
                 }
                 dc.DrawText(stringChunk, x, y);
+
+                if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH))
+                {
+                    wxPen oldPen = dc.GetPen();
+                    dc.SetPen(wxPen(attr.GetTextColour(), 1));
+                    dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5));
+                    dc.SetPen(oldPen);
+                }
+
                 x = nextTabPos;
             }
         }
@@ -4306,6 +4400,15 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, c
             dc.DrawRectangle(selRect);
         }
         dc.DrawText(str, x, y);
+
+        if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH))
+        {
+            wxPen oldPen = dc.GetPen();
+            dc.SetPen(wxPen(attr.GetTextColour(), 1));
+            dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5));
+            dc.SetPen(oldPen);
+        }
+
         x += w;
     }
     return true;
@@ -4315,21 +4418,7 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, c
 /// Lay the item out
 bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style))
 {
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
-    wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
-    wxASSERT (para != NULL);
-
-    wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
-#else
-    wxTextAttrEx textAttr(GetAttributes());
-#endif
-
-    if (textAttr.GetFont().Ok())
-        dc.SetFont(textAttr.GetFont());
-
-    wxCoord w, h;
-    dc.GetTextExtent(m_text, & w, & h, & m_descent);
-    m_size = wxSize(w, dc.GetCharHeight());
+    GetRangeSize(GetRange(), m_size, m_descent, dc, 0, wxPoint(0, 0));
 
     return true;
 }
@@ -4349,14 +4438,10 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz
     if (!range.IsWithin(GetRange()))
         return false;
 
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
     wxASSERT (para != NULL);
 
     wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
-#else
-    wxTextAttrEx textAttr(GetAttributes());
-#endif
 
     // Always assume unformatted text, since at this level we have no knowledge
     // of line breaks - and we don't need it, since we'll calculate size within
@@ -4367,7 +4452,16 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz
 
     int startPos = range.GetStart() - GetRange().GetStart();
     long len = range.GetLength();
-    wxString stringChunk = m_text.Mid(startPos, (size_t) len);
+
+    wxString str(m_text);
+    wxString toReplace = wxRichTextLineBreakChar;
+    str.Replace(toReplace, wxT(" "));
+
+    wxString stringChunk = str.Mid(startPos, (size_t) len);
+
+    if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS))
+        stringChunk.MakeUpper();
+
     wxCoord w, h;
     int width = 0;
     if (stringChunk.Find(wxT('\t')) != wxNOT_FOUND)
@@ -4399,12 +4493,23 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz
             dc.GetTextExtent(stringFragment, & w, & h);
             width += w;
             int absoluteWidth = width + position.x;
+
             bool notFound = true;
             for (int i = 0; i < tabCount && notFound; ++i)
             {
                 nextTabPos = tabArray.Item(i);
-                if (nextTabPos > absoluteWidth)
+
+                // Find the next tab position.
+                // Even if we're at the end of the tab array, we must still process the chunk.
+
+                if (nextTabPos > absoluteWidth || (i == (tabCount - 1)))
                 {
+                    if (nextTabPos <= absoluteWidth)
+                    {
+                        int defaultTabWidth = ((wxRichTextPlainText*) this)->ConvertTenthsMMToPixels(dc, WIDTH_FOR_DEFAULT_TABS);
+                        nextTabPos = absoluteWidth + defaultTabWidth;
+                    }
+
                     notFound = false;
                     width = nextTabPos - position.x;
                 }
@@ -4422,7 +4527,8 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz
 /// the first part in 'this'.
 wxRichTextObject* wxRichTextPlainText::DoSplit(long pos)
 {
-    int index = pos - GetRange().GetStart();
+    long index = pos - GetRange().GetStart();
+
     if (index < 0 || index >= (int) m_text.length())
         return NULL;
 
@@ -4510,6 +4616,23 @@ void wxRichTextPlainText::Dump(wxTextOutputStream& stream)
     stream << m_text << wxT("\n");
 }
 
+/// Get the first position from pos that has a line break character.
+long wxRichTextPlainText::GetFirstLineBreakPosition(long pos)
+{
+    int i;
+    int len = m_text.length();
+    int startPos = pos - m_range.GetStart();
+    for (i = startPos; i < len; i++)
+    {
+        wxChar ch = m_text[i];
+        if (ch == wxRichTextLineBreakChar)
+        {
+            return i + m_range.GetStart();
+        }
+    }
+    return -1;
+}
+
 /*!
  * wxRichTextBuffer
  * This is a kind of box, used to represent the whole buffer
@@ -4545,19 +4668,12 @@ wxRichTextBuffer::~wxRichTextBuffer()
     ClearEventHandlers();
 }
 
-void wxRichTextBuffer::Clear()
+void wxRichTextBuffer::ResetAndClearCommands()
 {
-    DeleteChildren();
-    GetCommandProcessor()->ClearCommands();
-    Modify(false);
-    Invalidate(wxRICHTEXT_ALL);
-}
+    Reset();
 
-void wxRichTextBuffer::Reset()
-{
-    DeleteChildren();
-    AddParagraph(wxEmptyString);
     GetCommandProcessor()->ClearCommands();
+
     Modify(false);
     Invalidate(wxRICHTEXT_ALL);
 }
@@ -4604,6 +4720,8 @@ bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagr
 {
     wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
 
+    wxTextAttrEx attr(GetDefaultStyle());
+
     wxTextAttrEx* p = NULL;
     wxTextAttrEx paraAttr;
     if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
@@ -4612,13 +4730,8 @@ bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagr
         if (!paraAttr.IsDefault())
             p = & paraAttr;
     }
-
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
-    wxTextAttrEx attr(GetDefaultStyle());
-#else
-    wxTextAttrEx attr(GetBasicStyle());
-    wxRichTextApplyStyle(attr, GetDefaultStyle());
-#endif
+    else
+        p = & attr;
 
     action->GetNewParagraphs() = paragraphs;
 
@@ -4652,18 +4765,12 @@ bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRich
     wxTextAttrEx paraAttr;
     if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
     {
-        paraAttr = GetStyleForNewParagraph(pos);
+        // Get appropriate paragraph style
+        paraAttr = GetStyleForNewParagraph(pos, false, false);
         if (!paraAttr.IsDefault())
             p = & paraAttr;
     }
 
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
-    wxTextAttrEx attr(GetDefaultStyle());
-#else
-    wxTextAttrEx attr(GetBasicStyle());
-    wxRichTextApplyStyle(attr, GetDefaultStyle());
-#endif
-
     action->GetNewParagraphs().AddParagraphs(text, p);
 
     int length = action->GetNewParagraphs().GetRange().GetLength();
@@ -4674,6 +4781,8 @@ bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRich
         length --;
         action->GetNewParagraphs().SetPartialParagraph(true);
     }
+    else if (text.length() > 0 && text.Last() == wxT('\n'))
+        length --;
 
     action->SetPosition(pos);
 
@@ -4694,17 +4803,12 @@ bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int
     wxTextAttrEx paraAttr;
     if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
     {
-        paraAttr = GetStyleForNewParagraph(pos);
+        paraAttr = GetStyleForNewParagraph(pos, false, true /* look for next paragraph style */);
         if (!paraAttr.IsDefault())
             p = & paraAttr;
     }
 
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     wxTextAttrEx attr(GetDefaultStyle());
-#else
-    wxTextAttrEx attr(GetBasicStyle());
-    wxRichTextApplyStyle(attr, GetDefaultStyle());
-#endif
 
     wxRichTextParagraph* newPara = new wxRichTextParagraph(wxEmptyString, this, & attr);
     action->GetNewParagraphs().AppendChild(newPara);
@@ -4737,12 +4841,7 @@ bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock&
             p = & paraAttr;
     }
 
-#if wxRICHTEXT_USE_DYNAMIC_STYLES
     wxTextAttrEx attr(GetDefaultStyle());
-#else
-    wxTextAttrEx attr(GetBasicStyle());
-    wxRichTextApplyStyle(attr, GetDefaultStyle());
-#endif
 
     wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
     if (p)
@@ -4768,16 +4867,16 @@ bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock&
 /// Get the style that is appropriate for a new paragraph at this position.
 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
 /// style.
-wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPosition) const
+wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPosition, bool lookUpNewParaStyle) const
 {
     wxRichTextParagraph* para = GetParagraphAtPosition(pos, caretPosition);
     if (para)
     {
         wxRichTextAttr attr;
         bool foundAttributes = false;
-        
+
         // Look for a matching paragraph style
-        if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
+        if (lookUpNewParaStyle && !para->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
         {
             wxRichTextParagraphStyleDefinition* paraDef = GetStyleSheet()->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
             if (paraDef)
@@ -4788,15 +4887,15 @@ wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPos
                     if (nextParaDef)
                     {
                         foundAttributes = true;
-                        attr = nextParaDef->GetStyle();
+                        attr = nextParaDef->GetStyleMergedWithBase(GetStyleSheet());
                     }
                 }
-                
+
                 // If we didn't find the 'next style', use this style instead.
                 if (!foundAttributes)
                 {
                     foundAttributes = true;
-                    attr = paraDef->GetStyle();
+                    attr = paraDef->GetStyleMergedWithBase(GetStyleSheet());
                 }
             }
         }
@@ -4811,13 +4910,13 @@ wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPos
                     (~ wxTEXT_ATTR_BACKGROUND_COLOUR) );
             attr.SetFlags(flags);
         }
-        
+
         // Now see if we need to number the paragraph.
         if (attr.HasBulletStyle())
         {
             wxRichTextAttr numberingAttr;
             if (FindNextParagraphNumber(para, numberingAttr))
-                wxRichTextApplyStyle(attr, numberingAttr);
+                wxRichTextApplyStyle(attr, (const wxRichTextAttr&) numberingAttr);
         }
 
         return attr;
@@ -4827,11 +4926,11 @@ wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPos
 }
 
 /// Submit command to delete this range
-bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, long initialCaretPosition, long WXUNUSED(newCaretPositon), wxRichTextCtrl* ctrl)
+bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, wxRichTextCtrl* ctrl)
 {
     wxRichTextAction* action = new wxRichTextAction(NULL, _("Delete"), wxRICHTEXT_DELETE, this, ctrl);
 
-    action->SetPosition(initialCaretPosition);
+    action->SetPosition(ctrl->GetCaretPosition());
 
     // Set the range to delete
     action->SetRange(range);
@@ -5154,8 +5253,7 @@ bool wxRichTextBuffer::BeginCharacterStyle(const wxString& characterStyle)
         wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
         if (def)
         {
-            wxTextAttrEx attr;
-            def->GetStyle().CopyTo(attr);
+            wxTextAttrEx attr = def->GetStyleMergedWithBase(GetStyleSheet());
             return BeginStyle(attr);
         }
     }
@@ -5170,8 +5268,7 @@ bool wxRichTextBuffer::BeginParagraphStyle(const wxString& paragraphStyle)
         wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(paragraphStyle);
         if (def)
         {
-            wxTextAttrEx attr;
-            def->GetStyle().CopyTo(attr);
+            wxTextAttrEx attr = def->GetStyleMergedWithBase(GetStyleSheet());
             return BeginStyle(attr);
         }
     }
@@ -5206,7 +5303,7 @@ bool wxRichTextBuffer::BeginURL(const wxString& url, const wxString& characterSt
         wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
         if (def)
         {
-            def->GetStyle().CopyTo(attr);
+            attr = def->GetStyleMergedWithBase(GetStyleSheet());
         }
     }
     attr.SetURL(url);
@@ -5578,7 +5675,7 @@ bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler* handler, bool deleteHand
         m_eventHandlers.Erase(node);
         if (deleteHandler)
             delete handler;
-        
+
         return true;
     }
     else
@@ -5613,17 +5710,17 @@ bool wxRichTextBuffer::SendEvent(wxEvent& event, bool sendToAll)
 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet* sheet)
 {
     wxRichTextStyleSheet* oldSheet = GetStyleSheet();
-    
+
     wxWindowID id = wxID_ANY;
     if (GetRichTextCtrl())
         id = GetRichTextCtrl()->GetId();
-    
+
     wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, id);
     event.SetEventObject(GetRichTextCtrl());
     event.SetOldStyleSheet(oldSheet);
     event.SetNewStyleSheet(sheet);
     event.Allow();
-    
+
     if (SendEvent(event) && !event.IsAllowed())
     {
         if (sheet != oldSheet)
@@ -5674,26 +5771,26 @@ bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, w
     dc.SetFont(font);
 
     int charHeight = dc.GetCharHeight();
-                
+
     int bulletWidth = (int) (((float) charHeight) * wxRichTextBuffer::GetBulletProportion());
     int bulletHeight = bulletWidth;
 
     int x = rect.x;
-    
+
     // Calculate the top position of the character (as opposed to the whole line height)
     int y = rect.y + (rect.height - charHeight);
-    
+
     // Calculate where the bullet should be positioned
     y = y + (charHeight+1)/2 - (bulletHeight+1)/2;
-                
+
     // The margin between a bullet and text.
     int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
-                
+
     if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
         x = rect.x + rect.width - bulletWidth - margin;
     else if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
         x = x + (rect.width)/2 - bulletWidth/2;
-                
+
     if (bulletAttr.GetBulletName() == wxT("standard/square"))
     {
         dc.DrawRectangle(x, y, bulletWidth, bulletHeight);
@@ -5705,7 +5802,7 @@ bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, w
         pts[1].x = x + bulletWidth/2;   pts[1].y = y;
         pts[2].x = x + bulletWidth;     pts[2].y = y + bulletHeight/2;
         pts[3].x = x + bulletWidth/2;   pts[3].y = y + bulletHeight;
-                    
+
         dc.DrawPolygon(4, pts);
     }
     else if (bulletAttr.GetBulletName() == wxT("standard/triangle"))
@@ -5714,14 +5811,14 @@ bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, w
         pts[0].x = x;                   pts[0].y = y;
         pts[1].x = x + bulletWidth;     pts[1].y = y + bulletHeight/2;
         pts[2].x = x;                   pts[2].y = y + bulletHeight;
-                    
+
         dc.DrawPolygon(3, pts);
     }
     else // "standard/circle", and catch-all
     {
         dc.DrawEllipse(x, y, bulletWidth, bulletHeight);
-    }                
+    }
+
     return true;
 }
 
@@ -5755,18 +5852,18 @@ bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph* paragraph, wxDC&
         int x = rect.x;
 
         // Calculate the top position of the character (as opposed to the whole line height)
-        int y = rect.y + (rect.height - charHeight);    
+        int y = rect.y + (rect.height - charHeight);
 
         // The margin between a bullet and text.
         int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
-                
+
         if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
             x = (rect.x + rect.width) - tw - margin;
         else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
             x = x + (rect.width)/2 - tw/2;
 
         dc.DrawText(text, x, y);
-        
+
         return true;
     }
     else
@@ -5806,7 +5903,7 @@ public:
         wxRichTextBuffer::InitStandardHandlers();
         wxRichTextParagraph::InitDefaultTabs();
         return true;
-    };
+    }
     void OnExit()
     {
         wxRichTextBuffer::CleanUpHandlers();
@@ -5814,7 +5911,7 @@ public:
         wxRichTextParagraph::ClearDefaultTabs();
         wxRichTextCtrl::ClearAvailableFontNames();
         wxRichTextBuffer::SetRenderer(NULL);
-    };
+    }
 };
 
 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule)
@@ -5916,22 +6013,92 @@ bool wxRichTextAction::Do()
     {
     case wxRICHTEXT_INSERT:
         {
-            m_buffer->InsertFragment(GetPosition(), m_newParagraphs);
-            m_buffer->UpdateRanges();
-            m_buffer->Invalidate(GetRange());
-
-            long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength();
+            // Store a list of line start character and y positions so we can figure out which area
+            // we need to refresh
+            wxArrayInt optimizationLineCharPositions;
+            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;
 
-            // Character position to caret position
-            newCaretPosition --;
+                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();
 
-            // Don't take into account the last newline
+                        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
+
+            m_buffer->InsertFragment(GetPosition(), m_newParagraphs);
+            m_buffer->UpdateRanges();
+            m_buffer->Invalidate(GetRange());
+
+            long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength();
+
+            // Character position to caret position
+            newCaretPosition --;
+
+            // Don't take into account the last newline
             if (m_newParagraphs.GetPartialParagraph())
                 newCaretPosition --;
+            else
+                if (m_newParagraphs.GetChildren().GetCount() > 1)
+                {
+                    wxRichTextObject* p = (wxRichTextObject*) m_newParagraphs.GetChildren().GetLast()->GetData();
+                    if (p->GetRange().GetLength() == 1)
+                        newCaretPosition --;
+                }
 
             newCaretPosition = wxMin(newCaretPosition, (m_buffer->GetRange().GetEnd()-1));
 
-            UpdateAppearance(newCaretPosition, true /* send update event */);
+            if (optimizationLineCharPositions.GetCount() > 0)
+                UpdateAppearance(newCaretPosition, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions);
+            else
+                UpdateAppearance(newCaretPosition, true /* send update event */);
+
+            wxRichTextEvent cmdEvent(
+                wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED,
+                m_ctrl ? m_ctrl->GetId() : -1);
+            cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+            cmdEvent.SetRange(GetRange());
+            cmdEvent.SetPosition(GetRange().GetStart());
+
+            m_buffer->SendEvent(cmdEvent);
 
             break;
         }
@@ -5943,6 +6110,15 @@ bool wxRichTextAction::Do()
 
             UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
 
+            wxRichTextEvent cmdEvent(
+                wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED,
+                m_ctrl ? m_ctrl->GetId() : -1);
+            cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+            cmdEvent.SetRange(GetRange());
+            cmdEvent.SetPosition(GetRange().GetStart());
+
+            m_buffer->SendEvent(cmdEvent);
+
             break;
         }
     case wxRICHTEXT_CHANGE_STYLE:
@@ -5952,6 +6128,15 @@ bool wxRichTextAction::Do()
 
             UpdateAppearance(GetPosition());
 
+            wxRichTextEvent cmdEvent(
+                wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED,
+                m_ctrl ? m_ctrl->GetId() : -1);
+            cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+            cmdEvent.SetRange(GetRange());
+            cmdEvent.SetPosition(GetRange().GetStart());
+
+            m_buffer->SendEvent(cmdEvent);
+
             break;
         }
     default:
@@ -5974,11 +6159,18 @@ bool wxRichTextAction::Undo()
             m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
 
             long newCaretPosition = GetPosition() - 1;
-            // if (m_newParagraphs.GetPartialParagraph())
-            //    newCaretPosition --;
 
             UpdateAppearance(newCaretPosition, true /* send update event */);
 
+            wxRichTextEvent cmdEvent(
+                wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED,
+                m_ctrl ? m_ctrl->GetId() : -1);
+            cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+            cmdEvent.SetRange(GetRange());
+            cmdEvent.SetPosition(GetRange().GetStart());
+
+            m_buffer->SendEvent(cmdEvent);
+
             break;
         }
     case wxRICHTEXT_DELETE:
@@ -5989,6 +6181,15 @@ bool wxRichTextAction::Undo()
 
             UpdateAppearance(GetPosition(), true /* send update event */);
 
+            wxRichTextEvent cmdEvent(
+                wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED,
+                m_ctrl ? m_ctrl->GetId() : -1);
+            cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+            cmdEvent.SetRange(GetRange());
+            cmdEvent.SetPosition(GetRange().GetStart());
+
+            m_buffer->SendEvent(cmdEvent);
+
             break;
         }
     case wxRICHTEXT_CHANGE_STYLE:
@@ -5998,6 +6199,15 @@ bool wxRichTextAction::Undo()
 
             UpdateAppearance(GetPosition());
 
+            wxRichTextEvent cmdEvent(
+                wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED,
+                m_ctrl ? m_ctrl->GetId() : -1);
+            cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
+            cmdEvent.SetRange(GetRange());
+            cmdEvent.SetPosition(GetRange().GetStart());
+
+            m_buffer->SendEvent(cmdEvent);
+
             break;
         }
     default:
@@ -6008,7 +6218,7 @@ bool wxRichTextAction::Undo()
 }
 
 /// Update the control appearance
-void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent)
+void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent, wxArrayInt* optimizationLineCharPositions, wxArrayInt* optimizationLineYPositions)
 {
     if (m_ctrl)
     {
@@ -6017,7 +6227,99 @@ void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent
         {
             m_ctrl->LayoutContent();
             m_ctrl->PositionCaret();
-            m_ctrl->Refresh(false);
+
+#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)
+            {
+                size_t i;
+
+                wxSize clientSize = m_ctrl->GetClientSize();
+                wxPoint firstVisiblePt = m_ctrl->GetFirstVisiblePoint();
+
+                // Start/end positions
+                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();
+
+                // 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());
+                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();
+
+                        // we want to find the first line that is in the same position
+                        // as before. This will mean we're at the end of the changed text.
+
+                        if (pt.y > lastY) // going past the end of the window, no more info
+                        {
+                            node2 = wxRichTextLineList::compatibility_iterator();
+                            node = wxRichTextObjectList::compatibility_iterator();
+                        }
+                        else
+                        {
+                            if (!foundStart)
+                            {
+                                firstY = pt.y - firstVisiblePt.y;
+                                foundStart = true;
+                            }
+
+                            // search for this line being at the same position as before
+                            for (i = 0; i < optimizationLineCharPositions->GetCount(); i++)
+                            {
+                                if (((*optimizationLineCharPositions)[i] + positionOffset == range.GetStart()) &&
+                                    ((*optimizationLineYPositions)[i] == pt.y))
+                                {
+                                    // Stop, we're now the same as we were
+                                    foundEnd = true;
+                                    lastY = pt.y - firstVisiblePt.y;
+
+                                    node2 = wxRichTextLineList::compatibility_iterator();
+                                    node = wxRichTextObjectList::compatibility_iterator();
+
+                                    break;
+                                }
+                            }
+                        }
+
+                        if (node2)
+                            node2 = node2->GetNext();
+                    }
+
+                    if (node)
+                        node = node->GetNext();
+                }
+
+                if (!foundStart)
+                    firstY = firstVisiblePt.y;
+                if (!foundEnd)
+                    lastY = firstVisiblePt.y + clientSize.y;
+
+                wxRect rect(firstVisiblePt.x, firstY, firstVisiblePt.x + 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 (sendUpdateEvent)
                 m_ctrl->SendTextUpdatedEvent();
@@ -6082,17 +6384,21 @@ bool wxRichTextRange::LimitTo(const wxRichTextRange& range)
 
 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject)
 
-wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent):
+wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent, wxTextAttrEx* charStyle):
     wxRichTextObject(parent)
 {
     m_image = image;
+    if (charStyle)
+        SetAttributes(*charStyle);
 }
 
-wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent):
+wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent, wxTextAttrEx* charStyle):
     wxRichTextObject(parent)
 {
     m_imageBlock = imageBlock;
     m_imageBlock.Load(m_image);
+    if (charStyle)
+        SetAttributes(*charStyle);
 }
 
 /// Load wxImage from the block
@@ -6203,6 +6509,8 @@ bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2)
         attr1.GetFont().GetWeight() == attr2.GetFontWeight() &&
         attr1.GetFont().GetFaceName() == attr2.GetFontFaceName() &&
         attr1.GetFont().GetUnderlined() == attr2.GetFontUnderlined() &&
+        attr1.GetTextEffects() == attr2.GetTextEffects() &&
+        attr1.GetTextEffectFlags() == attr2.GetTextEffectFlags() &&
         attr1.GetAlignment() == attr2.GetAlignment() &&
         attr1.GetLeftIndent() == attr2.GetLeftIndent() &&
         attr1.GetRightIndent() == attr2.GetRightIndent() &&
@@ -6216,9 +6524,11 @@ bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2)
         attr1.GetBulletText() == attr2.GetBulletText() &&
         attr1.GetBulletName() == attr2.GetBulletName() &&
         attr1.GetBulletFont() == attr2.GetBulletFont() &&
+        attr1.GetOutlineLevel() == attr2.GetOutlineLevel() &&
         attr1.GetCharacterStyleName() == attr2.GetCharacterStyleName() &&
         attr1.GetParagraphStyleName() == attr2.GetParagraphStyleName() &&
-        attr1.GetListStyleName() == attr2.GetListStyleName());
+        attr1.GetListStyleName() == attr2.GetListStyleName() &&
+        attr1.HasPageBreak() == attr2.HasPageBreak());
 }
 
 /// Compare two attribute objects, but take into account the flags
@@ -6307,6 +6617,22 @@ bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, i
         !wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()))
         return false;
 
+    if ((flags & wxTEXT_ATTR_PAGE_BREAK) &&
+        (attr1.HasPageBreak() != attr2.HasPageBreak()))
+         return false;
+
+    if (flags & wxTEXT_ATTR_EFFECTS)
+    {
+        if (attr1.HasTextEffects() != attr2.HasTextEffects())
+            return false;
+        if (!wxRichTextBitlistsEqPartial(attr1.GetTextEffects(), attr2.GetTextEffects(), attr2.GetTextEffectFlags()))
+            return false;
+    }
+
+    if ((flags & wxTEXT_ATTR_OUTLINE_LEVEL) &&
+        (attr1.GetOutlineLevel() != attr2.GetOutlineLevel()))
+         return false;
+
     return true;
 }
 
@@ -6397,6 +6723,22 @@ bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2,
         !wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()))
         return false;
 
+    if ((flags & wxTEXT_ATTR_PAGE_BREAK) &&
+        (attr1.HasPageBreak() != attr2.HasPageBreak()))
+         return false;
+
+    if (flags & wxTEXT_ATTR_EFFECTS)
+    {
+        if (attr1.HasTextEffects() != attr2.HasTextEffects())
+            return false;
+        if (!wxRichTextBitlistsEqPartial(attr1.GetTextEffects(), attr2.GetTextEffects(), attr2.GetTextEffectFlags()))
+            return false;
+    }
+
+    if ((flags & wxTEXT_ATTR_OUTLINE_LEVEL) &&
+        (attr1.GetOutlineLevel() != attr2.GetOutlineLevel()))
+         return false;
+
     return true;
 }
 
@@ -6415,7 +6757,6 @@ bool wxRichTextTabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
     return true;
 }
 
-
 /// Apply one style to another
 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style)
 {
@@ -6520,18 +6861,43 @@ bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style)
     if (style.HasURL())
         destStyle.SetURL(style.GetURL());
 
+    if (style.HasPageBreak())
+        destStyle.SetPageBreak();
+
+    if (style.HasTextEffects())
+    {
+        int destBits = destStyle.GetTextEffects();
+        int destFlags = destStyle.GetTextEffectFlags();
+
+        int srcBits = style.GetTextEffects();
+        int srcFlags = style.GetTextEffectFlags();
+
+        wxRichTextCombineBitlists(destBits, srcBits, destFlags, srcFlags);
+
+        destStyle.SetTextEffects(destBits);
+        destStyle.SetTextEffectFlags(destFlags);
+    }
+
+    if (style.HasOutlineLevel())
+        destStyle.SetOutlineLevel(style.GetOutlineLevel());
+
     return true;
 }
 
 bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxTextAttrEx& style)
 {
-    wxTextAttrEx destStyle2;
-    destStyle.CopyTo(destStyle2);
+    wxTextAttrEx destStyle2 = destStyle;
     wxRichTextApplyStyle(destStyle2, style);
     destStyle = destStyle2;
     return true;
 }
 
+bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith)
+{
+    destStyle = destStyle.Combine(style, compareWith);
+    return true;
+}
+
 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith)
 {
     // Whole font. Avoiding setting individual attributes if possible, since
@@ -6547,7 +6913,7 @@ bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style,
 
         if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE)
         {
-            if (compareWith && compareWith->HasFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName())
+            if (compareWith && compareWith->HasFontFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName())
             {
                 // The same as currently displayed, so don't set
             }
@@ -6560,7 +6926,7 @@ bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style,
 
         if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE)
         {
-            if (compareWith && compareWith->HasSize() && compareWith->GetFontSize() == style.GetFontSize())
+            if (compareWith && compareWith->HasFontSize() && compareWith->GetFontSize() == style.GetFontSize())
             {
                 // The same as currently displayed, so don't set
             }
@@ -6573,7 +6939,7 @@ bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style,
 
         if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC)
         {
-            if (compareWith && compareWith->HasItalic() && compareWith->GetFontStyle() == style.GetFontStyle())
+            if (compareWith && compareWith->HasFontItalic() && compareWith->GetFontStyle() == style.GetFontStyle())
             {
                 // The same as currently displayed, so don't set
             }
@@ -6586,7 +6952,7 @@ bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style,
 
         if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT)
         {
-            if (compareWith && compareWith->HasWeight() && compareWith->GetFontWeight() == style.GetFontWeight())
+            if (compareWith && compareWith->HasFontWeight() && compareWith->GetFontWeight() == style.GetFontWeight())
             {
                 // The same as currently displayed, so don't set
             }
@@ -6599,7 +6965,7 @@ bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style,
 
         if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE)
         {
-            if (compareWith && compareWith->HasUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined())
+            if (compareWith && compareWith->HasFontUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined())
             {
                 // The same as currently displayed, so don't set
             }
@@ -6726,6 +7092,88 @@ bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style,
             destStyle.SetURL(style.GetURL());
     }
 
+    if (style.HasPageBreak())
+    {
+        if (!(compareWith && compareWith->HasPageBreak()))
+            destStyle.SetPageBreak();
+    }
+
+    if (style.HasTextEffects())
+    {
+        if (!(compareWith && compareWith->HasTextEffects() && compareWith->GetTextEffects() == style.GetTextEffects()))
+        {
+            int destBits = destStyle.GetTextEffects();
+            int destFlags = destStyle.GetTextEffectFlags();
+
+            int srcBits = style.GetTextEffects();
+            int srcFlags = style.GetTextEffectFlags();
+
+            wxRichTextCombineBitlists(destBits, srcBits, destFlags, srcFlags);
+
+            destStyle.SetTextEffects(destBits);
+            destStyle.SetTextEffectFlags(destFlags);
+        }
+    }
+
+    if (style.HasOutlineLevel())
+    {
+        if (!(compareWith && compareWith->HasOutlineLevel() && compareWith->GetOutlineLevel() == style.GetOutlineLevel()))
+            destStyle.SetOutlineLevel(style.GetOutlineLevel());
+    }
+
+    return true;
+}
+
+// Remove attributes
+bool wxRichTextRemoveStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style)
+{
+    int flags = style.GetFlags();
+    int destFlags = destStyle.GetFlags();
+
+    destStyle.SetFlags(destFlags & ~flags);
+
+    return true;
+}
+
+/// Combine two bitlists, specifying the bits of interest with separate flags.
+bool wxRichTextCombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
+{
+    // We want to apply B's bits to A, taking into account each's flags which indicate which bits
+    // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
+    // indicate it.
+
+    // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
+    // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
+    int valueA2 = ~(~valueB & flagsB) & valueA;
+
+    // Now combine the 1 bits.
+    int valueA3 = (valueB & flagsB) | valueA2;
+
+    valueA = valueA3;
+    flagsA = (flagsA | flagsB);
+
+    return true;
+}
+
+/// Compare two bitlists
+bool wxRichTextBitlistsEqPartial(int valueA, int valueB, int flags)
+{
+    int relevantBitsA = valueA & flags;
+    int relevantBitsB = valueB & flags;
+    return (relevantBitsA != relevantBitsB);
+}
+
+/// Split into paragraph and character styles
+bool wxRichTextSplitParaCharStyles(const wxTextAttrEx& style, wxTextAttrEx& parStyle, wxTextAttrEx& charStyle)
+{
+    wxTextAttrEx defaultCharStyle1(style);
+    wxTextAttrEx defaultParaStyle1(style);
+    defaultCharStyle1.SetFlags(defaultCharStyle1.GetFlags()&wxTEXT_ATTR_CHARACTER);
+    defaultParaStyle1.SetFlags(defaultParaStyle1.GetFlags()&wxTEXT_ATTR_PARAGRAPH);
+
+    wxRichTextApplyStyle(charStyle, defaultCharStyle1);
+    wxRichTextApplyStyle(parStyle, defaultParaStyle1);
+
     return true;
 }
 
@@ -6837,6 +7285,9 @@ void wxRichTextAttr::Init()
     m_paragraphSpacingBefore = 0;
     m_lineSpacing = 0;
     m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
+    m_textEffects = wxTEXT_ATTR_EFFECT_NONE;
+    m_textEffectFlags = wxTEXT_ATTR_EFFECT_NONE;
+    m_outlineLevel = 0;
     m_bulletNumber = 0;
 }
 
@@ -6857,6 +7308,8 @@ void wxRichTextAttr::Copy(const wxRichTextAttr& attr)
     m_fontWeight = attr.m_fontWeight;
     m_fontUnderlined = attr.m_fontUnderlined;
     m_fontFaceName = attr.m_fontFaceName;
+    m_textEffects = attr.m_textEffects;
+    m_textEffectFlags = attr.m_textEffectFlags;
 
     m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
     m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
@@ -6869,19 +7322,22 @@ void wxRichTextAttr::Copy(const wxRichTextAttr& attr)
     m_bulletText = attr.m_bulletText;
     m_bulletFont = attr.m_bulletFont;
     m_bulletName = attr.m_bulletName;
+    m_outlineLevel = attr.m_outlineLevel;
 
     m_urlTarget = attr.m_urlTarget;
 }
 
 // operators
 void wxRichTextAttr::operator= (const wxRichTextAttr& attr)
-{    
+{
     Copy(attr);
 }
 
 // operators
 void wxRichTextAttr::operator= (const wxTextAttrEx& attr)
 {
+    m_flags = attr.GetFlags();
+
     m_colText = attr.GetTextColour();
     m_colBack = attr.GetBackgroundColour();
     m_textAlignment = attr.GetAlignment();
@@ -6889,7 +7345,8 @@ void wxRichTextAttr::operator= (const wxTextAttrEx& attr)
     m_leftSubIndent = attr.GetLeftSubIndent();
     m_rightIndent = attr.GetRightIndent();
     m_tabs = attr.GetTabs();
-    m_flags = attr.GetFlags();
+    m_textEffects = attr.GetTextEffects();
+    m_textEffectFlags = attr.GetTextEffectFlags();
 
     m_paragraphSpacingAfter = attr.GetParagraphSpacingAfter();
     m_paragraphSpacingBefore = attr.GetParagraphSpacingBefore();
@@ -6902,6 +7359,7 @@ void wxRichTextAttr::operator= (const wxTextAttrEx& attr)
     m_bulletText = attr.GetBulletText();
     m_bulletName = attr.GetBulletName();
     m_bulletFont = attr.GetBulletFont();
+    m_outlineLevel = attr.GetOutlineLevel();
 
     m_urlTarget = attr.GetURL();
 
@@ -6913,7 +7371,32 @@ void wxRichTextAttr::operator= (const wxTextAttrEx& attr)
 wxRichTextAttr::operator wxTextAttrEx () const
 {
     wxTextAttrEx attr;
-    CopyTo(attr);
+    attr.SetTextColour(GetTextColour());
+    attr.SetBackgroundColour(GetBackgroundColour());
+    attr.SetAlignment(GetAlignment());
+    attr.SetTabs(GetTabs());
+    attr.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
+    attr.SetRightIndent(GetRightIndent());
+    attr.SetFont(CreateFont());
+
+    attr.SetParagraphSpacingAfter(m_paragraphSpacingAfter);
+    attr.SetParagraphSpacingBefore(m_paragraphSpacingBefore);
+    attr.SetLineSpacing(m_lineSpacing);
+    attr.SetBulletStyle(m_bulletStyle);
+    attr.SetBulletNumber(m_bulletNumber);
+    attr.SetBulletText(m_bulletText);
+    attr.SetBulletName(m_bulletName);
+    attr.SetBulletFont(m_bulletFont);
+    attr.SetCharacterStyleName(m_characterStyleName);
+    attr.SetParagraphStyleName(m_paragraphStyleName);
+    attr.SetListStyleName(m_listStyleName);
+    attr.SetTextEffects(m_textEffects);
+    attr.SetTextEffectFlags(m_textEffectFlags);
+    attr.SetOutlineLevel(m_outlineLevel);
+
+    attr.SetURL(m_urlTarget);
+
+    attr.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
     return attr;
 }
 
@@ -6944,41 +7427,18 @@ bool wxRichTextAttr::operator== (const wxRichTextAttr& attr) const
             GetBulletFont() == attr.GetBulletFont() &&
             GetBulletName() == attr.GetBulletName() &&
 
-            m_fontSize == attr.m_fontSize &&
-            m_fontStyle == attr.m_fontStyle &&
-            m_fontWeight == attr.m_fontWeight &&
-            m_fontUnderlined == attr.m_fontUnderlined &&
-            m_fontFaceName == attr.m_fontFaceName &&
-            
-            m_urlTarget == attr.m_urlTarget;
-}
+            GetTextEffects() == attr.GetTextEffects() &&
+            GetTextEffectFlags() == attr.GetTextEffectFlags() &&
 
-// Copy to a wxTextAttr
-void wxRichTextAttr::CopyTo(wxTextAttrEx& attr) const
-{
-    attr.SetTextColour(GetTextColour());
-    attr.SetBackgroundColour(GetBackgroundColour());
-    attr.SetAlignment(GetAlignment());
-    attr.SetTabs(GetTabs());
-    attr.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
-    attr.SetRightIndent(GetRightIndent());
-    attr.SetFont(CreateFont());
-
-    attr.SetParagraphSpacingAfter(m_paragraphSpacingAfter);
-    attr.SetParagraphSpacingBefore(m_paragraphSpacingBefore);
-    attr.SetLineSpacing(m_lineSpacing);
-    attr.SetBulletStyle(m_bulletStyle);
-    attr.SetBulletNumber(m_bulletNumber);
-    attr.SetBulletText(m_bulletText);
-    attr.SetBulletName(m_bulletName);
-    attr.SetBulletFont(m_bulletFont);
-    attr.SetCharacterStyleName(m_characterStyleName);
-    attr.SetParagraphStyleName(m_paragraphStyleName);
-    attr.SetListStyleName(m_listStyleName);
+            GetOutlineLevel() == attr.GetOutlineLevel() &&
 
-    attr.SetURL(m_urlTarget);
+            GetFontSize() == attr.GetFontSize() &&
+            GetFontStyle() == attr.GetFontStyle() &&
+            GetFontWeight() == attr.GetFontWeight() &&
+            GetFontUnderlined() == attr.GetFontUnderlined() &&
+            GetFontFaceName() == attr.GetFontFaceName() &&
 
-    attr.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
+            GetURL() == attr.GetURL();
 }
 
 // Create font from font attributes.
@@ -7006,111 +7466,191 @@ bool wxRichTextAttr::GetFontAttributes(const wxFont& font)
     return true;
 }
 
-wxRichTextAttr wxRichTextAttr::Combine(const wxRichTextAttr& attr,
-                               const wxRichTextAttr& attrDef,
-                               const wxTextCtrlBase *text)
+wxRichTextAttr wxRichTextAttr::Combine(const wxRichTextAttr& style, const wxRichTextAttr* compareWith) const
 {
-    wxColour colFg = attr.GetTextColour();
-    if ( !colFg.Ok() )
-    {
-        colFg = attrDef.GetTextColour();
+    wxRichTextAttr destStyle = (*this);
+    destStyle.Apply(style, compareWith);
 
-        if ( text && !colFg.Ok() )
-            colFg = text->GetForegroundColour();
+    return destStyle;
+}
+
+bool wxRichTextAttr::Apply(const wxRichTextAttr& style, const wxRichTextAttr* compareWith)
+{
+    wxRichTextAttr& destStyle = (*this);
+
+    if (style.HasFontWeight())
+    {
+        if (!(compareWith && compareWith->HasFontWeight() && compareWith->GetFontWeight() == style.GetFontWeight()))
+            destStyle.SetFontWeight(style.GetFontWeight());
     }
 
-    wxColour colBg = attr.GetBackgroundColour();
-    if ( !colBg.Ok() )
+    if (style.HasFontSize())
     {
-        colBg = attrDef.GetBackgroundColour();
+        if (!(compareWith && compareWith->HasFontSize() && compareWith->GetFontSize() == style.GetFontSize()))
+            destStyle.SetFontSize(style.GetFontSize());
+    }
 
-        if ( text && !colBg.Ok() )
-            colBg = text->GetBackgroundColour();
+    if (style.HasFontItalic())
+    {
+        if (!(compareWith && compareWith->HasFontItalic() && compareWith->GetFontStyle() == style.GetFontStyle()))
+            destStyle.SetFontStyle(style.GetFontStyle());
     }
 
-    wxRichTextAttr newAttr(colFg, colBg);
+    if (style.HasFontUnderlined())
+    {
+        if (!(compareWith && compareWith->HasFontUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined()))
+            destStyle.SetFontUnderlined(style.GetFontUnderlined());
+    }
 
-    if (attr.HasWeight())
-        newAttr.SetFontWeight(attr.GetFontWeight());
+    if (style.HasFontFaceName())
+    {
+        if (!(compareWith && compareWith->HasFontFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName()))
+            destStyle.SetFontFaceName(style.GetFontFaceName());
+    }
 
-    if (attr.HasSize())
-        newAttr.SetFontSize(attr.GetFontSize());
+    if (style.GetTextColour().Ok() && style.HasTextColour())
+    {
+        if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
+            destStyle.SetTextColour(style.GetTextColour());
+    }
 
-    if (attr.HasItalic())
-        newAttr.SetFontStyle(attr.GetFontStyle());
+    if (style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
+    {
+        if (!(compareWith && compareWith->HasBackgroundColour() && compareWith->GetBackgroundColour() == style.GetBackgroundColour()))
+            destStyle.SetBackgroundColour(style.GetBackgroundColour());
+    }
 
-    if (attr.HasUnderlined())
-        newAttr.SetFontUnderlined(attr.GetFontUnderlined());
+    if (style.HasAlignment())
+    {
+        if (!(compareWith && compareWith->HasAlignment() && compareWith->GetAlignment() == style.GetAlignment()))
+            destStyle.SetAlignment(style.GetAlignment());
+    }
 
-    if (attr.HasFaceName())
-        newAttr.SetFontFaceName(attr.GetFontFaceName());
+    if (style.HasTabs())
+    {
+        if (!(compareWith && compareWith->HasTabs() && wxRichTextTabsEq(compareWith->GetTabs(), style.GetTabs())))
+            destStyle.SetTabs(style.GetTabs());
+    }
 
-    if (attr.HasAlignment())
-        newAttr.SetAlignment(attr.GetAlignment());
-    else if (attrDef.HasAlignment())
-        newAttr.SetAlignment(attrDef.GetAlignment());
+    if (style.HasLeftIndent())
+    {
+        if (!(compareWith && compareWith->HasLeftIndent() && compareWith->GetLeftIndent() == style.GetLeftIndent()
+                          && compareWith->GetLeftSubIndent() == style.GetLeftSubIndent()))
+            destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
+    }
 
-    if (attr.HasTabs())
-        newAttr.SetTabs(attr.GetTabs());
-    else if (attrDef.HasTabs())
-        newAttr.SetTabs(attrDef.GetTabs());
+    if (style.HasRightIndent())
+    {
+        if (!(compareWith && compareWith->HasRightIndent() && compareWith->GetRightIndent() == style.GetRightIndent()))
+            destStyle.SetRightIndent(style.GetRightIndent());
+    }
 
-    if (attr.HasLeftIndent())
-        newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
-    else if (attrDef.HasLeftIndent())
-        newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
+    if (style.HasParagraphSpacingAfter())
+    {
+        if (!(compareWith && compareWith->HasParagraphSpacingAfter() && compareWith->GetParagraphSpacingAfter() == style.GetParagraphSpacingAfter()))
+            destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
+    }
 
-    if (attr.HasRightIndent())
-        newAttr.SetRightIndent(attr.GetRightIndent());
-    else if (attrDef.HasRightIndent())
-        newAttr.SetRightIndent(attrDef.GetRightIndent());
+    if (style.HasParagraphSpacingBefore())
+    {
+        if (!(compareWith && compareWith->HasParagraphSpacingBefore() && compareWith->GetParagraphSpacingBefore() == style.GetParagraphSpacingBefore()))
+            destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
+    }
 
-    // NEW ATTRIBUTES
+    if (style.HasLineSpacing())
+    {
+        if (!(compareWith && compareWith->HasLineSpacing() && compareWith->GetLineSpacing() == style.GetLineSpacing()))
+            destStyle.SetLineSpacing(style.GetLineSpacing());
+    }
 
-    if (attr.HasParagraphSpacingAfter())
-        newAttr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
+    if (style.HasCharacterStyleName())
+    {
+        if (!(compareWith && compareWith->HasCharacterStyleName() && compareWith->GetCharacterStyleName() == style.GetCharacterStyleName()))
+            destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
+    }
 
-    if (attr.HasParagraphSpacingBefore())
-        newAttr.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
+    if (style.HasParagraphStyleName())
+    {
+        if (!(compareWith && compareWith->HasParagraphStyleName() && compareWith->GetParagraphStyleName() == style.GetParagraphStyleName()))
+            destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
+    }
 
-    if (attr.HasLineSpacing())
-        newAttr.SetLineSpacing(attr.GetLineSpacing());
+    if (style.HasListStyleName())
+    {
+        if (!(compareWith && compareWith->HasListStyleName() && compareWith->GetListStyleName() == style.GetListStyleName()))
+            destStyle.SetListStyleName(style.GetListStyleName());
+    }
 
-    if (attr.HasCharacterStyleName())
-        newAttr.SetCharacterStyleName(attr.GetCharacterStyleName());
+    if (style.HasBulletStyle())
+    {
+        if (!(compareWith && compareWith->HasBulletStyle() && compareWith->GetBulletStyle() == style.GetBulletStyle()))
+            destStyle.SetBulletStyle(style.GetBulletStyle());
+    }
 
-    if (attr.HasParagraphStyleName())
-        newAttr.SetParagraphStyleName(attr.GetParagraphStyleName());
+    if (style.HasBulletText())
+    {
+        if (!(compareWith && compareWith->HasBulletText() && compareWith->GetBulletText() == style.GetBulletText()))
+        {
+            destStyle.SetBulletText(style.GetBulletText());
+            destStyle.SetBulletFont(style.GetBulletFont());
+        }
+    }
 
-    if (attr.HasListStyleName())
-        newAttr.SetListStyleName(attr.GetListStyleName());
+    if (style.HasBulletNumber())
+    {
+        if (!(compareWith && compareWith->HasBulletNumber() && compareWith->GetBulletNumber() == style.GetBulletNumber()))
+            destStyle.SetBulletNumber(style.GetBulletNumber());
+    }
 
-    if (attr.HasBulletStyle())
-        newAttr.SetBulletStyle(attr.GetBulletStyle());
+    if (style.HasBulletName())
+    {
+        if (!(compareWith && compareWith->HasBulletName() && compareWith->GetBulletName() == style.GetBulletName()))
+            destStyle.SetBulletName(style.GetBulletName());
+    }
 
-    if (attr.HasBulletNumber())
-        newAttr.SetBulletNumber(attr.GetBulletNumber());
+    if (style.HasURL())
+    {
+        if (!(compareWith && compareWith->HasURL() && compareWith->GetURL() == style.GetURL()))
+            destStyle.SetURL(style.GetURL());
+    }
 
-    if (attr.HasBulletName())
-        newAttr.SetBulletName(attr.GetBulletName());
+    if (style.HasPageBreak())
+    {
+        if (!(compareWith && compareWith->HasPageBreak()))
+            destStyle.SetPageBreak();
+    }
 
-    if (attr.HasBulletText())
+    if (style.HasTextEffects())
     {
-        newAttr.SetBulletText(attr.GetBulletText());
-        newAttr.SetBulletFont(attr.GetBulletFont());
+        if (!(compareWith && compareWith->HasTextEffects() && compareWith->GetTextEffects() == style.GetTextEffects()))
+        {
+            int destBits = destStyle.GetTextEffects();
+            int destFlags = destStyle.GetTextEffectFlags();
+
+            int srcBits = style.GetTextEffects();
+            int srcFlags = style.GetTextEffectFlags();
+
+            wxRichTextCombineBitlists(destBits, srcBits, destFlags, srcFlags);
+
+            destStyle.SetTextEffects(destBits);
+            destStyle.SetTextEffectFlags(destFlags);
+        }
     }
 
-    if (attr.HasURL())
-        newAttr.SetURL(attr.GetURL());
+    if (style.HasOutlineLevel())
+    {
+        if (!(compareWith && compareWith->HasOutlineLevel() && compareWith->GetOutlineLevel() == style.GetOutlineLevel()))
+            destStyle.SetOutlineLevel(style.GetOutlineLevel());
+    }
 
-    return newAttr;
+    return true;
 }
 
 /*!
  * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
  */
 
-wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx& attr)
+wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx& attr): wxTextAttr()
 {
     Copy(attr);
 }
@@ -7122,7 +7662,10 @@ void wxTextAttrEx::Init()
     m_paragraphSpacingBefore = 0;
     m_lineSpacing = 0;
     m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
+    m_textEffects = wxTEXT_ATTR_EFFECT_NONE;
+    m_textEffectFlags = wxTEXT_ATTR_EFFECT_NONE;
     m_bulletNumber = 0;
+    m_outlineLevel = 0;
 }
 
 // Copy
@@ -7142,6 +7685,9 @@ void wxTextAttrEx::Copy(const wxTextAttrEx& attr)
     m_bulletFont = attr.m_bulletFont;
     m_bulletName = attr.m_bulletName;
     m_urlTarget = attr.m_urlTarget;
+    m_textEffects = attr.m_textEffects;
+    m_textEffectFlags = attr.m_textEffectFlags;
+    m_outlineLevel = attr.m_outlineLevel;
 }
 
 // Assignment from a wxTextAttrEx object
@@ -7160,9 +7706,12 @@ void wxTextAttrEx::operator= (const wxTextAttr& attr)
 bool wxTextAttrEx::operator== (const wxTextAttrEx& attr) const
 {
     return (
+        GetFlags() == attr.GetFlags() &&
         GetTextColour() == attr.GetTextColour() &&
         GetBackgroundColour() == attr.GetBackgroundColour() &&
         GetFont() == attr.GetFont() &&
+        GetTextEffects() == attr.GetTextEffects() &&
+        GetTextEffectFlags() == attr.GetTextEffectFlags() &&
         GetAlignment() == attr.GetAlignment() &&
         GetLeftIndent() == attr.GetLeftIndent() &&
         GetRightIndent() == attr.GetRightIndent() &&
@@ -7179,6 +7728,7 @@ bool wxTextAttrEx::operator== (const wxTextAttrEx& attr) const
         GetCharacterStyleName() == attr.GetCharacterStyleName() &&
         GetParagraphStyleName() == attr.GetParagraphStyleName() &&
         GetListStyleName() == attr.GetListStyleName() &&
+        GetOutlineLevel() == attr.GetOutlineLevel() &&
         GetURL() == attr.GetURL());
 }
 
@@ -7216,27 +7766,27 @@ wxTextAttrEx wxTextAttrEx::CombineEx(const wxTextAttrEx& attr,
         // Otherwise, if there are font attributes in attr, apply them
         if (attr.GetFlags() & wxTEXT_ATTR_FONT)
         {
-            if (attr.HasSize())
+            if (attr.HasFontSize())
             {
                 flags |= wxTEXT_ATTR_FONT_SIZE;
                 font.SetPointSize(attr.GetFont().GetPointSize());
             }
-            if (attr.HasItalic())
+            if (attr.HasFontItalic())
             {
                 flags |= wxTEXT_ATTR_FONT_ITALIC;;
                 font.SetStyle(attr.GetFont().GetStyle());
             }
-            if (attr.HasWeight())
+            if (attr.HasFontWeight())
             {
                 flags |= wxTEXT_ATTR_FONT_WEIGHT;
                 font.SetWeight(attr.GetFont().GetWeight());
             }
-            if (attr.HasFaceName())
+            if (attr.HasFontFaceName())
             {
                 flags |= wxTEXT_ATTR_FONT_FACE;
                 font.SetFaceName(attr.GetFont().GetFaceName());
             }
-            if (attr.HasUnderlined())
+            if (attr.HasFontUnderlined())
             {
                 flags |= wxTEXT_ATTR_FONT_UNDERLINE;
                 font.SetUnderlined(attr.GetFont().GetUnderlined());
@@ -7330,6 +7880,15 @@ wxTextAttrEx wxTextAttrEx::CombineEx(const wxTextAttrEx& attr,
     if (attr.HasURL())
         newAttr.SetURL(attr.GetURL());
 
+    if (attr.HasTextEffects())
+    {
+        newAttr.SetTextEffects(attr.GetTextEffects());
+        newAttr.SetTextEffectFlags(attr.GetTextEffectFlags());
+    }
+
+    if (attr.HasOutlineLevel())
+        newAttr.SetOutlineLevel(attr.GetOutlineLevel());
+
     return newAttr;
 }
 
@@ -7341,7 +7900,7 @@ wxTextAttrEx wxTextAttrEx::CombineEx(const wxTextAttrEx& attr,
 
 IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject)
 
-#if wxUSE_STREAMS
+#if wxUSE_FFILE && wxUSE_STREAMS
 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename)
 {
     wxFFileInputStream stream(filename);
@@ -7359,7 +7918,7 @@ bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& f
 
     return false;
 }
-#endif // wxUSE_STREAMS
+#endif // wxUSE_FFILE && wxUSE_STREAMS
 
 /// Can we handle this filename (if using files)? By default, checks the extension.
 bool wxRichTextFileHandler::CanHandle(const wxString& filename) const
@@ -7402,12 +7961,12 @@ bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStr
         }
     }
 
+    buffer->ResetAndClearCommands();
     buffer->Clear();
     buffer->AddParagraphs(str);
     buffer->UpdateRanges();
 
     return true;
-
 }
 
 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
@@ -7416,6 +7975,10 @@ bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputSt
         return false;
 
     wxString text = buffer->GetText();
+
+    wxString newLine = wxRichTextLineBreakChar;
+    text.Replace(newLine, wxT("\n"));
+
     wxCharBuffer buf = text.ToAscii();
 
     stream.Write((const char*) buf, text.length());
@@ -7610,16 +8173,33 @@ bool wxRichTextImageBlock::Load(wxImage& image)
 // Write data in hex to a stream
 bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream)
 {
-    wxString hex;
-    int i;
-    for (i = 0; i < (int) m_dataSize; i++)
+    const int bufSize = 512;
+    char buf[bufSize+1];
+
+    int left = m_dataSize;
+    int n, i, j;
+    j = 0;
+    while (left > 0)
     {
-        hex = wxDecToHex(m_data[i]);
-        wxCharBuffer buf = hex.ToAscii();
+        if (left*2 > bufSize)
+        {
+            n = bufSize; left -= (bufSize/2);
+        }
+        else
+        {
+            n = left*2; left = 0;
+        }
 
-        stream.Write((const char*) buf, hex.length());
-    }
+        char* b = buf;
+        for (i = 0; i < (n/2); i++)
+        {
+            wxDecToHex(m_data[j], b, b+1);
+            b += 2; j ++;
+        }
 
+        buf[n] = 0;
+        stream.Write((const char*) buf, n);
+    }
     return true;
 }
 
@@ -7631,13 +8211,13 @@ bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, int imageT
     if (m_data)
         delete[] m_data;
 
-    wxString str(wxT("  "));
+    wxChar str[2];
     m_data = new unsigned char[dataSize];
     int i;
     for (i = 0; i < dataSize; i ++)
     {
-        str[0] = stream.GetC();
-        str[1] = stream.GetC();
+        str[0] = (char)stream.GetC();
+        str[1] = (char)stream.GetC();
 
         m_data[i] = (unsigned char)wxHexToDec(str);
     }