]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_layout.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/m_layout.cpp
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"
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 wxArrayInt
& known_pagebreaks
) 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 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell
)
85 // Comparison routine for bsearch into an int* array of pagebreaks.
88 static int wxCMPFUNC_CONV
wxInteger_compare(void const* i0
, void const* i1
)
90 return *(int*)i0
- *(int*)i1
;
94 bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak
, wxArrayInt
& known_pagebreaks
) 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( known_pagebreaks
.GetCount() == 0 || *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
;
115 for ( wxHtmlCell
*parent
= GetParent(); parent
; parent
= parent
->GetParent() )
117 total_height
+= parent
->GetPosY();
121 // Search the array of pagebreaks to see whether we've already set
122 // a pagebreak here. The standard bsearch() function is appropriate
123 // because the array of pagebreaks through known_pagebreaks[number_of_pages]
124 // is known to be sorted in strictly increasing order. '1 + number_of_pages'
125 // is used as a bsearch() argument because the array contains a leading
126 // zero plus one element for each page.
127 int where
= known_pagebreaks
.Index( total_height
);
128 // Add a pagebreak only if there isn't one already set here.
129 if( wxNOT_FOUND
!= where
)
142 TAG_HANDLER_BEGIN(P
, "P")
143 TAG_HANDLER_CONSTR(P
) { }
145 TAG_HANDLER_PROC(tag
)
147 if (m_WParser
->GetContainer()->GetFirstChild() != NULL
)
149 m_WParser
->CloseContainer();
150 m_WParser
->OpenContainer();
152 m_WParser
->GetContainer()->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_TOP
);
153 m_WParser
->GetContainer()->SetAlign(tag
);
161 TAG_HANDLER_BEGIN(BR
, "BR")
162 TAG_HANDLER_CONSTR(BR
) { }
164 TAG_HANDLER_PROC(tag
)
166 int al
= m_WParser
->GetContainer()->GetAlignHor();
167 wxHtmlContainerCell
*c
;
169 m_WParser
->CloseContainer();
170 c
= m_WParser
->OpenContainer();
173 c
->SetMinHeight(m_WParser
->GetCharHeight());
181 TAG_HANDLER_BEGIN(CENTER
, "CENTER")
182 TAG_HANDLER_CONSTR(CENTER
) { }
184 TAG_HANDLER_PROC(tag
)
186 int old
= m_WParser
->GetAlign();
187 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
189 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
190 if (c
->GetFirstChild() != NULL
)
192 m_WParser
->CloseContainer();
193 m_WParser
->OpenContainer();
196 c
->SetAlignHor(wxHTML_ALIGN_CENTER
);
202 m_WParser
->SetAlign(old
);
203 if (c
->GetFirstChild() != NULL
)
205 m_WParser
->CloseContainer();
206 m_WParser
->OpenContainer();
216 TAG_HANDLER_END(CENTER
)
220 TAG_HANDLER_BEGIN(DIV
, "DIV")
221 TAG_HANDLER_CONSTR(DIV
) { }
223 TAG_HANDLER_PROC(tag
)
225 if(tag
.HasParam(wxT("STYLE")))
227 if(tag
.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
229 m_WParser
->CloseContainer();
230 m_WParser
->OpenContainer()->InsertCell(new wxHtmlPageBreakCell
);
231 m_WParser
->CloseContainer();
232 m_WParser
->OpenContainer();
237 // Treat other STYLE parameters here when they're supported.
241 else if(tag
.HasParam(wxT("ALIGN")))
243 int old
= m_WParser
->GetAlign();
244 wxHtmlContainerCell
*c
= m_WParser
->GetContainer();
245 if (c
->GetFirstChild() != NULL
)
247 m_WParser
->CloseContainer();
248 m_WParser
->OpenContainer();
249 c
= m_WParser
->GetContainer();
251 m_WParser
->SetAlign(c
->GetAlignHor());
256 m_WParser
->SetAlign(c
->GetAlignHor());
261 m_WParser
->SetAlign(old
);
262 if (c
->GetFirstChild() != NULL
)
264 m_WParser
->CloseContainer();
265 m_WParser
->OpenContainer();
275 int al
= m_WParser
->GetContainer()->GetAlignHor();
276 wxHtmlContainerCell
*c
;
278 m_WParser
->CloseContainer();
279 c
= m_WParser
->OpenContainer();
282 c
->SetMinHeight(m_WParser
->GetCharHeight());
292 TAG_HANDLER_BEGIN(TITLE
, "TITLE")
293 TAG_HANDLER_CONSTR(TITLE
) { }
295 TAG_HANDLER_PROC(tag
)
297 wxHtmlWindowInterface
*winIface
= m_WParser
->GetWindowInterface();
300 wxString
title(tag
.GetBeginIter(), tag
.GetEndIter1());
301 #if !wxUSE_UNICODE && wxUSE_WCHAR_T
302 const wxFontEncoding enc
= m_WParser
->GetInputEncoding();
303 if ( enc
!= wxFONTENCODING_DEFAULT
)
305 // need to convert to the current one
306 title
= wxString(title
.wc_str(wxCSConv(enc
)), wxConvLocal
);
308 #endif // !wxUSE_UNICODE
310 title
= m_WParser
->GetEntitiesParser()->Parse(title
);
312 winIface
->SetHTMLWindowTitle(title
);
317 TAG_HANDLER_END(TITLE
)
322 TAG_HANDLER_BEGIN(BODY
, "BODY")
323 TAG_HANDLER_CONSTR(BODY
) { }
325 TAG_HANDLER_PROC(tag
)
329 if (tag
.GetParamAsColour(wxT("TEXT"), &clr
))
331 m_WParser
->SetActualColor(clr
);
332 m_WParser
->GetContainer()->InsertCell(new wxHtmlColourCell(clr
));
335 if (tag
.GetParamAsColour(wxT("LINK"), &clr
))
336 m_WParser
->SetLinkColor(clr
);
338 wxHtmlWindowInterface
*winIface
= m_WParser
->GetWindowInterface();
339 // the rest of this function requires a window:
343 if (tag
.HasParam(wxT("BACKGROUND")))
345 wxFSFile
*fileBgImage
= m_WParser
->OpenURL
348 tag
.GetParam(wxT("BACKGROUND"))
352 wxInputStream
*is
= fileBgImage
->GetStream();
357 winIface
->SetHTMLBackgroundImage(image
);
364 if (tag
.GetParamAsColour(wxT("BGCOLOR"), &clr
))
366 m_WParser
->GetContainer()->InsertCell(
367 new wxHtmlColourCell(clr
, wxHTML_CLR_BACKGROUND
));
368 winIface
->SetHTMLBackgroundColour(clr
);
374 TAG_HANDLER_END(BODY
)
378 TAG_HANDLER_BEGIN(BLOCKQUOTE
, "BLOCKQUOTE")
379 TAG_HANDLER_CONSTR(BLOCKQUOTE
) { }
381 TAG_HANDLER_PROC(tag
)
383 wxHtmlContainerCell
*c
;
385 m_WParser
->CloseContainer();
386 c
= m_WParser
->OpenContainer();
388 if (c
->GetAlignHor() == wxHTML_ALIGN_RIGHT
)
389 c
->SetIndent(5 * m_WParser
->GetCharWidth(), wxHTML_INDENT_RIGHT
);
391 c
->SetIndent(5 * m_WParser
->GetCharWidth(), wxHTML_INDENT_LEFT
);
393 c
->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_TOP
);
394 m_WParser
->OpenContainer();
396 c
= m_WParser
->CloseContainer();
397 c
->SetIndent(m_WParser
->GetCharHeight(), wxHTML_INDENT_BOTTOM
);
398 m_WParser
->CloseContainer();
399 m_WParser
->OpenContainer();
403 TAG_HANDLER_END(BLOCKQUOTE
)
407 TAG_HANDLER_BEGIN(SUBSUP
, "SUB,SUP")
409 TAG_HANDLER_PROC(tag
)
411 bool issub
= (tag
.GetName() == wxT("SUB"));
412 wxHtmlScriptMode oldmode
= m_WParser
->GetScriptMode();
413 int oldbase
= m_WParser
->GetScriptBaseline();
414 int oldsize
= m_WParser
->GetFontSize();
416 wxHtmlContainerCell
*cont
= m_WParser
->GetContainer();
417 wxHtmlCell
*c
= cont
->GetLastChild();
419 m_WParser
->SetScriptMode(issub
? wxHTML_SCRIPT_SUB
: wxHTML_SCRIPT_SUP
);
420 m_WParser
->SetScriptBaseline(
421 oldbase
+ c
? c
->GetScriptBaseline() : 0);
423 // select smaller font
424 m_WParser
->SetFontSize(m_WParser
->GetFontSize()-2);
425 cont
->InsertCell(new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
430 m_WParser
->SetFontSize(oldsize
);
431 m_WParser
->GetContainer()->InsertCell(
432 new wxHtmlFontCell(m_WParser
->CreateCurrentFont()));
434 // restore base and alignment
435 m_WParser
->SetScriptBaseline(oldbase
);
436 m_WParser
->SetScriptMode(oldmode
);
441 TAG_HANDLER_END(SUBSUP
)
444 // Tag handler for tags that we have to ignore, otherwise non-text data
445 // would show up as text:
446 TAG_HANDLER_BEGIN(DoNothing
, "SCRIPT")
447 TAG_HANDLER_CONSTR(DoNothing
) { }
449 TAG_HANDLER_PROC(WXUNUSED(tag
))
453 TAG_HANDLER_END(DoNothing
)
459 TAGS_MODULE_BEGIN(Layout
)
463 TAGS_MODULE_ADD(CENTER
)
465 TAGS_MODULE_ADD(TITLE
)
466 TAGS_MODULE_ADD(BODY
)
467 TAGS_MODULE_ADD(BLOCKQUOTE
)
468 TAGS_MODULE_ADD(SUBSUP
)
469 TAGS_MODULE_ADD(DoNothing
)
471 TAGS_MODULE_END(Layout
)