2005-10-19 08:52:47 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2011-03-19 20:14:35 -04:00
|
|
|
// Name: wx/richtext/richtextxml.h
|
2005-10-19 08:52:47 -04:00
|
|
|
// Purpose: XML and HTML I/O for wxRichTextCtrl
|
|
|
|
// Author: Julian Smart
|
2005-10-19 13:00:58 -04:00
|
|
|
// Modified by:
|
2005-10-19 08:52:47 -04:00
|
|
|
// Created: 2005-09-30
|
|
|
|
// Copyright: (c) Julian Smart
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_RICHTEXTXML_H_
|
|
|
|
#define _WX_RICHTEXTXML_H_
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Includes
|
|
|
|
*/
|
|
|
|
|
2012-05-10 07:59:59 -04:00
|
|
|
#include "wx/hashmap.h"
|
2005-10-28 10:24:04 -04:00
|
|
|
#include "wx/richtext/richtextbuffer.h"
|
2006-10-26 02:32:47 -04:00
|
|
|
#include "wx/richtext/richtextstyles.h"
|
2005-10-19 08:52:47 -04:00
|
|
|
|
2005-10-31 09:27:53 -05:00
|
|
|
#if wxUSE_RICHTEXT && wxUSE_XML
|
2005-10-19 08:52:47 -04:00
|
|
|
|
|
|
|
/*!
|
2013-09-03 12:47:09 -04:00
|
|
|
@class wxRichTextXMLHelper
|
|
|
|
A utility class to help with XML import/export, that can be used outside
|
|
|
|
saving a buffer if needed.
|
|
|
|
*/
|
|
|
|
|
2013-09-05 10:10:39 -04:00
|
|
|
class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHelper: public wxObject
|
2013-09-03 12:47:09 -04:00
|
|
|
{
|
|
|
|
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);
|
2013-11-10 10:56:37 -05:00
|
|
|
|
|
|
|
/// Create a string containing style attributes, plus further object 'attributes' (shown, id)
|
|
|
|
static wxString AddAttributes(wxRichTextObject* obj, bool isPara = false);
|
|
|
|
|
2013-09-03 12:47:09 -04:00
|
|
|
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);
|
2013-11-10 10:56:37 -05:00
|
|
|
static bool AddAttributes(wxXmlNode* node, wxRichTextObject* obj, bool isPara = false);
|
2013-09-03 12:47:09 -04:00
|
|
|
|
|
|
|
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.
|
2005-10-19 08:52:47 -04:00
|
|
|
*/
|
|
|
|
|
2007-07-09 06:09:52 -04:00
|
|
|
class WXDLLIMPEXP_FWD_XML wxXmlNode;
|
2010-10-06 16:22:03 -04:00
|
|
|
class WXDLLIMPEXP_FWD_XML wxXmlDocument;
|
2005-10-19 08:52:47 -04:00
|
|
|
|
2006-06-29 03:16:39 -04:00
|
|
|
class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHandler: public wxRichTextFileHandler
|
2005-10-19 08:52:47 -04:00
|
|
|
{
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_DYNAMIC_CLASS(wxRichTextXMLHandler);
|
2005-10-19 08:52:47 -04:00
|
|
|
public:
|
|
|
|
wxRichTextXMLHandler(const wxString& name = wxT("XML"), const wxString& ext = wxT("xml"), int type = wxRICHTEXT_TYPE_XML)
|
|
|
|
: wxRichTextFileHandler(name, ext, type)
|
2010-10-06 16:22:03 -04:00
|
|
|
{ Init(); }
|
2012-04-24 07:22:18 -04:00
|
|
|
|
2010-10-06 16:22:03 -04:00
|
|
|
void Init();
|
2005-10-19 08:52:47 -04:00
|
|
|
|
|
|
|
#if wxUSE_STREAMS
|
2010-10-06 16:22:03 -04:00
|
|
|
|
|
|
|
#if wxRICHTEXT_HAVE_DIRECT_OUTPUT
|
2005-10-19 08:52:47 -04:00
|
|
|
/// Recursively export an object
|
2010-10-06 16:22:03 -04:00
|
|
|
bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
|
|
|
|
bool ExportXML(wxXmlNode* parent, wxRichTextObject& obj);
|
|
|
|
#endif
|
|
|
|
|
2005-10-19 08:52:47 -04:00
|
|
|
/// Recursively import an object
|
2010-10-06 16:22:03 -04:00
|
|
|
bool ImportXML(wxRichTextBuffer* buffer, wxRichTextObject* obj, wxXmlNode* node);
|
2005-10-19 08:52:47 -04:00
|
|
|
#endif
|
|
|
|
|
2010-10-06 16:22:03 -04:00
|
|
|
/// Creates an object given an XML element name
|
|
|
|
virtual wxRichTextObject* CreateObjectForXMLName(wxRichTextObject* parent, const wxString& name) const;
|
2012-04-24 07:22:18 -04:00
|
|
|
|
2005-10-19 08:52:47 -04:00
|
|
|
/// Can we save using this handler?
|
2015-09-06 20:20:42 -04:00
|
|
|
virtual bool CanSave() const wxOVERRIDE { return true; }
|
2005-10-19 08:52:47 -04:00
|
|
|
|
|
|
|
/// Can we load using this handler?
|
2015-09-06 20:20:42 -04:00
|
|
|
virtual bool CanLoad() const wxOVERRIDE { return true; }
|
2005-10-19 08:52:47 -04:00
|
|
|
|
2013-09-03 12:47:09 -04:00
|
|
|
/// Returns the XML helper object, implementing functionality
|
|
|
|
/// that can be reused elsewhere.
|
|
|
|
wxRichTextXMLHelper& GetHelper() { return m_helper; }
|
2010-10-06 16:22:03 -04:00
|
|
|
|
2005-10-19 08:52:47 -04:00
|
|
|
// Implementation
|
|
|
|
|
2012-05-10 07:59:59 -04:00
|
|
|
/**
|
|
|
|
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(); }
|
|
|
|
|
2005-10-19 08:52:47 -04:00
|
|
|
protected:
|
2006-02-08 16:47:09 -05:00
|
|
|
#if wxUSE_STREAMS
|
2015-09-06 20:20:42 -04:00
|
|
|
virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream) wxOVERRIDE;
|
|
|
|
virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) wxOVERRIDE;
|
2006-02-08 16:47:09 -05:00
|
|
|
#endif
|
2010-10-06 16:22:03 -04:00
|
|
|
|
2013-09-03 12:47:09 -04:00
|
|
|
wxRichTextXMLHelper m_helper;
|
2012-05-10 07:59:59 -04:00
|
|
|
|
|
|
|
static wxStringToStringHashMap sm_nodeNameToClassMap;
|
2005-10-19 08:52:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2005-10-31 09:27:53 -05:00
|
|
|
// wxUSE_RICHTEXT && wxUSE_XML
|
2005-10-19 08:52:47 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
// _WX_RICHTEXTXML_H_
|