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
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;
53 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
57 for (i
= 0; i
< 2; i
++)
58 for (j
= 0; j
< 2; j
++)
59 for (k
= 0; k
< 2; k
++)
60 for (l
= 0; l
< 2; l
++)
61 for (m
= 0; m
< 7; m
++)
63 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
64 m_FontsFacesTable
[i
][j
][k
][l
][m
] = wxEmptyString
;
65 m_FontsEncTable
[i
][j
][k
][l
][m
] = wxFONTENCODING_DEFAULT
;
68 static int default_sizes
[7] = {7, 8, 10, 12, 16, 22, 30};
69 #elif defined(__WXMAC__)
70 static int default_sizes
[7] = {9, 12, 14, 18, 24, 30, 36};
72 static int default_sizes
[7] = {10, 12, 14, 16, 19, 24, 32};
74 SetFonts("", "", default_sizes
);
77 // fill in wxHtmlParser's tables:
78 wxNode
*node
= m_Modules
.GetFirst();
81 wxHtmlTagsModule
*mod
= (wxHtmlTagsModule
*) node
->GetData();
82 mod
->FillHandlersTable(this);
83 node
= node
->GetNext();
87 wxHtmlWinParser::~wxHtmlWinParser()
91 for (i
= 0; i
< 2; i
++)
92 for (j
= 0; j
< 2; j
++)
93 for (k
= 0; k
< 2; k
++)
94 for (l
= 0; l
< 2; l
++)
95 for (m
= 0; m
< 7; m
++)
97 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
98 delete m_FontsTable
[i
][j
][k
][l
][m
];
101 delete[] m_tmpStrBuf
;
104 void wxHtmlWinParser::AddModule(wxHtmlTagsModule
*module)
106 m_Modules
.Append(module);
109 void wxHtmlWinParser::RemoveModule(wxHtmlTagsModule
*module)
111 m_Modules
.DeleteObject(module);
114 void wxHtmlWinParser::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
118 for (i
= 0; i
< 7; i
++) m_FontsSizes
[i
] = sizes
[i
];
119 m_FontFaceFixed
= fixed_face
;
120 m_FontFaceNormal
= normal_face
;
122 SetInputEncoding(m_InputEnc
);
124 for (i
= 0; i
< 2; i
++)
125 for (j
= 0; j
< 2; j
++)
126 for (k
= 0; k
< 2; k
++)
127 for (l
= 0; l
< 2; l
++)
128 for (m
= 0; m
< 7; m
++) {
129 if (m_FontsTable
[i
][j
][k
][l
][m
] != NULL
)
131 delete m_FontsTable
[i
][j
][k
][l
][m
];
132 m_FontsTable
[i
][j
][k
][l
][m
] = NULL
;
137 void wxHtmlWinParser::InitParser(const wxString
& source
)
139 wxHtmlParser::InitParser(source
);
140 wxASSERT_MSG(m_DC
!= NULL
, _T("no DC assigned to wxHtmlWinParser!!"));
142 m_FontBold
= m_FontItalic
= m_FontUnderlined
= m_FontFixed
= FALSE
;
143 m_FontSize
= 3; //default one
144 CreateCurrentFont(); // we're selecting default font into
145 m_DC
->GetTextExtent("H", &m_CharWidth
, &m_CharHeight
);
146 /* NOTE : we're not using GetCharWidth/Height() because
147 of differences under X and win
151 m_Link
= wxHtmlLinkInfo("", "");
152 m_LinkColor
.Set(0, 0, 0xFF);
153 m_ActualColor
.Set(0, 0, 0);
154 m_Align
= wxHTML_ALIGN_LEFT
;
155 m_tmpLastWasSpace
= FALSE
;
160 m_Container
->InsertCell(new wxHtmlColourCell(m_ActualColor
));
161 m_Container
->InsertCell(new wxHtmlFontCell(CreateCurrentFont()));
164 void wxHtmlWinParser::DoneParser()
167 SetInputEncoding(wxFONTENCODING_DEFAULT
); // for next call
168 wxHtmlParser::DoneParser();
171 wxObject
* wxHtmlWinParser::GetProduct()
173 wxHtmlContainerCell
*top
;
179 while (top
->GetParent()) top
= top
->GetParent();
183 wxFSFile
*wxHtmlWinParser::OpenURL(wxHtmlURLType type
,
184 const wxString
& url
) const
186 // FIXME - normalize the URL to full path before passing to
191 wxHtmlOpeningStatus status
;
195 status
= m_Window
->OnOpeningURL(type
, myurl
, &redirect
);
196 if ( status
!= wxHTML_REDIRECT
)
202 if ( status
== wxHTML_BLOCK
)
205 return GetFS()->OpenFile(myurl
);
208 return wxHtmlParser::OpenURL(type
, url
);
211 void wxHtmlWinParser::AddText(const wxChar
* txt
)
219 wxChar nbsp
= GetEntitiesParser()->GetCharForCode(160 /* nbsp */);
221 if (lng
+1 > m_tmpStrBufSize
)
223 delete[] m_tmpStrBuf
;
224 m_tmpStrBuf
= new wxChar
[lng
+1];
225 m_tmpStrBufSize
= lng
+1;
227 wxChar
*temp
= m_tmpStrBuf
;
229 if (m_tmpLastWasSpace
)
232 ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) || (txt
[i
] == wxT(' ')) ||
233 (txt
[i
] == wxT('\t')))) i
++;
239 d
= temp
[templen
++] = txt
[i
];
240 if ((d
== wxT('\n')) || (d
== wxT('\r')) || (d
== wxT(' ')) || (d
== wxT('\t')))
243 while ((i
< lng
) && ((txt
[i
] == wxT('\n')) || (txt
[i
] == wxT('\r')) ||
244 (txt
[i
] == wxT(' ')) || (txt
[i
] == wxT('\t')))) i
++, x
++;
250 temp
[templen
-1] = wxT(' ');
252 if (templen
== 1) continue;
255 m_EncConv
->Convert(temp
);
256 size_t len
= wxStrlen(temp
);
257 for (size_t j
= 0; j
< len
; j
++)
260 c
= new wxHtmlWordCell(temp
, *(GetDC()));
263 m_Container
->InsertCell(c
);
264 m_tmpLastWasSpace
= TRUE
;
268 if (templen
&& (templen
> 1 || temp
[0] != wxT(' ')))
272 m_EncConv
->Convert(temp
);
273 size_t len
= wxStrlen(temp
);
274 for (size_t j
= 0; j
< len
; j
++)
277 c
= new wxHtmlWordCell(temp
, *(GetDC()));
280 m_Container
->InsertCell(c
);
281 m_tmpLastWasSpace
= FALSE
;
287 wxHtmlContainerCell
* wxHtmlWinParser::OpenContainer()
289 m_Container
= new wxHtmlContainerCell(m_Container
);
290 m_Container
->SetAlignHor(m_Align
);
291 m_tmpLastWasSpace
= TRUE
;
292 /* to avoid space being first character in paragraph */
298 wxHtmlContainerCell
* wxHtmlWinParser::SetContainer(wxHtmlContainerCell
*c
)
300 m_tmpLastWasSpace
= TRUE
;
301 /* to avoid space being first character in paragraph */
302 return m_Container
= c
;
307 wxHtmlContainerCell
* wxHtmlWinParser::CloseContainer()
309 m_Container
= m_Container
->GetParent();
314 void wxHtmlWinParser::SetFontSize(int s
)
317 else if (s
> 7) s
= 7;
323 wxFont
* wxHtmlWinParser::CreateCurrentFont()
325 int fb
= GetFontBold(),
326 fi
= GetFontItalic(),
327 fu
= GetFontUnderlined(),
329 fs
= GetFontSize() - 1 /*remap from <1;7> to <0;6>*/ ;
331 wxString face
= ff
? m_FontFaceFixed
: m_FontFaceNormal
;
332 wxString
*faceptr
= &(m_FontsFacesTable
[fb
][fi
][fu
][ff
][fs
]);
333 wxFont
**fontptr
= &(m_FontsTable
[fb
][fi
][fu
][ff
][fs
]);
334 wxFontEncoding
*encptr
= &(m_FontsEncTable
[fb
][fi
][fu
][ff
][fs
]);
336 if (*fontptr
!= NULL
&& (*faceptr
!= face
|| *encptr
!= m_OutputEnc
))
342 if (*fontptr
== NULL
)
345 *encptr
= m_OutputEnc
;
346 *fontptr
= new wxFont(
347 (int) (m_FontsSizes
[fs
] * m_PixelScale
),
348 ff
? wxMODERN
: wxSWISS
,
349 fi
? wxITALIC
: wxNORMAL
,
350 fb
? wxBOLD
: wxNORMAL
,
351 fu
? TRUE
: FALSE
, face
,
354 m_DC
->SetFont(**fontptr
);
360 void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo
& link
)
363 m_UseLink
= (link
.GetHref() != wxEmptyString
);
367 void wxHtmlWinParser::SetFontFace(const wxString
& face
)
369 if (GetFontFixed()) m_FontFaceFixed
= face
;
370 else m_FontFaceNormal
= face
;
372 if (m_InputEnc
!= wxFONTENCODING_DEFAULT
)
373 SetInputEncoding(m_InputEnc
);
378 void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc
)
380 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
387 if (enc
== wxFONTENCODING_DEFAULT
) return;
389 wxFontEncoding altfix
, altnorm
;
390 bool availfix
, availnorm
;
393 availnorm
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceNormal
);
394 availfix
= wxTheFontMapper
->IsEncodingAvailable(enc
, m_FontFaceFixed
);
395 if (availnorm
&& availfix
)
399 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
) &&
400 wxTheFontMapper
->GetAltForEncoding(enc
, &altfix
, m_FontFaceFixed
, FALSE
) &&
402 m_OutputEnc
= altnorm
;
404 // at least normal face?
407 else if (wxTheFontMapper
->GetAltForEncoding(enc
, &altnorm
, m_FontFaceNormal
, FALSE
))
408 m_OutputEnc
= altnorm
;
410 // okay, let convert to ISO_8859-1, available always
412 m_OutputEnc
= wxFONTENCODING_DEFAULT
;
415 if (m_OutputEnc
== wxFONTENCODING_DEFAULT
)
416 GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM
);
418 GetEntitiesParser()->SetEncoding(m_OutputEnc
);
420 if (m_InputEnc
== m_OutputEnc
) return;
422 m_EncConv
= new wxEncodingConverter();
423 if (!m_EncConv
->Init(m_InputEnc
,
424 (m_OutputEnc
== wxFONTENCODING_DEFAULT
) ?
425 wxFONTENCODING_ISO8859_1
: m_OutputEnc
,
426 wxCONVERT_SUBSTITUTE
))
427 { // total failture :-(
428 wxLogError(_("Failed to display HTML document in %s encoding"),
429 wxFontMapper::GetEncodingName(enc
).c_str());
430 m_InputEnc
= m_OutputEnc
= wxFONTENCODING_DEFAULT
;
440 //-----------------------------------------------------------------------------
441 // wxHtmlWinTagHandler
442 //-----------------------------------------------------------------------------
444 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWinTagHandler
, wxHtmlTagHandler
)
446 //-----------------------------------------------------------------------------
448 //-----------------------------------------------------------------------------
450 // NB: This is *NOT* winpars.cpp's initialization and shutdown code!!
451 // This module is an ancestor for tag handlers modules defined
452 // in m_*.cpp files with TAGS_MODULE_BEGIN...TAGS_MODULE_END construct.
454 // Do not add any winpars.cpp shutdown or initialization code to it,
455 // create a new module instead!
457 IMPLEMENT_DYNAMIC_CLASS(wxHtmlTagsModule
, wxModule
)
459 bool wxHtmlTagsModule::OnInit()
461 wxHtmlWinParser::AddModule(this);
465 void wxHtmlTagsModule::OnExit()
467 wxHtmlWinParser::RemoveModule(this);