1
/////////////////////////////////////////////////////////////////////////////
2
// Name: src/html/m_layout.cpp
3
// Purpose: wxHtml module for basic paragraphs/layout handling
4
// Author: Vaclav Slavik
5
// RCS-ID: $Id: m_layout.cpp 70528 2012-02-07 13:02:32Z VZ $
6
// Copyright: (c) 1999 Vaclav Slavik
7
// Licence: wxWindows licence
8
/////////////////////////////////////////////////////////////////////////////
10
#include "wx/wxprec.h"
16
#if wxUSE_HTML && wxUSE_STREAMS
22
#include "wx/html/forcelnk.h"
23
#include "wx/html/m_templ.h"
25
#include "wx/html/htmlwin.h"
27
FORCE_LINK_ME(m_layout)
30
#include "wx/msw/wince/missing.h" // for bsearch()
32
#include <stdlib.h> // bsearch()
35
//-----------------------------------------------------------------------------
36
// wxHtmlPageBreakCell
37
//-----------------------------------------------------------------------------
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">
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.
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
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.
68
class wxHtmlPageBreakCell : public wxHtmlCell
71
wxHtmlPageBreakCell() {}
73
bool AdjustPagebreak(int* pagebreak,
74
const wxArrayInt& known_pagebreaks,
75
int pageHeight) const;
77
void Draw(wxDC& WXUNUSED(dc),
78
int WXUNUSED(x), int WXUNUSED(y),
79
int WXUNUSED(view_y1), int WXUNUSED(view_y2),
80
wxHtmlRenderingInfo& WXUNUSED(info)) {}
83
wxDECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell);
87
wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak,
88
const wxArrayInt& known_pagebreaks,
89
int WXUNUSED(pageHeight)) const
91
// When we are counting pages, 'known_pagebreaks' is non-NULL.
92
// That's the only time we change 'pagebreak'. Otherwise, pages
93
// were already counted, 'known_pagebreaks' is NULL, and we don't
94
// do anything except return false.
96
// We also simply return false if the 'pagebreak' argument is
97
// less than (vertically above) or the same as the current
98
// vertical position. Otherwise we'd be setting a pagebreak above
99
// the current cell, which is incorrect, or duplicating a
100
// pagebreak that has already been set.
101
if( known_pagebreaks.GetCount() == 0 || *pagebreak <= m_PosY)
106
// m_PosY is only the vertical offset from the parent. The pagebreak
107
// required here is the total page offset, so m_PosY must be added
108
// to the parent's offset and height.
109
int total_height = m_PosY;
110
for ( wxHtmlCell *parent = GetParent(); parent; parent = parent->GetParent() )
112
total_height += parent->GetPosY();
116
// Search the array of pagebreaks to see whether we've already set
118
int where = known_pagebreaks.Index( total_height);
119
// Add a pagebreak only if there isn't one already set here.
120
if( wxNOT_FOUND != where)
133
TAG_HANDLER_BEGIN(P, "P")
134
TAG_HANDLER_CONSTR(P) { }
136
TAG_HANDLER_PROC(tag)
138
if (m_WParser->GetContainer()->GetFirstChild() != NULL)
140
m_WParser->CloseContainer();
141
m_WParser->OpenContainer();
143
m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
144
m_WParser->GetContainer()->SetAlign(tag);
152
TAG_HANDLER_BEGIN(BR, "BR")
153
TAG_HANDLER_CONSTR(BR) { }
155
TAG_HANDLER_PROC(tag)
157
int al = m_WParser->GetContainer()->GetAlignHor();
158
wxHtmlContainerCell *c;
160
m_WParser->CloseContainer();
161
c = m_WParser->OpenContainer();
164
c->SetMinHeight(m_WParser->GetCharHeight());
172
TAG_HANDLER_BEGIN(CENTER, "CENTER")
173
TAG_HANDLER_CONSTR(CENTER) { }
175
TAG_HANDLER_PROC(tag)
177
int old = m_WParser->GetAlign();
178
wxHtmlContainerCell *c = m_WParser->GetContainer();
180
m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
181
if (c->GetFirstChild() != NULL)
183
m_WParser->CloseContainer();
184
m_WParser->OpenContainer();
187
c->SetAlignHor(wxHTML_ALIGN_CENTER);
193
m_WParser->SetAlign(old);
194
if (c->GetFirstChild() != NULL)
196
m_WParser->CloseContainer();
197
m_WParser->OpenContainer();
207
TAG_HANDLER_END(CENTER)
211
TAG_HANDLER_BEGIN(DIV, "DIV")
212
TAG_HANDLER_CONSTR(DIV) { }
214
TAG_HANDLER_PROC(tag)
216
if(tag.HasParam(wxT("STYLE")))
218
if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
220
m_WParser->CloseContainer();
221
m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
222
m_WParser->CloseContainer();
223
m_WParser->OpenContainer();
228
// Treat other STYLE parameters here when they're supported.
232
else if(tag.HasParam(wxT("ALIGN")))
234
int old = m_WParser->GetAlign();
235
wxHtmlContainerCell *c = m_WParser->GetContainer();
236
if (c->GetFirstChild() != NULL)
238
m_WParser->CloseContainer();
239
m_WParser->OpenContainer();
240
c = m_WParser->GetContainer();
242
m_WParser->SetAlign(c->GetAlignHor());
247
m_WParser->SetAlign(c->GetAlignHor());
252
m_WParser->SetAlign(old);
253
if (c->GetFirstChild() != NULL)
255
m_WParser->CloseContainer();
256
m_WParser->OpenContainer();
266
int al = m_WParser->GetContainer()->GetAlignHor();
267
wxHtmlContainerCell *c;
269
m_WParser->CloseContainer();
270
c = m_WParser->OpenContainer();
273
c->SetMinHeight(m_WParser->GetCharHeight());
283
TAG_HANDLER_BEGIN(TITLE, "TITLE")
284
TAG_HANDLER_CONSTR(TITLE) { }
286
TAG_HANDLER_PROC(tag)
288
wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
291
wxString title(tag.GetBeginIter(), tag.GetEndIter1());
293
const wxFontEncoding enc = m_WParser->GetInputEncoding();
294
if ( enc != wxFONTENCODING_DEFAULT )
296
// need to convert to the current one
297
title = wxString(title.wc_str(wxCSConv(enc)), wxConvLocal);
299
#endif // !wxUSE_UNICODE
301
title = m_WParser->GetEntitiesParser()->Parse(title);
303
winIface->SetHTMLWindowTitle(title);
308
TAG_HANDLER_END(TITLE)
313
TAG_HANDLER_BEGIN(BODY, "BODY")
314
TAG_HANDLER_CONSTR(BODY) { }
316
TAG_HANDLER_PROC(tag)
320
if (tag.GetParamAsColour(wxT("TEXT"), &clr))
322
m_WParser->SetActualColor(clr);
323
m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
326
if (tag.GetParamAsColour(wxT("LINK"), &clr))
327
m_WParser->SetLinkColor(clr);
329
wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
330
// the rest of this function requires a window:
334
if (tag.HasParam(wxT("BACKGROUND")))
336
wxFSFile *fileBgImage = m_WParser->OpenURL
339
tag.GetParam(wxT("BACKGROUND"))
343
wxInputStream *is = fileBgImage->GetStream();
348
winIface->SetHTMLBackgroundImage(image);
355
if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
357
m_WParser->GetContainer()->InsertCell(
358
new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
359
winIface->SetHTMLBackgroundColour(clr);
365
TAG_HANDLER_END(BODY)
369
TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
370
TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
372
TAG_HANDLER_PROC(tag)
374
wxHtmlContainerCell *c;
376
m_WParser->CloseContainer();
377
c = m_WParser->OpenContainer();
379
if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
380
c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
382
c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
384
c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
385
m_WParser->OpenContainer();
387
c = m_WParser->CloseContainer();
388
c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
389
m_WParser->CloseContainer();
390
m_WParser->OpenContainer();
394
TAG_HANDLER_END(BLOCKQUOTE)
398
TAG_HANDLER_BEGIN(SUBSUP, "SUB,SUP")
400
TAG_HANDLER_PROC(tag)
402
bool issub = (tag.GetName() == wxT("SUB"));
403
wxHtmlScriptMode oldmode = m_WParser->GetScriptMode();
404
int oldbase = m_WParser->GetScriptBaseline();
405
int oldsize = m_WParser->GetFontSize();
407
wxHtmlContainerCell *cont = m_WParser->GetContainer();
408
wxHtmlCell *c = cont->GetLastChild();
410
m_WParser->SetScriptMode(issub ? wxHTML_SCRIPT_SUB : wxHTML_SCRIPT_SUP);
411
m_WParser->SetScriptBaseline(
412
oldbase + c ? c->GetScriptBaseline() : 0);
414
// select smaller font
415
m_WParser->SetFontSize(m_WParser->GetFontSize()-2);
416
cont->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
421
m_WParser->SetFontSize(oldsize);
422
m_WParser->GetContainer()->InsertCell(
423
new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
425
// restore base and alignment
426
m_WParser->SetScriptBaseline(oldbase);
427
m_WParser->SetScriptMode(oldmode);
432
TAG_HANDLER_END(SUBSUP)
435
// Tag handler for tags that we have to ignore, otherwise non-text data
436
// would show up as text:
437
TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
438
TAG_HANDLER_CONSTR(DoNothing) { }
440
TAG_HANDLER_PROC(WXUNUSED(tag))
444
TAG_HANDLER_END(DoNothing)
450
TAGS_MODULE_BEGIN(Layout)
454
TAGS_MODULE_ADD(CENTER)
456
TAGS_MODULE_ADD(TITLE)
457
TAGS_MODULE_ADD(BODY)
458
TAGS_MODULE_ADD(BLOCKQUOTE)
459
TAGS_MODULE_ADD(SUBSUP)
460
TAGS_MODULE_ADD(DoNothing)
462
TAGS_MODULE_END(Layout)