1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/richtext/richtexthtml.h
3 // Purpose: HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_RICHTEXTHTML_H_
12 #define _WX_RICHTEXTHTML_H_
18 #include "wx/richtext/richtextbuffer.h"
20 // Use CSS styles where applicable, otherwise use non-CSS workarounds
21 #define wxRICHTEXT_HANDLER_USE_CSS 0x1000
24 * wxRichTextHTMLHandler
27 class WXDLLIMPEXP_RICHTEXT wxRichTextHTMLHandler
: public wxRichTextFileHandler
29 DECLARE_DYNAMIC_CLASS(wxRichTextHTMLHandler
)
31 wxRichTextHTMLHandler(const wxString
& name
= wxT("HTML"), const wxString
& ext
= wxT("html"), int type
= wxRICHTEXT_TYPE_HTML
);
33 /// Can we save using this handler?
34 virtual bool CanSave() const { return true; }
36 /// Can we load using this handler?
37 virtual bool CanLoad() const { return false; }
39 /// Can we handle this filename (if using files)? By default, checks the extension.
40 virtual bool CanHandle(const wxString
& filename
) const;
42 // Accessors and operations unique to this handler
44 /// Set and get the list of image locations generated by the last operation
45 void SetTemporaryImageLocations(const wxArrayString
& locations
) { m_imageLocations
= locations
; }
46 const wxArrayString
& GetTemporaryImageLocations() const { return m_imageLocations
; }
48 /// Clear the image locations generated by the last operation
49 void ClearTemporaryImageLocations() { m_imageLocations
.Clear(); }
51 /// Delete the in-memory or temporary files generated by the last operation
52 bool DeleteTemporaryImages();
54 /// Delete the in-memory or temporary files generated by the last operation. This is a static
55 /// function that can be used to delete the saved locations from an earlier operation,
56 /// for example after the user has viewed the HTML file.
57 static bool DeleteTemporaryImages(int flags
, const wxArrayString
& imageLocations
);
59 /// Reset the file counter, in case, for example, the same names are required each time
60 static void SetFileCounter(int counter
) { sm_fileCounter
= counter
; }
62 /// Set and get the directory for storing temporary files. If empty, the system
63 /// temporary directory will be used.
64 void SetTempDir(const wxString
& tempDir
) { m_tempDir
= tempDir
; }
65 const wxString
& GetTempDir() const { return m_tempDir
; }
67 /// Set and get mapping from point size to HTML font size. There should be 7 elements,
68 /// one for each HTML font size, each element specifying the maximum point size for that
69 /// HTML font size. E.g. 8, 10, 13, 17, 22, 29, 100
70 void SetFontSizeMapping(const wxArrayInt
& fontSizeMapping
) { m_fontSizeMapping
= fontSizeMapping
; }
71 wxArrayInt
GetFontSizeMapping() const { return m_fontSizeMapping
; }
78 virtual bool DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
);
79 virtual bool DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
);
81 /// Output character formatting
82 void BeginCharacterFormatting(const wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& thisStyle
, const wxRichTextAttr
& paraStyle
, wxTextOutputStream
& stream
);
83 void EndCharacterFormatting(const wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& thisStyle
, const wxRichTextAttr
& paraStyle
, wxTextOutputStream
& stream
);
85 /// Output paragraph formatting
86 void BeginParagraphFormatting(const wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& thisStyle
, wxTextOutputStream
& stream
);
87 void EndParagraphFormatting(const wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& thisStyle
, wxTextOutputStream
& stream
);
90 void OutputFont(const wxRichTextAttr
& style
, wxTextOutputStream
& stream
);
92 /// Closes lists to level (-1 means close all)
93 void CloseLists(int level
, wxTextOutputStream
& str
);
95 /// Writes an image to its base64 equivalent, or to the memory filesystem, or to a file
96 void WriteImage(wxRichTextImage
* image
, wxOutputStream
& stream
);
98 /// Converts from pt to size property compatible height
99 long PtToSize(long size
);
101 /// Typical base64 encoder
102 wxChar
* b64enc(unsigned char* input
, size_t in_len
);
104 /// Gets the mime type of the given wxBITMAP_TYPE
105 const wxChar
* GetMimeType(int imageType
);
107 /// Gets the html equivalent of the specified value
108 wxString
GetAlignment(const wxRichTextAttr
& thisStyle
);
110 /// Generates array for indentations
111 wxString
SymbolicIndent(long indent
);
113 /// Finds the html equivalent of the specified bullet
114 int TypeOfList(const wxRichTextAttr
& thisStyle
, wxString
& tag
);
119 wxRichTextBuffer
* m_buffer
;
121 /// Indentation values of the table tags
122 wxArrayInt m_indents
;
124 /// Stack of list types: 0 = ol, 1 = ul
125 wxArrayInt m_listTypes
;
127 /// Is there any opened font tag?
130 /// Are we in a table?
133 /// A list of the image files or in-memory images created by the last operation.
134 wxArrayString m_imageLocations
;
136 /// A location for the temporary files
139 /// A mapping from point size to HTML font size
140 wxArrayInt m_fontSizeMapping
;
142 /// A counter for generating filenames
143 static int sm_fileCounter
;
147 // _WX_RICHTEXTXML_H_