]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_layout.cpp
don't require <value> node in wxColourPickerCtrl's XRC handler, use black as default...
[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 WXPRECOMP
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.Count() == 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 + GetParent()->GetPosY() + GetParent()->GetHeight();
112
113 // Search the array of pagebreaks to see whether we've already set
114 // a pagebreak here. The standard bsearch() function is appropriate
115 // because the array of pagebreaks through known_pagebreaks[number_of_pages]
116 // is known to be sorted in strictly increasing order. '1 + number_of_pages'
117 // is used as a bsearch() argument because the array contains a leading
118 // zero plus one element for each page.
119 int where = known_pagebreaks.Index( total_height);
120 // Add a pagebreak only if there isn't one already set here.
121 if( wxNOT_FOUND != where)
122 {
123 return false;
124 }
125 else
126 {
127 *pagebreak = m_PosY;
128 return true;
129 }
130 }
131
132
133
134 TAG_HANDLER_BEGIN(P, "P")
135 TAG_HANDLER_CONSTR(P) { }
136
137 TAG_HANDLER_PROC(tag)
138 {
139 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
140 {
141 m_WParser->CloseContainer();
142 m_WParser->OpenContainer();
143 }
144 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
145 m_WParser->GetContainer()->SetAlign(tag);
146 return false;
147 }
148
149 TAG_HANDLER_END(P)
150
151
152
153 TAG_HANDLER_BEGIN(BR, "BR")
154 TAG_HANDLER_CONSTR(BR) { }
155
156 TAG_HANDLER_PROC(tag)
157 {
158 int al = m_WParser->GetContainer()->GetAlignHor();
159 wxHtmlContainerCell *c;
160
161 m_WParser->CloseContainer();
162 c = m_WParser->OpenContainer();
163 c->SetAlignHor(al);
164 c->SetAlign(tag);
165 c->SetMinHeight(m_WParser->GetCharHeight());
166 return false;
167 }
168
169 TAG_HANDLER_END(BR)
170
171
172
173 TAG_HANDLER_BEGIN(CENTER, "CENTER")
174 TAG_HANDLER_CONSTR(CENTER) { }
175
176 TAG_HANDLER_PROC(tag)
177 {
178 int old = m_WParser->GetAlign();
179 wxHtmlContainerCell *c = m_WParser->GetContainer();
180
181 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
182 if (c->GetFirstChild() != NULL)
183 {
184 m_WParser->CloseContainer();
185 m_WParser->OpenContainer();
186 }
187 else
188 c->SetAlignHor(wxHTML_ALIGN_CENTER);
189
190 if (tag.HasEnding())
191 {
192 ParseInner(tag);
193
194 m_WParser->SetAlign(old);
195 if (c->GetFirstChild() != NULL)
196 {
197 m_WParser->CloseContainer();
198 m_WParser->OpenContainer();
199 }
200 else
201 c->SetAlignHor(old);
202
203 return true;
204 }
205 else return false;
206 }
207
208 TAG_HANDLER_END(CENTER)
209
210
211
212 TAG_HANDLER_BEGIN(DIV, "DIV")
213 TAG_HANDLER_CONSTR(DIV) { }
214
215 TAG_HANDLER_PROC(tag)
216 {
217 if(tag.HasParam(wxT("STYLE")))
218 {
219 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
220 {
221 m_WParser->CloseContainer();
222 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
223 m_WParser->CloseContainer();
224 m_WParser->OpenContainer();
225 return false;
226 }
227 else
228 {
229 // Treat other STYLE parameters here when they're supported.
230 return false;
231 }
232 }
233 else if(tag.HasParam(wxT("ALIGN")))
234 {
235 int old = m_WParser->GetAlign();
236 wxHtmlContainerCell *c = m_WParser->GetContainer();
237 if (c->GetFirstChild() != NULL)
238 {
239 m_WParser->CloseContainer();
240 m_WParser->OpenContainer();
241 c = m_WParser->GetContainer();
242 c->SetAlign(tag);
243 m_WParser->SetAlign(c->GetAlignHor());
244 }
245 else
246 {
247 c->SetAlign(tag);
248 m_WParser->SetAlign(c->GetAlignHor());
249 }
250
251 ParseInner(tag);
252
253 m_WParser->SetAlign(old);
254 if (c->GetFirstChild() != NULL)
255 {
256 m_WParser->CloseContainer();
257 m_WParser->OpenContainer();
258 }
259 else
260 c->SetAlignHor(old);
261
262 return true;
263 }
264 else
265 {
266 // Same as BR
267 int al = m_WParser->GetContainer()->GetAlignHor();
268 wxHtmlContainerCell *c;
269
270 m_WParser->CloseContainer();
271 c = m_WParser->OpenContainer();
272 c->SetAlignHor(al);
273 c->SetAlign(tag);
274 c->SetMinHeight(m_WParser->GetCharHeight());
275 return false;
276 }
277 }
278
279 TAG_HANDLER_END(DIV)
280
281
282
283
284 TAG_HANDLER_BEGIN(TITLE, "TITLE")
285 TAG_HANDLER_CONSTR(TITLE) { }
286
287 TAG_HANDLER_PROC(tag)
288 {
289 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
290 if (winIface)
291 {
292 wxString title = m_WParser->GetSource()->Mid(
293 tag.GetBeginPos(),
294 tag.GetEndPos1()-tag.GetBeginPos());
295 #if !wxUSE_UNICODE && wxUSE_WCHAR_T
296 wxCSConv conv(m_WParser->GetInputEncoding());
297 title = wxString(title.wc_str(conv), wxConvLocal);
298 #endif
299 title = m_WParser->GetEntitiesParser()->Parse(title);
300
301 winIface->SetHTMLWindowTitle(title);
302 }
303 return true;
304 }
305
306 TAG_HANDLER_END(TITLE)
307
308
309
310
311 TAG_HANDLER_BEGIN(BODY, "BODY")
312 TAG_HANDLER_CONSTR(BODY) { }
313
314 TAG_HANDLER_PROC(tag)
315 {
316 wxColour clr;
317
318 if (tag.GetParamAsColour(wxT("TEXT"), &clr))
319 {
320 m_WParser->SetActualColor(clr);
321 m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
322 }
323
324 if (tag.GetParamAsColour(wxT("LINK"), &clr))
325 m_WParser->SetLinkColor(clr);
326
327 wxHtmlWindowInterface *winIface = m_WParser->GetWindowInterface();
328 // the rest of this function requires a window:
329 if ( !winIface )
330 return false;
331
332 if (tag.HasParam(wxT("BACKGROUND")))
333 {
334 wxFSFile *fileBgImage = m_WParser->OpenURL
335 (
336 wxHTML_URL_IMAGE,
337 tag.GetParam(wxT("BACKGROUND"))
338 );
339 if ( fileBgImage )
340 {
341 wxInputStream *is = fileBgImage->GetStream();
342 if ( is )
343 {
344 #if !defined(__WXMSW__) || wxUSE_WXDIB
345 wxImage image(*is);
346 if ( image.Ok() )
347 winIface->SetHTMLBackgroundImage(image);
348 #endif
349 }
350 }
351 }
352
353 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
354 {
355 m_WParser->GetContainer()->InsertCell(
356 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
357 winIface->SetHTMLBackgroundColour(clr);
358 }
359
360 return false;
361 }
362
363 TAG_HANDLER_END(BODY)
364
365
366
367 TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
368 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
369
370 TAG_HANDLER_PROC(tag)
371 {
372 wxHtmlContainerCell *c;
373
374 m_WParser->CloseContainer();
375 c = m_WParser->OpenContainer();
376
377 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
378 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
379 else
380 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
381
382 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
383 m_WParser->OpenContainer();
384 ParseInner(tag);
385 c = m_WParser->CloseContainer();
386 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
387 m_WParser->CloseContainer();
388 m_WParser->OpenContainer();
389 return true;
390 }
391
392 TAG_HANDLER_END(BLOCKQUOTE)
393
394
395
396 TAG_HANDLER_BEGIN(SUBSUP, "SUB,SUP")
397
398 TAG_HANDLER_PROC(tag)
399 {
400 bool issub = (tag.GetName() == wxT("SUB"));
401 wxHtmlScriptMode oldmode = m_WParser->GetScriptMode();
402 int oldbase = m_WParser->GetScriptBaseline();
403 int oldsize = m_WParser->GetFontSize();
404
405 wxHtmlContainerCell *cont = m_WParser->GetContainer();
406 wxHtmlCell *c = cont->GetLastChild();
407
408 m_WParser->SetScriptMode(issub ? wxHTML_SCRIPT_SUB : wxHTML_SCRIPT_SUP);
409 m_WParser->SetScriptBaseline(oldbase + c->GetScriptBaseline());
410
411 // select smaller font
412 m_WParser->SetFontSize(m_WParser->GetFontSize()-2);
413 cont->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
414
415 ParseInner(tag);
416
417 // restore font size
418 m_WParser->SetFontSize(oldsize);
419 m_WParser->GetContainer()->InsertCell(
420 new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
421
422 // restore base and alignment
423 m_WParser->SetScriptBaseline(oldbase);
424 m_WParser->SetScriptMode(oldmode);
425
426 return true;
427 }
428
429 TAG_HANDLER_END(SUBSUP)
430
431
432 // Tag handler for tags that we have to ignore, otherwise non-text data
433 // would show up as text:
434 TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
435 TAG_HANDLER_CONSTR(DoNothing) { }
436
437 TAG_HANDLER_PROC(WXUNUSED(tag))
438 {
439 return true;
440 }
441 TAG_HANDLER_END(DoNothing)
442
443
444
445
446
447 TAGS_MODULE_BEGIN(Layout)
448
449 TAGS_MODULE_ADD(P)
450 TAGS_MODULE_ADD(BR)
451 TAGS_MODULE_ADD(CENTER)
452 TAGS_MODULE_ADD(DIV)
453 TAGS_MODULE_ADD(TITLE)
454 TAGS_MODULE_ADD(BODY)
455 TAGS_MODULE_ADD(BLOCKQUOTE)
456 TAGS_MODULE_ADD(SUBSUP)
457 TAGS_MODULE_ADD(DoNothing)
458
459 TAGS_MODULE_END(Layout)
460
461 #endif