]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilter.cpp
wxHTML compilation fixes
[wxWidgets.git] / src / html / htmlfilter.cpp
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
12 #endif
13
14 #include <wx/wxprec.h>
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML
18
19 #ifdef __BORDLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WXPRECOMP
24 #include <wx/wx.h>
25 #endif
26
27 #include <wx/html/htmlfilter.h>
28 #include <wx/html/htmlwin.h>
29
30
31 /*
32
33 There is code for several default filters:
34
35 */
36
37 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
38
39 //--------------------------------------------------------------------------------
40 // wxHtmlFilterPlainText
41 // filter for text/plain or uknown
42 //--------------------------------------------------------------------------------
43
44 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
45
46 bool wxHtmlFilterPlainText::CanRead(const wxFSFile& file)
47 {
48 return TRUE;
49 }
50
51
52
53 wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file)
54 {
55 wxInputStream *s = file.GetStream();
56 char *src;
57 wxString doc, doc2;
58
59 if (s == NULL) return wxEmptyString;
60 src = new char[s -> GetSize()+1];
61 src[s -> GetSize()] = 0;
62 s -> Read(src, s -> GetSize());
63 doc = src;
64 delete [] src;
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
81 class wxHtmlFilterImage : public wxHtmlFilter
82 {
83 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
84
85 public:
86 virtual bool CanRead(const wxFSFile& file);
87 virtual wxString ReadFile(const wxFSFile& file);
88 };
89
90 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
91
92
93
94 bool wxHtmlFilterImage::CanRead(const wxFSFile& file)
95 {
96 return (file.GetMimeType().Left(6) == "image/");
97 }
98
99
100
101 wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file)
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
114 class wxHtmlFilterHTML : public wxHtmlFilter
115 {
116 DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML)
117
118 public:
119 virtual bool CanRead(const wxFSFile& file);
120 virtual wxString ReadFile(const wxFSFile& file);
121 };
122
123
124 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
125
126 bool wxHtmlFilterHTML::CanRead(const wxFSFile& file)
127 {
128 return (file.GetMimeType() == "text/html");
129 }
130
131
132
133 wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file)
134 {
135 wxInputStream *s = file.GetStream();
136 char *src;
137 wxString doc;
138
139 if (s == NULL) return wxEmptyString;
140 src = (char*) malloc(s -> GetSize() + 1);
141 src[s -> GetSize()] = 0;
142 s -> Read(src, s -> GetSize());
143 doc = src;
144 free(src);
145
146 return doc;
147 }
148
149
150
151
152 ///// Module:
153
154 class wxHtmlFilterModule : public wxModule
155 {
156 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
157
158 public:
159 virtual bool OnInit()
160 {
161 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
162 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
163 return TRUE;
164 }
165 virtual void OnExit() {}
166 };
167
168 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
169
170 #endif