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 L"Shell.Explorer"
173 #define PROGID CLSID_WebBrowser
174 //#define PROGID CLSID_HTMLDocument
175 //#define PROGID L"MSCAL.Calendar"
176 //#define PROGID L"WordPad.Document.1"
177 //#define PROGID L"SoftwareFX.ChartFX.20"
180 wxIEHtmlWin::wxIEHtmlWin(wxWindow
* parent
, wxWindowID id
)
181 : wxActiveX(parent
, PROGID
, id
)
186 wxIEHtmlWin::~wxIEHtmlWin()
190 void wxIEHtmlWin::SetupBrowser()
194 // Get IWebBrowser2 Interface
195 hret
= m_webBrowser
.QueryInterface(IID_IWebBrowser2
, m_ActiveX
);
196 assert(SUCCEEDED(hret
));
198 // Web Browser Events
199 FS_DWebBrowserEvents2
*events
= new FS_DWebBrowserEvents2(this);
200 hret
= ConnectAdvise(DIID_DWebBrowserEvents2
, events
);
201 if (! SUCCEEDED(hret
))
205 m_webBrowser
->put_MenuBar(VARIANT_FALSE
);
206 m_webBrowser
->put_AddressBar(VARIANT_FALSE
);
207 m_webBrowser
->put_StatusBar(VARIANT_FALSE
);
208 m_webBrowser
->put_ToolBar(VARIANT_FALSE
);
210 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
211 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
213 m_webBrowser
->Navigate( L
"about:blank", NULL
, NULL
, NULL
, NULL
);
217 void wxIEHtmlWin::SetEditMode(bool seton
)
219 m_bAmbientUserMode
= ! seton
;
220 AmbientPropertyChanged(DISPID_AMBIENT_USERMODE
);
223 bool wxIEHtmlWin::GetEditMode()
225 return ! m_bAmbientUserMode
;
229 void wxIEHtmlWin::SetCharset(wxString charset
)
232 IDispatch
*pDisp
= NULL
;
233 HRESULT hret
= m_webBrowser
->get_Document(&pDisp
);
234 wxAutoOleInterface
<IDispatch
> disp(pDisp
);
238 wxAutoOleInterface
<IHTMLDocument2
> doc(IID_IHTMLDocument2
, disp
);
240 doc
->put_charset((BSTR
) wxConvUTF8
.cMB2WC(charset
).data());
245 class IStreamAdaptorBase
: public IStream
248 DECLARE_OLE_UNKNOWN(IStreamAdaptorBase
);
251 IStreamAdaptorBase() {}
252 virtual ~IStreamAdaptorBase() {}
255 HRESULT STDMETHODCALLTYPE
Read(void __RPC_FAR
*pv
, ULONG cb
, ULONG __RPC_FAR
*pcbRead
) = 0;
256 HRESULT STDMETHODCALLTYPE
Write(const void __RPC_FAR
*pv
, ULONG cb
, ULONG __RPC_FAR
*pcbWritten
) {return E_NOTIMPL
;}
259 HRESULT STDMETHODCALLTYPE
Seek(LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER __RPC_FAR
*plibNewPosition
) {return E_NOTIMPL
;}
260 HRESULT STDMETHODCALLTYPE
SetSize(ULARGE_INTEGER libNewSize
) {return E_NOTIMPL
;}
261 HRESULT STDMETHODCALLTYPE
CopyTo(IStream __RPC_FAR
*pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER __RPC_FAR
*pcbRead
, ULARGE_INTEGER __RPC_FAR
*pcbWritten
) {return E_NOTIMPL
;}
262 HRESULT STDMETHODCALLTYPE
Commit(DWORD grfCommitFlags
) {return E_NOTIMPL
;}
263 HRESULT STDMETHODCALLTYPE
Revert(void) {return E_NOTIMPL
;}
264 HRESULT STDMETHODCALLTYPE
LockRegion(ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
) {return E_NOTIMPL
;}
265 HRESULT STDMETHODCALLTYPE
UnlockRegion(ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
) {return E_NOTIMPL
;}
266 HRESULT STDMETHODCALLTYPE
Stat(STATSTG __RPC_FAR
*pstatstg
, DWORD grfStatFlag
) {return E_NOTIMPL
;}
267 HRESULT STDMETHODCALLTYPE
Clone(IStream __RPC_FAR
*__RPC_FAR
*ppstm
) {return E_NOTIMPL
;}
270 DEFINE_OLE_TABLE(IStreamAdaptorBase
)
271 OLE_IINTERFACE(IUnknown
)
272 OLE_IINTERFACE(ISequentialStream
)
273 OLE_IINTERFACE(IStream
)
276 class IStreamAdaptor
: public IStreamAdaptorBase
283 IStreamAdaptor(istream
*is
) : IStreamAdaptorBase(), m_is(is
)
285 wxASSERT(m_is
!= NULL
);
293 HRESULT STDMETHODCALLTYPE
Read(void __RPC_FAR
*pv
, ULONG cb
, ULONG __RPC_FAR
*pcbRead
)
295 m_is
->read((char *) pv
, cb
);
297 *pcbRead
= m_is
->gcount();
304 void wxIEHtmlWin::LoadUrl(const wxString
& url
)
306 VARIANTARG navFlag
, targetFrame
, postData
, headers
;
307 navFlag
.vt
= VT_EMPTY
;
309 navFlag
.iVal
= navNoReadFromCache
;
310 targetFrame
.vt
= VT_EMPTY
;
311 postData
.vt
= VT_EMPTY
;
312 headers
.vt
= VT_EMPTY
;
315 hret
= m_webBrowser
->Navigate((BSTR
) wxConvUTF8
.cMB2WC(url
).data(),
316 &navFlag
, &targetFrame
, &postData
, &headers
);
320 bool wxIEHtmlWin::LoadString(wxString html
)
322 string s
= html
.c_str();
323 istringstream
*is
= new istringstream(s
);
324 return LoadStream(is
);
327 bool wxIEHtmlWin::LoadStream(istream
*is
)
329 // wrap refernce around stream
330 IStreamAdaptor
*pstrm
= new IStreamAdaptor(is
);
333 wxAutoOleInterface
<IStream
> strm(pstrm
);
335 // Document Interface
336 IDispatch
*pDisp
= NULL
;
337 HRESULT hret
= m_webBrowser
->get_Document(&pDisp
);
340 wxAutoOleInterface
<IDispatch
> disp(pDisp
);
343 // get IPersistStreamInit
344 wxAutoOleInterface
<IPersistStreamInit
>
345 pPersistStreamInit(IID_IPersistStreamInit
, disp
);
347 if (pPersistStreamInit
.Ok())
349 HRESULT hr
= pPersistStreamInit
->InitNew();
351 hr
= pPersistStreamInit
->Load(strm
);
357 bool wxIEHtmlWin::GoBack()
360 hret
= m_webBrowser
->GoBack();
364 bool wxIEHtmlWin::GoForward()
367 hret
= m_webBrowser
->GoForward();
371 bool wxIEHtmlWin::GoHome()
374 hret
= m_webBrowser
->GoHome();
378 bool wxIEHtmlWin::GoSearch()
381 hret
= m_webBrowser
->GoSearch();
385 bool wxIEHtmlWin::Refresh(wxIEHtmlRefreshLevel level
)
391 levelArg
.iVal
= level
;
392 hret
= m_webBrowser
->Refresh2(&levelArg
);
396 bool wxIEHtmlWin::Stop()
399 hret
= m_webBrowser
->Stop();
403 ///////////////////////////////////////////////////////////////////////////////
405 static wxAutoOleInterface
<IHTMLSelectionObject
> GetSelObject(IOleObject
*oleObject
)
407 // Query for IWebBrowser interface
408 wxAutoOleInterface
<IWebBrowser2
> wb(IID_IWebBrowser2
, oleObject
);
410 return wxAutoOleInterface
<IHTMLSelectionObject
>();
412 IDispatch
*iDisp
= NULL
;
413 HRESULT hr
= wb
->get_Document(&iDisp
);
415 return wxAutoOleInterface
<IHTMLSelectionObject
>();
417 // Query for Document Interface
418 wxAutoOleInterface
<IHTMLDocument2
> hd(IID_IHTMLDocument2
, iDisp
);
422 return wxAutoOleInterface
<IHTMLSelectionObject
>();
424 IHTMLSelectionObject
*_so
= NULL
;
425 hr
= hd
->get_selection(&_so
);
427 // take ownership of selection object
428 wxAutoOleInterface
<IHTMLSelectionObject
> so(_so
);
433 static wxAutoOleInterface
<IHTMLTxtRange
> GetSelRange(IOleObject
*oleObject
)
435 wxAutoOleInterface
<IHTMLTxtRange
> tr
;
437 wxAutoOleInterface
<IHTMLSelectionObject
> so(GetSelObject(oleObject
));
441 IDispatch
*iDisp
= NULL
;
442 HRESULT hr
= so
->createRange(&iDisp
);
446 // Query for IHTMLTxtRange interface
447 tr
.QueryInterface(IID_IHTMLTxtRange
, iDisp
);
453 wxString
wxIEHtmlWin::GetStringSelection(bool asHTML
)
455 wxAutoOleInterface
<IHTMLTxtRange
> tr(GetSelRange(m_oleObject
));
463 hr
= tr
->get_htmlText(&text
);
465 hr
= tr
->get_text(&text
);
475 wxString
wxIEHtmlWin::GetText(bool asHTML
)
477 if (! m_webBrowser
.Ok())
480 // get document dispatch interface
481 IDispatch
*iDisp
= NULL
;
482 HRESULT hr
= m_webBrowser
->get_Document(&iDisp
);
486 // Query for Document Interface
487 wxAutoOleInterface
<IHTMLDocument2
> hd(IID_IHTMLDocument2
, iDisp
);
494 IHTMLElement
*_body
= NULL
;
495 hd
->get_body(&_body
);
498 wxAutoOleInterface
<IHTMLElement
> body(_body
);
505 hr
= body
->get_innerHTML(&text
);
507 hr
= body
->get_innerText(&text
);