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