1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/webview_ie.cpp
3 // Purpose: wxMSW wxWebViewIE class implementation for web view component
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if defined(__BORLANDC__)
17 #include "wx/msw/webview_ie.h"
19 #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE
26 #include "wx/msw/registry.h"
27 #include "wx/msw/missing.h"
28 #include "wx/filesys.h"
29 #include "wx/dynlib.h"
32 /* These GUID definitions are our own implementation to support interfaces
33 * normally in urlmon.h. See include/wx/msw/webview_ie.h
38 DEFINE_GUID(wxIID_IInternetProtocolRoot
,0x79eac9e3,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
39 DEFINE_GUID(wxIID_IInternetProtocol
,0x79eac9e4,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
40 DEFINE_GUID(wxIID_IDocHostUIHandler
, 0xbd3f23c0, 0xd43e, 0x11cf, 0x89, 0x3b, 0x00, 0xaa, 0x00, 0xbd, 0xce, 0x1a);
44 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE
, wxWebView
);
46 BEGIN_EVENT_TABLE(wxWebViewIE
, wxControl
)
47 EVT_ACTIVEX(wxID_ANY
, wxWebViewIE::onActiveXEvent
)
48 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg
)
51 bool wxWebViewIE::Create(wxWindow
* parent
,
59 if (!wxControl::Create(parent
, id
, pos
, size
, style
,
60 wxDefaultValidator
, name
))
67 m_historyLoadingFromList
= false;
68 m_historyEnabled
= true;
69 m_historyPosition
= -1;
70 m_zoomType
= wxWEB_VIEW_ZOOM_TYPE_TEXT
;
72 if (::CoCreateInstance(CLSID_WebBrowser
, NULL
,
73 CLSCTX_INPROC_SERVER
, // CLSCTX_INPROC,
74 IID_IWebBrowser2
, (void**)&m_webBrowser
) != 0)
76 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
80 m_ie
.SetDispatchPtr(m_webBrowser
); // wxAutomationObject will release itself
82 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
83 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
85 m_uiHandler
= new DocHostUIHandler
;
86 m_uiHandler
->AddRef();
88 m_container
= new wxIEContainer(this, IID_IWebBrowser2
, m_webBrowser
, m_uiHandler
);
94 wxWebViewIE::~wxWebViewIE()
96 for(unsigned int i
= 0; i
< m_factories
.size(); i
++)
98 m_factories
[i
]->Release();
101 m_uiHandler
->Release();
104 void wxWebViewIE::LoadURL(const wxString
& url
)
106 m_ie
.CallMethod("Navigate", wxConvertStringToOle(url
));
109 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
111 BSTR bstr
= SysAllocString(OLESTR(""));
112 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
113 if (psaStrings
!= NULL
)
116 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
118 param
->bstrVal
= bstr
;
120 hr
= SafeArrayUnaccessData(psaStrings
);
122 IHTMLDocument2
* document
= GetDocument();
123 document
->write(psaStrings
);
127 SafeArrayDestroy(psaStrings
);
129 bstr
= SysAllocString(html
.wc_str());
131 // Creates a new one-dimensional array
132 psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
133 if (psaStrings
!= NULL
)
135 hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
137 param
->bstrVal
= bstr
;
138 hr
= SafeArrayUnaccessData(psaStrings
);
140 document
= GetDocument();
141 document
->write(psaStrings
);
144 // SafeArrayDestroy calls SysFreeString for each BSTR
145 SafeArrayDestroy(psaStrings
);
147 //We send the events when we are done to mimic webkit
149 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
150 GetId(), baseUrl
, "");
151 event
.SetEventObject(this);
152 HandleWindowEvent(event
);
154 //Document complete event
155 event
.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED
);
156 event
.SetEventObject(this);
157 HandleWindowEvent(event
);
161 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
166 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL during clear");
170 wxString
wxWebViewIE::GetPageSource() const
172 IHTMLDocument2
* document
= GetDocument();
173 IHTMLElement
*bodyTag
= NULL
;
174 IHTMLElement
*htmlTag
= NULL
;
176 HRESULT hr
= document
->get_body(&bodyTag
);
179 hr
= bodyTag
->get_parentElement(&htmlTag
);
183 htmlTag
->get_outerHTML(&bstr
);
184 source
= wxString(bstr
);
194 wxWebViewZoom
wxWebViewIE::GetZoom() const
198 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
199 return GetIEOpticalZoom();
200 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
201 return GetIETextZoom();
206 //Dummy return to stop compiler warnings
207 return wxWEB_VIEW_ZOOM_MEDIUM
;
211 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
215 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
216 SetIEOpticalZoom(zoom
);
218 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
226 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level
)
228 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
229 //is 0 to 4 so the check is unnecessary, these match exactly with the
232 VariantInit (&zoomVariant
);
233 V_VT(&zoomVariant
) = VT_I4
;
234 V_I4(&zoomVariant
) = level
;
239 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
240 OLECMDEXECOPT_DONTPROMPTUSER
,
242 wxASSERT(result
== S_OK
);
245 wxWebViewZoom
wxWebViewIE::GetIETextZoom() const
248 VariantInit (&zoomVariant
);
249 V_VT(&zoomVariant
) = VT_I4
;
254 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
255 OLECMDEXECOPT_DONTPROMPTUSER
,
257 wxASSERT(result
== S_OK
);
259 //We can safely cast here as we know that the range matches our enum
260 return static_cast<wxWebViewZoom
>(V_I4(&zoomVariant
));
263 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level
)
265 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
266 //is 10 to 1000 so the check is unnecessary
268 VariantInit (&zoomVariant
);
269 V_VT(&zoomVariant
) = VT_I4
;
271 //We make a somewhat arbitray map here, taken from values used by webkit
274 case wxWEB_VIEW_ZOOM_TINY
:
275 V_I4(&zoomVariant
) = 60;
277 case wxWEB_VIEW_ZOOM_SMALL
:
278 V_I4(&zoomVariant
) = 80;
280 case wxWEB_VIEW_ZOOM_MEDIUM
:
281 V_I4(&zoomVariant
) = 100;
283 case wxWEB_VIEW_ZOOM_LARGE
:
284 V_I4(&zoomVariant
) = 130;
286 case wxWEB_VIEW_ZOOM_LARGEST
:
287 V_I4(&zoomVariant
) = 160;
296 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
297 OLECMDEXECOPT_DODEFAULT
,
300 wxASSERT(result
== S_OK
);
303 wxWebViewZoom
wxWebViewIE::GetIEOpticalZoom() const
306 VariantInit (&zoomVariant
);
307 V_VT(&zoomVariant
) = VT_I4
;
312 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
313 OLECMDEXECOPT_DODEFAULT
, NULL
,
315 wxASSERT(result
== S_OK
);
317 const int zoom
= V_I4(&zoomVariant
);
319 //We make a somewhat arbitray map here, taken from values used by webkit
322 return wxWEB_VIEW_ZOOM_TINY
;
324 else if (zoom
> 65 && zoom
<= 90)
326 return wxWEB_VIEW_ZOOM_SMALL
;
328 else if (zoom
> 90 && zoom
<= 115)
330 return wxWEB_VIEW_ZOOM_MEDIUM
;
332 else if (zoom
> 115 && zoom
<= 145)
334 return wxWEB_VIEW_ZOOM_LARGE
;
336 else /*if (zoom > 145) */ //Using else removes a compiler warning
338 return wxWEB_VIEW_ZOOM_LARGEST
;
342 void wxWebViewIE::SetZoomType(wxWebViewZoomType type
)
347 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
352 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type
) const
354 //IE 6 and below only support text zoom, so check the registry to see what
355 //version we actually have
356 wxRegKey
key(wxRegKey::HKLM
, "Software\\Microsoft\\Internet Explorer");
358 key
.QueryValue("Version", value
);
360 long version
= wxAtoi(value
.Left(1));
361 if(version
<= 6 && type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
367 void wxWebViewIE::Print()
369 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
370 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
373 bool wxWebViewIE::CanGoBack() const
376 return m_historyPosition
> 0;
381 bool wxWebViewIE::CanGoForward() const
384 return m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1;
389 void wxWebViewIE::LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
)
392 for(unsigned int i
= 0; i
< m_historyList
.size(); i
++)
394 //We compare the actual pointers to find the correct item
395 if(m_historyList
[i
].get() == item
.get())
398 wxASSERT_MSG(pos
!= static_cast<int>(m_historyList
.size()),
399 "invalid history item");
400 m_historyLoadingFromList
= true;
401 LoadURL(item
->GetUrl());
402 m_historyPosition
= pos
;
405 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetBackwardHistory()
407 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > backhist
;
408 //As we don't have std::copy or an iterator constructor in the wxwidgets
409 //native vector we construct it by hand
410 for(int i
= 0; i
< m_historyPosition
; i
++)
412 backhist
.push_back(m_historyList
[i
]);
417 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetForwardHistory()
419 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forwardhist
;
420 //As we don't have std::copy or an iterator constructor in the wxwidgets
421 //native vector we construct it by hand
422 for(int i
= m_historyPosition
+ 1; i
< static_cast<int>(m_historyList
.size()); i
++)
424 forwardhist
.push_back(m_historyList
[i
]);
429 void wxWebViewIE::GoBack()
431 LoadHistoryItem(m_historyList
[m_historyPosition
- 1]);
434 void wxWebViewIE::GoForward()
436 LoadHistoryItem(m_historyList
[m_historyPosition
+ 1]);
439 void wxWebViewIE::Stop()
441 m_ie
.CallMethod("Stop");
444 void wxWebViewIE::ClearHistory()
446 m_historyList
.clear();
447 m_historyPosition
= -1;
450 void wxWebViewIE::EnableHistory(bool enable
)
452 m_historyEnabled
= enable
;
453 m_historyList
.clear();
454 m_historyPosition
= -1;
457 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
461 V_VT(&level
) = VT_I2
;
465 case wxWEB_VIEW_RELOAD_DEFAULT
:
466 V_I2(&level
) = REFRESH_NORMAL
;
468 case wxWEB_VIEW_RELOAD_NO_CACHE
:
469 V_I2(&level
) = REFRESH_COMPLETELY
;
472 wxFAIL_MSG("Unexpected reload type");
475 m_webBrowser
->Refresh2(&level
);
478 bool wxWebViewIE::IsOfflineMode()
480 wxVariant out
= m_ie
.GetProperty("Offline");
482 wxASSERT(out
.GetType() == "bool");
484 return out
.GetBool();
487 void wxWebViewIE::SetOfflineMode(bool offline
)
489 // FIXME: the wxWidgets docs do not really document what the return
490 // parameter of PutProperty is
494 m_ie
.PutProperty("Offline", (offline
?
500 bool wxWebViewIE::IsBusy() const
502 if (m_isBusy
) return true;
504 wxVariant out
= m_ie
.GetProperty("Busy");
506 wxASSERT(out
.GetType() == "bool");
508 return out
.GetBool();
511 wxString
wxWebViewIE::GetCurrentURL() const
513 wxVariant out
= m_ie
.GetProperty("LocationURL");
515 wxASSERT(out
.GetType() == "string");
516 return out
.GetString();
519 wxString
wxWebViewIE::GetCurrentTitle() const
521 IHTMLDocument2
* document
= GetDocument();
524 document
->get_nameProp(&title
);
526 return wxString(title
);
529 bool wxWebViewIE::CanCut() const
531 return CanExecCommand("Cut");
534 bool wxWebViewIE::CanCopy() const
536 return CanExecCommand("Copy");
538 bool wxWebViewIE::CanPaste() const
540 return CanExecCommand("Paste");
543 void wxWebViewIE::Cut()
548 void wxWebViewIE::Copy()
553 void wxWebViewIE::Paste()
555 ExecCommand("Paste");
558 bool wxWebViewIE::CanUndo() const
560 return CanExecCommand("Undo");
562 bool wxWebViewIE::CanRedo() const
564 return CanExecCommand("Redo");
567 void wxWebViewIE::Undo()
572 void wxWebViewIE::Redo()
577 void wxWebViewIE::SetEditable(bool enable
)
579 IHTMLDocument2
* document
= GetDocument();
581 document
->put_designMode(SysAllocString(L
"On"));
583 document
->put_designMode(SysAllocString(L
"Off"));
588 bool wxWebViewIE::IsEditable() const
590 IHTMLDocument2
* document
= GetDocument();
592 document
->get_designMode(&mode
);
594 if(wxString(mode
) == "On")
600 void wxWebViewIE::SelectAll()
602 ExecCommand("SelectAll");
605 bool wxWebViewIE::HasSelection() const
607 IHTMLDocument2
* document
= GetDocument();
608 IHTMLSelectionObject
* selection
;
610 HRESULT hr
= document
->get_selection(&selection
);
614 selection
->get_type(&type
);
615 sel
= wxString(type
);
616 selection
->Release();
619 return sel
!= "None";
622 void wxWebViewIE::DeleteSelection()
624 ExecCommand("Delete");
627 wxString
wxWebViewIE::GetSelectedText() const
629 IHTMLDocument2
* document
= GetDocument();
630 IHTMLSelectionObject
* selection
;
632 HRESULT hr
= document
->get_selection(&selection
);
636 hr
= selection
->createRange(&disrange
);
639 IHTMLTxtRange
* range
;
640 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
644 range
->get_text(&text
);
645 selected
= wxString(text
);
650 selection
->Release();
656 wxString
wxWebViewIE::GetSelectedSource() const
658 IHTMLDocument2
* document
= GetDocument();
659 IHTMLSelectionObject
* selection
;
661 HRESULT hr
= document
->get_selection(&selection
);
665 hr
= selection
->createRange(&disrange
);
668 IHTMLTxtRange
* range
;
669 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
673 range
->get_htmlText(&text
);
674 selected
= wxString(text
);
679 selection
->Release();
685 void wxWebViewIE::ClearSelection()
687 IHTMLDocument2
* document
= GetDocument();
688 IHTMLSelectionObject
* selection
;
690 HRESULT hr
= document
->get_selection(&selection
);
694 selection
->Release();
699 wxString
wxWebViewIE::GetPageText() const
701 IHTMLDocument2
* document
= GetDocument();
704 HRESULT hr
= document
->get_body(&body
);
708 body
->get_innerText(&out
);
709 text
= wxString(out
);
716 void wxWebViewIE::RunScript(const wxString
& javascript
)
718 IHTMLDocument2
* document
= GetDocument();
719 IHTMLWindow2
* window
;
720 wxString language
= "javascript";
721 HRESULT hr
= document
->get_parentWindow(&window
);
726 V_VT(&level
) = VT_EMPTY
;
727 window
->execScript(SysAllocString(javascript
.wc_str()),
728 SysAllocString(language
.wc_str()),
734 void wxWebViewIE::RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
)
736 wxDynamicLibrary
urlMon(wxT("urlmon.dll"));
737 if(urlMon
.HasSymbol(wxT("CoInternetGetSession")))
739 typedef HRESULT (WINAPI
*CoInternetGetSession_t
)(DWORD
, wxIInternetSession
**, DWORD
);
740 wxDYNLIB_FUNCTION(CoInternetGetSession_t
, CoInternetGetSession
, urlMon
);
742 ClassFactory
* cf
= new ClassFactory(handler
);
743 wxIInternetSession
* session
;
744 HRESULT res
= (*pfnCoInternetGetSession
)(0, &session
, 0);
747 wxFAIL_MSG("Could not retrive internet session");
750 HRESULT hr
= session
->RegisterNameSpace(cf
, CLSID_FileProtocol
,
751 handler
->GetName().wc_str(),
755 wxFAIL_MSG("Could not register protocol");
757 m_factories
.push_back(cf
);
761 wxFAIL_MSG("urlmon does not contain CoInternetGetSession");
765 bool wxWebViewIE::CanExecCommand(wxString command
) const
767 IHTMLDocument2
* document
= GetDocument();
768 VARIANT_BOOL enabled
;
770 document
->queryCommandEnabled(SysAllocString(command
.wc_str()), &enabled
);
773 return (enabled
== VARIANT_TRUE
);
776 void wxWebViewIE::ExecCommand(wxString command
)
778 IHTMLDocument2
* document
= GetDocument();
779 document
->execCommand(SysAllocString(command
.wc_str()), VARIANT_FALSE
, VARIANT(), NULL
);
783 IHTMLDocument2
* wxWebViewIE::GetDocument() const
785 wxVariant variant
= m_ie
.GetProperty("Document");
786 IHTMLDocument2
* document
= (IHTMLDocument2
*)variant
.GetVoidPtr();
793 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
795 if (m_webBrowser
== NULL
) return;
797 switch (evt
.GetDispatchId())
799 case DISPID_BEFORENAVIGATE2
:
803 wxString url
= evt
[1].GetString();
804 wxString target
= evt
[3].GetString();
806 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
807 GetId(), url
, target
);
809 //skip empty javascript events.
810 if(url
== "javascript:\"\"" && target
.IsEmpty())
816 event
.SetEventObject(this);
817 HandleWindowEvent(event
);
820 if (!event
.IsAllowed())
822 wxActiveXEventNativeMSW
* nativeParams
=
823 evt
.GetNativeParameters();
824 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
827 // at this point, either the navigation event has been cancelled
828 // and we're not busy, either it was accepted and IWebBrowser2's
829 // Busy property will be true; so we don't need our override
836 case DISPID_NAVIGATECOMPLETE2
:
838 wxString url
= evt
[1].GetString();
839 // TODO: set target parameter if possible
840 wxString target
= wxEmptyString
;
841 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
842 GetId(), url
, target
);
843 event
.SetEventObject(this);
844 HandleWindowEvent(event
);
848 case DISPID_PROGRESSCHANGE
:
854 case DISPID_DOCUMENTCOMPLETE
:
856 //Only send a complete even if we are actually finished, this brings
857 //the event in to line with webkit
859 m_webBrowser
->get_ReadyState( &rs
);
860 if(rs
!= READYSTATE_COMPLETE
)
863 wxString url
= evt
[1].GetString();
865 //As we are complete we also add to the history list, but not if the
866 //page is not the main page, ie it is a subframe
867 //We also have to check if we are loading a file:// url, if so we
868 //need to change the comparison as ie passes back a different style
870 if(m_historyEnabled
&& !m_historyLoadingFromList
&&
871 (url
== GetCurrentURL() ||
872 (GetCurrentURL().substr(0, 4) == "file" &&
873 wxFileSystem::URLToFileName(GetCurrentURL()).GetFullPath() == url
)))
875 //If we are not at the end of the list, then erase everything
876 //between us and the end before adding the new page
877 if(m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1)
879 m_historyList
.erase(m_historyList
.begin() + m_historyPosition
+ 1,
880 m_historyList
.end());
882 wxSharedPtr
<wxWebViewHistoryItem
> item(new wxWebViewHistoryItem(url
, GetCurrentTitle()));
883 m_historyList
.push_back(item
);
886 //Reset as we are done now
887 m_historyLoadingFromList
= false;
888 // TODO: set target parameter if possible
889 wxString target
= wxEmptyString
;
890 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
892 event
.SetEventObject(this);
893 HandleWindowEvent(event
);
897 case DISPID_STATUSTEXTCHANGE
:
902 case DISPID_TITLECHANGE
:
904 wxString title
= evt
[0].GetString();
906 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
907 GetId(), GetCurrentURL(), "");
908 event
.SetString(title
);
909 event
.SetEventObject(this);
910 HandleWindowEvent(event
);
914 case DISPID_NAVIGATEERROR
:
916 wxWebViewNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
917 wxString errorCode
= "?";
918 switch (evt
[3].GetLong())
920 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
921 errorCode
= "INET_E_INVALID_URL";
922 errorType
= wxWEB_NAV_ERR_REQUEST
;
924 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
925 errorCode
= "INET_E_NO_SESSION";
926 errorType
= wxWEB_NAV_ERR_CONNECTION
;
928 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
929 errorCode
= "INET_E_CANNOT_CONNECT";
930 errorType
= wxWEB_NAV_ERR_CONNECTION
;
932 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
933 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
934 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
936 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
937 errorCode
= "INET_E_OBJECT_NOT_FOUND";
938 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
940 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
941 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
942 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
944 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
945 errorCode
= "INET_E_DOWNLOAD_FAILURE";
946 errorType
= wxWEB_NAV_ERR_CONNECTION
;
948 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
949 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
950 errorType
= wxWEB_NAV_ERR_AUTH
;
952 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
953 errorCode
= "INET_E_NO_VALID_MEDIA";
954 errorType
= wxWEB_NAV_ERR_REQUEST
;
956 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
957 errorCode
= "INET_E_CONNECTION_TIMEOUT";
958 errorType
= wxWEB_NAV_ERR_CONNECTION
;
960 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
961 errorCode
= "INET_E_INVALID_REQUEST";
962 errorType
= wxWEB_NAV_ERR_REQUEST
;
964 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
965 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
966 errorType
= wxWEB_NAV_ERR_REQUEST
;
968 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
969 errorCode
= "INET_E_SECURITY_PROBLEM";
970 errorType
= wxWEB_NAV_ERR_SECURITY
;
972 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
973 errorCode
= "INET_E_CANNOT_LOAD_DATA";
974 errorType
= wxWEB_NAV_ERR_OTHER
;
976 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
977 // CoCreateInstance will return an error code if this happens,
978 // we'll handle this above.
981 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
982 errorCode
= "INET_E_REDIRECT_FAILED";
983 errorType
= wxWEB_NAV_ERR_OTHER
;
985 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
986 errorCode
= "INET_E_REDIRECT_TO_DIR";
987 errorType
= wxWEB_NAV_ERR_REQUEST
;
989 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
990 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
991 errorType
= wxWEB_NAV_ERR_OTHER
;
993 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
994 errorCode
= "INET_E_USE_EXTEND_BINDING";
995 errorType
= wxWEB_NAV_ERR_OTHER
;
997 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
998 errorCode
= "INET_E_TERMINATED_BIND";
999 errorType
= wxWEB_NAV_ERR_OTHER
;
1001 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
1002 errorCode
= "INET_E_INVALID_CERTIFICATE";
1003 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
1005 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
1006 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
1007 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
1009 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
1010 // cancel request cancelled...
1011 errorCode
= "INET_E_RESULT_DISPATCHED";
1012 errorType
= wxWEB_NAV_ERR_OTHER
;
1014 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
1015 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
1016 errorType
= wxWEB_NAV_ERR_SECURITY
;
1018 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
1019 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
1020 errorType
= wxWEB_NAV_ERR_SECURITY
;
1022 case INET_E_CODE_INSTALL_SUPPRESSED
:
1023 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
1024 errorType
= wxWEB_NAV_ERR_SECURITY
;
1028 wxString url
= evt
[1].GetString();
1029 wxString target
= evt
[2].GetString();
1030 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
1032 event
.SetEventObject(this);
1033 event
.SetInt(errorType
);
1034 event
.SetString(errorCode
);
1035 HandleWindowEvent(event
);
1038 case DISPID_NEWWINDOW3
:
1040 wxString url
= evt
[4].GetString();
1042 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
1043 GetId(), url
, wxEmptyString
);
1044 event
.SetEventObject(this);
1045 HandleWindowEvent(event
);
1047 //We always cancel this event otherwise an Internet Exporer window
1048 //is opened for the url
1049 wxActiveXEventNativeMSW
* nativeParams
= evt
.GetNativeParameters();
1050 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[3]) = VARIANT_TRUE
;
1058 VirtualProtocol::VirtualProtocol(wxSharedPtr
<wxWebViewHandler
> handler
)
1061 m_handler
= handler
;
1064 BEGIN_IID_TABLE(VirtualProtocol
)
1066 ADD_RAW_IID(wxIID_IInternetProtocolRoot
)
1067 ADD_RAW_IID(wxIID_IInternetProtocol
)
1070 IMPLEMENT_IUNKNOWN_METHODS(VirtualProtocol
)
1072 HRESULT
VirtualProtocol::Start(LPCWSTR szUrl
, wxIInternetProtocolSink
*pOIProtSink
,
1073 wxIInternetBindInfo
*pOIBindInfo
, DWORD grfPI
,
1074 HANDLE_PTR dwReserved
)
1077 wxUnusedVar(pOIBindInfo
);
1079 wxUnusedVar(dwReserved
);
1080 m_protocolSink
= pOIProtSink
;
1082 //We get the file itself from the protocol handler
1083 m_file
= m_handler
->GetFile(szUrl
);
1087 return INET_E_RESOURCE_NOT_FOUND
;
1089 //We return the stream length for current and total size as we can always
1090 //read the whole file from the stream
1091 wxFileOffset length
= m_file
->GetStream()->GetLength();
1092 m_protocolSink
->ReportData(wxBSCF_FIRSTDATANOTIFICATION
|
1093 wxBSCF_DATAFULLYAVAILABLE
|
1094 wxBSCF_LASTDATANOTIFICATION
,
1099 HRESULT
VirtualProtocol::Read(void *pv
, ULONG cb
, ULONG
*pcbRead
)
1101 //If the file is null we return false to indicte it is finished
1105 wxStreamError err
= m_file
->GetStream()->Read(pv
, cb
).GetLastError();
1106 *pcbRead
= m_file
->GetStream()->LastRead();
1108 if(err
== wxSTREAM_NO_ERROR
)
1113 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1115 //As we are not eof there is more data
1118 else if(err
== wxSTREAM_EOF
)
1121 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1122 //We are eof and so finished
1125 else if(err
== wxSTREAM_READ_ERROR
)
1128 return INET_E_DOWNLOAD_FAILURE
;
1132 //Dummy return to surpress a compiler warning
1134 return INET_E_DOWNLOAD_FAILURE
;
1138 BEGIN_IID_TABLE(ClassFactory
)
1140 ADD_IID(ClassFactory
)
1143 IMPLEMENT_IUNKNOWN_METHODS(ClassFactory
)
1145 HRESULT
ClassFactory::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
,
1149 return CLASS_E_NOAGGREGATION
;
1150 VirtualProtocol
* vp
= new VirtualProtocol(m_handler
);
1152 HRESULT hr
= vp
->QueryInterface(riid
, ppvObject
);
1158 STDMETHODIMP
ClassFactory::LockServer(BOOL fLock
)
1164 wxIEContainer::wxIEContainer(wxWindow
*parent
, REFIID iid
, IUnknown
*pUnk
,
1165 DocHostUIHandler
* uiHandler
) :
1166 wxActiveXContainer(parent
,iid
,pUnk
)
1168 m_uiHandler
= uiHandler
;
1171 wxIEContainer::~wxIEContainer()
1175 bool wxIEContainer::QueryClientSiteInterface(REFIID iid
, void **_interface
,
1178 if (m_uiHandler
&& IsEqualIID(iid
, wxIID_IDocHostUIHandler
))
1180 *_interface
= (IUnknown
*) (wxIDocHostUIHandler
*) m_uiHandler
;
1181 desc
= "IDocHostUIHandler";
1187 HRESULT
DocHostUIHandler::ShowContextMenu(DWORD dwID
, POINT
*ppt
,
1188 IUnknown
*pcmdtReserved
,
1189 IDispatch
*pdispReserved
)
1193 wxUnusedVar(pcmdtReserved
);
1194 wxUnusedVar(pdispReserved
);
1198 HRESULT
DocHostUIHandler::GetHostInfo(DOCHOSTUIINFO
*pInfo
)
1200 //don't show 3d border and ebales themes.
1201 pInfo
->dwFlags
= pInfo
->dwFlags
| DOCHOSTUIFLAG_NO3DBORDER
| DOCHOSTUIFLAG_THEME
;
1205 HRESULT
DocHostUIHandler::ShowUI(DWORD dwID
,
1206 IOleInPlaceActiveObject
*pActiveObject
,
1207 IOleCommandTarget
*pCommandTarget
,
1208 IOleInPlaceFrame
*pFrame
,
1209 IOleInPlaceUIWindow
*pDoc
)
1212 wxUnusedVar(pActiveObject
);
1213 wxUnusedVar(pCommandTarget
);
1214 wxUnusedVar(pFrame
);
1219 HRESULT
DocHostUIHandler::HideUI(void)
1224 HRESULT
DocHostUIHandler::UpdateUI(void)
1229 HRESULT
DocHostUIHandler::EnableModeless(BOOL fEnable
)
1231 wxUnusedVar(fEnable
);
1235 HRESULT
DocHostUIHandler::OnDocWindowActivate(BOOL fActivate
)
1237 wxUnusedVar(fActivate
);
1241 HRESULT
DocHostUIHandler::OnFrameWindowActivate(BOOL fActivate
)
1243 wxUnusedVar(fActivate
);
1247 HRESULT
DocHostUIHandler::ResizeBorder(LPCRECT prcBorder
,
1248 IOleInPlaceUIWindow
*pUIWindow
,
1251 wxUnusedVar(prcBorder
);
1252 wxUnusedVar(pUIWindow
);
1253 wxUnusedVar(fFrameWindow
);
1257 HRESULT
DocHostUIHandler::TranslateAccelerator(LPMSG lpMsg
,
1258 const GUID
*pguidCmdGroup
,
1261 if(lpMsg
&& lpMsg
->message
== WM_KEYDOWN
)
1264 if((GetKeyState(VK_CONTROL
) & 0x8000 ))
1266 //skip CTRL-N, CTRL-F and CTRL-P
1267 if(lpMsg
->wParam
== 'N' || lpMsg
->wParam
== 'P' || lpMsg
->wParam
== 'F')
1273 if(lpMsg
->wParam
== VK_F5
)
1279 wxUnusedVar(pguidCmdGroup
);
1280 wxUnusedVar(nCmdID
);
1284 HRESULT
DocHostUIHandler::GetOptionKeyPath(LPOLESTR
*pchKey
,DWORD dw
)
1286 wxUnusedVar(pchKey
);
1291 HRESULT
DocHostUIHandler::GetDropTarget(IDropTarget
*pDropTarget
,
1292 IDropTarget
**ppDropTarget
)
1294 wxUnusedVar(pDropTarget
);
1295 wxUnusedVar(ppDropTarget
);
1299 HRESULT
DocHostUIHandler::GetExternal(IDispatch
**ppDispatch
)
1301 wxUnusedVar(ppDispatch
);
1305 HRESULT
DocHostUIHandler::TranslateUrl(DWORD dwTranslate
,
1307 OLECHAR
**ppchURLOut
)
1309 wxUnusedVar(dwTranslate
);
1310 wxUnusedVar(pchURLIn
);
1311 wxUnusedVar(ppchURLOut
);
1315 HRESULT
DocHostUIHandler::FilterDataObject(IDataObject
*pDO
, IDataObject
**ppDORet
)
1318 wxUnusedVar(ppDORet
);
1322 BEGIN_IID_TABLE(DocHostUIHandler
)
1324 ADD_RAW_IID(wxIID_IDocHostUIHandler
)
1327 IMPLEMENT_IUNKNOWN_METHODS(DocHostUIHandler
)
1329 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE