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