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