]>
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 | ||
b4f4d3dd | 18 | #ifndef WX_PRECOMP |
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. | |
b4a980f4 | 103 | if( known_pagebreaks.GetCount() == 0 || *pagebreak <= m_PosY) |
fd0bab43 | 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. | |
c3485a4e VS |
111 | int total_height = m_PosY; |
112 | for ( wxHtmlCell *parent = GetParent(); parent; parent = parent->GetParent() ) | |
113 | { | |
114 | total_height += parent->GetPosY(); | |
115 | } | |
116 | ||
f2034f1b VS |
117 | |
118 | // Search the array of pagebreaks to see whether we've already set | |
119 | // a pagebreak here. The standard bsearch() function is appropriate | |
120 | // because the array of pagebreaks through known_pagebreaks[number_of_pages] | |
121 | // is known to be sorted in strictly increasing order. '1 + number_of_pages' | |
122 | // is used as a bsearch() argument because the array contains a leading | |
123 | // zero plus one element for each page. | |
fd0bab43 | 124 | int where = known_pagebreaks.Index( total_height); |
f2034f1b | 125 | // Add a pagebreak only if there isn't one already set here. |
fd0bab43 VZ |
126 | if( wxNOT_FOUND != where) |
127 | { | |
d1da8872 | 128 | return false; |
fd0bab43 | 129 | } |
f2034f1b | 130 | else |
fd0bab43 | 131 | { |
f2034f1b | 132 | *pagebreak = m_PosY; |
d1da8872 | 133 | return true; |
fd0bab43 | 134 | } |
f2034f1b VS |
135 | } |
136 | ||
fd0bab43 VZ |
137 | |
138 | ||
5526e819 | 139 | TAG_HANDLER_BEGIN(P, "P") |
fc7a2a60 | 140 | TAG_HANDLER_CONSTR(P) { } |
5526e819 VS |
141 | |
142 | TAG_HANDLER_PROC(tag) | |
143 | { | |
e3774124 | 144 | if (m_WParser->GetContainer()->GetFirstChild() != NULL) |
04dbb646 | 145 | { |
4f9297b0 VS |
146 | m_WParser->CloseContainer(); |
147 | m_WParser->OpenContainer(); | |
8c651ab7 | 148 | } |
4f9297b0 VS |
149 | m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP); |
150 | m_WParser->GetContainer()->SetAlign(tag); | |
d1da8872 | 151 | return false; |
5526e819 VS |
152 | } |
153 | ||
154 | TAG_HANDLER_END(P) | |
155 | ||
156 | ||
157 | ||
158 | TAG_HANDLER_BEGIN(BR, "BR") | |
fc7a2a60 | 159 | TAG_HANDLER_CONSTR(BR) { } |
5526e819 VS |
160 | |
161 | TAG_HANDLER_PROC(tag) | |
162 | { | |
4f9297b0 | 163 | int al = m_WParser->GetContainer()->GetAlignHor(); |
5526e819 | 164 | wxHtmlContainerCell *c; |
33ac7e6f | 165 | |
4f9297b0 VS |
166 | m_WParser->CloseContainer(); |
167 | c = m_WParser->OpenContainer(); | |
168 | c->SetAlignHor(al); | |
169 | c->SetAlign(tag); | |
170 | c->SetMinHeight(m_WParser->GetCharHeight()); | |
d1da8872 | 171 | return false; |
5526e819 VS |
172 | } |
173 | ||
174 | TAG_HANDLER_END(BR) | |
175 | ||
176 | ||
177 | ||
178 | TAG_HANDLER_BEGIN(CENTER, "CENTER") | |
fc7a2a60 | 179 | TAG_HANDLER_CONSTR(CENTER) { } |
5526e819 VS |
180 | |
181 | TAG_HANDLER_PROC(tag) | |
182 | { | |
4f9297b0 VS |
183 | int old = m_WParser->GetAlign(); |
184 | wxHtmlContainerCell *c = m_WParser->GetContainer(); | |
185 | ||
186 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); | |
e3774124 | 187 | if (c->GetFirstChild() != NULL) |
04dbb646 | 188 | { |
4f9297b0 VS |
189 | m_WParser->CloseContainer(); |
190 | m_WParser->OpenContainer(); | |
5526e819 VS |
191 | } |
192 | else | |
4f9297b0 | 193 | c->SetAlignHor(wxHTML_ALIGN_CENTER); |
5526e819 | 194 | |
33ac7e6f | 195 | if (tag.HasEnding()) |
04dbb646 | 196 | { |
5526e819 VS |
197 | ParseInner(tag); |
198 | ||
4f9297b0 | 199 | m_WParser->SetAlign(old); |
e3774124 | 200 | if (c->GetFirstChild() != NULL) |
04dbb646 | 201 | { |
4f9297b0 VS |
202 | m_WParser->CloseContainer(); |
203 | m_WParser->OpenContainer(); | |
5526e819 VS |
204 | } |
205 | else | |
4f9297b0 | 206 | c->SetAlignHor(old); |
5526e819 | 207 | |
d1da8872 | 208 | return true; |
5526e819 | 209 | } |
d1da8872 | 210 | else return false; |
5526e819 VS |
211 | } |
212 | ||
213 | TAG_HANDLER_END(CENTER) | |
214 | ||
215 | ||
216 | ||
217 | TAG_HANDLER_BEGIN(DIV, "DIV") | |
fc7a2a60 | 218 | TAG_HANDLER_CONSTR(DIV) { } |
5526e819 VS |
219 | |
220 | TAG_HANDLER_PROC(tag) | |
221 | { | |
92337e39 | 222 | if(tag.HasParam(wxT("STYLE"))) |
04dbb646 | 223 | { |
d1da8872 | 224 | if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false)) |
f2034f1b VS |
225 | { |
226 | m_WParser->CloseContainer(); | |
227 | m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell); | |
228 | m_WParser->CloseContainer(); | |
229 | m_WParser->OpenContainer(); | |
d1da8872 | 230 | return false; |
f2034f1b VS |
231 | } |
232 | else | |
233 | { | |
234 | // Treat other STYLE parameters here when they're supported. | |
d1da8872 | 235 | return false; |
f2034f1b | 236 | } |
5526e819 | 237 | } |
92337e39 | 238 | else if(tag.HasParam(wxT("ALIGN"))) |
04dbb646 | 239 | { |
f2034f1b VS |
240 | int old = m_WParser->GetAlign(); |
241 | wxHtmlContainerCell *c = m_WParser->GetContainer(); | |
e3774124 | 242 | if (c->GetFirstChild() != NULL) |
f2034f1b VS |
243 | { |
244 | m_WParser->CloseContainer(); | |
245 | m_WParser->OpenContainer(); | |
246 | c = m_WParser->GetContainer(); | |
247 | c->SetAlign(tag); | |
248 | m_WParser->SetAlign(c->GetAlignHor()); | |
249 | } | |
250 | else | |
251 | { | |
252 | c->SetAlign(tag); | |
253 | m_WParser->SetAlign(c->GetAlignHor()); | |
254 | } | |
5526e819 | 255 | |
f2034f1b | 256 | ParseInner(tag); |
5526e819 | 257 | |
f2034f1b | 258 | m_WParser->SetAlign(old); |
e3774124 | 259 | if (c->GetFirstChild() != NULL) |
f2034f1b VS |
260 | { |
261 | m_WParser->CloseContainer(); | |
262 | m_WParser->OpenContainer(); | |
263 | } | |
264 | else | |
265 | c->SetAlignHor(old); | |
266 | ||
c5448f38 | 267 | return true; |
5526e819 VS |
268 | } |
269 | else | |
f2034f1b | 270 | { |
c5448f38 VS |
271 | // Same as BR |
272 | int al = m_WParser->GetContainer()->GetAlignHor(); | |
273 | wxHtmlContainerCell *c; | |
274 | ||
275 | m_WParser->CloseContainer(); | |
276 | c = m_WParser->OpenContainer(); | |
277 | c->SetAlignHor(al); | |
278 | c->SetAlign(tag); | |
279 | c->SetMinHeight(m_WParser->GetCharHeight()); | |
280 | return false; | |
f2034f1b | 281 | } |
5526e819 VS |
282 | } |
283 | ||
284 | TAG_HANDLER_END(DIV) | |
285 | ||
286 | ||
287 | ||
288 | ||
289 | TAG_HANDLER_BEGIN(TITLE, "TITLE") | |
fc7a2a60 | 290 | TAG_HANDLER_CONSTR(TITLE) { } |
5526e819 VS |
291 | |
292 | TAG_HANDLER_PROC(tag) | |
293 | { | |
bc55e31b VS |
294 | wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface(); |
295 | if (winIface) | |
04dbb646 | 296 | { |
b1a3a964 | 297 | wxString title(tag.GetBeginIter(), tag.GetEndIter1()); |
61c213fe | 298 | #if !wxUSE_UNICODE && wxUSE_WCHAR_T |
4d70ab08 VZ |
299 | const wxFontEncoding enc = m_WParser->GetInputEncoding(); |
300 | if ( enc != wxFONTENCODING_DEFAULT ) | |
301 | { | |
302 | // need to convert to the current one | |
303 | title = wxString(title.wc_str(wxCSConv(enc)), wxConvLocal); | |
304 | } | |
305 | #endif // !wxUSE_UNICODE | |
306 | ||
bc55e31b VS |
307 | title = m_WParser->GetEntitiesParser()->Parse(title); |
308 | ||
309 | winIface->SetHTMLWindowTitle(title); | |
5526e819 | 310 | } |
d1da8872 | 311 | return true; |
5526e819 VS |
312 | } |
313 | ||
314 | TAG_HANDLER_END(TITLE) | |
315 | ||
316 | ||
317 | ||
318 | ||
319 | TAG_HANDLER_BEGIN(BODY, "BODY") | |
fc7a2a60 | 320 | TAG_HANDLER_CONSTR(BODY) { } |
5526e819 VS |
321 | |
322 | TAG_HANDLER_PROC(tag) | |
323 | { | |
5526e819 VS |
324 | wxColour clr; |
325 | ||
8bd72d90 | 326 | if (tag.GetParamAsColour(wxT("TEXT"), &clr)) |
04dbb646 | 327 | { |
8bd72d90 VS |
328 | m_WParser->SetActualColor(clr); |
329 | m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr)); | |
5526e819 VS |
330 | } |
331 | ||
8bd72d90 VS |
332 | if (tag.GetParamAsColour(wxT("LINK"), &clr)) |
333 | m_WParser->SetLinkColor(clr); | |
334 | ||
bc55e31b VS |
335 | wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface(); |
336 | // the rest of this function requires a window: | |
337 | if ( !winIface ) | |
338 | return false; | |
339 | ||
97e490f8 VZ |
340 | if (tag.HasParam(wxT("BACKGROUND"))) |
341 | { | |
342 | wxFSFile *fileBgImage = m_WParser->OpenURL | |
343 | ( | |
344 | wxHTML_URL_IMAGE, | |
345 | tag.GetParam(wxT("BACKGROUND")) | |
346 | ); | |
347 | if ( fileBgImage ) | |
348 | { | |
349 | wxInputStream *is = fileBgImage->GetStream(); | |
350 | if ( is ) | |
351 | { | |
352 | wxImage image(*is); | |
353 | if ( image.Ok() ) | |
bc55e31b | 354 | winIface->SetHTMLBackgroundImage(image); |
97e490f8 | 355 | } |
a237d7ed VZ |
356 | |
357 | delete fileBgImage; | |
97e490f8 VZ |
358 | } |
359 | } | |
360 | ||
8bd72d90 | 361 | if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr)) |
04dbb646 | 362 | { |
8bd72d90 VS |
363 | m_WParser->GetContainer()->InsertCell( |
364 | new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND)); | |
bc55e31b | 365 | winIface->SetHTMLBackgroundColour(clr); |
5526e819 | 366 | } |
97e490f8 | 367 | |
d1da8872 | 368 | return false; |
5526e819 VS |
369 | } |
370 | ||
371 | TAG_HANDLER_END(BODY) | |
372 | ||
373 | ||
374 | ||
375 | TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE") | |
fc7a2a60 | 376 | TAG_HANDLER_CONSTR(BLOCKQUOTE) { } |
5526e819 VS |
377 | |
378 | TAG_HANDLER_PROC(tag) | |
379 | { | |
380 | wxHtmlContainerCell *c; | |
33ac7e6f | 381 | |
4f9297b0 VS |
382 | m_WParser->CloseContainer(); |
383 | c = m_WParser->OpenContainer(); | |
33ac7e6f | 384 | |
04dbb646 | 385 | if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT) |
4f9297b0 | 386 | c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT); |
5526e819 | 387 | else |
4f9297b0 | 388 | c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT); |
33ac7e6f | 389 | |
04dbb646 | 390 | c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP); |
4f9297b0 | 391 | m_WParser->OpenContainer(); |
5526e819 | 392 | ParseInner(tag); |
4f9297b0 VS |
393 | c = m_WParser->CloseContainer(); |
394 | c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM); | |
395 | m_WParser->CloseContainer(); | |
396 | m_WParser->OpenContainer(); | |
d1da8872 | 397 | return true; |
5526e819 VS |
398 | } |
399 | ||
400 | TAG_HANDLER_END(BLOCKQUOTE) | |
401 | ||
402 | ||
403 | ||
3c115835 VS |
404 | TAG_HANDLER_BEGIN(SUBSUP, "SUB,SUP") |
405 | ||
406 | TAG_HANDLER_PROC(tag) | |
407 | { | |
408 | bool issub = (tag.GetName() == wxT("SUB")); | |
409 | wxHtmlScriptMode oldmode = m_WParser->GetScriptMode(); | |
410 | int oldbase = m_WParser->GetScriptBaseline(); | |
411 | int oldsize = m_WParser->GetFontSize(); | |
412 | ||
413 | wxHtmlContainerCell *cont = m_WParser->GetContainer(); | |
414 | wxHtmlCell *c = cont->GetLastChild(); | |
415 | ||
416 | m_WParser->SetScriptMode(issub ? wxHTML_SCRIPT_SUB : wxHTML_SCRIPT_SUP); | |
198d7c6c VS |
417 | m_WParser->SetScriptBaseline( |
418 | oldbase + c ? c->GetScriptBaseline() : 0); | |
3c115835 VS |
419 | |
420 | // select smaller font | |
421 | m_WParser->SetFontSize(m_WParser->GetFontSize()-2); | |
422 | cont->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
423 | ||
424 | ParseInner(tag); | |
425 | ||
426 | // restore font size | |
427 | m_WParser->SetFontSize(oldsize); | |
428 | m_WParser->GetContainer()->InsertCell( | |
429 | new wxHtmlFontCell(m_WParser->CreateCurrentFont())); | |
430 | ||
431 | // restore base and alignment | |
432 | m_WParser->SetScriptBaseline(oldbase); | |
433 | m_WParser->SetScriptMode(oldmode); | |
434 | ||
435 | return true; | |
436 | } | |
437 | ||
438 | TAG_HANDLER_END(SUBSUP) | |
439 | ||
440 | ||
8829fa82 VS |
441 | // Tag handler for tags that we have to ignore, otherwise non-text data |
442 | // would show up as text: | |
443 | TAG_HANDLER_BEGIN(DoNothing, "SCRIPT") | |
fc7a2a60 VZ |
444 | TAG_HANDLER_CONSTR(DoNothing) { } |
445 | ||
446 | TAG_HANDLER_PROC(WXUNUSED(tag)) | |
8829fa82 VS |
447 | { |
448 | return true; | |
449 | } | |
450 | TAG_HANDLER_END(DoNothing) | |
5526e819 VS |
451 | |
452 | ||
453 | ||
3c115835 VS |
454 | |
455 | ||
5526e819 VS |
456 | TAGS_MODULE_BEGIN(Layout) |
457 | ||
458 | TAGS_MODULE_ADD(P) | |
459 | TAGS_MODULE_ADD(BR) | |
460 | TAGS_MODULE_ADD(CENTER) | |
461 | TAGS_MODULE_ADD(DIV) | |
462 | TAGS_MODULE_ADD(TITLE) | |
463 | TAGS_MODULE_ADD(BODY) | |
464 | TAGS_MODULE_ADD(BLOCKQUOTE) | |
3c115835 | 465 | TAGS_MODULE_ADD(SUBSUP) |
8829fa82 | 466 | TAGS_MODULE_ADD(DoNothing) |
5526e819 VS |
467 | |
468 | TAGS_MODULE_END(Layout) | |
469 | ||
470 | #endif |