]> git.saurik.com Git - wxWidgets.git/blame - src/html/winpars.cpp
Insure suitable choices of path/drive separator on OS/2.
[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));
eb6a4098 191 m_Container->InsertCell(
3403ccd5
RD
192 new wxHtmlColourCell(GetWindow() ?
193 GetWindow()->GetBackgroundColour() :
194 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW),
eb6a4098 195 wxHTML_CLR_BACKGROUND));
4f9297b0 196 m_Container->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
5526e819
VS
197}
198
5526e819
VS
199void wxHtmlWinParser::DoneParser()
200{
201 m_Container = NULL;
2b5f62a0
VZ
202#if !wxUSE_UNICODE
203 SetInputEncoding(wxFONTENCODING_ISO8859_1); // for next call
204#endif
5526e819
VS
205 wxHtmlParser::DoneParser();
206}
207
5526e819
VS
208wxObject* wxHtmlWinParser::GetProduct()
209{
210 wxHtmlContainerCell *top;
211
212 CloseContainer();
213 OpenContainer();
67cfebc2 214
5526e819 215 top = m_Container;
4f9297b0 216 while (top->GetParent()) top = top->GetParent();
ace0fab4
VS
217 top->RemoveExtraSpacing(true, true);
218
5526e819
VS
219 return top;
220}
221
0423bdc7 222wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type,
6cc4e6b8 223 const wxString& url) const
04db5c3f
VS
224{
225 // FIXME - normalize the URL to full path before passing to
226 // OnOpeningURL!!
227 if ( m_Window )
6cc4e6b8 228 {
6cc4e6b8
VS
229 wxString myurl(url);
230 wxHtmlOpeningStatus status;
231 for (;;)
232 {
0423bdc7
VZ
233 wxString redirect;
234 status = m_Window->OnOpeningURL(type, myurl, &redirect);
235 if ( status != wxHTML_REDIRECT )
6cc4e6b8 236 break;
0423bdc7
VZ
237
238 myurl = redirect;
6cc4e6b8 239 }
0423bdc7 240
6cc4e6b8
VS
241 if ( status == wxHTML_BLOCK )
242 return NULL;
2c892c0b
VZ
243
244 return GetFS()->OpenFile(myurl);
6cc4e6b8 245 }
2c892c0b
VZ
246
247 return wxHtmlParser::OpenURL(type, url);
04db5c3f 248}
5526e819 249
211dfedd 250void wxHtmlWinParser::AddText(const wxChar* txt)
5526e819
VS
251{
252 wxHtmlCell *c;
e3c7fd79
VZ
253 size_t i = 0,
254 x,
255 lng = wxStrlen(txt);
211dfedd 256 register wxChar d;
5526e819 257 int templen = 0;
f23e92e7 258 wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
211dfedd
VS
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;
3c8c8da2
VZ
267
268 if (m_tmpLastWasSpace)
4f9297b0 269 {
3c8c8da2
VZ
270 while ((i < lng) &&
271 ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) ||
211dfedd 272 (txt[i] == wxT('\t')))) i++;
5526e819
VS
273 }
274
3c8c8da2 275 while (i < lng)
4f9297b0 276 {
5526e819
VS
277 x = 0;
278 d = temp[templen++] = txt[i];
3c8c8da2 279 if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t')))
e3c7fd79 280 {
5526e819 281 i++, x++;
3c8c8da2 282 while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) ||
211dfedd 283 (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++;
5526e819
VS
284 }
285 else i++;
286
3c8c8da2 287 if (x)
e3c7fd79 288 {
211dfedd 289 temp[templen-1] = wxT(' ');
5526e819
VS
290 temp[templen] = 0;
291 templen = 0;
2b5f62a0 292#if !wxUSE_UNICODE
3c8c8da2 293 if (m_EncConv)
daa616fc 294 m_EncConv->Convert(temp);
2b5f62a0 295#endif
88dcf47c 296 size_t len = wxStrlen(temp);
f23e92e7 297 for (size_t j = 0; j < len; j++)
88dcf47c
VS
298 if (temp[j] == nbsp)
299 temp[j] = wxT(' ');
300 c = new wxHtmlWordCell(temp, *(GetDC()));
3c8c8da2 301 if (m_UseLink)
daa616fc 302 c->SetLink(m_Link);
4f9297b0 303 m_Container->InsertCell(c);
b6d93b26
VS
304 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
305 m_lastWordCell = (wxHtmlWordCell*)c;
5526e819
VS
306 m_tmpLastWasSpace = TRUE;
307 }
308 }
af035b26
VS
309
310 if (templen && (templen > 1 || temp[0] != wxT(' ')))
4f9297b0 311 {
5526e819 312 temp[templen] = 0;
2b5f62a0 313#if !wxUSE_UNICODE
3c8c8da2 314 if (m_EncConv)
daa616fc 315 m_EncConv->Convert(temp);
2b5f62a0 316#endif
88dcf47c 317 size_t len = wxStrlen(temp);
f23e92e7 318 for (size_t j = 0; j < len; j++)
88dcf47c
VS
319 if (temp[j] == nbsp)
320 temp[j] = wxT(' ');
321 c = new wxHtmlWordCell(temp, *(GetDC()));
211dfedd 322 if (m_UseLink)
daa616fc 323 c->SetLink(m_Link);
4f9297b0 324 m_Container->InsertCell(c);
b6d93b26
VS
325 ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell);
326 m_lastWordCell = (wxHtmlWordCell*)c;
5526e819
VS
327 m_tmpLastWasSpace = FALSE;
328 }
329}
330
331
332
333wxHtmlContainerCell* wxHtmlWinParser::OpenContainer()
334{
335 m_Container = new wxHtmlContainerCell(m_Container);
4f9297b0 336 m_Container->SetAlignHor(m_Align);
5526e819
VS
337 m_tmpLastWasSpace = TRUE;
338 /* to avoid space being first character in paragraph */
339 return m_Container;
340}
341
342
343
344wxHtmlContainerCell* 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
353wxHtmlContainerCell* wxHtmlWinParser::CloseContainer()
354{
4f9297b0 355 m_Container = m_Container->GetParent();
5526e819
VS
356 return m_Container;
357}
358
359
f2c2fa4d
VS
360void 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
5526e819
VS
369wxFont* wxHtmlWinParser::CreateCurrentFont()
370{
371 int fb = GetFontBold(),
372 fi = GetFontItalic(),
373 fu = GetFontUnderlined(),
374 ff = GetFontFixed(),
f2c2fa4d 375 fs = GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
5526e819 376
f1ad10f3
VS
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]);
2b5f62a0 380#if !wxUSE_UNICODE
b250d384 381 wxFontEncoding *encptr = &(m_FontsEncTable[fb][fi][fu][ff][fs]);
2b5f62a0 382#endif
f1ad10f3 383
2b5f62a0
VZ
384 if (*fontptr != NULL && (*faceptr != face
385#if !wxUSE_UNICODE
386 || *encptr != m_OutputEnc
387#endif
388 ))
4f9297b0 389 {
f1ad10f3
VS
390 delete *fontptr;
391 *fontptr = NULL;
392 }
393
3c8c8da2 394 if (*fontptr == NULL)
4f9297b0 395 {
f1ad10f3
VS
396 *faceptr = face;
397 *fontptr = new wxFont(
7a5e6267 398 (int) (m_FontsSizes[fs] * m_PixelScale),
f1ad10f3
VS
399 ff ? wxMODERN : wxSWISS,
400 fi ? wxITALIC : wxNORMAL,
401 fb ? wxBOLD : wxNORMAL,
2b5f62a0
VZ
402 fu ? TRUE : FALSE, face
403#if wxUSE_UNICODE
404 );
405#else
406 , m_OutputEnc);
407 *encptr = m_OutputEnc;
408#endif
5526e819 409 }
4f9297b0 410 m_DC->SetFont(**fontptr);
f1ad10f3 411 return (*fontptr);
5526e819
VS
412}
413
414
415
f2c2fa4d
VS
416void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link)
417{
3c8c8da2 418 m_Link = link;
f2c2fa4d
VS
419 m_UseLink = (link.GetHref() != wxEmptyString);
420}
421
422
3c8c8da2 423void wxHtmlWinParser::SetFontFace(const wxString& face)
b250d384 424{
3c8c8da2 425 if (GetFontFixed()) m_FontFaceFixed = face;
b250d384
VS
426 else m_FontFaceNormal = face;
427
2b5f62a0 428#if !wxUSE_UNICODE
b250d384
VS
429 if (m_InputEnc != wxFONTENCODING_DEFAULT)
430 SetInputEncoding(m_InputEnc);
2b5f62a0 431#endif
b250d384
VS
432}
433
434
435
2b5f62a0 436#if !wxUSE_UNICODE
b250d384
VS
437void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc)
438{
439 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
3c8c8da2 440 if (m_EncConv)
daa616fc 441 {
3c8c8da2 442 delete m_EncConv;
daa616fc
VS
443 m_EncConv = NULL;
444 }
b250d384
VS
445
446 if (enc == wxFONTENCODING_DEFAULT) return;
447
448 wxFontEncoding altfix, altnorm;
449 bool availfix, availnorm;
3c8c8da2
VZ
450
451 // exact match?
142b3bc2
VS
452 availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal);
453 availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed);
3c8c8da2 454 if (availnorm && availfix)
b250d384 455 m_OutputEnc = enc;
3c8c8da2 456
b250d384 457 // alternatives?
142b3bc2
VS
458 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE) &&
459 wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, FALSE) &&
b250d384
VS
460 altnorm == altfix)
461 m_OutputEnc = altnorm;
3c8c8da2 462
b250d384
VS
463 // at least normal face?
464 else if (availnorm)
465 m_OutputEnc = enc;
142b3bc2 466 else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, FALSE))
b250d384 467 m_OutputEnc = altnorm;
3c8c8da2 468
b250d384
VS
469 // okay, let convert to ISO_8859-1, available always
470 else
471 m_OutputEnc = wxFONTENCODING_DEFAULT;
3c8c8da2 472
b250d384 473 m_InputEnc = enc;
daa616fc
VS
474 if (m_OutputEnc == wxFONTENCODING_DEFAULT)
475 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM);
476 else
477 GetEntitiesParser()->SetEncoding(m_OutputEnc);
3c8c8da2 478
b250d384
VS
479 if (m_InputEnc == m_OutputEnc) return;
480
481 m_EncConv = new wxEncodingConverter();
3c8c8da2 482 if (!m_EncConv->Init(m_InputEnc,
b250d384
VS
483 (m_OutputEnc == wxFONTENCODING_DEFAULT) ?
484 wxFONTENCODING_ISO8859_1 : m_OutputEnc,
3c8c8da2 485 wxCONVERT_SUBSTITUTE))
b250d384 486 { // total failture :-(
3c8c8da2
VZ
487 wxLogError(_("Failed to display HTML document in %s encoding"),
488 wxFontMapper::GetEncodingName(enc).c_str());
b250d384
VS
489 m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT;
490 delete m_EncConv;
491 m_EncConv = NULL;
492 }
493}
2b5f62a0 494#endif
b250d384
VS
495
496
f2c2fa4d 497
5526e819
VS
498
499//-----------------------------------------------------------------------------
500// wxHtmlWinTagHandler
501//-----------------------------------------------------------------------------
502
503IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler, wxHtmlTagHandler)
504
5526e819
VS
505//-----------------------------------------------------------------------------
506// wxHtmlTagsModule
507//-----------------------------------------------------------------------------
508
d6a6d666
VS
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!
5526e819
VS
515
516IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule, wxModule)
517
5526e819
VS
518bool wxHtmlTagsModule::OnInit()
519{
520 wxHtmlWinParser::AddModule(this);
521 return TRUE;
522}
523
5526e819
VS
524void wxHtmlTagsModule::OnExit()
525{
f6bcfd97 526 wxHtmlWinParser::RemoveModule(this);
5526e819 527}
d6a6d666 528
223d09f6 529#endif
5526e819 530