1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/m_layout.cpp
3 // Purpose: wxHtml module for basic paragraphs/layout handling
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
15 #if wxUSE_HTML && wxUSE_STREAMS
21 #include "wx/html/forcelnk.h"
22 #include "wx/html/m_templ.h"
24 #include "wx/html/htmlwin.h"
26 FORCE_LINK_ME(m_layout
)
29 #include "wx/msw/wince/missing.h" // for bsearch()
31 #include <stdlib.h> // bsearch()
34 //-----------------------------------------------------------------------------
35 // wxHtmlPageBreakCell
36 //-----------------------------------------------------------------------------
38 // Since html isn't a page-layout language, it doesn't support page
39 // page breaks directly--that requires CSS2 support. But a page-break
40 // facility is handy, and has been requested more than once on the
41 // mailing lists. This wxHtml tag handler implements just enough of
42 // CSS2 to support a page break by recognizing only
43 // <div style="page-break-before:always">
45 // wxHtml maintains page breaks in wxHtmlPrintout::m_PageBreaks. The
46 // tag handler below adds appropriate offsets to that array member.
47 // wxHtmlDCRenderer::Render() accesses that array and makes a new page
48 // begin after each page-break tag.
50 // The page-break handler does all its work in AdjustPagebreak(). For
51 // all tag handlers, that function adjusts the page-break position.
52 // For other tags, it determines whether the html element can fit on
53 // the remainder of the page; if it cannot fit, but must not be split,
54 // then the function moves the page break provided in the argument up,
55 // and returns 'true' to inform the caller that the argument was
58 // Due to its special purpose, the page-break facility differs from
59 // other tags. It takes up no space, but it behaves as though there is
60 // never enough room to fit it on the remainder of the page--it always
61 // forces a page break. Therefore, unlike other elements that trigger
62 // a page break, it would never 'fit' on the following page either.
63 // Therefore it's necessary to compare each pagebreak candidate to the
64 // array wxHtmlPrintout::m_PageBreaks of pagebreaks already set, and
65 // set a new one only if it's not in that array.
67 class wxHtmlPageBreakCell
: public wxHtmlCell
70 wxHtmlPageBreakCell() {}
72 bool AdjustPagebreak(int* pagebreak
,
73 const wxArrayInt
& known_pagebreaks
,
74 int pageHeight
) const;
76 void Draw(wxDC
& WXUNUSED(dc
),
77 int WXUNUSED(x
), int WXUNUSED(y
),
78 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
79 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
82 wxDECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell
);
86 wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak
,
87 const wxArrayInt
& known_pagebreaks
,
88 int WXUNUSED(pageHeight
)) const
90 // When we are counting pages, 'known_pagebreaks' is non-NULL.
91 // That's the only time we change 'pagebreak'. Otherwise, pages
92 // were already counted, 'known_pagebreaks' is NULL, and we don't
93 // do anything except return false.
95 // We also simply return false if the 'pagebreak' argument is
96 // less than (vertically above) or the same as the current
97 // vertical position. Otherwise we'd be setting a pagebreak above
98 // the current cell, which is incorrect, or duplicating a
99 // pagebreak that has already been set.
100 if( known_pagebreaks
.GetCount() == 0 || *pagebreak
<= m_PosY
)
105 // m_PosY is only the vertical offset from the parent. The pagebreak
106 // required here is the total page offset, so m_PosY must be added
107 // to the parent's offset and height.
108 int total_height
= m_PosY
;
109 for ( wxHtmlCell
*parent
= GetParent(); parent
; parent
= parent
->GetParent() )
111 total_height
+= parent
->GetPosY();
115 // Search the array of pagebreaks to see whether we've already set
117 int where
= known_pagebreaks
.Index( total_height
);
118 // Add a pagebreak only if there isn't one already set here.
119 if( wxNOT_FOUND
!= where
)
132 TAG_HANDLER_BEGIN(P
, "P")
133 TAG_HANDLER_CONSTR(P
) { }
135 TAG_HANDLER_PROC(tag
)
137 if (m_WParser
->GetContainer()->GetFirstChild() != NULL
)
139 m_WParser
->CloseContainer();
140 m_WParser
->OpenContainer();
142 m_WParser
->GetContainer()->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_TOP
);
143 m_WParser
->GetContainer()->SetAlign(tag
);
151 TAG_HANDLER_BEGIN(BR
, "BR")
152 TAG_HANDLER_CONSTR(BR
) { }
154 TAG_HANDLER_PROC(tag
)
156 int al
= m_WParser
->GetContainer()->GetAlignHor();
157 wxHtmlContainerCell
*c
;
159 m_WParser
->CloseContainer();
160 c
= m_WParser
->OpenContainer();
163 c
->SetMinHeight(m_WParser
->GetCharHeight());
171 TAG_HANDLER_BEGIN(CENTER
, "CENTER")
172 TAG_HANDLER_CONSTR(CENTER
) { }
174 TAG_HANDLER_PROC(tag
)
176 int old
= m_WParser
->GetAlign();
177 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
179 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
180 if (c
->GetFirstChild() != NULL
)
182 m_WParser
->CloseContainer();
183 m_WParser
->OpenContainer();
186 c
->SetAlignHor(wxHTML_ALIGN_CENTER
);
192 m_WParser
->SetAlign(old
);
193 if (c
->GetFirstChild() != NULL
)
195 m_WParser
->CloseContainer();
196 m_WParser
->OpenContainer();
206 TAG_HANDLER_END(CENTER
)
210 TAG_HANDLER_BEGIN(DIV
, "DIV")
211 TAG_HANDLER_CONSTR(DIV
) { }
213 TAG_HANDLER_PROC(tag
)
215 if(tag
.HasParam(wxT("STYLE")))
217 if(tag
.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
219 m_WParser
->CloseContainer();
220 m_WParser
->OpenContainer()->InsertCell(new wxHtmlPageBreakCell
);
221 m_WParser
->CloseContainer();
222 m_WParser
->OpenContainer();
227 // Treat other STYLE parameters here when they're supported.
231 else if(tag
.HasParam(wxT("ALIGN")))
233 int old
= m_WParser
->GetAlign();
234 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
235 if (c
->GetFirstChild() != NULL
)
237 m_WParser
->CloseContainer();
238 m_WParser
->OpenContainer();
239 c
= m_WParser
->GetContainer();
241 m_WParser
->SetAlign(c
->GetAlignHor());
246 m_WParser
->SetAlign(c
->GetAlignHor());
251 m_WParser
->SetAlign(old
);
252 if (c
->GetFirstChild() != NULL
)
254 m_WParser
->CloseContainer();
255 m_WParser
->OpenContainer();
265 int al
= m_WParser
->GetContainer()->GetAlignHor();
266 wxHtmlContainerCell
*c
;
268 m_WParser
->CloseContainer();
269 c
= m_WParser
->OpenContainer();
272 c
->SetMinHeight(m_WParser
->GetCharHeight());
282 TAG_HANDLER_BEGIN(TITLE
, "TITLE")
283 TAG_HANDLER_CONSTR(TITLE
) { }
285 TAG_HANDLER_PROC(tag
)
287 wxHtmlWindowInterface
*winIface
= m_WParser
->GetWindowInterface();
290 wxString
title(tag
.GetBeginIter(), tag
.GetEndIter1());
292 const wxFontEncoding enc
= m_WParser
->GetInputEncoding();
293 if ( enc
!= wxFONTENCODING_DEFAULT
)
295 // need to convert to the current one
296 title
= wxString(title
.wc_str(wxCSConv(enc
)), wxConvLocal
);
298 #endif // !wxUSE_UNICODE
300 title
= m_WParser
->GetEntitiesParser()->Parse(title
);
302 winIface
->SetHTMLWindowTitle(title
);
307 TAG_HANDLER_END(TITLE
)
312 TAG_HANDLER_BEGIN(BODY
, "BODY")
313 TAG_HANDLER_CONSTR(BODY
) { }
315 TAG_HANDLER_PROC(tag
)
319 if (tag
.GetParamAsColour(wxT("TEXT"), &clr
))
321 m_WParser
->SetActualColor(clr
);
322 m_WParser
->GetContainer()->InsertCell(new wxHtmlColourCell(clr
));
325 if (tag
.GetParamAsColour(wxT("LINK"), &clr
))
326 m_WParser
->SetLinkColor(clr
);
328 wxHtmlWindowInterface
*winIface
= m_WParser
->GetWindowInterface();
329 // the rest of this function requires a window:
333 if (tag
.HasParam(wxT("BACKGROUND")))
335 wxFSFile
*fileBgImage
= m_WParser
->OpenURL
338 tag
.GetParam(wxT("BACKGROUND"))
342 wxInputStream
*is
= fileBgImage
->GetStream();
347 winIface
->SetHTMLBackgroundImage(image
);
354 if (tag
.GetParamAsColour(wxT("BGCOLOR"), &clr
))
356 m_WParser
->GetContainer()->InsertCell(
357 new wxHtmlColourCell(clr
, wxHTML_CLR_TRANSPARENT_BACKGROUND
));
358 winIface
->SetHTMLBackgroundColour(clr
);
364 TAG_HANDLER_END(BODY
)
368 TAG_HANDLER_BEGIN(BLOCKQUOTE
, "BLOCKQUOTE")
369 TAG_HANDLER_CONSTR(BLOCKQUOTE
) { }
371 TAG_HANDLER_PROC(tag
)
373 wxHtmlContainerCell
*c
;
375 m_WParser
->CloseContainer();
376 c
= m_WParser
->OpenContainer();
378 if (c
->GetAlignHor() == wxHTML_ALIGN_RIGHT
)
379 c
->SetIndent(5 * m_WParser
->GetCharWidth(), wxHTML_INDENT_RIGHT
);
381 c
->SetIndent(5 * m_WParser
->GetCharWidth(), wxHTML_INDENT_LEFT
);
383 c
->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_TOP
);
384 m_WParser
->OpenContainer();
386 c
= m_WParser
->CloseContainer();
387 c
->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_BOTTOM
);
388 m_WParser
->CloseContainer();
389 m_WParser
->OpenContainer();
393 TAG_HANDLER_END(BLOCKQUOTE
)
397 TAG_HANDLER_BEGIN(SUBSUP
, "SUB,SUP")
399 TAG_HANDLER_PROC(tag
)
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();
406 wxHtmlContainerCell
*cont
= m_WParser
->GetContainer();
407 wxHtmlCell
*c
= cont
->GetLastChild();
409 m_WParser
->SetScriptMode(issub
? wxHTML_SCRIPT_SUB
: wxHTML_SCRIPT_SUP
);
410 m_WParser
->SetScriptBaseline(
411 oldbase
+ c
? c
->GetScriptBaseline() : 0);
413 // select smaller font
414 m_WParser
->SetFontSize(m_WParser
->GetFontSize()-2);
415 cont
->InsertCell(new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
420 m_WParser
->SetFontSize(oldsize
);
421 m_WParser
->GetContainer()->InsertCell(
422 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
424 // restore base and alignment
425 m_WParser
->SetScriptBaseline(oldbase
);
426 m_WParser
->SetScriptMode(oldmode
);
431 TAG_HANDLER_END(SUBSUP
)
434 // Tag handler for tags that we have to ignore, otherwise non-text data
435 // would show up as text:
436 TAG_HANDLER_BEGIN(DoNothing
, "SCRIPT")
437 TAG_HANDLER_CONSTR(DoNothing
) { }
439 TAG_HANDLER_PROC(WXUNUSED(tag
))
443 TAG_HANDLER_END(DoNothing
)
449 TAGS_MODULE_BEGIN(Layout
)
453 TAGS_MODULE_ADD(CENTER
)
455 TAGS_MODULE_ADD(TITLE
)
456 TAGS_MODULE_ADD(BODY
)
457 TAGS_MODULE_ADD(BLOCKQUOTE
)
458 TAGS_MODULE_ADD(SUBSUP
)
459 TAGS_MODULE_ADD(DoNothing
)
461 TAGS_MODULE_END(Layout
)