]> git.saurik.com Git - wxWidgets.git/blame - include/wx/richtext/richtextxml.h
Deallocate wxThreadSpecificInfo when wxThread ends.
[wxWidgets.git] / include / wx / richtext / richtextxml.h
CommitLineData
5d7836c4 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: wx/richtext/richtextxml.h
5d7836c4
JS
3// Purpose: XML and HTML I/O for wxRichTextCtrl
4// Author: Julian Smart
7fe8059f 5// Modified by:
5d7836c4 6// Created: 2005-09-30
5d7836c4
JS
7// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_RICHTEXTXML_H_
12#define _WX_RICHTEXTXML_H_
13
14/*!
15 * Includes
16 */
17
1aca9fcd 18#include "wx/hashmap.h"
b01ca8b6 19#include "wx/richtext/richtextbuffer.h"
d2d0adc7 20#include "wx/richtext/richtextstyles.h"
5d7836c4 21
fcdbeefa 22#if wxUSE_RICHTEXT && wxUSE_XML
5d7836c4 23
5d7836c4 24/*!
bd21f7ea
JS
25 @class wxRichTextXMLHelper
26 A utility class to help with XML import/export, that can be used outside
27 saving a buffer if needed.
28 */
29
41fbdced 30class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHelper: public wxObject
bd21f7ea
JS
31{
32public:
33 wxRichTextXMLHelper() { Init(); }
34 wxRichTextXMLHelper(const wxString& enc) { Init(); SetupForSaving(enc); }
35 ~wxRichTextXMLHelper();
36
37 void Init();
38
39 void SetupForSaving(const wxString& enc);
40
41 void Clear();
42
43 void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
44 const wxString& GetFileEncoding() const { return m_fileEncoding; }
45
46 // Convert a colour to a 6-digit hex string
47 static wxString ColourToHexString(const wxColour& col);
48
49 // Convert 6-digit hex string to a colour
50 static wxColour HexStringToColour(const wxString& hex);
51
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); }
57
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);
64
65 static wxString AttributeToXML(const wxString& str);
66
67 static bool RichTextFixFaceName(wxString& facename);
68 static long ColourStringToLong(const wxString& colStr);
69 static wxTextAttrDimension ParseDimension(const wxString& dimStr);
70
71 // Make a string from the given property. This can be overridden for custom variants.
72 virtual wxString MakeStringFromProperty(const wxVariant& var);
73
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);
76
77 // Import properties
78 virtual bool ImportProperties(wxRichTextProperties& properties, wxXmlNode* node);
79
80 virtual bool ImportStyle(wxRichTextAttr& attr, wxXmlNode* node, bool isPara = false);
81 virtual bool ImportStyleDefinition(wxRichTextStyleSheet* sheet, wxXmlNode* node);
82
83 // Get flags, as per handler flags
84 int GetFlags() const { return m_flags; }
85 void SetFlags(int flags) { m_flags = flags; }
86
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);
91
92 static void OutputIndentation(wxOutputStream& stream, int indent);
93
94 // Same as above, but create entities first.
95 // Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
96 static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
97 wxMBConv *convMem, wxMBConv *convFile);
98
99 void OutputString(wxOutputStream& stream, const wxString& str);
100 void OutputStringEnt(wxOutputStream& stream, const wxString& str);
101
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); }
108
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);
119
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);
123
124 virtual bool WriteProperties(wxOutputStream& stream, const wxRichTextProperties& properties, int level);
125#endif
126
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);
137
138 static bool AddAttributes(wxXmlNode* node, wxRichTextAttr& attr, bool isPara = false);
139
140 virtual bool ExportStyleDefinition(wxXmlNode* parent, wxRichTextStyleDefinition* def);
141
142 // Write the properties
143 virtual bool WriteProperties(wxXmlNode* node, const wxRichTextProperties& properties);
144#endif
145
146public:
147
148#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
149 // Used during saving
150 wxMBConv* m_convMem;
151 wxMBConv* m_convFile;
152 bool m_deleteConvFile;
153#endif
154
155 wxString m_fileEncoding;
156 int m_flags;
157};
158
159/*!
160 @class wxRichTextXMLHandler
161
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.
5d7836c4
JS
166 */
167
b5dbe15d 168class WXDLLIMPEXP_FWD_XML wxXmlNode;
bec80f4f 169class WXDLLIMPEXP_FWD_XML wxXmlDocument;
5d7836c4 170
3b2cb431 171class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHandler: public wxRichTextFileHandler
5d7836c4 172{
48dd0055 173 DECLARE_DYNAMIC_CLASS(wxRichTextXMLHandler)
5d7836c4
JS
174public:
175 wxRichTextXMLHandler(const wxString& name = wxT("XML"), const wxString& ext = wxT("xml"), int type = wxRICHTEXT_TYPE_XML)
176 : wxRichTextFileHandler(name, ext, type)
bec80f4f 177 { Init(); }
c6182d48 178
bec80f4f 179 void Init();
5d7836c4
JS
180
181#if wxUSE_STREAMS
bec80f4f
JS
182
183#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
5d7836c4 184 /// Recursively export an object
bec80f4f 185 bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
bec80f4f
JS
186#endif
187
188#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
189 bool ExportXML(wxXmlNode* parent, wxRichTextObject& obj);
bec80f4f
JS
190#endif
191
5d7836c4 192 /// Recursively import an object
bec80f4f 193 bool ImportXML(wxRichTextBuffer* buffer, wxRichTextObject* obj, wxXmlNode* node);
5d7836c4
JS
194#endif
195
bec80f4f
JS
196 /// Creates an object given an XML element name
197 virtual wxRichTextObject* CreateObjectForXMLName(wxRichTextObject* parent, const wxString& name) const;
c6182d48 198
5d7836c4
JS
199 /// Can we save using this handler?
200 virtual bool CanSave() const { return true; }
201
202 /// Can we load using this handler?
203 virtual bool CanLoad() const { return true; }
204
bd21f7ea
JS
205 /// Returns the XML helper object, implementing functionality
206 /// that can be reused elsewhere.
207 wxRichTextXMLHelper& GetHelper() { return m_helper; }
bec80f4f 208
5d7836c4
JS
209// Implementation
210
1aca9fcd
JS
211 /**
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.
214 */
215 static void RegisterNodeName(const wxString& nodeName, const wxString& className) { sm_nodeNameToClassMap[nodeName] = className; }
216
217 /**
218 Cleans up the mapping between node name and C++ class.
219 */
220 static void ClearNodeToClassMap() { sm_nodeNameToClassMap.clear(); }
221
5d7836c4 222protected:
6f02a879
VZ
223#if wxUSE_STREAMS
224 virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream);
225 virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
226#endif
bec80f4f 227
bd21f7ea 228 wxRichTextXMLHelper m_helper;
1aca9fcd
JS
229
230 static wxStringToStringHashMap sm_nodeNameToClassMap;
5d7836c4
JS
231};
232
5d7836c4 233#endif
fcdbeefa 234 // wxUSE_RICHTEXT && wxUSE_XML
5d7836c4
JS
235
236#endif
237 // _WX_RICHTEXTXML_H_