richtextxml.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_RICHTEXTXML_H_
  11. #define _WX_RICHTEXTXML_H_
  12. /*!
  13. * Includes
  14. */
  15. #include "wx/hashmap.h"
  16. #include "wx/richtext/richtextbuffer.h"
  17. #include "wx/richtext/richtextstyles.h"
  18. #if wxUSE_RICHTEXT && wxUSE_XML
  19. /*!
  20. @class wxRichTextXMLHelper
  21. A utility class to help with XML import/export, that can be used outside
  22. saving a buffer if needed.
  23. */
  24. class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHelper: public wxObject
  25. {
  26. public:
  27. wxRichTextXMLHelper() { Init(); }
  28. wxRichTextXMLHelper(const wxString& enc) { Init(); SetupForSaving(enc); }
  29. ~wxRichTextXMLHelper();
  30. void Init();
  31. void SetupForSaving(const wxString& enc);
  32. void Clear();
  33. void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
  34. const wxString& GetFileEncoding() const { return m_fileEncoding; }
  35. // Convert a colour to a 6-digit hex string
  36. static wxString ColourToHexString(const wxColour& col);
  37. // Convert 6-digit hex string to a colour
  38. static wxColour HexStringToColour(const wxString& hex);
  39. static wxString MakeString(const int& v) { return wxString::Format(wxT("%d"), v); }
  40. static wxString MakeString(const long& v) { return wxString::Format(wxT("%ld"), v); }
  41. static wxString MakeString(const double& v) { return wxString::Format(wxT("%.2f"), (float) v); }
  42. static wxString MakeString(const wxString& s) { return s; }
  43. static wxString MakeString(const wxColour& col) { return wxT("#") + ColourToHexString(col); }
  44. static bool HasParam(wxXmlNode* node, const wxString& param);
  45. static wxXmlNode *GetParamNode(wxXmlNode* node, const wxString& param);
  46. static wxString GetNodeContent(wxXmlNode *node);
  47. static wxString GetParamValue(wxXmlNode *node, const wxString& param);
  48. static wxString GetText(wxXmlNode *node, const wxString& param = wxEmptyString);
  49. static wxXmlNode* FindNode(wxXmlNode* node, const wxString& name);
  50. static wxString AttributeToXML(const wxString& str);
  51. static bool RichTextFixFaceName(wxString& facename);
  52. static long ColourStringToLong(const wxString& colStr);
  53. static wxTextAttrDimension ParseDimension(const wxString& dimStr);
  54. // Make a string from the given property. This can be overridden for custom variants.
  55. virtual wxString MakeStringFromProperty(const wxVariant& var);
  56. // Create a proprty from the string read from the XML file.
  57. virtual wxVariant MakePropertyFromString(const wxString& name, const wxString& value, const wxString& type);
  58. // Import properties
  59. virtual bool ImportProperties(wxRichTextProperties& properties, wxXmlNode* node);
  60. virtual bool ImportStyle(wxRichTextAttr& attr, wxXmlNode* node, bool isPara = false);
  61. virtual bool ImportStyleDefinition(wxRichTextStyleSheet* sheet, wxXmlNode* node);
  62. // Get flags, as per handler flags
  63. int GetFlags() const { return m_flags; }
  64. void SetFlags(int flags) { m_flags = flags; }
  65. #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
  66. // write string to output
  67. static void OutputString(wxOutputStream& stream, const wxString& str,
  68. wxMBConv *convMem, wxMBConv *convFile);
  69. static void OutputIndentation(wxOutputStream& stream, int indent);
  70. // Same as above, but create entities first.
  71. // Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
  72. static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
  73. wxMBConv *convMem, wxMBConv *convFile);
  74. void OutputString(wxOutputStream& stream, const wxString& str);
  75. void OutputStringEnt(wxOutputStream& stream, const wxString& str);
  76. static void AddString(wxString& str, const int& v) { str << wxString::Format(wxT("%d"), v); }
  77. static void AddString(wxString& str, const long& v) { str << wxString::Format(wxT("%ld"), v); }
  78. static void AddString(wxString& str, const double& v) { str << wxString::Format(wxT("%.2f"), (float) v); }
  79. static void AddString(wxString& str, const wxChar* s) { str << s; }
  80. static void AddString(wxString& str, const wxString& s) { str << s; }
  81. static void AddString(wxString& str, const wxColour& col) { str << wxT("#") << ColourToHexString(col); }
  82. static void AddAttribute(wxString& str, const wxString& name, const int& v);
  83. static void AddAttribute(wxString& str, const wxString& name, const long& v);
  84. static void AddAttribute(wxString& str, const wxString& name, const double& v);
  85. static void AddAttribute(wxString& str, const wxString& name, const wxChar* s);
  86. static void AddAttribute(wxString& str, const wxString& name, const wxString& s);
  87. static void AddAttribute(wxString& str, const wxString& name, const wxColour& col);
  88. static void AddAttribute(wxString& str, const wxString& name, const wxTextAttrDimension& dim);
  89. static void AddAttribute(wxString& str, const wxString& rootName, const wxTextAttrDimensions& dims);
  90. static void AddAttribute(wxString& str, const wxString& rootName, const wxTextAttrBorder& border);
  91. static void AddAttribute(wxString& str, const wxString& rootName, const wxTextAttrBorders& borders);
  92. /// Create a string containing style attributes
  93. static wxString AddAttributes(const wxRichTextAttr& attr, bool isPara = false);
  94. virtual bool ExportStyleDefinition(wxOutputStream& stream, wxRichTextStyleDefinition* def, int level);
  95. virtual bool WriteProperties(wxOutputStream& stream, const wxRichTextProperties& properties, int level);
  96. #endif
  97. #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
  98. static void AddAttribute(wxXmlNode* node, const wxString& name, const int& v);
  99. static void AddAttribute(wxXmlNode* node, const wxString& name, const long& v);
  100. static void AddAttribute(wxXmlNode* node, const wxString& name, const double& v);
  101. static void AddAttribute(wxXmlNode* node, const wxString& name, const wxString& s);
  102. static void AddAttribute(wxXmlNode* node, const wxString& name, const wxColour& col);
  103. static void AddAttribute(wxXmlNode* node, const wxString& name, const wxTextAttrDimension& dim);
  104. static void AddAttribute(wxXmlNode* node, const wxString& rootName, const wxTextAttrDimensions& dims);
  105. static void AddAttribute(wxXmlNode* node, const wxString& rootName, const wxTextAttrBorder& border);
  106. static void AddAttribute(wxXmlNode* node, const wxString& rootName, const wxTextAttrBorders& borders);
  107. static bool AddAttributes(wxXmlNode* node, wxRichTextAttr& attr, bool isPara = false);
  108. virtual bool ExportStyleDefinition(wxXmlNode* parent, wxRichTextStyleDefinition* def);
  109. // Write the properties
  110. virtual bool WriteProperties(wxXmlNode* node, const wxRichTextProperties& properties);
  111. #endif
  112. public:
  113. #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
  114. // Used during saving
  115. wxMBConv* m_convMem;
  116. wxMBConv* m_convFile;
  117. bool m_deleteConvFile;
  118. #endif
  119. wxString m_fileEncoding;
  120. int m_flags;
  121. };
  122. /*!
  123. @class wxRichTextXMLHandler
  124. Implements XML loading and saving. Two methods of saving are included:
  125. writing directly to a text stream, and populating an wxXmlDocument
  126. before writing it. The former method is considerably faster, so we favour
  127. that one, even though the code is a little less elegant.
  128. */
  129. class WXDLLIMPEXP_FWD_XML wxXmlNode;
  130. class WXDLLIMPEXP_FWD_XML wxXmlDocument;
  131. class WXDLLIMPEXP_RICHTEXT wxRichTextXMLHandler: public wxRichTextFileHandler
  132. {
  133. DECLARE_DYNAMIC_CLASS(wxRichTextXMLHandler)
  134. public:
  135. wxRichTextXMLHandler(const wxString& name = wxT("XML"), const wxString& ext = wxT("xml"), int type = wxRICHTEXT_TYPE_XML)
  136. : wxRichTextFileHandler(name, ext, type)
  137. { Init(); }
  138. void Init();
  139. #if wxUSE_STREAMS
  140. #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
  141. /// Recursively export an object
  142. bool ExportXML(wxOutputStream& stream, wxRichTextObject& obj, int level);
  143. #endif
  144. #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
  145. bool ExportXML(wxXmlNode* parent, wxRichTextObject& obj);
  146. #endif
  147. /// Recursively import an object
  148. bool ImportXML(wxRichTextBuffer* buffer, wxRichTextObject* obj, wxXmlNode* node);
  149. #endif
  150. /// Creates an object given an XML element name
  151. virtual wxRichTextObject* CreateObjectForXMLName(wxRichTextObject* parent, const wxString& name) const;
  152. /// Can we save using this handler?
  153. virtual bool CanSave() const { return true; }
  154. /// Can we load using this handler?
  155. virtual bool CanLoad() const { return true; }
  156. /// Returns the XML helper object, implementing functionality
  157. /// that can be reused elsewhere.
  158. wxRichTextXMLHelper& GetHelper() { return m_helper; }
  159. // Implementation
  160. /**
  161. Call with XML node name, C++ class name so that wxRTC can read in the node.
  162. If you add a custom object, call this.
  163. */
  164. static void RegisterNodeName(const wxString& nodeName, const wxString& className) { sm_nodeNameToClassMap[nodeName] = className; }
  165. /**
  166. Cleans up the mapping between node name and C++ class.
  167. */
  168. static void ClearNodeToClassMap() { sm_nodeNameToClassMap.clear(); }
  169. protected:
  170. #if wxUSE_STREAMS
  171. virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream);
  172. virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
  173. #endif
  174. wxRichTextXMLHelper m_helper;
  175. static wxStringToStringHashMap sm_nodeNameToClassMap;
  176. };
  177. #endif
  178. // wxUSE_RICHTEXT && wxUSE_XML
  179. #endif
  180. // _WX_RICHTEXTXML_H_