wxRTC: extracted XML utilities into a separate class for potential reuse.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74749 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -571,6 +571,7 @@ All (GUI):
|
|||||||
- Fix crash in wxHTML on mal-formed <area> elements (LukasK).
|
- Fix crash in wxHTML on mal-formed <area> elements (LukasK).
|
||||||
- Set correct cursor when the mouse is over image map links in wxHTML (LukasK).
|
- Set correct cursor when the mouse is over image map links in wxHTML (LukasK).
|
||||||
- Add wxPropertyGridPageState::GetColumnFullWidth() (Teodor Petrov).
|
- Add wxPropertyGridPageState::GetColumnFullWidth() (Teodor Petrov).
|
||||||
|
- wxRTC: extracted XML utilities into a separate class for potential reuse.
|
||||||
|
|
||||||
wxGTK:
|
wxGTK:
|
||||||
|
|
||||||
|
@@ -22,7 +22,147 @@
|
|||||||
#if wxUSE_RICHTEXT && wxUSE_XML
|
#if wxUSE_RICHTEXT && wxUSE_XML
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* wxRichTextXMLHandler
|
@class wxRichTextXMLHelper
|
||||||
|
A utility class to help with XML import/export, that can be used outside
|
||||||
|
saving a buffer if needed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class wxRichTextXMLHelper: public wxObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
wxRichTextXMLHelper() { Init(); }
|
||||||
|
wxRichTextXMLHelper(const wxString& enc) { Init(); SetupForSaving(enc); }
|
||||||
|
~wxRichTextXMLHelper();
|
||||||
|
|
||||||
|
void Init();
|
||||||
|
|
||||||
|
void SetupForSaving(const wxString& enc);
|
||||||
|
|
||||||
|
void Clear();
|
||||||
|
|
||||||
|
void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
|
||||||
|
const wxString& GetFileEncoding() const { return m_fileEncoding; }
|
||||||
|
|
||||||
|
// Convert a colour to a 6-digit hex string
|
||||||
|
static wxString ColourToHexString(const wxColour& col);
|
||||||
|
|
||||||
|
// Convert 6-digit hex string to a colour
|
||||||
|
static wxColour HexStringToColour(const wxString& hex);
|
||||||
|
|
||||||
|
static wxString MakeString(const int& v) { return wxString::Format(wxT("%d"), v); }
|
||||||
|
static wxString MakeString(const long& v) { return wxString::Format(wxT("%ld"), v); }
|
||||||
|
static wxString MakeString(const double& v) { return wxString::Format(wxT("%.2f"), (float) v); }
|
||||||
|
static wxString MakeString(const wxString& s) { return s; }
|
||||||
|
static wxString MakeString(const wxColour& col) { return wxT("#") + ColourToHexString(col); }
|
||||||
|
|
||||||
|
static bool HasParam(wxXmlNode* node, const wxString& param);
|
||||||
|
static wxXmlNode *GetParamNode(wxXmlNode* node, const wxString& param);
|
||||||
|
static wxString GetNodeContent(wxXmlNode *node);
|
||||||
|
static wxString GetParamValue(wxXmlNode *node, const wxString& param);
|
||||||
|
static wxString GetText(wxXmlNode *node, const wxString& param = wxEmptyString);
|
||||||
|
static wxXmlNode* FindNode(wxXmlNode* node, const wxString& name);
|
||||||
|
|
||||||
|
static wxString AttributeToXML(const wxString& str);
|
||||||
|
|
||||||
|
static bool RichTextFixFaceName(wxString& facename);
|
||||||
|
static long ColourStringToLong(const wxString& colStr);
|
||||||
|
static wxTextAttrDimension ParseDimension(const wxString& dimStr);
|
||||||
|
|
||||||
|
// Make a string from the given property. This can be overridden for custom variants.
|
||||||
|
virtual wxString MakeStringFromProperty(const wxVariant& var);
|
||||||
|
|
||||||
|
// Create a proprty from the string read from the XML file.
|
||||||
|
virtual wxVariant MakePropertyFromString(const wxString& name, const wxString& value, const wxString& type);
|
||||||
|
|
||||||
|
// Import properties
|
||||||
|
virtual bool ImportProperties(wxRichTextProperties& properties, wxXmlNode* node);
|
||||||
|
|
||||||
|
virtual bool ImportStyle(wxRichTextAttr& attr, wxXmlNode* node, bool isPara = false);
|
||||||
|
virtual bool ImportStyleDefinition(wxRichTextStyleSheet* sheet, wxXmlNode* node);
|
||||||
|
|
||||||
|
// Get flags, as per handler flags
|
||||||
|
int GetFlags() const { return m_flags; }
|
||||||
|
void SetFlags(int flags) { m_flags = flags; }
|
||||||
|
|
||||||
|
#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
|
||||||
|
// write string to output
|
||||||
|
static void OutputString(wxOutputStream& stream, const wxString& str,
|
||||||
|
wxMBConv *convMem, wxMBConv *convFile);
|
||||||
|
|
||||||
|
static void OutputIndentation(wxOutputStream& stream, int indent);
|
||||||
|
|
||||||
|
// Same as above, but create entities first.
|
||||||
|
// Translates '<' to "<", '>' to ">" and '&' to "&"
|
||||||
|
static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
|
||||||
|
wxMBConv *convMem, wxMBConv *convFile);
|
||||||
|
|
||||||
|
void OutputString(wxOutputStream& stream, const wxString& str);
|
||||||
|
void OutputStringEnt(wxOutputStream& stream, const wxString& str);
|
||||||
|
|
||||||
|
static void AddString(wxString& str, const int& v) { str << wxString::Format(wxT("%d"), v); }
|
||||||
|
static void AddString(wxString& str, const long& v) { str << wxString::Format(wxT("%ld"), v); }
|
||||||
|
static void AddString(wxString& str, const double& v) { str << wxString::Format(wxT("%.2f"), (float) v); }
|
||||||
|
static void AddString(wxString& str, const wxChar* s) { str << s; }
|
||||||
|
static void AddString(wxString& str, const wxString& s) { str << s; }
|
||||||
|
static void AddString(wxString& str, const wxColour& col) { str << wxT("#") << ColourToHexString(col); }
|
||||||
|
|
||||||
|
static void AddAttribute(wxString& str, const wxString& name, const int& v);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& name, const long& v);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& name, const double& v);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& name, const wxChar* s);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& name, const wxString& s);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& name, const wxColour& col);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& name, const wxTextAttrDimension& dim);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& rootName, const wxTextAttrDimensions& dims);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& rootName, const wxTextAttrBorder& border);
|
||||||
|
static void AddAttribute(wxString& str, const wxString& rootName, const wxTextAttrBorders& borders);
|
||||||
|
|
||||||
|
/// Create a string containing style attributes
|
||||||
|
static wxString AddAttributes(const wxRichTextAttr& attr, bool isPara = false);
|
||||||
|
virtual bool ExportStyleDefinition(wxOutputStream& stream, wxRichTextStyleDefinition* def, int level);
|
||||||
|
|
||||||
|
virtual bool WriteProperties(wxOutputStream& stream, const wxRichTextProperties& properties, int level);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& name, const int& v);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& name, const long& v);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& name, const double& v);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& name, const wxString& s);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& name, const wxColour& col);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& name, const wxTextAttrDimension& dim);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& rootName, const wxTextAttrDimensions& dims);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& rootName, const wxTextAttrBorder& border);
|
||||||
|
static void AddAttribute(wxXmlNode* node, const wxString& rootName, const wxTextAttrBorders& borders);
|
||||||
|
|
||||||
|
static bool AddAttributes(wxXmlNode* node, wxRichTextAttr& attr, bool isPara = false);
|
||||||
|
|
||||||
|
virtual bool ExportStyleDefinition(wxXmlNode* parent, wxRichTextStyleDefinition* def);
|
||||||
|
|
||||||
|
// Write the properties
|
||||||
|
virtual bool WriteProperties(wxXmlNode* node, const wxRichTextProperties& properties);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
|
||||||
|
// Used during saving
|
||||||
|
wxMBConv* m_convMem;
|
||||||
|
wxMBConv* m_convFile;
|
||||||
|
bool m_deleteConvFile;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
wxString m_fileEncoding;
|
||||||
|
int m_flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@class wxRichTextXMLHandler
|
||||||
|
|
||||||
|
Implements XML loading and saving. Two methods of saving are included:
|
||||||
|
writing directly to a text stream, and populating an wxXmlDocument
|
||||||
|
before writing it. The former method is considerably faster, so we favour
|
||||||
|
that one, even though the code is a little less elegant.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class WXDLLIMPEXP_FWD_XML wxXmlNode;
|
class WXDLLIMPEXP_FWD_XML wxXmlNode;
|
||||||
@@ -43,36 +183,14 @@ public:
|
|||||||
#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
|
#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
|
||||||
/// Recursively export an object
|
/// Recursively export an object
|
||||||
bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
|
bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
|
||||||
bool ExportStyleDefinition(wxOutputStream& stream, wxRichTextStyleDefinition* def, int level);
|
|
||||||
wxString AddAttributes(const wxRichTextAttr& attr, bool isPara = false);
|
|
||||||
bool WriteProperties(wxOutputStream& stream, const wxRichTextProperties& properties, int level);
|
|
||||||
void OutputString(wxOutputStream& stream, const wxString& str);
|
|
||||||
void OutputStringEnt(wxOutputStream& stream, const wxString& str);
|
|
||||||
void OutputIndentation(wxOutputStream& stream, int indent);
|
|
||||||
static wxString AttributeToXML(const wxString& str);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
|
#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
|
||||||
bool ExportXML(wxXmlNode* parent, wxRichTextObject& obj);
|
bool ExportXML(wxXmlNode* parent, wxRichTextObject& obj);
|
||||||
bool ExportStyleDefinition(wxXmlNode* parent, wxRichTextStyleDefinition* def);
|
|
||||||
bool AddAttributes(wxXmlNode* node, wxRichTextAttr& attr, bool isPara = false);
|
|
||||||
bool WriteProperties(wxXmlNode* node, const wxRichTextProperties& properties);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Make a string from the given property. This can be overridden for custom variants.
|
|
||||||
virtual wxString MakeStringFromProperty(const wxVariant& var);
|
|
||||||
|
|
||||||
/// Create a proprty from the string read from the XML file.
|
|
||||||
virtual wxVariant MakePropertyFromString(const wxString& name, const wxString& value, const wxString& type);
|
|
||||||
|
|
||||||
/// Recursively import an object
|
/// Recursively import an object
|
||||||
bool ImportXML(wxRichTextBuffer* buffer, wxRichTextObject* obj, wxXmlNode* node);
|
bool ImportXML(wxRichTextBuffer* buffer, wxRichTextObject* obj, wxXmlNode* node);
|
||||||
bool ImportStyleDefinition(wxRichTextStyleSheet* sheet, wxXmlNode* node);
|
|
||||||
bool ImportProperties(wxRichTextObject* obj, wxXmlNode* node);
|
|
||||||
bool ImportProperties(wxRichTextProperties& properties, wxXmlNode* node);
|
|
||||||
|
|
||||||
/// Import style parameters
|
|
||||||
bool ImportStyle(wxRichTextAttr& attr, wxXmlNode* node, bool isPara = false);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Creates an object given an XML element name
|
/// Creates an object given an XML element name
|
||||||
@@ -84,19 +202,12 @@ public:
|
|||||||
/// Can we load using this handler?
|
/// Can we load using this handler?
|
||||||
virtual bool CanLoad() const { return true; }
|
virtual bool CanLoad() const { return true; }
|
||||||
|
|
||||||
// Used during saving
|
/// Returns the XML helper object, implementing functionality
|
||||||
wxMBConv* GetConvMem() const { return m_convMem; }
|
/// that can be reused elsewhere.
|
||||||
wxMBConv* GetConvFile() const { return m_convFile; }
|
wxRichTextXMLHelper& GetHelper() { return m_helper; }
|
||||||
|
|
||||||
// Implementation
|
// Implementation
|
||||||
|
|
||||||
bool HasParam(wxXmlNode* node, const wxString& param);
|
|
||||||
wxXmlNode *GetParamNode(wxXmlNode* node, const wxString& param);
|
|
||||||
wxString GetNodeContent(wxXmlNode *node);
|
|
||||||
wxString GetParamValue(wxXmlNode *node, const wxString& param);
|
|
||||||
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.
|
Call with XML node name, C++ class name so that wxRTC can read in the node.
|
||||||
If you add a custom object, call this.
|
If you add a custom object, call this.
|
||||||
@@ -114,11 +225,7 @@ protected:
|
|||||||
virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
|
virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
|
wxRichTextXMLHelper m_helper;
|
||||||
// Used during saving
|
|
||||||
wxMBConv* m_convMem;
|
|
||||||
wxMBConv* m_convFile;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static wxStringToStringHashMap sm_nodeNameToClassMap;
|
static wxStringToStringHashMap sm_nodeNameToClassMap;
|
||||||
};
|
};
|
||||||
|
@@ -53,33 +53,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
|
bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
|
||||||
|
|
||||||
/**
|
|
||||||
Helper function: gets node context.
|
|
||||||
*/
|
|
||||||
wxString GetNodeContent(wxXmlNode* node);
|
|
||||||
|
|
||||||
/**
|
|
||||||
Helper function: gets a named parameter from the XML node.
|
|
||||||
*/
|
|
||||||
wxXmlNode* GetParamNode(wxXmlNode* node, const wxString& param);
|
|
||||||
|
|
||||||
/**
|
|
||||||
Helper function: gets a named parameter from the XML node.
|
|
||||||
*/
|
|
||||||
wxString GetParamValue(wxXmlNode* node, const wxString& param);
|
|
||||||
|
|
||||||
/**
|
|
||||||
Helper function: gets text from the node.
|
|
||||||
*/
|
|
||||||
wxString GetText(wxXmlNode* node,
|
|
||||||
const wxString& param = wxEmptyString,
|
|
||||||
bool translate = false);
|
|
||||||
|
|
||||||
/**
|
|
||||||
Helper function: returns @true if the node has the given parameter.
|
|
||||||
*/
|
|
||||||
bool HasParam(wxXmlNode* node, const wxString& param);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Recursively imports an object.
|
Recursively imports an object.
|
||||||
*/
|
*/
|
||||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user