X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2d143b6689b9ce3f02398d1ee4129f6a21024183..65fb4b3a4f18e1d8b58ac92d50d4cc7f81f03783:/samples/richtext/richtext.cpp diff --git a/samples/richtext/richtext.cpp b/samples/richtext/richtext.cpp index 860e15d022..f101c86e5b 100644 --- a/samples/richtext/richtext.cpp +++ b/samples/richtext/richtext.cpp @@ -35,6 +35,7 @@ #include "wx/sstream.h" #include "wx/html/htmlwin.h" #include "wx/stopwatch.h" +#include "wx/sysopt.h" #if wxUSE_FILESYSTEM #include "wx/filesys.h" @@ -45,7 +46,7 @@ #include "wx/cshelp.h" #endif -#ifndef __WXMSW__ +#ifndef wxHAS_IMAGES_IN_RESOURCES #include "../sample.xpm" #endif @@ -81,6 +82,61 @@ #include "wx/richtext/richtextprint.h" #include "wx/richtext/richtextimagedlg.h" +// A custom field type +class wxRichTextFieldTypePropertiesTest: public wxRichTextFieldTypeStandard +{ +public: + wxRichTextFieldTypePropertiesTest(const wxString& name, const wxString& label, int displayStyle = wxRICHTEXT_FIELD_STYLE_RECTANGLE): + wxRichTextFieldTypeStandard(name, label, displayStyle) + { + } + wxRichTextFieldTypePropertiesTest(const wxString& name, const wxBitmap& bitmap, int displayStyle = wxRICHTEXT_FIELD_STYLE_RECTANGLE): + wxRichTextFieldTypeStandard(name, bitmap, displayStyle) + { + } + + virtual bool CanEditProperties(wxRichTextField* WXUNUSED(obj)) const { return true; } + virtual bool EditProperties(wxRichTextField* WXUNUSED(obj), wxWindow* WXUNUSED(parent), wxRichTextBuffer* WXUNUSED(buffer)) + { + wxString label = GetLabel(); + wxMessageBox(wxString::Format(wxT("Editing %s"), label.c_str())); + return true; + } + + virtual wxString GetPropertiesMenuLabel(wxRichTextField* WXUNUSED(obj)) const + { + return GetLabel(); + } +}; + +// A custom composite field type +class wxRichTextFieldTypeCompositeTest: public wxRichTextFieldTypePropertiesTest +{ +public: + wxRichTextFieldTypeCompositeTest(const wxString& name, const wxString& label): + wxRichTextFieldTypePropertiesTest(name, label, wxRICHTEXT_FIELD_STYLE_COMPOSITE) + { + } + + virtual bool UpdateField(wxRichTextBuffer* buffer, wxRichTextField* obj) + { + if (buffer) + { + wxRichTextAttr attr(buffer->GetAttributes()); + attr.GetTextBoxAttr().Reset(); + attr.SetParagraphSpacingAfter(0); + attr.SetLineSpacing(10); + obj->SetAttributes(attr); + } + obj->GetChildren().Clear(); + wxRichTextParagraph* para = new wxRichTextParagraph; + wxRichTextPlainText* text = new wxRichTextPlainText(GetLabel()); + para->AppendChild(text); + obj->AppendChild(para); + return true; + } +}; + // ---------------------------------------------------------------------------- // resources // ---------------------------------------------------------------------------- @@ -89,6 +145,49 @@ // private classes // ---------------------------------------------------------------------------- +// Define a new application type, each program should derive a class from wxApp +class MyRichTextCtrl: public wxRichTextCtrl +{ +public: + MyRichTextCtrl( wxWindow* parent, wxWindowID id = -1, const wxString& value = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, + long style = wxRE_MULTILINE, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxTextCtrlNameStr): + wxRichTextCtrl(parent, id, value, pos, size, style, validator, name) + { + m_lockId = 0; + m_locked = false; + } + + void SetLockId(long id) { m_lockId = id; } + long GetLockId() const { return m_lockId; } + + void BeginLock() { m_lockId ++; m_locked = true; } + void EndLock() { m_locked = false; } + bool IsLocked() const { return m_locked; } + + static void SetEnhancedDrawingHandler(); + + /** + Prepares the content just before insertion (or after buffer reset). Called by the same function in wxRichTextBuffer. + Currently is only called if undo mode is on. + */ + virtual void PrepareContent(wxRichTextParagraphLayoutBox& container); + + /** + Can we delete this range? + Sends an event to the control. + */ + virtual bool CanDeleteRange(wxRichTextParagraphLayoutBox& container, const wxRichTextRange& range) const; + + /** + Can we insert content at this position? + Sends an event to the control. + */ + virtual bool CanInsertContent(wxRichTextParagraphLayoutBox& container, long pos) const; + + long m_lockId; + bool m_locked; +}; + // Define a new application type, each program should derive a class from wxApp class MyApp : public wxApp { @@ -193,6 +292,9 @@ public: void OnPageSetup(wxCommandEvent& event); void OnInsertImage(wxCommandEvent& event); + + void OnSetFontScale(wxCommandEvent& event); + void OnSetDimensionScale(wxCommandEvent& event); protected: // Forward command events to the current rich text control, if any @@ -205,7 +307,7 @@ private: // any class wishing to process wxWidgets events must use this macro DECLARE_EVENT_TABLE() - wxRichTextCtrl* m_richTextCtrl; + MyRichTextCtrl* m_richTextCtrl; }; // ---------------------------------------------------------------------------- @@ -258,6 +360,9 @@ enum ID_FORMAT_DEMOTE_LIST, ID_FORMAT_CLEAR_LIST, + ID_SET_FONT_SCALE, + ID_SET_DIMENSION_SCALE, + ID_VIEW_HTML, ID_SWITCH_STYLE_SHEETS, ID_MANAGE_STYLES, @@ -352,6 +457,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_TEXT_URL(wxID_ANY, MyFrame::OnURL) EVT_RICHTEXT_STYLESHEET_REPLACING(wxID_ANY, MyFrame::OnStyleSheetReplacing) + + EVT_MENU(ID_SET_FONT_SCALE, MyFrame::OnSetFontScale) + EVT_MENU(ID_SET_DIMENSION_SCALE, MyFrame::OnSetDimensionScale) END_EVENT_TABLE() // Create a new application object: this macro will allow wxWidgets to create @@ -387,10 +495,31 @@ bool MyApp::OnInit() CreateStyles(); + MyRichTextCtrl::SetEnhancedDrawingHandler(); + // Add extra handlers (plain text is automatically added) wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler); wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler); + // Add field types + + wxRichTextBuffer::AddFieldType(new wxRichTextFieldTypePropertiesTest(wxT("rectangle"), wxT("RECTANGLE"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_RECTANGLE)); + + wxRichTextFieldTypeStandard* s1 = new wxRichTextFieldTypeStandard(wxT("begin-section"), wxT("SECTION"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_START_TAG); + s1->SetBackgroundColour(*wxBLUE); + + wxRichTextFieldTypeStandard* s2 = new wxRichTextFieldTypeStandard(wxT("end-section"), wxT("SECTION"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_END_TAG); + s2->SetBackgroundColour(*wxBLUE); + + wxRichTextFieldTypeStandard* s3 = new wxRichTextFieldTypeStandard(wxT("bitmap"), wxBitmap(paste_xpm), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_NO_BORDER); + + wxRichTextBuffer::AddFieldType(s1); + wxRichTextBuffer::AddFieldType(s2); + wxRichTextBuffer::AddFieldType(s3); + + wxRichTextFieldTypeCompositeTest* s4 = new wxRichTextFieldTypeCompositeTest(wxT("composite"), wxT("This is a field value")); + wxRichTextBuffer::AddFieldType(s4); + // Add image handlers #if wxUSE_LIBPNG wxImage::AddHandler( new wxPNGHandler ); @@ -589,6 +718,10 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) : wxFrame(NULL, id, title, pos, size, style) { +#ifdef __WXMAC__ + SetWindowVariant(wxWINDOW_VARIANT_SMALL); +#endif + // set the frame icon SetIcon(wxICON(sample)); @@ -623,11 +756,9 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, editMenu->AppendSeparator(); editMenu->Append(wxID_SELECTALL, _("Select A&ll\tCtrl+A")); -#if 0 editMenu->AppendSeparator(); - editMenu->Append(wxID_FIND, _("&Find...\tCtrl+F")); - editMenu->Append(stID_FIND_REPLACE, _("&Replace...\tCtrl+R")); -#endif + editMenu->Append(ID_SET_FONT_SCALE, _("Set &Text Scale...")); + editMenu->Append(ID_SET_DIMENSION_SCALE, _("Set &Dimension Scale...")); wxMenu* formatMenu = new wxMenu; formatMenu->AppendCheckItem(ID_FORMAT_BOLD, _("&Bold\tCtrl+B")); @@ -699,7 +830,21 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, } #endif - wxToolBar* toolBar = CreateToolBar(); + wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); + SetSizer(sizer); + + // On Mac, don't create a 'native' wxToolBar because small bitmaps are not supported by native + // toolbars. On Mac, a non-native, small-bitmap toolbar doesn't show unless it is explicitly + // managed, hence the use of sizers. In a real application, use larger icons for the main + // toolbar to avoid the need for this workaround. Or, use the toolbar in a container window + // as part of a more complex hierarchy, and the toolbar will automatically be non-native. + + wxSystemOptions::SetOption(wxT("mac.toolbar.no-native"), 1); + + wxToolBar* toolBar = new wxToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, + wxNO_BORDER|wxTB_FLAT|wxTB_NODIVIDER|wxTB_NOALIGN); + + sizer->Add(toolBar, 0, wxEXPAND); toolBar->AddTool(wxID_OPEN, wxEmptyString, wxBitmap(open_xpm), _("Open")); toolBar->AddTool(wxID_SAVEAS, wxEmptyString, wxBitmap(save_xpm), _("Save")); @@ -723,24 +868,29 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxEmptyString, wxBitmap(indentmore_xpm), _("Indent More")); toolBar->AddSeparator(); toolBar->AddTool(ID_FORMAT_FONT, wxEmptyString, wxBitmap(font_xpm), _("Font")); - toolBar->AddTool(ID_FORMAT_IMAGE, wxString("Im"), wxBitmap(font_xpm), _("Image Property")); + toolBar->AddSeparator(); - wxRichTextStyleComboCtrl* combo = new wxRichTextStyleComboCtrl(toolBar, ID_RICHTEXT_STYLE_COMBO, wxDefaultPosition, wxSize(200, -1)); + wxRichTextStyleComboCtrl* combo = new wxRichTextStyleComboCtrl(toolBar, ID_RICHTEXT_STYLE_COMBO, wxDefaultPosition, wxSize(160, -1), wxCB_READONLY); toolBar->AddControl(combo); toolBar->Realize(); - wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, GetClientSize(), wxSP_LIVE_UPDATE); + wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxSize(100,100), wxSP_LIVE_UPDATE); + sizer->Add(splitter, 1, wxEXPAND); wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL); wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD); wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL); - m_richTextCtrl = new wxRichTextCtrl(splitter, ID_RICHTEXT_CTRL, wxEmptyString, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxWANTS_CHARS); + m_richTextCtrl = new MyRichTextCtrl(splitter, ID_RICHTEXT_CTRL, wxEmptyString, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxWANTS_CHARS); + wxASSERT(!m_richTextCtrl->GetBuffer().GetAttributes().HasFontPixelSize()); + wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL); m_richTextCtrl->SetFont(font); + wxASSERT(!m_richTextCtrl->GetBuffer().GetAttributes().HasFontPixelSize()); + m_richTextCtrl->SetMargins(10, 10); m_richTextCtrl->SetStyleSheet(wxGetApp().GetStyleSheet()); @@ -761,6 +911,8 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, splitter->SplitVertically(m_richTextCtrl, styleListCtrl, 500); } + Layout(); + splitter->UpdateSize(); styleListCtrl->SetStyleSheet(wxGetApp().GetStyleSheet()); @@ -773,14 +925,14 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos, // Write text void MyFrame::WriteInitialText() { - wxRichTextCtrl& r = *m_richTextCtrl; + MyRichTextCtrl& r = *m_richTextCtrl; r.SetDefaultStyle(wxRichTextAttr()); - r.BeginSuppressUndo(); - r.Freeze(); + r.BeginSuppressUndo(); + r.BeginParagraphSpacing(0, 20); r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE); @@ -807,6 +959,13 @@ void MyFrame::WriteInitialText() r.EndAlignment(); +#if 0 + r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE); + r.WriteText(wxString(wxT("This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side."))); + r.Newline(); + r.EndAlignment(); +#endif + r.BeginAlignment(wxTEXT_ALIGNMENT_LEFT); wxRichTextAttr imageAttr; imageAttr.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_LEFT); @@ -975,8 +1134,8 @@ void MyFrame::WriteInitialText() r.WriteText(wxT("Note: this sample content was generated programmatically from within the MyFrame constructor in the demo. The images were loaded from inline XPMs. Enjoy wxRichTextCtrl!\n")); r.EndParagraphSpacing(); -#if 1 +#if 1 { // Add a text box @@ -1038,9 +1197,43 @@ void MyFrame::WriteInitialText() } #endif - r.Thaw(); + r.Newline(); + wxRichTextProperties properties; + r.WriteText(wxT("This is a rectangle field: ")); + r.WriteField(wxT("rectangle"), properties); + r.WriteText(wxT(" and a begin section field: ")); + r.WriteField(wxT("begin-section"), properties); + r.WriteText(wxT("This is text between the two tags.")); + r.WriteField(wxT("end-section"), properties); + r.WriteText(wxT(" Now a bitmap. ")); + r.WriteField(wxT("bitmap"), properties); + r.WriteText(wxT(" Before we go, here's a composite field: ***")); + wxRichTextField* field = r.WriteField(wxT("composite"), properties); + field->UpdateField(& r.GetBuffer()); // Creates the composite value (sort of a text box) + r.WriteText(wxT("*** End of composite field.")); + + r.Newline(); r.EndSuppressUndo(); + + // Add some locked content first - needs Undo to be enabled + { + r.BeginLock(); + r.WriteText(wxString(wxT("This is a locked object."))); + r.EndLock(); + + r.WriteText(wxString(wxT(" This is unlocked text. "))); + + r.BeginLock(); + r.WriteText(wxString(wxT("More locked content."))); + r.EndLock(); + r.Newline(); + + // Flush the Undo buffer + r.GetCommandProcessor()->ClearCommands(); + } + + r.Thaw(); } // event handlers @@ -1273,39 +1466,13 @@ void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event)) int pages = wxRICHTEXT_FORMAT_FONT; wxRichTextFormattingDialog formatDlg(pages, this); + formatDlg.SetOptions(wxRichTextFormattingDialog::Option_AllowPixelFontSize); formatDlg.GetStyle(m_richTextCtrl, range); if (formatDlg.ShowModal() == wxID_OK) { formatDlg.ApplyStyle(m_richTextCtrl, range, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); } - - // Old method using wxFontDialog -#if 0 - if (!m_richTextCtrl->HasSelection()) - return; - - wxRichTextRange range = m_richTextCtrl->GetSelectionRange(); - wxFontData fontData; - - wxRichTextAttr attr; - attr.SetFlags(wxTEXT_ATTR_FONT); - - if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr)) - fontData.SetInitialFont(attr.GetFont()); - - wxFontDialog dialog(this, fontData); - if (dialog.ShowModal() == wxID_OK) - { - fontData = dialog.GetFontData(); - attr.SetFlags(wxTEXT_ATTR_FONT); - attr.SetFont(fontData.GetChosenFont()); - if (attr.GetFont().IsOk()) - { - m_richTextCtrl->SetStyle(range, attr); - } - } -#endif } void MyFrame::OnImage(wxCommandEvent& WXUNUSED(event)) @@ -1807,3 +1974,131 @@ void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event)) // 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); +}