]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlfilt.cpp
Add wxActivateEvent::GetActivationReason().
[wxWidgets.git] / src / html / htmlfilt.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/html/htmlfilt.cpp
5526e819
VS
3// Purpose: wxHtmlFilter - input filter for translating into HTML format
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
65571936 6// Licence: wxWindows licence
5526e819
VS
7/////////////////////////////////////////////////////////////////////////////
8
4dcaf11a 9#include "wx/wxprec.h"
5526e819 10
2b5f62a0 11#ifdef __BORLANDC__
93763ad5 12 #pragma hdrstop
5526e819
VS
13#endif
14
93763ad5
WS
15#if wxUSE_HTML && wxUSE_STREAMS
16
b4f4d3dd 17#ifndef WX_PRECOMP
04dbb646
VZ
18 #include "wx/log.h"
19 #include "wx/intl.h"
5526e819
VS
20#endif
21
2b5f62a0 22#include "wx/strconv.h"
63482fd5 23#include "wx/sstream.h"
69941f05 24#include "wx/html/htmlfilt.h"
4dcaf11a 25#include "wx/html/htmlwin.h"
5526e819 26
63482fd5
VZ
27// utility function: read entire contents of an wxInputStream into a wxString
28//
29// TODO: error handling?
2b5f62a0 30static void ReadString(wxString& str, wxInputStream* s, wxMBConv& conv)
eb37e1d2 31{
63482fd5
VZ
32 wxStringOutputStream out(&str, conv);
33 s->Read(out);
eb37e1d2 34}
5526e819
VS
35
36/*
37
38There is code for several default filters:
39
40*/
41
42IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
43
44//--------------------------------------------------------------------------------
45// wxHtmlFilterPlainText
46// filter for text/plain or uknown
47//--------------------------------------------------------------------------------
48
49IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
50
a4c97004 51bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
5526e819 52{
d1da8872 53 return true;
5526e819
VS
54}
55
56
57
420ec58a 58wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
5526e819
VS
59{
60 wxInputStream *s = file.GetStream();
5526e819
VS
61 wxString doc, doc2;
62
63 if (s == NULL) return wxEmptyString;
2b5f62a0 64 ReadString(doc, s, wxConvISO8859_1);
5526e819 65
d1da8872
WS
66 doc.Replace(wxT("&"), wxT("&"), true);
67 doc.Replace(wxT("<"), wxT("&lt;"), true);
68 doc.Replace(wxT(">"), wxT("&gt;"), true);
2b5f62a0 69 doc2 = wxT("<HTML><BODY><PRE>\n") + doc + wxT("\n</PRE></BODY></HTML>");
5526e819
VS
70 return doc2;
71}
72
73
74
75
76
77//--------------------------------------------------------------------------------
78// wxHtmlFilterImage
79// filter for image/*
80//--------------------------------------------------------------------------------
81
82class wxHtmlFilterImage : public wxHtmlFilter
83{
84 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
85
86 public:
420ec58a
VS
87 virtual bool CanRead(const wxFSFile& file) const;
88 virtual wxString ReadFile(const wxFSFile& file) const;
5526e819
VS
89};
90
91IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
92
93
94
420ec58a 95bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
5526e819 96{
0413cec5 97 return (file.GetMimeType().Left(6) == wxT("image/"));
5526e819
VS
98}
99
100
101
420ec58a 102wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
5526e819 103{
2b5f62a0
VZ
104 wxString res = wxT("<HTML><BODY><IMG SRC=\"") + file.GetLocation() + wxT("\"></BODY></HTML>");
105 return res;
5526e819
VS
106}
107
108
109
110
111//--------------------------------------------------------------------------------
2b5f62a0
VZ
112// wxHtmlFilterHTML
113// filter for text/html
5526e819
VS
114//--------------------------------------------------------------------------------
115
5526e819
VS
116
117IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
118
420ec58a 119bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
5526e819 120{
f61815af
GL
121// return (file.GetMimeType() == "text/html");
122// This is true in most case but some page can return:
123// "text/html; char-encoding=...."
124// So we use Find instead
77611ad4 125 return (file.GetMimeType().Find(wxT("text/html")) == 0);
5526e819
VS
126}
127
128
129
420ec58a 130wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
5526e819
VS
131{
132 wxInputStream *s = file.GetStream();
5526e819
VS
133 wxString doc;
134
04dbb646 135 if (s == NULL)
f3c82859 136 {
f6bcfd97 137 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
f3c82859
VS
138 return wxEmptyString;
139 }
5526e819 140
d1da8872 141 // NB: We convert input file to wchar_t here in Unicode mode, based on
2b5f62a0
VZ
142 // either Content-Type header or <meta> tags. In ANSI mode, we don't
143 // do it as it is done by wxHtmlParser (for this reason, we add <meta>
144 // tag if we used Content-Type header).
145#if wxUSE_UNICODE
d1da8872 146 int charsetPos;
9a83f860 147 if ((charsetPos = file.GetMimeType().Find(wxT("; charset="))) != wxNOT_FOUND)
2b5f62a0
VZ
148 {
149 wxString charset = file.GetMimeType().Mid(charsetPos + 10);
150 wxCSConv conv(charset);
151 ReadString(doc, s, conv);
152 }
153 else
154 {
08f744fb 155 size_t size = s->GetSize();
935693c4 156 wxCharBuffer buf( size );
08f744fb 157 s->Read( buf.data(), size );
08f744fb 158 wxString tmpdoc( buf, wxConvISO8859_1);
03647350 159
2b5f62a0
VZ
160 wxString charset = wxHtmlParser::ExtractCharsetInformation(tmpdoc);
161 if (charset.empty())
162 doc = tmpdoc;
163 else
164 {
165 wxCSConv conv(charset);
08f744fb 166 doc = wxString( buf, conv );
2b5f62a0
VZ
167 }
168 }
169#else // !wxUSE_UNICODE
170 ReadString(doc, s, wxConvLibc);
04dbb646 171 // add meta tag if we obtained this through http:
2b5f62a0 172 if (!file.GetMimeType().empty())
981e62aa 173 {
2b5f62a0
VZ
174 wxString hdr;
175 wxString mime = file.GetMimeType();
9a83f860 176 hdr.Printf(wxT("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime.c_str());
2b5f62a0 177 return hdr+doc;
981e62aa 178 }
2b5f62a0 179#endif
981e62aa 180
3ca6a5f0 181 return doc;
5526e819
VS
182}
183
184
185
186
187///// Module:
188
189class wxHtmlFilterModule : public wxModule
190{
191 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
192
193 public:
194 virtual bool OnInit()
195 {
196 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
197 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
d1da8872 198 return true;
5526e819
VS
199 }
200 virtual void OnExit() {}
201};
202
203IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
204
8dd71e2b 205#endif