]> git.saurik.com Git - wxWidgets.git/commitdiff
Added wxRichTextXMLHandler::RegisterNodeName so custom content classes can be added...
authorJulian Smart <julian@anthemion.co.uk>
Thu, 10 May 2012 11:59:59 +0000 (11:59 +0000)
committerJulian Smart <julian@anthemion.co.uk>
Thu, 10 May 2012 11:59:59 +0000 (11:59 +0000)
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
interface/wx/richtext/richtextxml.h
src/richtext/richtextbuffer.cpp
src/richtext/richtextxml.cpp

index 44a933cb48caa789508222166f5d5f82cca9c372..14ce5236d821a9b47fccbd097fea1424427fc22a 100644 (file)
@@ -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
index 8f4b262f7d0039d29eefb67b66d9dd7b9ad59bcd..b5a838c6ea2506b364b5a10944f2de799f623d9d 100644 (file)
@@ -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:
 
     /**
index 663336f03006e8a40b66fbd8d3002c7b23f877c4..fce3cffe9d01cc2df0bb9d762ee74f9a96a19bf8 100644 (file)
@@ -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();
index 2d8acafb8274e8909b81ae95d5795f57abb99403..fa3ee081c336a4e3fab94bd098cf88975728862e 100644 (file)
@@ -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