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 //-----------------------------------------------------------------------------
42 wxList
wxHtmlWinParser::m_Modules
;
44 wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindow
*wnd
) : wxHtmlParser()
51 m_CharHeight
= m_CharWidth
= 0;
55 m_InputEnc
= wxFONTENCODING_ISO8859_1
;
56 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
58 m_lastWordCell
= NULL
;
62 for (i
= 0; i
< 2; i
++)
63 for (j
= 0; j
< 2; j
++)
64 for (k
= 0; k
< 2; k
++)
65 for (l
= 0; l
< 2; l
++)
66 for (m
= 0; m
< 7; m
++)
68 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
69 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
71 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
75 SetFonts(wxEmptyString
, wxEmptyString
, NULL
);
78 // fill in wxHtmlParser's tables:
79 wxList::compatibility_iterator node
= m_Modules
.GetFirst();
82 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
83 mod
->FillHandlersTable(this);
84 node
= node
->GetNext();
88 wxHtmlWinParser::~wxHtmlWinParser()
92 for (i
= 0; i
< 2; i
++)
93 for (j
= 0; j
< 2; j
++)
94 for (k
= 0; k
< 2; k
++)
95 for (l
= 0; l
< 2; l
++)
96 for (m
= 0; m
< 7; m
++)
98 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
99 delete m_FontsTable
[i
][j
][k
][l
][m
];
104 delete[] m_tmpStrBuf
;
107 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
109 m_Modules
.Append(module);
112 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
114 m_Modules
.DeleteObject(module);
117 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
,
120 static int default_sizes
[7] =
131 if (sizes
== NULL
) sizes
= default_sizes
;
135 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
136 m_FontFaceFixed
= fixed_face
;
137 m_FontFaceNormal
= normal_face
;
140 SetInputEncoding(m_InputEnc
);
143 for (i
= 0; i
< 2; i
++)
144 for (j
= 0; j
< 2; j
++)
145 for (k
= 0; k
< 2; k
++)
146 for (l
= 0; l
< 2; l
++)
147 for (m
= 0; m
< 7; m
++) {
148 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
150 delete m_FontsTable
[i
][j
][k
][l
][m
];
151 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
156 void wxHtmlWinParser::InitParser(const wxString
& source
)
158 wxHtmlParser::InitParser(source
);
159 wxASSERT_MSG(m_DC
!= NULL
, wxT("no DC assigned to wxHtmlWinParser!!"));
161 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
162 m_FontSize
= 3; //default one
163 CreateCurrentFont(); // we're selecting default font into
164 m_DC
->GetTextExtent( wxT("H"), &m_CharWidth
, &m_CharHeight
);
165 /* NOTE : we're not using GetCharWidth/Height() because
166 of differences under X and win
170 m_Link
= wxHtmlLinkInfo( wxT(""), wxT("") );
171 m_LinkColor
.Set(0, 0, 0xFF);
172 m_ActualColor
.Set(0, 0, 0);
173 m_Align
= wxHTML_ALIGN_LEFT
;
174 m_tmpLastWasSpace
= FALSE
;
175 m_lastWordCell
= NULL
;
181 wxString charset
= ExtractCharsetInformation(source
);
182 if (!charset
.empty())
184 wxFontEncoding enc
= wxFontMapper::Get()->CharsetToEncoding(charset
);
185 if (enc
!= wxFONTENCODING_SYSTEM
)
186 SetInputEncoding(enc
);
190 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
191 wxColour windowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
) ;
192 m_Container
->InsertCell(
193 new wxHtmlColourCell(GetWindow() ?
194 GetWindow()->GetBackgroundColour() :
196 wxHTML_CLR_BACKGROUND
));
197 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
200 void wxHtmlWinParser::DoneParser()
204 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
206 wxHtmlParser::DoneParser();
209 wxObject
* wxHtmlWinParser::GetProduct()
211 wxHtmlContainerCell
*top
;
217 while (top
->GetParent()) top
= top
->GetParent();
218 top
->RemoveExtraSpacing(true, true);
223 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
224 const wxString
& url
) const
226 // FIXME - normalize the URL to full path before passing to
231 wxHtmlOpeningStatus status
;
235 status
= m_Window
->OnOpeningURL(type
, myurl
, &redirect
);
236 if ( status
!= wxHTML_REDIRECT
)
242 if ( status
== wxHTML_BLOCK
)
245 return GetFS()->OpenFile(myurl
);
248 return wxHtmlParser::OpenURL(type
, url
);
251 void wxHtmlWinParser::AddText(const wxChar
* txt
)
259 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
261 if (lng
+1 > m_tmpStrBufSize
)
263 delete[] m_tmpStrBuf
;
264 m_tmpStrBuf
= new wxChar
[lng
+1];
265 m_tmpStrBufSize
= lng
+1;
267 wxChar
*temp
= m_tmpStrBuf
;
269 if (m_tmpLastWasSpace
)
272 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
273 (txt
[i
] == wxT('\t')))) i
++;
279 d
= temp
[templen
++] = txt
[i
];
280 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
283 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
284 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
290 temp
[templen
-1] = wxT(' ');
295 m_EncConv
->Convert(temp
);
297 size_t len
= wxStrlen(temp
);
298 for (size_t j
= 0; j
< len
; j
++)
301 c
= new wxHtmlWordCell(temp
, *(GetDC()));
304 m_Container
->InsertCell(c
);
305 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
306 m_lastWordCell
= (wxHtmlWordCell
*)c
;
307 m_tmpLastWasSpace
= TRUE
;
311 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
316 m_EncConv
->Convert(temp
);
318 size_t len
= wxStrlen(temp
);
319 for (size_t j
= 0; j
< len
; j
++)
322 c
= new wxHtmlWordCell(temp
, *(GetDC()));
325 m_Container
->InsertCell(c
);
326 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
327 m_lastWordCell
= (wxHtmlWordCell
*)c
;
328 m_tmpLastWasSpace
= FALSE
;
334 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
336 m_Container
= new wxHtmlContainerCell(m_Container
);
337 m_Container
->SetAlignHor(m_Align
);
338 m_tmpLastWasSpace
= TRUE
;
339 /* to avoid space being first character in paragraph */
345 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
347 m_tmpLastWasSpace
= TRUE
;
348 /* to avoid space being first character in paragraph */
349 return m_Container
= c
;
354 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
356 m_Container
= m_Container
->GetParent();
361 void wxHtmlWinParser::SetFontSize(int s
)
364 else if (s
> 7) s
= 7;
370 wxFont
* wxHtmlWinParser::CreateCurrentFont()
372 int fb
= GetFontBold(),
373 fi
= GetFontItalic(),
374 fu
= GetFontUnderlined(),
376 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
378 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
379 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
380 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
382 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
385 if (*fontptr
!= NULL
&& (*faceptr
!= face
387 || *encptr
!= m_OutputEnc
395 if (*fontptr
== NULL
)
398 *fontptr
= new wxFont(
399 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
400 ff
? wxMODERN
: wxSWISS
,
401 fi
? wxITALIC
: wxNORMAL
,
402 fb
? wxBOLD
: wxNORMAL
,
403 fu
? TRUE
: FALSE
, face
408 *encptr
= m_OutputEnc
;
411 m_DC
->SetFont(**fontptr
);
417 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
420 m_UseLink
= (link
.GetHref() != wxEmptyString
);
424 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
426 if (GetFontFixed()) m_FontFaceFixed
= face
;
427 else m_FontFaceNormal
= face
;
430 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
431 SetInputEncoding(m_InputEnc
);
438 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
440 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
447 if (enc
== wxFONTENCODING_DEFAULT
) return;
449 wxFontEncoding altfix
, altnorm
;
450 bool availfix
, availnorm
;
453 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
454 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
455 if (availnorm
&& availfix
)
459 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
460 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
462 m_OutputEnc
= altnorm
;
464 // at least normal face?
467 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
468 m_OutputEnc
= altnorm
;
470 // okay, let convert to ISO_8859-1, available always
472 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
475 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
476 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
478 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
480 if (m_InputEnc
== m_OutputEnc
) return;
482 m_EncConv
= new wxEncodingConverter();
483 if (!m_EncConv
->Init(m_InputEnc
,
484 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
485 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
486 wxCONVERT_SUBSTITUTE
))
487 { // total failture :-(
488 wxLogError(_("Failed to display HTML document in %s encoding"),
489 wxFontMapper::GetEncodingName(enc
).c_str());
490 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
500 //-----------------------------------------------------------------------------
501 // wxHtmlWinTagHandler
502 //-----------------------------------------------------------------------------
504 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
506 //-----------------------------------------------------------------------------
508 //-----------------------------------------------------------------------------
510 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
511 // This module is an ancestor for tag handlers modules defined
512 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
514 // Do not add any winpars.cpp shutdown or initialization code to it,
515 // create a new module instead!
517 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
519 bool wxHtmlTagsModule::OnInit()
521 wxHtmlWinParser::AddModule(this);
525 void wxHtmlTagsModule::OnExit()
527 wxHtmlWinParser::RemoveModule(this);