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