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
;
60 for (i
= 0; i
< 2; i
++)
61 for (j
= 0; j
< 2; j
++)
62 for (k
= 0; k
< 2; k
++)
63 for (l
= 0; l
< 2; l
++)
64 for (m
= 0; m
< 7; m
++)
66 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
67 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
69 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
73 static int default_sizes
[7] = {7, 8, 10, 12, 16, 22, 30};
74 #elif defined(__WXMAC__)
75 static int default_sizes
[7] = {9, 12, 14, 18, 24, 30, 36};
77 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
79 SetFonts(wxT(""), wxT(""), default_sizes
);
82 // fill in wxHtmlParser's tables:
83 wxNode
*node
= m_Modules
.GetFirst();
86 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
87 mod
->FillHandlersTable(this);
88 node
= node
->GetNext();
92 wxHtmlWinParser::~wxHtmlWinParser()
96 for (i
= 0; i
< 2; i
++)
97 for (j
= 0; j
< 2; j
++)
98 for (k
= 0; k
< 2; k
++)
99 for (l
= 0; l
< 2; l
++)
100 for (m
= 0; m
< 7; m
++)
102 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
103 delete m_FontsTable
[i
][j
][k
][l
][m
];
108 delete[] m_tmpStrBuf
;
111 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
113 m_Modules
.Append(module);
116 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
118 m_Modules
.DeleteObject(module);
121 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
125 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
126 m_FontFaceFixed
= fixed_face
;
127 m_FontFaceNormal
= normal_face
;
130 SetInputEncoding(m_InputEnc
);
133 for (i
= 0; i
< 2; i
++)
134 for (j
= 0; j
< 2; j
++)
135 for (k
= 0; k
< 2; k
++)
136 for (l
= 0; l
< 2; l
++)
137 for (m
= 0; m
< 7; m
++) {
138 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
140 delete m_FontsTable
[i
][j
][k
][l
][m
];
141 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
146 void wxHtmlWinParser::InitParser(const wxString
& source
)
148 wxHtmlParser::InitParser(source
);
149 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
151 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
152 m_FontSize
= 3; //default one
153 CreateCurrentFont(); // we're selecting default font into
154 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
155 /* NOTE : we're not using GetCharWidth/Height() because
156 of differences under X and win
160 m_Link
= wxHtmlLinkInfo( wxT(""), wxT("") );
161 m_LinkColor
.Set(0, 0, 0xFF);
162 m_ActualColor
.Set(0, 0, 0);
163 m_Align
= wxHTML_ALIGN_LEFT
;
164 m_tmpLastWasSpace
= FALSE
;
169 #if 0 //!wxUSE_UNICODE
170 wxString charset
= ExtractCharsetInformation(source
);
171 if (!charset
.empty())
173 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
174 if (enc
!= wxFONTENCODING_SYSTEM
)
175 SetInputEncoding(enc
);
179 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
180 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
183 void wxHtmlWinParser::DoneParser()
187 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
189 wxHtmlParser::DoneParser();
192 wxObject
* wxHtmlWinParser::GetProduct()
194 wxHtmlContainerCell
*top
;
200 while (top
->GetParent()) top
= top
->GetParent();
204 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
205 const wxString
& url
) const
207 // FIXME - normalize the URL to full path before passing to
212 wxHtmlOpeningStatus status
;
216 status
= m_Window
->OnOpeningURL(type
, myurl
, &redirect
);
217 if ( status
!= wxHTML_REDIRECT
)
223 if ( status
== wxHTML_BLOCK
)
226 return GetFS()->OpenFile(myurl
);
229 return wxHtmlParser::OpenURL(type
, url
);
232 void wxHtmlWinParser::AddText(const wxChar
* txt
)
240 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
242 if (lng
+1 > m_tmpStrBufSize
)
244 delete[] m_tmpStrBuf
;
245 m_tmpStrBuf
= new wxChar
[lng
+1];
246 m_tmpStrBufSize
= lng
+1;
248 wxChar
*temp
= m_tmpStrBuf
;
250 if (m_tmpLastWasSpace
)
253 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
254 (txt
[i
] == wxT('\t')))) i
++;
260 d
= temp
[templen
++] = txt
[i
];
261 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
264 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
265 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
271 temp
[templen
-1] = wxT(' ');
273 #if 0 // VS - WHY was this here?!
274 if (templen
== 1) continue;
279 m_EncConv
->Convert(temp
);
281 size_t len
= wxStrlen(temp
);
282 for (size_t j
= 0; j
< len
; j
++)
285 c
= new wxHtmlWordCell(temp
, *(GetDC()));
288 m_Container
->InsertCell(c
);
289 m_tmpLastWasSpace
= TRUE
;
293 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
298 m_EncConv
->Convert(temp
);
300 size_t len
= wxStrlen(temp
);
301 for (size_t j
= 0; j
< len
; j
++)
304 c
= new wxHtmlWordCell(temp
, *(GetDC()));
307 m_Container
->InsertCell(c
);
308 m_tmpLastWasSpace
= FALSE
;
314 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
316 m_Container
= new wxHtmlContainerCell(m_Container
);
317 m_Container
->SetAlignHor(m_Align
);
318 m_tmpLastWasSpace
= TRUE
;
319 /* to avoid space being first character in paragraph */
325 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
327 m_tmpLastWasSpace
= TRUE
;
328 /* to avoid space being first character in paragraph */
329 return m_Container
= c
;
334 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
336 m_Container
= m_Container
->GetParent();
341 void wxHtmlWinParser::SetFontSize(int s
)
344 else if (s
> 7) s
= 7;
350 wxFont
* wxHtmlWinParser::CreateCurrentFont()
352 int fb
= GetFontBold(),
353 fi
= GetFontItalic(),
354 fu
= GetFontUnderlined(),
356 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
358 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
359 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
360 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
362 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
365 if (*fontptr
!= NULL
&& (*faceptr
!= face
367 || *encptr
!= m_OutputEnc
375 if (*fontptr
== NULL
)
378 *fontptr
= new wxFont(
379 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
380 ff
? wxMODERN
: wxSWISS
,
381 fi
? wxITALIC
: wxNORMAL
,
382 fb
? wxBOLD
: wxNORMAL
,
383 fu
? TRUE
: FALSE
, face
388 *encptr
= m_OutputEnc
;
391 m_DC
->SetFont(**fontptr
);
397 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
400 m_UseLink
= (link
.GetHref() != wxEmptyString
);
404 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
406 if (GetFontFixed()) m_FontFaceFixed
= face
;
407 else m_FontFaceNormal
= face
;
410 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
411 SetInputEncoding(m_InputEnc
);
418 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
420 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
427 if (enc
== wxFONTENCODING_DEFAULT
) return;
429 wxFontEncoding altfix
, altnorm
;
430 bool availfix
, availnorm
;
433 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
434 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
435 if (availnorm
&& availfix
)
439 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
440 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
442 m_OutputEnc
= altnorm
;
444 // at least normal face?
447 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
448 m_OutputEnc
= altnorm
;
450 // okay, let convert to ISO_8859-1, available always
452 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
455 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
456 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
458 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
460 if (m_InputEnc
== m_OutputEnc
) return;
462 m_EncConv
= new wxEncodingConverter();
463 if (!m_EncConv
->Init(m_InputEnc
,
464 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
465 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
466 wxCONVERT_SUBSTITUTE
))
467 { // total failture :-(
468 wxLogError(_("Failed to display HTML document in %s encoding"),
469 wxFontMapper::GetEncodingName(enc
).c_str());
470 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
480 //-----------------------------------------------------------------------------
481 // wxHtmlWinTagHandler
482 //-----------------------------------------------------------------------------
484 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
486 //-----------------------------------------------------------------------------
488 //-----------------------------------------------------------------------------
490 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
491 // This module is an ancestor for tag handlers modules defined
492 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
494 // Do not add any winpars.cpp shutdown or initialization code to it,
495 // create a new module instead!
497 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
499 bool wxHtmlTagsModule::OnInit()
501 wxHtmlWinParser::AddModule(this);
505 void wxHtmlTagsModule::OnExit()
507 wxHtmlWinParser::RemoveModule(this);