]>
Commit | Line | Data |
---|---|---|
bd330a69 | 1 | \subsection{Tag Handlers}\label{handlers} |
704a4b75 | 2 | |
448af9a4 | 3 | The wxHTML library provides architecture of pluggable {\it tag handlers}. |
704a4b75 VS |
4 | Tag handler is class that understands particular HTML tag (or tags) and is |
5 | able to interpret it. | |
6 | ||
7 | \helpref{wxHtmlWinParser}{wxhtmlwinparser} has static table of {\bf modules}. | |
448af9a4 | 8 | Each module contains one or more tag handlers. Each time a new wxHtmlWinParser |
704a4b75 | 9 | object is constructed all modules are scanned and handlers are added |
448af9a4 | 10 | to wxHtmlParser's list of available handlers (note: wxHtmlParser's list |
704a4b75 VS |
11 | is non-static). |
12 | ||
13 | \wxheading{How it works} | |
14 | ||
15 | Common tag handler's \helpref{HandleTag}{wxhtmltaghandlerhandletag} method | |
16 | works in four steps: | |
17 | ||
448af9a4 | 18 | \begin{enumerate}\itemsep=0pt |
704a4b75 VS |
19 | \item Save state of parent parser into local variables |
20 | \item Change parser state according to tag's params | |
21 | \item Parse text between the tag and paired ending tag (if present) | |
22 | \item Restore original parser state | |
23 | \end{enumerate} | |
24 | ||
25 | See \helpref{wxHtmlWinParser}{wxhtmlwinparser} for methods for modifying | |
26 | parser's state. In general you can do things like opening/closing containers, | |
27 | changing colors, fonts etc. | |
28 | ||
29 | \wxheading{Providing own tag handlers} | |
30 | ||
31 | You should create new .cpp file and place following lines into it: | |
32 | ||
33 | \begin{verbatim} | |
34 | #include <mod_templ.h> | |
35 | #include <forcelink.h> | |
36 | FORCE_LINK_ME(yourmodulefilenamewithoutcpp) | |
37 | \end{verbatim} | |
38 | ||
39 | Then you must define handlers and one module. | |
40 | ||
41 | \wxheading{Tag handlers} | |
42 | ||
43 | The handler is derived from \helpref{wxHtmlWinTagHandler}{wxhtmlwintaghandler} | |
44 | (or directly from \helpref{wxHtmlTagHandler}{wxhtmltaghandler}) | |
45 | ||
22d6efa8 JS |
46 | You can use set of macros to define the handler (see src/mod\_*.cpp files |
47 | for details). Handler definition must start with {\bf TAG\_HANDLER\_BEGIN} macro | |
48 | and end with {\bf TAG\_HANDLER\_END} macro. I strongly recommend to have a look | |
49 | at {\it include/wxhtml/mod\_templ.h} file. Otherwise you won't understand | |
448af9a4 | 50 | the structure of macros. See macros reference: |
704a4b75 | 51 | |
22d6efa8 | 52 | {\bf TAG\_HANDLER\_BEGIN}({\it name}, {\it tags}) |
704a4b75 VS |
53 | |
54 | Starts handler definition. {\it name} is handler identifier (in fact | |
55 | part of class name), {\it tags} is string containing list of tags | |
56 | supported by this handler (in uppercase). This macro derives new class from | |
f6bcfd97 | 57 | wxHtmlWinTagHandler and implements it is |
704a4b75 VS |
58 | \helpref{GetSupportedTags}{wxhtmltaghandlergetsupportedtags} method. |
59 | ||
22d6efa8 | 60 | Example: TAG\_HANDLER\_BEGIN(FONTS, "B,I,U,T") |
704a4b75 | 61 | |
22d6efa8 | 62 | {\bf TAG\_HANDLER\_VARS} |
704a4b75 VS |
63 | |
64 | This macro starts block of variables definitions. (Variables are identical | |
65 | to class attributes.) Example: | |
66 | ||
67 | \begin{verbatim} | |
68 | TAG_HANDLER_BEGIN(VARS_ONLY, "CRAZYTAG") | |
69 | TAG_HANDLER_VARS | |
70 | int my_int_var; | |
71 | wxString something_else; | |
72 | TAG_HANDLER_END(VARS_ONLY) | |
73 | \end{verbatim} | |
74 | ||
75 | This macro is used only in rare cases. | |
76 | ||
22d6efa8 | 77 | {\bf TAG\_HANDLER\_CONSTR}({\it name}) |
704a4b75 VS |
78 | |
79 | This macro supplies object constructor. {\it name} is same name as the one | |
22d6efa8 | 80 | from TAG\_HANDLER\_BEGIN macro. Body of constructor follow after |
704a4b75 VS |
81 | this macro (you must use { and } ). Example: |
82 | ||
83 | \begin{verbatim} | |
84 | TAG_HANDLER_BEGIN(VARS2, "CRAZYTAG") | |
85 | TAG_HANDLER_VARS | |
86 | int my_int_var; | |
87 | TAG_HANDLER_CONSTR(vars2) | |
88 | { // !!!!!! | |
89 | my_int_var = 666; | |
90 | } // !!!!!! | |
91 | TAG_HANDLER_END(VARS2) | |
92 | \end{verbatim} | |
93 | ||
94 | Never used in wxHTML :-) | |
95 | ||
22d6efa8 | 96 | {\bf TAG\_HANDLER\_PROC}({\it varib}) |
704a4b75 | 97 | |
d7cb14ce | 98 | This is very important macro. It defines \helpref{HandleTag}{wxhtmltaghandlerhandletag} |
704a4b75 VS |
99 | method. {\it varib} is name of parameter passed to the method, usually |
100 | {\it tag}. Body of method follows after this macro. | |
101 | Note than you must use { and } ! Example: | |
102 | ||
103 | \begin{verbatim} | |
104 | TAG_HANDLER_BEGIN(TITLE, "TITLE") | |
105 | TAG_HANDLER_PROC(tag) | |
106 | { | |
107 | printf("TITLE found...\n"); | |
108 | } | |
109 | TAG_HANDLER_END(TITLE) | |
110 | \end{verbatim} | |
111 | ||
22d6efa8 | 112 | {\bf TAG\_HANDLER\_END}({\it name}) |
704a4b75 VS |
113 | |
114 | Ends definition of tag handler {\it name}. | |
115 | ||
704a4b75 VS |
116 | \wxheading{Tags Modules} |
117 | ||
22d6efa8 JS |
118 | You can use set of 3 macros TAGS\_MODULE\_BEGIN, TAGS\_MODULE\_ADD and |
119 | TAGS\_MODULE\_END to inherit new module from | |
704a4b75 VS |
120 | \helpref{wxHtmlTagsModule}{wxhtmltagsmodule} and to create instance of it. |
121 | See macros reference: | |
122 | ||
22d6efa8 | 123 | {\bf TAGS\_MODULE\_BEGIN}({\it modname}) |
704a4b75 VS |
124 | |
125 | Begins module definition. {\it modname} is part of class name and must | |
126 | be unique. | |
127 | ||
22d6efa8 | 128 | {\bf TAGS\_MODULE\_ADD}({\it name}) |
704a4b75 VS |
129 | |
130 | Adds the handler to this module. {\it name} is the identifier from | |
22d6efa8 | 131 | TAG\_HANDLER\_BEGIN. |
704a4b75 | 132 | |
22d6efa8 | 133 | {\bf TAGS\_MODULE\_END}({\it modname}) |
704a4b75 VS |
134 | |
135 | Ends the definition of module. | |
136 | ||
137 | {\bf Example:} | |
138 | ||
139 | \begin{verbatim} | |
140 | TAGS_MODULE_BEGIN(Examples) | |
141 | TAGS_MODULE_ADD(VARS_ONLY) | |
142 | TAGS_MODULE_ADD(VARS2) | |
143 | TAGS_MODULE_ADD(TITLE) | |
144 | TAGS_MODULE_END(Examples) | |
145 | \end{verbatim} | |
22d6efa8 | 146 |