]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlfilter.cpp
serbase.cpp added to the list of files
[wxWidgets.git] / src / html / htmlfilter.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: filter.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
10#ifdef __GNUG__
4dcaf11a 11#pragma implementation "htmlfilter.h"
5526e819
VS
12#endif
13
4dcaf11a 14#include "wx/wxprec.h"
5526e819 15
5526e819
VS
16#if wxUSE_HTML
17
18#ifdef __BORDLANDC__
19#pragma hdrstop
20#endif
21
22#ifndef WXPRECOMP
4dcaf11a 23#include "wx/wx.h"
5526e819
VS
24#endif
25
4dcaf11a
RR
26#include "wx/html/htmlfilter.h"
27#include "wx/html/htmlwin.h"
5526e819
VS
28
29
30/*
31
32There is code for several default filters:
33
34*/
35
36IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
37
38//--------------------------------------------------------------------------------
39// wxHtmlFilterPlainText
40// filter for text/plain or uknown
41//--------------------------------------------------------------------------------
42
43IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
44
420ec58a 45bool wxHtmlFilterPlainText::CanRead(const wxFSFile& file) const
5526e819
VS
46{
47 return TRUE;
48}
49
50
51
420ec58a 52wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
5526e819
VS
53{
54 wxInputStream *s = file.GetStream();
55 char *src;
56 wxString doc, doc2;
57
58 if (s == NULL) return wxEmptyString;
259d1674
VZ
59 src = new char[s -> GetSize()+1];
60 src[s -> GetSize()] = 0;
61 s -> Read(src, s -> GetSize());
5526e819 62 doc = src;
8dd71e2b 63 delete [] src;
5526e819
VS
64
65 doc.Replace("<", "&lt;", TRUE);
66 doc.Replace(">", "&gt;", TRUE);
67 doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>";
68 return doc2;
69}
70
71
72
73
74
75//--------------------------------------------------------------------------------
76// wxHtmlFilterImage
77// filter for image/*
78//--------------------------------------------------------------------------------
79
80class wxHtmlFilterImage : public wxHtmlFilter
81{
82 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
83
84 public:
420ec58a
VS
85 virtual bool CanRead(const wxFSFile& file) const;
86 virtual wxString ReadFile(const wxFSFile& file) const;
5526e819
VS
87};
88
89IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
90
91
92
420ec58a 93bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
5526e819
VS
94{
95 return (file.GetMimeType().Left(6) == "image/");
96}
97
98
99
420ec58a 100wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
5526e819
VS
101{
102 return ("<HTML><BODY><IMG SRC=\"" + file.GetLocation() + "\"></BODY></HTML>");
103}
104
105
106
107
108//--------------------------------------------------------------------------------
109// wxHtmlFilterPlainText
110// filter for text/plain or uknown
111//--------------------------------------------------------------------------------
112
113class wxHtmlFilterHTML : public wxHtmlFilter
114{
115 DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML)
116
117 public:
420ec58a
VS
118 virtual bool CanRead(const wxFSFile& file) const;
119 virtual wxString ReadFile(const wxFSFile& file) const;
5526e819
VS
120};
121
122
123IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
124
420ec58a 125bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
5526e819 126{
f61815af
GL
127// return (file.GetMimeType() == "text/html");
128// This is true in most case but some page can return:
129// "text/html; char-encoding=...."
130// So we use Find instead
131 return (file.GetMimeType().Find(_T("text/html")) == 0);
5526e819
VS
132}
133
134
135
420ec58a 136wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
5526e819
VS
137{
138 wxInputStream *s = file.GetStream();
139 char *src;
140 wxString doc;
141
142 if (s == NULL) return wxEmptyString;
2776d7c3 143 src = new char[s -> GetSize() + 1];
259d1674
VZ
144 src[s -> GetSize()] = 0;
145 s -> Read(src, s -> GetSize());
5526e819 146 doc = src;
2776d7c3 147 delete[] src;
5526e819
VS
148
149 return doc;
150}
151
152
153
154
155///// Module:
156
157class wxHtmlFilterModule : public wxModule
158{
159 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
160
161 public:
162 virtual bool OnInit()
163 {
164 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
165 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
166 return TRUE;
167 }
168 virtual void OnExit() {}
169};
170
171IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
172
8dd71e2b 173#endif