1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        winpars.h
3 // Purpose:     wxHtmlWinParser class (parser to be used with wxHtmlWindow)
4 // Author:      Vaclav Slavik
5 // RCS-ID:      $Id: winpars.h 59260 2009-03-02 10:43:00Z VS $
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 struct wxHtmlWinParser_TextParsingState;
28 
29 
30 //--------------------------------------------------------------------------------
31 // wxHtmlWinParser
32 //                  This class is derived from wxHtmlParser and its mail goal
33 //                  is to parse HTML input so that it can be displayed in
34 //                  wxHtmlWindow. It uses special wxHtmlWinTagHandler.
35 //--------------------------------------------------------------------------------
36 
37 class WXDLLIMPEXP_HTML wxHtmlWinParser : public wxHtmlParser
38 {
39     DECLARE_ABSTRACT_CLASS(wxHtmlWinParser)
40     friend class wxHtmlWindow;
41 
42 public:
43     wxHtmlWinParser(wxHtmlWindowInterface *wndIface = NULL);
44 
45     virtual ~wxHtmlWinParser();
46 
47     virtual void InitParser(const wxString& source);
48     virtual void DoneParser();
49     virtual wxObject* GetProduct();
50 
51     virtual wxFSFile *OpenURL(wxHtmlURLType type, const wxString& url) const;
52 
53     // Set's the DC used for parsing. If SetDC() is not called,
54     // parsing won't proceed
55     virtual void SetDC(wxDC *dc, double pixel_scale = 1.0)
56         { m_DC = dc; m_PixelScale = pixel_scale; }
57 
GetDC()58     wxDC *GetDC() {return m_DC;}
GetPixelScale()59     double GetPixelScale() {return m_PixelScale;}
GetCharHeight()60     int GetCharHeight() const {return m_CharHeight;}
GetCharWidth()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
GetWindowInterface()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 ...);
GetContainer()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 
GetFontSize()108     int GetFontSize() const {return m_FontSize;}
109     void SetFontSize(int s);
GetFontBold()110     int GetFontBold() const {return m_FontBold;}
SetFontBold(int x)111     void SetFontBold(int x) {m_FontBold = x;}
GetFontItalic()112     int GetFontItalic() const {return m_FontItalic;}
SetFontItalic(int x)113     void SetFontItalic(int x) {m_FontItalic = x;}
GetFontUnderlined()114     int GetFontUnderlined() const {return m_FontUnderlined;}
SetFontUnderlined(int x)115     void SetFontUnderlined(int x) {m_FontUnderlined = x;}
GetFontFixed()116     int GetFontFixed() const {return m_FontFixed;}
SetFontFixed(int x)117     void SetFontFixed(int x) {m_FontFixed = x;}
GetFontFace()118     wxString GetFontFace() const {return GetFontFixed() ? m_FontFaceFixed : m_FontFaceNormal;}
119     void SetFontFace(const wxString& face);
120 
GetAlign()121     int GetAlign() const {return m_Align;}
SetAlign(int a)122     void SetAlign(int a) {m_Align = a;}
123 
GetScriptMode()124     wxHtmlScriptMode GetScriptMode() const { return m_ScriptMode; }
SetScriptMode(wxHtmlScriptMode mode)125     void SetScriptMode(wxHtmlScriptMode mode) { m_ScriptMode = mode; }
GetScriptBaseline()126     long GetScriptBaseline() const { return m_ScriptBaseline; }
SetScriptBaseline(long base)127     void SetScriptBaseline(long base) { m_ScriptBaseline = base; }
128 
GetLinkColor()129     const wxColour& GetLinkColor() const { return m_LinkColor; }
SetLinkColor(const wxColour & clr)130     void SetLinkColor(const wxColour& clr) { m_LinkColor = clr; }
GetActualColor()131     const wxColour& GetActualColor() const { return m_ActualColor; }
SetActualColor(const wxColour & clr)132     void SetActualColor(const wxColour& clr) { m_ActualColor = clr ;}
GetLink()133     const wxHtmlLinkInfo& GetLink() const { return m_Link; }
134     void SetLink(const wxHtmlLinkInfo& link);
135 
136     // applies current parser state (link, sub/supscript, ...) to given cell
137     void ApplyStateToCell(wxHtmlCell *cell);
138 
139 #if !wxUSE_UNICODE
140     void SetInputEncoding(wxFontEncoding enc);
GetInputEncoding()141     wxFontEncoding GetInputEncoding() const { return m_InputEnc; }
GetOutputEncoding()142     wxFontEncoding GetOutputEncoding() const { return m_OutputEnc; }
GetEncodingConverter()143     wxEncodingConverter *GetEncodingConverter() const { return m_EncConv; }
144 #endif
145 
146     // creates font depending on m_Font* members.
147     virtual wxFont* CreateCurrentFont();
148 
149 #if wxABI_VERSION >= 20808
150     enum WhitespaceMode
151     {
152         Whitespace_Normal,  // normal mode, collapse whitespace
153         Whitespace_Pre      // inside <pre>, keep whitespace as-is
154     };
155 
156     // change the current whitespace handling mode
157     void SetWhitespaceMode(WhitespaceMode mode);
158     WhitespaceMode GetWhitespaceMode() const;
159 #endif // wxABI_VERSION >= 20808
160 
161 protected:
162     virtual void AddText(const wxChar* txt);
163 
164 private:
165     void FlushWordBuf(wxChar *temp, int& templen, wxChar nbsp);
166     void AddWord(wxHtmlWordCell *c);
167     void AddWord(const wxString& word);
168     void AddPreBlock(const wxString& text);
169 
170     bool m_tmpLastWasSpace;
171     wxChar *m_tmpStrBuf;
172     size_t  m_tmpStrBufSize;
173         // temporary variables used by AddText
174     wxHtmlWindowInterface *m_windowInterface;
175             // window we're parsing for
176     double m_PixelScale;
177     wxDC *m_DC;
178             // Device Context we're parsing for
179     static wxList m_Modules;
180             // list of tags modules (see wxHtmlTagsModule for details)
181             // This list is used to initialize m_Handlers member.
182 
183     wxHtmlContainerCell *m_Container;
184             // current container. See Open/CloseContainer for details.
185 
186     int m_FontBold, m_FontItalic, m_FontUnderlined, m_FontFixed; // this is not true,false but 1,0, we need it for indexing
187     int m_FontSize; /* -2 to +4,  0 is default */
188     wxColour m_LinkColor;
189     wxColour m_ActualColor;
190             // basic font parameters.
191     wxHtmlLinkInfo m_Link;
192             // actual hypertext link or empty string
193     bool m_UseLink;
194             // true if m_Link is not empty
195     long m_CharHeight, m_CharWidth;
196             // average height of normal-sized text
197     int m_Align;
198             // actual alignment
199     wxHtmlScriptMode m_ScriptMode;
200             // current script mode (sub/sup/normal)
201     long m_ScriptBaseline;
202             // current sub/supscript base
203 
204     wxFont* m_FontsTable[2][2][2][2][7];
205     wxString m_FontsFacesTable[2][2][2][2][7];
206 #if !wxUSE_UNICODE
207     wxFontEncoding m_FontsEncTable[2][2][2][2][7];
208 #endif
209             // table of loaded fonts. 1st four indexes are 0 or 1, depending on on/off
210             // state of these flags (from left to right):
211             // [bold][italic][underlined][fixed_size]
212             // last index is font size : from 0 to 6 (remapped from html sizes 1 to 7)
213             // Note : this table covers all possible combinations of fonts, but not
214             // all of them are used, so many items in table are usually NULL.
215     int m_FontsSizes[7];
216     wxString m_FontFaceFixed, m_FontFaceNormal;
217             // html font sizes and faces of fixed and proportional fonts
218 
219 #if !wxUSE_UNICODE
220     wxFontEncoding m_InputEnc, m_OutputEnc;
221             // I/O font encodings
222     wxEncodingConverter *m_EncConv;
223 #endif
224 
225     // NB: this pointer replaces m_lastWordCell pointer in wx<=2.8.7; this
226     //     way, wxHtmlWinParser remains ABI compatible with older versions
227     //     despite addition of two fields in wxHtmlWinParser_TextParsingState
228     wxHtmlWinParser_TextParsingState *m_textParsingState;
229 
230     DECLARE_NO_COPY_CLASS(wxHtmlWinParser)
231 };
232 
233 
234 
235 
236 
237 
238 //-----------------------------------------------------------------------------
239 // wxHtmlWinTagHandler
240 //                  This is basicly wxHtmlTagHandler except
241 //                  it is extended with protected member m_Parser pointing to
242 //                  the wxHtmlWinParser object
243 //-----------------------------------------------------------------------------
244 
245 class WXDLLIMPEXP_HTML wxHtmlWinTagHandler : public wxHtmlTagHandler
246 {
DECLARE_ABSTRACT_CLASS(wxHtmlWinTagHandler)247     DECLARE_ABSTRACT_CLASS(wxHtmlWinTagHandler)
248 
249 public:
250     wxHtmlWinTagHandler() : wxHtmlTagHandler() {}
251 
SetParser(wxHtmlParser * parser)252     virtual void SetParser(wxHtmlParser *parser) {wxHtmlTagHandler::SetParser(parser); m_WParser = (wxHtmlWinParser*) parser;}
253 
254 protected:
255     wxHtmlWinParser *m_WParser; // same as m_Parser, but overcasted
256 
257     DECLARE_NO_COPY_CLASS(wxHtmlWinTagHandler)
258 };
259 
260 
261 
262 
263 
264 
265 //----------------------------------------------------------------------------
266 // wxHtmlTagsModule
267 //                  This is basic of dynamic tag handlers binding.
268 //                  The class provides methods for filling parser's handlers
269 //                  hash table.
270 //                  (See documentation for details)
271 //----------------------------------------------------------------------------
272 
273 class WXDLLIMPEXP_HTML wxHtmlTagsModule : public wxModule
274 {
DECLARE_DYNAMIC_CLASS(wxHtmlTagsModule)275     DECLARE_DYNAMIC_CLASS(wxHtmlTagsModule)
276 
277 public:
278     wxHtmlTagsModule() : wxModule() {}
279 
280     virtual bool OnInit();
281     virtual void OnExit();
282 
283     // This is called by wxHtmlWinParser.
284     // The method must simply call parser->AddTagHandler(new
285     // <handler_class_name>); for each handler
FillHandlersTable(wxHtmlWinParser * WXUNUSED (parser))286     virtual void FillHandlersTable(wxHtmlWinParser * WXUNUSED(parser)) { }
287 };
288 
289 
290 #endif
291 
292 #endif // _WX_WINPARS_H_
293