]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
Make mouse capture checking asserts stronger and more detailed.
[wxWidgets.git] / src / html / htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmlfilt.cpp
3 // Purpose: wxHtmlFilter - input filter for translating into HTML format
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #include "wx/wxprec.h"
10
11 #ifdef __BORLANDC__
12 #pragma hdrstop
13 #endif
14
15 #if wxUSE_HTML && wxUSE_STREAMS
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/intl.h"
20 #endif
21
22 #include "wx/strconv.h"
23 #include "wx/sstream.h"
24 #include "wx/html/htmlfilt.h"
25 #include "wx/html/htmlwin.h"
26
27 // utility function: read entire contents of an wxInputStream into a wxString
28 //
29 // TODO: error handling?
30 static void ReadString(wxString& str, wxInputStream* s, wxMBConv& conv)
31 {
32 wxStringOutputStream out(&str, conv);
33 s->Read(out);
34 }
35
36 /*
37
38 There is code for several default filters:
39
40 */
41
42 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
43
44 //--------------------------------------------------------------------------------
45 // wxHtmlFilterPlainText
46 // filter for text/plain or uknown
47 //--------------------------------------------------------------------------------
48
49 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
50
51 bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
52 {
53 return true;
54 }
55
56
57
58 wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
59 {
60 wxInputStream *s = file.GetStream();
61 wxString doc, doc2;
62
63 if (s == NULL) return wxEmptyString;
64 ReadString(doc, s, wxConvISO8859_1);
65
66 doc.Replace(wxT("&"), wxT("&"), true);
67 doc.Replace(wxT("<"), wxT("&lt;"), true);
68 doc.Replace(wxT(">"), wxT("&gt;"), true);
69 doc2 = wxT("<HTML><BODY><PRE>\n") + doc + wxT("\n</PRE></BODY></HTML>");
70 return doc2;
71 }
72
73
74
75
76
77 //--------------------------------------------------------------------------------
78 // wxHtmlFilterImage
79 // filter for image/*
80 //--------------------------------------------------------------------------------
81
82 class wxHtmlFilterImage : public wxHtmlFilter
83 {
84 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
85
86 public:
87 virtual bool CanRead(const wxFSFile& file) const;
88 virtual wxString ReadFile(const wxFSFile& file) const;
89 };
90
91 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
92
93
94
95 bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
96 {
97 return (file.GetMimeType().Left(6) == wxT("image/"));
98 }
99
100
101
102 wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
103 {
104 wxString res = wxT("<HTML><BODY><IMG SRC=\"") + file.GetLocation() + wxT("\"></BODY></HTML>");
105 return res;
106 }
107
108
109
110
111 //--------------------------------------------------------------------------------
112 // wxHtmlFilterHTML
113 // filter for text/html
114 //--------------------------------------------------------------------------------
115
116
117 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
118
119 bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
120 {
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
125 return (file.GetMimeType().Find(wxT("text/html")) == 0);
126 }
127
128
129
130 wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
131 {
132 wxInputStream *s = file.GetStream();
133 wxString doc;
134
135 if (s == NULL)
136 {
137 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
138 return wxEmptyString;
139 }
140
141 // NB: We convert input file to wchar_t here in Unicode mode, based on
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
146 int charsetPos;
147 if ((charsetPos = file.GetMimeType().Find(wxT("; charset="))) != wxNOT_FOUND)
148 {
149 wxString charset = file.GetMimeType().Mid(charsetPos + 10);
150 wxCSConv conv(charset);
151 ReadString(doc, s, conv);
152 }
153 else
154 {
155 size_t size = s->GetSize();
156 wxCharBuffer buf( size );
157 s->Read( buf.data(), size );
158 wxString tmpdoc( buf, wxConvISO8859_1);
159
160 wxString charset = wxHtmlParser::ExtractCharsetInformation(tmpdoc);
161 if (charset.empty())
162 doc = tmpdoc;
163 else
164 {
165 wxCSConv conv(charset);
166 doc = wxString( buf, conv );
167 }
168 }
169 #else // !wxUSE_UNICODE
170 ReadString(doc, s, wxConvLibc);
171 // add meta tag if we obtained this through http:
172 if (!file.GetMimeType().empty())
173 {
174 wxString hdr;
175 wxString mime = file.GetMimeType();
176 hdr.Printf(wxT("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime.c_str());
177 return hdr+doc;
178 }
179 #endif
180
181 return doc;
182 }
183
184
185
186
187 ///// Module:
188
189 class 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);
198 return true;
199 }
200 virtual void OnExit() {}
201 };
202
203 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
204
205 #endif