]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
639ea7272a82de5397777d4bdaede4b39a531e83
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxHtmlFilter - input filter for translating into HTML format 
   4 // Author:      Vaclav Slavik 
   6 // Copyright:   (c) 1999 Vaclav Slavik 
   7 // Licence:     wxWindows Licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  12 #pragma implementation 
  15 #include "wx/wxprec.h" 
  18 #if wxUSE_HTML && wxUSE_STREAMS 
  28 #include "wx/html/htmlfilt.h" 
  29 #include "wx/html/htmlwin.h" 
  34 There is code for several default filters: 
  38 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
) 
  40 //-------------------------------------------------------------------------------- 
  41 // wxHtmlFilterPlainText 
  42 //          filter for text/plain or uknown 
  43 //-------------------------------------------------------------------------------- 
  45 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
) 
  47 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const 
  54 wxString 
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const 
  56     wxInputStream 
*s 
= file
.GetStream(); 
  60     if (s 
== NULL
) return wxEmptyString
; 
  61     src 
= new char[s 
-> GetSize()+1]; 
  62     src
[s 
-> GetSize()] = 0; 
  63     s 
-> Read(src
, s 
-> GetSize()); 
  67     doc
.Replace(wxT("<"), wxT("<"), TRUE
); 
  68     doc
.Replace(wxT(">"), wxT(">"), TRUE
); 
  69     doc2 
= "<HTML><BODY><PRE>\n" + doc 
+ "\n</PRE></BODY></HTML>"; 
  77 //-------------------------------------------------------------------------------- 
  80 //-------------------------------------------------------------------------------- 
  82 class wxHtmlFilterImage 
: public wxHtmlFilter
 
  84     DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
) 
  87         virtual bool CanRead(const wxFSFile
& file
) const; 
  88         virtual wxString 
ReadFile(const wxFSFile
& file
) const; 
  91 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
) 
  95 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const 
  97     return (file
.GetMimeType().Left(6) == wxT("image/")); 
 102 wxString 
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const 
 104     return ("<HTML><BODY><IMG SRC=\"" + file
.GetLocation() + "\"></BODY></HTML>"); 
 110 //-------------------------------------------------------------------------------- 
 111 // wxHtmlFilterPlainText 
 112 //          filter for text/plain or uknown 
 113 //-------------------------------------------------------------------------------- 
 115 class wxHtmlFilterHTML 
: public wxHtmlFilter
 
 117     DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML
) 
 120         virtual bool CanRead(const wxFSFile
& file
) const; 
 121         virtual wxString 
ReadFile(const wxFSFile
& file
) const; 
 125 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
) 
 127 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const 
 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 
 133   return (file
.GetMimeType().Find(wxT("text/html")) == 0); 
 138 wxString 
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const 
 140     wxInputStream 
*s 
= file
.GetStream(); 
 146         wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str()); 
 147         return wxEmptyString
; 
 149     src 
= new char[s 
-> GetSize() + 1]; 
 150     src
[s 
-> GetSize()] = 0; 
 151     s 
-> Read(src
, s 
-> GetSize()); 
 155     // add meta tag if we obtained this through http:     
 156     if (file
.GetMimeType().Find(_T("; charset=")) == 0) 
 158         wxString 
s(_T("<meta http-equiv=\"Content-Type\" content=\"")); 
 159         s 
<< file
.GetMimeType() <<  _T("\">"); 
 171 class wxHtmlFilterModule 
: public wxModule
 
 173     DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
) 
 176         virtual bool OnInit() 
 178             wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
); 
 179             wxHtmlWindow::AddFilter(new wxHtmlFilterImage
); 
 182         virtual void OnExit() {} 
 185 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)