]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlfilt.cpp
fingers crossed..
[wxWidgets.git] / src / html / htmlfilt.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
69941f05 2// Name: htmlfilt.cpp
5526e819
VS
3// Purpose: wxHtmlFilter - input filter for translating into HTML format
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819
VS
6// Copyright: (c) 1999 Vaclav Slavik
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
69941f05 12#pragma implementation
5526e819
VS
13#endif
14
4dcaf11a 15#include "wx/wxprec.h"
5526e819 16
314260fb 17#include "wx/defs.h"
f6bcfd97 18#if wxUSE_HTML && wxUSE_STREAMS
5526e819
VS
19
20#ifdef __BORDLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WXPRECOMP
4dcaf11a 25#include "wx/wx.h"
5526e819
VS
26#endif
27
69941f05 28#include "wx/html/htmlfilt.h"
4dcaf11a 29#include "wx/html/htmlwin.h"
5526e819
VS
30
31
32/*
33
34There is code for several default filters:
35
36*/
37
38IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
39
40//--------------------------------------------------------------------------------
41// wxHtmlFilterPlainText
42// filter for text/plain or uknown
43//--------------------------------------------------------------------------------
44
45IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
46
a4c97004 47bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
5526e819
VS
48{
49 return TRUE;
50}
51
52
53
420ec58a 54wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
5526e819
VS
55{
56 wxInputStream *s = file.GetStream();
57 char *src;
58 wxString doc, doc2;
59
60 if (s == NULL) return wxEmptyString;
4f9297b0
VS
61 src = new char[s->GetSize()+1];
62 src[s->GetSize()] = 0;
63 s->Read(src, s->GetSize());
5526e819 64 doc = src;
8dd71e2b 65 delete [] src;
5526e819 66
66a77a74
OK
67 doc.Replace(wxT("<"), wxT("&lt;"), TRUE);
68 doc.Replace(wxT(">"), wxT("&gt;"), TRUE);
5526e819
VS
69 doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>";
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
VS
103{
104 return ("<HTML><BODY><IMG SRC=\"" + file.GetLocation() + "\"></BODY></HTML>");
105}
106
107
108
109
110//--------------------------------------------------------------------------------
111// wxHtmlFilterPlainText
112// filter for text/plain or uknown
113//--------------------------------------------------------------------------------
114
115class wxHtmlFilterHTML : public wxHtmlFilter
116{
117 DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML)
118
119 public:
420ec58a
VS
120 virtual bool CanRead(const wxFSFile& file) const;
121 virtual wxString ReadFile(const wxFSFile& file) const;
5526e819
VS
122};
123
124
125IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
126
420ec58a 127bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
5526e819 128{
f61815af
GL
129// return (file.GetMimeType() == "text/html");
130// This is true in most case but some page can return:
131// "text/html; char-encoding=...."
132// So we use Find instead
77611ad4 133 return (file.GetMimeType().Find(wxT("text/html")) == 0);
5526e819
VS
134}
135
136
137
420ec58a 138wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
5526e819
VS
139{
140 wxInputStream *s = file.GetStream();
141 char *src;
142 wxString doc;
143
f3c82859
VS
144 if (s == NULL)
145 {
f6bcfd97 146 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
f3c82859
VS
147 return wxEmptyString;
148 }
4f9297b0
VS
149 src = new char[s->GetSize() + 1];
150 src[s->GetSize()] = 0;
151 s->Read(src, s->GetSize());
5526e819 152 doc = src;
2776d7c3 153 delete[] src;
5526e819 154
981e62aa
VS
155 // add meta tag if we obtained this through http:
156 if (file.GetMimeType().Find(_T("; charset=")) == 0)
157 {
158 wxString s(_T("<meta http-equiv=\"Content-Type\" content=\""));
159 s << file.GetMimeType() << _T("\">");
160 return s;
161 }
162
3ca6a5f0 163 return doc;
5526e819
VS
164}
165
166
167
168
169///// Module:
170
171class wxHtmlFilterModule : public wxModule
172{
173 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
174
175 public:
176 virtual bool OnInit()
177 {
178 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
179 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
180 return TRUE;
181 }
182 virtual void OnExit() {}
183};
184
185IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
186
8dd71e2b 187#endif