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