]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: m_layout.cpp | |
3 | // Purpose: wxHtml module for basic paragraphs/layout handling | |
4 | // Author: Vaclav Slavik | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 1999 Vaclav Slavik | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #include "wx/wxprec.h" | |
11 | ||
12 | #include "wx/defs.h" | |
13 | ||
14 | #if wxUSE_HTML && wxUSE_STREAMS | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WXPRECOMP | |
21 | #endif | |
22 | ||
23 | #include "wx/image.h" | |
24 | ||
25 | #include "wx/html/forcelnk.h" | |
26 | #include "wx/html/m_templ.h" | |
27 | ||
28 | #include "wx/html/htmlwin.h" | |
29 | ||
30 | FORCE_LINK_ME(m_layout) | |
31 | ||
32 | #ifdef __WXWINCE__ | |
33 | #include "wx/msw/wince/missing.h" // for bsearch() | |
34 | #else | |
35 | #include <stdlib.h> // bsearch() | |
36 | #endif | |
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 | ||
71 | class wxHtmlPageBreakCell : public wxHtmlCell | |
72 | { | |
73 | public: | |
74 | wxHtmlPageBreakCell() {} | |
75 | ||
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), | |
82 | wxHtmlRenderingInfo& WXUNUSED(info)) {} | |
83 | ||
84 | private: | |
85 | DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell) | |
86 | }; | |
87 | ||
88 | // Comparison routine for bsearch into an int* array of pagebreaks. | |
89 | extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1) | |
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 | |
99 | // do anything except return false. | |
100 | // | |
101 | // We also simply return false if the 'pagebreak' argument is | |
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 | { | |
108 | return false; | |
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), | |
124 | wxInteger_compare); | |
125 | // Add a pagebreak only if there isn't one already set here. | |
126 | if(NULL != where) | |
127 | { | |
128 | return false; | |
129 | } | |
130 | else | |
131 | { | |
132 | *pagebreak = m_PosY; | |
133 | return true; | |
134 | } | |
135 | } | |
136 | ||
137 | TAG_HANDLER_BEGIN(P, "P") | |
138 | TAG_HANDLER_CONSTR(P) { } | |
139 | ||
140 | TAG_HANDLER_PROC(tag) | |
141 | { | |
142 | if (m_WParser->GetContainer()->GetFirstChild() != NULL) | |
143 | { | |
144 | m_WParser->CloseContainer(); | |
145 | m_WParser->OpenContainer(); | |
146 | } | |
147 | m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP); | |
148 | m_WParser->GetContainer()->SetAlign(tag); | |
149 | return false; | |
150 | } | |
151 | ||
152 | TAG_HANDLER_END(P) | |
153 | ||
154 | ||
155 | ||
156 | TAG_HANDLER_BEGIN(BR, "BR") | |
157 | TAG_HANDLER_CONSTR(BR) { } | |
158 | ||
159 | TAG_HANDLER_PROC(tag) | |
160 | { | |
161 | int al = m_WParser->GetContainer()->GetAlignHor(); | |
162 | wxHtmlContainerCell *c; | |
163 | ||
164 | m_WParser->CloseContainer(); | |
165 | c = m_WParser->OpenContainer(); | |
166 | c->SetAlignHor(al); | |
167 | c->SetAlign(tag); | |
168 | c->SetMinHeight(m_WParser->GetCharHeight()); | |
169 | return false; | |
170 | } | |
171 | ||
172 | TAG_HANDLER_END(BR) | |
173 | ||
174 | ||
175 | ||
176 | TAG_HANDLER_BEGIN(CENTER, "CENTER") | |
177 | TAG_HANDLER_CONSTR(CENTER) { } | |
178 | ||
179 | TAG_HANDLER_PROC(tag) | |
180 | { | |
181 | int old = m_WParser->GetAlign(); | |
182 | wxHtmlContainerCell *c = m_WParser->GetContainer(); | |
183 | ||
184 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); | |
185 | if (c->GetFirstChild() != NULL) | |
186 | { | |
187 | m_WParser->CloseContainer(); | |
188 | m_WParser->OpenContainer(); | |
189 | } | |
190 | else | |
191 | c->SetAlignHor(wxHTML_ALIGN_CENTER); | |
192 | ||
193 | if (tag.HasEnding()) | |
194 | { | |
195 | ParseInner(tag); | |
196 | ||
197 | m_WParser->SetAlign(old); | |
198 | if (c->GetFirstChild() != NULL) | |
199 | { | |
200 | m_WParser->CloseContainer(); | |
201 | m_WParser->OpenContainer(); | |
202 | } | |
203 | else | |
204 | c->SetAlignHor(old); | |
205 | ||
206 | return true; | |
207 | } | |
208 | else return false; | |
209 | } | |
210 | ||
211 | TAG_HANDLER_END(CENTER) | |
212 | ||
213 | ||
214 | ||
215 | TAG_HANDLER_BEGIN(DIV, "DIV") | |
216 | TAG_HANDLER_CONSTR(DIV) { } | |
217 | ||
218 | TAG_HANDLER_PROC(tag) | |
219 | { | |
220 | if(tag.HasParam(wxT("STYLE"))) | |
221 | { | |
222 | if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false)) | |
223 | { | |
224 | m_WParser->CloseContainer(); | |
225 | m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell); | |
226 | m_WParser->CloseContainer(); | |
227 | m_WParser->OpenContainer(); | |
228 | return false; | |
229 | } | |
230 | else | |
231 | { | |
232 | // Treat other STYLE parameters here when they're supported. | |
233 | return false; | |
234 | } | |
235 | } | |
236 | else if(tag.HasParam(wxT("ALIGN"))) | |
237 | { | |
238 | int old = m_WParser->GetAlign(); | |
239 | wxHtmlContainerCell *c = m_WParser->GetContainer(); | |
240 | if (c->GetFirstChild() != NULL) | |
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 | } | |
253 | ||
254 | ParseInner(tag); | |
255 | ||
256 | m_WParser->SetAlign(old); | |
257 | if (c->GetFirstChild() != NULL) | |
258 | { | |
259 | m_WParser->CloseContainer(); | |
260 | m_WParser->OpenContainer(); | |
261 | } | |
262 | else | |
263 | c->SetAlignHor(old); | |
264 | ||
265 | return true; | |
266 | } | |
267 | else | |
268 | { | |
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; | |
279 | } | |
280 | } | |
281 | ||
282 | TAG_HANDLER_END(DIV) | |
283 | ||
284 | ||
285 | ||
286 | ||
287 | TAG_HANDLER_BEGIN(TITLE, "TITLE") | |
288 | TAG_HANDLER_CONSTR(TITLE) { } | |
289 | ||
290 | TAG_HANDLER_PROC(tag) | |
291 | { | |
292 | if (m_WParser->GetWindow()) | |
293 | { | |
294 | wxHtmlWindow *wfr = (wxHtmlWindow*)(m_WParser->GetWindow()); | |
295 | if (wfr) | |
296 | { | |
297 | wxString title = m_WParser->GetSource()->Mid( | |
298 | tag.GetBeginPos(), | |
299 | tag.GetEndPos1()-tag.GetBeginPos()); | |
300 | #if !wxUSE_UNICODE && wxUSE_WCHAR_T | |
301 | wxCSConv conv(m_WParser->GetInputEncoding()); | |
302 | title = wxString(title.wc_str(conv), wxConvLocal); | |
303 | #endif | |
304 | title = m_WParser->GetEntitiesParser()->Parse(title); | |
305 | wfr->OnSetTitle(title); | |
306 | } | |
307 | } | |
308 | return true; | |
309 | } | |
310 | ||
311 | TAG_HANDLER_END(TITLE) | |
312 | ||
313 | ||
314 | ||
315 | ||
316 | TAG_HANDLER_BEGIN(BODY, "BODY") | |
317 | TAG_HANDLER_CONSTR(BODY) { } | |
318 | ||
319 | TAG_HANDLER_PROC(tag) | |
320 | { | |
321 | wxColour clr; | |
322 | ||
323 | if (tag.GetParamAsColour(wxT("TEXT"), &clr)) | |
324 | { | |
325 | m_WParser->SetActualColor(clr); | |
326 | m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr)); | |
327 | } | |
328 | ||
329 | if (tag.GetParamAsColour(wxT("LINK"), &clr)) | |
330 | m_WParser->SetLinkColor(clr); | |
331 | ||
332 | if (tag.HasParam(wxT("BACKGROUND"))) | |
333 | { | |
334 | wxFSFile *fileBgImage = m_WParser->OpenURL | |
335 | ( | |
336 | wxHTML_URL_IMAGE, | |
337 | tag.GetParam(wxT("BACKGROUND")) | |
338 | ); | |
339 | if ( fileBgImage ) | |
340 | { | |
341 | wxInputStream *is = fileBgImage->GetStream(); | |
342 | if ( is ) | |
343 | { | |
344 | #if !defined(__WXMSW__) || wxUSE_WXDIB | |
345 | wxImage image(*is); | |
346 | if ( image.Ok() ) | |
347 | m_WParser->GetWindow()->SetBackgroundImage(image); | |
348 | #endif | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
353 | if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr)) | |
354 | { | |
355 | m_WParser->GetContainer()->InsertCell( | |
356 | new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND)); | |
357 | if (m_WParser->GetWindow() != NULL) | |
358 | m_WParser->GetWindow()->SetBackgroundColour(clr); | |
359 | } | |
360 | ||
361 | return false; | |
362 | } | |
363 | ||
364 | TAG_HANDLER_END(BODY) | |
365 | ||
366 | ||
367 | ||
368 | TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE") | |
369 | TAG_HANDLER_CONSTR(BLOCKQUOTE) { } | |
370 | ||
371 | TAG_HANDLER_PROC(tag) | |
372 | { | |
373 | wxHtmlContainerCell *c; | |
374 | ||
375 | m_WParser->CloseContainer(); | |
376 | c = m_WParser->OpenContainer(); | |
377 | ||
378 | if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT) | |
379 | c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT); | |
380 | else | |
381 | c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT); | |
382 | ||
383 | c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP); | |
384 | m_WParser->OpenContainer(); | |
385 | ParseInner(tag); | |
386 | c = m_WParser->CloseContainer(); | |
387 | c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM); | |
388 | m_WParser->CloseContainer(); | |
389 | m_WParser->OpenContainer(); | |
390 | return true; | |
391 | } | |
392 | ||
393 | TAG_HANDLER_END(BLOCKQUOTE) | |
394 | ||
395 | ||
396 | ||
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 | ||
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") | |
436 | TAG_HANDLER_CONSTR(DoNothing) { } | |
437 | ||
438 | TAG_HANDLER_PROC(WXUNUSED(tag)) | |
439 | { | |
440 | return true; | |
441 | } | |
442 | TAG_HANDLER_END(DoNothing) | |
443 | ||
444 | ||
445 | ||
446 | ||
447 | ||
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) | |
457 | TAGS_MODULE_ADD(SUBSUP) | |
458 | TAGS_MODULE_ADD(DoNothing) | |
459 | ||
460 | TAGS_MODULE_END(Layout) | |
461 | ||
462 | #endif |