Explore I will be implementing markdown support in a wxRichTextCtrl in a C++ wxWidgets project,… Save Share Download ▾ ReportJump to turn (8) 1 I will be implementing markdown support in a wxRichTextCtrl in a C++ wxWidgets p 2 Perfect! You got everything right, so let's go and add the List support. Let's s 3 I removed the inBulletList boolean as I think it is not necessary, and I wrote t 4 It seems to be working correctly but the style and the bullet list isn't applied 5 The style didn't work, and didn't do anything. I checked my code and it seems th 6 OK I see you removed the style in the AppendMessage which is great, but you forg 7 It didn't work so just focus on my code now, I tried using a different approach 8 OK I changed the code but didn't use the unicode bullet because it doesn't show, I will be implementing markdown support in a wxRichTextCtrl in a C++ wxWidgets project, I will begin with a basic feature set and gradually expand the functionality. Here is a suggested order to implement features incrementally:
Basic Markdown Features: [DONE]
Add support for basic markdown formatting such as bold, italic, and underline. Implement parsing logic to recognize and apply these formatting styles to the messages.
Headers and Lists: [Headers only DONE]
Introduce support for headers and lists in markdown messages. Allow users to create structured content using headers and bullet/numbered lists.
Code Blocks and Blockquotes: [DONE blockquotes only]
Add support for displaying code blocks and blockquotes in markdown messages. Implement formatting logic to differentiate code snippets and quoted text.
Advanced Features (optional):
Consider implementing more advanced markdown features such as tables, task lists, etc.
Copy first prompt Copy first response
Details
Provider openai
Model provenance As recorded by source dataset
Turns 8 (16 messages)
Language en
Added to archive Nov 24, 2026
ID 01m867qhc6zak4jkat8bappxeaSource & license Imported from WildChat-4.8M (source ). Imported records earn no Research Points.
Upstream record: 4156055226384430f3465ec29983f9b4
allenai/WildChat-4.8M, revision c827c6df8fcf008219ffaffa4d1dd77491099367
License: ODC-BY-1.0
Cite this conversation “I will be implementing markdown support in a wxRichTextCtrl in a C++ wxWidgets project,….” Conversation with gpt-4o-2024-05-13, from the WildChat-4.8M dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m867qhc6zak4jkat8bappxea
Pending Tasks:
Lists: Bullet and numbered lists need to be added.
Code Blocks: Complete the remaining code block implementation.
Advanced Features (Optional): Tables, task lists, etc.
Let me know if you understood this so we can move to my current code and how we can start dealing with the markdown support. Don’t erite code yet until I ask you.
Assistant2024-06-13 13:59 UTC Copy response
Understood! It seems like you have a clear plan for incrementally adding markdown support to your wxRichTextCtrl. Here's a summary of where your implementation stands:
Basic Markdown Features: Complete
Bold, Italic, Underline are supported.
Headers and Lists: Partially Complete
Headers are supported.
Bullet and numbered lists need to be added.
Code Blocks and Blockquotes: Partially Complete
Blockquotes are supported.
Code blocks need to be added.
Advanced Features (Optional): Pending
Features like tables and task lists are to be considered later.
Next Steps:
Implement bullet and numbered lists.
Complete the implementation of code blocks.
Feel free to share your current code or specific sections of it where you'd like to add support for the pending tasks. Once the code is provided, I can give you detailed guidance on how to proceed with adding the remaining markdown features.
Perfect! You got everything right, so let's go and add the List support. Let's start with the bullet list and then we can add the numberes lists.
To do so, we should use this method:
bool wxRichTextCtrl::BeginStandardBullet ( const wxString & bulletName,
int leftIndent,
int leftSubIndent,
int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
Begins applying a symbol bullet. BulletName value could be "standard/circle".
Code sample:
r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
r.BeginStandardBullet(wxT("standard/circle"), 100, 60);
r.Newline();
r.WriteText(wxT("Compatibility with wxTextCtrl API"));
r.EndStandardBullet();
And this is my code were we have to implement this:
void Chat::WriteText(const wxString& message, const wxTextAttr& style)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
Show full message · 264 lines Assistant2024-06-13 14:43 UTC Copy response
Great! Now that we have the context and your current implementation, let's go ahead and implement support for bullet lists. We will make use of the wxRichTextCtrl::BeginStandardBullet method to start the bullet list, and call wxRichTextCtrl::EndStandardBullet to end it.
To handle bullet lists, we'll need to detect lines that start with a bullet list marker (such as * or - ) and apply the bullet list styles accordingly.
Let's enhance your WriteText function to handle bullet lists:
Detect lines starting with * or - to recognize them as bullet list items.
Use BeginStandardBullet to begin a bullet list item and EndStandardBullet to end it.
Here's an updated version of your WriteText function with bullet list support implemented:
Show full message · 305 lines I removed the inBulletList boolean as I think it is not necessary, and I wrote the bullet as it comes, but it doesn't seem to work, and I don't know why. Even the indentation is not working, as it keeps the same style as it had before. I even used two different methods for bullet lists and still doesn't work, what could go wrong?
else if (markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos)) // Bullet list
{
richTextChatBox->WriteText("\n");
//richTextChatBox->BeginSymbolBullet('*', 100, 60); // Another method
richTextChatBox->BeginStandardBullet(wxT("standard/circle"), 100, 60);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStandardBullet();
//richTextChatBox->EndSymbolBullet(); // Another method
code Copy code
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
Assistant2024-06-13 15:28 UTC Copy response
Based on what you’ve described and the provided code, it seems that the bullet list marker is not being interpreted correctly. Additionally, the text may not be broken down as expected, and the indentation levels might not be appropriately managed.
Let's refine the logic to ensure that bullet points are properly recognized and styled within the wxRichTextCtrl. Here is a revised approach, including necessary changes to ensure bullets are processed correctly:
Properly Detect Bullet Points:
Ensure we correctly detect bullet points, whether at the start of the message or in the middle.
Consistent Styling within List Items:
Ensure the text that follows the bullet points is correctly styled and incorporates proper indentation.
Ensure End Bullet List:
End bullet lists properly since each bullet is a separate bullet list in wxRichTextCtrl.
Here’s the refined version of your code with additional debugging checks to help identify any issues:
Show full message · 313 lines It seems to be working correctly but the style and the bullet list isn't applied:
Terminal output
"13:04:00: Debug: Model parameters initialized.
13:04:03: Debug: Start of bullet detected at position: 37
13:04:03: Debug: Bullet text: Item 1
13:04:03: Debug: Start of bullet detected at position: 54
13:04:03: Debug: Bullet text: Item 3
13:04:03: Debug: Start of bullet detected at position: 72
13:04:03: Debug: Bullet text: Item 5
"
rich text ctrl show:
""
Assistant2024-06-13 16:06 UTC Copy response
Given that the bullets are detected correctly but the style and bullet lists are not being applied, it's possible that the BeginStandardBullet and EndStandardBullet methods are not functioning as expected within the context of your code.
Alternative Approach:
Instead of using BeginStandardBullet and EndStandardBullet, let's explicitly set the bullet style via wxTextAttr and apply it to the text. This will ensure we have full control over the bullet styling.
Below is the revised version of your WriteText function with this approach:
Show full message · 311 lines The style didn't work, and didn't do anything. I checked my code and it seems that I already have a begin style method previously open, so maybe that iswhy it is not working:
void Chat::AppendMessage(const wxString& message, const wxString& role)
{
wxMutexGuiEnter();
code Copy code
wxTextAttr style;
if (role == "user")
{
style.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
style.SetBackgroundColour(wxColour("#4A67CF"));
style.SetTextColour(wxColour("#FFFFFF"));
style.SetLeftIndent(20);
style.SetRightIndent(5);
style.SetParagraphSpacingBefore(20);
style.SetParagraphSpacingAfter(15);
}
else
{
style.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
style.SetBackgroundColour(wxColour("#2B651A"));
style.SetTextColour(wxColour("#FFFFFF"));
style.SetLeftIndent(5);
style.SetRightIndent(20);
}
richTextChatBox->Freeze();
richTextChatBox->SetInsertionPointEnd();
richTextChatBox->BeginStyle(style);
WriteText(message, style);
richTextChatBox->EndStyle();
richTextChatBox->SetInsertionPointEnd();
richTextChatBox->ShowPosition(richTextChatBox->GetLastPosition());
richTextChatBox->Thaw();
wxMutexGuiLeave();
}
Maybe we can rewrite this to make the style inside and also modifying it if needed.
Show full message · 44 lines Assistant2024-06-13 16:21 UTC Copy response
Given that the AppendMessage function already sets a style, it might indeed be interfering with our custom bullet style applied in WriteText. To properly handle this, let's update the AppendMessage function to allow flexible application of styles within WriteText.
Refactoring Plan:
Adjust Existing Style Logic:
Modify the AppendMessage function to properly enclose the WriteText calls and ensure any internal styles, such as for bullets, can be set without conflict.
Integrate Style Application in WriteText:
Within WriteText, handle style transitions smoothly while retaining the wrapping environment set by AppendMessage.
Updated Code:
First, let's modify AppendMessage:
cpp Copy code
void Chat::AppendMessage(const wxString& message, const wxString& role)
{
wxMutexGuiEnter();
wxTextAttr commonStyle;
if (role == "user")
{
commonStyle.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
commonStyle.SetBackgroundColour(wxColour("#4A67CF"));
commonStyle.SetTextColour(wxColour("#FFFFFF"));
commonStyle.SetLeftIndent(20);
commonStyle.SetRightIndent(5);
commonStyle.SetParagraphSpacingBefore(20);
commonStyle.SetParagraphSpacingAfter(15);
}
else
{
commonStyle.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
commonStyle.SetBackgroundColour(wxColour("#2B651A"));
commonStyle.SetTextColour(wxColour("#FFFFFF"));
commonStyle.SetLeftIndent(5);
commonStyle.SetRightIndent(20);
}
richTextChatBox->Freeze();
richTextChatBox->SetInsertionPointEnd();
WriteText(message, commonStyle);
richTextChatBox->SetInsertionPointEnd();
richTextChatBox->ShowPosition(richTextChatBox->GetLastPosition());
richTextChatBox->Thaw();
wxMutexGuiLeave();
}
Show full message · 355 lines OK I see you removed the style in the AppendMessage which is great, but you forgot to apply this style, which should always be applied, you took it off and never applied again, the style should always be applied and sometimes it changes like in the bullet part, but in other cases it should respect the original common style, which have a different text color, etc.
Assistant2024-06-13 16:34 UTC Copy response
You're right! We need to keep the commonStyle applied generally while allowing specific styles to override it when necessary. Let's ensure commonStyle is applied as the base style, and then override it for specific markers like bullets and headings.
Here’s the refined version of both AppendMessage and WriteText, making sure commonStyle is consistently applied as the base, while allowing for localized style adjustments:
Updated AppendMessage:
We’ll keep the base style application within AppendMessage:
cpp Copy code
void Chat::AppendMessage(const wxString& message, const wxString& role)
{
wxMutexGuiEnter();
wxTextAttr commonStyle;
if (role == "user")
{
commonStyle.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
commonStyle.SetBackgroundColour(wxColour("#4A67CF"));
commonStyle.SetTextColour(wxColour("#FFFFFF"));
commonStyle.SetLeftIndent(20);
commonStyle.SetRightIndent(5);
commonStyle.SetParagraphSpacingBefore(20);
commonStyle.SetParagraphSpacingAfter(15);
}
else
{
commonStyle.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
commonStyle.SetBackgroundColour(wxColour("#2B651A"));
commonStyle.SetTextColour(wxColour("#FFFFFF"));
commonStyle.SetLeftIndent(5);
commonStyle.SetRightIndent(20);
}
richTextChatBox->Freeze();
richTextChatBox->SetInsertionPointEnd();
WriteText(message, commonStyle);
richTextChatBox->SetInsertionPointEnd();
richTextChatBox->ShowPosition(richTextChatBox->GetLastPosition());
richTextChatBox->Thaw();
wxMutexGuiLeave();
}
Updated WriteText:
We’ll ensure WriteText starts with the and overrides it where necessary:
Show full message · 339 lines It didn't work so just focus on my code now, I tried using a different approach and found out the \n is being taken from the string, so I think the parsing is wrong in the part of \n(something), for example:
This is my code:
void Chat::WriteText(const wxString& message, const wxTextAttr& style)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
Show full message · 292 lines Assistant2024-06-13 17:27 UTC Copy response
To address the issue with how newlines and markers are being parsed, we need to refactor the code to properly handle sequences of newline characters and markers. The current issue appears to be in ensuring the parser correctly identifies transition points between different types of content and maintains the correct formatted output.
Key Changes:
Handle Leading Newlines:
Correct handling of sequences where a newline character precedes a markdown marker.
Ensure Proper Marker and Text Extraction:
Ensure the extraction process captures the marker and text properly, separating the end of one segment from the start of another.
Updated Code:
Here’s your refactored WriteText method with improved parsing of newline markers and elements:
Show full message · 237 lines OK I changed the code but didn't use the unicode bullet because it doesn't show, so I changed it to an "@" symbol, but still the output was erroneous?
As you can see some of them are different, like the first item has this prefix "@ " the third and fift items have this "@ " and the second and fourth has no prefix.
rich text ctrl shows:
"This is a non-numbered bullet list:
@ Item 1Item 2
@ Item 3Item 4
@ Item 5
"
My code:
//... same as yours
else if (markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos)) // Bullet list
{
richTextChatBox->WriteText("\n");
code Copy code
richTextChatBox->BeginBold();
richTextChatBox->WriteText("@ ");
richTextChatBox->EndBold();
richTextChatBox->WriteText(styledText);
if (formattedMessage.at(endMarker) == '\n')
{
endMarker++;
}
}
//... same as yours
Assistant2024-06-13 18:55 UTC Copy response
Thank you for the detailed observation. The problem seems to stem from how the text is being appended in specific cases. Let's enhance the parsing logic and ensure bullet points are always followed by a proper newline and add appropriate spaces.
Plan:
Ensure Consistent Newlines and Spaces:
This guarantees proper separation between bullet points and text content.
Correctly Extract Bullet-Point Text:
Extract text corresponding to a bullet point marker correctly to ensure uniform output.
Here's the revised code achieving this:
Show full message · 235 lines Dataset releases Not yet included in a published release.
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
if (pos == 0)
{
startHeading1 = formattedMessage.find("# ", pos);
startHeading2 = formattedMessage.find("## ", pos);
startHeading3 = formattedMessage.find("### ", pos);
startHeading4 = formattedMessage.find("#### ", pos);
startHeading5 = formattedMessage.find("##### ", pos);
startHeading6 = formattedMessage.find("###### ", pos);
startBlockquote = formattedMessage.find("> ", pos);
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startLinkText, -1 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos)
{
endMarker = formattedMessage.find("**", markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find("__", markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find("*", markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find("`", markerPos + markerLength);
}
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos)
{
richTextChatBox->BeginBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->BeginUnderline();
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (startBold == markerPos)
{
richTextChatBox->EndBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->EndUnderline();
}
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos)
{
pointSize *= 2;
}
else if (startHeading2 == markerPos)
{
pointSize *= 1.7;
}
else if (startHeading3 == markerPos)
{
pointSize *= 1.5;
}
else if (startHeading4 == markerPos)
{
pointSize *= 1.3;
}
else if (startHeading5 == markerPos)
{
pointSize *= 1.2;
}
else if (startHeading6 == markerPos)
{
pointSize *= 1.1;
}
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
void Chat::WriteText(const wxString& message, const wxTextAttr& style)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
bool inBulletList = false; // Track if we are inside a bullet list
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
// Bullet points
wxString::size_type startBullet1 = formattedMessage.find("\n* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("\n- ", pos);
if (pos == 0)
{
startHeading1 = formattedMessage.find("# ", pos);
startHeading2 = formattedMessage.find("## ", pos);
startHeading3 = formattedMessage.find("### ", pos);
startHeading4 = formattedMessage.find("#### ", pos);
startHeading5 = formattedMessage.find("##### ", pos);
startHeading6 = formattedMessage.find("###### ", pos);
startBlockquote = formattedMessage.find("> ", pos);
startBullet1 = formattedMessage.find("* ", pos);
startBullet2 = formattedMessage.find("- ", pos);
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos)
{
endMarker = formattedMessage.find("**", markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find("__", markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find("*", markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find("`", markerPos + markerLength);
}
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
}
else if (startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos) {
endMarker = formattedMessage.Length();
}
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos)
{
richTextChatBox->BeginBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->BeginUnderline();
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (startBold == markerPos)
{
richTextChatBox->EndBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->EndUnderline();
}
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if ((markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos))) // Bullet list
{
if (!inBulletList)
{
richTextChatBox->BeginStandardBullet(wxT("standard/circle"), 100, 60);
inBulletList = true;
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos)
{
pointSize *= 2;
}
else if (startHeading2 == markerPos)
{
pointSize *= 1.7;
}
else if (startHeading3 == markerPos)
{
pointSize *= 1.5;
}
else if (startHeading4 == markerPos)
{
pointSize *= 1.3;
}
else if (startHeading5 == markerPos)
{
pointSize *= 1.2;
}
else if (startHeading6 == markerPos)
{
pointSize *= 1.1;
}
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
// Make sure we end the bullet list if we started it
if (inBulletList)
{
richTextChatBox->EndStandardBullet();
}
}
Explanation:
Added detection for bullet list markers \n* and \n- .
Added a boolean flag inBulletList to track if we are currently inside a bullet list.
Used BeginStandardBullet to start a bullet list when the marker is found, and EndStandardBullet to end the bullet list at the appropriate position.
Ensured the bullet list is properly ended after the loop if it was started.
With this implementation, your WriteText function should now correctly handle bullet lists in the given messages. Once you've tested this and it works as expected, we can move on to implementing numbered lists.
void Chat::WriteText(const wxString& message, const wxTextAttr& style)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
// Bullet points
wxString::size_type startBullet1 = formattedMessage.find("\n* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("\n- ", pos);
if (pos == 0)
{
startHeading1 = formattedMessage.find("# ", pos);
startHeading2 = formattedMessage.find("## ", pos);
startHeading3 = formattedMessage.find("### ", pos);
startHeading4 = formattedMessage.find("#### ", pos);
startHeading5 = formattedMessage.find("##### ", pos);
startHeading6 = formattedMessage.find("###### ", pos);
startBlockquote = formattedMessage.find("> ", pos);
startBullet1 = formattedMessage.find("* ", pos);
startBullet2 = formattedMessage.find("- ", pos);
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos)
{
endMarker = formattedMessage.find("**", markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find("__", markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find("*", markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find("`", markerPos + markerLength);
}
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
}
else if (startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos) {
endMarker = formattedMessage.Length();
}
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos)
{
richTextChatBox->BeginBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->BeginUnderline();
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (startBold == markerPos)
{
richTextChatBox->EndBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->EndUnderline();
}
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if ((markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos))) // Bullet list
{
// Debugging Statements for Bullet List
wxLogDebug("Start of bullet detected at position: %zu", markerPos);
if (pos == 0 || formattedMessage[markerPos - 1] == '\n')
{
richTextChatBox->BeginStandardBullet(wxT("standard/circle"), 100, 60);
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
// Debugging Statements for Bullet Content
wxLogDebug("Bullet text: %s", styledText);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStandardBullet();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos)
{
pointSize *= 2;
}
else if (startHeading2 == markerPos)
{
pointSize *= 1.7;
}
else if (startHeading3 == markerPos)
{
pointSize *= 1.5;
}
else if (startHeading4 == markerPos)
{
pointSize *= 1.3;
}
else if (startHeading5 == markerPos)
{
pointSize *= 1.2;
}
else if (startHeading6 == markerPos)
{
pointSize *= 1.1;
}
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
}
Key Changes & Additions:
Debugging Logs:
Added wxLogDebug statements to aid in debugging and track the positions and text of bullet markers.
Correct Start of Bullets:
Added a check to ensure bullet lists are only started at the beginning of a line or at position zero.
End Bullet List Consistently:
End the bullet list appropriately after writing the styled text.
Additional Advice: After making these changes, run the program and check the debug output to ensure the bullet detection and content styling are functioning as expected. The debug logs will help identify any issues or misinterpretations in the position markers and styled text. If the bullets still do not render properly, verify that the wxRichTextCtrl supports the bullet styles you are trying to use in your wxWidgets version.
void Chat::WriteText(const wxString& message, const wxTextAttr& style)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
// Bullet points
wxString::size_type startBullet1 = formattedMessage.find("\n* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("\n- ", pos);
if (pos == 0)
{
startHeading1 = formattedMessage.find("# ", pos);
startHeading2 = formattedMessage.find("## ", pos);
startHeading3 = formattedMessage.find("### ", pos);
startHeading4 = formattedMessage.find("#### ", pos);
startHeading5 = formattedMessage.find("##### ", pos);
startHeading6 = formattedMessage.find("###### ", pos);
startBlockquote = formattedMessage.find("> ", pos);
startBullet1 = formattedMessage.find("* ", pos);
startBullet2 = formattedMessage.find("- ", pos);
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos)
{
endMarker = formattedMessage.find("**", markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find("__", markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find("*", markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find("`", markerPos + markerLength);
}
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
}
else if (startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos) {
endMarker = formattedMessage.Length();
}
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos)
{
richTextChatBox->BeginBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->BeginUnderline();
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (startBold == markerPos)
{
richTextChatBox->EndBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->EndUnderline();
}
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if ((markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos))) // Bullet list
{
// Debugging Statements for Bullet List
wxLogDebug("Start of bullet detected at position: %zu", markerPos);
wxTextAttr bulletAttr;
bulletAttr.SetFlags(wxTEXT_ATTR_BULLET_STYLE | wxTEXT_ATTR_LEFT_INDENT);
bulletAttr.SetBulletStyle(wxTEXT_ATTR_BULLET_STYLE_STANDARD);
bulletAttr.SetBulletName("•");
bulletAttr.SetLeftIndent(100, 60);
richTextChatBox->WriteText("\n"); // Newline before the bullet list
richTextChatBox->BeginStyle(bulletAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
// Debugging Statements for Bullet Content
wxLogDebug("Bullet text: %s", styledText);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos)
{
pointSize *= 2;
}
else if (startHeading2 == markerPos)
{
pointSize *= 1.7;
}
else if (startHeading3 == markerPos)
{
pointSize *= 1.5;
}
else if (startHeading4 == markerPos)
{
pointSize *= 1.3;
}
else if (startHeading5 == markerPos)
{
pointSize *= 1.2;
}
else if (startHeading6 == markerPos)
{
pointSize *= 1.1;
}
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
}
Key Changes:
Explicitly Set Bullet Style:
We define wxTextAttr bulletAttr and set the bullet attributes manually, using SetFlags, SetBulletStyle, SetBulletName, and SetLeftIndent.
We start and end the bullet point style using BeginStyle and EndStyle.
Debugging Output:
Added debug output to help trace through the bullet processing steps.
Newlines Handling:
Ensured that each bullet item starts with a new line for better formatting.
With these changes, we're manually setting the bullet style using wxTextAttr instead of relying on potentially simpler but less flexible BeginStandardBullet and EndStandardBullet methods.
Please run this updated code, and let me know if the bullets now appear correctly. If they still don't appear as expected, the debug logs will provide more clues to help diagnose and address any issues.
Next, update WriteText to ensure bullet styles can be applied ad-hoc:
cpp Copy code
void Chat::WriteText(const wxString& message, const wxTextAttr& commonStyle)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
// Bullet points
wxString::size_type startBullet1 = formattedMessage.find("\n* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("\n- ", pos);
if (pos == 0)
{
startHeading1 = formattedMessage.find("# ", pos);
startHeading2 = formattedMessage.find("## ", pos);
startHeading3 = formattedMessage.find("### ", pos);
startHeading4 = formattedMessage.find("#### ", pos);
startHeading5 = formattedMessage.find("##### ", pos);
startHeading6 = formattedMessage.find("###### ", pos);
startBlockquote = formattedMessage.find("> ", pos);
startBullet1 = formattedMessage.find("* ", pos);
startBullet2 = formattedMessage.find("- ", pos);
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos)
{
endMarker = formattedMessage.find("**", markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find("__", markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find("*", markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find("`", markerPos + markerLength);
}
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
}
else if (startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos) {
endMarker = formattedMessage.Length();
}
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos)
{
richTextChatBox->BeginBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->BeginUnderline();
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (startBold == markerPos)
{
richTextChatBox->EndBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->EndUnderline();
}
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(commonStyle.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(commonStyle.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(commonStyle.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if ((markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos))) // Bullet list
{
// Debugging Statements for Bullet List
wxLogDebug("Start of bullet detected at position: %zu", markerPos);
wxTextAttr bulletAttr = commonStyle;
bulletAttr.SetFlags(wxTEXT_ATTR_BULLET_STYLE | wxTEXT_ATTR_LEFT_INDENT);
bulletAttr.SetBulletStyle(wxTEXT_ATTR_BULLET_STYLE_STANDARD);
bulletAttr.SetBulletName("•");
bulletAttr.SetLeftIndent(commonStyle.GetLeftIndent() + 100, 60);
richTextChatBox->WriteText("\n"); // Newline before the bullet list
richTextChatBox->BeginStyle(bulletAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
// Debugging Statements for Bullet Content
wxLogDebug("Bullet text: %s", styledText);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos)
{
pointSize *= 2;
}
else if (startHeading2 == markerPos)
{
pointSize *= 1.7;
}
else if (startHeading3 == markerPos)
{
pointSize *= 1.5;
}
else if (startHeading4 == markerPos)
{
pointSize *= 1.3;
}
else if (startHeading5 == markerPos)
{
pointSize *= 1.2;
}
else if (startHeading6 == markerPos)
{
pointSize *= 1.1;
}
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
}
Key Changes:
Refactored AppendMessage:
Removed BeginStyle and EndStyle wrapping WriteText. This allows WriteText to control styling as needed.
Customized Bullet Styling in WriteText:
In WriteText, we adapt the bulletAttr from commonStyle, adding setting specifics for bullets to avoid breaking any existing styles set by AppendMessage.
Consistent Handling of Insertion Points and View Updates:
Ensured that the rich text box insertion points and view positioning are handled outside the individual styling logic to avoid conflicts.
With these changes, the applied bullet styling should be appropriately handled without interference from surrounding styles. This refined structure should give you more flexibility to apply specific styles as needed across different sections of your text content. Let me know how this works!
commonStyle
cpp Copy code
void Chat::WriteText(const wxString& message, const wxTextAttr& commonStyle)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
richTextChatBox->BeginStyle(commonStyle); // Apply common style by default
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("> ", pos);
// Bullet points
wxString::size_type startBullet1 = formattedMessage.find("* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("- ", pos);
if (pos == 0)
{
startHeading1 = formattedMessage.find("# ", pos);
startHeading2 = formattedMessage.find("## ", pos);
startHeading3 = formattedMessage.find("### ", pos);
startHeading4 = formattedMessage.find("#### ", pos);
startHeading5 = formattedMessage.find("##### ", pos);
startHeading6 = formattedMessage.find("###### ", pos);
startBlockquote = formattedMessage.find("> ", pos);
startBullet1 = formattedMessage.find("* ", pos);
startBullet2 = formattedMessage.find("- ", pos);
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos)
{
endMarker = formattedMessage.find("**", markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find("__", markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find("*", markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find("`", markerPos + markerLength);
}
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
}
else if (startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos) {
endMarker = formattedMessage.Length();
}
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
wxRichTextAttr urlStyle = commonStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->WriteText(beforeMarker);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
wxTextAttr style = commonStyle;
if (startBold == markerPos)
{
style.SetFontWeight(wxFONTWEIGHT_BOLD);
}
else if (startUnderline == markerPos)
{
style.SetFontUnderlined(true);
}
richTextChatBox->BeginStyle(style);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
wxTextAttr style = commonStyle;
style.SetFontStyle(wxFONTSTYLE_ITALIC);
richTextChatBox->BeginStyle(style);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr = commonStyle;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(commonStyle.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(commonStyle.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr = commonStyle;
blockquoteAttr.SetBackgroundColour(commonStyle.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if ((markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos))) // Bullet list
{
// Debugging Statements for Bullet List
wxLogDebug("Start of bullet detected at position: %zu", markerPos);
wxTextAttr bulletAttr = commonStyle;
bulletAttr.SetFlags(wxTEXT_ATTR_BULLET_STYLE | wxTEXT_ATTR_LEFT_INDENT);
bulletAttr.SetBulletStyle(wxTEXT_ATTR_BULLET_STYLE_STANDARD);
bulletAttr.SetBulletName("•");
bulletAttr.SetLeftIndent(commonStyle.GetLeftIndent() + 100, 60);
richTextChatBox->WriteText("\n"); // Newline before the bullet list
richTextChatBox->BeginStyle(bulletAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
// Debugging Statements for Bullet Content
wxLogDebug("Bullet text: %s", styledText);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos) pointSize *= 2;
else if (startHeading2 == markerPos) pointSize *= 1.7;
else if (startHeading3 == markerPos) pointSize *= 1.5;
else if (startHeading4 == markerPos) pointSize *= 1.3;
else if (startHeading5 == markerPos) pointSize *= 1.2;
else if (startHeading6 == markerPos) pointSize *= 1.1;
wxTextAttr headingAttr = commonStyle;
headingAttr.SetFontSize(pointSize);
richTextChatBox->BeginStyle(headingAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
richTextChatBox->EndStyle(); // End common style
}
Key Points:
Common Style Application:
commonStyle is applied at the start and end of the function to make sure all text respects the general styling unless overridden.
Style Overrides Within WriteText:
Specific markers (like bold, italics, bullets) set their own styles temporarily, which inherit from commonStyle to maintain consistency in base properties like text color and alignment.
Debug Logging:
Debug output statements are kept for troubleshooting bullet processing and other markers.
Smooth Style Transition:
Ensured the logic for overriding styles correctly falls back to commonStyle between different styled sections.
With this approach, the general style should persist while still allowing for specific markdown formatting where necessary. Let me know how this works out and if it addresses the styling issues effectively!
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
wxString::size_type startBullet1 = formattedMessage.find("\n* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("\n- ", pos);
if (pos == 0)
{
startHeading1 = formattedMessage.find("# ", pos);
startHeading2 = formattedMessage.find("## ", pos);
startHeading3 = formattedMessage.find("### ", pos);
startHeading4 = formattedMessage.find("#### ", pos);
startHeading5 = formattedMessage.find("##### ", pos);
startHeading6 = formattedMessage.find("###### ", pos);
startBlockquote = formattedMessage.find("> ", pos);
startBullet1 = formattedMessage.find("* ", pos);
startBullet2 = formattedMessage.find("- ", pos);
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 }
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos)
{
endMarker = formattedMessage.find("**", markerPos + markerLength);
}
else if (startUnderline == markerPos)
{
endMarker = formattedMessage.find("__", markerPos + markerLength);
}
else if (startItalic == markerPos)
{
endMarker = formattedMessage.find("*", markerPos + markerLength);
}
else if (startCode == markerPos)
{
endMarker = formattedMessage.find("`", markerPos + markerLength);
}
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos || startBlockquote == markerPos || startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos)
{
endMarker = formattedMessage.Length();
}
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos)
{
richTextChatBox->BeginBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->BeginUnderline();
}
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (startBold == markerPos)
{
richTextChatBox->EndBold();
}
else if (startUnderline == markerPos)
{
richTextChatBox->EndUnderline();
}
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos)) // Bullet list
{
richTextChatBox->WriteText("\n");
richTextChatBox->BeginBold();
richTextChatBox->WriteText("@");
richTextChatBox->EndBold();
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength >= 2 && markerLength <= 7 && (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos)
{
pointSize *= 2;
}
else if (startHeading2 == markerPos)
{
pointSize *= 1.7;
}
else if (startHeading3 == markerPos)
{
pointSize *= 1.5;
}
else if (startHeading4 == markerPos)
{
pointSize *= 1.3;
}
else if (startHeading5 == markerPos)
{
pointSize *= 1.2;
}
else if (startHeading6 == markerPos)
{
pointSize *= 1.1;
}
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos || startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)
{
pos = endMarker + 1;
}
else
{
pos = endMarker + markerLength;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
I tried using this string:
"This is a non-numbered bullet list:\n\n- Item 1\n- Item 2\n- Item 3\n- Item 4\n- Item 5"
And this is the result shown:
"This is a non-numbered bullet list:
@Item 1
Item 2
@ Item 3
Item 4
@ Item 5
As it looks, the code doesn't take into account the ending marker of one element and the first element of the other, and that happens between elements, how can I fix my code, and after fixing this error we can then move to the list itself
void Chat::WriteText(const wxString& message, const wxTextAttr& style)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
wxString::size_type startBullet1 = formattedMessage.find("\n* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("\n- ", pos);
// Handle position 0 markers
if (pos == 0)
{
if (formattedMessage.StartsWith("# ")) startHeading1 = 0;
else if (formattedMessage.StartsWith("## ")) startHeading2 = 0;
else if (formattedMessage.StartsWith("### ")) startHeading3 = 0;
else if (formattedMessage.StartsWith("#### ")) startHeading4 = 0;
else if (formattedMessage.StartsWith("##### ")) startHeading5 = 0;
else if (formattedMessage.StartsWith("###### ")) startHeading6 = 0;
else if (formattedMessage.StartsWith("> ")) startBlockquote = 0;
else if (formattedMessage.StartsWith("* ")) startBullet1 = 0;
else if (formattedMessage.StartsWith("- ")) startBullet2 = 0;
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 },
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos) endMarker = formattedMessage.find("**", markerPos + markerLength);
else if (startUnderline == markerPos) endMarker = formattedMessage.find("__", markerPos + markerLength);
else if (startItalic == markerPos) endMarker = formattedMessage.find("*", markerPos + markerLength);
else if (startCode == markerPos) endMarker = formattedMessage.find("`", markerPos + markerLength);
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos ||
startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos ||
startBlockquote == markerPos || startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos) endMarker = formattedMessage.Length();
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos) richTextChatBox->BeginBold();
else if (startUnderline == markerPos) richTextChatBox->BeginUnderline();
richTextChatBox->WriteText(styledText);
if (startBold == markerPos) richTextChatBox->EndBold();
else if (startUnderline == markerPos) richTextChatBox->EndUnderline();
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos)) // Bullet list
{
richTextChatBox->WriteText("\n");
richTextChatBox->BeginBold();
richTextChatBox->WriteText("•\t");
richTextChatBox->EndBold();
richTextChatBox->WriteText(styledText);
if (formattedMessage.at(endMarker) == '\n') endMarker++;
}
else if (markerLength >= 2 && markerLength <= 7 &&
(startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos ||
startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos) pointSize *= 2;
else if (startHeading2 == markerPos) pointSize *= 1.7;
else if (startHeading3 == markerPos) pointSize *= 1.5;
else if (startHeading4 == markerPos) pointSize *= 1.3;
else if (startHeading5 == markerPos) pointSize *= 1.2;
else if (startHeading6 == markerPos) pointSize *= 1.1;
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
pos = endMarker + 1;
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
}
Key Points in the Updated Code:
Initial Position Handling:
If the position pos is 0, the function handles the possible markdown marker at the initial position.
Correct Handling of Newline-Surrounded Markers:
Special attention is given to markers like bullet points that follow newline characters. This helps ensure proper distinction between segments.
Clear Extraction of Styled Text:
Extracting the text to be styled correctly considers where each styled segment ends and where the next begins.
Inline Bullet Representation:
For clarity in bullet points, the • character is used directly, followed by a tab (\t) for alignment.
By using this revised approach, the transition between different markdown elements, especially around newline characters, should work more reliably, extracting and formatting each segment distinctly. This should resolve the parsing issues you've encountered.
void Chat::WriteText(const wxString& message, const wxTextAttr& style)
{
wxString formattedMessage = FormatMessageString(message) + (streaming ? "" : "\n");
formattedMessage.Replace("<br>", "\n");
wxString::size_type pos = 0;
while (pos < formattedMessage.Length() && pos != wxString::npos)
{
wxString::size_type startBold = formattedMessage.find("**", pos);
wxString::size_type startItalic = formattedMessage.find("*", pos);
wxString::size_type startUnderline = formattedMessage.find("__", pos);
wxString::size_type startLinkText = formattedMessage.find("[", pos);
wxString::size_type startCode = formattedMessage.find("`", pos);
wxString::size_type startHeading1 = formattedMessage.find("\n# ", pos);
wxString::size_type startHeading2 = formattedMessage.find("\n## ", pos);
wxString::size_type startHeading3 = formattedMessage.find("\n### ", pos);
wxString::size_type startHeading4 = formattedMessage.find("\n#### ", pos);
wxString::size_type startHeading5 = formattedMessage.find("\n##### ", pos);
wxString::size_type startHeading6 = formattedMessage.find("\n###### ", pos);
wxString::size_type startBlockquote = formattedMessage.find("\n> ", pos);
wxString::size_type startBullet1 = formattedMessage.find("\n* ", pos);
wxString::size_type startBullet2 = formattedMessage.find("\n- ", pos);
// Handle position 0 markers
if (pos == 0)
{
if (formattedMessage.StartsWith("# ")) startHeading1 = 0;
else if (formattedMessage.StartsWith("## ")) startHeading2 = 0;
else if (formattedMessage.StartsWith("### ")) startHeading3 = 0;
else if (formattedMessage.StartsWith("#### ")) startHeading4 = 0;
else if (formattedMessage.StartsWith("##### ")) startHeading5 = 0;
else if (formattedMessage.StartsWith("###### ")) startHeading6 = 0;
else if (formattedMessage.StartsWith("> ")) startBlockquote = 0;
else if (formattedMessage.StartsWith("* ")) startBullet1 = 0;
else if (formattedMessage.StartsWith("- ")) startBullet2 = 0;
}
std::vector<std::pair<wxString::size_type, int>> markers = {
{ startBold, 2 },
{ startItalic, 1 },
{ startUnderline, 2 },
{ startCode, 1 },
{ startHeading6, 7 },
{ startHeading5, 6 },
{ startHeading4, 5 },
{ startHeading3, 4 },
{ startHeading2, 3 },
{ startHeading1, 2 },
{ startBlockquote, 2 },
{ startBullet1, 2 },
{ startBullet2, 2 },
{ startLinkText, -1 },
};
// Find the earliest marker
auto earliestIt = std::min_element(markers.begin(), markers.end(), [](const auto& lhs, const auto& rhs){
return (lhs.first != wxString::npos && (rhs.first == wxString::npos || lhs.first < rhs.first));
});
if (earliestIt->first != wxString::npos)
{
wxString::size_type markerPos = earliestIt->first;
int markerLength = earliestIt->second;
wxString::size_type endMarker;
if (startBold == markerPos) endMarker = formattedMessage.find("**", markerPos + markerLength);
else if (startUnderline == markerPos) endMarker = formattedMessage.find("__", markerPos + markerLength);
else if (startItalic == markerPos) endMarker = formattedMessage.find("*", markerPos + markerLength);
else if (startCode == markerPos) endMarker = formattedMessage.find("`", markerPos + markerLength);
else if (startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos ||
startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos ||
startBlockquote == markerPos || startBullet1 == markerPos || startBullet2 == markerPos)
{
endMarker = formattedMessage.find("\n", markerPos + markerLength);
if (endMarker == wxString::npos) endMarker = formattedMessage.Length();
}
else if (startLinkText == markerPos)
{
wxString::size_type endLinkText = formattedMessage.find("]", markerPos);
wxString::size_type startLinkUrl = formattedMessage.find("(", endLinkText);
wxString::size_type endLinkUrl = formattedMessage.find(")", startLinkUrl);
if (endLinkText != wxString::npos && startLinkUrl != wxString::npos && endLinkUrl != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
wxString linkText = formattedMessage.substr(markerPos + 1, endLinkText - markerPos - 1);
wxString linkUrl = formattedMessage.substr(startLinkUrl + 1, endLinkUrl - startLinkUrl - 1);
if (linkUrl.StartsWith("http://") || linkUrl.StartsWith("https://"))
{
richTextChatBox->WriteText(beforeMarker);
wxRichTextAttr urlStyle;
urlStyle.SetTextColour(*wxBLUE);
urlStyle.SetFontUnderlined(true);
richTextChatBox->BeginStyle(urlStyle);
richTextChatBox->BeginURL(linkUrl);
richTextChatBox->WriteText(linkText);
richTextChatBox->EndURL();
richTextChatBox->EndStyle();
pos = endLinkUrl + 1;
continue;
}
else
{
wxString invalidLinkText = formattedMessage.substr(markerPos, endLinkUrl - markerPos + 1);
richTextChatBox->WriteText(beforeMarker + invalidLinkText);
pos = endLinkUrl + 1;
continue;
}
}
}
if (endMarker != wxString::npos)
{
wxString beforeMarker = formattedMessage.substr(pos, markerPos - pos);
richTextChatBox->WriteText(beforeMarker);
wxString styledText = formattedMessage.substr(markerPos + markerLength, endMarker - markerPos - markerLength);
if (markerLength == 2 && (startBold == markerPos || startUnderline == markerPos)) // Bold or Underline
{
if (startBold == markerPos) richTextChatBox->BeginBold();
else if (startUnderline == markerPos) richTextChatBox->BeginUnderline();
richTextChatBox->WriteText(styledText);
if (startBold == markerPos) richTextChatBox->EndBold();
else if (startUnderline == markerPos) richTextChatBox->EndUnderline();
}
else if (markerLength == 1 && startItalic == markerPos) // Italic
{
richTextChatBox->BeginItalic();
richTextChatBox->WriteText(styledText);
richTextChatBox->EndItalic();
}
else if (markerLength == 1 && startCode == markerPos) // Inline code
{
wxTextAttr codeAttr;
codeAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
codeAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(60));
codeAttr.SetTextColour(style.GetTextColour().ChangeLightness(140));
richTextChatBox->BeginStyle(codeAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
}
else if (markerLength == 2 && startBlockquote == markerPos) // Blockquote
{
wxTextAttr blockquoteAttr;
blockquoteAttr.SetBackgroundColour(style.GetBackgroundColour().ChangeLightness(50));
blockquoteAttr.SetTextColour(wxColour(220, 220, 220));
richTextChatBox->BeginStyle(blockquoteAttr);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndStyle();
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n')
{
richTextChatBox->WriteText("\n");
}
}
else if (markerLength == 2 && (startBullet1 == markerPos || startBullet2 == markerPos)) // Bullet list
{
richTextChatBox->WriteText("\n");
richTextChatBox->BeginBold();
richTextChatBox->WriteText("@ ");
richTextChatBox->EndBold();
richTextChatBox->WriteText(styledText);
endMarker++; // Move to next position to prevent overlap
if (endMarker < formattedMessage.Length() && formattedMessage[endMarker] == '\n') {
endMarker++;
}
}
else if (markerLength >= 2 && markerLength <= 7 &&
(startHeading1 == markerPos || startHeading2 == markerPos || startHeading3 == markerPos ||
startHeading4 == markerPos || startHeading5 == markerPos || startHeading6 == markerPos)) // Headings
{
wxFont font = richTextChatBox->GetFont();
int pointSize = font.GetPointSize();
if (startHeading1 == markerPos) pointSize *= 2;
else if (startHeading2 == markerPos) pointSize *= 1.7;
else if (startHeading3 == markerPos) pointSize *= 1.5;
else if (startHeading4 == markerPos) pointSize *= 1.3;
else if (startHeading5 == markerPos) pointSize *= 1.2;
else if (startHeading6 == markerPos) pointSize *= 1.1;
richTextChatBox->WriteText("\n");
richTextChatBox->BeginFontSize(pointSize);
richTextChatBox->WriteText(styledText);
richTextChatBox->EndFontSize();
richTextChatBox->WriteText("\n");
}
pos = endMarker;
}
else
{
pos++;
richTextChatBox->WriteText(formattedMessage.substr(pos));
}
}
else
{
richTextChatBox->WriteText(formattedMessage.substr(pos));
pos = wxString::npos;
}
}
}
Key Adjustments:
Ensure Constant Newlines and Spaces:
Guarantee newline precedes all bullet markers.
Ensure Text Extraction:
Correctly extract the start and end of segments, including bullet points, ensuring no overlap or missed transitions.
Precise Position Increment:
Move position pointer pos correctly through recursive increments to prevent shared or skipped characters between segment markers.
This approach should ensure consistent formatting for bullet points and intended transitions between markdown-segmented content. Let me know if this resolves the issue!