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