1. added abstract interface to wxHtmlWindow for use by wxHtmlWinParser and implemente...
[wxWidgets.git] / include / wx / html / winpars.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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_HTML wxHtmlWindow;
23 class WXDLLIMPEXP_HTML wxHtmlWindowInterface;
24 class WXDLLIMPEXP_HTML wxHtmlWinParser;
25 class WXDLLIMPEXP_HTML wxHtmlWinTagHandler;
26 class WXDLLIMPEXP_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 ~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 { m_DC = dc; m_PixelScale = pixel_scale; }
56
57 wxDC *GetDC() {return m_DC;}
58 double GetPixelScale() {return m_PixelScale;}
59 int GetCharHeight() const {return m_CharHeight;}
60 int GetCharWidth() const {return m_CharWidth;}
61
62 // NOTE : these functions do _not_ return _actual_
63 // height/width. They return h/w of default font
64 // for this DC. If you want actual values, call
65 // GetDC()->GetChar...()
66
67 // returns associated wxWindow
68 wxHtmlWindowInterface *GetWindowInterface() {return m_windowInterface;}
69 #if WXWIN_COMPATIBILITY_2_6
70 wxDEPRECATED( wxHtmlWindow *GetWindow() );
71 #endif
72
73 // Sets fonts to be used when displaying HTML page. (if size null then default sizes used).
74 void SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes = NULL);
75
76 // Sets font sizes to be relative to the given size or the system
77 // default size; use either specified or default font
78 void SetStandardFonts(int size = -1,
79 const wxString& normal_face = wxEmptyString,
80 const wxString& fixed_face = wxEmptyString);
81
82 // Adds tags module. see wxHtmlTagsModule for details.
83 static void AddModule(wxHtmlTagsModule *module);
84
85 static void RemoveModule(wxHtmlTagsModule *module);
86
87 // parsing-related methods. These methods are called by tag handlers:
88
89 // Returns pointer to actual container. Common use in tag handler is :
90 // m_WParser->GetContainer()->InsertCell(new ...);
91 wxHtmlContainerCell *GetContainer() const {return m_Container;}
92
93 // opens new container. This container is sub-container of opened
94 // container. Sets GetContainer to newly created container
95 // and returns it.
96 wxHtmlContainerCell *OpenContainer();
97
98 // works like OpenContainer except that new container is not created
99 // but c is used. You can use this to directly set actual container
100 wxHtmlContainerCell *SetContainer(wxHtmlContainerCell *c);
101
102 // closes the container and sets actual Container to upper-level
103 // container
104 wxHtmlContainerCell *CloseContainer();
105
106 int GetFontSize() const {return m_FontSize;}
107 void SetFontSize(int s);
108 int GetFontBold() const {return m_FontBold;}
109 void SetFontBold(int x) {m_FontBold = x;}
110 int GetFontItalic() const {return m_FontItalic;}
111 void SetFontItalic(int x) {m_FontItalic = x;}
112 int GetFontUnderlined() const {return m_FontUnderlined;}
113 void SetFontUnderlined(int x) {m_FontUnderlined = x;}
114 int GetFontFixed() const {return m_FontFixed;}
115 void SetFontFixed(int x) {m_FontFixed = x;}
116 wxString GetFontFace() const {return GetFontFixed() ? m_FontFaceFixed : m_FontFaceNormal;}
117 void SetFontFace(const wxString& face);
118
119 int GetAlign() const {return m_Align;}
120 void SetAlign(int a) {m_Align = a;}
121
122 wxHtmlScriptMode GetScriptMode() const { return m_ScriptMode; }
123 void SetScriptMode(wxHtmlScriptMode mode) { m_ScriptMode = mode; }
124 long GetScriptBaseline() const { return m_ScriptBaseline; }
125 void SetScriptBaseline(long base) { m_ScriptBaseline = base; }
126
127 const wxColour& GetLinkColor() const { return m_LinkColor; }
128 void SetLinkColor(const wxColour& clr) { m_LinkColor = clr; }
129 const wxColour& GetActualColor() const { return m_ActualColor; }
130 void SetActualColor(const wxColour& clr) { m_ActualColor = clr ;}
131 const wxHtmlLinkInfo& GetLink() const { return m_Link; }
132 void SetLink(const wxHtmlLinkInfo& link);
133
134 // applies current parser state (link, sub/supscript, ...) to given cell
135 void ApplyStateToCell(wxHtmlCell *cell);
136
137 #if !wxUSE_UNICODE
138 void SetInputEncoding(wxFontEncoding enc);
139 wxFontEncoding GetInputEncoding() const { return m_InputEnc; }
140 wxFontEncoding GetOutputEncoding() const { return m_OutputEnc; }
141 wxEncodingConverter *GetEncodingConverter() const { return m_EncConv; }
142 #endif
143
144 // creates font depending on m_Font* members.
145 virtual wxFont* CreateCurrentFont();
146
147 protected:
148 virtual void AddText(const wxChar* txt);
149
150 private:
151 void DoAddText(wxChar *temp, int& templen, wxChar nbsp);
152
153 bool m_tmpLastWasSpace;
154 wxChar *m_tmpStrBuf;
155 size_t m_tmpStrBufSize;
156 // temporary variables used by AddText
157 wxHtmlWindowInterface *m_windowInterface;
158 // window we're parsing for
159 double m_PixelScale;
160 wxDC *m_DC;
161 // Device Context we're parsing for
162 static wxList m_Modules;
163 // list of tags modules (see wxHtmlTagsModule for details)
164 // This list is used to initialize m_Handlers member.
165
166 wxHtmlContainerCell *m_Container;
167 // current container. See Open/CloseContainer for details.
168
169 int m_FontBold, m_FontItalic, m_FontUnderlined, m_FontFixed; // this is not true,false but 1,0, we need it for indexing
170 int m_FontSize; /* -2 to +4, 0 is default */
171 wxColour m_LinkColor;
172 wxColour m_ActualColor;
173 // basic font parameters.
174 wxHtmlLinkInfo m_Link;
175 // actual hypertext link or empty string
176 bool m_UseLink;
177 // true if m_Link is not empty
178 long m_CharHeight, m_CharWidth;
179 // average height of normal-sized text
180 int m_Align;
181 // actual alignment
182 wxHtmlScriptMode m_ScriptMode;
183 // current script mode (sub/sup/normal)
184 long m_ScriptBaseline;
185 // current sub/supscript base
186
187 wxFont* m_FontsTable[2][2][2][2][7];
188 wxString m_FontsFacesTable[2][2][2][2][7];
189 #if !wxUSE_UNICODE
190 wxFontEncoding m_FontsEncTable[2][2][2][2][7];
191 #endif
192 // table of loaded fonts. 1st four indexes are 0 or 1, depending on on/off
193 // state of these flags (from left to right):
194 // [bold][italic][underlined][fixed_size]
195 // last index is font size : from 0 to 6 (remapped from html sizes 1 to 7)
196 // Note : this table covers all possible combinations of fonts, but not
197 // all of them are used, so many items in table are usually NULL.
198 int m_FontsSizes[7];
199 wxString m_FontFaceFixed, m_FontFaceNormal;
200 // html font sizes and faces of fixed and proportional fonts
201
202 #if !wxUSE_UNICODE
203 wxFontEncoding m_InputEnc, m_OutputEnc;
204 // I/O font encodings
205 wxEncodingConverter *m_EncConv;
206 #endif
207
208 wxHtmlWordCell *m_lastWordCell;
209
210 DECLARE_NO_COPY_CLASS(wxHtmlWinParser)
211 };
212
213
214
215
216
217
218 //-----------------------------------------------------------------------------
219 // wxHtmlWinTagHandler
220 // This is basicly wxHtmlTagHandler except
221 // it is extended with protected member m_Parser pointing to
222 // the wxHtmlWinParser object
223 //-----------------------------------------------------------------------------
224
225 class WXDLLIMPEXP_HTML wxHtmlWinTagHandler : public wxHtmlTagHandler
226 {
227 DECLARE_ABSTRACT_CLASS(wxHtmlWinTagHandler)
228
229 public:
230 wxHtmlWinTagHandler() : wxHtmlTagHandler() {}
231
232 virtual void SetParser(wxHtmlParser *parser) {wxHtmlTagHandler::SetParser(parser); m_WParser = (wxHtmlWinParser*) parser;};
233
234 protected:
235 wxHtmlWinParser *m_WParser; // same as m_Parser, but overcasted
236
237 DECLARE_NO_COPY_CLASS(wxHtmlWinTagHandler)
238 };
239
240
241
242
243
244
245 //----------------------------------------------------------------------------
246 // wxHtmlTagsModule
247 // This is basic of dynamic tag handlers binding.
248 // The class provides methods for filling parser's handlers
249 // hash table.
250 // (See documentation for details)
251 //----------------------------------------------------------------------------
252
253 class WXDLLIMPEXP_HTML wxHtmlTagsModule : public wxModule
254 {
255 DECLARE_DYNAMIC_CLASS(wxHtmlTagsModule)
256
257 public:
258 wxHtmlTagsModule() : wxModule() {}
259
260 virtual bool OnInit();
261 virtual void OnExit();
262
263 // This is called by wxHtmlWinParser.
264 // The method must simply call parser->AddTagHandler(new
265 // <handler_class_name>); for each handler
266 virtual void FillHandlersTable(wxHtmlWinParser * WXUNUSED(parser)) { }
267 };
268
269
270 #endif
271
272 #endif // _WX_WINPARS_H_