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