2 #include <wx/strconv.h>
4 #include <wx/listctrl.h>
14 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MSHTML_BEFORENAVIGATE2
);
15 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MSHTML_NEWWINDOW2
);
16 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MSHTML_DOCUMENTCOMPLETE
);
17 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MSHTML_PROGRESSCHANGE
);
18 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MSHTML_STATUSTEXTCHANGE
);
19 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MSHTML_TITLECHANGE
);
21 IMPLEMENT_DYNAMIC_CLASS(wxMSHTMLEvent
, wxNotifyEvent
);
24 //////////////////////////////////////////////////////////////////////
25 BEGIN_EVENT_TABLE(wxIEHtmlWin
, wxActiveX
)
28 class FS_DWebBrowserEvents2
: public IDispatch
31 DECLARE_OLE_UNKNOWN(FS_DWebBrowserEvents2
);
37 FS_DWebBrowserEvents2(wxIEHtmlWin
*iewin
) : m_iewin(iewin
) {}
38 ~FS_DWebBrowserEvents2()
43 STDMETHODIMP
GetIDsOfNames(REFIID r
, OLECHAR
** o
, unsigned int i
, LCID l
, DISPID
* d
)
48 STDMETHODIMP
GetTypeInfo(unsigned int i
, LCID l
, ITypeInfo
** t
)
53 STDMETHODIMP
GetTypeInfoCount(unsigned int* i
)
58 void Post(WXTYPE etype
, wxString text
, long l1
= 0, long l2
= 0)
60 if (! m_iewin
|| ! m_iewin
->GetParent())
64 event
.SetId(m_iewin
->GetId());
65 event
.SetEventType(etype
);
70 m_iewin
->GetParent()->AddPendingEvent(event
);
73 bool Process(WXTYPE etype
, wxString text
= "", long l1
= 0, long l2
= 0)
75 if (! m_iewin
|| ! m_iewin
->GetParent())
79 event
.SetId(m_iewin
->GetId());
80 event
.SetEventType(etype
);
85 m_iewin
->GetParent()->ProcessEvent(event
);
87 return event
.IsAllowed();
90 wxString
GetStrArg(VARIANT
& v
)
92 VARTYPE vt
= v
.vt
& ~VT_BYREF
;
95 return GetStrArg(*v
.pvarVal
);
96 else if (vt
== VT_BSTR
)
99 return (v
.pbstrVal
? *v
.pbstrVal
: L
"");
107 #define STR_ARG(arg) GetStrArg(pDispParams->rgvarg[arg])
109 #define LONG_ARG(arg)\
110 (pDispParams->rgvarg[arg].lVal)
113 STDMETHODIMP
Invoke(DISPID dispIdMember
, REFIID riid
, LCID lcid
,
114 WORD wFlags
, DISPPARAMS
* pDispParams
,
115 VARIANT
* pVarResult
, EXCEPINFO
* pExcepInfo
,
116 unsigned int * puArgErr
)
118 if (wFlags
& DISPATCH_PROPERTYGET
)
121 switch (dispIdMember
)
123 case DISPID_BEFORENAVIGATE2
:
124 if (Process(wxEVT_COMMAND_MSHTML_BEFORENAVIGATE2
, STR_ARG(5)))
125 *pDispParams
->rgvarg
->pboolVal
= VARIANT_FALSE
;
127 *pDispParams
->rgvarg
->pboolVal
= VARIANT_TRUE
;
130 case DISPID_NEWWINDOW2
:
131 if (Process(wxEVT_COMMAND_MSHTML_NEWWINDOW2
))
132 *pDispParams
->rgvarg
->pboolVal
= VARIANT_FALSE
;
134 *pDispParams
->rgvarg
->pboolVal
= VARIANT_TRUE
;
137 case DISPID_PROGRESSCHANGE
:
138 Post(wxEVT_COMMAND_MSHTML_PROGRESSCHANGE
, "", LONG_ARG(1), LONG_ARG(0));
141 case DISPID_DOCUMENTCOMPLETE
:
142 Post(wxEVT_COMMAND_MSHTML_DOCUMENTCOMPLETE
, STR_ARG(0));
145 case DISPID_STATUSTEXTCHANGE
:
146 Post(wxEVT_COMMAND_MSHTML_STATUSTEXTCHANGE
, STR_ARG(0));
149 case DISPID_TITLECHANGE
:
150 Post(wxEVT_COMMAND_MSHTML_TITLECHANGE
, STR_ARG(0));
160 DEFINE_OLE_TABLE(FS_DWebBrowserEvents2
)
161 OLE_IINTERFACE(IUnknown
)
162 OLE_INTERFACE(DIID_DWebBrowserEvents2
, DWebBrowserEvents2
)
166 static const CLSID CLSID_MozillaBrowser
=
167 { 0x1339B54C, 0x3453, 0x11D2,
168 { 0x93, 0xB9, 0x00, 0x00,
169 0x00, 0x00, 0x00, 0x00 } };
172 //#define PROGID "Shell.Explorer"
173 #define PROGID CLSID_WebBrowser
174 //#define PROGID CLSID_MozillaBrowser
175 //#define PROGID CLSID_HTMLDocument
176 //#define PROGID "MSCAL.Calendar"
177 //#define PROGID "WordPad.Document.1"
178 //#define PROGID "SoftwareFX.ChartFX.20"
180 wxIEHtmlWin::wxIEHtmlWin(wxWindow
* parent
, wxWindowID id
,
184 const wxString
& name
) :
185 wxActiveX(parent
, PROGID
, id
, pos
, size
, style
, name
)
191 wxIEHtmlWin::~wxIEHtmlWin()
195 void wxIEHtmlWin::SetupBrowser()
199 // Get IWebBrowser2 Interface
200 hret
= m_webBrowser
.QueryInterface(IID_IWebBrowser2
, m_ActiveX
);
201 assert(SUCCEEDED(hret
));
203 // Web Browser Events
204 FS_DWebBrowserEvents2
*events
= new FS_DWebBrowserEvents2(this);
205 hret
= ConnectAdvise(DIID_DWebBrowserEvents2
, events
);
206 if (! SUCCEEDED(hret
))
210 m_webBrowser
->put_MenuBar(VARIANT_FALSE
);
211 m_webBrowser
->put_AddressBar(VARIANT_FALSE
);
212 m_webBrowser
->put_StatusBar(VARIANT_FALSE
);
213 m_webBrowser
->put_ToolBar(VARIANT_FALSE
);
215 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
216 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
218 m_webBrowser
->Navigate( L
"about:blank", NULL
, NULL
, NULL
, NULL
);
222 void wxIEHtmlWin::SetEditMode(bool seton
)
224 m_bAmbientUserMode
= ! seton
;
225 AmbientPropertyChanged(DISPID_AMBIENT_USERMODE
);
228 bool wxIEHtmlWin::GetEditMode()
230 return ! m_bAmbientUserMode
;
234 void wxIEHtmlWin::SetCharset(wxString charset
)
237 IDispatch
*pDisp
= NULL
;
238 HRESULT hret
= m_webBrowser
->get_Document(&pDisp
);
239 wxAutoOleInterface
<IDispatch
> disp(pDisp
);
243 wxAutoOleInterface
<IHTMLDocument2
> doc(IID_IHTMLDocument2
, disp
);
245 doc
->put_charset((BSTR
) (const wchar_t*)charset
.wc_str(wxConvUTF8
));
250 class IStreamAdaptorBase
: public IStream
253 DECLARE_OLE_UNKNOWN(IStreamAdaptorBase
);
256 IStreamAdaptorBase() {}
257 virtual ~IStreamAdaptorBase() {}
260 HRESULT STDMETHODCALLTYPE
Read(void __RPC_FAR
*pv
, ULONG cb
, ULONG __RPC_FAR
*pcbRead
) = 0;
261 HRESULT STDMETHODCALLTYPE
Write(const void __RPC_FAR
*pv
, ULONG cb
, ULONG __RPC_FAR
*pcbWritten
) {return E_NOTIMPL
;}
264 HRESULT STDMETHODCALLTYPE
Seek(LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER __RPC_FAR
*plibNewPosition
) {return E_NOTIMPL
;}
265 HRESULT STDMETHODCALLTYPE
SetSize(ULARGE_INTEGER libNewSize
) {return E_NOTIMPL
;}
266 HRESULT STDMETHODCALLTYPE
CopyTo(IStream __RPC_FAR
*pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER __RPC_FAR
*pcbRead
, ULARGE_INTEGER __RPC_FAR
*pcbWritten
) {return E_NOTIMPL
;}
267 HRESULT STDMETHODCALLTYPE
Commit(DWORD grfCommitFlags
) {return E_NOTIMPL
;}
268 HRESULT STDMETHODCALLTYPE
Revert(void) {return E_NOTIMPL
;}
269 HRESULT STDMETHODCALLTYPE
LockRegion(ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
) {return E_NOTIMPL
;}
270 HRESULT STDMETHODCALLTYPE
UnlockRegion(ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
) {return E_NOTIMPL
;}
271 HRESULT STDMETHODCALLTYPE
Stat(STATSTG __RPC_FAR
*pstatstg
, DWORD grfStatFlag
) {return E_NOTIMPL
;}
272 HRESULT STDMETHODCALLTYPE
Clone(IStream __RPC_FAR
*__RPC_FAR
*ppstm
) {return E_NOTIMPL
;}
275 DEFINE_OLE_TABLE(IStreamAdaptorBase
)
276 OLE_IINTERFACE(IUnknown
)
277 OLE_IINTERFACE(ISequentialStream
)
278 OLE_IINTERFACE(IStream
)
281 class IStreamAdaptor
: public IStreamAdaptorBase
288 IStreamAdaptor(istream
*is
) : IStreamAdaptorBase(), m_is(is
)
290 wxASSERT(m_is
!= NULL
);
298 HRESULT STDMETHODCALLTYPE
Read(void __RPC_FAR
*pv
, ULONG cb
, ULONG __RPC_FAR
*pcbRead
)
300 m_is
->read((char *) pv
, cb
);
302 *pcbRead
= m_is
->gcount();
309 void wxIEHtmlWin::LoadUrl(const wxString
& url
)
311 VARIANTARG navFlag
, targetFrame
, postData
, headers
;
312 navFlag
.vt
= VT_EMPTY
;
314 navFlag
.iVal
= navNoReadFromCache
;
315 targetFrame
.vt
= VT_EMPTY
;
316 postData
.vt
= VT_EMPTY
;
317 headers
.vt
= VT_EMPTY
;
320 hret
= m_webBrowser
->Navigate((BSTR
) (const wchar_t*)url
.wc_str(wxConvUTF8
),
321 &navFlag
, &targetFrame
, &postData
, &headers
);
325 bool wxIEHtmlWin::LoadString(wxString html
)
327 string s
= html
.mb_str(wxConvUTF8
);
328 istringstream
*is
= new istringstream(s
);
329 return LoadStream(is
);
332 bool wxIEHtmlWin::LoadStream(istream
*is
)
334 // wrap reference around stream
335 IStreamAdaptor
*pstrm
= new IStreamAdaptor(is
);
338 wxAutoOleInterface
<IStream
> strm(pstrm
);
340 // Document Interface
341 IDispatch
*pDisp
= NULL
;
342 HRESULT hret
= m_webBrowser
->get_Document(&pDisp
);
345 wxAutoOleInterface
<IDispatch
> disp(pDisp
);
348 // get IPersistStreamInit
349 wxAutoOleInterface
<IPersistStreamInit
>
350 pPersistStreamInit(IID_IPersistStreamInit
, disp
);
352 if (pPersistStreamInit
.Ok())
354 HRESULT hr
= pPersistStreamInit
->InitNew();
356 hr
= pPersistStreamInit
->Load(strm
);
358 return SUCCEEDED(hr
);
364 bool wxIEHtmlWin::GoBack()
367 hret
= m_webBrowser
->GoBack();
371 bool wxIEHtmlWin::GoForward()
374 hret
= m_webBrowser
->GoForward();
378 bool wxIEHtmlWin::GoHome()
381 hret
= m_webBrowser
->GoHome();
385 bool wxIEHtmlWin::GoSearch()
388 hret
= m_webBrowser
->GoSearch();
392 bool wxIEHtmlWin::Refresh(wxIEHtmlRefreshLevel level
)
398 levelArg
.iVal
= level
;
399 hret
= m_webBrowser
->Refresh2(&levelArg
);
403 bool wxIEHtmlWin::Stop()
406 hret
= m_webBrowser
->Stop();
411 ///////////////////////////////////////////////////////////////////////////////
413 static wxAutoOleInterface
<IHTMLSelectionObject
> GetSelObject(IOleObject
*oleObject
)
415 // Query for IWebBrowser interface
416 wxAutoOleInterface
<IWebBrowser2
> wb(IID_IWebBrowser2
, oleObject
);
418 return wxAutoOleInterface
<IHTMLSelectionObject
>();
420 IDispatch
*iDisp
= NULL
;
421 HRESULT hr
= wb
->get_Document(&iDisp
);
423 return wxAutoOleInterface
<IHTMLSelectionObject
>();
425 // Query for Document Interface
426 wxAutoOleInterface
<IHTMLDocument2
> hd(IID_IHTMLDocument2
, iDisp
);
430 return wxAutoOleInterface
<IHTMLSelectionObject
>();
432 IHTMLSelectionObject
*_so
= NULL
;
433 hr
= hd
->get_selection(&_so
);
435 // take ownership of selection object
436 wxAutoOleInterface
<IHTMLSelectionObject
> so(_so
);
441 static wxAutoOleInterface
<IHTMLTxtRange
> GetSelRange(IOleObject
*oleObject
)
443 wxAutoOleInterface
<IHTMLTxtRange
> tr
;
445 wxAutoOleInterface
<IHTMLSelectionObject
> so(GetSelObject(oleObject
));
449 IDispatch
*iDisp
= NULL
;
450 HRESULT hr
= so
->createRange(&iDisp
);
454 // Query for IHTMLTxtRange interface
455 tr
.QueryInterface(IID_IHTMLTxtRange
, iDisp
);
461 wxString
wxIEHtmlWin::GetStringSelection(bool asHTML
)
463 wxAutoOleInterface
<IHTMLTxtRange
> tr(GetSelRange(m_oleObject
));
471 hr
= tr
->get_htmlText(&text
);
473 hr
= tr
->get_text(&text
);
483 wxString
wxIEHtmlWin::GetText(bool asHTML
)
485 if (! m_webBrowser
.Ok())
488 // get document dispatch interface
489 IDispatch
*iDisp
= NULL
;
490 HRESULT hr
= m_webBrowser
->get_Document(&iDisp
);
494 // Query for Document Interface
495 wxAutoOleInterface
<IHTMLDocument2
> hd(IID_IHTMLDocument2
, iDisp
);
502 IHTMLElement
*_body
= NULL
;
503 hd
->get_body(&_body
);
506 wxAutoOleInterface
<IHTMLElement
> body(_body
);
513 hr
= body
->get_innerHTML(&text
);
515 hr
= body
->get_innerText(&text
);