]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_layout.cpp
wxSYS_COLOUR_WINDOW is better for window backgrounds on PPC/Smartphone
[wxWidgets.git] / src / html / m_layout.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
c88293a4 2// Name: 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/////////////////////////////////////////////////////////////////////////////
14f355c2 9#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3364ab79
RS
10#pragma implementation
11#endif
12
314260fb 13#include "wx/wxprec.h"
5526e819 14
314260fb 15#include "wx/defs.h"
97e490f8 16
f6bcfd97 17#if wxUSE_HTML && wxUSE_STREAMS
97e490f8 18
2b5f62a0 19#ifdef __BORLANDC__
97e490f8 20 #pragma hdrstop
3364ab79
RS
21#endif
22
23#ifndef WXPRECOMP
3364ab79
RS
24#endif
25
97e490f8 26#include "wx/image.h"
5526e819 27
69941f05
VS
28#include "wx/html/forcelnk.h"
29#include "wx/html/m_templ.h"
5526e819 30
69941f05 31#include "wx/html/htmlwin.h"
5526e819 32
c88293a4 33FORCE_LINK_ME(m_layout)
5526e819 34
7127d129
RR
35#ifdef __WXWINCE__
36 #include "wx/msw/wince/missing.h" // for bsearch()
37#else
38 #include <stdlib.h> // bsearch()
39#endif
f2034f1b
VS
40
41//-----------------------------------------------------------------------------
42// wxHtmlPageBreakCell
43//-----------------------------------------------------------------------------
44
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">
51//
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.
56
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
63// modified.
64//
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.
73
4460b6c4 74class wxHtmlPageBreakCell : public wxHtmlCell
f2034f1b 75{
36c4ff4d 76public:
f2034f1b
VS
77 wxHtmlPageBreakCell() {}
78
36c4ff4d
VS
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),
e3ac6ee1 85 wxHtmlRenderingInfo& WXUNUSED(info)) {}
f2034f1b 86
36c4ff4d 87private:
f2034f1b
VS
88 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell)
89};
90
91// Comparison routine for bsearch into an int* array of pagebreaks.
f9ba645e 92extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1)
f2034f1b
VS
93{
94 return *(int*)i0 - *(int*)i1;
95}
96
97bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak, int* known_pagebreaks, int number_of_pages) const
98{
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
d1da8872 102 // do anything except return false.
f2034f1b 103 //
d1da8872 104 // We also simply return false if the 'pagebreak' argument is
f2034f1b
VS
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)
110 {
d1da8872 111 return false;
f2034f1b
VS
112 }
113
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();
118
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),
f9ba645e 127 wxInteger_compare);
f2034f1b
VS
128 // Add a pagebreak only if there isn't one already set here.
129 if(NULL != where)
130 {
d1da8872 131 return false;
f2034f1b
VS
132 }
133 else
134 {
135 *pagebreak = m_PosY;
d1da8872 136 return true;
f2034f1b
VS
137 }
138}
139
5526e819 140TAG_HANDLER_BEGIN(P, "P")
fc7a2a60 141 TAG_HANDLER_CONSTR(P) { }
5526e819
VS
142
143 TAG_HANDLER_PROC(tag)
144 {
e3774124 145 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
04dbb646 146 {
4f9297b0
VS
147 m_WParser->CloseContainer();
148 m_WParser->OpenContainer();
8c651ab7 149 }
4f9297b0
VS
150 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
151 m_WParser->GetContainer()->SetAlign(tag);
d1da8872 152 return false;
5526e819
VS
153 }
154
155TAG_HANDLER_END(P)
156
157
158
159TAG_HANDLER_BEGIN(BR, "BR")
fc7a2a60 160 TAG_HANDLER_CONSTR(BR) { }
5526e819
VS
161
162 TAG_HANDLER_PROC(tag)
163 {
4f9297b0 164 int al = m_WParser->GetContainer()->GetAlignHor();
5526e819 165 wxHtmlContainerCell *c;
33ac7e6f 166
4f9297b0
VS
167 m_WParser->CloseContainer();
168 c = m_WParser->OpenContainer();
169 c->SetAlignHor(al);
170 c->SetAlign(tag);
171 c->SetMinHeight(m_WParser->GetCharHeight());
d1da8872 172 return false;
5526e819
VS
173 }
174
175TAG_HANDLER_END(BR)
176
177
178
179TAG_HANDLER_BEGIN(CENTER, "CENTER")
fc7a2a60 180 TAG_HANDLER_CONSTR(CENTER) { }
5526e819
VS
181
182 TAG_HANDLER_PROC(tag)
183 {
4f9297b0
VS
184 int old = m_WParser->GetAlign();
185 wxHtmlContainerCell *c = m_WParser->GetContainer();
186
187 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
e3774124 188 if (c->GetFirstChild() != NULL)
04dbb646 189 {
4f9297b0
VS
190 m_WParser->CloseContainer();
191 m_WParser->OpenContainer();
5526e819
VS
192 }
193 else
4f9297b0 194 c->SetAlignHor(wxHTML_ALIGN_CENTER);
5526e819 195
33ac7e6f 196 if (tag.HasEnding())
04dbb646 197 {
5526e819
VS
198 ParseInner(tag);
199
4f9297b0 200 m_WParser->SetAlign(old);
e3774124 201 if (c->GetFirstChild() != NULL)
04dbb646 202 {
4f9297b0
VS
203 m_WParser->CloseContainer();
204 m_WParser->OpenContainer();
5526e819
VS
205 }
206 else
4f9297b0 207 c->SetAlignHor(old);
5526e819 208
d1da8872 209 return true;
5526e819 210 }
d1da8872 211 else return false;
5526e819
VS
212 }
213
214TAG_HANDLER_END(CENTER)
215
216
217
218TAG_HANDLER_BEGIN(DIV, "DIV")
fc7a2a60 219 TAG_HANDLER_CONSTR(DIV) { }
5526e819
VS
220
221 TAG_HANDLER_PROC(tag)
222 {
92337e39 223 if(tag.HasParam(wxT("STYLE")))
04dbb646 224 {
d1da8872 225 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
f2034f1b
VS
226 {
227 m_WParser->CloseContainer();
228 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
229 m_WParser->CloseContainer();
230 m_WParser->OpenContainer();
d1da8872 231 return false;
f2034f1b
VS
232 }
233 else
234 {
235 // Treat other STYLE parameters here when they're supported.
d1da8872 236 return false;
f2034f1b 237 }
5526e819 238 }
92337e39 239 else if(tag.HasParam(wxT("ALIGN")))
04dbb646 240 {
f2034f1b
VS
241 int old = m_WParser->GetAlign();
242 wxHtmlContainerCell *c = m_WParser->GetContainer();
e3774124 243 if (c->GetFirstChild() != NULL)
f2034f1b
VS
244 {
245 m_WParser->CloseContainer();
246 m_WParser->OpenContainer();
247 c = m_WParser->GetContainer();
248 c->SetAlign(tag);
249 m_WParser->SetAlign(c->GetAlignHor());
250 }
251 else
252 {
253 c->SetAlign(tag);
254 m_WParser->SetAlign(c->GetAlignHor());
255 }
5526e819 256
f2034f1b 257 ParseInner(tag);
5526e819 258
f2034f1b 259 m_WParser->SetAlign(old);
e3774124 260 if (c->GetFirstChild() != NULL)
f2034f1b
VS
261 {
262 m_WParser->CloseContainer();
263 m_WParser->OpenContainer();
264 }
265 else
266 c->SetAlignHor(old);
267
c5448f38 268 return true;
5526e819
VS
269 }
270 else
f2034f1b 271 {
c5448f38
VS
272 // Same as BR
273 int al = m_WParser->GetContainer()->GetAlignHor();
274 wxHtmlContainerCell *c;
275
276 m_WParser->CloseContainer();
277 c = m_WParser->OpenContainer();
278 c->SetAlignHor(al);
279 c->SetAlign(tag);
280 c->SetMinHeight(m_WParser->GetCharHeight());
281 return false;
f2034f1b 282 }
5526e819
VS
283 }
284
285TAG_HANDLER_END(DIV)
286
287
288
289
290TAG_HANDLER_BEGIN(TITLE, "TITLE")
fc7a2a60 291 TAG_HANDLER_CONSTR(TITLE) { }
5526e819
VS
292
293 TAG_HANDLER_PROC(tag)
294 {
33ac7e6f 295 if (m_WParser->GetWindow())
04dbb646 296 {
4f9297b0 297 wxHtmlWindow *wfr = (wxHtmlWindow*)(m_WParser->GetWindow());
33ac7e6f 298 if (wfr)
04dbb646 299 {
4d5881b1 300 wxString title = m_WParser->GetSource()->Mid(
d1da8872 301 tag.GetBeginPos(),
4d5881b1 302 tag.GetEndPos1()-tag.GetBeginPos());
140ba067
VS
303#if !wxUSE_UNICODE
304 wxCSConv conv(m_WParser->GetInputEncoding());
140ba067 305 title = wxString(title.wc_str(conv), wxConvLocal);
140ba067 306#endif
4d5881b1
VS
307 title = m_WParser->GetEntitiesParser()->Parse(title);
308 wfr->OnSetTitle(title);
5526e819
VS
309 }
310 }
d1da8872 311 return true;
5526e819
VS
312 }
313
314TAG_HANDLER_END(TITLE)
315
316
317
318
319TAG_HANDLER_BEGIN(BODY, "BODY")
fc7a2a60 320 TAG_HANDLER_CONSTR(BODY) { }
5526e819
VS
321
322 TAG_HANDLER_PROC(tag)
323 {
5526e819
VS
324 wxColour clr;
325
8bd72d90 326 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
04dbb646 327 {
8bd72d90
VS
328 m_WParser->SetActualColor(clr);
329 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
5526e819
VS
330 }
331
8bd72d90
VS
332 if (tag.GetParamAsColour(wxT("LINK"), &clr))
333 m_WParser->SetLinkColor(clr);
334
97e490f8
VZ
335 if (tag.HasParam(wxT("BACKGROUND")))
336 {
337 wxFSFile *fileBgImage = m_WParser->OpenURL
338 (
339 wxHTML_URL_IMAGE,
340 tag.GetParam(wxT("BACKGROUND"))
341 );
342 if ( fileBgImage )
343 {
344 wxInputStream *is = fileBgImage->GetStream();
345 if ( is )
346 {
347 wxImage image(*is);
348 if ( image.Ok() )
349 m_WParser->GetWindow()->SetBackgroundImage(image);
350 }
351 }
352 }
353
8bd72d90 354 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
04dbb646 355 {
8bd72d90
VS
356 m_WParser->GetContainer()->InsertCell(
357 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
358 if (m_WParser->GetWindow() != NULL)
359 m_WParser->GetWindow()->SetBackgroundColour(clr);
5526e819 360 }
97e490f8 361
d1da8872 362 return false;
5526e819
VS
363 }
364
365TAG_HANDLER_END(BODY)
366
367
368
369TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
fc7a2a60 370 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
5526e819
VS
371
372 TAG_HANDLER_PROC(tag)
373 {
374 wxHtmlContainerCell *c;
33ac7e6f 375
4f9297b0
VS
376 m_WParser->CloseContainer();
377 c = m_WParser->OpenContainer();
33ac7e6f 378
04dbb646 379 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
4f9297b0 380 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
5526e819 381 else
4f9297b0 382 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
33ac7e6f 383
04dbb646 384 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
4f9297b0 385 m_WParser->OpenContainer();
5526e819 386 ParseInner(tag);
4f9297b0
VS
387 c = m_WParser->CloseContainer();
388 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
389 m_WParser->CloseContainer();
390 m_WParser->OpenContainer();
d1da8872 391 return true;
5526e819
VS
392 }
393
394TAG_HANDLER_END(BLOCKQUOTE)
395
396
397
8829fa82
VS
398// Tag handler for tags that we have to ignore, otherwise non-text data
399// would show up as text:
400TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
fc7a2a60
VZ
401 TAG_HANDLER_CONSTR(DoNothing) { }
402
403 TAG_HANDLER_PROC(WXUNUSED(tag))
8829fa82
VS
404 {
405 return true;
406 }
407TAG_HANDLER_END(DoNothing)
5526e819
VS
408
409
410
411TAGS_MODULE_BEGIN(Layout)
412
413 TAGS_MODULE_ADD(P)
414 TAGS_MODULE_ADD(BR)
415 TAGS_MODULE_ADD(CENTER)
416 TAGS_MODULE_ADD(DIV)
417 TAGS_MODULE_ADD(TITLE)
418 TAGS_MODULE_ADD(BODY)
419 TAGS_MODULE_ADD(BLOCKQUOTE)
8829fa82 420 TAGS_MODULE_ADD(DoNothing)
5526e819
VS
421
422TAGS_MODULE_END(Layout)
423
424#endif