winpars.h 11 KB

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