]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_layout.cpp
convert COM arguments to wx lazily to improve performance and allow calling Invoke...
[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:
f2034f1b
VS
82 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell)
83};
84
85// Comparison routine for bsearch into an int* array of pagebreaks.
7d7b3f69 86extern "C"
f2034f1b 87{
7d7b3f69
FM
88 static int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1)
89 {
90 return *(int*)i0 - *(int*)i1;
91 }
f2034f1b
VS
92}
93
fd0bab43 94bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak, wxArrayInt& known_pagebreaks) const
f2034f1b
VS
95{
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
d1da8872 99 // do anything except return false.
f2034f1b 100 //
d1da8872 101 // We also simply return false if the 'pagebreak' argument is
f2034f1b
VS
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.
b4a980f4 106 if( known_pagebreaks.GetCount() == 0 || *pagebreak <= m_PosY)
fd0bab43 107 {
d1da8872 108 return false;
fd0bab43 109 }
f2034f1b
VS
110
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.
c3485a4e
VS
114 int total_height = m_PosY;
115 for ( wxHtmlCell *parent = GetParent(); parent; parent = parent->GetParent() )
116 {
117 total_height += parent->GetPosY();
118 }
119
f2034f1b
VS
120
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.
fd0bab43 127 int where = known_pagebreaks.Index( total_height);
f2034f1b 128 // Add a pagebreak only if there isn't one already set here.
fd0bab43
VZ
129 if( wxNOT_FOUND != where)
130 {
d1da8872 131 return false;
fd0bab43 132 }
f2034f1b 133 else
fd0bab43 134 {
f2034f1b 135 *pagebreak = m_PosY;
d1da8872 136 return true;
fd0bab43 137 }
f2034f1b
VS
138}
139
fd0bab43
VZ
140
141
5526e819 142TAG_HANDLER_BEGIN(P, "P")
fc7a2a60 143 TAG_HANDLER_CONSTR(P) { }
5526e819
VS
144
145 TAG_HANDLER_PROC(tag)
146 {
e3774124 147 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
04dbb646 148 {
4f9297b0
VS
149 m_WParser->CloseContainer();
150 m_WParser->OpenContainer();
8c651ab7 151 }
4f9297b0
VS
152 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
153 m_WParser->GetContainer()->SetAlign(tag);
d1da8872 154 return false;
5526e819
VS
155 }
156
157TAG_HANDLER_END(P)
158
159
160
161TAG_HANDLER_BEGIN(BR, "BR")
fc7a2a60 162 TAG_HANDLER_CONSTR(BR) { }
5526e819
VS
163
164 TAG_HANDLER_PROC(tag)
165 {
4f9297b0 166 int al = m_WParser->GetContainer()->GetAlignHor();
5526e819 167 wxHtmlContainerCell *c;
33ac7e6f 168
4f9297b0
VS
169 m_WParser->CloseContainer();
170 c = m_WParser->OpenContainer();
171 c->SetAlignHor(al);
172 c->SetAlign(tag);
173 c->SetMinHeight(m_WParser->GetCharHeight());
d1da8872 174 return false;
5526e819
VS
175 }
176
177TAG_HANDLER_END(BR)
178
179
180
181TAG_HANDLER_BEGIN(CENTER, "CENTER")
fc7a2a60 182 TAG_HANDLER_CONSTR(CENTER) { }
5526e819
VS
183
184 TAG_HANDLER_PROC(tag)
185 {
4f9297b0
VS
186 int old = m_WParser->GetAlign();
187 wxHtmlContainerCell *c = m_WParser->GetContainer();
188
189 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
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(wxHTML_ALIGN_CENTER);
5526e819 197
33ac7e6f 198 if (tag.HasEnding())
04dbb646 199 {
5526e819
VS
200 ParseInner(tag);
201
4f9297b0 202 m_WParser->SetAlign(old);
e3774124 203 if (c->GetFirstChild() != NULL)
04dbb646 204 {
4f9297b0
VS
205 m_WParser->CloseContainer();
206 m_WParser->OpenContainer();
5526e819
VS
207 }
208 else
4f9297b0 209 c->SetAlignHor(old);
5526e819 210
d1da8872 211 return true;
5526e819 212 }
d1da8872 213 else return false;
5526e819
VS
214 }
215
216TAG_HANDLER_END(CENTER)
217
218
219
220TAG_HANDLER_BEGIN(DIV, "DIV")
fc7a2a60 221 TAG_HANDLER_CONSTR(DIV) { }
5526e819
VS
222
223 TAG_HANDLER_PROC(tag)
224 {
92337e39 225 if(tag.HasParam(wxT("STYLE")))
04dbb646 226 {
d1da8872 227 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
f2034f1b
VS
228 {
229 m_WParser->CloseContainer();
230 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
231 m_WParser->CloseContainer();
232 m_WParser->OpenContainer();
d1da8872 233 return false;
f2034f1b
VS
234 }
235 else
236 {
237 // Treat other STYLE parameters here when they're supported.
d1da8872 238 return false;
f2034f1b 239 }
5526e819 240 }
92337e39 241 else if(tag.HasParam(wxT("ALIGN")))
04dbb646 242 {
f2034f1b
VS
243 int old = m_WParser->GetAlign();
244 wxHtmlContainerCell *c = m_WParser->GetContainer();
e3774124 245 if (c->GetFirstChild() != NULL)
f2034f1b
VS
246 {
247 m_WParser->CloseContainer();
248 m_WParser->OpenContainer();
249 c = m_WParser->GetContainer();
250 c->SetAlign(tag);
251 m_WParser->SetAlign(c->GetAlignHor());
252 }
253 else
254 {
255 c->SetAlign(tag);
256 m_WParser->SetAlign(c->GetAlignHor());
257 }
5526e819 258
f2034f1b 259 ParseInner(tag);
5526e819 260
f2034f1b 261 m_WParser->SetAlign(old);
e3774124 262 if (c->GetFirstChild() != NULL)
f2034f1b
VS
263 {
264 m_WParser->CloseContainer();
265 m_WParser->OpenContainer();
266 }
267 else
268 c->SetAlignHor(old);
269
c5448f38 270 return true;
5526e819
VS
271 }
272 else
f2034f1b 273 {
c5448f38
VS
274 // Same as BR
275 int al = m_WParser->GetContainer()->GetAlignHor();
276 wxHtmlContainerCell *c;
277
278 m_WParser->CloseContainer();
279 c = m_WParser->OpenContainer();
280 c->SetAlignHor(al);
281 c->SetAlign(tag);
282 c->SetMinHeight(m_WParser->GetCharHeight());
283 return false;
f2034f1b 284 }
5526e819
VS
285 }
286
287TAG_HANDLER_END(DIV)
288
289
290
291
292TAG_HANDLER_BEGIN(TITLE, "TITLE")
fc7a2a60 293 TAG_HANDLER_CONSTR(TITLE) { }
5526e819
VS
294
295 TAG_HANDLER_PROC(tag)
296 {
bc55e31b
VS
297 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
298 if (winIface)
04dbb646 299 {
b1a3a964 300 wxString title(tag.GetBeginIter(), tag.GetEndIter1());
61c213fe 301#if !wxUSE_UNICODE && wxUSE_WCHAR_T
4d70ab08
VZ
302 const wxFontEncoding enc = m_WParser->GetInputEncoding();
303 if ( enc != wxFONTENCODING_DEFAULT )
304 {
305 // need to convert to the current one
306 title = wxString(title.wc_str(wxCSConv(enc)), wxConvLocal);
307 }
308#endif // !wxUSE_UNICODE
309
bc55e31b
VS
310 title = m_WParser->GetEntitiesParser()->Parse(title);
311
312 winIface->SetHTMLWindowTitle(title);
5526e819 313 }
d1da8872 314 return true;
5526e819
VS
315 }
316
317TAG_HANDLER_END(TITLE)
318
319
320
321
322TAG_HANDLER_BEGIN(BODY, "BODY")
fc7a2a60 323 TAG_HANDLER_CONSTR(BODY) { }
5526e819
VS
324
325 TAG_HANDLER_PROC(tag)
326 {
5526e819
VS
327 wxColour clr;
328
8bd72d90 329 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
04dbb646 330 {
8bd72d90
VS
331 m_WParser->SetActualColor(clr);
332 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
5526e819
VS
333 }
334
8bd72d90
VS
335 if (tag.GetParamAsColour(wxT("LINK"), &clr))
336 m_WParser->SetLinkColor(clr);
337
bc55e31b
VS
338 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
339 // the rest of this function requires a window:
340 if ( !winIface )
341 return false;
342
97e490f8
VZ
343 if (tag.HasParam(wxT("BACKGROUND")))
344 {
345 wxFSFile *fileBgImage = m_WParser->OpenURL
346 (
347 wxHTML_URL_IMAGE,
348 tag.GetParam(wxT("BACKGROUND"))
349 );
350 if ( fileBgImage )
351 {
352 wxInputStream *is = fileBgImage->GetStream();
353 if ( is )
354 {
355 wxImage image(*is);
356 if ( image.Ok() )
bc55e31b 357 winIface->SetHTMLBackgroundImage(image);
97e490f8 358 }
a237d7ed
VZ
359
360 delete fileBgImage;
97e490f8
VZ
361 }
362 }
363
8bd72d90 364 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
04dbb646 365 {
8bd72d90
VS
366 m_WParser->GetContainer()->InsertCell(
367 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
bc55e31b 368 winIface->SetHTMLBackgroundColour(clr);
5526e819 369 }
97e490f8 370
d1da8872 371 return false;
5526e819
VS
372 }
373
374TAG_HANDLER_END(BODY)
375
376
377
378TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
fc7a2a60 379 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
5526e819
VS
380
381 TAG_HANDLER_PROC(tag)
382 {
383 wxHtmlContainerCell *c;
33ac7e6f 384
4f9297b0
VS
385 m_WParser->CloseContainer();
386 c = m_WParser->OpenContainer();
33ac7e6f 387
04dbb646 388 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
4f9297b0 389 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
5526e819 390 else
4f9297b0 391 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
33ac7e6f 392
04dbb646 393 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
4f9297b0 394 m_WParser->OpenContainer();
5526e819 395 ParseInner(tag);
4f9297b0
VS
396 c = m_WParser->CloseContainer();
397 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
398 m_WParser->CloseContainer();
399 m_WParser->OpenContainer();
d1da8872 400 return true;
5526e819
VS
401 }
402
403TAG_HANDLER_END(BLOCKQUOTE)
404
405
406
3c115835
VS
407TAG_HANDLER_BEGIN(SUBSUP, "SUB,SUP")
408
409 TAG_HANDLER_PROC(tag)
410 {
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();
415
416 wxHtmlContainerCell *cont = m_WParser->GetContainer();
417 wxHtmlCell *c = cont->GetLastChild();
418
419 m_WParser->SetScriptMode(issub ? wxHTML_SCRIPT_SUB : wxHTML_SCRIPT_SUP);
198d7c6c
VS
420 m_WParser->SetScriptBaseline(
421 oldbase + c ? c->GetScriptBaseline() : 0);
3c115835
VS
422
423 // select smaller font
424 m_WParser->SetFontSize(m_WParser->GetFontSize()-2);
425 cont->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
426
427 ParseInner(tag);
428
429 // restore font size
430 m_WParser->SetFontSize(oldsize);
431 m_WParser->GetContainer()->InsertCell(
432 new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
433
434 // restore base and alignment
435 m_WParser->SetScriptBaseline(oldbase);
436 m_WParser->SetScriptMode(oldmode);
437
438 return true;
439 }
440
441TAG_HANDLER_END(SUBSUP)
442
443
8829fa82
VS
444// Tag handler for tags that we have to ignore, otherwise non-text data
445// would show up as text:
446TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
fc7a2a60
VZ
447 TAG_HANDLER_CONSTR(DoNothing) { }
448
449 TAG_HANDLER_PROC(WXUNUSED(tag))
8829fa82
VS
450 {
451 return true;
452 }
453TAG_HANDLER_END(DoNothing)
5526e819
VS
454
455
456
3c115835
VS
457
458
5526e819
VS
459TAGS_MODULE_BEGIN(Layout)
460
461 TAGS_MODULE_ADD(P)
462 TAGS_MODULE_ADD(BR)
463 TAGS_MODULE_ADD(CENTER)
464 TAGS_MODULE_ADD(DIV)
465 TAGS_MODULE_ADD(TITLE)
466 TAGS_MODULE_ADD(BODY)
467 TAGS_MODULE_ADD(BLOCKQUOTE)
3c115835 468 TAGS_MODULE_ADD(SUBSUP)
8829fa82 469 TAGS_MODULE_ADD(DoNothing)
5526e819
VS
470
471TAGS_MODULE_END(Layout)
472
473#endif