1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlWinParser class (parser to be used with wxHtmlWindow)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_WINPARS_H_
11 #define _WX_WINPARS_H_
16 #include "wx/module.h"
18 #include "wx/html/htmlpars.h"
19 #include "wx/html/htmlcell.h"
20 #include "wx/encconv.h"
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
;
29 //--------------------------------------------------------------------------------
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 //--------------------------------------------------------------------------------
36 class WXDLLIMPEXP_HTML wxHtmlWinParser
: public wxHtmlParser
38 DECLARE_ABSTRACT_CLASS(wxHtmlWinParser
)
39 friend class wxHtmlWindow
;
42 wxHtmlWinParser(wxHtmlWindowInterface
*wndIface
= NULL
);
44 virtual ~wxHtmlWinParser();
46 virtual void InitParser(const wxString
& source
);
47 virtual void DoneParser();
48 virtual wxObject
* GetProduct();
50 virtual wxFSFile
*OpenURL(wxHtmlURLType type
, const wxString
& url
) const;
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
);
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
;}
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...()
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() );
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
);
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
);
84 // Adds tags module. see wxHtmlTagsModule for details.
85 static void AddModule(wxHtmlTagsModule
*module);
87 static void RemoveModule(wxHtmlTagsModule
*module);
89 // parsing-related methods. These methods are called by tag handlers:
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
;}
95 // opens new container. This container is sub-container of opened
96 // container. Sets GetContainer to newly created container
98 wxHtmlContainerCell
*OpenContainer();
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
);
104 // closes the container and sets actual Container to upper-level
106 wxHtmlContainerCell
*CloseContainer();
108 int GetFontSize() const {return m_FontSize
;}
109 void SetFontSize(int s
);
110 int GetFontBold() const {return m_FontBold
;}
111 void SetFontBold(int x
) {m_FontBold
= x
;}
112 int GetFontItalic() const {return m_FontItalic
;}
113 void SetFontItalic(int x
) {m_FontItalic
= x
;}
114 int GetFontUnderlined() const {return m_FontUnderlined
;}
115 void SetFontUnderlined(int x
) {m_FontUnderlined
= x
;}
116 int GetFontFixed() const {return m_FontFixed
;}
117 void SetFontFixed(int x
) {m_FontFixed
= x
;}
118 wxString
GetFontFace() const {return GetFontFixed() ? m_FontFaceFixed
: m_FontFaceNormal
;}
119 void SetFontFace(const wxString
& face
);
121 int GetAlign() const {return m_Align
;}
122 void SetAlign(int a
) {m_Align
= a
;}
124 wxHtmlScriptMode
GetScriptMode() const { return m_ScriptMode
; }
125 void SetScriptMode(wxHtmlScriptMode mode
) { m_ScriptMode
= mode
; }
126 long GetScriptBaseline() const { return m_ScriptBaseline
; }
127 void SetScriptBaseline(long base
) { m_ScriptBaseline
= base
; }
129 const wxColour
& GetLinkColor() const { return m_LinkColor
; }
130 void SetLinkColor(const wxColour
& clr
) { m_LinkColor
= clr
; }
131 const wxColour
& GetActualColor() const { return m_ActualColor
; }
132 void SetActualColor(const wxColour
& clr
) { m_ActualColor
= clr
;}
133 const wxHtmlLinkInfo
& GetLink() const { return m_Link
; }
134 void SetLink(const wxHtmlLinkInfo
& link
);
136 // applies current parser state (link, sub/supscript, ...) to given cell
137 void ApplyStateToCell(wxHtmlCell
*cell
);
140 void SetInputEncoding(wxFontEncoding enc
);
141 wxFontEncoding
GetInputEncoding() const { return m_InputEnc
; }
142 wxFontEncoding
GetOutputEncoding() const { return m_OutputEnc
; }
143 wxEncodingConverter
*GetEncodingConverter() const { return m_EncConv
; }
146 // creates font depending on m_Font* members.
147 virtual wxFont
* CreateCurrentFont();
151 Whitespace_Normal
, // normal mode, collapse whitespace
152 Whitespace_Pre
// inside <pre>, keep whitespace as-is
155 // change the current whitespace handling mode
156 void SetWhitespaceMode(WhitespaceMode mode
) { m_whitespaceMode
= mode
; }
157 WhitespaceMode
GetWhitespaceMode() const { return m_whitespaceMode
; }
160 virtual void AddText(const wxString
& txt
);
163 void FlushWordBuf(wxChar
*temp
, int& len
);
164 void AddWord(wxHtmlWordCell
*word
);
165 void AddWord(const wxString
& word
)
166 { AddWord(new wxHtmlWordCell(word
, *(GetDC()))); }
167 void AddPreBlock(const wxString
& text
);
169 bool m_tmpLastWasSpace
;
171 size_t m_tmpStrBufSize
;
172 // temporary variables used by AddText
173 wxHtmlWindowInterface
*m_windowInterface
;
174 // window we're parsing for
175 double m_PixelScale
, m_FontScale
;
177 // Device Context we're parsing for
178 static wxList m_Modules
;
179 // list of tags modules (see wxHtmlTagsModule for details)
180 // This list is used to initialize m_Handlers member.
182 wxHtmlContainerCell
*m_Container
;
183 // current container. See Open/CloseContainer for details.
185 int m_FontBold
, m_FontItalic
, m_FontUnderlined
, m_FontFixed
; // this is not true,false but 1,0, we need it for indexing
186 int m_FontSize
; /* -2 to +4, 0 is default */
187 wxColour m_LinkColor
;
188 wxColour m_ActualColor
;
189 // basic font parameters.
190 wxHtmlLinkInfo m_Link
;
191 // actual hypertext link or empty string
193 // true if m_Link is not empty
194 long m_CharHeight
, m_CharWidth
;
195 // average height of normal-sized text
198 wxHtmlScriptMode m_ScriptMode
;
199 // current script mode (sub/sup/normal)
200 long m_ScriptBaseline
;
201 // current sub/supscript base
203 wxFont
* m_FontsTable
[2][2][2][2][7];
204 wxString m_FontsFacesTable
[2][2][2][2][7];
206 wxFontEncoding m_FontsEncTable
[2][2][2][2][7];
208 // table of loaded fonts. 1st four indexes are 0 or 1, depending on on/off
209 // state of these flags (from left to right):
210 // [bold][italic][underlined][fixed_size]
211 // last index is font size : from 0 to 6 (remapped from html sizes 1 to 7)
212 // Note : this table covers all possible combinations of fonts, but not
213 // all of them are used, so many items in table are usually NULL.
215 wxString m_FontFaceFixed
, m_FontFaceNormal
;
216 // html font sizes and faces of fixed and proportional fonts
220 wxFontEncoding m_InputEnc
, m_OutputEnc
;
221 // I/O font encodings
222 wxEncodingConverter
*m_EncConv
;
225 // current whitespace handling mode
226 WhitespaceMode m_whitespaceMode
;
228 wxHtmlWordCell
*m_lastWordCell
;
230 // current position on line, in num. of characters; used to properly
231 // expand TABs; only updated while inside <pre>
234 wxDECLARE_NO_COPY_CLASS(wxHtmlWinParser
);
242 //-----------------------------------------------------------------------------
243 // wxHtmlWinTagHandler
244 // This is basicly wxHtmlTagHandler except
245 // it is extended with protected member m_Parser pointing to
246 // the wxHtmlWinParser object
247 //-----------------------------------------------------------------------------
249 class WXDLLIMPEXP_HTML wxHtmlWinTagHandler
: public wxHtmlTagHandler
251 DECLARE_ABSTRACT_CLASS(wxHtmlWinTagHandler
)
254 wxHtmlWinTagHandler() : wxHtmlTagHandler() {}
256 virtual void SetParser(wxHtmlParser
*parser
) {wxHtmlTagHandler::SetParser(parser
); m_WParser
= (wxHtmlWinParser
*) parser
;}
259 wxHtmlWinParser
*m_WParser
; // same as m_Parser, but overcasted
261 wxDECLARE_NO_COPY_CLASS(wxHtmlWinTagHandler
);
269 //----------------------------------------------------------------------------
271 // This is basic of dynamic tag handlers binding.
272 // The class provides methods for filling parser's handlers
274 // (See documentation for details)
275 //----------------------------------------------------------------------------
277 class WXDLLIMPEXP_HTML wxHtmlTagsModule
: public wxModule
279 DECLARE_DYNAMIC_CLASS(wxHtmlTagsModule
)
282 wxHtmlTagsModule() : wxModule() {}
284 virtual bool OnInit();
285 virtual void OnExit();
287 // This is called by wxHtmlWinParser.
288 // The method must simply call parser->AddTagHandler(new
289 // <handler_class_name>); for each handler
290 virtual void FillHandlersTable(wxHtmlWinParser
* WXUNUSED(parser
)) { }
296 #endif // _WX_WINPARS_H_