+
+/*
+ * RichTextFrame is used to demonstrate rich text behaviour
+ */
+
+enum
+{
+ RICHTEXT_CLOSE = 1000,
+ RICHTEXT_LEFT_ALIGN,
+ RICHTEXT_RIGHT_ALIGN,
+ RICHTEXT_CENTRE,
+ RICHTEXT_JUSTIFY,
+ RICHTEXT_CHANGE_FONT,
+ RICHTEXT_CHANGE_TEXT_COLOUR,
+ RICHTEXT_CHANGE_BACKGROUND_COLOUR,
+ RICHTEXT_LEFT_INDENT,
+ RICHTEXT_RIGHT_INDENT
+};
+
+BEGIN_EVENT_TABLE(RichTextFrame, wxFrame)
+ EVT_IDLE(RichTextFrame::OnIdle)
+ EVT_MENU(RICHTEXT_CLOSE, RichTextFrame::OnClose)
+ EVT_MENU(RICHTEXT_LEFT_ALIGN, RichTextFrame::OnLeftAlign)
+ EVT_MENU(RICHTEXT_RIGHT_ALIGN, RichTextFrame::OnRightAlign)
+ EVT_MENU(RICHTEXT_CENTRE, RichTextFrame::OnCentre)
+ EVT_MENU(RICHTEXT_JUSTIFY, RichTextFrame::OnJustify)
+ EVT_MENU(RICHTEXT_CHANGE_FONT, RichTextFrame::OnChangeFont)
+ EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR, RichTextFrame::OnChangeTextColour)
+ EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR, RichTextFrame::OnChangeBackgroundColour)
+ EVT_MENU(RICHTEXT_LEFT_INDENT, RichTextFrame::OnLeftIndent)
+ EVT_MENU(RICHTEXT_RIGHT_INDENT, RichTextFrame::OnRightIndent)
+END_EVENT_TABLE()
+
+RichTextFrame::RichTextFrame(wxWindow* parent, const wxString& title):
+ wxFrame(parent, -1, title, wxDefaultPosition, wxSize(300, 400))
+{
+ m_currentPosition = -1;
+ m_textCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
+ wxDefaultSize, wxTE_MULTILINE|wxTE_RICH2);
+
+ wxString value;
+ int i;
+ for (i = 0; i < 10; i++)
+ {
+ int j;
+ for (j = 0; j < 10; j++)
+ {
+ value << wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
+ }
+ value << wxT("\n\n");
+ }
+ m_textCtrl->SetValue(value);
+
+ wxMenuBar* menuBar = new wxMenuBar;
+ wxMenu* fileMenu = new wxMenu;
+ fileMenu->Append(RICHTEXT_CLOSE, _("Close\tCtrl+W"));
+ menuBar->Append(fileMenu, _("File"));
+
+ wxMenu* editMenu = new wxMenu;
+ editMenu->Append(RICHTEXT_LEFT_ALIGN, _("Left Align"));
+ editMenu->Append(RICHTEXT_RIGHT_ALIGN, _("Right Align"));
+ editMenu->Append(RICHTEXT_CENTRE, _("Centre"));
+ editMenu->Append(RICHTEXT_JUSTIFY, _("Justify"));
+ editMenu->AppendSeparator();
+ editMenu->Append(RICHTEXT_CHANGE_FONT, _("Change Font"));
+ editMenu->Append(RICHTEXT_CHANGE_TEXT_COLOUR, _("Change Text Colour"));
+ editMenu->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR, _("Change Background Colour"));
+ editMenu->AppendSeparator();
+ editMenu->Append(RICHTEXT_LEFT_INDENT, _("Left Indent"));
+ editMenu->Append(RICHTEXT_RIGHT_INDENT, _("Right indent"));
+ menuBar->Append(editMenu, _("Edit"));
+
+ SetMenuBar(menuBar);
+ CreateStatusBar();
+}
+
+// Event handlers
+
+void RichTextFrame::OnClose(wxCommandEvent& WXUNUSED(event))
+{
+ Close(TRUE);
+}
+
+void RichTextFrame::OnLeftAlign(wxCommandEvent& WXUNUSED(event))
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+}
+
+void RichTextFrame::OnRightAlign(wxCommandEvent& WXUNUSED(event))
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+}
+
+void RichTextFrame::OnJustify(wxCommandEvent& WXUNUSED(event))
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+}
+
+void RichTextFrame::OnCentre(wxCommandEvent& WXUNUSED(event))
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+}
+
+void RichTextFrame::OnChangeFont(wxCommandEvent& WXUNUSED(event))
+{
+ wxFontData data;
+
+ wxFontDialog dialog(this, data);
+
+ if (dialog.ShowModal() == wxID_OK)
+ {
+ wxFontData retData = dialog.GetFontData();
+ wxFont font = retData.GetChosenFont();
+
+ wxTextAttr attr;
+ attr.SetFont(font);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+ }
+}
+
+void RichTextFrame::OnChangeTextColour(wxCommandEvent& WXUNUSED(event))
+{
+ wxColourData data;
+ data.SetColour(* wxBLACK);
+ data.SetChooseFull(TRUE);
+ for (int i = 0; i < 16; i++)
+ {
+ wxColour colour(i*16, i*16, i*16);
+ data.SetCustomColour(i, colour);
+ }
+
+ wxColourDialog dialog(this, &data);
+ dialog.SetTitle(_T("Choose the text colour"));
+ if (dialog.ShowModal() == wxID_OK)
+ {
+ wxColourData retData = dialog.GetColourData();
+ wxColour col = retData.GetColour();
+
+ wxTextAttr attr;
+ attr.SetTextColour(col);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+ }
+}
+
+void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
+{
+ wxColourData data;
+ data.SetColour(* wxWHITE);
+ data.SetChooseFull(TRUE);
+ for (int i = 0; i < 16; i++)
+ {
+ wxColour colour(i*16, i*16, i*16);
+ data.SetCustomColour(i, colour);
+ }
+
+ wxColourDialog dialog(this, &data);
+ dialog.SetTitle(_T("Choose the text background colour"));
+ if (dialog.ShowModal() == wxID_OK)
+ {
+ wxColourData retData = dialog.GetColourData();
+ wxColour col = retData.GetColour();
+
+ wxTextAttr attr;
+ attr.SetBackgroundColour(col);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+ }
+}
+
+void RichTextFrame::OnLeftIndent(wxCommandEvent& WXUNUSED(event))
+{
+ wxString indentStr = wxGetTextFromUser
+ (
+ _("Please enter the left indent in tenths of a millimetre."),
+ _("Left Indent")
+ );
+ if (!indentStr.IsEmpty())
+ {
+ int indent = wxAtoi(indentStr);
+
+ wxTextAttr attr;
+ attr.SetLeftIndent(indent);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+ }
+}
+
+void RichTextFrame::OnRightIndent(wxCommandEvent& WXUNUSED(event))
+{
+ wxString indentStr = wxGetTextFromUser
+ (
+ _("Please enter the right indent in tenths of a millimetre."),
+ _("Right Indent")
+ );
+ if (!indentStr.IsEmpty())
+ {
+ int indent = wxAtoi(indentStr);
+
+ wxTextAttr attr;
+ attr.SetRightIndent(indent);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+
+ m_currentPosition = -1;
+ }
+}
+
+void RichTextFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
+{
+ long insertionPoint = m_textCtrl->GetInsertionPoint();
+ if (insertionPoint != m_currentPosition)
+ {
+ wxTextAttr attr;
+ if (m_textCtrl->GetStyle(insertionPoint, attr))
+ {
+ wxString msg;
+ wxString facename(wxT("unknown"));
+ if (attr.GetFont().Ok())
+ {
+ facename = attr.GetFont().GetFaceName();
+ }
+ wxString alignment(wxT("unknown alignment"));
+ if (attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
+ alignment = wxT("centred");
+ else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
+ alignment = wxT("right-aligned");
+ else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_LEFT)
+ alignment = wxT("left-aligned");
+ else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED)
+ alignment = wxT("justified");
+ msg.Printf(wxT("Facename: %s, wxColour(%d, %d, %d), %s"),
+ (const wxChar*) facename,
+ attr.GetTextColour().Red(), attr.GetTextColour().Green(), attr.GetTextColour().Blue(),
+ (const wxChar*) alignment);
+ SetStatusText(msg);
+ }
+ m_currentPosition = insertionPoint;
+ }
+}