2                 wxActiveX Library Licence, Version 3 
   3                 ==================================== 
   5   Copyright (C) 2003 Lindsay Mathieson [, ...] 
   7   Everyone is permitted to copy and distribute verbatim copies 
   8   of this licence document, but changing it is not allowed. 
  10                        wxActiveX LIBRARY LICENCE 
  11      TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 
  13   This library is free software; you can redistribute it and/or modify it 
  14   under the terms of the GNU Library General Public Licence as published by 
  15   the Free Software Foundation; either version 2 of the Licence, or (at 
  16   your option) any later version. 
  18   This library is distributed in the hope that it will be useful, but 
  19   WITHOUT ANY WARRANTY; without even the implied warranty of 
  20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library 
  21   General Public Licence for more details. 
  23   You should have received a copy of the GNU Library General Public Licence 
  24   along with this software, usually in a file named COPYING.LIB.  If not, 
  25   write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
  26   Boston, MA 02111-1307 USA. 
  30   1. As a special exception, the copyright holders of this library give 
  31   permission for additional uses of the text contained in this release of 
  32   the library as licenced under the wxActiveX Library Licence, applying 
  33   either version 3 of the Licence, or (at your option) any later version of 
  34   the Licence as published by the copyright holders of version 3 of the 
  37   2. The exception is that you may use, copy, link, modify and distribute 
  38   under the user's own terms, binary object code versions of works based 
  41   3. If you copy code from files distributed under the terms of the GNU 
  42   General Public Licence or the GNU Library General Public Licence into a 
  43   copy of this library, as this licence permits, the exception does not 
  44   apply to the code that you add in this way.  To avoid misleading anyone as 
  45   to the status of such modified files, you must delete this exception 
  46   notice from such code and/or adjust the licensing conditions notice 
  49   4. If you write modifications of your own for this library, it is your 
  50   choice whether to permit this exception to apply to your modifications.  
  51   If you do not wish that, you must delete the exception notice from such 
  52   code and/or adjust the licensing conditions notice accordingly. 
  55 #include "IEHtmlWin.h" 
  56 #include <wx/strconv.h> 
  57 #include <wx/string.h> 
  59 #include <wx/listctrl.h> 
  60 #include <wx/mstream.h> 
  68 #include <IEHtmlStream.h> 
  73 ////////////////////////////////////////////////////////////////////// 
  74 // Stream adapters and such 
  76 HRESULT STDMETHODCALLTYPE 
IStreamAdaptorBase::Read(void __RPC_FAR 
*pv
, ULONG cb
, ULONG __RPC_FAR 
*pcbRead
) 
  78     if (prepend
.size() > 0) 
  80         int n 
= min(prepend
.size(), cb
); 
  81         prepend
.copy((char *) pv
, n
); 
  82         prepend 
= prepend
.substr(n
); 
  89     int rc 
= Read((char *) pv
, cb
); 
  96 DEFINE_OLE_TABLE(IStreamAdaptorBase
) 
  97     OLE_IINTERFACE(IUnknown
) 
  98     OLE_IINTERFACE(ISequentialStream
) 
  99     OLE_IINTERFACE(IStream
) 
 103 IStreamAdaptor::IStreamAdaptor(istream 
*is
) 
 104     : IStreamAdaptorBase(), m_is(is
) 
 106     wxASSERT(m_is 
!= NULL
); 
 109 IStreamAdaptor::~IStreamAdaptor()    
 114 int IStreamAdaptor::Read(char *buf
, int cb
) 
 117     return m_is
->gcount(); 
 121 IwxStreamAdaptor::IwxStreamAdaptor(wxInputStream 
*is
) 
 122     : IStreamAdaptorBase(), m_is(is
)  
 124     wxASSERT(m_is 
!= NULL
); 
 126 IwxStreamAdaptor::~IwxStreamAdaptor()  
 132 int IwxStreamAdaptor::Read(char *buf
, int cb
) 
 135     return m_is
->LastRead(); 
 138 wxOwnedMemInputStream::wxOwnedMemInputStream(char *data
, size_t len
) 
 139     :  wxMemoryInputStream(data
, len
), m_data(data
) 
 142 wxOwnedMemInputStream::~wxOwnedMemInputStream()  
 148 ////////////////////////////////////////////////////////////////////// 
 149 BEGIN_EVENT_TABLE(wxIEHtmlWin
, wxActiveX
) 
 153 static const CLSID CLSID_MozillaBrowser 
= 
 154 { 0x1339B54C, 0x3453, 0x11D2, 
 155   { 0x93, 0xB9, 0x00, 0x00, 
 156     0x00, 0x00, 0x00, 0x00 } }; 
 159 //#define PROGID "Shell.Explorer" 
 160 #define PROGID CLSID_WebBrowser 
 161 //#define PROGID CLSID_MozillaBrowser 
 162 //#define PROGID CLSID_HTMLDocument 
 163 //#define PROGID "MSCAL.Calendar" 
 165 //#define PROGID "SoftwareFX.ChartFX.20" 
 167 wxIEHtmlWin::wxIEHtmlWin(wxWindow 
* parent
, wxWindowID id
, 
 171         const wxString
& name
) :  
 172     wxActiveX(parent
, PROGID
, id
, pos
, size
, style
, name
) 
 178 wxIEHtmlWin::~wxIEHtmlWin() 
 182 void wxIEHtmlWin::SetupBrowser() 
 186     // Get IWebBrowser2 Interface 
 187     hret 
= m_webBrowser
.QueryInterface(IID_IWebBrowser2
, m_ActiveX
); 
 188     assert(SUCCEEDED(hret
)); 
 191     m_webBrowser
->put_MenuBar(VARIANT_FALSE
); 
 192     m_webBrowser
->put_AddressBar(VARIANT_FALSE
); 
 193     m_webBrowser
->put_StatusBar(VARIANT_FALSE
); 
 194     m_webBrowser
->put_ToolBar(VARIANT_FALSE
); 
 196     m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
); 
 197     m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
); 
 199     m_webBrowser
->Navigate( L
"about:blank", NULL
, NULL
, NULL
, NULL 
); 
 203 void wxIEHtmlWin::SetEditMode(bool seton
) 
 205     m_bAmbientUserMode 
= ! seton
; 
 206     AmbientPropertyChanged(DISPID_AMBIENT_USERMODE
); 
 209 bool wxIEHtmlWin::GetEditMode() 
 211     return ! m_bAmbientUserMode
; 
 215 void wxIEHtmlWin::SetCharset(const wxString
& charset
) 
 218     IDispatch 
*pDisp 
= NULL
; 
 219     HRESULT hret 
= m_webBrowser
->get_Document(&pDisp
); 
 220     wxAutoOleInterface
<IDispatch
> disp(pDisp
); 
 224         wxAutoOleInterface
<IHTMLDocument2
> doc(IID_IHTMLDocument2
, disp
); 
 226             doc
->put_charset((BSTR
) (const wchar_t *) charset
.wc_str(wxConvUTF8
)); 
 227             //doc->put_charset((BSTR) wxConvUTF8.cMB2WC(charset).data()); 
 232 void wxIEHtmlWin::LoadUrl(const wxString
& url
) 
 234     VARIANTARG navFlag
, targetFrame
, postData
, headers
; 
 235     navFlag
.vt 
= VT_EMPTY
;  
 237     navFlag
.iVal 
= navNoReadFromCache
; 
 238     targetFrame
.vt 
= VT_EMPTY
; 
 239     postData
.vt 
= VT_EMPTY
; 
 240     headers
.vt 
= VT_EMPTY
; 
 243     hret 
= m_webBrowser
->Navigate((BSTR
) (const wchar_t *) url
.wc_str(wxConvUTF8
),  
 244         &navFlag
, &targetFrame
, &postData
, &headers
);    
 248 bool  wxIEHtmlWin::LoadString(const wxString
& html
) 
 251     size_t len 
= html
.length(); 
 255     data 
= (char *) malloc(len
); 
 256     memcpy(data
, html
.c_str(), len
); 
 257     return LoadStream(new wxOwnedMemInputStream(data
, len
)); 
 260 bool wxIEHtmlWin::LoadStream(IStreamAdaptorBase 
*pstrm
) 
 262     // need to prepend this as poxy MSHTML will not recognise a HTML comment 
 263     // as starting a html document and treats it as plain text 
 264     // Does nayone know how to force it to html mode ? 
 265     pstrm
->prepend 
= "<html>"; 
 267     // strip leading whitespace as it can confuse MSHTML 
 268     wxAutoOleInterface
<IStream
> strm(pstrm
); 
 270     // Document Interface 
 271     IDispatch 
*pDisp 
= NULL
; 
 272     HRESULT hret 
= m_webBrowser
->get_Document(&pDisp
); 
 275     wxAutoOleInterface
<IDispatch
> disp(pDisp
); 
 278     // get IPersistStreamInit 
 279     wxAutoOleInterface
<IPersistStreamInit
> 
 280         pPersistStreamInit(IID_IPersistStreamInit
, disp
); 
 282     if (pPersistStreamInit
.Ok()) 
 284         HRESULT hr 
= pPersistStreamInit
->InitNew(); 
 286             hr 
= pPersistStreamInit
->Load(strm
); 
 288         return SUCCEEDED(hr
); 
 294 bool  wxIEHtmlWin::LoadStream(istream 
*is
) 
 296     // wrap reference around stream 
 297     IStreamAdaptor 
*pstrm 
= new IStreamAdaptor(is
); 
 300     return LoadStream(pstrm
); 
 303 bool wxIEHtmlWin::LoadStream(wxInputStream 
*is
) 
 305     // wrap reference around stream 
 306     IwxStreamAdaptor 
*pstrm 
= new IwxStreamAdaptor(is
); 
 309     return LoadStream(pstrm
); 
 313 bool wxIEHtmlWin::GoBack() 
 316     hret 
= m_webBrowser
->GoBack(); 
 320 bool wxIEHtmlWin::GoForward() 
 323     hret 
= m_webBrowser
->GoForward(); 
 327 bool wxIEHtmlWin::GoHome() 
 331         CallMethod(_T("GoHome")); 
 340 bool wxIEHtmlWin::GoSearch() 
 343     hret 
= m_webBrowser
->GoSearch(); 
 347 bool wxIEHtmlWin::Refresh(wxIEHtmlRefreshLevel level
) 
 353     levelArg
.iVal 
= level
; 
 354     hret 
= m_webBrowser
->Refresh2(&levelArg
); 
 358 bool wxIEHtmlWin::Stop() 
 361     hret 
= m_webBrowser
->Stop(); 
 366 /////////////////////////////////////////////////////////////////////////////// 
 368 static wxAutoOleInterface
<IHTMLSelectionObject
> GetSelObject(IOleObject 
*oleObject
) 
 370     // Query for IWebBrowser interface 
 371     wxAutoOleInterface
<IWebBrowser2
> wb(IID_IWebBrowser2
, oleObject
); 
 373         return wxAutoOleInterface
<IHTMLSelectionObject
>(); 
 375     IDispatch 
*iDisp 
= NULL
; 
 376     HRESULT hr 
= wb
->get_Document(&iDisp
); 
 378         return wxAutoOleInterface
<IHTMLSelectionObject
>(); 
 380     // Query for Document Interface 
 381     wxAutoOleInterface
<IHTMLDocument2
> hd(IID_IHTMLDocument2
, iDisp
); 
 385         return wxAutoOleInterface
<IHTMLSelectionObject
>(); 
 387     IHTMLSelectionObject 
*_so 
= NULL
; 
 388     hr 
= hd
->get_selection(&_so
); 
 390     // take ownership of selection object 
 391     wxAutoOleInterface
<IHTMLSelectionObject
> so(_so
); 
 396 wxAutoOleInterface
<IHTMLTxtRange
> wxieGetSelRange(IOleObject 
*oleObject
) 
 398     wxAutoOleInterface
<IHTMLTxtRange
> tr
; 
 400     wxAutoOleInterface
<IHTMLSelectionObject
> so(GetSelObject(oleObject
)); 
 404     IDispatch 
*iDisp 
= NULL
; 
 405     HRESULT hr 
= so
->createRange(&iDisp
); 
 409     // Query for IHTMLTxtRange interface 
 410     tr
.QueryInterface(IID_IHTMLTxtRange
, iDisp
); 
 416 wxString 
wxIEHtmlWin::GetStringSelection(bool asHTML
) 
 418     wxAutoOleInterface
<IHTMLTxtRange
> tr(wxieGetSelRange(m_oleObject
)); 
 420         return wxEmptyString
; 
 426         hr 
= tr
->get_htmlText(&text
); 
 428         hr 
= tr
->get_text(&text
); 
 430         return wxEmptyString
; 
 438 wxString 
wxIEHtmlWin::GetText(bool asHTML
) 
 440     if (! m_webBrowser
.Ok()) 
 441         return wxEmptyString
; 
 443     // get document dispatch interface 
 444     IDispatch 
*iDisp 
= NULL
; 
 445     HRESULT hr 
= m_webBrowser
->get_Document(&iDisp
); 
 447         return wxEmptyString
; 
 449     // Query for Document Interface 
 450     wxAutoOleInterface
<IHTMLDocument2
> hd(IID_IHTMLDocument2
, iDisp
); 
 454         return wxEmptyString
; 
 457     IHTMLElement 
*_body 
= NULL
; 
 458     hd
->get_body(&_body
); 
 460         return wxEmptyString
; 
 461     wxAutoOleInterface
<IHTMLElement
> body(_body
); 
 468         hr 
= body
->get_innerHTML(&text
); 
 470         hr 
= body
->get_innerText(&text
); 
 472         return wxEmptyString
;