fixed wrong arguments to Draw
[wxWidgets.git] / src / html / m_layout.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: m_layout.cpp
3 // Purpose: wxHtml module for basic paragraphs/layout handling
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
10 #pragma implementation
11 #endif
12
13 #include "wx/wxprec.h"
14
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WXPRECOMP
23 #endif
24
25
26 #include "wx/html/forcelnk.h"
27 #include "wx/html/m_templ.h"
28
29 #include "wx/html/htmlwin.h"
30
31 FORCE_LINK_ME(m_layout)
32
33
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
69 class wxHtmlPageBreakCell : public wxHtmlCell
70 {
71 public:
72 wxHtmlPageBreakCell() {}
73
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 wxHtmlRenderingInfo& WXUNUSED(info)) {}
81
82 private:
83 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell)
84 };
85
86 // Comparison routine for bsearch into an int* array of pagebreaks.
87 extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1)
88 {
89 return *(int*)i0 - *(int*)i1;
90 }
91
92 bool 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),
122 wxInteger_compare);
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
135 TAG_HANDLER_BEGIN(P, "P")
136 TAG_HANDLER_CONSTR(P) { }
137
138 TAG_HANDLER_PROC(tag)
139 {
140 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
141 {
142 m_WParser->CloseContainer();
143 m_WParser->OpenContainer();
144 }
145 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
146 m_WParser->GetContainer()->SetAlign(tag);
147 return FALSE;
148 }
149
150 TAG_HANDLER_END(P)
151
152
153
154 TAG_HANDLER_BEGIN(BR, "BR")
155 TAG_HANDLER_CONSTR(BR) { }
156
157 TAG_HANDLER_PROC(tag)
158 {
159 int al = m_WParser->GetContainer()->GetAlignHor();
160 wxHtmlContainerCell *c;
161
162 m_WParser->CloseContainer();
163 c = m_WParser->OpenContainer();
164 c->SetAlignHor(al);
165 c->SetAlign(tag);
166 c->SetMinHeight(m_WParser->GetCharHeight());
167 return FALSE;
168 }
169
170 TAG_HANDLER_END(BR)
171
172
173
174 TAG_HANDLER_BEGIN(CENTER, "CENTER")
175 TAG_HANDLER_CONSTR(CENTER) { }
176
177 TAG_HANDLER_PROC(tag)
178 {
179 int old = m_WParser->GetAlign();
180 wxHtmlContainerCell *c = m_WParser->GetContainer();
181
182 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
183 if (c->GetFirstChild() != NULL)
184 {
185 m_WParser->CloseContainer();
186 m_WParser->OpenContainer();
187 }
188 else
189 c->SetAlignHor(wxHTML_ALIGN_CENTER);
190
191 if (tag.HasEnding())
192 {
193 ParseInner(tag);
194
195 m_WParser->SetAlign(old);
196 if (c->GetFirstChild() != NULL)
197 {
198 m_WParser->CloseContainer();
199 m_WParser->OpenContainer();
200 }
201 else
202 c->SetAlignHor(old);
203
204 return TRUE;
205 }
206 else return FALSE;
207 }
208
209 TAG_HANDLER_END(CENTER)
210
211
212
213 TAG_HANDLER_BEGIN(DIV, "DIV")
214 TAG_HANDLER_CONSTR(DIV) { }
215
216 TAG_HANDLER_PROC(tag)
217 {
218 if(tag.HasParam(wxT("STYLE")))
219 {
220 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), FALSE))
221 {
222 m_WParser->CloseContainer();
223 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
224 m_WParser->CloseContainer();
225 m_WParser->OpenContainer();
226 return FALSE;
227 }
228 else
229 {
230 // Treat other STYLE parameters here when they're supported.
231 return FALSE;
232 }
233 }
234 else if(tag.HasParam(wxT("ALIGN")))
235 {
236 int old = m_WParser->GetAlign();
237 wxHtmlContainerCell *c = m_WParser->GetContainer();
238 if (c->GetFirstChild() != NULL)
239 {
240 m_WParser->CloseContainer();
241 m_WParser->OpenContainer();
242 c = m_WParser->GetContainer();
243 c->SetAlign(tag);
244 m_WParser->SetAlign(c->GetAlignHor());
245 }
246 else
247 {
248 c->SetAlign(tag);
249 m_WParser->SetAlign(c->GetAlignHor());
250 }
251
252 ParseInner(tag);
253
254 m_WParser->SetAlign(old);
255 if (c->GetFirstChild() != NULL)
256 {
257 m_WParser->CloseContainer();
258 m_WParser->OpenContainer();
259 }
260 else
261 c->SetAlignHor(old);
262
263 return TRUE;
264 }
265 else
266 {
267 return FALSE;
268 }
269 }
270
271 TAG_HANDLER_END(DIV)
272
273
274
275
276 TAG_HANDLER_BEGIN(TITLE, "TITLE")
277 TAG_HANDLER_CONSTR(TITLE) { }
278
279 TAG_HANDLER_PROC(tag)
280 {
281 if (m_WParser->GetWindow())
282 {
283 wxHtmlWindow *wfr = (wxHtmlWindow*)(m_WParser->GetWindow());
284 if (wfr)
285 {
286 const wxString& src = *m_WParser->GetSource();
287 wfr->OnSetTitle(src.Mid(tag.GetBeginPos(),
288 tag.GetEndPos1()-tag.GetBeginPos()));
289 }
290 }
291 return TRUE;
292 }
293
294 TAG_HANDLER_END(TITLE)
295
296
297
298
299 TAG_HANDLER_BEGIN(BODY, "BODY")
300 TAG_HANDLER_CONSTR(BODY) { }
301
302 TAG_HANDLER_PROC(tag)
303 {
304 wxColour clr;
305
306 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
307 {
308 m_WParser->SetActualColor(clr);
309 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
310 }
311
312 if (tag.GetParamAsColour(wxT("LINK"), &clr))
313 m_WParser->SetLinkColor(clr);
314
315 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
316 {
317 m_WParser->GetContainer()->InsertCell(
318 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
319 if (m_WParser->GetWindow() != NULL)
320 m_WParser->GetWindow()->SetBackgroundColour(clr);
321 }
322 return FALSE;
323 }
324
325 TAG_HANDLER_END(BODY)
326
327
328
329 TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
330 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
331
332 TAG_HANDLER_PROC(tag)
333 {
334 wxHtmlContainerCell *c;
335
336 m_WParser->CloseContainer();
337 c = m_WParser->OpenContainer();
338
339 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
340 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
341 else
342 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
343
344 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
345 m_WParser->OpenContainer();
346 ParseInner(tag);
347 c = m_WParser->CloseContainer();
348 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
349 m_WParser->CloseContainer();
350 m_WParser->OpenContainer();
351 return TRUE;
352 }
353
354 TAG_HANDLER_END(BLOCKQUOTE)
355
356
357
358 // Tag handler for tags that we have to ignore, otherwise non-text data
359 // would show up as text:
360 TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
361 TAG_HANDLER_CONSTR(DoNothing) { }
362
363 TAG_HANDLER_PROC(WXUNUSED(tag))
364 {
365 return true;
366 }
367 TAG_HANDLER_END(DoNothing)
368
369
370
371 TAGS_MODULE_BEGIN(Layout)
372
373 TAGS_MODULE_ADD(P)
374 TAGS_MODULE_ADD(BR)
375 TAGS_MODULE_ADD(CENTER)
376 TAGS_MODULE_ADD(DIV)
377 TAGS_MODULE_ADD(TITLE)
378 TAGS_MODULE_ADD(BODY)
379 TAGS_MODULE_ADD(BLOCKQUOTE)
380 TAGS_MODULE_ADD(DoNothing)
381
382 TAGS_MODULE_END(Layout)
383
384 #endif