]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_layout.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml module for basic paragraphs/layout handling
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
14 #if wxUSE_HTML && wxUSE_STREAMS
25 #include "wx/html/forcelnk.h"
26 #include "wx/html/m_templ.h"
28 #include "wx/html/htmlwin.h"
30 FORCE_LINK_ME(m_layout
)
33 #include "wx/msw/wince/missing.h" // for bsearch()
35 #include <stdlib.h> // bsearch()
38 //-----------------------------------------------------------------------------
39 // wxHtmlPageBreakCell
40 //-----------------------------------------------------------------------------
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">
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.
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
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.
71 class wxHtmlPageBreakCell
: public wxHtmlCell
74 wxHtmlPageBreakCell() {}
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
),
82 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
85 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell
)
88 // Comparison routine for bsearch into an int* array of pagebreaks.
89 extern "C" int wxCMPFUNC_CONV
wxInteger_compare(void const* i0
, void const* i1
)
91 return *(int*)i0
- *(int*)i1
;
94 bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
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
99 // do anything except return false.
101 // We also simply return false if the 'pagebreak' argument is
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
)
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();
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),
125 // Add a pagebreak only if there isn't one already set here.
137 TAG_HANDLER_BEGIN(P
, "P")
138 TAG_HANDLER_CONSTR(P
) { }
140 TAG_HANDLER_PROC(tag
)
142 if (m_WParser
->GetContainer()->GetFirstChild() != NULL
)
144 m_WParser
->CloseContainer();
145 m_WParser
->OpenContainer();
147 m_WParser
->GetContainer()->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_TOP
);
148 m_WParser
->GetContainer()->SetAlign(tag
);
156 TAG_HANDLER_BEGIN(BR
, "BR")
157 TAG_HANDLER_CONSTR(BR
) { }
159 TAG_HANDLER_PROC(tag
)
161 int al
= m_WParser
->GetContainer()->GetAlignHor();
162 wxHtmlContainerCell
*c
;
164 m_WParser
->CloseContainer();
165 c
= m_WParser
->OpenContainer();
168 c
->SetMinHeight(m_WParser
->GetCharHeight());
176 TAG_HANDLER_BEGIN(CENTER
, "CENTER")
177 TAG_HANDLER_CONSTR(CENTER
) { }
179 TAG_HANDLER_PROC(tag
)
181 int old
= m_WParser
->GetAlign();
182 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
184 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
185 if (c
->GetFirstChild() != NULL
)
187 m_WParser
->CloseContainer();
188 m_WParser
->OpenContainer();
191 c
->SetAlignHor(wxHTML_ALIGN_CENTER
);
197 m_WParser
->SetAlign(old
);
198 if (c
->GetFirstChild() != NULL
)
200 m_WParser
->CloseContainer();
201 m_WParser
->OpenContainer();
211 TAG_HANDLER_END(CENTER
)
215 TAG_HANDLER_BEGIN(DIV
, "DIV")
216 TAG_HANDLER_CONSTR(DIV
) { }
218 TAG_HANDLER_PROC(tag
)
220 if(tag
.HasParam(wxT("STYLE")))
222 if(tag
.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
224 m_WParser
->CloseContainer();
225 m_WParser
->OpenContainer()->InsertCell(new wxHtmlPageBreakCell
);
226 m_WParser
->CloseContainer();
227 m_WParser
->OpenContainer();
232 // Treat other STYLE parameters here when they're supported.
236 else if(tag
.HasParam(wxT("ALIGN")))
238 int old
= m_WParser
->GetAlign();
239 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
240 if (c
->GetFirstChild() != NULL
)
242 m_WParser
->CloseContainer();
243 m_WParser
->OpenContainer();
244 c
= m_WParser
->GetContainer();
246 m_WParser
->SetAlign(c
->GetAlignHor());
251 m_WParser
->SetAlign(c
->GetAlignHor());
256 m_WParser
->SetAlign(old
);
257 if (c
->GetFirstChild() != NULL
)
259 m_WParser
->CloseContainer();
260 m_WParser
->OpenContainer();
270 int al
= m_WParser
->GetContainer()->GetAlignHor();
271 wxHtmlContainerCell
*c
;
273 m_WParser
->CloseContainer();
274 c
= m_WParser
->OpenContainer();
277 c
->SetMinHeight(m_WParser
->GetCharHeight());
287 TAG_HANDLER_BEGIN(TITLE
, "TITLE")
288 TAG_HANDLER_CONSTR(TITLE
) { }
290 TAG_HANDLER_PROC(tag
)
292 if (m_WParser
->GetWindow())
294 wxHtmlWindow
*wfr
= (wxHtmlWindow
*)(m_WParser
->GetWindow());
297 wxString title
= m_WParser
->GetSource()->Mid(
299 tag
.GetEndPos1()-tag
.GetBeginPos());
300 #if !wxUSE_UNICODE && wxUSE_WCHAR_T
301 wxCSConv
conv(m_WParser
->GetInputEncoding());
302 title
= wxString(title
.wc_str(conv
), wxConvLocal
);
304 title
= m_WParser
->GetEntitiesParser()->Parse(title
);
305 wfr
->OnSetTitle(title
);
311 TAG_HANDLER_END(TITLE
)
316 TAG_HANDLER_BEGIN(BODY
, "BODY")
317 TAG_HANDLER_CONSTR(BODY
) { }
319 TAG_HANDLER_PROC(tag
)
323 if (tag
.GetParamAsColour(wxT("TEXT"), &clr
))
325 m_WParser
->SetActualColor(clr
);
326 m_WParser
->GetContainer()->InsertCell(new wxHtmlColourCell(clr
));
329 if (tag
.GetParamAsColour(wxT("LINK"), &clr
))
330 m_WParser
->SetLinkColor(clr
);
332 if (tag
.HasParam(wxT("BACKGROUND")))
334 wxFSFile
*fileBgImage
= m_WParser
->OpenURL
337 tag
.GetParam(wxT("BACKGROUND"))
341 wxInputStream
*is
= fileBgImage
->GetStream();
344 #if !defined(__WXMSW__) || wxUSE_WXDIB
347 m_WParser
->GetWindow()->SetBackgroundImage(image
);
353 if (tag
.GetParamAsColour(wxT("BGCOLOR"), &clr
))
355 m_WParser
->GetContainer()->InsertCell(
356 new wxHtmlColourCell(clr
, wxHTML_CLR_BACKGROUND
));
357 if (m_WParser
->GetWindow() != NULL
)
358 m_WParser
->GetWindow()->SetBackgroundColour(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(oldbase
+ c
->GetScriptBaseline());
412 // select smaller font
413 m_WParser
->SetFontSize(m_WParser
->GetFontSize()-2);
414 cont
->InsertCell(new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
419 m_WParser
->SetFontSize(oldsize
);
420 m_WParser
->GetContainer()->InsertCell(
421 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
423 // restore base and alignment
424 m_WParser
->SetScriptBaseline(oldbase
);
425 m_WParser
->SetScriptMode(oldmode
);
430 TAG_HANDLER_END(SUBSUP
)
433 // Tag handler for tags that we have to ignore, otherwise non-text data
434 // would show up as text:
435 TAG_HANDLER_BEGIN(DoNothing
, "SCRIPT")
436 TAG_HANDLER_CONSTR(DoNothing
) { }
438 TAG_HANDLER_PROC(WXUNUSED(tag
))
442 TAG_HANDLER_END(DoNothing
)
448 TAGS_MODULE_BEGIN(Layout
)
452 TAGS_MODULE_ADD(CENTER
)
454 TAGS_MODULE_ADD(TITLE
)
455 TAGS_MODULE_ADD(BODY
)
456 TAGS_MODULE_ADD(BLOCKQUOTE
)
457 TAGS_MODULE_ADD(SUBSUP
)
458 TAGS_MODULE_ADD(DoNothing
)
460 TAGS_MODULE_END(Layout
)