+
+/*
+ * 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_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_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);
+}
+
+// Event handlers
+
+void RichTextFrame::OnClose(wxCommandEvent& event)
+{
+ Close(TRUE);
+}
+
+void RichTextFrame::OnLeftAlign(wxCommandEvent& event)
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+}
+
+void RichTextFrame::OnRightAlign(wxCommandEvent& event)
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+}
+
+void RichTextFrame::OnJustify(wxCommandEvent& event)
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+}
+
+void RichTextFrame::OnCentre(wxCommandEvent& event)
+{
+ wxTextAttr attr;
+ attr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
+
+ long start, end;
+ m_textCtrl->GetSelection(& start, & end);
+ m_textCtrl->SetStyle(start, end, attr);
+}
+
+void RichTextFrame::OnChangeFont(wxCommandEvent& 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);
+ }
+}
+
+void RichTextFrame::OnChangeTextColour(wxCommandEvent& 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);
+ }
+}
+
+void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent& 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);
+ }
+}
+
+void RichTextFrame::OnLeftIndent(wxCommandEvent& 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);
+ }
+}
+
+void RichTextFrame::OnRightIndent(wxCommandEvent& 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);
+ }
+}