richtexthtml.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/richtext/richtexthtml.h
  3. // Purpose: 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_RICHTEXTHTML_H_
  11. #define _WX_RICHTEXTHTML_H_
  12. /*!
  13. * Includes
  14. */
  15. #include "wx/richtext/richtextbuffer.h"
  16. // Use CSS styles where applicable, otherwise use non-CSS workarounds
  17. #define wxRICHTEXT_HANDLER_USE_CSS 0x1000
  18. /*!
  19. * wxRichTextHTMLHandler
  20. */
  21. class WXDLLIMPEXP_RICHTEXT wxRichTextHTMLHandler: public wxRichTextFileHandler
  22. {
  23. DECLARE_DYNAMIC_CLASS(wxRichTextHTMLHandler)
  24. public:
  25. wxRichTextHTMLHandler(const wxString& name = wxT("HTML"), const wxString& ext = wxT("html"), int type = wxRICHTEXT_TYPE_HTML);
  26. /// Can we save using this handler?
  27. virtual bool CanSave() const { return true; }
  28. /// Can we load using this handler?
  29. virtual bool CanLoad() const { return false; }
  30. /// Can we handle this filename (if using files)? By default, checks the extension.
  31. virtual bool CanHandle(const wxString& filename) const;
  32. // Accessors and operations unique to this handler
  33. /// Set and get the list of image locations generated by the last operation
  34. void SetTemporaryImageLocations(const wxArrayString& locations) { m_imageLocations = locations; }
  35. const wxArrayString& GetTemporaryImageLocations() const { return m_imageLocations; }
  36. /// Clear the image locations generated by the last operation
  37. void ClearTemporaryImageLocations() { m_imageLocations.Clear(); }
  38. /// Delete the in-memory or temporary files generated by the last operation
  39. bool DeleteTemporaryImages();
  40. /// Delete the in-memory or temporary files generated by the last operation. This is a static
  41. /// function that can be used to delete the saved locations from an earlier operation,
  42. /// for example after the user has viewed the HTML file.
  43. static bool DeleteTemporaryImages(int flags, const wxArrayString& imageLocations);
  44. /// Reset the file counter, in case, for example, the same names are required each time
  45. static void SetFileCounter(int counter) { sm_fileCounter = counter; }
  46. /// Set and get the directory for storing temporary files. If empty, the system
  47. /// temporary directory will be used.
  48. void SetTempDir(const wxString& tempDir) { m_tempDir = tempDir; }
  49. const wxString& GetTempDir() const { return m_tempDir; }
  50. /// Set and get mapping from point size to HTML font size. There should be 7 elements,
  51. /// one for each HTML font size, each element specifying the maximum point size for that
  52. /// HTML font size. E.g. 8, 10, 13, 17, 22, 29, 100
  53. void SetFontSizeMapping(const wxArrayInt& fontSizeMapping) { m_fontSizeMapping = fontSizeMapping; }
  54. wxArrayInt GetFontSizeMapping() const { return m_fontSizeMapping; }
  55. protected:
  56. // Implementation
  57. #if wxUSE_STREAMS
  58. virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream);
  59. virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
  60. /// Output character formatting
  61. void BeginCharacterFormatting(const wxRichTextAttr& currentStyle, const wxRichTextAttr& thisStyle, const wxRichTextAttr& paraStyle, wxTextOutputStream& stream );
  62. void EndCharacterFormatting(const wxRichTextAttr& currentStyle, const wxRichTextAttr& thisStyle, const wxRichTextAttr& paraStyle, wxTextOutputStream& stream );
  63. /// Output paragraph formatting
  64. void BeginParagraphFormatting(const wxRichTextAttr& currentStyle, const wxRichTextAttr& thisStyle, wxTextOutputStream& stream);
  65. void EndParagraphFormatting(const wxRichTextAttr& currentStyle, const wxRichTextAttr& thisStyle, wxTextOutputStream& stream);
  66. /// Output font tag
  67. void OutputFont(const wxRichTextAttr& style, wxTextOutputStream& stream);
  68. /// Closes lists to level (-1 means close all)
  69. void CloseLists(int level, wxTextOutputStream& str);
  70. /// Writes an image to its base64 equivalent, or to the memory filesystem, or to a file
  71. void WriteImage(wxRichTextImage* image, wxOutputStream& stream);
  72. /// Converts from pt to size property compatible height
  73. long PtToSize(long size);
  74. /// Typical base64 encoder
  75. wxChar* b64enc(unsigned char* input, size_t in_len);
  76. /// Gets the mime type of the given wxBITMAP_TYPE
  77. const wxChar* GetMimeType(int imageType);
  78. /// Gets the html equivalent of the specified value
  79. wxString GetAlignment(const wxRichTextAttr& thisStyle);
  80. /// Generates   array for indentations
  81. wxString SymbolicIndent(long indent);
  82. /// Finds the html equivalent of the specified bullet
  83. int TypeOfList(const wxRichTextAttr& thisStyle, wxString& tag);
  84. #endif
  85. // Data members
  86. wxRichTextBuffer* m_buffer;
  87. /// Indentation values of the table tags
  88. wxArrayInt m_indents;
  89. /// Stack of list types: 0 = ol, 1 = ul
  90. wxArrayInt m_listTypes;
  91. /// Is there any opened font tag?
  92. bool m_font;
  93. /// Are we in a table?
  94. bool m_inTable;
  95. /// A list of the image files or in-memory images created by the last operation.
  96. wxArrayString m_imageLocations;
  97. /// A location for the temporary files
  98. wxString m_tempDir;
  99. /// A mapping from point size to HTML font size
  100. wxArrayInt m_fontSizeMapping;
  101. /// A counter for generating filenames
  102. static int sm_fileCounter;
  103. };
  104. #endif
  105. // _WX_RICHTEXTXML_H_