]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: winpars.cpp | |
3 | // Purpose: wxHtmlParser class (generic parser) | |
4 | // Author: Vaclav Slavik | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 1999 Vaclav Slavik | |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "winpars.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #include "wx/defs.h" | |
18 | #if wxUSE_HTML && wxUSE_STREAMS | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
25 | #include "wx/intl.h" | |
26 | #include "wx/dc.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/html/htmldefs.h" | |
30 | #include "wx/html/winpars.h" | |
31 | #include "wx/html/htmlwin.h" | |
32 | #include "wx/fontmap.h" | |
33 | #include "wx/log.h" | |
34 | ||
35 | ||
36 | //----------------------------------------------------------------------------- | |
37 | // wxHtmlWinParser | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | ||
41 | wxList wxHtmlWinParser::m_Modules; | |
42 | ||
43 | wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow *wnd) : wxHtmlParser() | |
44 | { | |
45 | m_tmpStrBuf = NULL; | |
46 | m_tmpStrBufSize = 0; | |
47 | m_Window = wnd; | |
48 | m_Container = NULL; | |
49 | m_DC = NULL; | |
50 | m_CharHeight = m_CharWidth = 0; | |
51 | m_UseLink = FALSE; | |
52 | #if !wxUSE_UNICODE | |
53 | m_EncConv = NULL; | |
54 | m_InputEnc = wxFONTENCODING_ISO8859_1; | |
55 | m_OutputEnc = wxFONTENCODING_DEFAULT; | |
56 | #endif | |
57 | ||
58 | { | |
59 | int i, j, k, l, m; | |
60 | for (i = 0; i < 2; i++) | |
61 | for (j = 0; j < 2; j++) | |
62 | for (k = 0; k < 2; k++) | |
63 | for (l = 0; l < 2; l++) | |
64 | for (m = 0; m < 7; m++) | |
65 | { | |
66 | m_FontsTable[i][j][k][l][m] = NULL; | |
67 | m_FontsFacesTable[i][j][k][l][m] = wxEmptyString; | |
68 | #if !wxUSE_UNICODE | |
69 | m_FontsEncTable[i][j][k][l][m] = wxFONTENCODING_DEFAULT; | |
70 | #endif | |
71 | } | |
72 | ||
73 | SetFonts(wxEmptyString, wxEmptyString, NULL); | |
74 | } | |
75 | ||
76 | // fill in wxHtmlParser's tables: | |
77 | wxNode *node = m_Modules.GetFirst(); | |
78 | while (node) | |
79 | { | |
80 | wxHtmlTagsModule *mod = (wxHtmlTagsModule*) node->GetData(); | |
81 | mod->FillHandlersTable(this); | |
82 | node = node->GetNext(); | |
83 | } | |
84 | } | |
85 | ||
86 | wxHtmlWinParser::~wxHtmlWinParser() | |
87 | { | |
88 | int i, j, k, l, m; | |
89 | ||
90 | for (i = 0; i < 2; i++) | |
91 | for (j = 0; j < 2; j++) | |
92 | for (k = 0; k < 2; k++) | |
93 | for (l = 0; l < 2; l++) | |
94 | for (m = 0; m < 7; m++) | |
95 | { | |
96 | if (m_FontsTable[i][j][k][l][m] != NULL) | |
97 | delete m_FontsTable[i][j][k][l][m]; | |
98 | } | |
99 | #if !wxUSE_UNICODE | |
100 | delete m_EncConv; | |
101 | #endif | |
102 | delete[] m_tmpStrBuf; | |
103 | } | |
104 | ||
105 | void wxHtmlWinParser::AddModule(wxHtmlTagsModule *module) | |
106 | { | |
107 | m_Modules.Append(module); | |
108 | } | |
109 | ||
110 | void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule *module) | |
111 | { | |
112 | m_Modules.DeleteObject(module); | |
113 | } | |
114 | ||
115 | void wxHtmlWinParser::SetFonts(wxString normal_face, wxString fixed_face, | |
116 | const int *sizes) | |
117 | { | |
118 | static int default_sizes[7] = | |
119 | { | |
120 | wxHTML_FONT_SIZE_1, | |
121 | wxHTML_FONT_SIZE_2, | |
122 | wxHTML_FONT_SIZE_3, | |
123 | wxHTML_FONT_SIZE_4, | |
124 | wxHTML_FONT_SIZE_5, | |
125 | wxHTML_FONT_SIZE_6, | |
126 | wxHTML_FONT_SIZE_7 | |
127 | }; | |
128 | ||
129 | if (sizes == NULL) sizes = default_sizes; | |
130 | ||
131 | int i, j, k, l, m; | |
132 | ||
133 | for (i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i]; | |
134 | m_FontFaceFixed = fixed_face; | |
135 | m_FontFaceNormal = normal_face; | |
136 | ||
137 | #if !wxUSE_UNICODE | |
138 | SetInputEncoding(m_InputEnc); | |
139 | #endif | |
140 | ||
141 | for (i = 0; i < 2; i++) | |
142 | for (j = 0; j < 2; j++) | |
143 | for (k = 0; k < 2; k++) | |
144 | for (l = 0; l < 2; l++) | |
145 | for (m = 0; m < 7; m++) { | |
146 | if (m_FontsTable[i][j][k][l][m] != NULL) | |
147 | { | |
148 | delete m_FontsTable[i][j][k][l][m]; | |
149 | m_FontsTable[i][j][k][l][m] = NULL; | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | void wxHtmlWinParser::InitParser(const wxString& source) | |
155 | { | |
156 | wxHtmlParser::InitParser(source); | |
157 | wxASSERT_MSG(m_DC != NULL, wxT("no DC assigned to wxHtmlWinParser!!")); | |
158 | ||
159 | m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE; | |
160 | m_FontSize = 3; //default one | |
161 | CreateCurrentFont(); // we're selecting default font into | |
162 | m_DC->GetTextExtent( wxT("H"), &m_CharWidth, &m_CharHeight); | |
163 | /* NOTE : we're not using GetCharWidth/Height() because | |
164 | of differences under X and win | |
165 | */ | |
166 | ||
167 | m_UseLink = FALSE; | |
168 | m_Link = wxHtmlLinkInfo( wxT(""), wxT("") ); | |
169 | m_LinkColor.Set(0, 0, 0xFF); | |
170 | m_ActualColor.Set(0, 0, 0); | |
171 | m_Align = wxHTML_ALIGN_LEFT; | |
172 | m_tmpLastWasSpace = FALSE; | |
173 | ||
174 | OpenContainer(); | |
175 | OpenContainer(); | |
176 | ||
177 | #if !wxUSE_UNICODE | |
178 | wxString charset = ExtractCharsetInformation(source); | |
179 | if (!charset.empty()) | |
180 | { | |
181 | wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(charset); | |
182 | if (enc != wxFONTENCODING_SYSTEM) | |
183 | SetInputEncoding(enc); | |
184 | } | |
185 | #endif | |
186 | ||
187 | m_Container->InsertCell(new wxHtmlColourCell(m_ActualColor)); | |
188 | m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont())); | |
189 | } | |
190 | ||
191 | void wxHtmlWinParser::DoneParser() | |
192 | { | |
193 | m_Container = NULL; | |
194 | #if !wxUSE_UNICODE | |
195 | SetInputEncoding(wxFONTENCODING_ISO8859_1); // for next call | |
196 | #endif | |
197 | wxHtmlParser::DoneParser(); | |
198 | } | |
199 | ||
200 | wxObject* wxHtmlWinParser::GetProduct() | |
201 | { | |
202 | wxHtmlContainerCell *top; | |
203 | ||
204 | CloseContainer(); | |
205 | OpenContainer(); | |
206 | ||
207 | top = m_Container; | |
208 | while (top->GetParent()) top = top->GetParent(); | |
209 | return top; | |
210 | } | |
211 | ||
212 | wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type, | |
213 | const wxString& url) const | |
214 | { | |
215 | // FIXME - normalize the URL to full path before passing to | |
216 | // OnOpeningURL!! | |
217 | if ( m_Window ) | |
218 | { | |
219 | wxString myurl(url); | |
220 | wxHtmlOpeningStatus status; | |
221 | for (;;) | |
222 | { | |
223 | wxString redirect; | |
224 | status = m_Window->OnOpeningURL(type, myurl, &redirect); | |
225 | if ( status != wxHTML_REDIRECT ) | |
226 | break; | |
227 | ||
228 | myurl = redirect; | |
229 | } | |
230 | ||
231 | if ( status == wxHTML_BLOCK ) | |
232 | return NULL; | |
233 | ||
234 | return GetFS()->OpenFile(myurl); | |
235 | } | |
236 | ||
237 | return wxHtmlParser::OpenURL(type, url); | |
238 | } | |
239 | ||
240 | void wxHtmlWinParser::AddText(const wxChar* txt) | |
241 | { | |
242 | wxHtmlCell *c; | |
243 | size_t i = 0, | |
244 | x, | |
245 | lng = wxStrlen(txt); | |
246 | register wxChar d; | |
247 | int templen = 0; | |
248 | wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */); | |
249 | ||
250 | if (lng+1 > m_tmpStrBufSize) | |
251 | { | |
252 | delete[] m_tmpStrBuf; | |
253 | m_tmpStrBuf = new wxChar[lng+1]; | |
254 | m_tmpStrBufSize = lng+1; | |
255 | } | |
256 | wxChar *temp = m_tmpStrBuf; | |
257 | ||
258 | if (m_tmpLastWasSpace) | |
259 | { | |
260 | while ((i < lng) && | |
261 | ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) || | |
262 | (txt[i] == wxT('\t')))) i++; | |
263 | } | |
264 | ||
265 | while (i < lng) | |
266 | { | |
267 | x = 0; | |
268 | d = temp[templen++] = txt[i]; | |
269 | if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t'))) | |
270 | { | |
271 | i++, x++; | |
272 | while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || | |
273 | (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++; | |
274 | } | |
275 | else i++; | |
276 | ||
277 | if (x) | |
278 | { | |
279 | temp[templen-1] = wxT(' '); | |
280 | temp[templen] = 0; | |
281 | #if 0 // VS - WHY was this here?! | |
282 | if (templen == 1) continue; | |
283 | #endif | |
284 | templen = 0; | |
285 | #if !wxUSE_UNICODE | |
286 | if (m_EncConv) | |
287 | m_EncConv->Convert(temp); | |
288 | #endif | |
289 | size_t len = wxStrlen(temp); | |
290 | for (size_t j = 0; j < len; j++) | |
291 | if (temp[j] == nbsp) | |
292 | temp[j] = wxT(' '); | |
293 | c = new wxHtmlWordCell(temp, *(GetDC())); | |
294 | if (m_UseLink) | |
295 | c->SetLink(m_Link); | |
296 | m_Container->InsertCell(c); | |
297 | m_tmpLastWasSpace = TRUE; | |
298 | } | |
299 | } | |
300 | ||
301 | if (templen && (templen > 1 || temp[0] != wxT(' '))) | |
302 | { | |
303 | temp[templen] = 0; | |
304 | #if !wxUSE_UNICODE | |
305 | if (m_EncConv) | |
306 | m_EncConv->Convert(temp); | |
307 | #endif | |
308 | size_t len = wxStrlen(temp); | |
309 | for (size_t j = 0; j < len; j++) | |
310 | if (temp[j] == nbsp) | |
311 | temp[j] = wxT(' '); | |
312 | c = new wxHtmlWordCell(temp, *(GetDC())); | |
313 | if (m_UseLink) | |
314 | c->SetLink(m_Link); | |
315 | m_Container->InsertCell(c); | |
316 | m_tmpLastWasSpace = FALSE; | |
317 | } | |
318 | } | |
319 | ||
320 | ||
321 | ||
322 | wxHtmlContainerCell* wxHtmlWinParser::OpenContainer() | |
323 | { | |
324 | m_Container = new wxHtmlContainerCell(m_Container); | |
325 | m_Container->SetAlignHor(m_Align); | |
326 | m_tmpLastWasSpace = TRUE; | |
327 | /* to avoid space being first character in paragraph */ | |
328 | return m_Container; | |
329 | } | |
330 | ||
331 | ||
332 | ||
333 | wxHtmlContainerCell* wxHtmlWinParser::SetContainer(wxHtmlContainerCell *c) | |
334 | { | |
335 | m_tmpLastWasSpace = TRUE; | |
336 | /* to avoid space being first character in paragraph */ | |
337 | return m_Container = c; | |
338 | } | |
339 | ||
340 | ||
341 | ||
342 | wxHtmlContainerCell* wxHtmlWinParser::CloseContainer() | |
343 | { | |
344 | m_Container = m_Container->GetParent(); | |
345 | return m_Container; | |
346 | } | |
347 | ||
348 | ||
349 | void wxHtmlWinParser::SetFontSize(int s) | |
350 | { | |
351 | if (s < 1) s = 1; | |
352 | else if (s > 7) s = 7; | |
353 | m_FontSize = s; | |
354 | } | |
355 | ||
356 | ||
357 | ||
358 | wxFont* wxHtmlWinParser::CreateCurrentFont() | |
359 | { | |
360 | int fb = GetFontBold(), | |
361 | fi = GetFontItalic(), | |
362 | fu = GetFontUnderlined(), | |
363 | ff = GetFontFixed(), | |
364 | fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ; | |
365 | ||
366 | wxString face = ff ? m_FontFaceFixed : m_FontFaceNormal; | |
367 | wxString *faceptr = &(m_FontsFacesTable[fb][fi][fu][ff][fs]); | |
368 | wxFont **fontptr = &(m_FontsTable[fb][fi][fu][ff][fs]); | |
369 | #if !wxUSE_UNICODE | |
370 | wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]); | |
371 | #endif | |
372 | ||
373 | if (*fontptr != NULL && (*faceptr != face | |
374 | #if !wxUSE_UNICODE | |
375 | || *encptr != m_OutputEnc | |
376 | #endif | |
377 | )) | |
378 | { | |
379 | delete *fontptr; | |
380 | *fontptr = NULL; | |
381 | } | |
382 | ||
383 | if (*fontptr == NULL) | |
384 | { | |
385 | *faceptr = face; | |
386 | *fontptr = new wxFont( | |
387 | (int) (m_FontsSizes[fs] * m_PixelScale), | |
388 | ff ? wxMODERN : wxSWISS, | |
389 | fi ? wxITALIC : wxNORMAL, | |
390 | fb ? wxBOLD : wxNORMAL, | |
391 | fu ? TRUE : FALSE, face | |
392 | #if wxUSE_UNICODE | |
393 | ); | |
394 | #else | |
395 | , m_OutputEnc); | |
396 | *encptr = m_OutputEnc; | |
397 | #endif | |
398 | } | |
399 | m_DC->SetFont(**fontptr); | |
400 | return (*fontptr); | |
401 | } | |
402 | ||
403 | ||
404 | ||
405 | void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link) | |
406 | { | |
407 | m_Link = link; | |
408 | m_UseLink = (link.GetHref() != wxEmptyString); | |
409 | } | |
410 | ||
411 | ||
412 | void wxHtmlWinParser::SetFontFace(const wxString& face) | |
413 | { | |
414 | if (GetFontFixed()) m_FontFaceFixed = face; | |
415 | else m_FontFaceNormal = face; | |
416 | ||
417 | #if !wxUSE_UNICODE | |
418 | if (m_InputEnc != wxFONTENCODING_DEFAULT) | |
419 | SetInputEncoding(m_InputEnc); | |
420 | #endif | |
421 | } | |
422 | ||
423 | ||
424 | ||
425 | #if !wxUSE_UNICODE | |
426 | void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc) | |
427 | { | |
428 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; | |
429 | if (m_EncConv) | |
430 | { | |
431 | delete m_EncConv; | |
432 | m_EncConv = NULL; | |
433 | } | |
434 | ||
435 | if (enc == wxFONTENCODING_DEFAULT) return; | |
436 | ||
437 | wxFontEncoding altfix, altnorm; | |
438 | bool availfix, availnorm; | |
439 | ||
440 | // exact match? | |
441 | availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal); | |
442 | availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed); | |
443 | if (availnorm && availfix) | |
444 | m_OutputEnc = enc; | |
445 | ||
446 | // alternatives? | |
447 | else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) && | |
448 | wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) && | |
449 | altnorm == altfix) | |
450 | m_OutputEnc = altnorm; | |
451 | ||
452 | // at least normal face? | |
453 | else if (availnorm) | |
454 | m_OutputEnc = enc; | |
455 | else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE)) | |
456 | m_OutputEnc = altnorm; | |
457 | ||
458 | // okay, let convert to ISO_8859-1, available always | |
459 | else | |
460 | m_OutputEnc = wxFONTENCODING_DEFAULT; | |
461 | ||
462 | m_InputEnc = enc; | |
463 | if (m_OutputEnc == wxFONTENCODING_DEFAULT) | |
464 | GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM); | |
465 | else | |
466 | GetEntitiesParser()->SetEncoding(m_OutputEnc); | |
467 | ||
468 | if (m_InputEnc == m_OutputEnc) return; | |
469 | ||
470 | m_EncConv = new wxEncodingConverter(); | |
471 | if (!m_EncConv->Init(m_InputEnc, | |
472 | (m_OutputEnc == wxFONTENCODING_DEFAULT) ? | |
473 | wxFONTENCODING_ISO8859_1 : m_OutputEnc, | |
474 | wxCONVERT_SUBSTITUTE)) | |
475 | { // total failture :-( | |
476 | wxLogError(_("Failed to display HTML document in %s encoding"), | |
477 | wxFontMapper::GetEncodingName(enc).c_str()); | |
478 | m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; | |
479 | delete m_EncConv; | |
480 | m_EncConv = NULL; | |
481 | } | |
482 | } | |
483 | #endif | |
484 | ||
485 | ||
486 | ||
487 | ||
488 | //----------------------------------------------------------------------------- | |
489 | // wxHtmlWinTagHandler | |
490 | //----------------------------------------------------------------------------- | |
491 | ||
492 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler) | |
493 | ||
494 | //----------------------------------------------------------------------------- | |
495 | // wxHtmlTagsModule | |
496 | //----------------------------------------------------------------------------- | |
497 | ||
498 | // NB: This is *NOT* winpars.cpp's initialization and shutdown code!! | |
499 | // This module is an ancestor for tag handlers modules defined | |
500 | // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct. | |
501 | // | |
502 | // Do not add any winpars.cpp shutdown or initialization code to it, | |
503 | // create a new module instead! | |
504 | ||
505 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule) | |
506 | ||
507 | bool wxHtmlTagsModule::OnInit() | |
508 | { | |
509 | wxHtmlWinParser::AddModule(this); | |
510 | return TRUE; | |
511 | } | |
512 | ||
513 | void wxHtmlTagsModule::OnExit() | |
514 | { | |
515 | wxHtmlWinParser::RemoveModule(this); | |
516 | } | |
517 | ||
518 | #endif | |
519 |