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