From 1aca9fcdfc51f165210a9976f28afb192bfb252f Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Thu, 10 May 2012 11:59:59 +0000 Subject: [PATCH] Added wxRichTextXMLHandler::RegisterNodeName so custom content classes can be added without breaking XML loading git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71399 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/richtext/richtextxml.h | 14 ++++++++++++++ interface/wx/richtext/richtextxml.h | 13 ++++++++++++- src/richtext/richtextbuffer.cpp | 13 +++++++++++++ src/richtext/richtextxml.cpp | 24 +++++++----------------- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/include/wx/richtext/richtextxml.h b/include/wx/richtext/richtextxml.h index 44a933c..14ce523 100644 --- a/include/wx/richtext/richtextxml.h +++ b/include/wx/richtext/richtextxml.h @@ -16,6 +16,7 @@ * Includes */ +#include "wx/hashmap.h" #include "wx/richtext/richtextbuffer.h" #include "wx/richtext/richtextstyles.h" @@ -97,6 +98,17 @@ public: wxString GetText(wxXmlNode *node, const wxString& param = wxEmptyString, bool translate = false); static wxXmlNode* FindNode(wxXmlNode* node, const wxString& name); + /** + Call with XML node name, C++ class name so that wxRTC can read in the node. + If you add a custom object, call this. + */ + static void RegisterNodeName(const wxString& nodeName, const wxString& className) { sm_nodeNameToClassMap[nodeName] = className; } + + /** + Cleans up the mapping between node name and C++ class. + */ + static void ClearNodeToClassMap() { sm_nodeNameToClassMap.clear(); } + protected: #if wxUSE_STREAMS virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream); @@ -108,6 +120,8 @@ protected: wxMBConv* m_convMem; wxMBConv* m_convFile; #endif + + static wxStringToStringHashMap sm_nodeNameToClassMap; }; #endif diff --git a/interface/wx/richtext/richtextxml.h b/interface/wx/richtext/richtextxml.h index 8f4b262..b5a838c6 100644 --- a/interface/wx/richtext/richtextxml.h +++ b/interface/wx/richtext/richtextxml.h @@ -53,7 +53,7 @@ public: Recursively exports an object to the stream. */ bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level); - + /** Helper function: gets node context. */ @@ -86,6 +86,17 @@ public: */ bool ImportXML(wxRichTextBuffer* buffer, wxRichTextObject* obj, wxXmlNode* node); + /** + Call with XML node name, C++ class name so that wxRTC can read in the node. + If you add a custom object, call this. + */ + static void RegisterNodeName(const wxString& nodeName, const wxString& className) { sm_nodeNameToClassMap[nodeName] = className; } + + /** + Cleans up the mapping between node name and C++ class. + */ + static void ClearNodeToClassMap() { sm_nodeNameToClassMap.clear(); } + protected: /** diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index 663336f..fce3cff 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -42,6 +42,7 @@ #include "wx/richtext/richtextstyles.h" #include "wx/richtext/richtextimagedlg.h" #include "wx/richtext/richtextsizepage.h" +#include "wx/richtext/richtextxml.h" #include "wx/listimpl.cpp" #include "wx/arrimpl.cpp" @@ -9855,6 +9856,17 @@ public: wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer); wxRichTextBuffer::InitStandardHandlers(); wxRichTextParagraph::InitDefaultTabs(); + + wxRichTextXMLHandler::RegisterNodeName(wxT("text"), wxT("wxRichTextPlainText")); + wxRichTextXMLHandler::RegisterNodeName(wxT("symbol"), wxT("wxRichTextPlainText")); + wxRichTextXMLHandler::RegisterNodeName(wxT("image"), wxT("wxRichTextImage")); + wxRichTextXMLHandler::RegisterNodeName(wxT("paragraph"), wxT("wxRichTextParagraph")); + wxRichTextXMLHandler::RegisterNodeName(wxT("paragraphlayout"), wxT("wxRichTextParagraphLayoutBox")); + wxRichTextXMLHandler::RegisterNodeName(wxT("textbox"), wxT("wxRichTextBox")); + wxRichTextXMLHandler::RegisterNodeName(wxT("cell"), wxT("wxRichTextCell")); + wxRichTextXMLHandler::RegisterNodeName(wxT("table"), wxT("wxRichTextTable")); + wxRichTextXMLHandler::RegisterNodeName(wxT("field"), wxT("wxRichTextField")); + return true; } void OnExit() @@ -9862,6 +9874,7 @@ public: wxRichTextBuffer::CleanUpHandlers(); wxRichTextBuffer::CleanUpDrawingHandlers(); wxRichTextBuffer::CleanUpFieldTypes(); + wxRichTextXMLHandler::ClearNodeToClassMap(); wxRichTextDecimalToRoman(-1); wxRichTextParagraph::ClearDefaultTabs(); wxRichTextCtrl::ClearAvailableFontNames(); diff --git a/src/richtext/richtextxml.cpp b/src/richtext/richtextxml.cpp index 2d8acaf..fa3ee08 100644 --- a/src/richtext/richtextxml.cpp +++ b/src/richtext/richtextxml.cpp @@ -89,6 +89,8 @@ static inline void AddString(wxString& str, const wxColour& col) { str << wxT("# IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler, wxRichTextFileHandler) +wxStringToStringHashMap wxRichTextXMLHandler::sm_nodeNameToClassMap; + void wxRichTextXMLHandler::Init() { #if wxRICHTEXT_HAVE_DIRECT_OUTPUT @@ -158,24 +160,12 @@ bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& s /// Creates an object given an XML element name wxRichTextObject* wxRichTextXMLHandler::CreateObjectForXMLName(wxRichTextObject* WXUNUSED(parent), const wxString& name) const { - if (name == wxT("text") || name == wxT("symbol")) - return new wxRichTextPlainText; - else if (name == wxT("image")) - return new wxRichTextImage; - else if (name == wxT("paragraph")) - return new wxRichTextParagraph; - else if (name == wxT("paragraphlayout")) - return new wxRichTextParagraphLayoutBox; - else if (name == wxT("textbox")) - return new wxRichTextBox; - else if (name == wxT("cell")) - return new wxRichTextCell; - else if (name == wxT("table")) - return new wxRichTextTable; - else if (name == wxT("field")) - return new wxRichTextField; - else + // The standard node to class mappings are added in wxRichTextModule::OnInit in richtextbuffer.cpp + wxStringToStringHashMap::const_iterator it = sm_nodeNameToClassMap.find(name); + if (it == sm_nodeNameToClassMap.end()) return NULL; + else + return wxDynamicCast(wxCreateDynamicObject(it->second), wxRichTextObject); } /// Recursively import an object -- 2.7.4