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