]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_layout.cpp
Applied patch [ 1381895 ] remove circular include dependendcy in wxchar.h
[wxWidgets.git] / src / html / m_layout.cpp
CommitLineData
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 30FORCE_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 71class wxHtmlPageBreakCell : public wxHtmlCell
f2034f1b 72{
36c4ff4d 73public:
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 84private:
f2034f1b
VS
85 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell)
86};
87
88// Comparison routine for bsearch into an int* array of pagebreaks.
f9ba645e 89extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1)
f2034f1b
VS
90{
91 return *(int*)i0 - *(int*)i1;
92}
93
94bool 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 137TAG_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
152TAG_HANDLER_END(P)
153
154
155
156TAG_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
172TAG_HANDLER_END(BR)
173
174
175
176TAG_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
211TAG_HANDLER_END(CENTER)
212
213
214
215TAG_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
282TAG_HANDLER_END(DIV)
283
284
285
286
287TAG_HANDLER_BEGIN(TITLE, "TITLE")
fc7a2a60 288 TAG_HANDLER_CONSTR(TITLE) { }
5526e819
VS
289
290 TAG_HANDLER_PROC(tag)
291 {
33ac7e6f 292 if (m_WParser->GetWindow())
04dbb646 293 {
4f9297b0 294 wxHtmlWindow *wfr = (wxHtmlWindow*)(m_WParser->GetWindow());
33ac7e6f 295 if (wfr)
04dbb646 296 {
4d5881b1 297 wxString title = m_WParser->GetSource()->Mid(
d1da8872 298 tag.GetBeginPos(),
4d5881b1 299 tag.GetEndPos1()-tag.GetBeginPos());
61c213fe 300#if !wxUSE_UNICODE && wxUSE_WCHAR_T
140ba067 301 wxCSConv conv(m_WParser->GetInputEncoding());
140ba067 302 title = wxString(title.wc_str(conv), wxConvLocal);
140ba067 303#endif
4d5881b1
VS
304 title = m_WParser->GetEntitiesParser()->Parse(title);
305 wfr->OnSetTitle(title);
5526e819
VS
306 }
307 }
d1da8872 308 return true;
5526e819
VS
309 }
310
311TAG_HANDLER_END(TITLE)
312
313
314
315
316TAG_HANDLER_BEGIN(BODY, "BODY")
fc7a2a60 317 TAG_HANDLER_CONSTR(BODY) { }
5526e819
VS
318
319 TAG_HANDLER_PROC(tag)
320 {
5526e819
VS
321 wxColour clr;
322
8bd72d90 323 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
04dbb646 324 {
8bd72d90
VS
325 m_WParser->SetActualColor(clr);
326 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
5526e819
VS
327 }
328
8bd72d90
VS
329 if (tag.GetParamAsColour(wxT("LINK"), &clr))
330 m_WParser->SetLinkColor(clr);
331
97e490f8
VZ
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 wxImage image(*is);
345 if ( image.Ok() )
346 m_WParser->GetWindow()->SetBackgroundImage(image);
347 }
348 }
349 }
350
8bd72d90 351 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
04dbb646 352 {
8bd72d90
VS
353 m_WParser->GetContainer()->InsertCell(
354 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
355 if (m_WParser->GetWindow() != NULL)
356 m_WParser->GetWindow()->SetBackgroundColour(clr);
5526e819 357 }
97e490f8 358
d1da8872 359 return false;
5526e819
VS
360 }
361
362TAG_HANDLER_END(BODY)
363
364
365
366TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
fc7a2a60 367 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
5526e819
VS
368
369 TAG_HANDLER_PROC(tag)
370 {
371 wxHtmlContainerCell *c;
33ac7e6f 372
4f9297b0
VS
373 m_WParser->CloseContainer();
374 c = m_WParser->OpenContainer();
33ac7e6f 375
04dbb646 376 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
4f9297b0 377 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
5526e819 378 else
4f9297b0 379 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
33ac7e6f 380
04dbb646 381 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
4f9297b0 382 m_WParser->OpenContainer();
5526e819 383 ParseInner(tag);
4f9297b0
VS
384 c = m_WParser->CloseContainer();
385 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
386 m_WParser->CloseContainer();
387 m_WParser->OpenContainer();
d1da8872 388 return true;
5526e819
VS
389 }
390
391TAG_HANDLER_END(BLOCKQUOTE)
392
393
394
3c115835
VS
395TAG_HANDLER_BEGIN(SUBSUP, "SUB,SUP")
396
397 TAG_HANDLER_PROC(tag)
398 {
399 bool issub = (tag.GetName() == wxT("SUB"));
400 wxHtmlScriptMode oldmode = m_WParser->GetScriptMode();
401 int oldbase = m_WParser->GetScriptBaseline();
402 int oldsize = m_WParser->GetFontSize();
403
404 wxHtmlContainerCell *cont = m_WParser->GetContainer();
405 wxHtmlCell *c = cont->GetLastChild();
406
407 m_WParser->SetScriptMode(issub ? wxHTML_SCRIPT_SUB : wxHTML_SCRIPT_SUP);
408 m_WParser->SetScriptBaseline(oldbase + c->GetScriptBaseline());
409
410 // select smaller font
411 m_WParser->SetFontSize(m_WParser->GetFontSize()-2);
412 cont->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
413
414 ParseInner(tag);
415
416 // restore font size
417 m_WParser->SetFontSize(oldsize);
418 m_WParser->GetContainer()->InsertCell(
419 new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
420
421 // restore base and alignment
422 m_WParser->SetScriptBaseline(oldbase);
423 m_WParser->SetScriptMode(oldmode);
424
425 return true;
426 }
427
428TAG_HANDLER_END(SUBSUP)
429
430
8829fa82
VS
431// Tag handler for tags that we have to ignore, otherwise non-text data
432// would show up as text:
433TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
fc7a2a60
VZ
434 TAG_HANDLER_CONSTR(DoNothing) { }
435
436 TAG_HANDLER_PROC(WXUNUSED(tag))
8829fa82
VS
437 {
438 return true;
439 }
440TAG_HANDLER_END(DoNothing)
5526e819
VS
441
442
443
3c115835
VS
444
445
5526e819
VS
446TAGS_MODULE_BEGIN(Layout)
447
448 TAGS_MODULE_ADD(P)
449 TAGS_MODULE_ADD(BR)
450 TAGS_MODULE_ADD(CENTER)
451 TAGS_MODULE_ADD(DIV)
452 TAGS_MODULE_ADD(TITLE)
453 TAGS_MODULE_ADD(BODY)
454 TAGS_MODULE_ADD(BLOCKQUOTE)
3c115835 455 TAGS_MODULE_ADD(SUBSUP)
8829fa82 456 TAGS_MODULE_ADD(DoNothing)
5526e819
VS
457
458TAGS_MODULE_END(Layout)
459
460#endif