html.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: html.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_html wxHTML Overview
  9. @tableofcontents
  10. The wxHTML library provides classes for parsing and displaying HTML.
  11. It is not intended to be a high-end HTML browser. If you are looking for
  12. something like that try <http://www.mozilla.org/>.
  13. wxHTML can be used as a generic rich text viewer - for example to display
  14. a nice About Box (like those of GNOME apps) or to display the result of
  15. database searching. There is a wxFileSystem class which allows you to use
  16. your own virtual file systems.
  17. wxHtmlWindow supports tag handlers. This means that you can easily
  18. extend wxHtml library with new, unsupported tags. Not only that,
  19. you can even use your own application-specific tags!
  20. See @c src/html/m_*.cpp files for details.
  21. There is a generic wxHtmlParser class, independent of wxHtmlWindow.
  22. @section overview_html_quickstart wxHTML Quick Start
  23. @subsection overview_html_quickstart_disphtml Displaying HTML
  24. First of all, you must include @c wx/wxhtml.h.
  25. Class wxHtmlWindow (derived from ::wxScrolledWindow) is used to display HTML
  26. documents.
  27. It has two important methods: wxHtmlWindow::LoadPage and wxHtmlWindow::SetPage.
  28. LoadPage loads and displays HTML file while SetPage displays directly the
  29. passed @b string. See the example:
  30. @code
  31. mywin->LoadPage("test.htm");
  32. mywin->SetPage("htmlbody"
  33. "h1Error/h1"
  34. "Some error occurred :-H)"
  35. "/body/hmtl");
  36. @endcode
  37. @subsection overview_html_quickstart_settingup Setting up wxHtmlWindow
  38. Because wxHtmlWindow is derived from ::wxScrolledWindow and not from
  39. wxFrame, it doesn't have visible frame. But the user usually wants to see
  40. the title of HTML page displayed somewhere and the frame's titlebar is
  41. the ideal place for it.
  42. wxHtmlWindow provides 2 methods in order to handle this:
  43. wxHtmlWindow::SetRelatedFrame and wxHtmlWindow::SetRelatedStatusBar.
  44. See the example:
  45. @code
  46. html = new wxHtmlWindow(this);
  47. html->SetRelatedFrame(this, "HTML : %%s");
  48. html->SetRelatedStatusBar(0);
  49. @endcode
  50. The first command associates the HTML object with its parent frame
  51. (this points to wxFrame object there) and sets the format of the title.
  52. Page title "Hello, world!" will be displayed as "HTML : Hello, world!"
  53. in this example.
  54. The second command sets which frame's status bar should be used to display
  55. browser's messages (such as "Loading..." or "Done" or hypertext links).
  56. @subsection overview_html_quickstart_custom Customizing wxHtmlWindow
  57. You can customize wxHtmlWindow by setting font size, font face and
  58. borders (space between border of window and displayed HTML). Related functions:
  59. @li wxHtmlWindow::SetFonts
  60. @li wxHtmlWindow::SetBorders
  61. @li wxHtmlWindow::ReadCustomization
  62. @li wxHtmlWindow::WriteCustomization
  63. The last two functions are used to store user customization info wxConfig stuff
  64. (for example in the registry under Windows, or in a dotfile under Unix).
  65. @section overview_html_printing HTML Printing
  66. The wxHTML library provides printing facilities with several levels of
  67. complexity. The easiest way to print an HTML document is to use the
  68. wxHtmlEasyPrinting class.
  69. It lets you print HTML documents with only one command and you don't have to
  70. worry about deriving from the wxPrintout class at all. It is only a simple
  71. wrapper around the wxHtmlPrintout, normal wxWidgets printout class.
  72. And finally there is the low level class wxHtmlDCRenderer which you can use to
  73. render HTML into a rectangular area on any DC. It supports rendering into
  74. multiple rectangles with the same width. The most common use of this is placing
  75. one rectangle on each page or printing into two columns.
  76. @section overview_html_helpformats Help Files Format
  77. wxHTML library can be used to show an help manual to the user; in fact, it
  78. supports natively (through wxHtmlHelpController) a reduced version of MS HTML
  79. Workshop format.
  80. A @b book consists of three files: the header file, the contents file
  81. and the index file.
  82. You can make a regular zip archive of these files, plus the HTML and any image
  83. files, for wxHTML (or helpview) to read; and the @c ".zip" file can optionally
  84. be renamed to @c ".htb".
  85. @subsection overview_html_helpformats_hhp Header file (.hhp)
  86. The header file must contain these lines (and may contain additional lines
  87. which are ignored):
  88. @code
  89. Contents file=filename.hhc
  90. Index file=filename.hhk
  91. Title=title of your book
  92. Default topic=default page to be displayed.htm
  93. @endcode
  94. All filenames (including the Default topic) are relative to the
  95. location of the @c ".hhp" file.
  96. @note For localization, in addition the @c ".hhp" file may contain the line
  97. @code
  98. Charset=rfc_charset
  99. @endcode
  100. which specifies what charset (e.g. "iso8859_1") was used in contents
  101. and index files. Please note that this line is incompatible with
  102. MS HTML Help Workshop and it would either silently remove it or complain
  103. with some error. See also @ref overview_nonenglish.
  104. @subsection overview_html_helpformats_hhc Contents file (.hhc)
  105. Contents file has HTML syntax and it can be parsed by regular HTML parser.
  106. It contains exactly one list (@c &lt;ul&gt;....@c &lt;/ul&gt; statement):
  107. @code
  108. <ul>
  109. <li><object type="text/sitemap">
  110. <param name="Name" value="@topic name@">
  111. <param name="ID" value=@numeric_id@>
  112. <param name="Local" value="@filename.htm@">
  113. </object>
  114. <li><object type="text/sitemap">
  115. <param name="Name" value="@topic name@">
  116. <param name="ID" value=@numeric_id@>
  117. <param name="Local" value="@filename.htm@">
  118. </object>
  119. ...
  120. </ul>
  121. @endcode
  122. You can modify value attributes of param tags. The <em>topic name</em> is name
  123. of chapter/topic as is displayed in contents, <em>filename.htm</em> is the HTML
  124. page name (relative to the @c ".hhp" file) and <em>numeric_id</em> is optional,
  125. it is used only when you use wxHtmlHelpController::Display(int).
  126. Items in the list may be nested - one @c &lt;li&gt; statement may contain a
  127. @c &lt;ul&gt; sub-statement:
  128. @code
  129. <ul>
  130. <li><object type="text/sitemap">
  131. <param name="Name" value="Top node">
  132. <param name="Local" value="top.htm">
  133. </object>
  134. <ul>
  135. <li><object type="text/sitemap">
  136. <param name="Name" value="subnode in topnode">
  137. <param name="Local" value="subnode1.htm">
  138. </object>
  139. ...
  140. </ul>
  141. <li><object type="text/sitemap">
  142. <param name="Name" value="Another Top">
  143. <param name="Local" value="top2.htm">
  144. </object>
  145. ...
  146. </ul>
  147. @endcode
  148. @subsection overview_html_helpformats_hhk Index Files (.hhk)
  149. Index files have same format as contents files except that ID params are
  150. ignored and sublists are @b not allowed.
  151. @section overview_html_filters Input Filters
  152. The wxHTML library provides a mechanism for reading and displaying
  153. files of many different file formats.
  154. wxHtmlWindow::LoadPage can load not only HTML files but any known file.
  155. To make a file type known to wxHtmlWindow you must create a wxHtmlFilter filter and
  156. register it using wxHtmlWindow::AddFilter.
  157. @section overview_html_cells Cells and Containers
  158. This article describes mechanism used by wxHtmlWinParser and
  159. wxHtmlWindow to parse and display HTML documents.
  160. @subsection overview_html_cells_cells Cells
  161. You can divide any text (or HTML) into small fragments. Let's call these
  162. fragments @b cells. Cell is for example one word, horizontal line, image
  163. or any other part of document. Each cell has width and height (except special
  164. "magic" cells with zero dimensions - e.g. colour changers or font changers).
  165. See wxHtmlCell.
  166. @subsection overview_html_cells_containers Containers
  167. Container is kind of cell that may contain sub-cells. Its size depends
  168. on number and sizes of its sub-cells (and also depends on width of window).
  169. See wxHtmlContainerCell, wxHtmlCell::Layout. This image shows the cells and
  170. containers:
  171. @image html overview_html_contbox.png
  172. @subsection overview_html_cells_conttaghandler Using Containers in Tag Handler
  173. wxHtmlWinParser provides a user-friendly way of managing containers.
  174. It is based on the idea of opening and closing containers.
  175. Use wxHtmlWinParser::OpenContainer to open new a container @e within an already
  176. opened container.
  177. This new container is a @e sub-container of the old one. (If you want to create a
  178. new container with the same depth level you can call @c CloseContainer(); OpenContainer();.)
  179. Use wxHtmlWinParser::CloseContainer to close the container.
  180. This doesn't create a new container with same depth level but it returns "control"
  181. to the parent container. See explanation:
  182. @image html overview_html_cont.png
  183. There clearly must be same number of calls to OpenContainer as to
  184. CloseContainer.
  185. This code creates a new paragraph (container at same depth level) with
  186. "Hello, world!":
  187. @code
  188. m_WParser->CloseContainer();
  189. c = m_WParser->OpenContainer();
  190. m_WParser->AddText("Hello, ");
  191. m_WParser->AddText("world!");
  192. m_WParser->CloseContainer();
  193. m_WParser->OpenContainer();
  194. @endcode
  195. and here is image of the situation:
  196. @image html overview_html_hello.png
  197. You can see that there was an opened container before the code was executed.
  198. We closed it, created our own container, then closed our container and opened
  199. new container.
  200. The result was that we had @e same depth level after executing. This is general
  201. rule that should be followed by tag handlers: leave depth level of containers
  202. unmodified (in other words, number of OpenContainer and CloseContainer calls
  203. should be same within wxHtmlTagHandler::HandleTag's body).
  204. Notice that it would be usually better to use wxHtmlContainerCell::InsertCell
  205. instead of adding text to the parser directly.
  206. @section overview_html_handlers Tag Handlers
  207. The wxHTML library provides architecture of pluggable @e tag handlers.
  208. Tag handler is class that understands particular HTML tag (or tags) and is
  209. able to interpret it.
  210. wxHtmlWinParser has a static table of @b modules.
  211. Each module contains one or more tag handlers. Each time a new wxHtmlWinParser
  212. object is constructed all modules are scanned and handlers are added
  213. to wxHtmlParser's list of available handlers (note: wxHtmlParser's list
  214. is non-static).
  215. @subsection overview_html_handlers_howworks How it works
  216. Common tag handler's wxHtmlTagHandler::HandleTag method works in four steps:
  217. @li Save state of parent parser into local variables
  218. @li Change parser state according to tag's params
  219. @li Parse text between the tag and paired ending tag (if present)
  220. @li Restore original parser state
  221. See wxHtmlWinParser for methods for modifying parser's state.
  222. In general you can do things like opening/closing containers, changing colors, fonts etc.
  223. @subsection overview_html_handlers_custom Providing own tag handlers
  224. You should create a new .cpp file and place the following lines into it:
  225. @code
  226. #include <mod_templ.h>
  227. #include <forcelink.h>
  228. FORCE_LINK_ME(yourmodulefilenamewithoutcpp)
  229. @endcode
  230. Then you must define handlers and one module.
  231. @subsection overview_html_handlers_tag Tag handlers
  232. The handler is derived from wxHtmlWinTagHandler (or directly from wxHtmlTagHandler).
  233. You can use set of macros to define the handler (see src/html/m_*.cpp files
  234. for details). Handler definition must start with @b TAG_HANDLER_BEGIN macro
  235. and end with @b TAG_HANDLER_END macro.
  236. I strongly recommend to have a look at @e include/wxhtml/mod_templ.h file.
  237. Otherwise you won't understand the structure of macros.
  238. See macros reference:
  239. @li @b TAG_HANDLER_BEGIN(@e name, @e tags):
  240. Starts handler definition. @e name is handler identifier (in fact
  241. part of class name), @e tags is string containing list of tags
  242. supported by this handler (in uppercase). This macro derives new class from
  243. wxHtmlWinTagHandler and implements it is wxHtmlTagHandler::GetSupportedTags method.
  244. Example: TAG_HANDLER_BEGIN(FONTS, "B,I,U,T")
  245. @li @b TAG_HANDLER_VARS:
  246. This macro starts block of variables definitions. (Variables are identical
  247. to class attributes.) Example:
  248. @code
  249. TAG_HANDLER_BEGIN(VARS_ONLY, "CRAZYTAG")
  250. TAG_HANDLER_VARS
  251. int my_int_var;
  252. wxString something_else;
  253. TAG_HANDLER_END(VARS_ONLY)
  254. @endcode
  255. This macro is used only in rare cases.
  256. @li @b TAG_HANDLER_CONSTR(@e name):
  257. This macro supplies object constructor. @e name is same name as the one
  258. from TAG_HANDLER_BEGIN macro. Body of constructor follow after
  259. this macro (you must use { and } ). Example:
  260. @code
  261. TAG_HANDLER_BEGIN(VARS2, "CRAZYTAG")
  262. TAG_HANDLER_VARS
  263. int my_int_var;
  264. TAG_HANDLER_CONSTR(vars2)
  265. { // !!!!!!
  266. my_int_var = 666;
  267. } // !!!!!!
  268. TAG_HANDLER_END(VARS2)
  269. @endcode
  270. Never used in wxHTML :-)
  271. @li @b TAG_HANDLER_PROC(@e varib):
  272. This is very important macro. It defines wxHtmlTagHandler::HandleTag
  273. method. @e varib is name of parameter passed to the method, usually
  274. @e tag. Body of method follows after this macro.
  275. Note than you must use { and } !
  276. Example:
  277. @code
  278. TAG_HANDLER_BEGIN(TITLE, "TITLE")
  279. TAG_HANDLER_PROC(tag)
  280. {
  281. printf("TITLE found...\n");
  282. }
  283. TAG_HANDLER_END(TITLE)
  284. @endcode
  285. @li @b TAG_HANDLER_END(@e name):
  286. Ends definition of tag handler @e name.
  287. @subsection overview_html_handlers_modules Tags Modules
  288. You can use set of 3 macros TAGS_MODULE_BEGIN, TAGS_MODULE_ADD and
  289. TAGS_MODULE_END to inherit new module from
  290. wxHtmlTagsModule and to create instance of it.
  291. See macros reference:
  292. @li @b TAGS_MODULE_BEGIN(@e modname):
  293. Begins module definition. @e modname is part of class name and must be unique.
  294. @li @b TAGS_MODULE_ADD(@e name):
  295. Adds the handler to this module. @e name is the identifier from TAG_HANDLER_BEGIN.
  296. @li @b TAGS_MODULE_END(@e modname):
  297. Ends the definition of module.
  298. Example:
  299. @code
  300. TAGS_MODULE_BEGIN(Examples)
  301. TAGS_MODULE_ADD(VARS_ONLY)
  302. TAGS_MODULE_ADD(VARS2)
  303. TAGS_MODULE_ADD(TITLE)
  304. TAGS_MODULE_END(Examples)
  305. @endcode
  306. @section overview_html_supptags Supported HTML Tags
  307. wxHTML is not full implementation of HTML standard. Instead, it supports most
  308. common tags so that it is possible to display @e simple HTML documents with it.
  309. (For example it works fine with pages created in Netscape Composer or generated
  310. by tex2rtf).
  311. Following tables list all tags known to wxHTML, together with supported
  312. parameters.
  313. A tag has general form of @c tagname param_1 param_2 ... param_n where param_i
  314. is either @c paramname="paramvalue" or @c paramname=paramvalue - these two are
  315. equivalent. Unless stated otherwise, wxHTML is case-insensitive.
  316. @subsection overview_html_supptags_commonvalues Common Parameter Values
  317. We will use these substitutions in tags descriptions:
  318. @code
  319. [alignment] CENTER
  320. LEFT
  321. RIGHT
  322. JUSTIFY
  323. [v_alignment] TOP
  324. BOTTOM
  325. CENTER
  326. [color] HTML 4.0-compliant colour specification
  327. [fontsize] -2
  328. -1
  329. +0
  330. +1
  331. +2
  332. +3
  333. +4
  334. 1
  335. 2
  336. 3
  337. 4
  338. 5
  339. 6
  340. 7
  341. [pixels] integer value that represents dimension in pixels
  342. [percent] i%
  343. where i is integer
  344. [url] an URL
  345. [string] text string
  346. [coords] c(1),c(2),c(3),...,c(n)
  347. where c(i) is integer
  348. @endcode
  349. @subsection overview_html_supptags_list List of Supported Tags
  350. @code
  351. A NAME=[string]
  352. HREF=[url]
  353. TARGET=[target window spec]
  354. ADDRESS
  355. AREA SHAPE=POLY
  356. SHAPE=CIRCLE
  357. SHAPE=RECT
  358. COORDS=[coords]
  359. HREF=[url]
  360. B
  361. BIG
  362. BLOCKQUOTE
  363. BODY TEXT=[color]
  364. LINK=[color]
  365. BGCOLOR=[color]
  366. BR ALIGN=[alignment]
  367. CENTER
  368. CITE
  369. CODE
  370. DD
  371. DIV ALIGN=[alignment]
  372. DL
  373. DT
  374. EM
  375. FONT COLOR=[color]
  376. SIZE=[fontsize]
  377. FACE=[comma-separated list of facenames]
  378. HR ALIGN=[alignment]
  379. SIZE=[pixels]
  380. WIDTH=[percent|pixels]
  381. NOSHADE
  382. H1
  383. H2
  384. H3
  385. H4
  386. H5
  387. H6
  388. I
  389. IMG SRC=[url]
  390. WIDTH=[percent|pixels]
  391. HEIGHT=[pixels]
  392. ALIGN=TEXTTOP
  393. ALIGN=CENTER
  394. ALIGN=ABSCENTER
  395. ALIGN=BOTTOM
  396. USEMAP=[url]
  397. KBD
  398. LI
  399. MAP NAME=[string]
  400. META HTTP-EQUIV="Content-Type"
  401. CONTENT=[string]
  402. OL
  403. P ALIGN=[alignment]
  404. PRE
  405. SAMP
  406. SMALL
  407. SPAN
  408. STRIKE
  409. STRONG
  410. SUB
  411. SUP
  412. TABLE ALIGN=[alignment]
  413. WIDTH=[percent|pixels]
  414. BORDER=[pixels]
  415. VALIGN=[v_alignment]
  416. BGCOLOR=[color]
  417. CELLSPACING=[pixels]
  418. CELLPADDING=[pixels]
  419. TD ALIGN=[alignment]
  420. VALIGN=[v_alignment]
  421. BGCOLOR=[color]
  422. WIDTH=[percent|pixels]
  423. COLSPAN=[pixels]
  424. ROWSPAN=[pixels]
  425. NOWRAP
  426. TH ALIGN=[alignment]
  427. VALIGN=[v_alignment]
  428. BGCOLOR=[color]
  429. WIDTH=[percent|pixels]
  430. COLSPAN=[pixels]
  431. ROWSPAN=[pixels]
  432. TITLE
  433. TR ALIGN=[alignment]
  434. VALIGN=[v_alignment]
  435. BGCOLOR=[color]
  436. TT
  437. U
  438. UL
  439. @endcode
  440. @subsection overview_html_suppstyles_list Supported Styles
  441. wxHTML doesn't really have CSS support but it does support a few simple styles:
  442. you can use @c "text-align", @c "width", @c "vertical-align" and @c
  443. "background" with all elements and for @c SPAN elements a few other styles are
  444. additionally recognized:
  445. - @c color
  446. - @c font-family
  447. - @c font-size (only in point units)
  448. - @c font-style (only "oblique", "italic" and "normal" values are supported)
  449. - @c font-weight (only "bold" and "normal" values are supported)
  450. - @c text-decoration (only "underline" value is supported)
  451. */