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