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