]> git.saurik.com Git - wxWidgets.git/blob - include/wx/richtext/richtextxml.h
14ce5236d821a9b47fccbd097fea1424427fc22a
[wxWidgets.git] / include / wx / richtext / richtextxml.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/richtext/richtextxml.h
3 // Purpose: XML and HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-09-30
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_RICHTEXTXML_H_
13 #define _WX_RICHTEXTXML_H_
14
15 /*!
16 * Includes
17 */
18
19 #include "wx/hashmap.h"
20 #include "wx/richtext/richtextbuffer.h"
21 #include "wx/richtext/richtextstyles.h"
22
23 #if wxUSE_RICHTEXT && wxUSE_XML
24
25 /*!
26 * wxRichTextXMLHandler
27 */
28
29 class WXDLLIMPEXP_FWD_XML wxXmlNode;
30 class WXDLLIMPEXP_FWD_XML wxXmlDocument;
31
32 class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHandler: public wxRichTextFileHandler
33 {
34 DECLARE_DYNAMIC_CLASS(wxRichTextXMLHandler)
35 public:
36 wxRichTextXMLHandler(const wxString& name = wxT("XML"), const wxString& ext = wxT("xml"), int type = wxRICHTEXT_TYPE_XML)
37 : wxRichTextFileHandler(name, ext, type)
38 { Init(); }
39
40 void Init();
41
42 #if wxUSE_STREAMS
43
44 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
45 /// Recursively export an object
46 bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
47 bool ExportStyleDefinition(wxOutputStream& stream, wxRichTextStyleDefinition* def, int level);
48 wxString AddAttributes(const wxRichTextAttr& attr, bool isPara = false);
49 bool WriteProperties(wxOutputStream& stream, const wxRichTextProperties& properties, int level);
50 void OutputString(wxOutputStream& stream, const wxString& str);
51 void OutputStringEnt(wxOutputStream& stream, const wxString& str);
52 void OutputIndentation(wxOutputStream& stream, int indent);
53 static wxString AttributeToXML(const wxString& str);
54 #endif
55
56 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
57 bool ExportXML(wxXmlNode* parent, wxRichTextObject& obj);
58 bool ExportStyleDefinition(wxXmlNode* parent, wxRichTextStyleDefinition* def);
59 bool AddAttributes(wxXmlNode* node, wxRichTextAttr& attr, bool isPara = false);
60 bool WriteProperties(wxXmlNode* node, const wxRichTextProperties& properties);
61 #endif
62
63 /// Make a string from the given property. This can be overridden for custom variants.
64 virtual wxString MakeStringFromProperty(const wxVariant& var);
65
66 /// Create a proprty from the string read from the XML file.
67 virtual wxVariant MakePropertyFromString(const wxString& name, const wxString& value, const wxString& type);
68
69 /// Recursively import an object
70 bool ImportXML(wxRichTextBuffer* buffer, wxRichTextObject* obj, wxXmlNode* node);
71 bool ImportStyleDefinition(wxRichTextStyleSheet* sheet, wxXmlNode* node);
72 bool ImportProperties(wxRichTextObject* obj, wxXmlNode* node);
73 bool ImportProperties(wxRichTextProperties& properties, wxXmlNode* node);
74
75 /// Import style parameters
76 bool ImportStyle(wxRichTextAttr& attr, wxXmlNode* node, bool isPara = false);
77 #endif
78
79 /// Creates an object given an XML element name
80 virtual wxRichTextObject* CreateObjectForXMLName(wxRichTextObject* parent, const wxString& name) const;
81
82 /// Can we save using this handler?
83 virtual bool CanSave() const { return true; }
84
85 /// Can we load using this handler?
86 virtual bool CanLoad() const { return true; }
87
88 // Used during saving
89 wxMBConv* GetConvMem() const { return m_convMem; }
90 wxMBConv* GetConvFile() const { return m_convFile; }
91
92 // Implementation
93
94 bool HasParam(wxXmlNode* node, const wxString& param);
95 wxXmlNode *GetParamNode(wxXmlNode* node, const wxString& param);
96 wxString GetNodeContent(wxXmlNode *node);
97 wxString GetParamValue(wxXmlNode *node, const wxString& param);
98 wxString GetText(wxXmlNode *node, const wxString& param = wxEmptyString, bool translate = false);
99 static wxXmlNode* FindNode(wxXmlNode* node, const wxString& name);
100
101 /**
102 Call with XML node name, C++ class name so that wxRTC can read in the node.
103 If you add a custom object, call this.
104 */
105 static void RegisterNodeName(const wxString& nodeName, const wxString& className) { sm_nodeNameToClassMap[nodeName] = className; }
106
107 /**
108 Cleans up the mapping between node name and C++ class.
109 */
110 static void ClearNodeToClassMap() { sm_nodeNameToClassMap.clear(); }
111
112 protected:
113 #if wxUSE_STREAMS
114 virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream);
115 virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
116 #endif
117
118 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
119 // Used during saving
120 wxMBConv* m_convMem;
121 wxMBConv* m_convFile;
122 #endif
123
124 static wxStringToStringHashMap sm_nodeNameToClassMap;
125 };
126
127 #endif
128 // wxUSE_RICHTEXT && wxUSE_XML
129
130 #endif
131 // _WX_RICHTEXTXML_H_