1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "winpars.h"
15 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
29 #include "wx/html/htmldefs.h"
30 #include "wx/html/winpars.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/fontmap.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 wxList
wxHtmlWinParser::m_Modules
;
43 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow
*wnd
) : wxHtmlParser()
50 m_CharHeight
= m_CharWidth
= 0;
54 m_InputEnc
= wxFONTENCODING_ISO8859_1
;
55 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
57 m_lastWordCell
= NULL
;
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
++)
65 for (m
= 0; m
< 7; m
++)
67 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
68 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
70 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
74 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
77 // fill in wxHtmlParser's tables:
78 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
81 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
82 mod
->FillHandlersTable(this);
83 node
= node
->GetNext();
87 wxHtmlWinParser::~wxHtmlWinParser()
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
++)
95 for (m
= 0; m
< 7; m
++)
97 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
98 delete m_FontsTable
[i
][j
][k
][l
][m
];
103 delete[] m_tmpStrBuf
;
106 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
108 m_Modules
.Append(module);
111 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
113 m_Modules
.DeleteObject(module);
116 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
,
119 static int default_sizes
[7] =
130 if (sizes
== NULL
) sizes
= default_sizes
;
134 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
135 m_FontFaceFixed
= fixed_face
;
136 m_FontFaceNormal
= normal_face
;
139 SetInputEncoding(m_InputEnc
);
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
++) {
147 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
149 delete m_FontsTable
[i
][j
][k
][l
][m
];
150 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
155 void wxHtmlWinParser::InitParser(const wxString
& source
)
157 wxHtmlParser::InitParser(source
);
158 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
160 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
161 m_FontSize
= 3; //default one
162 CreateCurrentFont(); // we're selecting default font into
163 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
164 /* NOTE : we're not using GetCharWidth/Height() because
165 of differences under X and win
169 m_Link
= wxHtmlLinkInfo( wxT(""), wxT("") );
170 m_LinkColor
.Set(0, 0, 0xFF);
171 m_ActualColor
.Set(0, 0, 0);
172 m_Align
= wxHTML_ALIGN_LEFT
;
173 m_tmpLastWasSpace
= FALSE
;
174 m_lastWordCell
= NULL
;
180 wxString charset
= ExtractCharsetInformation(source
);
181 if (!charset
.empty())
183 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
184 if (enc
!= wxFONTENCODING_SYSTEM
)
185 SetInputEncoding(enc
);
189 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
190 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
193 void wxHtmlWinParser::DoneParser()
197 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
199 wxHtmlParser::DoneParser();
202 wxObject
* wxHtmlWinParser::GetProduct()
204 wxHtmlContainerCell
*top
;
210 while (top
->GetParent()) top
= top
->GetParent();
211 top
->RemoveExtraSpacing(true, true);
216 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
217 const wxString
& url
) const
219 // FIXME - normalize the URL to full path before passing to
224 wxHtmlOpeningStatus status
;
228 status
= m_Window
->OnOpeningURL(type
, myurl
, &redirect
);
229 if ( status
!= wxHTML_REDIRECT
)
235 if ( status
== wxHTML_BLOCK
)
238 return GetFS()->OpenFile(myurl
);
241 return wxHtmlParser::OpenURL(type
, url
);
244 void wxHtmlWinParser::AddText(const wxChar
* txt
)
252 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
254 if (lng
+1 > m_tmpStrBufSize
)
256 delete[] m_tmpStrBuf
;
257 m_tmpStrBuf
= new wxChar
[lng
+1];
258 m_tmpStrBufSize
= lng
+1;
260 wxChar
*temp
= m_tmpStrBuf
;
262 if (m_tmpLastWasSpace
)
265 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
266 (txt
[i
] == wxT('\t')))) i
++;
272 d
= temp
[templen
++] = txt
[i
];
273 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
276 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
277 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
283 temp
[templen
-1] = wxT(' ');
288 m_EncConv
->Convert(temp
);
290 size_t len
= wxStrlen(temp
);
291 for (size_t j
= 0; j
< len
; j
++)
294 c
= new wxHtmlWordCell(temp
, *(GetDC()));
297 m_Container
->InsertCell(c
);
298 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
299 m_lastWordCell
= (wxHtmlWordCell
*)c
;
300 m_tmpLastWasSpace
= TRUE
;
304 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
309 m_EncConv
->Convert(temp
);
311 size_t len
= wxStrlen(temp
);
312 for (size_t j
= 0; j
< len
; j
++)
315 c
= new wxHtmlWordCell(temp
, *(GetDC()));
318 m_Container
->InsertCell(c
);
319 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
320 m_lastWordCell
= (wxHtmlWordCell
*)c
;
321 m_tmpLastWasSpace
= FALSE
;
327 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
329 m_Container
= new wxHtmlContainerCell(m_Container
);
330 m_Container
->SetAlignHor(m_Align
);
331 m_tmpLastWasSpace
= TRUE
;
332 /* to avoid space being first character in paragraph */
338 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
340 m_tmpLastWasSpace
= TRUE
;
341 /* to avoid space being first character in paragraph */
342 return m_Container
= c
;
347 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
349 m_Container
= m_Container
->GetParent();
354 void wxHtmlWinParser::SetFontSize(int s
)
357 else if (s
> 7) s
= 7;
363 wxFont
* wxHtmlWinParser::CreateCurrentFont()
365 int fb
= GetFontBold(),
366 fi
= GetFontItalic(),
367 fu
= GetFontUnderlined(),
369 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
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
]);
375 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
378 if (*fontptr
!= NULL
&& (*faceptr
!= face
380 || *encptr
!= m_OutputEnc
388 if (*fontptr
== NULL
)
391 *fontptr
= new wxFont(
392 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
393 ff
? wxMODERN
: wxSWISS
,
394 fi
? wxITALIC
: wxNORMAL
,
395 fb
? wxBOLD
: wxNORMAL
,
396 fu
? TRUE
: FALSE
, face
401 *encptr
= m_OutputEnc
;
404 m_DC
->SetFont(**fontptr
);
410 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
413 m_UseLink
= (link
.GetHref() != wxEmptyString
);
417 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
419 if (GetFontFixed()) m_FontFaceFixed
= face
;
420 else m_FontFaceNormal
= face
;
423 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
424 SetInputEncoding(m_InputEnc
);
431 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
433 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
440 if (enc
== wxFONTENCODING_DEFAULT
) return;
442 wxFontEncoding altfix
, altnorm
;
443 bool availfix
, availnorm
;
446 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
447 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
448 if (availnorm
&& availfix
)
452 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
453 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
455 m_OutputEnc
= altnorm
;
457 // at least normal face?
460 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
461 m_OutputEnc
= altnorm
;
463 // okay, let convert to ISO_8859-1, available always
465 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
468 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
469 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
471 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
473 if (m_InputEnc
== m_OutputEnc
) return;
475 m_EncConv
= new wxEncodingConverter();
476 if (!m_EncConv
->Init(m_InputEnc
,
477 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
478 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
479 wxCONVERT_SUBSTITUTE
))
480 { // total failture :-(
481 wxLogError(_("Failed to display HTML document in %s encoding"),
482 wxFontMapper::GetEncodingName(enc
).c_str());
483 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
493 //-----------------------------------------------------------------------------
494 // wxHtmlWinTagHandler
495 //-----------------------------------------------------------------------------
497 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
499 //-----------------------------------------------------------------------------
501 //-----------------------------------------------------------------------------
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.
507 // Do not add any winpars.cpp shutdown or initialization code to it,
508 // create a new module instead!
510 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
512 bool wxHtmlTagsModule::OnInit()
514 wxHtmlWinParser::AddModule(this);
518 void wxHtmlTagsModule::OnExit()
520 wxHtmlWinParser::RemoveModule(this);