1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/richtext/richtextxml.h
3 // Purpose: XML and HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_RICHTEXTXML_H_
12 #define _WX_RICHTEXTXML_H_
18 #include "wx/hashmap.h"
19 #include "wx/richtext/richtextbuffer.h"
20 #include "wx/richtext/richtextstyles.h"
22 #if wxUSE_RICHTEXT && wxUSE_XML
25 @class wxRichTextXMLHelper
26 A utility class to help with XML import/export, that can be used outside
27 saving a buffer if needed.
30 class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHelper
: public wxObject
33 wxRichTextXMLHelper() { Init(); }
34 wxRichTextXMLHelper(const wxString
& enc
) { Init(); SetupForSaving(enc
); }
35 ~wxRichTextXMLHelper();
39 void SetupForSaving(const wxString
& enc
);
43 void SetFileEncoding(const wxString
& encoding
) { m_fileEncoding
= encoding
; }
44 const wxString
& GetFileEncoding() const { return m_fileEncoding
; }
46 // Convert a colour to a 6-digit hex string
47 static wxString
ColourToHexString(const wxColour
& col
);
49 // Convert 6-digit hex string to a colour
50 static wxColour
HexStringToColour(const wxString
& hex
);
52 static wxString
MakeString(const int& v
) { return wxString::Format(wxT("%d"), v
); }
53 static wxString
MakeString(const long& v
) { return wxString::Format(wxT("%ld"), v
); }
54 static wxString
MakeString(const double& v
) { return wxString::Format(wxT("%.2f"), (float) v
); }
55 static wxString
MakeString(const wxString
& s
) { return s
; }
56 static wxString
MakeString(const wxColour
& col
) { return wxT("#") + ColourToHexString(col
); }
58 static bool HasParam(wxXmlNode
* node
, const wxString
& param
);
59 static wxXmlNode
*GetParamNode(wxXmlNode
* node
, const wxString
& param
);
60 static wxString
GetNodeContent(wxXmlNode
*node
);
61 static wxString
GetParamValue(wxXmlNode
*node
, const wxString
& param
);
62 static wxString
GetText(wxXmlNode
*node
, const wxString
& param
= wxEmptyString
);
63 static wxXmlNode
* FindNode(wxXmlNode
* node
, const wxString
& name
);
65 static wxString
AttributeToXML(const wxString
& str
);
67 static bool RichTextFixFaceName(wxString
& facename
);
68 static long ColourStringToLong(const wxString
& colStr
);
69 static wxTextAttrDimension
ParseDimension(const wxString
& dimStr
);
71 // Make a string from the given property. This can be overridden for custom variants.
72 virtual wxString
MakeStringFromProperty(const wxVariant
& var
);
74 // Create a proprty from the string read from the XML file.
75 virtual wxVariant
MakePropertyFromString(const wxString
& name
, const wxString
& value
, const wxString
& type
);
78 virtual bool ImportProperties(wxRichTextProperties
& properties
, wxXmlNode
* node
);
80 virtual bool ImportStyle(wxRichTextAttr
& attr
, wxXmlNode
* node
, bool isPara
= false);
81 virtual bool ImportStyleDefinition(wxRichTextStyleSheet
* sheet
, wxXmlNode
* node
);
83 // Get flags, as per handler flags
84 int GetFlags() const { return m_flags
; }
85 void SetFlags(int flags
) { m_flags
= flags
; }
87 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
88 // write string to output
89 static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
90 wxMBConv
*convMem
, wxMBConv
*convFile
);
92 static void OutputIndentation(wxOutputStream
& stream
, int indent
);
94 // Same as above, but create entities first.
95 // Translates '<' to "<", '>' to ">" and '&' to "&"
96 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
97 wxMBConv
*convMem
, wxMBConv
*convFile
);
99 void OutputString(wxOutputStream
& stream
, const wxString
& str
);
100 void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
);
102 static void AddString(wxString
& str
, const int& v
) { str
<< wxString::Format(wxT("%d"), v
); }
103 static void AddString(wxString
& str
, const long& v
) { str
<< wxString::Format(wxT("%ld"), v
); }
104 static void AddString(wxString
& str
, const double& v
) { str
<< wxString::Format(wxT("%.2f"), (float) v
); }
105 static void AddString(wxString
& str
, const wxChar
* s
) { str
<< s
; }
106 static void AddString(wxString
& str
, const wxString
& s
) { str
<< s
; }
107 static void AddString(wxString
& str
, const wxColour
& col
) { str
<< wxT("#") << ColourToHexString(col
); }
109 static void AddAttribute(wxString
& str
, const wxString
& name
, const int& v
);
110 static void AddAttribute(wxString
& str
, const wxString
& name
, const long& v
);
111 static void AddAttribute(wxString
& str
, const wxString
& name
, const double& v
);
112 static void AddAttribute(wxString
& str
, const wxString
& name
, const wxChar
* s
);
113 static void AddAttribute(wxString
& str
, const wxString
& name
, const wxString
& s
);
114 static void AddAttribute(wxString
& str
, const wxString
& name
, const wxColour
& col
);
115 static void AddAttribute(wxString
& str
, const wxString
& name
, const wxTextAttrDimension
& dim
);
116 static void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
);
117 static void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorder
& border
);
118 static void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorders
& borders
);
120 /// Create a string containing style attributes
121 static wxString
AddAttributes(const wxRichTextAttr
& attr
, bool isPara
= false);
122 virtual bool ExportStyleDefinition(wxOutputStream
& stream
, wxRichTextStyleDefinition
* def
, int level
);
124 virtual bool WriteProperties(wxOutputStream
& stream
, const wxRichTextProperties
& properties
, int level
);
127 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
128 static void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const int& v
);
129 static void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const long& v
);
130 static void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const double& v
);
131 static void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxString
& s
);
132 static void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxColour
& col
);
133 static void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxTextAttrDimension
& dim
);
134 static void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
);
135 static void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorder
& border
);
136 static void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorders
& borders
);
138 static bool AddAttributes(wxXmlNode
* node
, wxRichTextAttr
& attr
, bool isPara
= false);
140 virtual bool ExportStyleDefinition(wxXmlNode
* parent
, wxRichTextStyleDefinition
* def
);
142 // Write the properties
143 virtual bool WriteProperties(wxXmlNode
* node
, const wxRichTextProperties
& properties
);
148 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
149 // Used during saving
151 wxMBConv
* m_convFile
;
152 bool m_deleteConvFile
;
155 wxString m_fileEncoding
;
160 @class wxRichTextXMLHandler
162 Implements XML loading and saving. Two methods of saving are included:
163 writing directly to a text stream, and populating an wxXmlDocument
164 before writing it. The former method is considerably faster, so we favour
165 that one, even though the code is a little less elegant.
168 class WXDLLIMPEXP_FWD_XML wxXmlNode
;
169 class WXDLLIMPEXP_FWD_XML wxXmlDocument
;
171 class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHandler
: public wxRichTextFileHandler
173 DECLARE_DYNAMIC_CLASS(wxRichTextXMLHandler
)
175 wxRichTextXMLHandler(const wxString
& name
= wxT("XML"), const wxString
& ext
= wxT("xml"), int type
= wxRICHTEXT_TYPE_XML
)
176 : wxRichTextFileHandler(name
, ext
, type
)
183 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
184 /// Recursively export an object
185 bool ExportXML(wxOutputStream
& stream
, wxRichTextObject
& obj
, int level
);
188 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
189 bool ExportXML(wxXmlNode
* parent
, wxRichTextObject
& obj
);
192 /// Recursively import an object
193 bool ImportXML(wxRichTextBuffer
* buffer
, wxRichTextObject
* obj
, wxXmlNode
* node
);
196 /// Creates an object given an XML element name
197 virtual wxRichTextObject
* CreateObjectForXMLName(wxRichTextObject
* parent
, const wxString
& name
) const;
199 /// Can we save using this handler?
200 virtual bool CanSave() const { return true; }
202 /// Can we load using this handler?
203 virtual bool CanLoad() const { return true; }
205 /// Returns the XML helper object, implementing functionality
206 /// that can be reused elsewhere.
207 wxRichTextXMLHelper
& GetHelper() { return m_helper
; }
212 Call with XML node name, C++ class name so that wxRTC can read in the node.
213 If you add a custom object, call this.
215 static void RegisterNodeName(const wxString
& nodeName
, const wxString
& className
) { sm_nodeNameToClassMap
[nodeName
] = className
; }
218 Cleans up the mapping between node name and C++ class.
220 static void ClearNodeToClassMap() { sm_nodeNameToClassMap
.clear(); }
224 virtual bool DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
);
225 virtual bool DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
);
228 wxRichTextXMLHelper m_helper
;
230 static wxStringToStringHashMap sm_nodeNameToClassMap
;
234 // wxUSE_RICHTEXT && wxUSE_XML
237 // _WX_RICHTEXTXML_H_