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