1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/winpars.cpp
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"
16 #if wxUSE_HTML && wxUSE_STREAMS
23 #include "wx/html/htmldefs.h"
24 #include "wx/html/winpars.h"
25 #include "wx/html/htmlwin.h"
26 #include "wx/fontmap.h"
28 #include "wx/settings.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser
, wxHtmlParser
)
38 wxList
wxHtmlWinParser::m_Modules
;
40 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindowInterface
*wndIface
)
44 m_windowInterface
= wndIface
;
47 m_CharHeight
= m_CharWidth
= 0;
51 m_InputEnc
= wxFONTENCODING_ISO8859_1
;
52 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
54 m_lastWordCell
= NULL
;
58 for (i
= 0; i
< 2; i
++)
59 for (j
= 0; j
< 2; j
++)
60 for (k
= 0; k
< 2; k
++)
61 for (l
= 0; l
< 2; l
++)
62 for (m
= 0; m
< 7; m
++)
64 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
65 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
67 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
71 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
74 // fill in wxHtmlParser's tables:
75 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
78 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
79 mod
->FillHandlersTable(this);
80 node
= node
->GetNext();
84 wxHtmlWinParser::~wxHtmlWinParser()
88 for (i
= 0; i
< 2; i
++)
89 for (j
= 0; j
< 2; j
++)
90 for (k
= 0; k
< 2; k
++)
91 for (l
= 0; l
< 2; l
++)
92 for (m
= 0; m
< 7; m
++)
94 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
95 delete m_FontsTable
[i
][j
][k
][l
][m
];
100 delete[] m_tmpStrBuf
;
103 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
105 m_Modules
.Append(module);
108 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
110 m_Modules
.DeleteObject(module);
113 void wxHtmlWinParser::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
,
116 static int default_sizes
[7] =
127 if (sizes
== NULL
) sizes
= default_sizes
;
131 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
132 m_FontFaceFixed
= fixed_face
;
133 m_FontFaceNormal
= normal_face
;
136 SetInputEncoding(m_InputEnc
);
139 for (i
= 0; i
< 2; i
++)
140 for (j
= 0; j
< 2; j
++)
141 for (k
= 0; k
< 2; k
++)
142 for (l
= 0; l
< 2; l
++)
143 for (m
= 0; m
< 7; m
++) {
144 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
146 delete m_FontsTable
[i
][j
][k
][l
][m
];
147 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
152 void wxHtmlWinParser::SetStandardFonts(int size
,
153 const wxString
& normal_face
,
154 const wxString
& fixed_face
)
156 wxFont defaultFont
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
160 size
= defaultFont
.GetPointSize();
162 f_sizes
[0] = int(size
* 0.6);
163 f_sizes
[1] = int(size
* 0.8);
165 f_sizes
[3] = int(size
* 1.2);
166 f_sizes
[4] = int(size
* 1.4);
167 f_sizes
[5] = int(size
* 1.6);
168 f_sizes
[6] = int(size
* 1.8);
170 wxString normal
= normal_face
.empty() ?
171 defaultFont
.GetFaceName() : normal_face
;
173 SetFonts(normal
, fixed_face
, f_sizes
);
176 void wxHtmlWinParser::InitParser(const wxString
& source
)
178 wxHtmlParser::InitParser(source
);
179 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
181 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
182 m_FontSize
= 3; //default one
183 CreateCurrentFont(); // we're selecting default font into
184 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
185 /* NOTE : we're not using GetCharWidth/Height() because
186 of differences under X and win
190 m_Link
= wxHtmlLinkInfo( wxEmptyString
);
191 m_LinkColor
.Set(0, 0, 0xFF);
192 m_ActualColor
.Set(0, 0, 0);
193 m_Align
= wxHTML_ALIGN_LEFT
;
194 m_ScriptMode
= wxHTML_SCRIPT_NORMAL
;
195 m_ScriptBaseline
= 0;
196 m_tmpLastWasSpace
= false;
197 m_lastWordCell
= NULL
;
203 wxString charset
= ExtractCharsetInformation(source
);
204 if (!charset
.empty())
206 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
207 if (enc
!= wxFONTENCODING_SYSTEM
)
208 SetInputEncoding(enc
);
212 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
213 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
215 m_Container
->InsertCell
220 ? m_windowInterface
->GetHTMLBackgroundColour()
222 wxHTML_CLR_BACKGROUND
226 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
229 void wxHtmlWinParser::DoneParser()
233 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
235 wxHtmlParser::DoneParser();
238 #if WXWIN_COMPATIBILITY_2_6
239 wxHtmlWindow
*wxHtmlWinParser::GetWindow()
241 if (!m_windowInterface
)
243 return wxDynamicCast(m_windowInterface
->GetHTMLWindow(), wxHtmlWindow
);
247 wxObject
* wxHtmlWinParser::GetProduct()
249 wxHtmlContainerCell
*top
;
255 while (top
->GetParent()) top
= top
->GetParent();
256 top
->RemoveExtraSpacing(true, true);
261 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
262 const wxString
& url
) const
264 if ( !m_windowInterface
)
265 return wxHtmlParser::OpenURL(type
, url
);
268 wxHtmlOpeningStatus status
;
271 wxString
myfullurl(myurl
);
273 // consider url as absolute path first
274 wxURI
current(myurl
);
275 myfullurl
= current
.BuildUnescapedURI();
277 // if not absolute then ...
278 if( current
.IsReference() )
280 wxString basepath
= GetFS()->GetPath();
281 wxURI
base(basepath
);
283 // ... try to apply base path if valid ...
284 if( !base
.IsReference() )
286 wxURI
path(myfullurl
);
287 path
.Resolve( base
);
288 myfullurl
= path
.BuildUnescapedURI();
292 // ... or force such addition if not included already
293 if( !current
.GetPath().Contains(base
.GetPath()) )
296 wxURI
connected( basepath
);
297 myfullurl
= connected
.BuildUnescapedURI();
303 status
= m_windowInterface
->OnHTMLOpeningURL(type
, myfullurl
, &redirect
);
304 if ( status
!= wxHTML_REDIRECT
)
310 if ( status
== wxHTML_BLOCK
)
313 return GetFS()->OpenFile(myurl
);
316 void wxHtmlWinParser::AddText(const wxChar
* txt
)
323 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
325 if (lng
+1 > m_tmpStrBufSize
)
327 delete[] m_tmpStrBuf
;
328 m_tmpStrBuf
= new wxChar
[lng
+1];
329 m_tmpStrBufSize
= lng
+1;
331 wxChar
*temp
= m_tmpStrBuf
;
333 if (m_tmpLastWasSpace
)
336 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
337 (txt
[i
] == wxT('\t')))) i
++;
343 d
= temp
[templen
++] = txt
[i
];
344 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
347 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
348 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
354 temp
[templen
-1] = wxT(' ');
355 DoAddText(temp
, templen
, nbsp
);
356 m_tmpLastWasSpace
= true;
360 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
362 DoAddText(temp
, templen
, nbsp
);
363 m_tmpLastWasSpace
= false;
367 void wxHtmlWinParser::DoAddText(wxChar
*temp
, int& templen
, wxChar nbsp
)
373 m_EncConv
->Convert(temp
);
375 size_t len
= wxStrlen(temp
);
376 for (size_t j
= 0; j
< len
; j
++)
382 wxHtmlCell
*c
= new wxHtmlWordCell(temp
, *(GetDC()));
386 m_Container
->InsertCell(c
);
387 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
388 m_lastWordCell
= (wxHtmlWordCell
*)c
;
393 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
395 m_Container
= new wxHtmlContainerCell(m_Container
);
396 m_Container
->SetAlignHor(m_Align
);
397 m_tmpLastWasSpace
= true;
398 /* to avoid space being first character in paragraph */
404 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
406 m_tmpLastWasSpace
= true;
407 /* to avoid space being first character in paragraph */
408 return m_Container
= c
;
413 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
415 m_Container
= m_Container
->GetParent();
420 void wxHtmlWinParser::SetFontSize(int s
)
423 else if (s
> 7) s
= 7;
429 wxFont
* wxHtmlWinParser::CreateCurrentFont()
431 int fb
= GetFontBold(),
432 fi
= GetFontItalic(),
433 fu
= GetFontUnderlined(),
435 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
437 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
438 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
439 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
441 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
444 if (*fontptr
!= NULL
&& (*faceptr
!= face
446 || *encptr
!= m_OutputEnc
454 if (*fontptr
== NULL
)
457 *fontptr
= new wxFont(
458 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
459 ff
? wxMODERN
: wxSWISS
,
460 fi
? wxITALIC
: wxNORMAL
,
461 fb
? wxBOLD
: wxNORMAL
,
462 fu
? true : false, face
467 *encptr
= m_OutputEnc
;
470 m_DC
->SetFont(**fontptr
);
476 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
479 m_UseLink
= (link
.GetHref() != wxEmptyString
);
482 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
484 if (GetFontFixed()) m_FontFaceFixed
= face
;
485 else m_FontFaceNormal
= face
;
488 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
489 SetInputEncoding(m_InputEnc
);
493 void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell
*cell
)
497 cell
->SetLink(GetLink());
499 // apply current script mode settings:
500 cell
->SetScriptMode(GetScriptMode(), GetScriptBaseline());
505 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
507 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
514 if (enc
== wxFONTENCODING_DEFAULT
) return;
516 wxFontEncoding altfix
, altnorm
;
517 bool availfix
, availnorm
;
520 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
521 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
522 if (availnorm
&& availfix
)
526 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false) &&
527 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, false) &&
529 m_OutputEnc
= altnorm
;
531 // at least normal face?
534 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false))
535 m_OutputEnc
= altnorm
;
540 // okay, let convert to ISO_8859-1, available always
541 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
543 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
548 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
549 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
551 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
553 if (m_InputEnc
== m_OutputEnc
) return;
555 m_EncConv
= new wxEncodingConverter();
556 if (!m_EncConv
->Init(m_InputEnc
,
557 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
558 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
559 wxCONVERT_SUBSTITUTE
))
560 { // total failture :-(
561 wxLogError(_("Failed to display HTML document in %s encoding"),
562 wxFontMapper::GetEncodingName(enc
).c_str());
563 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
573 //-----------------------------------------------------------------------------
574 // wxHtmlWinTagHandler
575 //-----------------------------------------------------------------------------
577 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
579 //-----------------------------------------------------------------------------
581 //-----------------------------------------------------------------------------
583 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
584 // This module is an ancestor for tag handlers modules defined
585 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
587 // Do not add any winpars.cpp shutdown or initialization code to it,
588 // create a new module instead!
590 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
592 bool wxHtmlTagsModule::OnInit()
594 wxHtmlWinParser::AddModule(this);
598 void wxHtmlTagsModule::OnExit()
600 wxHtmlWinParser::RemoveModule(this);