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::SetStandardFonts(int size
,
158 const wxString
& normal_face
,
159 const wxString
& fixed_face
)
161 wxFont defaultFont
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
165 size
= defaultFont
.GetPointSize();
167 f_sizes
[0] = int(size
* 0.6);
168 f_sizes
[1] = int(size
* 0.8);
170 f_sizes
[3] = int(size
* 1.2);
171 f_sizes
[4] = int(size
* 1.4);
172 f_sizes
[5] = int(size
* 1.6);
173 f_sizes
[6] = int(size
* 1.8);
175 wxString normal
= normal_face
.empty() ?
176 defaultFont
.GetFaceName() : normal_face
;
178 SetFonts(normal
, fixed_face
, f_sizes
);
181 void wxHtmlWinParser::InitParser(const wxString
& source
)
183 wxHtmlParser::InitParser(source
);
184 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
186 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
187 m_FontSize
= 3; //default one
188 CreateCurrentFont(); // we're selecting default font into
189 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
190 /* NOTE : we're not using GetCharWidth/Height() because
191 of differences under X and win
195 m_Link
= wxHtmlLinkInfo( wxT(""), wxT("") );
196 m_LinkColor
.Set(0, 0, 0xFF);
197 m_ActualColor
.Set(0, 0, 0);
198 m_Align
= wxHTML_ALIGN_LEFT
;
199 m_tmpLastWasSpace
= false;
200 m_lastWordCell
= NULL
;
206 wxString charset
= ExtractCharsetInformation(source
);
207 if (!charset
.empty())
209 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
210 if (enc
!= wxFONTENCODING_SYSTEM
)
211 SetInputEncoding(enc
);
215 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
216 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
217 m_Container
->InsertCell(
218 new wxHtmlColourCell(GetWindow() ?
219 GetWindow()->GetBackgroundColour() :
221 wxHTML_CLR_BACKGROUND
));
222 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
225 void wxHtmlWinParser::DoneParser()
229 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
231 wxHtmlParser::DoneParser();
234 wxObject
* wxHtmlWinParser::GetProduct()
236 wxHtmlContainerCell
*top
;
242 while (top
->GetParent()) top
= top
->GetParent();
243 top
->RemoveExtraSpacing(true, true);
248 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
249 const wxString
& url
) const
251 // FIXME - normalize the URL to full path before passing to
256 wxHtmlOpeningStatus status
;
260 status
= m_Window
->OnOpeningURL(type
, myurl
, &redirect
);
261 if ( status
!= wxHTML_REDIRECT
)
267 if ( status
== wxHTML_BLOCK
)
270 return GetFS()->OpenFile(myurl
);
273 return wxHtmlParser::OpenURL(type
, url
);
276 void wxHtmlWinParser::AddText(const wxChar
* txt
)
284 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
286 if (lng
+1 > m_tmpStrBufSize
)
288 delete[] m_tmpStrBuf
;
289 m_tmpStrBuf
= new wxChar
[lng
+1];
290 m_tmpStrBufSize
= lng
+1;
292 wxChar
*temp
= m_tmpStrBuf
;
294 if (m_tmpLastWasSpace
)
297 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
298 (txt
[i
] == wxT('\t')))) i
++;
304 d
= temp
[templen
++] = txt
[i
];
305 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
308 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
309 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
315 temp
[templen
-1] = wxT(' ');
320 m_EncConv
->Convert(temp
);
322 size_t len
= wxStrlen(temp
);
323 for (size_t j
= 0; j
< len
; j
++)
326 c
= new wxHtmlWordCell(temp
, *(GetDC()));
329 m_Container
->InsertCell(c
);
330 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
331 m_lastWordCell
= (wxHtmlWordCell
*)c
;
332 m_tmpLastWasSpace
= true;
336 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
341 m_EncConv
->Convert(temp
);
343 size_t len
= wxStrlen(temp
);
344 for (size_t j
= 0; j
< len
; j
++)
347 c
= new wxHtmlWordCell(temp
, *(GetDC()));
350 m_Container
->InsertCell(c
);
351 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
352 m_lastWordCell
= (wxHtmlWordCell
*)c
;
353 m_tmpLastWasSpace
= false;
359 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
361 m_Container
= new wxHtmlContainerCell(m_Container
);
362 m_Container
->SetAlignHor(m_Align
);
363 m_tmpLastWasSpace
= true;
364 /* to avoid space being first character in paragraph */
370 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
372 m_tmpLastWasSpace
= true;
373 /* to avoid space being first character in paragraph */
374 return m_Container
= c
;
379 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
381 m_Container
= m_Container
->GetParent();
386 void wxHtmlWinParser::SetFontSize(int s
)
389 else if (s
> 7) s
= 7;
395 wxFont
* wxHtmlWinParser::CreateCurrentFont()
397 int fb
= GetFontBold(),
398 fi
= GetFontItalic(),
399 fu
= GetFontUnderlined(),
401 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
403 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
404 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
405 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
407 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
410 if (*fontptr
!= NULL
&& (*faceptr
!= face
412 || *encptr
!= m_OutputEnc
420 if (*fontptr
== NULL
)
423 *fontptr
= new wxFont(
424 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
425 ff
? wxMODERN
: wxSWISS
,
426 fi
? wxITALIC
: wxNORMAL
,
427 fb
? wxBOLD
: wxNORMAL
,
428 fu
? true : false, face
433 *encptr
= m_OutputEnc
;
436 m_DC
->SetFont(**fontptr
);
442 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
445 m_UseLink
= (link
.GetHref() != wxEmptyString
);
449 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
451 if (GetFontFixed()) m_FontFaceFixed
= face
;
452 else m_FontFaceNormal
= face
;
455 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
456 SetInputEncoding(m_InputEnc
);
463 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
465 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
472 if (enc
== wxFONTENCODING_DEFAULT
) return;
474 wxFontEncoding altfix
, altnorm
;
475 bool availfix
, availnorm
;
478 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
479 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
480 if (availnorm
&& availfix
)
484 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false) &&
485 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, false) &&
487 m_OutputEnc
= altnorm
;
489 // at least normal face?
492 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, false))
493 m_OutputEnc
= altnorm
;
498 // okay, let convert to ISO_8859-1, available always
499 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
501 m_OutputEnc
= wxLocale::GetSystemEncoding() ;
506 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
507 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
509 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
511 if (m_InputEnc
== m_OutputEnc
) return;
513 m_EncConv
= new wxEncodingConverter();
514 if (!m_EncConv
->Init(m_InputEnc
,
515 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
516 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
517 wxCONVERT_SUBSTITUTE
))
518 { // total failture :-(
519 wxLogError(_("Failed to display HTML document in %s encoding"),
520 wxFontMapper::GetEncodingName(enc
).c_str());
521 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
531 //-----------------------------------------------------------------------------
532 // wxHtmlWinTagHandler
533 //-----------------------------------------------------------------------------
535 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
537 //-----------------------------------------------------------------------------
539 //-----------------------------------------------------------------------------
541 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
542 // This module is an ancestor for tag handlers modules defined
543 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
545 // Do not add any winpars.cpp shutdown or initialization code to it,
546 // create a new module instead!
548 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
550 bool wxHtmlTagsModule::OnInit()
552 wxHtmlWinParser::AddModule(this);
556 void wxHtmlTagsModule::OnExit()
558 wxHtmlWinParser::RemoveModule(this);