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