Fix handling of spaces after <img> tag in wxHTML.
[wxWidgets.git] / include / wx / html / winpars.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/html/winpars.h
3 // Purpose: wxHtmlWinParser class (parser to be used with wxHtmlWindow)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_WINPARS_H_
11 #define _WX_WINPARS_H_
12
13 #include "wx/defs.h"
14 #if wxUSE_HTML
15
16 #include "wx/module.h"
17 #include "wx/font.h"
18 #include "wx/html/htmlpars.h"
19 #include "wx/html/htmlcell.h"
20 #include "wx/encconv.h"
21
22 class WXDLLIMPEXP_FWD_HTML wxHtmlWindow;
23 class WXDLLIMPEXP_FWD_HTML wxHtmlWindowInterface;
24 class WXDLLIMPEXP_FWD_HTML wxHtmlWinParser;
25 class WXDLLIMPEXP_FWD_HTML wxHtmlWinTagHandler;
26 class WXDLLIMPEXP_FWD_HTML wxHtmlTagsModule;
27
28
29 //--------------------------------------------------------------------------------
30 // wxHtmlWinParser
31 // This class is derived from wxHtmlParser and its mail goal
32 // is to parse HTML input so that it can be displayed in
33 // wxHtmlWindow. It uses special wxHtmlWinTagHandler.
34 //--------------------------------------------------------------------------------
35
36 class WXDLLIMPEXP_HTML wxHtmlWinParser : public wxHtmlParser
37 {
38 DECLARE_ABSTRACT_CLASS(wxHtmlWinParser)
39 friend class wxHtmlWindow;
40
41 public:
42 wxHtmlWinParser(wxHtmlWindowInterface *wndIface = NULL);
43
44 virtual ~wxHtmlWinParser();
45
46 virtual void InitParser(const wxString& source);
47 virtual void DoneParser();
48 virtual wxObject* GetProduct();
49
50 virtual wxFSFile *OpenURL(wxHtmlURLType type, const wxString& url) const;
51
52 // Set's the DC used for parsing. If SetDC() is not called,
53 // parsing won't proceed
54 virtual void SetDC(wxDC *dc, double pixel_scale = 1.0)
55 { SetDC(dc, pixel_scale, pixel_scale); }
56 void SetDC(wxDC *dc, double pixel_scale, double font_scale);
57
58 wxDC *GetDC() {return m_DC;}
59 double GetPixelScale() {return m_PixelScale;}
60 int GetCharHeight() const {return m_CharHeight;}
61 int GetCharWidth() const {return m_CharWidth;}
62
63 // NOTE : these functions do _not_ return _actual_
64 // height/width. They return h/w of default font
65 // for this DC. If you want actual values, call
66 // GetDC()->GetChar...()
67
68 // returns interface to the rendering window
69 wxHtmlWindowInterface *GetWindowInterface() {return m_windowInterface;}
70 #if WXWIN_COMPATIBILITY_2_6
71 // deprecated, use GetWindowInterface()->GetHTMLWindow() instead
72 wxDEPRECATED( wxHtmlWindow *GetWindow() );
73 #endif
74
75 // Sets fonts to be used when displaying HTML page. (if size null then default sizes used).
76 void SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes = NULL);
77
78 // Sets font sizes to be relative to the given size or the system
79 // default size; use either specified or default font
80 void SetStandardFonts(int size = -1,
81 const wxString& normal_face = wxEmptyString,
82 const wxString& fixed_face = wxEmptyString);
83
84 // Adds tags module. see wxHtmlTagsModule for details.
85 static void AddModule(wxHtmlTagsModule *module);
86
87 static void RemoveModule(wxHtmlTagsModule *module);
88
89 // parsing-related methods. These methods are called by tag handlers:
90
91 // Returns pointer to actual container. Common use in tag handler is :
92 // m_WParser->GetContainer()->InsertCell(new ...);
93 wxHtmlContainerCell *GetContainer() const {return m_Container;}
94
95 // opens new container. This container is sub-container of opened
96 // container. Sets GetContainer to newly created container
97 // and returns it.
98 wxHtmlContainerCell *OpenContainer();
99
100 // works like OpenContainer except that new container is not created
101 // but c is used. You can use this to directly set actual container
102 wxHtmlContainerCell *SetContainer(wxHtmlContainerCell *c);
103
104 // closes the container and sets actual Container to upper-level
105 // container
106 wxHtmlContainerCell *CloseContainer();
107
108 int GetFontSize() const {return m_FontSize;}
109 void SetFontSize(int s);
110 // Try to map a font size in points to the HTML 1-7 font size range.
111 void SetFontPointSize(int pt);
112 int GetFontBold() const {return m_FontBold;}
113 void SetFontBold(int x) {m_FontBold = x;}
114 int GetFontItalic() const {return m_FontItalic;}
115 void SetFontItalic(int x) {m_FontItalic = x;}
116 int GetFontUnderlined() const {return m_FontUnderlined;}
117 void SetFontUnderlined(int x) {m_FontUnderlined = x;}
118 int GetFontFixed() const {return m_FontFixed;}
119 void SetFontFixed(int x) {m_FontFixed = x;}
120 wxString GetFontFace() const {return GetFontFixed() ? m_FontFaceFixed : m_FontFaceNormal;}
121 void SetFontFace(const wxString& face);
122
123 int GetAlign() const {return m_Align;}
124 void SetAlign(int a) {m_Align = a;}
125
126 wxHtmlScriptMode GetScriptMode() const { return m_ScriptMode; }
127 void SetScriptMode(wxHtmlScriptMode mode) { m_ScriptMode = mode; }
128 long GetScriptBaseline() const { return m_ScriptBaseline; }
129 void SetScriptBaseline(long base) { m_ScriptBaseline = base; }
130
131 const wxColour& GetLinkColor() const { return m_LinkColor; }
132 void SetLinkColor(const wxColour& clr) { m_LinkColor = clr; }
133 const wxColour& GetActualColor() const { return m_ActualColor; }
134 void SetActualColor(const wxColour& clr) { m_ActualColor = clr ;}
135 const wxHtmlLinkInfo& GetLink() const { return m_Link; }
136 void SetLink(const wxHtmlLinkInfo& link);
137
138 // applies current parser state (link, sub/supscript, ...) to given cell
139 void ApplyStateToCell(wxHtmlCell *cell);
140
141 // Needs to be called after inserting a cell that interrupts the flow of
142 // the text like e.g. <img> and tells us to not consider any of the
143 // following space as being part of the same space run as before.
144 void StopCollapsingSpaces() { m_tmpLastWasSpace = false; }
145
146 #if !wxUSE_UNICODE
147 void SetInputEncoding(wxFontEncoding enc);
148 wxFontEncoding GetInputEncoding() const { return m_InputEnc; }
149 wxFontEncoding GetOutputEncoding() const { return m_OutputEnc; }
150 wxEncodingConverter *GetEncodingConverter() const { return m_EncConv; }
151 #endif
152
153 // creates font depending on m_Font* members.
154 virtual wxFont* CreateCurrentFont();
155
156 enum WhitespaceMode
157 {
158 Whitespace_Normal, // normal mode, collapse whitespace
159 Whitespace_Pre // inside <pre>, keep whitespace as-is
160 };
161
162 // change the current whitespace handling mode
163 void SetWhitespaceMode(WhitespaceMode mode) { m_whitespaceMode = mode; }
164 WhitespaceMode GetWhitespaceMode() const { return m_whitespaceMode; }
165
166 protected:
167 virtual void AddText(const wxString& txt);
168
169 private:
170 void FlushWordBuf(wxChar *temp, int& len);
171 void AddWord(wxHtmlWordCell *word);
172 void AddWord(const wxString& word)
173 { AddWord(new wxHtmlWordCell(word, *(GetDC()))); }
174 void AddPreBlock(const wxString& text);
175
176 bool m_tmpLastWasSpace;
177 wxChar *m_tmpStrBuf;
178 size_t m_tmpStrBufSize;
179 // temporary variables used by AddText
180 wxHtmlWindowInterface *m_windowInterface;
181 // window we're parsing for
182 double m_PixelScale, m_FontScale;
183 wxDC *m_DC;
184 // Device Context we're parsing for
185 static wxList m_Modules;
186 // list of tags modules (see wxHtmlTagsModule for details)
187 // This list is used to initialize m_Handlers member.
188
189 wxHtmlContainerCell *m_Container;
190 // current container. See Open/CloseContainer for details.
191
192 int m_FontBold, m_FontItalic, m_FontUnderlined, m_FontFixed; // this is not true,false but 1,0, we need it for indexing
193 int m_FontSize; // From 1 (smallest) to 7, default is 3.
194 wxColour m_LinkColor;
195 wxColour m_ActualColor;
196 // basic font parameters.
197 wxHtmlLinkInfo m_Link;
198 // actual hypertext link or empty string
199 bool m_UseLink;
200 // true if m_Link is not empty
201 long m_CharHeight, m_CharWidth;
202 // average height of normal-sized text
203 int m_Align;
204 // actual alignment
205 wxHtmlScriptMode m_ScriptMode;
206 // current script mode (sub/sup/normal)
207 long m_ScriptBaseline;
208 // current sub/supscript base
209
210 wxFont* m_FontsTable[2][2][2][2][7];
211 wxString m_FontsFacesTable[2][2][2][2][7];
212 #if !wxUSE_UNICODE
213 wxFontEncoding m_FontsEncTable[2][2][2][2][7];
214 #endif
215 // table of loaded fonts. 1st four indexes are 0 or 1, depending on on/off
216 // state of these flags (from left to right):
217 // [bold][italic][underlined][fixed_size]
218 // last index is font size : from 0 to 6 (remapped from html sizes 1 to 7)
219 // Note : this table covers all possible combinations of fonts, but not
220 // all of them are used, so many items in table are usually NULL.
221 int m_FontsSizes[7];
222 wxString m_FontFaceFixed, m_FontFaceNormal;
223 // html font sizes and faces of fixed and proportional fonts
224
225 #if !wxUSE_UNICODE
226 wxChar m_nbsp;
227 wxFontEncoding m_InputEnc, m_OutputEnc;
228 // I/O font encodings
229 wxEncodingConverter *m_EncConv;
230 #endif
231
232 // current whitespace handling mode
233 WhitespaceMode m_whitespaceMode;
234
235 wxHtmlWordCell *m_lastWordCell;
236
237 // current position on line, in num. of characters; used to properly
238 // expand TABs; only updated while inside <pre>
239 int m_posColumn;
240
241 wxDECLARE_NO_COPY_CLASS(wxHtmlWinParser);
242 };
243
244
245
246
247
248
249 //-----------------------------------------------------------------------------
250 // wxHtmlWinTagHandler
251 // This is basicly wxHtmlTagHandler except
252 // it is extended with protected member m_Parser pointing to
253 // the wxHtmlWinParser object
254 //-----------------------------------------------------------------------------
255
256 class WXDLLIMPEXP_HTML wxHtmlWinTagHandler : public wxHtmlTagHandler
257 {
258 DECLARE_ABSTRACT_CLASS(wxHtmlWinTagHandler)
259
260 public:
261 wxHtmlWinTagHandler() : wxHtmlTagHandler() {}
262
263 virtual void SetParser(wxHtmlParser *parser) {wxHtmlTagHandler::SetParser(parser); m_WParser = (wxHtmlWinParser*) parser;}
264
265 protected:
266 wxHtmlWinParser *m_WParser; // same as m_Parser, but overcasted
267
268 wxDECLARE_NO_COPY_CLASS(wxHtmlWinTagHandler);
269 };
270
271
272
273
274
275
276 //----------------------------------------------------------------------------
277 // wxHtmlTagsModule
278 // This is basic of dynamic tag handlers binding.
279 // The class provides methods for filling parser's handlers
280 // hash table.
281 // (See documentation for details)
282 //----------------------------------------------------------------------------
283
284 class WXDLLIMPEXP_HTML wxHtmlTagsModule : public wxModule
285 {
286 DECLARE_DYNAMIC_CLASS(wxHtmlTagsModule)
287
288 public:
289 wxHtmlTagsModule() : wxModule() {}
290
291 virtual bool OnInit();
292 virtual void OnExit();
293
294 // This is called by wxHtmlWinParser.
295 // The method must simply call parser->AddTagHandler(new
296 // <handler_class_name>); for each handler
297 virtual void FillHandlersTable(wxHtmlWinParser * WXUNUSED(parser)) { }
298 };
299
300
301 #endif
302
303 #endif // _WX_WINPARS_H_