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