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
22 #include "wx/settings.h"
25 #include "wx/html/htmldefs.h"
26 #include "wx/html/winpars.h"
27 #include "wx/html/htmlwin.h"
28 #include "wx/fontmap.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
;
199 // open the toplevel container that contains everything else and that
200 // is never closed (this makes parser's life easier):
203 // then open the first container into which page's content will go:
207 wxString charset
= ExtractCharsetInformation(source
);
208 if (!charset
.empty())
210 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
211 if (enc
!= wxFONTENCODING_SYSTEM
)
212 SetInputEncoding(enc
);
216 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
217 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
219 m_Container
->InsertCell
224 ? m_windowInterface
->GetHTMLBackgroundColour()
226 wxHTML_CLR_BACKGROUND
230 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
233 void wxHtmlWinParser::DoneParser()
237 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
239 wxHtmlParser::DoneParser();
242 #if WXWIN_COMPATIBILITY_2_6
243 wxHtmlWindow
*wxHtmlWinParser::GetWindow()
245 if (!m_windowInterface
)
247 return wxDynamicCast(m_windowInterface
->GetHTMLWindow(), wxHtmlWindow
);
251 wxObject
* wxHtmlWinParser::GetProduct()
253 wxHtmlContainerCell
*top
;
259 while (top
->GetParent()) top
= top
->GetParent();
260 top
->RemoveExtraSpacing(true, true);
265 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
266 const wxString
& url
) const
268 if ( !m_windowInterface
)
269 return wxHtmlParser::OpenURL(type
, url
);
272 wxHtmlOpeningStatus status
;
275 wxString
myfullurl(myurl
);
277 // consider url as absolute path first
278 wxURI
current(myurl
);
279 myfullurl
= current
.BuildUnescapedURI();
281 // if not absolute then ...
282 if( current
.IsReference() )
284 wxString basepath
= GetFS()->GetPath();
285 wxURI
base(basepath
);
287 // ... try to apply base path if valid ...
288 if( !base
.IsReference() )
290 wxURI
path(myfullurl
);
291 path
.Resolve( base
);
292 myfullurl
= path
.BuildUnescapedURI();
296 // ... or force such addition if not included already
297 if( !current
.GetPath().Contains(base
.GetPath()) )
300 wxURI
connected( basepath
);
301 myfullurl
= connected
.BuildUnescapedURI();
307 status
= m_windowInterface
->OnHTMLOpeningURL(type
, myfullurl
, &redirect
);
308 if ( status
!= wxHTML_REDIRECT
)
314 if ( status
== wxHTML_BLOCK
)
317 return GetFS()->OpenFile(myurl
);
320 void wxHtmlWinParser::AddText(const wxChar
* txt
)
327 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
329 if (lng
+1 > m_tmpStrBufSize
)
331 delete[] m_tmpStrBuf
;
332 m_tmpStrBuf
= new wxChar
[lng
+1];
333 m_tmpStrBufSize
= lng
+1;
335 wxChar
*temp
= m_tmpStrBuf
;
337 if (m_tmpLastWasSpace
)
340 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
341 (txt
[i
] == wxT('\t')))) i
++;
347 d
= temp
[templen
++] = txt
[i
];
348 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
351 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
352 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
358 temp
[templen
-1] = wxT(' ');
359 DoAddText(temp
, templen
, nbsp
);
360 m_tmpLastWasSpace
= true;
364 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
366 DoAddText(temp
, templen
, nbsp
);
367 m_tmpLastWasSpace
= false;
371 void wxHtmlWinParser::DoAddText(wxChar
*temp
, int& templen
, wxChar nbsp
)
377 m_EncConv
->Convert(temp
);
379 size_t len
= wxStrlen(temp
);
380 for (size_t j
= 0; j
< len
; j
++)
386 wxHtmlCell
*c
= new wxHtmlWordCell(temp
, *(GetDC()));
390 m_Container
->InsertCell(c
);
391 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
392 m_lastWordCell
= (wxHtmlWordCell
*)c
;
397 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
399 m_Container
= new wxHtmlContainerCell(m_Container
);
400 m_Container
->SetAlignHor(m_Align
);
401 m_tmpLastWasSpace
= true;
402 /* to avoid space being first character in paragraph */
408 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
410 m_tmpLastWasSpace
= true;
411 /* to avoid space being first character in paragraph */
412 return m_Container
= c
;
417 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
419 m_Container
= m_Container
->GetParent();
424 void wxHtmlWinParser::SetFontSize(int s
)
427 else if (s
> 7) s
= 7;
433 wxFont
* wxHtmlWinParser::CreateCurrentFont()
435 int fb
= GetFontBold(),
436 fi
= GetFontItalic(),
437 fu
= GetFontUnderlined(),
439 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
441 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
442 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
443 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
445 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
448 if (*fontptr
!= NULL
&& (*faceptr
!= face
450 || *encptr
!= m_OutputEnc
458 if (*fontptr
== NULL
)
461 *fontptr
= new wxFont(
462 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
463 ff
? wxMODERN
: wxSWISS
,
464 fi
? wxITALIC
: wxNORMAL
,
465 fb
? wxBOLD
: wxNORMAL
,
466 fu
? true : false, face
471 *encptr
= m_OutputEnc
;
474 m_DC
->SetFont(**fontptr
);
480 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
483 m_UseLink
= (link
.GetHref() != wxEmptyString
);
486 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
488 if (GetFontFixed()) m_FontFaceFixed
= face
;
489 else m_FontFaceNormal
= face
;
492 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
493 SetInputEncoding(m_InputEnc
);
497 void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell
*cell
)
501 cell
->SetLink(GetLink());
503 // apply current script mode settings:
504 cell
->SetScriptMode(GetScriptMode(), GetScriptBaseline());
509 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
511 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
518 if (enc
== wxFONTENCODING_DEFAULT
) return;
520 wxFontEncoding altfix
, altnorm
;
521 bool availfix
, availnorm
;
524 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
525 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
526 if (availnorm
&& availfix
)
530 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false) &&
531 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, false) &&
533 m_OutputEnc
= altnorm
;
535 // at least normal face?
538 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false))
539 m_OutputEnc
= altnorm
;
544 // okay, let's convert to ISO_8859-1, available always
545 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
547 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
552 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
553 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
555 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
557 if (m_InputEnc
== m_OutputEnc
) return;
559 m_EncConv
= new wxEncodingConverter();
560 if (!m_EncConv
->Init(m_InputEnc
,
561 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
562 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
563 wxCONVERT_SUBSTITUTE
))
564 { // total failure :-(
565 wxLogError(_("Failed to display HTML document in %s encoding"),
566 wxFontMapper::GetEncodingName(enc
).c_str());
567 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
577 //-----------------------------------------------------------------------------
578 // wxHtmlWinTagHandler
579 //-----------------------------------------------------------------------------
581 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
583 //-----------------------------------------------------------------------------
585 //-----------------------------------------------------------------------------
587 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
588 // This module is an ancestor for tag handlers modules defined
589 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
591 // Do not add any winpars.cpp shutdown or initialization code to it,
592 // create a new module instead!
594 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
596 bool wxHtmlTagsModule::OnInit()
598 wxHtmlWinParser::AddModule(this);
602 void wxHtmlTagsModule::OnExit()
604 wxHtmlWinParser::RemoveModule(this);