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.
This commit is contained in:
Richard
2026-09-20 18:36:52 +02:00
committed by Vadim Zeitlin
parent 1515b8bac6
commit fe5f475bfd
2 changed files with 27 additions and 0 deletions
+6
View File
@@ -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);
}
+21
View File
@@ -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]")
{