From fe5f475bfd9236a651dfff8db6fe7422b1a1b49c Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 9 Jul 2026 23:56:21 -0600 Subject: [PATCH] Preserve wxRichTextCtrl append style stack AppendText() moves the insertion point to the end before writing, which refreshes the default style from the caret position. If a Begin*() style is active, this could replace the explicit append style with the style at the end of the existing text. Save and restore the active default style when the rich text style stack is non-empty, and cover appending text under a different text colour. Fixes #10936. Closes #27037. --- src/richtext/richtextctrl.cpp | 6 ++++++ tests/controls/richtextctrltest.cpp | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp index d8e4af28b3..117f93714d 100644 --- a/src/richtext/richtextctrl.cpp +++ b/src/richtext/richtextctrl.cpp @@ -3381,8 +3381,14 @@ void wxRichTextCtrl::DoWriteText(const wxString& value, int flags) void wxRichTextCtrl::AppendText(const wxString& text) { + const bool hasStyleStack = GetBuffer().GetStyleStackSize() > 0; + const wxRichTextAttr defaultStyle = GetDefaultStyleEx(); + SetInsertionPointEnd(); + if ( hasStyleStack ) + SetDefaultStyle(defaultStyle); + WriteText(text); } diff --git a/tests/controls/richtextctrltest.cpp b/tests/controls/richtextctrltest.cpp index 260b2976ad..86017aa3c2 100644 --- a/tests/controls/richtextctrltest.cpp +++ b/tests/controls/richtextctrltest.cpp @@ -699,6 +699,27 @@ TEST_CASE_METHOD(RichTextCtrlTestCase, "RichTextCtrl::TextColour", CHECK(colour.GetTextColour() == m_rich->GetBasicStyle().GetTextColour()); } +TEST_CASE_METHOD(RichTextCtrlTestCase, "RichTextCtrl::AppendTextStyle", + "[richtextctrl]") +{ + m_rich->BeginTextColour(*wxRED); + m_rich->WriteText("red"); + m_rich->EndTextColour(); + + m_rich->BeginTextColour(*wxBLUE); + m_rich->AppendText("blue"); + m_rich->EndTextColour(); + + wxTextAttr colour; + m_rich->GetStyle(1, colour); + + CHECK(colour.GetTextColour() == *wxRED); + + m_rich->GetStyle(5, colour); + + CHECK(colour.GetTextColour() == *wxBLUE); +} + TEST_CASE_METHOD(RichTextCtrlTestCase, "RichTextCtrl::NumberedBullet", "[richtextctrl]") {