]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
c88293a4 | 2 | // Name: m_layout.cpp |
5526e819 VS |
3 | // Purpose: wxHtml module for basic paragraphs/layout handling |
4 | // Author: Vaclav Slavik | |
69941f05 | 5 | // RCS-ID: $Id$ |
5526e819 | 6 | // Copyright: (c) 1999 Vaclav Slavik |
65571936 | 7 | // Licence: wxWindows licence |
5526e819 | 8 | ///////////////////////////////////////////////////////////////////////////// |
14f355c2 | 9 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
3364ab79 RS |
10 | #pragma implementation |
11 | #endif | |
12 | ||
314260fb | 13 | #include "wx/wxprec.h" |
5526e819 | 14 | |
314260fb VS |
15 | |
16 | #include "wx/defs.h" | |
f6bcfd97 | 17 | #if wxUSE_HTML && wxUSE_STREAMS |
2b5f62a0 | 18 | #ifdef __BORLANDC__ |
3364ab79 RS |
19 | #pragma hdrstop |
20 | #endif | |
21 | ||
22 | #ifndef WXPRECOMP | |
3364ab79 RS |
23 | #endif |
24 | ||
5526e819 | 25 | |
69941f05 VS |
26 | #include "wx/html/forcelnk.h" |
27 | #include "wx/html/m_templ.h" | |
5526e819 | 28 | |
69941f05 | 29 | #include "wx/html/htmlwin.h" |
5526e819 | 30 | |
c88293a4 | 31 | FORCE_LINK_ME(m_layout) |
5526e819 | 32 | |
7127d129 RR |
33 | #ifdef __WXWINCE__ |
34 | #include "wx/msw/wince/missing.h" // for bsearch() | |
35 | #else | |
36 | #include <stdlib.h> // bsearch() | |
37 | #endif | |
f2034f1b VS |
38 | |
39 | //----------------------------------------------------------------------------- | |
40 | // wxHtmlPageBreakCell | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | // Since html isn't a page-layout language, it doesn't support page | |
44 | // page breaks directly--that requires CSS2 support. But a page-break | |
45 | // facility is handy, and has been requested more than once on the | |
46 | // mailing lists. This wxHtml tag handler implements just enough of | |
47 | // CSS2 to support a page break by recognizing only | |
48 | // <div style="page-break-before:always"> | |
49 | // | |
50 | // wxHtml maintains page breaks in wxHtmlPrintout::m_PageBreaks. The | |
51 | // tag handler below adds appropriate offsets to that array member. | |
52 | // wxHtmlDCRenderer::Render() accesses that array and makes a new page | |
53 | // begin after each page-break tag. | |
54 | ||
55 | // The page-break handler does all its work in AdjustPagebreak(). For | |
56 | // all tag handlers, that function adjusts the page-break position. | |
57 | // For other tags, it determines whether the html element can fit on | |
58 | // the remainder of the page; if it cannot fit, but must not be split, | |
59 | // then the function moves the page break provided in the argument up, | |
60 | // and returns 'true' to inform the caller that the argument was | |
61 | // modified. | |
62 | // | |
63 | // Due to its special purpose, the page-break facility differs from | |
64 | // other tags. It takes up no space, but it behaves as though there is | |
65 | // never enough room to fit it on the remainder of the page--it always | |
66 | // forces a page break. Therefore, unlike other elements that trigger | |
67 | // a page break, it would never 'fit' on the following page either. | |
68 | // Therefore it's necessary to compare each pagebreak candidate to the | |
69 | // array wxHtmlPrintout::m_PageBreaks of pagebreaks already set, and | |
70 | // set a new one only if it's not in that array. | |
71 | ||
4460b6c4 | 72 | class wxHtmlPageBreakCell : public wxHtmlCell |
f2034f1b | 73 | { |
36c4ff4d | 74 | public: |
f2034f1b VS |
75 | wxHtmlPageBreakCell() {} |
76 | ||
36c4ff4d VS |
77 | bool AdjustPagebreak(int* pagebreak, |
78 | int* known_pagebreaks = NULL, | |
79 | int number_of_pages = 0) const; | |
80 | void Draw(wxDC& WXUNUSED(dc), | |
81 | int WXUNUSED(x), int WXUNUSED(y), | |
82 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
e3ac6ee1 | 83 | wxHtmlRenderingInfo& WXUNUSED(info)) {} |
f2034f1b | 84 | |
36c4ff4d | 85 | private: |
f2034f1b VS |
86 | DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell) |
87 | }; | |
88 | ||
89 | // Comparison routine for bsearch into an int* array of pagebreaks. | |
f9ba645e | 90 | extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1) |
f2034f1b VS |
91 | { |
92 | return *(int*)i0 - *(int*)i1; | |
93 | } | |
94 | ||
95 | bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak, int* known_pagebreaks, int number_of_pages) const | |
96 | { | |
97 | // When we are counting pages, 'known_pagebreaks' is non-NULL. | |
98 | // That's the only time we change 'pagebreak'. Otherwise, pages | |
99 | // were already counted, 'known_pagebreaks' is NULL, and we don't | |
d1da8872 | 100 | // do anything except return false. |
f2034f1b | 101 | // |
d1da8872 | 102 | // We also simply return false if the 'pagebreak' argument is |
f2034f1b VS |
103 | // less than (vertically above) or the same as the current |
104 | // vertical position. Otherwise we'd be setting a pagebreak above | |
105 | // the current cell, which is incorrect, or duplicating a | |
106 | // pagebreak that has already been set. | |
107 | if(NULL == known_pagebreaks || *pagebreak <= m_PosY) | |
108 | { | |
d1da8872 | 109 | return false; |
f2034f1b VS |
110 | } |
111 | ||
112 | // m_PosY is only the vertical offset from the parent. The pagebreak | |
113 | // required here is the total page offset, so m_PosY must be added | |
114 | // to the parent's offset and height. | |
115 | int total_height = m_PosY + GetParent()->GetPosY() + GetParent()->GetHeight(); | |
116 | ||
117 | // Search the array of pagebreaks to see whether we've already set | |
118 | // a pagebreak here. The standard bsearch() function is appropriate | |
119 | // because the array of pagebreaks through known_pagebreaks[number_of_pages] | |
120 | // is known to be sorted in strictly increasing order. '1 + number_of_pages' | |
121 | // is used as a bsearch() argument because the array contains a leading | |
122 | // zero plus one element for each page. | |
123 | int* where = (int*) bsearch(&total_height, known_pagebreaks, | |
124 | 1 + number_of_pages, sizeof(int), | |
f9ba645e | 125 | wxInteger_compare); |
f2034f1b VS |
126 | // Add a pagebreak only if there isn't one already set here. |
127 | if(NULL != where) | |
128 | { | |
d1da8872 | 129 | return false; |
f2034f1b VS |
130 | } |
131 | else | |
132 | { | |
133 | *pagebreak = m_PosY; | |
d1da8872 | 134 | return true; |
f2034f1b VS |
135 | } |
136 | } | |
137 | ||
5526e819 | 138 | TAG_HANDLER_BEGIN(P, "P") |
fc7a2a60 | 139 | TAG_HANDLER_CONSTR(P) { } |
5526e819 VS |
140 | |
141 | TAG_HANDLER_PROC(tag) | |
142 | { | |
e3774124 | 143 | if (m_WParser->GetContainer()->GetFirstChild() != NULL) |
04dbb646 | 144 | { |
4f9297b0 VS |
145 | m_WParser->CloseContainer(); |
146 | m_WParser->OpenContainer(); | |
8c651ab7 | 147 | } |
4f9297b0 VS |
148 | m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP); |
149 | m_WParser->GetContainer()->SetAlign(tag); | |
d1da8872 | 150 | return false; |
5526e819 VS |
151 | } |
152 | ||
153 | TAG_HANDLER_END(P) | |
154 | ||
155 | ||
156 | ||
157 | TAG_HANDLER_BEGIN(BR, "BR") | |
fc7a2a60 | 158 | TAG_HANDLER_CONSTR(BR) { } |
5526e819 VS |
159 | |
160 | TAG_HANDLER_PROC(tag) | |
161 | { | |
4f9297b0 | 162 | int al = m_WParser->GetContainer()->GetAlignHor(); |
5526e819 | 163 | wxHtmlContainerCell *c; |
33ac7e6f | 164 | |
4f9297b0 VS |
165 | m_WParser->CloseContainer(); |
166 | c = m_WParser->OpenContainer(); | |
167 | c->SetAlignHor(al); | |
168 | c->SetAlign(tag); | |
169 | c->SetMinHeight(m_WParser->GetCharHeight()); | |
d1da8872 | 170 | return false; |
5526e819 VS |
171 | } |
172 | ||
173 | TAG_HANDLER_END(BR) | |
174 | ||
175 | ||
176 | ||
177 | TAG_HANDLER_BEGIN(CENTER, "CENTER") | |
fc7a2a60 | 178 | TAG_HANDLER_CONSTR(CENTER) { } |
5526e819 VS |
179 | |
180 | TAG_HANDLER_PROC(tag) | |
181 | { | |
4f9297b0 VS |
182 | int old = m_WParser->GetAlign(); |
183 | wxHtmlContainerCell *c = m_WParser->GetContainer(); | |
184 | ||
185 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); | |
e3774124 | 186 | if (c->GetFirstChild() != NULL) |
04dbb646 | 187 | { |
4f9297b0 VS |
188 | m_WParser->CloseContainer(); |
189 | m_WParser->OpenContainer(); | |
5526e819 VS |
190 | } |
191 | else | |
4f9297b0 | 192 | c->SetAlignHor(wxHTML_ALIGN_CENTER); |
5526e819 | 193 | |
33ac7e6f | 194 | if (tag.HasEnding()) |
04dbb646 | 195 | { |
5526e819 VS |
196 | ParseInner(tag); |
197 | ||
4f9297b0 | 198 | m_WParser->SetAlign(old); |
e3774124 | 199 | if (c->GetFirstChild() != NULL) |
04dbb646 | 200 | { |
4f9297b0 VS |
201 | m_WParser->CloseContainer(); |
202 | m_WParser->OpenContainer(); | |
5526e819 VS |
203 | } |
204 | else | |
4f9297b0 | 205 | c->SetAlignHor(old); |
5526e819 | 206 | |
d1da8872 | 207 | return true; |
5526e819 | 208 | } |
d1da8872 | 209 | else return false; |
5526e819 VS |
210 | } |
211 | ||
212 | TAG_HANDLER_END(CENTER) | |
213 | ||
214 | ||
215 | ||
216 | TAG_HANDLER_BEGIN(DIV, "DIV") | |
fc7a2a60 | 217 | TAG_HANDLER_CONSTR(DIV) { } |
5526e819 VS |
218 | |
219 | TAG_HANDLER_PROC(tag) | |
220 | { | |
92337e39 | 221 | if(tag.HasParam(wxT("STYLE"))) |
04dbb646 | 222 | { |
d1da8872 | 223 | if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false)) |
f2034f1b VS |
224 | { |
225 | m_WParser->CloseContainer(); | |
226 | m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell); | |
227 | m_WParser->CloseContainer(); | |
228 | m_WParser->OpenContainer(); | |
d1da8872 | 229 | return false; |
f2034f1b VS |
230 | } |
231 | else | |
232 | { | |
233 | // Treat other STYLE parameters here when they're supported. | |
d1da8872 | 234 | return false; |
f2034f1b | 235 | } |
5526e819 | 236 | } |
92337e39 | 237 | else if(tag.HasParam(wxT("ALIGN"))) |
04dbb646 | 238 | { |
f2034f1b VS |
239 | int old = m_WParser->GetAlign(); |
240 | wxHtmlContainerCell *c = m_WParser->GetContainer(); | |
e3774124 | 241 | if (c->GetFirstChild() != NULL) |
f2034f1b VS |
242 | { |
243 | m_WParser->CloseContainer(); | |
244 | m_WParser->OpenContainer(); | |
245 | c = m_WParser->GetContainer(); | |
246 | c->SetAlign(tag); | |
247 | m_WParser->SetAlign(c->GetAlignHor()); | |
248 | } | |
249 | else | |
250 | { | |
251 | c->SetAlign(tag); | |
252 | m_WParser->SetAlign(c->GetAlignHor()); | |
253 | } | |
5526e819 | 254 | |
f2034f1b | 255 | ParseInner(tag); |
5526e819 | 256 | |
f2034f1b | 257 | m_WParser->SetAlign(old); |
e3774124 | 258 | if (c->GetFirstChild() != NULL) |
f2034f1b VS |
259 | { |
260 | m_WParser->CloseContainer(); | |
261 | m_WParser->OpenContainer(); | |
262 | } | |
263 | else | |
264 | c->SetAlignHor(old); | |
265 | ||
c5448f38 | 266 | return true; |
5526e819 VS |
267 | } |
268 | else | |
f2034f1b | 269 | { |
c5448f38 VS |
270 | // Same as BR |
271 | int al = m_WParser->GetContainer()->GetAlignHor(); | |
272 | wxHtmlContainerCell *c; | |
273 | ||
274 | m_WParser->CloseContainer(); | |
275 | c = m_WParser->OpenContainer(); | |
276 | c->SetAlignHor(al); | |
277 | c->SetAlign(tag); | |
278 | c->SetMinHeight(m_WParser->GetCharHeight()); | |
279 | return false; | |
f2034f1b | 280 | } |
5526e819 VS |
281 | } |
282 | ||
283 | TAG_HANDLER_END(DIV) | |
284 | ||
285 | ||
286 | ||
287 | ||
288 | TAG_HANDLER_BEGIN(TITLE, "TITLE") | |
fc7a2a60 | 289 | TAG_HANDLER_CONSTR(TITLE) { } |
5526e819 VS |
290 | |
291 | TAG_HANDLER_PROC(tag) | |
292 | { | |
33ac7e6f | 293 | if (m_WParser->GetWindow()) |
04dbb646 | 294 | { |
4f9297b0 | 295 | wxHtmlWindow *wfr = (wxHtmlWindow*)(m_WParser->GetWindow()); |
33ac7e6f | 296 | if (wfr) |
04dbb646 | 297 | { |
4d5881b1 | 298 | wxString title = m_WParser->GetSource()->Mid( |
d1da8872 | 299 | tag.GetBeginPos(), |
4d5881b1 | 300 | tag.GetEndPos1()-tag.GetBeginPos()); |
140ba067 VS |
301 | #if !wxUSE_UNICODE |
302 | wxCSConv conv(m_WParser->GetInputEncoding()); | |
140ba067 | 303 | title = wxString(title.wc_str(conv), wxConvLocal); |
140ba067 | 304 | #endif |
4d5881b1 VS |
305 | title = m_WParser->GetEntitiesParser()->Parse(title); |
306 | wfr->OnSetTitle(title); | |
5526e819 VS |
307 | } |
308 | } | |
d1da8872 | 309 | return true; |
5526e819 VS |
310 | } |
311 | ||
312 | TAG_HANDLER_END(TITLE) | |
313 | ||
314 | ||
315 | ||
316 | ||
317 | TAG_HANDLER_BEGIN(BODY, "BODY") | |
fc7a2a60 | 318 | TAG_HANDLER_CONSTR(BODY) { } |
5526e819 VS |
319 | |
320 | TAG_HANDLER_PROC(tag) | |
321 | { | |
5526e819 VS |
322 | wxColour clr; |
323 | ||
8bd72d90 | 324 | if (tag.GetParamAsColour(wxT("TEXT"), &clr)) |
04dbb646 | 325 | { |
8bd72d90 VS |
326 | m_WParser->SetActualColor(clr); |
327 | m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr)); | |
5526e819 VS |
328 | } |
329 | ||
8bd72d90 VS |
330 | if (tag.GetParamAsColour(wxT("LINK"), &clr)) |
331 | m_WParser->SetLinkColor(clr); | |
332 | ||
333 | if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr)) | |
04dbb646 | 334 | { |
8bd72d90 VS |
335 | m_WParser->GetContainer()->InsertCell( |
336 | new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND)); | |
337 | if (m_WParser->GetWindow() != NULL) | |
338 | m_WParser->GetWindow()->SetBackgroundColour(clr); | |
5526e819 | 339 | } |
d1da8872 | 340 | return false; |
5526e819 VS |
341 | } |
342 | ||
343 | TAG_HANDLER_END(BODY) | |
344 | ||
345 | ||
346 | ||
347 | TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE") | |
fc7a2a60 | 348 | TAG_HANDLER_CONSTR(BLOCKQUOTE) { } |
5526e819 VS |
349 | |
350 | TAG_HANDLER_PROC(tag) | |
351 | { | |
352 | wxHtmlContainerCell *c; | |
33ac7e6f | 353 | |
4f9297b0 VS |
354 | m_WParser->CloseContainer(); |
355 | c = m_WParser->OpenContainer(); | |
33ac7e6f | 356 | |
04dbb646 | 357 | if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT) |
4f9297b0 | 358 | c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT); |
5526e819 | 359 | else |
4f9297b0 | 360 | c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT); |
33ac7e6f | 361 | |
04dbb646 | 362 | c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP); |
4f9297b0 | 363 | m_WParser->OpenContainer(); |
5526e819 | 364 | ParseInner(tag); |
4f9297b0 VS |
365 | c = m_WParser->CloseContainer(); |
366 | c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM); | |
367 | m_WParser->CloseContainer(); | |
368 | m_WParser->OpenContainer(); | |
d1da8872 | 369 | return true; |
5526e819 VS |
370 | } |
371 | ||
372 | TAG_HANDLER_END(BLOCKQUOTE) | |
373 | ||
374 | ||
375 | ||
8829fa82 VS |
376 | // Tag handler for tags that we have to ignore, otherwise non-text data |
377 | // would show up as text: | |
378 | TAG_HANDLER_BEGIN(DoNothing, "SCRIPT") | |
fc7a2a60 VZ |
379 | TAG_HANDLER_CONSTR(DoNothing) { } |
380 | ||
381 | TAG_HANDLER_PROC(WXUNUSED(tag)) | |
8829fa82 VS |
382 | { |
383 | return true; | |
384 | } | |
385 | TAG_HANDLER_END(DoNothing) | |
5526e819 VS |
386 | |
387 | ||
388 | ||
389 | TAGS_MODULE_BEGIN(Layout) | |
390 | ||
391 | TAGS_MODULE_ADD(P) | |
392 | TAGS_MODULE_ADD(BR) | |
393 | TAGS_MODULE_ADD(CENTER) | |
394 | TAGS_MODULE_ADD(DIV) | |
395 | TAGS_MODULE_ADD(TITLE) | |
396 | TAGS_MODULE_ADD(BODY) | |
397 | TAGS_MODULE_ADD(BLOCKQUOTE) | |
8829fa82 | 398 | TAGS_MODULE_ADD(DoNothing) |
5526e819 VS |
399 | |
400 | TAGS_MODULE_END(Layout) | |
401 | ||
402 | #endif |