1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
34 #include "wx/settings.h"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinParser
, wxHtmlParser
)
43 wxList
wxHtmlWinParser::m_Modules
;
45 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow
*wnd
) : wxHtmlParser()
52 m_CharHeight
= m_CharWidth
= 0;
56 m_InputEnc
= wxFONTENCODING_ISO8859_1
;
57 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
59 m_lastWordCell
= NULL
;
63 for (i
= 0; i
< 2; i
++)
64 for (j
= 0; j
< 2; j
++)
65 for (k
= 0; k
< 2; k
++)
66 for (l
= 0; l
< 2; l
++)
67 for (m
= 0; m
< 7; m
++)
69 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
70 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
72 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
76 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
79 // fill in wxHtmlParser's tables:
80 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
83 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
84 mod
->FillHandlersTable(this);
85 node
= node
->GetNext();
89 wxHtmlWinParser::~wxHtmlWinParser()
93 for (i
= 0; i
< 2; i
++)
94 for (j
= 0; j
< 2; j
++)
95 for (k
= 0; k
< 2; k
++)
96 for (l
= 0; l
< 2; l
++)
97 for (m
= 0; m
< 7; m
++)
99 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
100 delete m_FontsTable
[i
][j
][k
][l
][m
];
105 delete[] m_tmpStrBuf
;
108 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
110 m_Modules
.Append(module);
113 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
115 m_Modules
.DeleteObject(module);
118 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
,
121 static int default_sizes
[7] =
132 if (sizes
== NULL
) sizes
= default_sizes
;
136 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
137 m_FontFaceFixed
= fixed_face
;
138 m_FontFaceNormal
= normal_face
;
141 SetInputEncoding(m_InputEnc
);
144 for (i
= 0; i
< 2; i
++)
145 for (j
= 0; j
< 2; j
++)
146 for (k
= 0; k
< 2; k
++)
147 for (l
= 0; l
< 2; l
++)
148 for (m
= 0; m
< 7; m
++) {
149 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
151 delete m_FontsTable
[i
][j
][k
][l
][m
];
152 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
157 void wxHtmlWinParser::NormalizeFontSizes(int size
)
161 size
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
).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 SetFonts(wxEmptyString
, wxEmptyString
, f_sizes
);
174 void wxHtmlWinParser::InitParser(const wxString
& source
)
176 wxHtmlParser::InitParser(source
);
177 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
179 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
180 m_FontSize
= 3; //default one
181 CreateCurrentFont(); // we're selecting default font into
182 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
183 /* NOTE : we're not using GetCharWidth/Height() because
184 of differences under X and win
188 m_Link
= wxHtmlLinkInfo( wxT(""), wxT("") );
189 m_LinkColor
.Set(0, 0, 0xFF);
190 m_ActualColor
.Set(0, 0, 0);
191 m_Align
= wxHTML_ALIGN_LEFT
;
192 m_tmpLastWasSpace
= FALSE
;
193 m_lastWordCell
= NULL
;
199 wxString charset
= ExtractCharsetInformation(source
);
200 if (!charset
.empty())
202 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
203 if (enc
!= wxFONTENCODING_SYSTEM
)
204 SetInputEncoding(enc
);
208 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
209 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
210 m_Container
->InsertCell(
211 new wxHtmlColourCell(GetWindow() ?
212 GetWindow()->GetBackgroundColour() :
214 wxHTML_CLR_BACKGROUND
));
215 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
218 void wxHtmlWinParser::DoneParser()
222 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
224 wxHtmlParser::DoneParser();
227 wxObject
* wxHtmlWinParser::GetProduct()
229 wxHtmlContainerCell
*top
;
235 while (top
->GetParent()) top
= top
->GetParent();
236 top
->RemoveExtraSpacing(true, true);
241 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
242 const wxString
& url
) const
244 // FIXME - normalize the URL to full path before passing to
249 wxHtmlOpeningStatus status
;
253 status
= m_Window
->OnOpeningURL(type
, myurl
, &redirect
);
254 if ( status
!= wxHTML_REDIRECT
)
260 if ( status
== wxHTML_BLOCK
)
263 return GetFS()->OpenFile(myurl
);
266 return wxHtmlParser::OpenURL(type
, url
);
269 void wxHtmlWinParser::AddText(const wxChar
* txt
)
277 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
279 if (lng
+1 > m_tmpStrBufSize
)
281 delete[] m_tmpStrBuf
;
282 m_tmpStrBuf
= new wxChar
[lng
+1];
283 m_tmpStrBufSize
= lng
+1;
285 wxChar
*temp
= m_tmpStrBuf
;
287 if (m_tmpLastWasSpace
)
290 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
291 (txt
[i
] == wxT('\t')))) i
++;
297 d
= temp
[templen
++] = txt
[i
];
298 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
301 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
302 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
308 temp
[templen
-1] = wxT(' ');
313 m_EncConv
->Convert(temp
);
315 size_t len
= wxStrlen(temp
);
316 for (size_t j
= 0; j
< len
; j
++)
319 c
= new wxHtmlWordCell(temp
, *(GetDC()));
322 m_Container
->InsertCell(c
);
323 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
324 m_lastWordCell
= (wxHtmlWordCell
*)c
;
325 m_tmpLastWasSpace
= TRUE
;
329 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
334 m_EncConv
->Convert(temp
);
336 size_t len
= wxStrlen(temp
);
337 for (size_t j
= 0; j
< len
; j
++)
340 c
= new wxHtmlWordCell(temp
, *(GetDC()));
343 m_Container
->InsertCell(c
);
344 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
345 m_lastWordCell
= (wxHtmlWordCell
*)c
;
346 m_tmpLastWasSpace
= FALSE
;
352 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
354 m_Container
= new wxHtmlContainerCell(m_Container
);
355 m_Container
->SetAlignHor(m_Align
);
356 m_tmpLastWasSpace
= TRUE
;
357 /* to avoid space being first character in paragraph */
363 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
365 m_tmpLastWasSpace
= TRUE
;
366 /* to avoid space being first character in paragraph */
367 return m_Container
= c
;
372 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
374 m_Container
= m_Container
->GetParent();
379 void wxHtmlWinParser::SetFontSize(int s
)
382 else if (s
> 7) s
= 7;
388 wxFont
* wxHtmlWinParser::CreateCurrentFont()
390 int fb
= GetFontBold(),
391 fi
= GetFontItalic(),
392 fu
= GetFontUnderlined(),
394 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
396 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
397 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
398 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
400 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
403 if (*fontptr
!= NULL
&& (*faceptr
!= face
405 || *encptr
!= m_OutputEnc
413 if (*fontptr
== NULL
)
416 *fontptr
= new wxFont(
417 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
418 ff
? wxMODERN
: wxSWISS
,
419 fi
? wxITALIC
: wxNORMAL
,
420 fb
? wxBOLD
: wxNORMAL
,
421 fu
? TRUE
: FALSE
, face
426 *encptr
= m_OutputEnc
;
429 m_DC
->SetFont(**fontptr
);
435 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
438 m_UseLink
= (link
.GetHref() != wxEmptyString
);
442 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
444 if (GetFontFixed()) m_FontFaceFixed
= face
;
445 else m_FontFaceNormal
= face
;
448 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
449 SetInputEncoding(m_InputEnc
);
456 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
458 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
465 if (enc
== wxFONTENCODING_DEFAULT
) return;
467 wxFontEncoding altfix
, altnorm
;
468 bool availfix
, availnorm
;
471 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
472 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
473 if (availnorm
&& availfix
)
477 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
478 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
480 m_OutputEnc
= altnorm
;
482 // at least normal face?
485 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
486 m_OutputEnc
= altnorm
;
491 // okay, let convert to ISO_8859-1, available always
492 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
494 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
499 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
500 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
502 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
504 if (m_InputEnc
== m_OutputEnc
) return;
506 m_EncConv
= new wxEncodingConverter();
507 if (!m_EncConv
->Init(m_InputEnc
,
508 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
509 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
510 wxCONVERT_SUBSTITUTE
))
511 { // total failture :-(
512 wxLogError(_("Failed to display HTML document in %s encoding"),
513 wxFontMapper::GetEncodingName(enc
).c_str());
514 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
524 //-----------------------------------------------------------------------------
525 // wxHtmlWinTagHandler
526 //-----------------------------------------------------------------------------
528 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
530 //-----------------------------------------------------------------------------
532 //-----------------------------------------------------------------------------
534 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
535 // This module is an ancestor for tag handlers modules defined
536 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
538 // Do not add any winpars.cpp shutdown or initialization code to it,
539 // create a new module instead!
541 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
543 bool wxHtmlTagsModule::OnInit()
545 wxHtmlWinParser::AddModule(this);
549 void wxHtmlTagsModule::OnExit()
551 wxHtmlWinParser::RemoveModule(this);