creating a style sheet for wxRichTextCtrl - sample code
Posted: Sat Oct 08, 2011 6:48 pm
Here is a code snippet I wrote for creating a style sheet for a wxRichTextCtrl, and thought I'll share it in case it will be of use to others. You can probably modify it for other types of text control. Public domain. Use at your own risk. Note that I chose to create objects as static instead of calling new.
Keywords: wxRichTextCtrl RichText rich text stylesheet style sheet wxTextCtrl TextCtrl
Keywords: wxRichTextCtrl RichText rich text stylesheet style sheet wxTextCtrl TextCtrl
Code: Select all
#include <wx/richtext/richtextstyles.h> // needed for wxRichTextStyleSheet below
void create_style_sheet_if_first_time(wxRichTextCtrl& rt )
{
static bool first_time(true);
if (!first_time) return;
// create a new style that we want
static wxRichTextAttr st_title = rt.GetDefaultStyleEx(); // start with default style as the base
st_title.SetAlignment(wxTEXT_ALIGNMENT_CENTRE); // center it
st_title.SetFontWeight(wxBOLD); // make it bold
st_title.SetParagraphSpacingAfter(20); // set spacing after para
static wxRichTextParagraphStyleDefinition std_title; // create a new style definition
std_title.SetStyle(st_title); // set its style to the style we created
std_title.SetName(L"Title_Style"); // give that style definition a name
// create another new style that we want
static wxRichTextAttr st_litem = rt.GetDefaultStyleEx(); // start with default style as the base
st_litem.SetAlignment(wxTEXT_ALIGNMENT_LEFT); // make it left aligned
st_litem.SetParagraphSpacingAfter(10); // set spacing after para
static wxRichTextParagraphStyleDefinition std_litem; // create a new style definition
std_litem.SetStyle(st_litem); // set its style to the style we created
std_litem.SetName(L"List_Item_Style"); // give that style definition a name
static wxRichTextStyleSheet myStyleSheet; // create a new rich text style sheet
myStyleSheet.AddParagraphStyle(&std_title); // add new style definition to style sheet
myStyleSheet.AddParagraphStyle(&std_litem); // add new style definition to style sheet
rt.SetStyleSheet(&myStyleSheet); // use the style sheet in our rich text control
first_time = false;
}