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