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
);
90 EnableControlFeature(21 /* FEATURE_DISABLE_NAVIGATION_SOUNDS */);
96 wxWebViewIE::~wxWebViewIE()
98 for(unsigned int i
= 0; i
< m_factories
.size(); i
++)
100 m_factories
[i
]->Release();
103 m_uiHandler
->Release();
106 void wxWebViewIE::LoadURL(const wxString
& url
)
108 m_ie
.CallMethod("Navigate", wxConvertStringToOle(url
));
111 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
113 BSTR bstr
= SysAllocString(OLESTR(""));
114 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
115 if (psaStrings
!= NULL
)
118 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
120 param
->bstrVal
= bstr
;
122 hr
= SafeArrayUnaccessData(psaStrings
);
124 IHTMLDocument2
* document
= GetDocument();
125 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();
143 document
->write(psaStrings
);
146 // SafeArrayDestroy calls SysFreeString for each BSTR
147 SafeArrayDestroy(psaStrings
);
149 //We send the events when we are done to mimic webkit
151 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
152 GetId(), baseUrl
, "");
153 event
.SetEventObject(this);
154 HandleWindowEvent(event
);
156 //Document complete event
157 event
.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED
);
158 event
.SetEventObject(this);
159 HandleWindowEvent(event
);
163 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
168 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL during clear");
172 wxString
wxWebViewIE::GetPageSource() const
174 IHTMLDocument2
* document
= GetDocument();
175 IHTMLElement
*bodyTag
= NULL
;
176 IHTMLElement
*htmlTag
= NULL
;
178 HRESULT hr
= document
->get_body(&bodyTag
);
181 hr
= bodyTag
->get_parentElement(&htmlTag
);
185 htmlTag
->get_outerHTML(&bstr
);
186 source
= wxString(bstr
);
196 wxWebViewZoom
wxWebViewIE::GetZoom() const
200 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
201 return GetIEOpticalZoom();
202 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
203 return GetIETextZoom();
208 //Dummy return to stop compiler warnings
209 return wxWEB_VIEW_ZOOM_MEDIUM
;
213 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
217 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
218 SetIEOpticalZoom(zoom
);
220 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
228 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level
)
230 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
231 //is 0 to 4 so the check is unnecessary, these match exactly with the
234 VariantInit (&zoomVariant
);
235 V_VT(&zoomVariant
) = VT_I4
;
236 V_I4(&zoomVariant
) = level
;
241 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
242 OLECMDEXECOPT_DONTPROMPTUSER
,
244 wxASSERT(result
== S_OK
);
247 wxWebViewZoom
wxWebViewIE::GetIETextZoom() const
250 VariantInit (&zoomVariant
);
251 V_VT(&zoomVariant
) = VT_I4
;
256 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
257 OLECMDEXECOPT_DONTPROMPTUSER
,
259 wxASSERT(result
== S_OK
);
261 //We can safely cast here as we know that the range matches our enum
262 return static_cast<wxWebViewZoom
>(V_I4(&zoomVariant
));
265 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level
)
267 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
268 //is 10 to 1000 so the check is unnecessary
270 VariantInit (&zoomVariant
);
271 V_VT(&zoomVariant
) = VT_I4
;
273 //We make a somewhat arbitray map here, taken from values used by webkit
276 case wxWEB_VIEW_ZOOM_TINY
:
277 V_I4(&zoomVariant
) = 60;
279 case wxWEB_VIEW_ZOOM_SMALL
:
280 V_I4(&zoomVariant
) = 80;
282 case wxWEB_VIEW_ZOOM_MEDIUM
:
283 V_I4(&zoomVariant
) = 100;
285 case wxWEB_VIEW_ZOOM_LARGE
:
286 V_I4(&zoomVariant
) = 130;
288 case wxWEB_VIEW_ZOOM_LARGEST
:
289 V_I4(&zoomVariant
) = 160;
298 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
299 OLECMDEXECOPT_DODEFAULT
,
302 wxASSERT(result
== S_OK
);
305 wxWebViewZoom
wxWebViewIE::GetIEOpticalZoom() const
308 VariantInit (&zoomVariant
);
309 V_VT(&zoomVariant
) = VT_I4
;
314 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
315 OLECMDEXECOPT_DODEFAULT
, NULL
,
317 wxASSERT(result
== S_OK
);
319 const int zoom
= V_I4(&zoomVariant
);
321 //We make a somewhat arbitray map here, taken from values used by webkit
324 return wxWEB_VIEW_ZOOM_TINY
;
326 else if (zoom
> 65 && zoom
<= 90)
328 return wxWEB_VIEW_ZOOM_SMALL
;
330 else if (zoom
> 90 && zoom
<= 115)
332 return wxWEB_VIEW_ZOOM_MEDIUM
;
334 else if (zoom
> 115 && zoom
<= 145)
336 return wxWEB_VIEW_ZOOM_LARGE
;
338 else /*if (zoom > 145) */ //Using else removes a compiler warning
340 return wxWEB_VIEW_ZOOM_LARGEST
;
344 void wxWebViewIE::SetZoomType(wxWebViewZoomType type
)
349 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
354 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type
) const
356 //IE 6 and below only support text zoom, so check the registry to see what
357 //version we actually have
358 wxRegKey
key(wxRegKey::HKLM
, "Software\\Microsoft\\Internet Explorer");
360 key
.QueryValue("Version", value
);
362 long version
= wxAtoi(value
.Left(1));
363 if(version
<= 6 && type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
369 void wxWebViewIE::Print()
371 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
372 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
375 bool wxWebViewIE::CanGoBack() const
378 return m_historyPosition
> 0;
383 bool wxWebViewIE::CanGoForward() const
386 return m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1;
391 void wxWebViewIE::LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
)
394 for(unsigned int i
= 0; i
< m_historyList
.size(); i
++)
396 //We compare the actual pointers to find the correct item
397 if(m_historyList
[i
].get() == item
.get())
400 wxASSERT_MSG(pos
!= static_cast<int>(m_historyList
.size()),
401 "invalid history item");
402 m_historyLoadingFromList
= true;
403 LoadURL(item
->GetUrl());
404 m_historyPosition
= pos
;
407 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetBackwardHistory()
409 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > backhist
;
410 //As we don't have std::copy or an iterator constructor in the wxwidgets
411 //native vector we construct it by hand
412 for(int i
= 0; i
< m_historyPosition
; i
++)
414 backhist
.push_back(m_historyList
[i
]);
419 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetForwardHistory()
421 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forwardhist
;
422 //As we don't have std::copy or an iterator constructor in the wxwidgets
423 //native vector we construct it by hand
424 for(int i
= m_historyPosition
+ 1; i
< static_cast<int>(m_historyList
.size()); i
++)
426 forwardhist
.push_back(m_historyList
[i
]);
431 void wxWebViewIE::GoBack()
433 LoadHistoryItem(m_historyList
[m_historyPosition
- 1]);
436 void wxWebViewIE::GoForward()
438 LoadHistoryItem(m_historyList
[m_historyPosition
+ 1]);
441 void wxWebViewIE::Stop()
443 m_ie
.CallMethod("Stop");
446 void wxWebViewIE::ClearHistory()
448 m_historyList
.clear();
449 m_historyPosition
= -1;
452 void wxWebViewIE::EnableHistory(bool enable
)
454 m_historyEnabled
= enable
;
455 m_historyList
.clear();
456 m_historyPosition
= -1;
459 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
463 V_VT(&level
) = VT_I2
;
467 case wxWEB_VIEW_RELOAD_DEFAULT
:
468 V_I2(&level
) = REFRESH_NORMAL
;
470 case wxWEB_VIEW_RELOAD_NO_CACHE
:
471 V_I2(&level
) = REFRESH_COMPLETELY
;
474 wxFAIL_MSG("Unexpected reload type");
477 m_webBrowser
->Refresh2(&level
);
480 bool wxWebViewIE::IsOfflineMode()
482 wxVariant out
= m_ie
.GetProperty("Offline");
484 wxASSERT(out
.GetType() == "bool");
486 return out
.GetBool();
489 void wxWebViewIE::SetOfflineMode(bool offline
)
491 // FIXME: the wxWidgets docs do not really document what the return
492 // parameter of PutProperty is
496 m_ie
.PutProperty("Offline", (offline
?
502 bool wxWebViewIE::IsBusy() const
504 if (m_isBusy
) return true;
506 wxVariant out
= m_ie
.GetProperty("Busy");
508 wxASSERT(out
.GetType() == "bool");
510 return out
.GetBool();
513 wxString
wxWebViewIE::GetCurrentURL() const
515 wxVariant out
= m_ie
.GetProperty("LocationURL");
517 wxASSERT(out
.GetType() == "string");
518 return out
.GetString();
521 wxString
wxWebViewIE::GetCurrentTitle() const
523 IHTMLDocument2
* document
= GetDocument();
526 document
->get_nameProp(&title
);
528 return wxString(title
);
531 bool wxWebViewIE::CanCut() const
533 return CanExecCommand("Cut");
536 bool wxWebViewIE::CanCopy() const
538 return CanExecCommand("Copy");
540 bool wxWebViewIE::CanPaste() const
542 return CanExecCommand("Paste");
545 void wxWebViewIE::Cut()
550 void wxWebViewIE::Copy()
555 void wxWebViewIE::Paste()
557 ExecCommand("Paste");
560 bool wxWebViewIE::CanUndo() const
562 return CanExecCommand("Undo");
564 bool wxWebViewIE::CanRedo() const
566 return CanExecCommand("Redo");
569 void wxWebViewIE::Undo()
574 void wxWebViewIE::Redo()
579 void wxWebViewIE::SetEditable(bool enable
)
581 IHTMLDocument2
* document
= GetDocument();
583 document
->put_designMode(SysAllocString(L
"On"));
585 document
->put_designMode(SysAllocString(L
"Off"));
590 bool wxWebViewIE::IsEditable() const
592 IHTMLDocument2
* document
= GetDocument();
594 document
->get_designMode(&mode
);
596 if(wxString(mode
) == "On")
602 void wxWebViewIE::SelectAll()
604 ExecCommand("SelectAll");
607 bool wxWebViewIE::HasSelection() const
609 IHTMLDocument2
* document
= GetDocument();
610 IHTMLSelectionObject
* selection
;
612 HRESULT hr
= document
->get_selection(&selection
);
616 selection
->get_type(&type
);
617 sel
= wxString(type
);
618 selection
->Release();
621 return sel
!= "None";
624 void wxWebViewIE::DeleteSelection()
626 ExecCommand("Delete");
629 wxString
wxWebViewIE::GetSelectedText() const
631 IHTMLDocument2
* document
= GetDocument();
632 IHTMLSelectionObject
* selection
;
634 HRESULT hr
= document
->get_selection(&selection
);
638 hr
= selection
->createRange(&disrange
);
641 IHTMLTxtRange
* range
;
642 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
646 range
->get_text(&text
);
647 selected
= wxString(text
);
652 selection
->Release();
658 wxString
wxWebViewIE::GetSelectedSource() const
660 IHTMLDocument2
* document
= GetDocument();
661 IHTMLSelectionObject
* selection
;
663 HRESULT hr
= document
->get_selection(&selection
);
667 hr
= selection
->createRange(&disrange
);
670 IHTMLTxtRange
* range
;
671 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
675 range
->get_htmlText(&text
);
676 selected
= wxString(text
);
681 selection
->Release();
687 void wxWebViewIE::ClearSelection()
689 IHTMLDocument2
* document
= GetDocument();
690 IHTMLSelectionObject
* selection
;
692 HRESULT hr
= document
->get_selection(&selection
);
696 selection
->Release();
701 wxString
wxWebViewIE::GetPageText() const
703 IHTMLDocument2
* document
= GetDocument();
706 HRESULT hr
= document
->get_body(&body
);
710 body
->get_innerText(&out
);
711 text
= wxString(out
);
718 void wxWebViewIE::RunScript(const wxString
& javascript
)
720 IHTMLDocument2
* document
= GetDocument();
721 IHTMLWindow2
* window
;
722 wxString language
= "javascript";
723 HRESULT hr
= document
->get_parentWindow(&window
);
728 V_VT(&level
) = VT_EMPTY
;
729 window
->execScript(SysAllocString(javascript
.wc_str()),
730 SysAllocString(language
.wc_str()),
736 void wxWebViewIE::RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
)
738 wxDynamicLibrary
urlMon(wxT("urlmon.dll"));
739 if(urlMon
.HasSymbol(wxT("CoInternetGetSession")))
741 typedef HRESULT (WINAPI
*CoInternetGetSession_t
)(DWORD
, wxIInternetSession
**, DWORD
);
742 wxDYNLIB_FUNCTION(CoInternetGetSession_t
, CoInternetGetSession
, urlMon
);
744 ClassFactory
* cf
= new ClassFactory(handler
);
745 wxIInternetSession
* session
;
746 HRESULT res
= (*pfnCoInternetGetSession
)(0, &session
, 0);
749 wxFAIL_MSG("Could not retrive internet session");
752 HRESULT hr
= session
->RegisterNameSpace(cf
, CLSID_FileProtocol
,
753 handler
->GetName().wc_str(),
757 wxFAIL_MSG("Could not register protocol");
759 m_factories
.push_back(cf
);
763 wxFAIL_MSG("urlmon does not contain CoInternetGetSession");
767 bool wxWebViewIE::CanExecCommand(wxString command
) const
769 IHTMLDocument2
* document
= GetDocument();
770 VARIANT_BOOL enabled
;
772 document
->queryCommandEnabled(SysAllocString(command
.wc_str()), &enabled
);
775 return (enabled
== VARIANT_TRUE
);
778 void wxWebViewIE::ExecCommand(wxString command
)
780 IHTMLDocument2
* document
= GetDocument();
781 document
->execCommand(SysAllocString(command
.wc_str()), VARIANT_FALSE
, VARIANT(), NULL
);
785 IHTMLDocument2
* wxWebViewIE::GetDocument() const
787 wxVariant variant
= m_ie
.GetProperty("Document");
788 IHTMLDocument2
* document
= (IHTMLDocument2
*)variant
.GetVoidPtr();
795 bool wxWebViewIE::EnableControlFeature(long flag
, bool enable
)
797 #if wxUSE_DYNLIB_CLASS
799 wxDynamicLibrary
urlMon(wxT("urlmon.dll"));
800 if( urlMon
.IsLoaded() &&
801 urlMon
.HasSymbol("CoInternetSetFeatureEnabled") &&
802 urlMon
.HasSymbol("CoInternetIsFeatureEnabled"))
804 typedef HRESULT (WINAPI
*CoInternetSetFeatureEnabled_t
)(DWORD
, DWORD
, BOOL
);
805 typedef HRESULT (WINAPI
*CoInternetIsFeatureEnabled_t
)(DWORD
, DWORD
);
807 wxDYNLIB_FUNCTION(CoInternetSetFeatureEnabled_t
, CoInternetSetFeatureEnabled
, urlMon
);
808 wxDYNLIB_FUNCTION(CoInternetIsFeatureEnabled_t
, CoInternetIsFeatureEnabled
, urlMon
);
810 HRESULT hr
= (*pfnCoInternetIsFeatureEnabled
)(flag
,
811 0x2 /* SET_FEATURE_ON_PROCESS */);
812 if((hr
== S_OK
&& enable
) || (hr
== S_FALSE
&& !enable
))
815 hr
= (*pfnCoInternetSetFeatureEnabled
)(flag
,
816 0x2/* SET_FEATURE_ON_PROCESS */,
817 (enable
? TRUE
: FALSE
));
820 wxLogApiError(wxT("CoInternetSetFeatureEnabled"), hr
);
830 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
833 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
835 if (m_webBrowser
== NULL
) return;
837 switch (evt
.GetDispatchId())
839 case DISPID_BEFORENAVIGATE2
:
843 wxString url
= evt
[1].GetString();
844 wxString target
= evt
[3].GetString();
846 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
847 GetId(), url
, target
);
849 //skip empty javascript events.
850 if(url
== "javascript:\"\"" && target
.IsEmpty())
856 event
.SetEventObject(this);
857 HandleWindowEvent(event
);
860 if (!event
.IsAllowed())
862 wxActiveXEventNativeMSW
* nativeParams
=
863 evt
.GetNativeParameters();
864 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
867 // at this point, either the navigation event has been cancelled
868 // and we're not busy, either it was accepted and IWebBrowser2's
869 // Busy property will be true; so we don't need our override
876 case DISPID_NAVIGATECOMPLETE2
:
878 wxString url
= evt
[1].GetString();
879 // TODO: set target parameter if possible
880 wxString target
= wxEmptyString
;
881 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
882 GetId(), url
, target
);
883 event
.SetEventObject(this);
884 HandleWindowEvent(event
);
888 case DISPID_PROGRESSCHANGE
:
894 case DISPID_DOCUMENTCOMPLETE
:
896 //Only send a complete even if we are actually finished, this brings
897 //the event in to line with webkit
899 m_webBrowser
->get_ReadyState( &rs
);
900 if(rs
!= READYSTATE_COMPLETE
)
903 wxString url
= evt
[1].GetString();
905 //As we are complete we also add to the history list, but not if the
906 //page is not the main page, ie it is a subframe
907 //We also have to check if we are loading a file:// url, if so we
908 //need to change the comparison as ie passes back a different style
910 if(m_historyEnabled
&& !m_historyLoadingFromList
&&
911 (url
== GetCurrentURL() ||
912 (GetCurrentURL().substr(0, 4) == "file" &&
913 wxFileSystem::URLToFileName(GetCurrentURL()).GetFullPath() == url
)))
915 //If we are not at the end of the list, then erase everything
916 //between us and the end before adding the new page
917 if(m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1)
919 m_historyList
.erase(m_historyList
.begin() + m_historyPosition
+ 1,
920 m_historyList
.end());
922 wxSharedPtr
<wxWebViewHistoryItem
> item(new wxWebViewHistoryItem(url
, GetCurrentTitle()));
923 m_historyList
.push_back(item
);
926 //Reset as we are done now
927 m_historyLoadingFromList
= false;
928 // TODO: set target parameter if possible
929 wxString target
= wxEmptyString
;
930 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
932 event
.SetEventObject(this);
933 HandleWindowEvent(event
);
937 case DISPID_STATUSTEXTCHANGE
:
942 case DISPID_TITLECHANGE
:
944 wxString title
= evt
[0].GetString();
946 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
947 GetId(), GetCurrentURL(), "");
948 event
.SetString(title
);
949 event
.SetEventObject(this);
950 HandleWindowEvent(event
);
954 case DISPID_NAVIGATEERROR
:
956 wxWebViewNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
957 wxString errorCode
= "?";
958 switch (evt
[3].GetLong())
960 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
961 errorCode
= "INET_E_INVALID_URL";
962 errorType
= wxWEB_NAV_ERR_REQUEST
;
964 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
965 errorCode
= "INET_E_NO_SESSION";
966 errorType
= wxWEB_NAV_ERR_CONNECTION
;
968 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
969 errorCode
= "INET_E_CANNOT_CONNECT";
970 errorType
= wxWEB_NAV_ERR_CONNECTION
;
972 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
973 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
974 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
976 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
977 errorCode
= "INET_E_OBJECT_NOT_FOUND";
978 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
980 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
981 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
982 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
984 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
985 errorCode
= "INET_E_DOWNLOAD_FAILURE";
986 errorType
= wxWEB_NAV_ERR_CONNECTION
;
988 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
989 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
990 errorType
= wxWEB_NAV_ERR_AUTH
;
992 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
993 errorCode
= "INET_E_NO_VALID_MEDIA";
994 errorType
= wxWEB_NAV_ERR_REQUEST
;
996 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
997 errorCode
= "INET_E_CONNECTION_TIMEOUT";
998 errorType
= wxWEB_NAV_ERR_CONNECTION
;
1000 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
1001 errorCode
= "INET_E_INVALID_REQUEST";
1002 errorType
= wxWEB_NAV_ERR_REQUEST
;
1004 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
1005 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
1006 errorType
= wxWEB_NAV_ERR_REQUEST
;
1008 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
1009 errorCode
= "INET_E_SECURITY_PROBLEM";
1010 errorType
= wxWEB_NAV_ERR_SECURITY
;
1012 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
1013 errorCode
= "INET_E_CANNOT_LOAD_DATA";
1014 errorType
= wxWEB_NAV_ERR_OTHER
;
1016 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
1017 // CoCreateInstance will return an error code if this happens,
1018 // we'll handle this above.
1021 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
1022 errorCode
= "INET_E_REDIRECT_FAILED";
1023 errorType
= wxWEB_NAV_ERR_OTHER
;
1025 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
1026 errorCode
= "INET_E_REDIRECT_TO_DIR";
1027 errorType
= wxWEB_NAV_ERR_REQUEST
;
1029 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
1030 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
1031 errorType
= wxWEB_NAV_ERR_OTHER
;
1033 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
1034 errorCode
= "INET_E_USE_EXTEND_BINDING";
1035 errorType
= wxWEB_NAV_ERR_OTHER
;
1037 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
1038 errorCode
= "INET_E_TERMINATED_BIND";
1039 errorType
= wxWEB_NAV_ERR_OTHER
;
1041 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
1042 errorCode
= "INET_E_INVALID_CERTIFICATE";
1043 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
1045 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
1046 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
1047 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
1049 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
1050 // cancel request cancelled...
1051 errorCode
= "INET_E_RESULT_DISPATCHED";
1052 errorType
= wxWEB_NAV_ERR_OTHER
;
1054 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
1055 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
1056 errorType
= wxWEB_NAV_ERR_SECURITY
;
1058 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
1059 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
1060 errorType
= wxWEB_NAV_ERR_SECURITY
;
1062 case INET_E_CODE_INSTALL_SUPPRESSED
:
1063 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
1064 errorType
= wxWEB_NAV_ERR_SECURITY
;
1068 wxString url
= evt
[1].GetString();
1069 wxString target
= evt
[2].GetString();
1070 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
1072 event
.SetEventObject(this);
1073 event
.SetInt(errorType
);
1074 event
.SetString(errorCode
);
1075 HandleWindowEvent(event
);
1078 case DISPID_NEWWINDOW3
:
1080 wxString url
= evt
[4].GetString();
1082 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
1083 GetId(), url
, wxEmptyString
);
1084 event
.SetEventObject(this);
1085 HandleWindowEvent(event
);
1087 //We always cancel this event otherwise an Internet Exporer window
1088 //is opened for the url
1089 wxActiveXEventNativeMSW
* nativeParams
= evt
.GetNativeParameters();
1090 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[3]) = VARIANT_TRUE
;
1098 VirtualProtocol::VirtualProtocol(wxSharedPtr
<wxWebViewHandler
> handler
)
1101 m_handler
= handler
;
1104 BEGIN_IID_TABLE(VirtualProtocol
)
1106 ADD_RAW_IID(wxIID_IInternetProtocolRoot
)
1107 ADD_RAW_IID(wxIID_IInternetProtocol
)
1110 IMPLEMENT_IUNKNOWN_METHODS(VirtualProtocol
)
1112 HRESULT
VirtualProtocol::Start(LPCWSTR szUrl
, wxIInternetProtocolSink
*pOIProtSink
,
1113 wxIInternetBindInfo
*pOIBindInfo
, DWORD grfPI
,
1114 HANDLE_PTR dwReserved
)
1117 wxUnusedVar(pOIBindInfo
);
1119 wxUnusedVar(dwReserved
);
1120 m_protocolSink
= pOIProtSink
;
1122 //We get the file itself from the protocol handler
1123 m_file
= m_handler
->GetFile(szUrl
);
1127 return INET_E_RESOURCE_NOT_FOUND
;
1129 //We return the stream length for current and total size as we can always
1130 //read the whole file from the stream
1131 wxFileOffset length
= m_file
->GetStream()->GetLength();
1132 m_protocolSink
->ReportData(wxBSCF_FIRSTDATANOTIFICATION
|
1133 wxBSCF_DATAFULLYAVAILABLE
|
1134 wxBSCF_LASTDATANOTIFICATION
,
1139 HRESULT
VirtualProtocol::Read(void *pv
, ULONG cb
, ULONG
*pcbRead
)
1141 //If the file is null we return false to indicte it is finished
1145 wxStreamError err
= m_file
->GetStream()->Read(pv
, cb
).GetLastError();
1146 *pcbRead
= m_file
->GetStream()->LastRead();
1148 if(err
== wxSTREAM_NO_ERROR
)
1153 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1155 //As we are not eof there is more data
1158 else if(err
== wxSTREAM_EOF
)
1161 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1162 //We are eof and so finished
1165 else if(err
== wxSTREAM_READ_ERROR
)
1168 return INET_E_DOWNLOAD_FAILURE
;
1172 //Dummy return to surpress a compiler warning
1174 return INET_E_DOWNLOAD_FAILURE
;
1178 BEGIN_IID_TABLE(ClassFactory
)
1180 ADD_IID(ClassFactory
)
1183 IMPLEMENT_IUNKNOWN_METHODS(ClassFactory
)
1185 HRESULT
ClassFactory::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
,
1189 return CLASS_E_NOAGGREGATION
;
1190 VirtualProtocol
* vp
= new VirtualProtocol(m_handler
);
1192 HRESULT hr
= vp
->QueryInterface(riid
, ppvObject
);
1198 STDMETHODIMP
ClassFactory::LockServer(BOOL fLock
)
1204 wxIEContainer::wxIEContainer(wxWindow
*parent
, REFIID iid
, IUnknown
*pUnk
,
1205 DocHostUIHandler
* uiHandler
) :
1206 wxActiveXContainer(parent
,iid
,pUnk
)
1208 m_uiHandler
= uiHandler
;
1211 wxIEContainer::~wxIEContainer()
1215 bool wxIEContainer::QueryClientSiteInterface(REFIID iid
, void **_interface
,
1218 if (m_uiHandler
&& IsEqualIID(iid
, wxIID_IDocHostUIHandler
))
1220 *_interface
= (IUnknown
*) (wxIDocHostUIHandler
*) m_uiHandler
;
1221 desc
= "IDocHostUIHandler";
1227 HRESULT
DocHostUIHandler::ShowContextMenu(DWORD dwID
, POINT
*ppt
,
1228 IUnknown
*pcmdtReserved
,
1229 IDispatch
*pdispReserved
)
1233 wxUnusedVar(pcmdtReserved
);
1234 wxUnusedVar(pdispReserved
);
1238 HRESULT
DocHostUIHandler::GetHostInfo(DOCHOSTUIINFO
*pInfo
)
1240 //don't show 3d border and ebales themes.
1241 pInfo
->dwFlags
= pInfo
->dwFlags
| DOCHOSTUIFLAG_NO3DBORDER
| DOCHOSTUIFLAG_THEME
;
1245 HRESULT
DocHostUIHandler::ShowUI(DWORD dwID
,
1246 IOleInPlaceActiveObject
*pActiveObject
,
1247 IOleCommandTarget
*pCommandTarget
,
1248 IOleInPlaceFrame
*pFrame
,
1249 IOleInPlaceUIWindow
*pDoc
)
1252 wxUnusedVar(pActiveObject
);
1253 wxUnusedVar(pCommandTarget
);
1254 wxUnusedVar(pFrame
);
1259 HRESULT
DocHostUIHandler::HideUI(void)
1264 HRESULT
DocHostUIHandler::UpdateUI(void)
1269 HRESULT
DocHostUIHandler::EnableModeless(BOOL fEnable
)
1271 wxUnusedVar(fEnable
);
1275 HRESULT
DocHostUIHandler::OnDocWindowActivate(BOOL fActivate
)
1277 wxUnusedVar(fActivate
);
1281 HRESULT
DocHostUIHandler::OnFrameWindowActivate(BOOL fActivate
)
1283 wxUnusedVar(fActivate
);
1287 HRESULT
DocHostUIHandler::ResizeBorder(LPCRECT prcBorder
,
1288 IOleInPlaceUIWindow
*pUIWindow
,
1291 wxUnusedVar(prcBorder
);
1292 wxUnusedVar(pUIWindow
);
1293 wxUnusedVar(fFrameWindow
);
1297 HRESULT
DocHostUIHandler::TranslateAccelerator(LPMSG lpMsg
,
1298 const GUID
*pguidCmdGroup
,
1301 if(lpMsg
&& lpMsg
->message
== WM_KEYDOWN
)
1304 if((GetKeyState(VK_CONTROL
) & 0x8000 ))
1306 //skip CTRL-N, CTRL-F and CTRL-P
1307 if(lpMsg
->wParam
== 'N' || lpMsg
->wParam
== 'P' || lpMsg
->wParam
== 'F')
1313 if(lpMsg
->wParam
== VK_F5
)
1319 wxUnusedVar(pguidCmdGroup
);
1320 wxUnusedVar(nCmdID
);
1324 HRESULT
DocHostUIHandler::GetOptionKeyPath(LPOLESTR
*pchKey
,DWORD dw
)
1326 wxUnusedVar(pchKey
);
1331 HRESULT
DocHostUIHandler::GetDropTarget(IDropTarget
*pDropTarget
,
1332 IDropTarget
**ppDropTarget
)
1334 wxUnusedVar(pDropTarget
);
1335 wxUnusedVar(ppDropTarget
);
1339 HRESULT
DocHostUIHandler::GetExternal(IDispatch
**ppDispatch
)
1341 wxUnusedVar(ppDispatch
);
1345 HRESULT
DocHostUIHandler::TranslateUrl(DWORD dwTranslate
,
1347 OLECHAR
**ppchURLOut
)
1349 wxUnusedVar(dwTranslate
);
1350 wxUnusedVar(pchURLIn
);
1351 wxUnusedVar(ppchURLOut
);
1355 HRESULT
DocHostUIHandler::FilterDataObject(IDataObject
*pDO
, IDataObject
**ppDORet
)
1358 wxUnusedVar(ppDORet
);
1362 BEGIN_IID_TABLE(DocHostUIHandler
)
1364 ADD_RAW_IID(wxIID_IDocHostUIHandler
)
1367 IMPLEMENT_IUNKNOWN_METHODS(DocHostUIHandler
)
1369 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE