Rich text
Wx::RTC::RichTextCtrl is a powerful formatted text editor. Unlike Wx::TextCtrl, it supports inline bold, italic, font sizes, colours, and paragraph alignment — all within a single editable widget. It is suitable for any application that needs to display or produce styled text: documentation tools, note-taking apps, report editors, and markdown previewers.
Creating a RichTextCtrl
The widget lives in the Wx::RTC namespace:
|
|
Wx::WANTS_CHARS is important — without it, Tab and Enter keys may not work correctly inside the control.
Writing content programmatically
Use freeze and thaw to suppress repainting while adding content. Without them, the control repaints on every write call, which is slow for large documents:
|
|
Inline formatting
Formatting uses a begin/end pair that brackets the text it applies to:
|
|
Mixed formatting in a single line requires interleaving plain text between formatted spans:
|
|
Font size
|
|
Text colour
|
|
Custom font
|
|
Paragraph alignment
|
|
Applying formatting to selections
When the user selects text and clicks a toolbar button, use apply_bold_to_selection and apply_italic_to_selection:
|
|
For other attributes (font size, colour) use set_style_ex with a RichTextAttr:
|
|
Always call set_focus after applying formatting from a toolbar button — otherwise the cursor leaves the editor.
Scrolling to position
After programmatically populating the control, scroll to the top:
|
|
Or to the end:
|
|
A reusable heading helper
When generating structured documents programmatically, a helper method keeps the code clean:
|
|
See rich_text_demo.rb for the complete demonstration including a simple toolbar with Bold, Italic, and font size controls.
What to take forward
Wx::RTC::RichTextCtrl— the namespace and class name- Use
freeze/thawaround bulk content generation - Formatting uses
begin_*/end_*pairs — nest them for combined effects apply_bold_to_selectionandapply_italic_to_selectionfor toolbar formattingset_style_exwithWx::RTC::RichTextAttrfor other selection formatting- Always
set_focusafter toolbar formatting to return the cursor to the editor
Previous: Images and bitmaps | Next: HtmlWindow