wxhtml overviewthis topic was written by vaclav slavik, the author of the wxhtml library. the wxhtml library provides classes for parsing and displaying html. it is not intended to be a high-end html browser. if you are looking for something like that try http://www.mozilla.org. wxhtml can be used as a generic rich text viewer - for example to display a nice about box (like those of gnome apps) or to display the result of database searching. there is a wxfilesystem class which allows you to use your own virtual file systems. wxhtmlwindow supports tag handlers. this means that you can easily extend wxhtml library with new, unsupported tags. not only that, you can even use your own application-specific tags! see src/html/m_*.cpp files for details. there is a generic wxhtmlparser class, independent of wxhtmlwindow.
wxhtml quick start
wxhtml quick startdisplaying html first of all, you must include <wx/wxhtml.h>. class wxhtmlwindow (derived from wxscrolledwindow) is used to display html documents. it has two important methods: loadpage and setpage. loadpage loads and displays html file while setpage displays directly the passed string. see the example:
mywin -> loadpage("test.htm"); mywin -> setpage("<html><body>" "<h1>error</h1>" "some error occurred :-h)" "</body></hmtl>");i think the difference is quite clear. displaying help see wxhtmlhelpcontroller. setting up wxhtmlwindow because wxhtmlwindow is derived from wxscrolledwindow and not from wxframe, it doesn't have visible frame. but the user usually wants to see the title of html page displayed somewhere and the frame's titlebar is the ideal place for it. wxhtmlwindow provides 2 methods in order to handle this: setrelatedframe and setrelatedstatusbar. see the example:
html = new wxhtmlwindow(this); html -> setrelatedframe(this, "html : %%s"); html -> setrelatedstatusbar(0);the first command associates the html object with its parent frame (this points to wxframe object there) and sets the format of the title. page title "hello, world!" will be displayed as "html : hello, world!" in this example. the second command sets which frame's status bar should be used to display browser's messages (such as "loading..." or "done" or hypertext links). customizing wxhtmlwindow you can customize wxhtmlwindow by setting font size, font face and borders (space between border of window and displayed html). related functions:
the last two functions are used to store user customization info wxconfig stuff (for example in the registry under windows, or in a dotfile under unix).
html printingthe wxhtml library provides printing facilities with several levels of complexity. the easiest way to print an html document is to use wxhtmleasyprinting class. it lets you print html documents with only one command and you don't have to worry about deriving from the wxprintout class at all. it is only a simple wrapper around the wxhtmlprintout, normal wxwidgets printout class. and finally there is the low level class wxhtmldcrenderer which you can use to render html into a rectangular area on any dc. it supports rendering into multiple rectangles with the same width. (the most common use of this is placing one rectangle on each page or printing into two columns.)
help files formatwxhtml library uses a reduced version of ms html workshop format. tex2rtf can produce these files when generating html, if you set htmlworkshopfiles to true in your tex2rtf.ini file. (see wxhtmlhelpcontroller for help controller description.) a book consists of three files: header file, contents file and index file. you can make a regular zip archive of these files, plus the html and any image files, for wxhtml (or helpview) to read; and the .zip file can optionally be renamed to .htb. header file (.hhp) header file must contain these lines (and may contain additional lines which are ignored) :
contents file=<filename.hhc> index file=<filename.hhk> title=<title of your book> default topic=<default page to be displayed.htm>all filenames (including the default topic) are relative to the location of .hhp file. localization note: in addition, .hhp file may contain line
charset=<rfc_charset>which specifies what charset (e.g. "iso8859_1") was used in contents and index files. please note that this line is incompatible with ms html help workshop and it would either silently remove it or complain with some error. see also writing non-english applications. contents file (.hhc) contents file has html syntax and it can be parsed by regular html parser. it contains exactly one list (<ul>....</ul> statement):
<ul> <li> <object type="text/sitemap"> <param name="name" value="@topic name@"> <param name="id" value=@numeric_id@> <param name="local" value="@filename.htm@"> </object> <li> <object type="text/sitemap"> <param name="name" value="@topic name@"> <param name="id" value=@numeric_id@> <param name="local" value="@filename.htm@"> </object> ... </ul>you can modify value attributes of param tags. topic name is name of chapter/topic as is displayed in contents, filename.htm is html page name (relative to .hhp file) and numeric_id is optional - it is used only when you use wxhtmlhelpcontroller::display(int) items in the list may be nested - one <li> statement may contain a <ul> sub-statement:
<ul> <li> <object type="text/sitemap"> <param name="name" value="top node"> <param name="local" value="top.htm"> </object> <ul> <li> <object type="text/sitemap"> <param name="name" value="subnode in topnode"> <param name="local" value="subnode1.htm"> </object> ... </ul> <li> <object type="text/sitemap"> <param name="name" value="another top"> <param name="local" value="top2.htm"> </object> ... </ul>index file (.hhk) index files have same format as contents file except that id params are ignored and sublists are not allowed.
input filtersthe wxhtml library provides a mechanism for reading and displaying files of many different file formats. wxhtmlwindow::loadpage can load not only html files but any known file. to make a file type known to wxhtmlwindow you must create a wxhtmlfilter filter and register it using wxhtmlwindow::addfilter.
cells and containersthis article describes mechanism used by wxhtmlwinparser and wxhtmlwindow to parse and display html documents. cells you can divide any text (or html) into small fragments. let's call these fragments cells. cell is for example one word, horizontal line, image or any other part of document. each cell has width and height (except special "magic" cells with zero dimensions - e.g. colour changers or font changers). see wxhtmlcell. containers container is kind of cell that may contain sub-cells. its size depends on number and sizes of its sub-cells (and also depends on width of window). see wxhtmlcontainercell, wxhtmlcell::layout. using containers in tag handler wxhtmlwinparser provides a user-friendly way of managing containers. it is based on the idea of opening and closing containers. use opencontainer to open new a container within an already opened container. this new container is a sub-container of the old one. (if you want to create a new container with the same depth level you can call closecontainer(); opencontainer();.) use closecontainer to close the container. this doesn't create a new container with same depth level but it returns "control" to the parent container. it is clear there must be same number of calls to opencontainer as to closecontainer... example this code creates a new paragraph (container at same depth level) with "hello, world!":
m_wparser -> closecontainer(); c = m_wparser -> opencontainer(); m_wparser -> addword("hello, "); m_wparser -> addword("world!"); m_wparser -> closecontainer(); m_wparser -> opencontainer();you can see that there was opened container before running the code. we closed it, created our own container, then closed our container and opened new container. the result was that we had same depth level after executing. this is general rule that should be followed by tag handlers: leave depth level of containers unmodified (in other words, number of opencontainer and closecontainer calls should be same within handletag's body).
tag handlersthe wxhtml library provides architecture of pluggable tag handlers. tag handler is class that understands particular html tag (or tags) and is able to interpret it. wxhtmlwinparser has static table of modules. each module contains one or more tag handlers. each time a new wxhtmlwinparser object is constructed all modules are scanned and handlers are added to wxhtmlparser's list of available handlers (note: wxhtmlparser's list is non-static). how it works common tag handler's handletag method works in four steps:
see wxhtmlwinparser for methods for modifying parser's state. in general you can do things like opening/closing containers, changing colors, fonts etc. providing own tag handlers you should create new .cpp file and place following lines into it:
#include <mod_templ.h> #include <forcelink.h> force_link_me(yourmodulefilenamewithoutcpp)then you must define handlers and one module. tag handlers the handler is derived from wxhtmlwintaghandler (or directly from wxhtmltaghandler) you can use set of macros to define the handler (see src/html/m_*.cpp files for details). handler definition must start with tag_handler_begin macro and end with tag_handler_end macro. i strongly recommend to have a look at include/wxhtml/mod_templ.h file. otherwise you won't understand the structure of macros. see macros reference: tag_handler_begin(name, tags) starts handler definition. name is handler identifier (in fact part of class name), tags is string containing list of tags supported by this handler (in uppercase). this macro derives new class from wxhtmlwintaghandler and implements it is getsupportedtags method. example: tag_handler_begin(fonts, "b,i,u,t") tag_handler_vars this macro starts block of variables definitions. (variables are identical to class attributes.) example:
tag_handler_begin(vars_only, "crazytag") tag_handler_vars int my_int_var; wxstring something_else; tag_handler_end(vars_only)this macro is used only in rare cases. tag_handler_constr(name) this macro supplies object constructor. name is same name as the one from tag_handler_begin macro. body of constructor follow after this macro (you must use and ). example:
tag_handler_begin(vars2, "crazytag") tag_handler_vars int my_int_var; tag_handler_constr(vars2) { // !!!!!! my_int_var = 666; } // !!!!!! tag_handler_end(vars2)never used in wxhtml :-) tag_handler_proc(varib) this is very important macro. it defines handletag method. varib is name of parameter passed to the method, usually tag. body of method follows after this macro. note than you must use and ! example:
tag_handler_begin(title, "title") tag_handler_proc(tag) { printf("title found...\n"); } tag_handler_end(title)tag_handler_end(name) ends definition of tag handler name. tags modules you can use set of 3 macros tags_module_begin, tags_module_add and tags_module_end to inherit new module from wxhtmltagsmodule and to create instance of it. see macros reference: tags_module_begin(modname) begins module definition. modname is part of class name and must be unique. tags_module_add(name) adds the handler to this module. name is the identifier from tag_handler_begin. tags_module_end(modname) ends the definition of module. example:
tags_module_begin(examples) tags_module_add(vars_only) tags_module_add(vars2) tags_module_add(title) tags_module_end(examples) tags supported by wxhtmlwxhtml is not full implementation of html standard. instead, it supports most common tags so that it is possible to display simple html documents with it. (for example it works fine with pages created in netscape composer or generated by tex2rtf). following tables list all tags known to wxhtml, together with supported parameters. a tag has general form of <tagname param_1 param_2 ... param_n> where param_i is either paramname="paramvalue" or paramname=paramvalue - these two are equivalent. unless stated otherwise, wxhtml is case-insensitive. table of common parameter values we will use these substitutions in tags descriptions:
[alignment] center left right justify [v_alignment] top bottom center [color] html 4.0-compliant colour specification [fontsize] -2 -1 +0 +1 +2 +3 +4 1 2 3 4 5 6 7 [pixels] integer value that represents dimension in pixels [percent] i% where i is integer [url] an url [string] text string [coords] c(1),c(2),c(3),...,c(n) where c(i) is integerlist of supported tags
a name=[string] href=[url] target=[target window spec] address area shape=poly shape=circle shape=rect coords=[coords] href=[url] b big blockquote body text=[color] link=[color] bgcolor=[color] br align=[alignment] center cite code dd div align=[alignment] dl dt em font color=[color] size=[fontsize] face=[comma-separated list of facenames] hr align=[alignment] size=[pixels] width=[percent|pixels] noshade h1 h2 h3 h4 h5 h6 i img src=[url] width=[pixels] height=[pixels] align=texttop align=center align=abscenter align=bottom usemap=[url] kbd li map name=[string] meta http-equiv="content-type" content=[string] ol p align=[alignment] pre samp small strike strong sub sup table align=[alignment] width=[percent|pixels] border=[pixels] valign=[v_alignment] bgcolor=[color] cellspacing=[pixels] cellpadding=[pixels] td align=[alignment] valign=[v_alignment] bgcolor=[color] width=[percent|pixels] colspan=[pixels] rowspan=[pixels] nowrap th align=[alignment] valign=[v_alignment] bgcolor=[color] width=[percent|pixels] colspan=[pixels] rowspan=[pixels] title tr align=[alignment] valign=[v_alignment] bgcolor=[color] tt u ul |