]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
(re)enable Cancel button in Resume()
[wxWidgets.git] / src / html / htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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
11 #ifdef __GNUG__
12 #pragma implementation "htmlfilt.h"
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/log.h"
26 #include "wx/intl.h"
27 #endif
28
29 #include "wx/html/htmlfilt.h"
30 #include "wx/html/htmlwin.h"
31
32 // utility function: read a wxString from a wxInputStream
33 void wxPrivate_ReadString(wxString& str, wxInputStream* s)
34 {
35 size_t streamSize = s->GetSize();
36
37 if(streamSize == ~(size_t)0)
38 {
39 const size_t bufSize = 4095;
40 char buffer[bufSize+1];
41 size_t lastRead;
42
43 do
44 {
45 s->Read(buffer, bufSize);
46 lastRead = s->LastRead();
47 buffer[lastRead] = 0;
48 str.Append(buffer);
49 }
50 while(lastRead == bufSize);
51 }
52 else
53 {
54 char* src = new char[streamSize+1];
55 s->Read(src, streamSize);
56 src[streamSize] = 0;
57 str = src;
58 delete [] src;
59 }
60 }
61
62 /*
63
64 There is code for several default filters:
65
66 */
67
68 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
69
70 //--------------------------------------------------------------------------------
71 // wxHtmlFilterPlainText
72 // filter for text/plain or uknown
73 //--------------------------------------------------------------------------------
74
75 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
76
77 bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
78 {
79 return TRUE;
80 }
81
82
83
84 wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
85 {
86 wxInputStream *s = file.GetStream();
87 wxString doc, doc2;
88
89 if (s == NULL) return wxEmptyString;
90 wxPrivate_ReadString(doc, s);
91
92 doc.Replace(wxT("&"), wxT("&"), TRUE);
93 doc.Replace(wxT("<"), wxT("&lt;"), TRUE);
94 doc.Replace(wxT(">"), wxT("&gt;"), TRUE);
95 doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>";
96 return doc2;
97 }
98
99
100
101
102
103 //--------------------------------------------------------------------------------
104 // wxHtmlFilterImage
105 // filter for image/*
106 //--------------------------------------------------------------------------------
107
108 class wxHtmlFilterImage : public wxHtmlFilter
109 {
110 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
111
112 public:
113 virtual bool CanRead(const wxFSFile& file) const;
114 virtual wxString ReadFile(const wxFSFile& file) const;
115 };
116
117 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
118
119
120
121 bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
122 {
123 return (file.GetMimeType().Left(6) == wxT("image/"));
124 }
125
126
127
128 wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
129 {
130 return ("<HTML><BODY><IMG SRC=\"" + file.GetLocation() + "\"></BODY></HTML>");
131 }
132
133
134
135
136 //--------------------------------------------------------------------------------
137 // wxHtmlFilterPlainText
138 // filter for text/plain or uknown
139 //--------------------------------------------------------------------------------
140
141 class wxHtmlFilterHTML : public wxHtmlFilter
142 {
143 DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML)
144
145 public:
146 virtual bool CanRead(const wxFSFile& file) const;
147 virtual wxString ReadFile(const wxFSFile& file) const;
148 };
149
150
151 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
152
153 bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
154 {
155 // return (file.GetMimeType() == "text/html");
156 // This is true in most case but some page can return:
157 // "text/html; char-encoding=...."
158 // So we use Find instead
159 return (file.GetMimeType().Find(wxT("text/html")) == 0);
160 }
161
162
163
164 wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
165 {
166 wxInputStream *s = file.GetStream();
167 wxString doc;
168
169 if (s == NULL)
170 {
171 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
172 return wxEmptyString;
173 }
174 wxPrivate_ReadString(doc, s);
175
176 // add meta tag if we obtained this through http:
177 if (file.GetMimeType().Find(_T("; charset=")) == 0)
178 {
179 wxString s(_T("<meta http-equiv=\"Content-Type\" content=\""));
180 s << file.GetMimeType() << _T("\">");
181 return s+doc;
182 }
183
184 return doc;
185 }
186
187
188
189
190 ///// Module:
191
192 class wxHtmlFilterModule : public wxModule
193 {
194 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
195
196 public:
197 virtual bool OnInit()
198 {
199 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
200 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
201 return TRUE;
202 }
203 virtual void OnExit() {}
204 };
205
206 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
207
208 #endif