]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_layout.cpp
Don't call StartingKey if the key was F2
[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 #include "wx/defs.h"
16
17 #if wxUSE_HTML && wxUSE_STREAMS
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WXPRECOMP
24 #endif
25
26 #include "wx/image.h"
27
28 #include "wx/html/forcelnk.h"
29 #include "wx/html/m_templ.h"
30
31 #include "wx/html/htmlwin.h"
32
33 FORCE_LINK_ME(m_layout)
34
35 #ifdef __WXWINCE__
36 #include "wx/msw/wince/missing.h" // for bsearch()
37 #else
38 #include <stdlib.h> // bsearch()
39 #endif
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
74 class wxHtmlPageBreakCell : public wxHtmlCell
75 {
76 public:
77 wxHtmlPageBreakCell() {}
78
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),
85 wxHtmlRenderingInfo& WXUNUSED(info)) {}
86
87 private:
88 DECLARE_NO_COPY_CLASS(wxHtmlPageBreakCell)
89 };
90
91 // Comparison routine for bsearch into an int* array of pagebreaks.
92 extern "C" int wxCMPFUNC_CONV wxInteger_compare(void const* i0, void const* i1)
93 {
94 return *(int*)i0 - *(int*)i1;
95 }
96
97 bool 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
102 // do anything except return false.
103 //
104 // We also simply return false if the 'pagebreak' argument is
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 {
111 return false;
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),
127 wxInteger_compare);
128 // Add a pagebreak only if there isn't one already set here.
129 if(NULL != where)
130 {
131 return false;
132 }
133 else
134 {
135 *pagebreak = m_PosY;
136 return true;
137 }
138 }
139
140 TAG_HANDLER_BEGIN(P, "P")
141 TAG_HANDLER_CONSTR(P) { }
142
143 TAG_HANDLER_PROC(tag)
144 {
145 if (m_WParser->GetContainer()->GetFirstChild() != NULL)
146 {
147 m_WParser->CloseContainer();
148 m_WParser->OpenContainer();
149 }
150 m_WParser->GetContainer()->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
151 m_WParser->GetContainer()->SetAlign(tag);
152 return false;
153 }
154
155 TAG_HANDLER_END(P)
156
157
158
159 TAG_HANDLER_BEGIN(BR, "BR")
160 TAG_HANDLER_CONSTR(BR) { }
161
162 TAG_HANDLER_PROC(tag)
163 {
164 int al = m_WParser->GetContainer()->GetAlignHor();
165 wxHtmlContainerCell *c;
166
167 m_WParser->CloseContainer();
168 c = m_WParser->OpenContainer();
169 c->SetAlignHor(al);
170 c->SetAlign(tag);
171 c->SetMinHeight(m_WParser->GetCharHeight());
172 return false;
173 }
174
175 TAG_HANDLER_END(BR)
176
177
178
179 TAG_HANDLER_BEGIN(CENTER, "CENTER")
180 TAG_HANDLER_CONSTR(CENTER) { }
181
182 TAG_HANDLER_PROC(tag)
183 {
184 int old = m_WParser->GetAlign();
185 wxHtmlContainerCell *c = m_WParser->GetContainer();
186
187 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
188 if (c->GetFirstChild() != NULL)
189 {
190 m_WParser->CloseContainer();
191 m_WParser->OpenContainer();
192 }
193 else
194 c->SetAlignHor(wxHTML_ALIGN_CENTER);
195
196 if (tag.HasEnding())
197 {
198 ParseInner(tag);
199
200 m_WParser->SetAlign(old);
201 if (c->GetFirstChild() != NULL)
202 {
203 m_WParser->CloseContainer();
204 m_WParser->OpenContainer();
205 }
206 else
207 c->SetAlignHor(old);
208
209 return true;
210 }
211 else return false;
212 }
213
214 TAG_HANDLER_END(CENTER)
215
216
217
218 TAG_HANDLER_BEGIN(DIV, "DIV")
219 TAG_HANDLER_CONSTR(DIV) { }
220
221 TAG_HANDLER_PROC(tag)
222 {
223 if(tag.HasParam(wxT("STYLE")))
224 {
225 if(tag.GetParam(wxT("STYLE")).IsSameAs(wxT("PAGE-BREAK-BEFORE:ALWAYS"), false))
226 {
227 m_WParser->CloseContainer();
228 m_WParser->OpenContainer()->InsertCell(new wxHtmlPageBreakCell);
229 m_WParser->CloseContainer();
230 m_WParser->OpenContainer();
231 return false;
232 }
233 else
234 {
235 // Treat other STYLE parameters here when they're supported.
236 return false;
237 }
238 }
239 else if(tag.HasParam(wxT("ALIGN")))
240 {
241 int old = m_WParser->GetAlign();
242 wxHtmlContainerCell *c = m_WParser->GetContainer();
243 if (c->GetFirstChild() != NULL)
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 }
256
257 ParseInner(tag);
258
259 m_WParser->SetAlign(old);
260 if (c->GetFirstChild() != NULL)
261 {
262 m_WParser->CloseContainer();
263 m_WParser->OpenContainer();
264 }
265 else
266 c->SetAlignHor(old);
267
268 return true;
269 }
270 else
271 {
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;
282 }
283 }
284
285 TAG_HANDLER_END(DIV)
286
287
288
289
290 TAG_HANDLER_BEGIN(TITLE, "TITLE")
291 TAG_HANDLER_CONSTR(TITLE) { }
292
293 TAG_HANDLER_PROC(tag)
294 {
295 if (m_WParser->GetWindow())
296 {
297 wxHtmlWindow *wfr = (wxHtmlWindow*)(m_WParser->GetWindow());
298 if (wfr)
299 {
300 wxString title = m_WParser->GetSource()->Mid(
301 tag.GetBeginPos(),
302 tag.GetEndPos1()-tag.GetBeginPos());
303 #if !wxUSE_UNICODE
304 wxCSConv conv(m_WParser->GetInputEncoding());
305 title = wxString(title.wc_str(conv), wxConvLocal);
306 #endif
307 title = m_WParser->GetEntitiesParser()->Parse(title);
308 wfr->OnSetTitle(title);
309 }
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 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
354 if (tag.GetParamAsColour(wxT("BGCOLOR"), &clr))
355 {
356 m_WParser->GetContainer()->InsertCell(
357 new wxHtmlColourCell(clr, wxHTML_CLR_BACKGROUND));
358 if (m_WParser->GetWindow() != NULL)
359 m_WParser->GetWindow()->SetBackgroundColour(clr);
360 }
361
362 return false;
363 }
364
365 TAG_HANDLER_END(BODY)
366
367
368
369 TAG_HANDLER_BEGIN(BLOCKQUOTE, "BLOCKQUOTE")
370 TAG_HANDLER_CONSTR(BLOCKQUOTE) { }
371
372 TAG_HANDLER_PROC(tag)
373 {
374 wxHtmlContainerCell *c;
375
376 m_WParser->CloseContainer();
377 c = m_WParser->OpenContainer();
378
379 if (c->GetAlignHor() == wxHTML_ALIGN_RIGHT)
380 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_RIGHT);
381 else
382 c->SetIndent(5 * m_WParser->GetCharWidth(), wxHTML_INDENT_LEFT);
383
384 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
385 m_WParser->OpenContainer();
386 ParseInner(tag);
387 c = m_WParser->CloseContainer();
388 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_BOTTOM);
389 m_WParser->CloseContainer();
390 m_WParser->OpenContainer();
391 return true;
392 }
393
394 TAG_HANDLER_END(BLOCKQUOTE)
395
396
397
398 // Tag handler for tags that we have to ignore, otherwise non-text data
399 // would show up as text:
400 TAG_HANDLER_BEGIN(DoNothing, "SCRIPT")
401 TAG_HANDLER_CONSTR(DoNothing) { }
402
403 TAG_HANDLER_PROC(WXUNUSED(tag))
404 {
405 return true;
406 }
407 TAG_HANDLER_END(DoNothing)
408
409
410
411 TAGS_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)
420 TAGS_MODULE_ADD(DoNothing)
421
422 TAGS_MODULE_END(Layout)
423
424 #endif