1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
13 #if wxUSE_HTML && wxUSE_STREAMS
24 #include "wx/html/htmldefs.h"
25 #include "wx/html/winpars.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/fontmap.h"
29 #include "wx/settings.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser
, wxHtmlParser
)
39 wxList
wxHtmlWinParser::m_Modules
;
41 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow
*wnd
) : wxHtmlParser()
48 m_CharHeight
= m_CharWidth
= 0;
52 m_InputEnc
= wxFONTENCODING_ISO8859_1
;
53 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
55 m_lastWordCell
= NULL
;
59 for (i
= 0; i
< 2; i
++)
60 for (j
= 0; j
< 2; j
++)
61 for (k
= 0; k
< 2; k
++)
62 for (l
= 0; l
< 2; l
++)
63 for (m
= 0; m
< 7; m
++)
65 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
66 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
68 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
72 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
75 // fill in wxHtmlParser's tables:
76 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
79 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
80 mod
->FillHandlersTable(this);
81 node
= node
->GetNext();
85 wxHtmlWinParser::~wxHtmlWinParser()
89 for (i
= 0; i
< 2; i
++)
90 for (j
= 0; j
< 2; j
++)
91 for (k
= 0; k
< 2; k
++)
92 for (l
= 0; l
< 2; l
++)
93 for (m
= 0; m
< 7; m
++)
95 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
96 delete m_FontsTable
[i
][j
][k
][l
][m
];
101 delete[] m_tmpStrBuf
;
104 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
106 m_Modules
.Append(module);
109 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
111 m_Modules
.DeleteObject(module);
114 void wxHtmlWinParser::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
117 static int default_sizes
[7] =
128 if (sizes
== NULL
) sizes
= default_sizes
;
132 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
133 m_FontFaceFixed
= fixed_face
;
134 m_FontFaceNormal
= normal_face
;
137 SetInputEncoding(m_InputEnc
);
140 for (i
= 0; i
< 2; i
++)
141 for (j
= 0; j
< 2; j
++)
142 for (k
= 0; k
< 2; k
++)
143 for (l
= 0; l
< 2; l
++)
144 for (m
= 0; m
< 7; m
++) {
145 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
147 delete m_FontsTable
[i
][j
][k
][l
][m
];
148 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
153 void wxHtmlWinParser::SetStandardFonts(int size
,
154 const wxString
& normal_face
,
155 const wxString
& fixed_face
)
157 wxFont defaultFont
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
161 size
= defaultFont
.GetPointSize();
163 f_sizes
[0] = int(size
* 0.6);
164 f_sizes
[1] = int(size
* 0.8);
166 f_sizes
[3] = int(size
* 1.2);
167 f_sizes
[4] = int(size
* 1.4);
168 f_sizes
[5] = int(size
* 1.6);
169 f_sizes
[6] = int(size
* 1.8);
171 wxString normal
= normal_face
.empty() ?
172 defaultFont
.GetFaceName() : normal_face
;
174 SetFonts(normal
, fixed_face
, f_sizes
);
177 void wxHtmlWinParser::InitParser(const wxString
& source
)
179 wxHtmlParser::InitParser(source
);
180 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
182 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
183 m_FontSize
= 3; //default one
184 CreateCurrentFont(); // we're selecting default font into
185 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
186 /* NOTE : we're not using GetCharWidth/Height() because
187 of differences under X and win
191 m_Link
= wxHtmlLinkInfo( wxEmptyString
);
192 m_LinkColor
.Set(0, 0, 0xFF);
193 m_ActualColor
.Set(0, 0, 0);
194 m_Align
= wxHTML_ALIGN_LEFT
;
195 m_tmpLastWasSpace
= false;
196 m_lastWordCell
= NULL
;
202 wxString charset
= ExtractCharsetInformation(source
);
203 if (!charset
.empty())
205 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
206 if (enc
!= wxFONTENCODING_SYSTEM
)
207 SetInputEncoding(enc
);
211 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
212 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
213 m_Container
->InsertCell(
214 new wxHtmlColourCell(GetWindow() ?
215 GetWindow()->GetBackgroundColour() :
217 wxHTML_CLR_BACKGROUND
));
218 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
221 void wxHtmlWinParser::DoneParser()
225 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
227 wxHtmlParser::DoneParser();
230 wxObject
* wxHtmlWinParser::GetProduct()
232 wxHtmlContainerCell
*top
;
238 while (top
->GetParent()) top
= top
->GetParent();
239 top
->RemoveExtraSpacing(true, true);
244 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
245 const wxString
& url
) const
250 wxHtmlOpeningStatus status
;
253 wxString
myfullurl(myurl
);
255 // consider url as absolute path first
256 wxURI
current(myurl
);
257 myfullurl
= current
.BuildUnescapedURI();
259 // if not absolute then ...
260 if( current
.IsReference() )
262 wxString basepath
= GetFS()->GetPath();
263 wxURI
base(basepath
);
265 // ... try to apply base path if valid ...
266 if( !base
.IsReference() )
268 wxURI
path(myfullurl
);
269 path
.Resolve( base
);
270 myfullurl
= path
.BuildUnescapedURI();
274 // ... or force such addition if not included already
275 if( !current
.GetPath().Contains(base
.GetPath()) )
278 wxURI
connected( basepath
);
279 myfullurl
= connected
.BuildUnescapedURI();
285 status
= m_Window
->OnOpeningURL(type
, myfullurl
, &redirect
);
286 if ( status
!= wxHTML_REDIRECT
)
292 if ( status
== wxHTML_BLOCK
)
295 return GetFS()->OpenFile(myurl
);
298 return wxHtmlParser::OpenURL(type
, url
);
301 void wxHtmlWinParser::AddText(const wxChar
* txt
)
308 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
310 if (lng
+1 > m_tmpStrBufSize
)
312 delete[] m_tmpStrBuf
;
313 m_tmpStrBuf
= new wxChar
[lng
+1];
314 m_tmpStrBufSize
= lng
+1;
316 wxChar
*temp
= m_tmpStrBuf
;
318 if (m_tmpLastWasSpace
)
321 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
322 (txt
[i
] == wxT('\t')))) i
++;
328 d
= temp
[templen
++] = txt
[i
];
329 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
332 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
333 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
339 temp
[templen
-1] = wxT(' ');
340 DoAddText(temp
, templen
, nbsp
);
341 m_tmpLastWasSpace
= true;
345 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
347 DoAddText(temp
, templen
, nbsp
);
348 m_tmpLastWasSpace
= false;
352 void wxHtmlWinParser::DoAddText(wxChar
*temp
, int& templen
, wxChar nbsp
)
358 m_EncConv
->Convert(temp
);
360 size_t len
= wxStrlen(temp
);
361 for (size_t j
= 0; j
< len
; j
++)
367 wxHtmlCell
*c
= new wxHtmlWordCell(temp
, *(GetDC()));
371 m_Container
->InsertCell(c
);
372 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
373 m_lastWordCell
= (wxHtmlWordCell
*)c
;
378 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
380 m_Container
= new wxHtmlContainerCell(m_Container
);
381 m_Container
->SetAlignHor(m_Align
);
382 m_tmpLastWasSpace
= true;
383 /* to avoid space being first character in paragraph */
389 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
391 m_tmpLastWasSpace
= true;
392 /* to avoid space being first character in paragraph */
393 return m_Container
= c
;
398 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
400 m_Container
= m_Container
->GetParent();
405 void wxHtmlWinParser::SetFontSize(int s
)
408 else if (s
> 7) s
= 7;
414 wxFont
* wxHtmlWinParser::CreateCurrentFont()
416 int fb
= GetFontBold(),
417 fi
= GetFontItalic(),
418 fu
= GetFontUnderlined(),
420 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
422 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
423 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
424 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
426 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
429 if (*fontptr
!= NULL
&& (*faceptr
!= face
431 || *encptr
!= m_OutputEnc
439 if (*fontptr
== NULL
)
442 *fontptr
= new wxFont(
443 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
444 ff
? wxMODERN
: wxSWISS
,
445 fi
? wxITALIC
: wxNORMAL
,
446 fb
? wxBOLD
: wxNORMAL
,
447 fu
? true : false, face
452 *encptr
= m_OutputEnc
;
455 m_DC
->SetFont(**fontptr
);
461 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
464 m_UseLink
= (link
.GetHref() != wxEmptyString
);
468 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
470 if (GetFontFixed()) m_FontFaceFixed
= face
;
471 else m_FontFaceNormal
= face
;
474 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
475 SetInputEncoding(m_InputEnc
);
482 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
484 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
491 if (enc
== wxFONTENCODING_DEFAULT
) return;
493 wxFontEncoding altfix
, altnorm
;
494 bool availfix
, availnorm
;
497 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
498 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
499 if (availnorm
&& availfix
)
503 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false) &&
504 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, false) &&
506 m_OutputEnc
= altnorm
;
508 // at least normal face?
511 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false))
512 m_OutputEnc
= altnorm
;
517 // okay, let convert to ISO_8859-1, available always
518 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
520 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
525 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
526 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
528 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
530 if (m_InputEnc
== m_OutputEnc
) return;
532 m_EncConv
= new wxEncodingConverter();
533 if (!m_EncConv
->Init(m_InputEnc
,
534 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
535 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
536 wxCONVERT_SUBSTITUTE
))
537 { // total failture :-(
538 wxLogError(_("Failed to display HTML document in %s encoding"),
539 wxFontMapper::GetEncodingName(enc
).c_str());
540 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
550 //-----------------------------------------------------------------------------
551 // wxHtmlWinTagHandler
552 //-----------------------------------------------------------------------------
554 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
556 //-----------------------------------------------------------------------------
558 //-----------------------------------------------------------------------------
560 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
561 // This module is an ancestor for tag handlers modules defined
562 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
564 // Do not add any winpars.cpp shutdown or initialization code to it,
565 // create a new module instead!
567 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
569 bool wxHtmlTagsModule::OnInit()
571 wxHtmlWinParser::AddModule(this);
575 void wxHtmlTagsModule::OnExit()
577 wxHtmlWinParser::RemoveModule(this);