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