+// Demonstrates how you can change the style sheets and have the changes
+// reflected in the control content without wiping out character formatting.
+
+void MyFrame::OnSwitchStyleSheets(wxCommandEvent& WXUNUSED(event))
+{
+ static wxRichTextStyleSheet* gs_AlternateStyleSheet = NULL;
+
+ wxRichTextStyleListCtrl *styleList = (wxRichTextStyleListCtrl*) FindWindow(ID_RICHTEXT_STYLE_LIST);
+ wxRichTextStyleComboCtrl* styleCombo = (wxRichTextStyleComboCtrl*) FindWindow(ID_RICHTEXT_STYLE_COMBO);
+
+ wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
+
+ // One-time creation of an alternate style sheet
+ if (!gs_AlternateStyleSheet)
+ {
+ gs_AlternateStyleSheet = new wxRichTextStyleSheet(*sheet);
+
+ // Make some modifications
+ for (int i = 0; i < (int) gs_AlternateStyleSheet->GetParagraphStyleCount(); i++)
+ {
+ wxRichTextParagraphStyleDefinition* def = gs_AlternateStyleSheet->GetParagraphStyle(i);
+
+ if (def->GetStyle().HasTextColour())
+ def->GetStyle().SetTextColour(*wxBLUE);
+
+ if (def->GetStyle().HasAlignment())
+ {
+ if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
+ def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
+ else if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT)
+ def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
+ }
+ if (def->GetStyle().HasLeftIndent())
+ {
+ def->GetStyle().SetLeftIndent(def->GetStyle().GetLeftIndent() * 2);
+ }
+ }
+ }
+
+ // Switch sheets
+ wxRichTextStyleSheet* tmp = gs_AlternateStyleSheet;
+ gs_AlternateStyleSheet = sheet;
+ sheet = tmp;
+
+ m_richTextCtrl->SetStyleSheet(sheet);
+ m_richTextCtrl->ApplyStyleSheet(sheet); // Makes the control reflect the new style definitions
+
+ styleList->SetStyleSheet(sheet);
+ styleList->UpdateStyles();
+
+ styleCombo->SetStyleSheet(sheet);
+ styleCombo->UpdateStyles();
+}
+
+void MyFrame::OnManageStyles(wxCommandEvent& WXUNUSED(event))
+{
+ wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
+
+ int flags = wxRICHTEXT_ORGANISER_CREATE_STYLES|wxRICHTEXT_ORGANISER_EDIT_STYLES;
+
+ wxRichTextStyleOrganiserDialog dlg(flags, sheet, NULL, this, wxID_ANY, _("Style Manager"));
+ dlg.ShowModal();
+}
+
+void MyFrame::OnInsertSymbol(wxCommandEvent& WXUNUSED(event))
+{
+ wxRichTextAttr attr;
+ attr.SetFlags(wxTEXT_ATTR_FONT);
+ m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr);
+
+ wxString currentFontName;
+ if (attr.HasFont() && attr.GetFont().IsOk())
+ currentFontName = attr.GetFont().GetFaceName();
+
+ // Don't set the initial font in the dialog (so the user is choosing
+ // 'normal text', i.e. the current font) but do tell the dialog
+ // what 'normal text' is.
+
+ wxSymbolPickerDialog dlg(wxT("*"), wxEmptyString, currentFontName, this);
+
+ if (dlg.ShowModal() == wxID_OK)
+ {
+ if (dlg.HasSelection())
+ {
+ long insertionPoint = m_richTextCtrl->GetInsertionPoint();
+
+ m_richTextCtrl->WriteText(dlg.GetSymbol());
+
+ if (!dlg.UseNormalFont())
+ {
+ wxFont font(attr.GetFont());
+ font.SetFaceName(dlg.GetFontName());
+ attr.SetFont(font);
+ m_richTextCtrl->SetStyle(insertionPoint, insertionPoint+1, attr);
+ }
+ }
+ }
+}
+
+void MyFrame::OnNumberList(wxCommandEvent& WXUNUSED(event))
+{
+ if (m_richTextCtrl->HasSelection())
+ {
+ wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
+ m_richTextCtrl->SetListStyle(range, wxT("Numbered List 1"), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
+ }
+}
+
+void MyFrame::OnBulletsAndNumbering(wxCommandEvent& WXUNUSED(event))
+{
+ wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
+
+ int flags = wxRICHTEXT_ORGANISER_BROWSE_NUMBERING;
+
+ wxRichTextStyleOrganiserDialog dlg(flags, sheet, m_richTextCtrl, this, wxID_ANY, _("Bullets and Numbering"));
+ if (dlg.ShowModal() == wxID_OK)
+ {
+ if (dlg.GetSelectedStyleDefinition())
+ dlg.ApplyStyle();
+ }
+}
+
+void MyFrame::OnItemizeList(wxCommandEvent& WXUNUSED(event))
+{
+ if (m_richTextCtrl->HasSelection())
+ {
+ wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
+ m_richTextCtrl->SetListStyle(range, wxT("Bullet List 1"));
+ }
+}
+
+void MyFrame::OnRenumberList(wxCommandEvent& WXUNUSED(event))
+{
+ if (m_richTextCtrl->HasSelection())
+ {
+ wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
+ m_richTextCtrl->NumberList(range, NULL, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
+ }
+}
+
+void MyFrame::OnPromoteList(wxCommandEvent& WXUNUSED(event))
+{
+ if (m_richTextCtrl->HasSelection())
+ {
+ wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
+ m_richTextCtrl->PromoteList(1, range, NULL);
+ }
+}
+
+void MyFrame::OnDemoteList(wxCommandEvent& WXUNUSED(event))
+{
+ if (m_richTextCtrl->HasSelection())
+ {
+ wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
+ m_richTextCtrl->PromoteList(-1, range, NULL);
+ }
+}
+
+void MyFrame::OnClearList(wxCommandEvent& WXUNUSED(event))
+{
+ if (m_richTextCtrl->HasSelection())
+ {
+ wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
+ m_richTextCtrl->ClearListStyle(range);
+ }
+}
+
+void MyFrame::OnInsertURL(wxCommandEvent& WXUNUSED(event))
+{
+ wxString url = wxGetTextFromUser(_("URL:"), _("Insert URL"));
+ if (!url.IsEmpty())
+ {
+ // Make a style suitable for showing a URL
+ wxRichTextAttr urlStyle;
+ urlStyle.SetTextColour(*wxBLUE);
+ urlStyle.SetFontUnderlined(true);
+
+ m_richTextCtrl->BeginStyle(urlStyle);
+ m_richTextCtrl->BeginURL(url);
+ m_richTextCtrl->WriteText(url);
+ m_richTextCtrl->EndURL();
+ m_richTextCtrl->EndStyle();
+ }
+}
+
+void MyFrame::OnInsertImage(wxCommandEvent& WXUNUSED(event))
+{
+ wxFileDialog dialog(this, _("Choose an image"), "", "", "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png");
+ if (dialog.ShowModal() == wxID_OK)
+ {
+ wxString path = dialog.GetPath();
+ m_richTextCtrl->WriteImage(path, wxBITMAP_TYPE_ANY);
+ }
+}
+
+void MyFrame::OnURL(wxTextUrlEvent& event)
+{
+ wxMessageBox(event.GetString());
+}
+
+// Veto style sheet replace events when loading from XML, since we want
+// to keep the original style sheet.
+void MyFrame::OnStyleSheetReplacing(wxRichTextEvent& event)
+{
+ event.Veto();
+}
+
+void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
+{
+ wxGetApp().GetPrinting()->PrintBuffer(m_richTextCtrl->GetBuffer());
+}
+
+void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
+{
+ wxGetApp().GetPrinting()->PreviewBuffer(m_richTextCtrl->GetBuffer());
+}
+
+void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
+{
+ wxDialog dialog(this, wxID_ANY, wxT("Testing"), wxPoint(10, 10), wxSize(400, 300), wxDEFAULT_DIALOG_STYLE);
+
+ wxNotebook* nb = new wxNotebook(& dialog, wxID_ANY, wxPoint(5, 5), wxSize(300, 250));
+ wxPanel* panel = new wxPanel(nb, wxID_ANY, wxDefaultPosition, wxDefaultSize);
+ wxPanel* panel2 = new wxPanel(nb, wxID_ANY, wxDefaultPosition, wxDefaultSize);
+
+ new wxRichTextCtrl(panel, wxID_ANY, wxEmptyString, wxPoint(5, 5), wxSize(200, 150), wxVSCROLL|wxTE_READONLY);
+ nb->AddPage(panel, wxT("Page 1"));
+
+ new wxRichTextCtrl(panel2, wxID_ANY, wxEmptyString, wxPoint(5, 5), wxSize(200, 150), wxVSCROLL|wxTE_READONLY);
+ nb->AddPage(panel2, wxT("Page 2"));
+
+ new wxButton(& dialog, wxID_OK, wxT("OK"), wxPoint(5, 180));
+
+ dialog.ShowModal();
+
+// wxGetApp().GetPrinting()->PageSetup();
+}
+
+void MyFrame::OnSetFontScale(wxCommandEvent& WXUNUSED(event))
+{
+ wxString value = wxString::Format(wxT("%g"), m_richTextCtrl->GetFontScale());
+ wxString text = wxGetTextFromUser(wxT("Enter a text scale factor:"), wxT("Text Scale Factor"), value, wxGetTopLevelParent(this));
+ if (!text.IsEmpty() && value != text)
+ {
+ double scale = 1.0;
+ wxSscanf(text, wxT("%lf"), & scale);
+ m_richTextCtrl->SetFontScale(scale, true);
+ }
+}
+
+void MyFrame::OnSetDimensionScale(wxCommandEvent& WXUNUSED(event))
+{
+ wxString value = wxString::Format(wxT("%g"), m_richTextCtrl->GetDimensionScale());
+ wxString text = wxGetTextFromUser(wxT("Enter a dimension scale factor:"), wxT("Dimension Scale Factor"), value, wxGetTopLevelParent(this));
+ if (!text.IsEmpty() && value != text)
+ {
+ double scale = 1.0;
+ wxSscanf(text, wxT("%lf"), & scale);
+ m_richTextCtrl->SetDimensionScale(scale, true);
+ }
+}
+
+void MyRichTextCtrl::PrepareContent(wxRichTextParagraphLayoutBox& container)
+{
+ if (IsLocked())
+ {
+ // Lock all content that's about to be added to the control
+ wxRichTextObjectList::compatibility_iterator node = container.GetChildren().GetFirst();
+ while (node)
+ {
+ wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
+ if (para)
+ {
+ wxRichTextObjectList::compatibility_iterator childNode = para->GetChildren().GetFirst();
+ while (childNode)
+ {
+ wxRichTextObject* obj = childNode->GetData();
+ obj->GetProperties().SetProperty(wxT("Lock"), m_lockId);
+
+ childNode = childNode->GetNext();
+ }
+ }
+ node = node->GetNext();
+ }
+ }
+}
+
+bool MyRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox& container, const wxRichTextRange& range) const
+{
+ long i;
+ for (i = range.GetStart(); i < range.GetEnd(); i++)
+ {
+ wxRichTextObject* obj = container.GetLeafObjectAtPosition(i);
+ if (obj && obj->GetProperties().HasProperty(wxT("Lock")))
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool MyRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox& container, long pos) const
+{
+ wxRichTextObject* child1 = container.GetLeafObjectAtPosition(pos);
+ wxRichTextObject* child2 = container.GetLeafObjectAtPosition(pos-1);
+
+ long lock1 = -1, lock2 = -1;
+
+ if (child1 && child1->GetProperties().HasProperty(wxT("Lock")))
+ lock1 = child1->GetProperties().GetPropertyLong(wxT("Lock"));
+ if (child2 && child2->GetProperties().HasProperty(wxT("Lock")))
+ lock2 = child2->GetProperties().GetPropertyLong(wxT("Lock"));
+
+ if (lock1 != -1 && lock1 == lock2)
+ return false;
+
+ // Don't allow insertion before a locked object if it's at the beginning of the buffer.
+ if (pos == 0 && lock1 != -1)
+ return false;
+
+ return true;
+}
+
+
+class wxRichTextEnhancedDrawingHandler: public wxRichTextDrawingHandler
+{
+public:
+ wxRichTextEnhancedDrawingHandler()
+ {
+ SetName(wxT("enhanceddrawing"));
+ m_lockBackgroundColour = wxColour(220, 220, 220);
+ }
+
+ /**
+ Returns @true if this object has virtual attributes that we can provide.
+ */
+ virtual bool HasVirtualAttributes(wxRichTextObject* obj) const;
+
+ /**
+ Provides virtual attributes that we can provide.
+ */
+ virtual bool GetVirtualAttributes(wxRichTextAttr& attr, wxRichTextObject* obj) const;
+
+ wxColour m_lockBackgroundColour;
+};
+
+bool wxRichTextEnhancedDrawingHandler::HasVirtualAttributes(wxRichTextObject* obj) const
+{
+ return obj->GetProperties().HasProperty(wxT("Lock"));
+}
+
+bool wxRichTextEnhancedDrawingHandler::GetVirtualAttributes(wxRichTextAttr& attr, wxRichTextObject* obj) const
+{
+ if (obj->GetProperties().HasProperty(wxT("Lock")))
+ {
+ attr.SetBackgroundColour(m_lockBackgroundColour);
+ return true;
+ }
+ return false;
+}
+
+void MyRichTextCtrl::SetEnhancedDrawingHandler()
+{
+ wxRichTextBuffer::AddDrawingHandler(new wxRichTextEnhancedDrawingHandler);
+}