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 m_Container
->InsertCell(
192 new wxHtmlColourCell(GetWindow() ?
193 GetWindow()->GetBackgroundColour() :
194 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
),
195 wxHTML_CLR_BACKGROUND
));
196 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
199 void wxHtmlWinParser::DoneParser()
203 SetInputEncoding(wxFONTENCODING_ISO8859_1
); // for next call
205 wxHtmlParser::DoneParser();
208 wxObject
* wxHtmlWinParser::GetProduct()
210 wxHtmlContainerCell
*top
;
216 while (top
->GetParent()) top
= top
->GetParent();
217 top
->RemoveExtraSpacing(true, true);
222 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
223 const wxString
& url
) const
225 // FIXME - normalize the URL to full path before passing to
230 wxHtmlOpeningStatus status
;
234 status
= m_Window
->OnOpeningURL(type
, myurl
, &redirect
);
235 if ( status
!= wxHTML_REDIRECT
)
241 if ( status
== wxHTML_BLOCK
)
244 return GetFS()->OpenFile(myurl
);
247 return wxHtmlParser::OpenURL(type
, url
);
250 void wxHtmlWinParser::AddText(const wxChar
* txt
)
258 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
260 if (lng
+1 > m_tmpStrBufSize
)
262 delete[] m_tmpStrBuf
;
263 m_tmpStrBuf
= new wxChar
[lng
+1];
264 m_tmpStrBufSize
= lng
+1;
266 wxChar
*temp
= m_tmpStrBuf
;
268 if (m_tmpLastWasSpace
)
271 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
272 (txt
[i
] == wxT('\t')))) i
++;
278 d
= temp
[templen
++] = txt
[i
];
279 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
282 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
283 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
289 temp
[templen
-1] = wxT(' ');
294 m_EncConv
->Convert(temp
);
296 size_t len
= wxStrlen(temp
);
297 for (size_t j
= 0; j
< len
; j
++)
300 c
= new wxHtmlWordCell(temp
, *(GetDC()));
303 m_Container
->InsertCell(c
);
304 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
305 m_lastWordCell
= (wxHtmlWordCell
*)c
;
306 m_tmpLastWasSpace
= TRUE
;
310 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
315 m_EncConv
->Convert(temp
);
317 size_t len
= wxStrlen(temp
);
318 for (size_t j
= 0; j
< len
; j
++)
321 c
= new wxHtmlWordCell(temp
, *(GetDC()));
324 m_Container
->InsertCell(c
);
325 ((wxHtmlWordCell
*)c
)->SetPreviousWord(m_lastWordCell
);
326 m_lastWordCell
= (wxHtmlWordCell
*)c
;
327 m_tmpLastWasSpace
= FALSE
;
333 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
335 m_Container
= new wxHtmlContainerCell(m_Container
);
336 m_Container
->SetAlignHor(m_Align
);
337 m_tmpLastWasSpace
= TRUE
;
338 /* to avoid space being first character in paragraph */
344 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
346 m_tmpLastWasSpace
= TRUE
;
347 /* to avoid space being first character in paragraph */
348 return m_Container
= c
;
353 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
355 m_Container
= m_Container
->GetParent();
360 void wxHtmlWinParser::SetFontSize(int s
)
363 else if (s
> 7) s
= 7;
369 wxFont
* wxHtmlWinParser::CreateCurrentFont()
371 int fb
= GetFontBold(),
372 fi
= GetFontItalic(),
373 fu
= GetFontUnderlined(),
375 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
377 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
378 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
379 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
381 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
384 if (*fontptr
!= NULL
&& (*faceptr
!= face
386 || *encptr
!= m_OutputEnc
394 if (*fontptr
== NULL
)
397 *fontptr
= new wxFont(
398 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
399 ff
? wxMODERN
: wxSWISS
,
400 fi
? wxITALIC
: wxNORMAL
,
401 fb
? wxBOLD
: wxNORMAL
,
402 fu
? TRUE
: FALSE
, face
407 *encptr
= m_OutputEnc
;
410 m_DC
->SetFont(**fontptr
);
416 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
419 m_UseLink
= (link
.GetHref() != wxEmptyString
);
423 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
425 if (GetFontFixed()) m_FontFaceFixed
= face
;
426 else m_FontFaceNormal
= face
;
429 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
430 SetInputEncoding(m_InputEnc
);
437 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
439 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
446 if (enc
== wxFONTENCODING_DEFAULT
) return;
448 wxFontEncoding altfix
, altnorm
;
449 bool availfix
, availnorm
;
452 availnorm
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceNormal
);
453 availfix
= wxFontMapper::Get()->IsEncodingAvailable(enc
, m_FontFaceFixed
);
454 if (availnorm
&& availfix
)
458 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
459 wxFontMapper::Get()->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
461 m_OutputEnc
= altnorm
;
463 // at least normal face?
466 else if (wxFontMapper::Get()->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
467 m_OutputEnc
= altnorm
;
469 // okay, let convert to ISO_8859-1, available always
471 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
474 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
475 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
477 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
479 if (m_InputEnc
== m_OutputEnc
) return;
481 m_EncConv
= new wxEncodingConverter();
482 if (!m_EncConv
->Init(m_InputEnc
,
483 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
484 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
485 wxCONVERT_SUBSTITUTE
))
486 { // total failture :-(
487 wxLogError(_("Failed to display HTML document in %s encoding"),
488 wxFontMapper::GetEncodingName(enc
).c_str());
489 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
499 //-----------------------------------------------------------------------------
500 // wxHtmlWinTagHandler
501 //-----------------------------------------------------------------------------
503 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
505 //-----------------------------------------------------------------------------
507 //-----------------------------------------------------------------------------
509 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
510 // This module is an ancestor for tag handlers modules defined
511 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
513 // Do not add any winpars.cpp shutdown or initialization code to it,
514 // create a new module instead!
516 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
518 bool wxHtmlTagsModule::OnInit()
520 wxHtmlWinParser::AddModule(this);
524 void wxHtmlTagsModule::OnExit()
526 wxHtmlWinParser::RemoveModule(this);