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
;
87 m_container
= new wxIEContainer(this, IID_IWebBrowser2
, m_webBrowser
, m_uiHandler
);
89 EnableControlFeature(21 /* FEATURE_DISABLE_NAVIGATION_SOUNDS */);
95 wxWebViewIE::~wxWebViewIE()
97 for(unsigned int i
= 0; i
< m_factories
.size(); i
++)
99 m_factories
[i
]->Release();
103 void wxWebViewIE::LoadURL(const wxString
& url
)
105 m_ie
.CallMethod("Navigate", wxConvertStringToOle(url
));
108 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
110 BSTR bstr
= SysAllocString(OLESTR(""));
111 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
112 if (psaStrings
!= NULL
)
115 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
117 param
->bstrVal
= bstr
;
119 hr
= SafeArrayUnaccessData(psaStrings
);
121 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
126 document
->write(psaStrings
);
129 SafeArrayDestroy(psaStrings
);
131 bstr
= SysAllocString(html
.wc_str());
133 // Creates a new one-dimensional array
134 psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
135 if (psaStrings
!= NULL
)
137 hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
139 param
->bstrVal
= bstr
;
140 hr
= SafeArrayUnaccessData(psaStrings
);
142 document
= GetDocument();
147 document
->write(psaStrings
);
149 // SafeArrayDestroy calls SysFreeString for each BSTR
150 SafeArrayDestroy(psaStrings
);
152 //We send the events when we are done to mimic webkit
154 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
155 GetId(), baseUrl
, "");
156 event
.SetEventObject(this);
157 HandleWindowEvent(event
);
159 //Document complete event
160 event
.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED
);
161 event
.SetEventObject(this);
162 HandleWindowEvent(event
);
166 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
171 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL during clear");
175 wxString
wxWebViewIE::GetPageSource() const
177 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
181 wxCOMPtr
<IHTMLElement
> bodyTag
;
182 wxCOMPtr
<IHTMLElement
> htmlTag
;
184 HRESULT hr
= document
->get_body(&bodyTag
);
187 hr
= bodyTag
->get_parentElement(&htmlTag
);
191 htmlTag
->get_outerHTML(&bstr
);
192 source
= wxString(bstr
);
203 wxWebViewZoom
wxWebViewIE::GetZoom() const
207 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
208 return GetIEOpticalZoom();
209 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
210 return GetIETextZoom();
215 //Dummy return to stop compiler warnings
216 return wxWEB_VIEW_ZOOM_MEDIUM
;
220 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
224 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
225 SetIEOpticalZoom(zoom
);
227 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
235 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level
)
237 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
238 //is 0 to 4 so the check is unnecessary, these match exactly with the
241 VariantInit (&zoomVariant
);
242 V_VT(&zoomVariant
) = VT_I4
;
243 V_I4(&zoomVariant
) = level
;
248 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
249 OLECMDEXECOPT_DONTPROMPTUSER
,
251 wxASSERT(result
== S_OK
);
254 wxWebViewZoom
wxWebViewIE::GetIETextZoom() const
257 VariantInit (&zoomVariant
);
258 V_VT(&zoomVariant
) = VT_I4
;
263 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
264 OLECMDEXECOPT_DONTPROMPTUSER
,
266 wxASSERT(result
== S_OK
);
268 //We can safely cast here as we know that the range matches our enum
269 return static_cast<wxWebViewZoom
>(V_I4(&zoomVariant
));
272 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level
)
274 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
275 //is 10 to 1000 so the check is unnecessary
277 VariantInit (&zoomVariant
);
278 V_VT(&zoomVariant
) = VT_I4
;
280 //We make a somewhat arbitray map here, taken from values used by webkit
283 case wxWEB_VIEW_ZOOM_TINY
:
284 V_I4(&zoomVariant
) = 60;
286 case wxWEB_VIEW_ZOOM_SMALL
:
287 V_I4(&zoomVariant
) = 80;
289 case wxWEB_VIEW_ZOOM_MEDIUM
:
290 V_I4(&zoomVariant
) = 100;
292 case wxWEB_VIEW_ZOOM_LARGE
:
293 V_I4(&zoomVariant
) = 130;
295 case wxWEB_VIEW_ZOOM_LARGEST
:
296 V_I4(&zoomVariant
) = 160;
305 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
306 OLECMDEXECOPT_DODEFAULT
,
309 wxASSERT(result
== S_OK
);
312 wxWebViewZoom
wxWebViewIE::GetIEOpticalZoom() const
315 VariantInit (&zoomVariant
);
316 V_VT(&zoomVariant
) = VT_I4
;
321 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
322 OLECMDEXECOPT_DODEFAULT
, NULL
,
324 wxASSERT(result
== S_OK
);
326 const int zoom
= V_I4(&zoomVariant
);
328 //We make a somewhat arbitray map here, taken from values used by webkit
331 return wxWEB_VIEW_ZOOM_TINY
;
333 else if (zoom
> 65 && zoom
<= 90)
335 return wxWEB_VIEW_ZOOM_SMALL
;
337 else if (zoom
> 90 && zoom
<= 115)
339 return wxWEB_VIEW_ZOOM_MEDIUM
;
341 else if (zoom
> 115 && zoom
<= 145)
343 return wxWEB_VIEW_ZOOM_LARGE
;
345 else /*if (zoom > 145) */ //Using else removes a compiler warning
347 return wxWEB_VIEW_ZOOM_LARGEST
;
351 void wxWebViewIE::SetZoomType(wxWebViewZoomType type
)
356 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
361 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type
) const
363 //IE 6 and below only support text zoom, so check the registry to see what
364 //version we actually have
365 wxRegKey
key(wxRegKey::HKLM
, "Software\\Microsoft\\Internet Explorer");
367 key
.QueryValue("Version", value
);
369 long version
= wxAtoi(value
.Left(1));
370 if(version
<= 6 && type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
376 void wxWebViewIE::Print()
378 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
379 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
382 bool wxWebViewIE::CanGoBack() const
385 return m_historyPosition
> 0;
390 bool wxWebViewIE::CanGoForward() const
393 return m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1;
398 void wxWebViewIE::LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
)
401 for(unsigned int i
= 0; i
< m_historyList
.size(); i
++)
403 //We compare the actual pointers to find the correct item
404 if(m_historyList
[i
].get() == item
.get())
407 wxASSERT_MSG(pos
!= static_cast<int>(m_historyList
.size()),
408 "invalid history item");
409 m_historyLoadingFromList
= true;
410 LoadURL(item
->GetUrl());
411 m_historyPosition
= pos
;
414 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetBackwardHistory()
416 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > backhist
;
417 //As we don't have std::copy or an iterator constructor in the wxwidgets
418 //native vector we construct it by hand
419 for(int i
= 0; i
< m_historyPosition
; i
++)
421 backhist
.push_back(m_historyList
[i
]);
426 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetForwardHistory()
428 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forwardhist
;
429 //As we don't have std::copy or an iterator constructor in the wxwidgets
430 //native vector we construct it by hand
431 for(int i
= m_historyPosition
+ 1; i
< static_cast<int>(m_historyList
.size()); i
++)
433 forwardhist
.push_back(m_historyList
[i
]);
438 void wxWebViewIE::GoBack()
440 LoadHistoryItem(m_historyList
[m_historyPosition
- 1]);
443 void wxWebViewIE::GoForward()
445 LoadHistoryItem(m_historyList
[m_historyPosition
+ 1]);
448 void wxWebViewIE::Stop()
450 m_ie
.CallMethod("Stop");
453 void wxWebViewIE::ClearHistory()
455 m_historyList
.clear();
456 m_historyPosition
= -1;
459 void wxWebViewIE::EnableHistory(bool enable
)
461 m_historyEnabled
= enable
;
462 m_historyList
.clear();
463 m_historyPosition
= -1;
466 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
470 V_VT(&level
) = VT_I2
;
474 case wxWEB_VIEW_RELOAD_DEFAULT
:
475 V_I2(&level
) = REFRESH_NORMAL
;
477 case wxWEB_VIEW_RELOAD_NO_CACHE
:
478 V_I2(&level
) = REFRESH_COMPLETELY
;
481 wxFAIL_MSG("Unexpected reload type");
484 m_webBrowser
->Refresh2(&level
);
487 bool wxWebViewIE::IsOfflineMode()
489 wxVariant out
= m_ie
.GetProperty("Offline");
491 wxASSERT(out
.GetType() == "bool");
493 return out
.GetBool();
496 void wxWebViewIE::SetOfflineMode(bool offline
)
498 // FIXME: the wxWidgets docs do not really document what the return
499 // parameter of PutProperty is
503 m_ie
.PutProperty("Offline", (offline
?
509 bool wxWebViewIE::IsBusy() const
511 if (m_isBusy
) return true;
513 wxVariant out
= m_ie
.GetProperty("Busy");
515 wxASSERT(out
.GetType() == "bool");
517 return out
.GetBool();
520 wxString
wxWebViewIE::GetCurrentURL() const
522 wxVariant out
= m_ie
.GetProperty("LocationURL");
524 wxASSERT(out
.GetType() == "string");
525 return out
.GetString();
528 wxString
wxWebViewIE::GetCurrentTitle() const
530 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
535 document
->get_nameProp(&title
);
536 return wxString(title
);
544 bool wxWebViewIE::CanCut() const
546 return CanExecCommand("Cut");
549 bool wxWebViewIE::CanCopy() const
551 return CanExecCommand("Copy");
554 bool wxWebViewIE::CanPaste() const
556 return CanExecCommand("Paste");
559 void wxWebViewIE::Cut()
564 void wxWebViewIE::Copy()
569 void wxWebViewIE::Paste()
571 ExecCommand("Paste");
574 bool wxWebViewIE::CanUndo() const
576 return CanExecCommand("Undo");
579 bool wxWebViewIE::CanRedo() const
581 return CanExecCommand("Redo");
584 void wxWebViewIE::Undo()
589 void wxWebViewIE::Redo()
594 void wxWebViewIE::SetEditable(bool enable
)
596 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
601 document
->put_designMode(SysAllocString(L
"On"));
603 document
->put_designMode(SysAllocString(L
"Off"));
608 bool wxWebViewIE::IsEditable() const
610 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
615 document
->get_designMode(&mode
);
616 if(wxString(mode
) == "On")
627 void wxWebViewIE::SelectAll()
629 ExecCommand("SelectAll");
632 bool wxWebViewIE::HasSelection() const
634 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
638 wxCOMPtr
<IHTMLSelectionObject
> selection
;
640 HRESULT hr
= document
->get_selection(&selection
);
644 selection
->get_type(&type
);
645 sel
= wxString(type
);
647 return sel
!= "None";
655 void wxWebViewIE::DeleteSelection()
657 ExecCommand("Delete");
660 wxString
wxWebViewIE::GetSelectedText() const
662 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
666 wxCOMPtr
<IHTMLSelectionObject
> selection
;
668 HRESULT hr
= document
->get_selection(&selection
);
671 wxCOMPtr
<IDispatch
> disrange
;
672 hr
= selection
->createRange(&disrange
);
675 wxCOMPtr
<IHTMLTxtRange
> range
;
676 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
680 range
->get_text(&text
);
681 selected
= wxString(text
);
693 wxString
wxWebViewIE::GetSelectedSource() const
695 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
699 wxCOMPtr
<IHTMLSelectionObject
> selection
;
701 HRESULT hr
= document
->get_selection(&selection
);
704 wxCOMPtr
<IDispatch
> disrange
;
705 hr
= selection
->createRange(&disrange
);
708 wxCOMPtr
<IHTMLTxtRange
> range
;
709 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
713 range
->get_htmlText(&text
);
714 selected
= wxString(text
);
726 void wxWebViewIE::ClearSelection()
728 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
732 wxCOMPtr
<IHTMLSelectionObject
> selection
;
734 HRESULT hr
= document
->get_selection(&selection
);
742 wxString
wxWebViewIE::GetPageText() const
744 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
749 wxCOMPtr
<IHTMLElement
> body
;
750 HRESULT hr
= document
->get_body(&body
);
754 body
->get_innerText(&out
);
755 text
= wxString(out
);
765 void wxWebViewIE::RunScript(const wxString
& javascript
)
767 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
771 wxCOMPtr
<IHTMLWindow2
> window
;
772 wxString language
= "javascript";
773 HRESULT hr
= document
->get_parentWindow(&window
);
778 V_VT(&level
) = VT_EMPTY
;
779 window
->execScript(SysAllocString(javascript
.wc_str()),
780 SysAllocString(language
.wc_str()),
786 void wxWebViewIE::RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
)
788 wxDynamicLibrary
urlMon(wxT("urlmon.dll"));
789 if(urlMon
.HasSymbol(wxT("CoInternetGetSession")))
791 typedef HRESULT (WINAPI
*CoInternetGetSession_t
)(DWORD
, wxIInternetSession
**, DWORD
);
792 wxDYNLIB_FUNCTION(CoInternetGetSession_t
, CoInternetGetSession
, urlMon
);
794 ClassFactory
* cf
= new ClassFactory(handler
);
795 wxIInternetSession
* session
;
796 HRESULT res
= (*pfnCoInternetGetSession
)(0, &session
, 0);
799 wxFAIL_MSG("Could not retrive internet session");
802 HRESULT hr
= session
->RegisterNameSpace(cf
, CLSID_FileProtocol
,
803 handler
->GetName().wc_str(),
807 wxFAIL_MSG("Could not register protocol");
809 m_factories
.push_back(cf
);
813 wxFAIL_MSG("urlmon does not contain CoInternetGetSession");
817 bool wxWebViewIE::CanExecCommand(wxString command
) const
819 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
823 VARIANT_BOOL enabled
;
825 document
->queryCommandEnabled(SysAllocString(command
.wc_str()), &enabled
);
827 return (enabled
== VARIANT_TRUE
);
836 void wxWebViewIE::ExecCommand(wxString command
)
838 wxCOMPtr
<IHTMLDocument2
> document(GetDocument());
842 document
->execCommand(SysAllocString(command
.wc_str()), VARIANT_FALSE
, VARIANT(), NULL
);
846 wxCOMPtr
<IHTMLDocument2
> wxWebViewIE::GetDocument() const
848 wxCOMPtr
<IDispatch
> dispatch
;
849 wxCOMPtr
<IHTMLDocument2
> document
;
850 HRESULT result
= m_webBrowser
->get_Document(&dispatch
);
851 if(dispatch
&& SUCCEEDED(result
))
853 //document is set to null automatically if the interface isn't supported
854 dispatch
->QueryInterface(IID_IHTMLDocument2
, (void**)&document
);
859 bool wxWebViewIE::EnableControlFeature(long flag
, bool enable
)
861 #if wxUSE_DYNLIB_CLASS
863 wxDynamicLibrary
urlMon(wxT("urlmon.dll"));
864 if( urlMon
.IsLoaded() &&
865 urlMon
.HasSymbol("CoInternetSetFeatureEnabled") &&
866 urlMon
.HasSymbol("CoInternetIsFeatureEnabled"))
868 typedef HRESULT (WINAPI
*CoInternetSetFeatureEnabled_t
)(DWORD
, DWORD
, BOOL
);
869 typedef HRESULT (WINAPI
*CoInternetIsFeatureEnabled_t
)(DWORD
, DWORD
);
871 wxDYNLIB_FUNCTION(CoInternetSetFeatureEnabled_t
, CoInternetSetFeatureEnabled
, urlMon
);
872 wxDYNLIB_FUNCTION(CoInternetIsFeatureEnabled_t
, CoInternetIsFeatureEnabled
, urlMon
);
874 HRESULT hr
= (*pfnCoInternetIsFeatureEnabled
)(flag
,
875 0x2 /* SET_FEATURE_ON_PROCESS */);
876 if((hr
== S_OK
&& enable
) || (hr
== S_FALSE
&& !enable
))
879 hr
= (*pfnCoInternetSetFeatureEnabled
)(flag
,
880 0x2/* SET_FEATURE_ON_PROCESS */,
881 (enable
? TRUE
: FALSE
));
884 wxLogApiError(wxT("CoInternetSetFeatureEnabled"), hr
);
894 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
897 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
899 if (m_webBrowser
== NULL
) return;
901 switch (evt
.GetDispatchId())
903 case DISPID_BEFORENAVIGATE2
:
907 wxString url
= evt
[1].GetString();
908 wxString target
= evt
[3].GetString();
910 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
911 GetId(), url
, target
);
913 //skip empty javascript events.
914 if(url
== "javascript:\"\"" && target
.IsEmpty())
920 event
.SetEventObject(this);
921 HandleWindowEvent(event
);
924 if (!event
.IsAllowed())
926 wxActiveXEventNativeMSW
* nativeParams
=
927 evt
.GetNativeParameters();
928 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
931 // at this point, either the navigation event has been cancelled
932 // and we're not busy, either it was accepted and IWebBrowser2's
933 // Busy property will be true; so we don't need our override
940 case DISPID_NAVIGATECOMPLETE2
:
942 wxString url
= evt
[1].GetString();
943 // TODO: set target parameter if possible
944 wxString target
= wxEmptyString
;
945 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
946 GetId(), url
, target
);
947 event
.SetEventObject(this);
948 HandleWindowEvent(event
);
952 case DISPID_PROGRESSCHANGE
:
958 case DISPID_DOCUMENTCOMPLETE
:
960 //Only send a complete even if we are actually finished, this brings
961 //the event in to line with webkit
963 m_webBrowser
->get_ReadyState( &rs
);
964 if(rs
!= READYSTATE_COMPLETE
)
967 wxString url
= evt
[1].GetString();
969 //As we are complete we also add to the history list, but not if the
970 //page is not the main page, ie it is a subframe
971 //We also have to check if we are loading a file:// url, if so we
972 //need to change the comparison as ie passes back a different style
974 if(m_historyEnabled
&& !m_historyLoadingFromList
&&
975 (url
== GetCurrentURL() ||
976 (GetCurrentURL().substr(0, 4) == "file" &&
977 wxFileSystem::URLToFileName(GetCurrentURL()).GetFullPath() == url
)))
979 //If we are not at the end of the list, then erase everything
980 //between us and the end before adding the new page
981 if(m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1)
983 m_historyList
.erase(m_historyList
.begin() + m_historyPosition
+ 1,
984 m_historyList
.end());
986 wxSharedPtr
<wxWebViewHistoryItem
> item(new wxWebViewHistoryItem(url
, GetCurrentTitle()));
987 m_historyList
.push_back(item
);
990 //Reset as we are done now
991 m_historyLoadingFromList
= false;
992 // TODO: set target parameter if possible
993 wxString target
= wxEmptyString
;
994 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
996 event
.SetEventObject(this);
997 HandleWindowEvent(event
);
1001 case DISPID_STATUSTEXTCHANGE
:
1006 case DISPID_TITLECHANGE
:
1008 wxString title
= evt
[0].GetString();
1010 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
1011 GetId(), GetCurrentURL(), "");
1012 event
.SetString(title
);
1013 event
.SetEventObject(this);
1014 HandleWindowEvent(event
);
1018 case DISPID_NAVIGATEERROR
:
1020 wxWebViewNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
1021 wxString errorCode
= "?";
1022 switch (evt
[3].GetLong())
1024 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
1025 errorCode
= "INET_E_INVALID_URL";
1026 errorType
= wxWEB_NAV_ERR_REQUEST
;
1028 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
1029 errorCode
= "INET_E_NO_SESSION";
1030 errorType
= wxWEB_NAV_ERR_CONNECTION
;
1032 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
1033 errorCode
= "INET_E_CANNOT_CONNECT";
1034 errorType
= wxWEB_NAV_ERR_CONNECTION
;
1036 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
1037 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
1038 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
1040 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
1041 errorCode
= "INET_E_OBJECT_NOT_FOUND";
1042 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
1044 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
1045 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
1046 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
1048 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
1049 errorCode
= "INET_E_DOWNLOAD_FAILURE";
1050 errorType
= wxWEB_NAV_ERR_CONNECTION
;
1052 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
1053 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
1054 errorType
= wxWEB_NAV_ERR_AUTH
;
1056 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
1057 errorCode
= "INET_E_NO_VALID_MEDIA";
1058 errorType
= wxWEB_NAV_ERR_REQUEST
;
1060 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
1061 errorCode
= "INET_E_CONNECTION_TIMEOUT";
1062 errorType
= wxWEB_NAV_ERR_CONNECTION
;
1064 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
1065 errorCode
= "INET_E_INVALID_REQUEST";
1066 errorType
= wxWEB_NAV_ERR_REQUEST
;
1068 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
1069 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
1070 errorType
= wxWEB_NAV_ERR_REQUEST
;
1072 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
1073 errorCode
= "INET_E_SECURITY_PROBLEM";
1074 errorType
= wxWEB_NAV_ERR_SECURITY
;
1076 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
1077 errorCode
= "INET_E_CANNOT_LOAD_DATA";
1078 errorType
= wxWEB_NAV_ERR_OTHER
;
1080 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
1081 // CoCreateInstance will return an error code if this happens,
1082 // we'll handle this above.
1085 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
1086 errorCode
= "INET_E_REDIRECT_FAILED";
1087 errorType
= wxWEB_NAV_ERR_OTHER
;
1089 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
1090 errorCode
= "INET_E_REDIRECT_TO_DIR";
1091 errorType
= wxWEB_NAV_ERR_REQUEST
;
1093 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
1094 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
1095 errorType
= wxWEB_NAV_ERR_OTHER
;
1097 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
1098 errorCode
= "INET_E_USE_EXTEND_BINDING";
1099 errorType
= wxWEB_NAV_ERR_OTHER
;
1101 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
1102 errorCode
= "INET_E_TERMINATED_BIND";
1103 errorType
= wxWEB_NAV_ERR_OTHER
;
1105 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
1106 errorCode
= "INET_E_INVALID_CERTIFICATE";
1107 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
1109 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
1110 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
1111 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
1113 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
1114 // cancel request cancelled...
1115 errorCode
= "INET_E_RESULT_DISPATCHED";
1116 errorType
= wxWEB_NAV_ERR_OTHER
;
1118 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
1119 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
1120 errorType
= wxWEB_NAV_ERR_SECURITY
;
1122 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
1123 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
1124 errorType
= wxWEB_NAV_ERR_SECURITY
;
1126 case INET_E_CODE_INSTALL_SUPPRESSED
:
1127 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
1128 errorType
= wxWEB_NAV_ERR_SECURITY
;
1132 wxString url
= evt
[1].GetString();
1133 wxString target
= evt
[2].GetString();
1134 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
1136 event
.SetEventObject(this);
1137 event
.SetInt(errorType
);
1138 event
.SetString(errorCode
);
1139 HandleWindowEvent(event
);
1142 case DISPID_NEWWINDOW3
:
1144 wxString url
= evt
[4].GetString();
1146 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
1147 GetId(), url
, wxEmptyString
);
1148 event
.SetEventObject(this);
1149 HandleWindowEvent(event
);
1151 //We always cancel this event otherwise an Internet Exporer window
1152 //is opened for the url
1153 wxActiveXEventNativeMSW
* nativeParams
= evt
.GetNativeParameters();
1154 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[3]) = VARIANT_TRUE
;
1162 VirtualProtocol::VirtualProtocol(wxSharedPtr
<wxWebViewHandler
> handler
)
1165 m_handler
= handler
;
1168 BEGIN_IID_TABLE(VirtualProtocol
)
1170 ADD_RAW_IID(wxIID_IInternetProtocolRoot
)
1171 ADD_RAW_IID(wxIID_IInternetProtocol
)
1174 IMPLEMENT_IUNKNOWN_METHODS(VirtualProtocol
)
1176 HRESULT
VirtualProtocol::Start(LPCWSTR szUrl
, wxIInternetProtocolSink
*pOIProtSink
,
1177 wxIInternetBindInfo
*pOIBindInfo
, DWORD grfPI
,
1178 HANDLE_PTR dwReserved
)
1181 wxUnusedVar(pOIBindInfo
);
1183 wxUnusedVar(dwReserved
);
1184 m_protocolSink
= pOIProtSink
;
1186 //We get the file itself from the protocol handler
1187 m_file
= m_handler
->GetFile(szUrl
);
1191 return INET_E_RESOURCE_NOT_FOUND
;
1193 //We return the stream length for current and total size as we can always
1194 //read the whole file from the stream
1195 wxFileOffset length
= m_file
->GetStream()->GetLength();
1196 m_protocolSink
->ReportData(wxBSCF_FIRSTDATANOTIFICATION
|
1197 wxBSCF_DATAFULLYAVAILABLE
|
1198 wxBSCF_LASTDATANOTIFICATION
,
1203 HRESULT
VirtualProtocol::Read(void *pv
, ULONG cb
, ULONG
*pcbRead
)
1205 //If the file is null we return false to indicte it is finished
1209 wxStreamError err
= m_file
->GetStream()->Read(pv
, cb
).GetLastError();
1210 *pcbRead
= m_file
->GetStream()->LastRead();
1212 if(err
== wxSTREAM_NO_ERROR
)
1217 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1219 //As we are not eof there is more data
1222 else if(err
== wxSTREAM_EOF
)
1225 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1226 //We are eof and so finished
1229 else if(err
== wxSTREAM_READ_ERROR
)
1232 return INET_E_DOWNLOAD_FAILURE
;
1236 //Dummy return to surpress a compiler warning
1238 return INET_E_DOWNLOAD_FAILURE
;
1242 BEGIN_IID_TABLE(ClassFactory
)
1244 ADD_IID(ClassFactory
)
1247 IMPLEMENT_IUNKNOWN_METHODS(ClassFactory
)
1249 HRESULT
ClassFactory::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
,
1253 return CLASS_E_NOAGGREGATION
;
1254 VirtualProtocol
* vp
= new VirtualProtocol(m_handler
);
1256 HRESULT hr
= vp
->QueryInterface(riid
, ppvObject
);
1262 STDMETHODIMP
ClassFactory::LockServer(BOOL fLock
)
1268 wxIEContainer::wxIEContainer(wxWindow
*parent
, REFIID iid
, IUnknown
*pUnk
,
1269 DocHostUIHandler
* uiHandler
) :
1270 wxActiveXContainer(parent
,iid
,pUnk
)
1272 m_uiHandler
= uiHandler
;
1275 wxIEContainer::~wxIEContainer()
1279 bool wxIEContainer::QueryClientSiteInterface(REFIID iid
, void **_interface
,
1282 if (m_uiHandler
&& IsEqualIID(iid
, wxIID_IDocHostUIHandler
))
1284 *_interface
= (IUnknown
*) (wxIDocHostUIHandler
*) m_uiHandler
;
1285 desc
= "IDocHostUIHandler";
1291 HRESULT
DocHostUIHandler::ShowContextMenu(DWORD dwID
, POINT
*ppt
,
1292 IUnknown
*pcmdtReserved
,
1293 IDispatch
*pdispReserved
)
1297 wxUnusedVar(pcmdtReserved
);
1298 wxUnusedVar(pdispReserved
);
1302 HRESULT
DocHostUIHandler::GetHostInfo(DOCHOSTUIINFO
*pInfo
)
1304 //don't show 3d border and enable themes.
1305 pInfo
->dwFlags
= pInfo
->dwFlags
| DOCHOSTUIFLAG_NO3DBORDER
| DOCHOSTUIFLAG_THEME
;
1309 HRESULT
DocHostUIHandler::ShowUI(DWORD dwID
,
1310 IOleInPlaceActiveObject
*pActiveObject
,
1311 IOleCommandTarget
*pCommandTarget
,
1312 IOleInPlaceFrame
*pFrame
,
1313 IOleInPlaceUIWindow
*pDoc
)
1316 wxUnusedVar(pActiveObject
);
1317 wxUnusedVar(pCommandTarget
);
1318 wxUnusedVar(pFrame
);
1323 HRESULT
DocHostUIHandler::HideUI(void)
1328 HRESULT
DocHostUIHandler::UpdateUI(void)
1333 HRESULT
DocHostUIHandler::EnableModeless(BOOL fEnable
)
1335 wxUnusedVar(fEnable
);
1339 HRESULT
DocHostUIHandler::OnDocWindowActivate(BOOL fActivate
)
1341 wxUnusedVar(fActivate
);
1345 HRESULT
DocHostUIHandler::OnFrameWindowActivate(BOOL fActivate
)
1347 wxUnusedVar(fActivate
);
1351 HRESULT
DocHostUIHandler::ResizeBorder(LPCRECT prcBorder
,
1352 IOleInPlaceUIWindow
*pUIWindow
,
1355 wxUnusedVar(prcBorder
);
1356 wxUnusedVar(pUIWindow
);
1357 wxUnusedVar(fFrameWindow
);
1361 HRESULT
DocHostUIHandler::TranslateAccelerator(LPMSG lpMsg
,
1362 const GUID
*pguidCmdGroup
,
1365 if(lpMsg
&& lpMsg
->message
== WM_KEYDOWN
)
1368 if((GetKeyState(VK_CONTROL
) & 0x8000 ))
1370 //skip the accelerators used by the control
1371 switch(lpMsg
->wParam
)
1382 if(lpMsg
->wParam
== VK_F5
)
1388 wxUnusedVar(pguidCmdGroup
);
1389 wxUnusedVar(nCmdID
);
1393 HRESULT
DocHostUIHandler::GetOptionKeyPath(LPOLESTR
*pchKey
,DWORD dw
)
1395 wxUnusedVar(pchKey
);
1400 HRESULT
DocHostUIHandler::GetDropTarget(IDropTarget
*pDropTarget
,
1401 IDropTarget
**ppDropTarget
)
1403 wxUnusedVar(pDropTarget
);
1404 wxUnusedVar(ppDropTarget
);
1408 HRESULT
DocHostUIHandler::GetExternal(IDispatch
**ppDispatch
)
1410 wxUnusedVar(ppDispatch
);
1414 HRESULT
DocHostUIHandler::TranslateUrl(DWORD dwTranslate
,
1416 OLECHAR
**ppchURLOut
)
1418 wxUnusedVar(dwTranslate
);
1419 wxUnusedVar(pchURLIn
);
1420 wxUnusedVar(ppchURLOut
);
1424 HRESULT
DocHostUIHandler::FilterDataObject(IDataObject
*pDO
, IDataObject
**ppDORet
)
1427 wxUnusedVar(ppDORet
);
1431 BEGIN_IID_TABLE(DocHostUIHandler
)
1433 ADD_RAW_IID(wxIID_IDocHostUIHandler
)
1436 IMPLEMENT_IUNKNOWN_METHODS(DocHostUIHandler
)
1438 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE