]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_layout.cpp
879f95a761418c422707ecb1dae9362db567e682
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 /////////////////////////////////////////////////////////////////////////////
9 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
10 #pragma implementation
13 #include "wx/wxprec.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
28 #include "wx/html/forcelnk.h"
29 #include "wx/html/m_templ.h"
31 #include "wx/html/htmlwin.h"
33 FORCE_LINK_ME(m_layout
)
36 #include "wx/msw/wince/missing.h" // for bsearch()
38 #include <stdlib.h> // bsearch()
41 //-----------------------------------------------------------------------------
42 // wxHtmlPageBreakCell
43 //-----------------------------------------------------------------------------
45 // Since html isn't a page-layout language, it doesn't support page
46 // page breaks directly--that requires CSS2 support. But a page-break
47 // facility is handy, and has been requested more than once on the
48 // mailing lists. This wxHtml tag handler implements just enough of
49 // CSS2 to support a page break by recognizing only
50 // <div style="page-break-before:always">
52 // wxHtml maintains page breaks in wxHtmlPrintout::m_PageBreaks. The
53 // tag handler below adds appropriate offsets to that array member.
54 // wxHtmlDCRenderer::Render() accesses that array and makes a new page
55 // begin after each page-break tag.
57 // The page-break handler does all its work in AdjustPagebreak(). For
58 // all tag handlers, that function adjusts the page-break position.
59 // For other tags, it determines whether the html element can fit on
60 // the remainder of the page; if it cannot fit, but must not be split,
61 // then the function moves the page break provided in the argument up,
62 // and returns 'true' to inform the caller that the argument was
65 // Due to its special purpose, the page-break facility differs from
66 // other tags. It takes up no space, but it behaves as though there is
67 // never enough room to fit it on the remainder of the page--it always
68 // forces a page break. Therefore, unlike other elements that trigger
69 // a page break, it would never 'fit' on the following page either.
70 // Therefore it's necessary to compare each pagebreak candidate to the
71 // array wxHtmlPrintout::m_PageBreaks of pagebreaks already set, and
72 // set a new one only if it's not in that array.
74 class wxHtmlPageBreakCell
: public wxHtmlCell
77 wxHtmlPageBreakCell() {}
79 bool AdjustPagebreak(int* pagebreak
,
80 int* known_pagebreaks
= NULL
,
81 int number_of_pages
= 0) const;
82 void Draw(wxDC
& WXUNUSED(dc
),
83 int WXUNUSED(x
), int WXUNUSED(y
),
84 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
85 wxHtmlRenderingInfo
& WXUNUSED(info
)) {}
88 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell
)
91 // Comparison routine for bsearch into an int* array of pagebreaks.
92 extern "C" int wxCMPFUNC_CONV
wxInteger_compare(void const* i0
, void const* i1
)
94 return *(int*)i0
- *(int*)i1
;
97 bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
99 // When we are counting pages, 'known_pagebreaks' is non-NULL.
100 // That's the only time we change 'pagebreak'. Otherwise, pages
101 // were already counted, 'known_pagebreaks' is NULL, and we don't
102 // do anything except return false.
104 // We also simply return false if the 'pagebreak' argument is
105 // less than (vertically above) or the same as the current
106 // vertical position. Otherwise we'd be setting a pagebreak above
107 // the current cell, which is incorrect, or duplicating a
108 // pagebreak that has already been set.
109 if(NULL
== known_pagebreaks
|| *pagebreak
<= m_PosY
)
114 // m_PosY is only the vertical offset from the parent. The pagebreak
115 // required here is the total page offset, so m_PosY must be added
116 // to the parent's offset and height.
117 int total_height
= m_PosY
+ GetParent()->GetPosY() + GetParent()->GetHeight();
119 // Search the array of pagebreaks to see whether we've already set
120 // a pagebreak here. The standard bsearch() function is appropriate
121 // because the array of pagebreaks through known_pagebreaks[number_of_pages]
122 // is known to be sorted in strictly increasing order. '1 + number_of_pages'
123 // is used as a bsearch() argument because the array contains a leading
124 // zero plus one element for each page.
125 int* where
= (int*) bsearch(&total_height
, known_pagebreaks
,
126 1 + number_of_pages
, sizeof(int),
128 // Add a pagebreak only if there isn't one already set here.
140 TAG_HANDLER_BEGIN(P
, "P")
141 TAG_HANDLER_CONSTR(P
) { }
143 TAG_HANDLER_PROC(tag
)
145 if (m_WParser
->GetContainer()->GetFirstChild() != NULL
)
147 m_WParser
->CloseContainer();
148 m_WParser
->OpenContainer();
150 m_WParser
->GetContainer()->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_TOP
);
151 m_WParser
->GetContainer()->SetAlign(tag
);
159 TAG_HANDLER_BEGIN(BR
, "BR")
160 TAG_HANDLER_CONSTR(BR
) { }
162 TAG_HANDLER_PROC(tag
)
164 int al
= m_WParser
->GetContainer()->GetAlignHor();
165 wxHtmlContainerCell
*c
;
167 m_WParser
->CloseContainer();
168 c
= m_WParser
->OpenContainer();
171 c
->SetMinHeight(m_WParser
->GetCharHeight());
179 TAG_HANDLER_BEGIN(CENTER
, "CENTER")
180 TAG_HANDLER_CONSTR(CENTER
) { }
182 TAG_HANDLER_PROC(tag
)
184 int old
= m_WParser
->GetAlign();
185 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
187 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
188 if (c
->GetFirstChild() != NULL
)
190 m_WParser
->CloseContainer();
191 m_WParser
->OpenContainer();
194 c
->SetAlignHor(wxHTML_ALIGN_CENTER
);
200 m_WParser
->SetAlign(old
);
201 if (c
->GetFirstChild() != NULL
)
203 m_WParser
->CloseContainer();
204 m_WParser
->OpenContainer();
214 TAG_HANDLER_END(CENTER
)
218 TAG_HANDLER_BEGIN(DIV
, "DIV")
219 TAG_HANDLER_CONSTR(DIV
) { }
221 TAG_HANDLER_PROC(tag
)
223 if(tag
.HasParam(wxT("STYLE")))
225 if(tag
.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
227 m_WParser
->CloseContainer();
228 m_WParser
->OpenContainer()->InsertCell(new wxHtmlPageBreakCell
);
229 m_WParser
->CloseContainer();
230 m_WParser
->OpenContainer();
235 // Treat other STYLE parameters here when they're supported.
239 else if(tag
.HasParam(wxT("ALIGN")))
241 int old
= m_WParser
->GetAlign();
242 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
243 if (c
->GetFirstChild() != NULL
)
245 m_WParser
->CloseContainer();
246 m_WParser
->OpenContainer();
247 c
= m_WParser
->GetContainer();
249 m_WParser
->SetAlign(c
->GetAlignHor());
254 m_WParser
->SetAlign(c
->GetAlignHor());
259 m_WParser
->SetAlign(old
);
260 if (c
->GetFirstChild() != NULL
)
262 m_WParser
->CloseContainer();
263 m_WParser
->OpenContainer();
273 int al
= m_WParser
->GetContainer()->GetAlignHor();
274 wxHtmlContainerCell
*c
;
276 m_WParser
->CloseContainer();
277 c
= m_WParser
->OpenContainer();
280 c
->SetMinHeight(m_WParser
->GetCharHeight());
290 TAG_HANDLER_BEGIN(TITLE
, "TITLE")
291 TAG_HANDLER_CONSTR(TITLE
) { }
293 TAG_HANDLER_PROC(tag
)
295 if (m_WParser
->GetWindow())
297 wxHtmlWindow
*wfr
= (wxHtmlWindow
*)(m_WParser
->GetWindow());
300 wxString title
= m_WParser
->GetSource()->Mid(
302 tag
.GetEndPos1()-tag
.GetBeginPos());
303 #if !wxUSE_UNICODE && wxUSE_WCHAR_T
304 wxCSConv
conv(m_WParser
->GetInputEncoding());
305 title
= wxString(title
.wc_str(conv
), wxConvLocal
);
307 title
= m_WParser
->GetEntitiesParser()->Parse(title
);
308 wfr
->OnSetTitle(title
);
314 TAG_HANDLER_END(TITLE
)
319 TAG_HANDLER_BEGIN(BODY
, "BODY")
320 TAG_HANDLER_CONSTR(BODY
) { }
322 TAG_HANDLER_PROC(tag
)
326 if (tag
.GetParamAsColour(wxT("TEXT"), &clr
))
328 m_WParser
->SetActualColor(clr
);
329 m_WParser
->GetContainer()->InsertCell(new wxHtmlColourCell(clr
));
332 if (tag
.GetParamAsColour(wxT("LINK"), &clr
))
333 m_WParser
->SetLinkColor(clr
);
335 if (tag
.HasParam(wxT("BACKGROUND")))
337 wxFSFile
*fileBgImage
= m_WParser
->OpenURL
340 tag
.GetParam(wxT("BACKGROUND"))
344 wxInputStream
*is
= fileBgImage
->GetStream();
349 m_WParser
->GetWindow()->SetBackgroundImage(image
);
354 if (tag
.GetParamAsColour(wxT("BGCOLOR"), &clr
))
356 m_WParser
->GetContainer()->InsertCell(
357 new wxHtmlColourCell(clr
, wxHTML_CLR_BACKGROUND
));
358 if (m_WParser
->GetWindow() != NULL
)
359 m_WParser
->GetWindow()->SetBackgroundColour(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 for tags that we have to ignore, otherwise non-text data
399 // would show up as text:
400 TAG_HANDLER_BEGIN(DoNothing
, "SCRIPT")
401 TAG_HANDLER_CONSTR(DoNothing
) { }
403 TAG_HANDLER_PROC(WXUNUSED(tag
))
407 TAG_HANDLER_END(DoNothing
)
411 TAGS_MODULE_BEGIN(Layout
)
415 TAGS_MODULE_ADD(CENTER
)
417 TAGS_MODULE_ADD(TITLE
)
418 TAGS_MODULE_ADD(BODY
)
419 TAGS_MODULE_ADD(BLOCKQUOTE
)
420 TAGS_MODULE_ADD(DoNothing
)
422 TAGS_MODULE_END(Layout
)