]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_layout.cpp
Improvements to OnIdle processing
[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
VS
6// Copyright: (c) 1999 Vaclav Slavik
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
3364ab79
RS
9#ifdef __GNUG__
10#pragma implementation
11#endif
12
314260fb 13#include "wx/wxprec.h"
5526e819 14
314260fb
VS
15
16#include "wx/defs.h"
f6bcfd97 17#if wxUSE_HTML && wxUSE_STREAMS
2b5f62a0 18#ifdef __BORLANDC__
3364ab79
RS
19#pragma hdrstop
20#endif
21
22#ifndef WXPRECOMP
3364ab79
RS
23#endif
24
5526e819 25
69941f05
VS
26#include "wx/html/forcelnk.h"
27#include "wx/html/m_templ.h"
5526e819 28
69941f05 29#include "wx/html/htmlwin.h"
5526e819 30
c88293a4 31FORCE_LINK_ME(m_layout)
5526e819
VS
32
33
f2034f1b
VS
34#include <stdlib.h> // bsearch()
35
36//-----------------------------------------------------------------------------
37// wxHtmlPageBreakCell
38//-----------------------------------------------------------------------------
39
40// Since html isn't a page-layout language, it doesn't support page
41// page breaks directly--that requires CSS2 support. But a page-break
42// facility is handy, and has been requested more than once on the
43// mailing lists. This wxHtml tag handler implements just enough of
44// CSS2 to support a page break by recognizing only
45// <div style="page-break-before:always">
46//
47// wxHtml maintains page breaks in wxHtmlPrintout::m_PageBreaks. The
48// tag handler below adds appropriate offsets to that array member.
49// wxHtmlDCRenderer::Render() accesses that array and makes a new page
50// begin after each page-break tag.
51
52// The page-break handler does all its work in AdjustPagebreak(). For
53// all tag handlers, that function adjusts the page-break position.
54// For other tags, it determines whether the html element can fit on
55// the remainder of the page; if it cannot fit, but must not be split,
56// then the function moves the page break provided in the argument up,
57// and returns 'true' to inform the caller that the argument was
58// modified.
59//
60// Due to its special purpose, the page-break facility differs from
61// other tags. It takes up no space, but it behaves as though there is
62// never enough room to fit it on the remainder of the page--it always
63// forces a page break. Therefore, unlike other elements that trigger
64// a page break, it would never 'fit' on the following page either.
65// Therefore it's necessary to compare each pagebreak candidate to the
66// array wxHtmlPrintout::m_PageBreaks of pagebreaks already set, and
67// set a new one only if it's not in that array.
68
4460b6c4 69class wxHtmlPageBreakCell : public wxHtmlCell
f2034f1b 70{
36c4ff4d 71public:
f2034f1b
VS
72 wxHtmlPageBreakCell() {}
73
36c4ff4d
VS
74 bool AdjustPagebreak(int* pagebreak,
75 int* known_pagebreaks = NULL,
76 int number_of_pages = 0) const;
77 void Draw(wxDC& WXUNUSED(dc),
78 int WXUNUSED(x), int WXUNUSED(y),
79 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
80 wxHtmlRenderingState& WXUNUSED(state)) {}
f2034f1b 81
36c4ff4d 82private:
f2034f1b
VS
83 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell)
84};
85
86// Comparison routine for bsearch into an int* array of pagebreaks.
f9ba645e 87extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1)
f2034f1b
VS
88{
89 return *(int*)i0 - *(int*)i1;
90}
91
92bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak, int* known_pagebreaks, int number_of_pages) const
93{
94 // When we are counting pages, 'known_pagebreaks' is non-NULL.
95 // That's the only time we change 'pagebreak'. Otherwise, pages
96 // were already counted, 'known_pagebreaks' is NULL, and we don't
97 // do anything except return FALSE.
98 //
99 // We also simply return FALSE if the 'pagebreak' argument is
100 // less than (vertically above) or the same as the current
101 // vertical position. Otherwise we'd be setting a pagebreak above
102 // the current cell, which is incorrect, or duplicating a
103 // pagebreak that has already been set.
104 if(NULL == known_pagebreaks || *pagebreak <= m_PosY)
105 {
106 return FALSE;
107 }
108
109 // m_PosY is only the vertical offset from the parent. The pagebreak
110 // required here is the total page offset, so m_PosY must be added
111 // to the parent's offset and height.
112 int total_height = m_PosY + GetParent()->GetPosY() + GetParent()->GetHeight();
113
114 // Search the array of pagebreaks to see whether we've already set
115 // a pagebreak here. The standard bsearch() function is appropriate
116 // because the array of pagebreaks through known_pagebreaks[number_of_pages]
117 // is known to be sorted in strictly increasing order. '1 + number_of_pages'
118 // is used as a bsearch() argument because the array contains a leading
119 // zero plus one element for each page.
120 int* where = (int*) bsearch(&total_height, known_pagebreaks,
121 1 + number_of_pages, sizeof(int),
f9ba645e 122 wxInteger_compare);
f2034f1b
VS
123 // Add a pagebreak only if there isn't one already set here.
124 if(NULL != where)
125 {
126 return FALSE;
127 }
128 else
129 {
130 *pagebreak = m_PosY;
131 return TRUE;
132 }
133}
134
5526e819
VS
135TAG_HANDLER_BEGIN(P, "P")
136
137 TAG_HANDLER_PROC(tag)
138 {
e3774124 139 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
04dbb646 140 {
4f9297b0
VS
141 m_WParser->CloseContainer();
142 m_WParser->OpenContainer();
8c651ab7 143 }
4f9297b0
VS
144 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
145 m_WParser->GetContainer()->SetAlign(tag);
5526e819
VS
146 return FALSE;
147 }
148
149TAG_HANDLER_END(P)
150
151
152
153TAG_HANDLER_BEGIN(BR, "BR")
154
155 TAG_HANDLER_PROC(tag)
156 {
4f9297b0 157 int al = m_WParser->GetContainer()->GetAlignHor();
5526e819 158 wxHtmlContainerCell *c;
33ac7e6f 159
4f9297b0
VS
160 m_WParser->CloseContainer();
161 c = m_WParser->OpenContainer();
162 c->SetAlignHor(al);
163 c->SetAlign(tag);
164 c->SetMinHeight(m_WParser->GetCharHeight());
5526e819
VS
165 return FALSE;
166 }
167
168TAG_HANDLER_END(BR)
169
170
171
172TAG_HANDLER_BEGIN(CENTER, "CENTER")
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
VS
200
201 return TRUE;
202 }
203 else return FALSE;
204 }
205
206TAG_HANDLER_END(CENTER)
207
208
209
210TAG_HANDLER_BEGIN(DIV, "DIV")
211
212 TAG_HANDLER_PROC(tag)
213 {
92337e39 214 if(tag.HasParam(wxT("STYLE")))
04dbb646 215 {
92337e39 216 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), FALSE))
f2034f1b
VS
217 {
218 m_WParser->CloseContainer();
219 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
220 m_WParser->CloseContainer();
221 m_WParser->OpenContainer();
222 return FALSE;
223 }
224 else
225 {
226 // Treat other STYLE parameters here when they're supported.
227 return FALSE;
228 }
5526e819 229 }
92337e39 230 else if(tag.HasParam(wxT("ALIGN")))
04dbb646 231 {
f2034f1b
VS
232 int old = m_WParser->GetAlign();
233 wxHtmlContainerCell *c = m_WParser->GetContainer();
e3774124 234 if (c->GetFirstChild() != NULL)
f2034f1b
VS
235 {
236 m_WParser->CloseContainer();
237 m_WParser->OpenContainer();
238 c = m_WParser->GetContainer();
239 c->SetAlign(tag);
240 m_WParser->SetAlign(c->GetAlignHor());
241 }
242 else
243 {
244 c->SetAlign(tag);
245 m_WParser->SetAlign(c->GetAlignHor());
246 }
5526e819 247
f2034f1b 248 ParseInner(tag);
5526e819 249
f2034f1b 250 m_WParser->SetAlign(old);
e3774124 251 if (c->GetFirstChild() != NULL)
f2034f1b
VS
252 {
253 m_WParser->CloseContainer();
254 m_WParser->OpenContainer();
255 }
256 else
257 c->SetAlignHor(old);
258
259 return TRUE;
5526e819
VS
260 }
261 else
f2034f1b
VS
262 {
263 return FALSE;
264 }
5526e819
VS
265 }
266
267TAG_HANDLER_END(DIV)
268
269
270
271
272TAG_HANDLER_BEGIN(TITLE, "TITLE")
273
274 TAG_HANDLER_PROC(tag)
275 {
33ac7e6f 276 if (m_WParser->GetWindow())
04dbb646 277 {
4f9297b0 278 wxHtmlWindow *wfr = (wxHtmlWindow*)(m_WParser->GetWindow());
33ac7e6f 279 if (wfr)
04dbb646 280 {
9eeca6df
VS
281 const wxString& src = *m_WParser->GetSource();
282 wfr->OnSetTitle(src.Mid(tag.GetBeginPos(),
283 tag.GetEndPos1()-tag.GetBeginPos()));
5526e819
VS
284 }
285 }
286 return TRUE;
287 }
288
289TAG_HANDLER_END(TITLE)
290
291
292
293
294TAG_HANDLER_BEGIN(BODY, "BODY")
295
296 TAG_HANDLER_PROC(tag)
297 {
5526e819
VS
298 wxColour clr;
299
8bd72d90 300 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
04dbb646 301 {
8bd72d90
VS
302 m_WParser->SetActualColor(clr);
303 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
5526e819
VS
304 }
305
8bd72d90
VS
306 if (tag.GetParamAsColour(wxT("LINK"), &clr))
307 m_WParser->SetLinkColor(clr);
308
309 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
04dbb646 310 {
8bd72d90
VS
311 m_WParser->GetContainer()->InsertCell(
312 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
313 if (m_WParser->GetWindow() != NULL)
314 m_WParser->GetWindow()->SetBackgroundColour(clr);
5526e819
VS
315 }
316 return FALSE;
317 }
318
319TAG_HANDLER_END(BODY)
320
321
322
323TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
324
325 TAG_HANDLER_PROC(tag)
326 {
327 wxHtmlContainerCell *c;
33ac7e6f 328
4f9297b0
VS
329 m_WParser->CloseContainer();
330 c = m_WParser->OpenContainer();
33ac7e6f 331
04dbb646 332 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
4f9297b0 333 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
5526e819 334 else
4f9297b0 335 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
33ac7e6f 336
04dbb646 337 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
4f9297b0 338 m_WParser->OpenContainer();
5526e819 339 ParseInner(tag);
4f9297b0
VS
340 c = m_WParser->CloseContainer();
341 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
342 m_WParser->CloseContainer();
343 m_WParser->OpenContainer();
5526e819
VS
344 return TRUE;
345 }
346
347TAG_HANDLER_END(BLOCKQUOTE)
348
349
350
8829fa82
VS
351// Tag handler for tags that we have to ignore, otherwise non-text data
352// would show up as text:
353TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
354 TAG_HANDLER_PROC(tag)
355 {
356 return true;
357 }
358TAG_HANDLER_END(DoNothing)
5526e819
VS
359
360
361
362TAGS_MODULE_BEGIN(Layout)
363
364 TAGS_MODULE_ADD(P)
365 TAGS_MODULE_ADD(BR)
366 TAGS_MODULE_ADD(CENTER)
367 TAGS_MODULE_ADD(DIV)
368 TAGS_MODULE_ADD(TITLE)
369 TAGS_MODULE_ADD(BODY)
370 TAGS_MODULE_ADD(BLOCKQUOTE)
8829fa82 371 TAGS_MODULE_ADD(DoNothing)
5526e819
VS
372
373TAGS_MODULE_END(Layout)
374
375#endif