]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_layout.cpp
Merge in from trunk r64802 - r68625
[wxWidgets.git] / src / html / m_layout.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/html/m_layout.cpp
5526e819
VS
3// Purpose: wxHtml module for basic paragraphs/layout handling
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819 6// Copyright: (c) 1999 Vaclav Slavik
65571936 7// Licence: wxWindows licence
5526e819 8/////////////////////////////////////////////////////////////////////////////
3364ab79 9
314260fb 10#include "wx/wxprec.h"
5526e819 11
2b5f62a0 12#ifdef __BORLANDC__
97e490f8 13 #pragma hdrstop
3364ab79
RS
14#endif
15
93763ad5
WS
16#if wxUSE_HTML && wxUSE_STREAMS
17
b4f4d3dd 18#ifndef WX_PRECOMP
155ecd4c 19 #include "wx/image.h"
3364ab79
RS
20#endif
21
69941f05
VS
22#include "wx/html/forcelnk.h"
23#include "wx/html/m_templ.h"
5526e819 24
69941f05 25#include "wx/html/htmlwin.h"
5526e819 26
c88293a4 27FORCE_LINK_ME(m_layout)
5526e819 28
7127d129
RR
29#ifdef __WXWINCE__
30 #include "wx/msw/wince/missing.h" // for bsearch()
31#else
32 #include <stdlib.h> // bsearch()
33#endif
f2034f1b
VS
34
35//-----------------------------------------------------------------------------
36// wxHtmlPageBreakCell
37//-----------------------------------------------------------------------------
38
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">
45//
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.
50
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
57// modified.
58//
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.
67
4460b6c4 68class wxHtmlPageBreakCell : public wxHtmlCell
f2034f1b 69{
36c4ff4d 70public:
f2034f1b
VS
71 wxHtmlPageBreakCell() {}
72
36c4ff4d 73 bool AdjustPagebreak(int* pagebreak,
fd0bab43
VZ
74 wxArrayInt& known_pagebreaks) const;
75
36c4ff4d
VS
76 void Draw(wxDC& WXUNUSED(dc),
77 int WXUNUSED(x), int WXUNUSED(y),
78 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
e3ac6ee1 79 wxHtmlRenderingInfo& WXUNUSED(info)) {}
f2034f1b 80
36c4ff4d 81private:
c0c133e1 82 wxDECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell);
f2034f1b
VS
83};
84
fd0bab43 85bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak, wxArrayInt& known_pagebreaks) const
f2034f1b
VS
86{
87 // When we are counting pages, 'known_pagebreaks' is non-NULL.
88 // That's the only time we change 'pagebreak'. Otherwise, pages
89 // were already counted, 'known_pagebreaks' is NULL, and we don't
d1da8872 90 // do anything except return false.
f2034f1b 91 //
d1da8872 92 // We also simply return false if the 'pagebreak' argument is
f2034f1b
VS
93 // less than (vertically above) or the same as the current
94 // vertical position. Otherwise we'd be setting a pagebreak above
95 // the current cell, which is incorrect, or duplicating a
96 // pagebreak that has already been set.
b4a980f4 97 if( known_pagebreaks.GetCount() == 0 || *pagebreak <= m_PosY)
fd0bab43 98 {
d1da8872 99 return false;
fd0bab43 100 }
f2034f1b
VS
101
102 // m_PosY is only the vertical offset from the parent. The pagebreak
103 // required here is the total page offset, so m_PosY must be added
104 // to the parent's offset and height.
c3485a4e
VS
105 int total_height = m_PosY;
106 for ( wxHtmlCell *parent = GetParent(); parent; parent = parent->GetParent() )
107 {
108 total_height += parent->GetPosY();
109 }
110
f2034f1b
VS
111
112 // Search the array of pagebreaks to see whether we've already set
e92b9e69 113 // a pagebreak here.
fd0bab43 114 int where = known_pagebreaks.Index( total_height);
f2034f1b 115 // Add a pagebreak only if there isn't one already set here.
fd0bab43
VZ
116 if( wxNOT_FOUND != where)
117 {
d1da8872 118 return false;
fd0bab43 119 }
f2034f1b 120 else
fd0bab43 121 {
f2034f1b 122 *pagebreak = m_PosY;
d1da8872 123 return true;
fd0bab43 124 }
f2034f1b
VS
125}
126
fd0bab43
VZ
127
128
5526e819 129TAG_HANDLER_BEGIN(P, "P")
fc7a2a60 130 TAG_HANDLER_CONSTR(P) { }
5526e819
VS
131
132 TAG_HANDLER_PROC(tag)
133 {
e3774124 134 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
04dbb646 135 {
4f9297b0
VS
136 m_WParser->CloseContainer();
137 m_WParser->OpenContainer();
8c651ab7 138 }
4f9297b0
VS
139 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
140 m_WParser->GetContainer()->SetAlign(tag);
d1da8872 141 return false;
5526e819
VS
142 }
143
144TAG_HANDLER_END(P)
145
146
147
148TAG_HANDLER_BEGIN(BR, "BR")
fc7a2a60 149 TAG_HANDLER_CONSTR(BR) { }
5526e819
VS
150
151 TAG_HANDLER_PROC(tag)
152 {
4f9297b0 153 int al = m_WParser->GetContainer()->GetAlignHor();
5526e819 154 wxHtmlContainerCell *c;
33ac7e6f 155
4f9297b0
VS
156 m_WParser->CloseContainer();
157 c = m_WParser->OpenContainer();
158 c->SetAlignHor(al);
159 c->SetAlign(tag);
160 c->SetMinHeight(m_WParser->GetCharHeight());
d1da8872 161 return false;
5526e819
VS
162 }
163
164TAG_HANDLER_END(BR)
165
166
167
168TAG_HANDLER_BEGIN(CENTER, "CENTER")
fc7a2a60 169 TAG_HANDLER_CONSTR(CENTER) { }
5526e819
VS
170
171 TAG_HANDLER_PROC(tag)
172 {
4f9297b0
VS
173 int old = m_WParser->GetAlign();
174 wxHtmlContainerCell *c = m_WParser->GetContainer();
175
176 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
e3774124 177 if (c->GetFirstChild() != NULL)
04dbb646 178 {
4f9297b0
VS
179 m_WParser->CloseContainer();
180 m_WParser->OpenContainer();
5526e819
VS
181 }
182 else
4f9297b0 183 c->SetAlignHor(wxHTML_ALIGN_CENTER);
5526e819 184
33ac7e6f 185 if (tag.HasEnding())
04dbb646 186 {
5526e819
VS
187 ParseInner(tag);
188
4f9297b0 189 m_WParser->SetAlign(old);
e3774124 190 if (c->GetFirstChild() != NULL)
04dbb646 191 {
4f9297b0
VS
192 m_WParser->CloseContainer();
193 m_WParser->OpenContainer();
5526e819
VS
194 }
195 else
4f9297b0 196 c->SetAlignHor(old);
5526e819 197
d1da8872 198 return true;
5526e819 199 }
d1da8872 200 else return false;
5526e819
VS
201 }
202
203TAG_HANDLER_END(CENTER)
204
205
206
207TAG_HANDLER_BEGIN(DIV, "DIV")
fc7a2a60 208 TAG_HANDLER_CONSTR(DIV) { }
5526e819
VS
209
210 TAG_HANDLER_PROC(tag)
211 {
92337e39 212 if(tag.HasParam(wxT("STYLE")))
04dbb646 213 {
d1da8872 214 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
f2034f1b
VS
215 {
216 m_WParser->CloseContainer();
217 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
218 m_WParser->CloseContainer();
219 m_WParser->OpenContainer();
d1da8872 220 return false;
f2034f1b
VS
221 }
222 else
223 {
224 // Treat other STYLE parameters here when they're supported.
d1da8872 225 return false;
f2034f1b 226 }
5526e819 227 }
92337e39 228 else if(tag.HasParam(wxT("ALIGN")))
04dbb646 229 {
f2034f1b
VS
230 int old = m_WParser->GetAlign();
231 wxHtmlContainerCell *c = m_WParser->GetContainer();
e3774124 232 if (c->GetFirstChild() != NULL)
f2034f1b
VS
233 {
234 m_WParser->CloseContainer();
235 m_WParser->OpenContainer();
236 c = m_WParser->GetContainer();
237 c->SetAlign(tag);
238 m_WParser->SetAlign(c->GetAlignHor());
239 }
240 else
241 {
242 c->SetAlign(tag);
243 m_WParser->SetAlign(c->GetAlignHor());
244 }
5526e819 245
f2034f1b 246 ParseInner(tag);
5526e819 247
f2034f1b 248 m_WParser->SetAlign(old);
e3774124 249 if (c->GetFirstChild() != NULL)
f2034f1b
VS
250 {
251 m_WParser->CloseContainer();
252 m_WParser->OpenContainer();
253 }
254 else
255 c->SetAlignHor(old);
256
c5448f38 257 return true;
5526e819
VS
258 }
259 else
f2034f1b 260 {
c5448f38
VS
261 // Same as BR
262 int al = m_WParser->GetContainer()->GetAlignHor();
263 wxHtmlContainerCell *c;
264
265 m_WParser->CloseContainer();
266 c = m_WParser->OpenContainer();
267 c->SetAlignHor(al);
268 c->SetAlign(tag);
269 c->SetMinHeight(m_WParser->GetCharHeight());
270 return false;
f2034f1b 271 }
5526e819
VS
272 }
273
274TAG_HANDLER_END(DIV)
275
276
277
278
279TAG_HANDLER_BEGIN(TITLE, "TITLE")
fc7a2a60 280 TAG_HANDLER_CONSTR(TITLE) { }
5526e819
VS
281
282 TAG_HANDLER_PROC(tag)
283 {
bc55e31b
VS
284 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
285 if (winIface)
04dbb646 286 {
b1a3a964 287 wxString title(tag.GetBeginIter(), tag.GetEndIter1());
8d94819c 288#if !wxUSE_UNICODE
4d70ab08
VZ
289 const wxFontEncoding enc = m_WParser->GetInputEncoding();
290 if ( enc != wxFONTENCODING_DEFAULT )
291 {
292 // need to convert to the current one
293 title = wxString(title.wc_str(wxCSConv(enc)), wxConvLocal);
294 }
295#endif // !wxUSE_UNICODE
296
bc55e31b
VS
297 title = m_WParser->GetEntitiesParser()->Parse(title);
298
299 winIface->SetHTMLWindowTitle(title);
5526e819 300 }
d1da8872 301 return true;
5526e819
VS
302 }
303
304TAG_HANDLER_END(TITLE)
305
306
307
308
309TAG_HANDLER_BEGIN(BODY, "BODY")
fc7a2a60 310 TAG_HANDLER_CONSTR(BODY) { }
5526e819
VS
311
312 TAG_HANDLER_PROC(tag)
313 {
5526e819
VS
314 wxColour clr;
315
8bd72d90 316 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
04dbb646 317 {
8bd72d90
VS
318 m_WParser->SetActualColor(clr);
319 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
5526e819
VS
320 }
321
8bd72d90
VS
322 if (tag.GetParamAsColour(wxT("LINK"), &clr))
323 m_WParser->SetLinkColor(clr);
324
bc55e31b
VS
325 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
326 // the rest of this function requires a window:
327 if ( !winIface )
328 return false;
329
97e490f8
VZ
330 if (tag.HasParam(wxT("BACKGROUND")))
331 {
332 wxFSFile *fileBgImage = m_WParser->OpenURL
333 (
334 wxHTML_URL_IMAGE,
335 tag.GetParam(wxT("BACKGROUND"))
336 );
337 if ( fileBgImage )
338 {
339 wxInputStream *is = fileBgImage->GetStream();
340 if ( is )
341 {
342 wxImage image(*is);
a1b806b9 343 if ( image.IsOk() )
bc55e31b 344 winIface->SetHTMLBackgroundImage(image);
97e490f8 345 }
a237d7ed
VZ
346
347 delete fileBgImage;
97e490f8
VZ
348 }
349 }
350
8bd72d90 351 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
04dbb646 352 {
8bd72d90
VS
353 m_WParser->GetContainer()->InsertCell(
354 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
bc55e31b 355 winIface->SetHTMLBackgroundColour(clr);
5526e819 356 }
97e490f8 357
d1da8872 358 return false;
5526e819
VS
359 }
360
361TAG_HANDLER_END(BODY)
362
363
364
365TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
fc7a2a60 366 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
5526e819
VS
367
368 TAG_HANDLER_PROC(tag)
369 {
370 wxHtmlContainerCell *c;
33ac7e6f 371
4f9297b0
VS
372 m_WParser->CloseContainer();
373 c = m_WParser->OpenContainer();
33ac7e6f 374
04dbb646 375 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
4f9297b0 376 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
5526e819 377 else
4f9297b0 378 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
33ac7e6f 379
04dbb646 380 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
4f9297b0 381 m_WParser->OpenContainer();
5526e819 382 ParseInner(tag);
4f9297b0
VS
383 c = m_WParser->CloseContainer();
384 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
385 m_WParser->CloseContainer();
386 m_WParser->OpenContainer();
d1da8872 387 return true;
5526e819
VS
388 }
389
390TAG_HANDLER_END(BLOCKQUOTE)
391
392
393
3c115835
VS
394TAG_HANDLER_BEGIN(SUBSUP, "SUB,SUP")
395
396 TAG_HANDLER_PROC(tag)
397 {
398 bool issub = (tag.GetName() == wxT("SUB"));
399 wxHtmlScriptMode oldmode = m_WParser->GetScriptMode();
400 int oldbase = m_WParser->GetScriptBaseline();
401 int oldsize = m_WParser->GetFontSize();
402
403 wxHtmlContainerCell *cont = m_WParser->GetContainer();
404 wxHtmlCell *c = cont->GetLastChild();
405
406 m_WParser->SetScriptMode(issub ? wxHTML_SCRIPT_SUB : wxHTML_SCRIPT_SUP);
198d7c6c
VS
407 m_WParser->SetScriptBaseline(
408 oldbase + c ? c->GetScriptBaseline() : 0);
3c115835
VS
409
410 // select smaller font
411 m_WParser->SetFontSize(m_WParser->GetFontSize()-2);
412 cont->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
413
414 ParseInner(tag);
415
416 // restore font size
417 m_WParser->SetFontSize(oldsize);
418 m_WParser->GetContainer()->InsertCell(
419 new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
420
421 // restore base and alignment
422 m_WParser->SetScriptBaseline(oldbase);
423 m_WParser->SetScriptMode(oldmode);
424
425 return true;
426 }
427
428TAG_HANDLER_END(SUBSUP)
429
430
8829fa82
VS
431// Tag handler for tags that we have to ignore, otherwise non-text data
432// would show up as text:
433TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
fc7a2a60
VZ
434 TAG_HANDLER_CONSTR(DoNothing) { }
435
436 TAG_HANDLER_PROC(WXUNUSED(tag))
8829fa82
VS
437 {
438 return true;
439 }
440TAG_HANDLER_END(DoNothing)
5526e819
VS
441
442
443
3c115835
VS
444
445
5526e819
VS
446TAGS_MODULE_BEGIN(Layout)
447
448 TAGS_MODULE_ADD(P)
449 TAGS_MODULE_ADD(BR)
450 TAGS_MODULE_ADD(CENTER)
451 TAGS_MODULE_ADD(DIV)
452 TAGS_MODULE_ADD(TITLE)
453 TAGS_MODULE_ADD(BODY)
454 TAGS_MODULE_ADD(BLOCKQUOTE)
3c115835 455 TAGS_MODULE_ADD(SUBSUP)
8829fa82 456 TAGS_MODULE_ADD(DoNothing)
5526e819
VS
457
458TAGS_MODULE_END(Layout)
459
460#endif