]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
   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 
  29 #include "wx/html/htmlfilt.h" 
  30 #include "wx/html/htmlwin.h" 
  35 There is code for several default filters: 
  39 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
) 
  41 //-------------------------------------------------------------------------------- 
  42 // wxHtmlFilterPlainText 
  43 //          filter for text/plain or uknown 
  44 //-------------------------------------------------------------------------------- 
  46 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
) 
  48 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const 
  55 wxString 
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const 
  57     wxInputStream 
*s 
= file
.GetStream(); 
  61     if (s 
== NULL
) return wxEmptyString
; 
  62     src 
= new char[s
->GetSize()+1]; 
  63     src
[s
->GetSize()] = 0; 
  64     s
->Read(src
, s
->GetSize()); 
  68     doc
.Replace(wxT("<"), wxT("<"), TRUE
); 
  69     doc
.Replace(wxT(">"), wxT(">"), TRUE
); 
  70     doc2 
= "<HTML><BODY><PRE>\n" + doc 
+ "\n</PRE></BODY></HTML>"; 
  78 //-------------------------------------------------------------------------------- 
  81 //-------------------------------------------------------------------------------- 
  83 class wxHtmlFilterImage 
: public wxHtmlFilter
 
  85     DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
) 
  88         virtual bool CanRead(const wxFSFile
& file
) const; 
  89         virtual wxString 
ReadFile(const wxFSFile
& file
) const; 
  92 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
) 
  96 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const 
  98     return (file
.GetMimeType().Left(6) == wxT("image/")); 
 103 wxString 
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const 
 105     return ("<HTML><BODY><IMG SRC=\"" + file
.GetLocation() + "\"></BODY></HTML>"); 
 111 //-------------------------------------------------------------------------------- 
 112 // wxHtmlFilterPlainText 
 113 //          filter for text/plain or uknown 
 114 //-------------------------------------------------------------------------------- 
 116 class wxHtmlFilterHTML 
: public wxHtmlFilter
 
 118     DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML
) 
 121         virtual bool CanRead(const wxFSFile
& file
) const; 
 122         virtual wxString 
ReadFile(const wxFSFile
& file
) const; 
 126 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
) 
 128 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const 
 130 //    return (file.GetMimeType() == "text/html"); 
 131 // This is true in most case but some page can return: 
 132 // "text/html; char-encoding=...." 
 133 // So we use Find instead 
 134   return (file
.GetMimeType().Find(wxT("text/html")) == 0); 
 139 wxString 
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const 
 141     wxInputStream 
*s 
= file
.GetStream(); 
 147         wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str()); 
 148         return wxEmptyString
; 
 150     src 
= new char[s
->GetSize() + 1]; 
 151     src
[s
->GetSize()] = 0; 
 152     s
->Read(src
, s
->GetSize()); 
 156     // add meta tag if we obtained this through http: 
 157     if (file
.GetMimeType().Find(_T("; charset=")) == 0) 
 159         wxString 
s(_T("<meta http-equiv=\"Content-Type\" content=\"")); 
 160         s 
<< file
.GetMimeType() <<  _T("\">"); 
 172 class wxHtmlFilterModule 
: public wxModule
 
 174     DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
) 
 177         virtual bool OnInit() 
 179             wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
); 
 180             wxHtmlWindow::AddFilter(new wxHtmlFilterImage
); 
 183         virtual void OnExit() {} 
 186 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)