]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_layout.cpp
fix for r56618: toolbar event handlers can toggle or disable tools again (patch ...
[wxWidgets.git] / src / html / m_layout.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/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
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_HTML && wxUSE_STREAMS
17
18 #ifndef WX_PRECOMP
19 #include "wx/image.h"
20 #endif
21
22 #include "wx/html/forcelnk.h"
23 #include "wx/html/m_templ.h"
24
25 #include "wx/html/htmlwin.h"
26
27 FORCE_LINK_ME(m_layout)
28
29 #ifdef __WXWINCE__
30 #include "wx/msw/wince/missing.h" // for bsearch()
31 #else
32 #include <stdlib.h> // bsearch()
33 #endif
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
68 class wxHtmlPageBreakCell : public wxHtmlCell
69 {
70 public:
71 wxHtmlPageBreakCell() {}
72
73 bool AdjustPagebreak(int* pagebreak,
74 wxArrayInt& known_pagebreaks) const;
75
76 void Draw(wxDC& WXUNUSED(dc),
77 int WXUNUSED(x), int WXUNUSED(y),
78 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
79 wxHtmlRenderingInfo& WXUNUSED(info)) {}
80
81 private:
82 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell)
83 };
84
85 // Comparison routine for bsearch into an int* array of pagebreaks.
86 extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1)
87 {
88 return *(int*)i0 - *(int*)i1;
89 }
90
91 bool wxHtmlPageBreakCell::AdjustPagebreak(int* pagebreak, wxArrayInt& known_pagebreaks) const
92 {
93 // When we are counting pages, 'known_pagebreaks' is non-NULL.
94 // That's the only time we change 'pagebreak'. Otherwise, pages
95 // were already counted, 'known_pagebreaks' is NULL, and we don't
96 // do anything except return false.
97 //
98 // We also simply return false if the 'pagebreak' argument is
99 // less than (vertically above) or the same as the current
100 // vertical position. Otherwise we'd be setting a pagebreak above
101 // the current cell, which is incorrect, or duplicating a
102 // pagebreak that has already been set.
103 if( known_pagebreaks.GetCount() == 0 || *pagebreak <= m_PosY)
104 {
105 return false;
106 }
107
108 // m_PosY is only the vertical offset from the parent. The pagebreak
109 // required here is the total page offset, so m_PosY must be added
110 // to the parent's offset and height.
111 int total_height = m_PosY;
112 for ( wxHtmlCell *parent = GetParent(); parent; parent = parent->GetParent() )
113 {
114 total_height += parent->GetPosY();
115 }
116
117
118 // Search the array of pagebreaks to see whether we've already set
119 // a pagebreak here. The standard bsearch() function is appropriate
120 // because the array of pagebreaks through known_pagebreaks[number_of_pages]
121 // is known to be sorted in strictly increasing order. '1 + number_of_pages'
122 // is used as a bsearch() argument because the array contains a leading
123 // zero plus one element for each page.
124 int where = known_pagebreaks.Index( total_height);
125 // Add a pagebreak only if there isn't one already set here.
126 if( wxNOT_FOUND != where)
127 {
128 return false;
129 }
130 else
131 {
132 *pagebreak = m_PosY;
133 return true;
134 }
135 }
136
137
138
139 TAG_HANDLER_BEGIN(P, "P")
140 TAG_HANDLER_CONSTR(P) { }
141
142 TAG_HANDLER_PROC(tag)
143 {
144 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
145 {
146 m_WParser->CloseContainer();
147 m_WParser->OpenContainer();
148 }
149 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
150 m_WParser->GetContainer()->SetAlign(tag);
151 return false;
152 }
153
154 TAG_HANDLER_END(P)
155
156
157
158 TAG_HANDLER_BEGIN(BR, "BR")
159 TAG_HANDLER_CONSTR(BR) { }
160
161 TAG_HANDLER_PROC(tag)
162 {
163 int al = m_WParser->GetContainer()->GetAlignHor();
164 wxHtmlContainerCell *c;
165
166 m_WParser->CloseContainer();
167 c = m_WParser->OpenContainer();
168 c->SetAlignHor(al);
169 c->SetAlign(tag);
170 c->SetMinHeight(m_WParser->GetCharHeight());
171 return false;
172 }
173
174 TAG_HANDLER_END(BR)
175
176
177
178 TAG_HANDLER_BEGIN(CENTER, "CENTER")
179 TAG_HANDLER_CONSTR(CENTER) { }
180
181 TAG_HANDLER_PROC(tag)
182 {
183 int old = m_WParser->GetAlign();
184 wxHtmlContainerCell *c = m_WParser->GetContainer();
185
186 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
187 if (c->GetFirstChild() != NULL)
188 {
189 m_WParser->CloseContainer();
190 m_WParser->OpenContainer();
191 }
192 else
193 c->SetAlignHor(wxHTML_ALIGN_CENTER);
194
195 if (tag.HasEnding())
196 {
197 ParseInner(tag);
198
199 m_WParser->SetAlign(old);
200 if (c->GetFirstChild() != NULL)
201 {
202 m_WParser->CloseContainer();
203 m_WParser->OpenContainer();
204 }
205 else
206 c->SetAlignHor(old);
207
208 return true;
209 }
210 else return false;
211 }
212
213 TAG_HANDLER_END(CENTER)
214
215
216
217 TAG_HANDLER_BEGIN(DIV, "DIV")
218 TAG_HANDLER_CONSTR(DIV) { }
219
220 TAG_HANDLER_PROC(tag)
221 {
222 if(tag.HasParam(wxT("STYLE")))
223 {
224 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
225 {
226 m_WParser->CloseContainer();
227 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
228 m_WParser->CloseContainer();
229 m_WParser->OpenContainer();
230 return false;
231 }
232 else
233 {
234 // Treat other STYLE parameters here when they're supported.
235 return false;
236 }
237 }
238 else if(tag.HasParam(wxT("ALIGN")))
239 {
240 int old = m_WParser->GetAlign();
241 wxHtmlContainerCell *c = m_WParser->GetContainer();
242 if (c->GetFirstChild() != NULL)
243 {
244 m_WParser->CloseContainer();
245 m_WParser->OpenContainer();
246 c = m_WParser->GetContainer();
247 c->SetAlign(tag);
248 m_WParser->SetAlign(c->GetAlignHor());
249 }
250 else
251 {
252 c->SetAlign(tag);
253 m_WParser->SetAlign(c->GetAlignHor());
254 }
255
256 ParseInner(tag);
257
258 m_WParser->SetAlign(old);
259 if (c->GetFirstChild() != NULL)
260 {
261 m_WParser->CloseContainer();
262 m_WParser->OpenContainer();
263 }
264 else
265 c->SetAlignHor(old);
266
267 return true;
268 }
269 else
270 {
271 // Same as BR
272 int al = m_WParser->GetContainer()->GetAlignHor();
273 wxHtmlContainerCell *c;
274
275 m_WParser->CloseContainer();
276 c = m_WParser->OpenContainer();
277 c->SetAlignHor(al);
278 c->SetAlign(tag);
279 c->SetMinHeight(m_WParser->GetCharHeight());
280 return false;
281 }
282 }
283
284 TAG_HANDLER_END(DIV)
285
286
287
288
289 TAG_HANDLER_BEGIN(TITLE, "TITLE")
290 TAG_HANDLER_CONSTR(TITLE) { }
291
292 TAG_HANDLER_PROC(tag)
293 {
294 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
295 if (winIface)
296 {
297 wxString title(tag.GetBeginIter(), tag.GetEndIter1());
298 #if !wxUSE_UNICODE && wxUSE_WCHAR_T
299 const wxFontEncoding enc = m_WParser->GetInputEncoding();
300 if ( enc != wxFONTENCODING_DEFAULT )
301 {
302 // need to convert to the current one
303 title = wxString(title.wc_str(wxCSConv(enc)), wxConvLocal);
304 }
305 #endif // !wxUSE_UNICODE
306
307 title = m_WParser->GetEntitiesParser()->Parse(title);
308
309 winIface->SetHTMLWindowTitle(title);
310 }
311 return true;
312 }
313
314 TAG_HANDLER_END(TITLE)
315
316
317
318
319 TAG_HANDLER_BEGIN(BODY, "BODY")
320 TAG_HANDLER_CONSTR(BODY) { }
321
322 TAG_HANDLER_PROC(tag)
323 {
324 wxColour clr;
325
326 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
327 {
328 m_WParser->SetActualColor(clr);
329 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
330 }
331
332 if (tag.GetParamAsColour(wxT("LINK"), &clr))
333 m_WParser->SetLinkColor(clr);
334
335 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
336 // the rest of this function requires a window:
337 if ( !winIface )
338 return false;
339
340 if (tag.HasParam(wxT("BACKGROUND")))
341 {
342 wxFSFile *fileBgImage = m_WParser->OpenURL
343 (
344 wxHTML_URL_IMAGE,
345 tag.GetParam(wxT("BACKGROUND"))
346 );
347 if ( fileBgImage )
348 {
349 wxInputStream *is = fileBgImage->GetStream();
350 if ( is )
351 {
352 wxImage image(*is);
353 if ( image.Ok() )
354 winIface->SetHTMLBackgroundImage(image);
355 }
356
357 delete fileBgImage;
358 }
359 }
360
361 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
362 {
363 m_WParser->GetContainer()->InsertCell(
364 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
365 winIface->SetHTMLBackgroundColour(clr);
366 }
367
368 return false;
369 }
370
371 TAG_HANDLER_END(BODY)
372
373
374
375 TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
376 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
377
378 TAG_HANDLER_PROC(tag)
379 {
380 wxHtmlContainerCell *c;
381
382 m_WParser->CloseContainer();
383 c = m_WParser->OpenContainer();
384
385 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
386 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
387 else
388 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
389
390 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
391 m_WParser->OpenContainer();
392 ParseInner(tag);
393 c = m_WParser->CloseContainer();
394 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
395 m_WParser->CloseContainer();
396 m_WParser->OpenContainer();
397 return true;
398 }
399
400 TAG_HANDLER_END(BLOCKQUOTE)
401
402
403
404 TAG_HANDLER_BEGIN(SUBSUP, "SUB,SUP")
405
406 TAG_HANDLER_PROC(tag)
407 {
408 bool issub = (tag.GetName() == wxT("SUB"));
409 wxHtmlScriptMode oldmode = m_WParser->GetScriptMode();
410 int oldbase = m_WParser->GetScriptBaseline();
411 int oldsize = m_WParser->GetFontSize();
412
413 wxHtmlContainerCell *cont = m_WParser->GetContainer();
414 wxHtmlCell *c = cont->GetLastChild();
415
416 m_WParser->SetScriptMode(issub ? wxHTML_SCRIPT_SUB : wxHTML_SCRIPT_SUP);
417 m_WParser->SetScriptBaseline(
418 oldbase + c ? c->GetScriptBaseline() : 0);
419
420 // select smaller font
421 m_WParser->SetFontSize(m_WParser->GetFontSize()-2);
422 cont->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
423
424 ParseInner(tag);
425
426 // restore font size
427 m_WParser->SetFontSize(oldsize);
428 m_WParser->GetContainer()->InsertCell(
429 new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
430
431 // restore base and alignment
432 m_WParser->SetScriptBaseline(oldbase);
433 m_WParser->SetScriptMode(oldmode);
434
435 return true;
436 }
437
438 TAG_HANDLER_END(SUBSUP)
439
440
441 // Tag handler for tags that we have to ignore, otherwise non-text data
442 // would show up as text:
443 TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
444 TAG_HANDLER_CONSTR(DoNothing) { }
445
446 TAG_HANDLER_PROC(WXUNUSED(tag))
447 {
448 return true;
449 }
450 TAG_HANDLER_END(DoNothing)
451
452
453
454
455
456 TAGS_MODULE_BEGIN(Layout)
457
458 TAGS_MODULE_ADD(P)
459 TAGS_MODULE_ADD(BR)
460 TAGS_MODULE_ADD(CENTER)
461 TAGS_MODULE_ADD(DIV)
462 TAGS_MODULE_ADD(TITLE)
463 TAGS_MODULE_ADD(BODY)
464 TAGS_MODULE_ADD(BLOCKQUOTE)
465 TAGS_MODULE_ADD(SUBSUP)
466 TAGS_MODULE_ADD(DoNothing)
467
468 TAGS_MODULE_END(Layout)
469
470 #endif