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