From 523d2f145ef7d8e653fbe1f16797c1f0b4d91c16 Mon Sep 17 00:00:00 2001
From: Julian Smart <julian@anthemion.co.uk>
Date: Fri, 17 Nov 2006 08:33:34 +0000
Subject: [PATCH] Added wxRICHTEXT_SETSTYLE_RESET SetStyleEx() flag to allow
 for clearing attributes before setting new ones SetStyle() now looks up and
 applies named paragraph or character style Fixed a bug in text entry (taking
 previous paragraph's style)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43459 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/richtextbuffer.tex     |  3 +-
 docs/latex/wx/richtextctrl.tex       |  3 +-
 include/wx/richtext/richtextbuffer.h |  3 ++
 src/richtext/richtextbuffer.cpp      | 58 +++++++++++++++++++++-------
 src/richtext/richtextctrl.cpp        |  2 +-
 5 files changed, 51 insertions(+), 18 deletions(-)

diff --git a/docs/latex/wx/richtextbuffer.tex b/docs/latex/wx/richtextbuffer.tex
index b0a57a1313..4140882573 100644
--- a/docs/latex/wx/richtextbuffer.tex
+++ b/docs/latex/wx/richtextbuffer.tex
@@ -807,10 +807,11 @@ This differs from the wxRichTextCtrl API, where you would specify (5,6).
 \item wxRICHTEXT\_SETSTYLE\_WITH\_UNDO: specifies that this operation should be undoable.
 \item wxRICHTEXT\_SETSTYLE\_OPTIMIZE: specifies that the style should not be applied if the
 combined style at this point is already the style in question.
-\item define wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY: specifies that the style should only be applied to paragraphs,
+\item wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY: specifies that the style should only be applied to paragraphs,
 and not the content. This allows content styling to be preserved independently from that of e.g. a named paragraph style.
 \item wxRICHTEXT\_SETSTYLE\_CHARACTERS\_ONLY: specifies that the style should only be applied to characters,
 and not the paragraph. This allows content styling to be preserved independently from that of e.g. a named paragraph style.
+\item wxRICHTEXT\_SETSTYLE\_RESET: resets (clears) the existing style before applying the new style.
 \end{itemize}
 
 \membersection{wxRichTextBuffer::SetStyleSheet}\label{wxrichtextbuffersetstylesheet}
diff --git a/docs/latex/wx/richtextctrl.tex b/docs/latex/wx/richtextctrl.tex
index 87b3b88e0a..d268a9be20 100644
--- a/docs/latex/wx/richtextctrl.tex
+++ b/docs/latex/wx/richtextctrl.tex
@@ -1373,10 +1373,11 @@ So, for example, to set the style for a character at position 5, use the range (
 \item wxRICHTEXT\_SETSTYLE\_WITH\_UNDO: specifies that this operation should be undoable.
 \item wxRICHTEXT\_SETSTYLE\_OPTIMIZE: specifies that the style should not be applied if the
 combined style at this point is already the style in question.
-\item define wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY: specifies that the style should only be applied to paragraphs,
+\item wxRICHTEXT\_SETSTYLE\_PARAGRAPHS\_ONLY: specifies that the style should only be applied to paragraphs,
 and not the content. This allows content styling to be preserved independently from that of e.g. a named paragraph style.
 \item wxRICHTEXT\_SETSTYLE\_CHARACTERS\_ONLY: specifies that the style should only be applied to characters,
 and not the paragraph. This allows content styling to be preserved independently from that of e.g. a named paragraph style.
+\item wxRICHTEXT\_SETSTYLE\_RESET: resets (clears) the existing style before applying the new style.
 \end{itemize}
 
 \membersection{wxRichTextCtrl::SetStyleSheet}\label{wxrichtextctrlsetstylesheet}
diff --git a/include/wx/richtext/richtextbuffer.h b/include/wx/richtext/richtextbuffer.h
index c57f858c33..8478c3f392 100644
--- a/include/wx/richtext/richtextbuffer.h
+++ b/include/wx/richtext/richtextbuffer.h
@@ -176,6 +176,9 @@ class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer;
 // the current indentation will be used
 #define wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL   0x20
 
+// Resets the existing style before applying the new style
+#define wxRICHTEXT_SETSTYLE_RESET           0x40
+
 /*!
  * Flags for text insertion
  */
diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp
index 0b1841d64c..1f3184d1a8 100644
--- a/src/richtext/richtextbuffer.cpp
+++ b/src/richtext/richtextbuffer.cpp
@@ -1599,11 +1599,29 @@ 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);
+
+    // Apply paragraph style first, if any
+    wxRichTextAttr wholeStyle(style);
+
+    if (wholeStyle.HasParagraphStyleName() && GetStyleSheet())
+    {
+        wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(wholeStyle.GetParagraphStyleName());
+        if (def)
+            wxRichTextApplyStyle(wholeStyle, def->GetStyle());
+    }
 
     // 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 (characterAttributes.HasCharacterStyleName() && GetStyleSheet())
+    {
+        wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterAttributes.GetCharacterStyleName());
+        if (def)
+            wxRichTextApplyStyle(characterAttributes, def->GetStyle());
+    }
+
     // If we are associated with a control, make undoable; otherwise, apply immediately
     // to the data.
 
@@ -1651,15 +1669,20 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const
                 // to be included in the paragraph style
                 if ((paragraphStyle || parasOnly) && !charactersOnly)
                 {
-                    if (applyMinimal)
+                    if (resetExistingStyle)
+                        newPara->GetAttributes() = wholeStyle;
+                    else
                     {
-                        // 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);
+                        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);
                     }
-                    else
-                        wxRichTextApplyStyle(newPara->GetAttributes(), style);
                 }
 
 #if wxRICHTEXT_USE_DYNAMIC_STYLES
@@ -1725,15 +1748,20 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const
                     {
                         wxRichTextObject* child = node2->GetData();
 
-                        if (applyMinimal)
+                        if (resetExistingStyle)
+                            child->GetAttributes() = characterAttributes;
+                        else
                         {
-                            // 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);
+                            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);
                         }
-                        else
-                            wxRichTextApplyStyle(child->GetAttributes(), characterAttributes);
 
                         if (node2 == lastNode)
                             break;
diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp
index 11b54b3fda..52bfe09cd0 100644
--- a/src/richtext/richtextctrl.cpp
+++ b/src/richtext/richtextctrl.cpp
@@ -769,7 +769,7 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
                 DeleteSelectedContent(& newPos);
 
                 wxString str = (wxChar) event.GetKeyCode();
-                GetBuffer().InsertTextWithUndo(newPos+1, str, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
+                GetBuffer().InsertTextWithUndo(newPos+1, str, this, 0);
 
                 EndBatchUndo();
 
-- 
2.50.0

