Implement IDocHostUIHandler in wxWebViewIE to improve the default behaviour. Disable...
[wxWidgets.git] / src / msw / webview_ie.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/webview_ie.cpp
3 // Purpose: wxMSW wxWebViewIE class implementation for web view component
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if defined(__BORLANDC__)
14 #pragma hdrstop
15 #endif
16
17 #include "wx/msw/webview_ie.h"
18
19 #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE
20
21 #include <olectl.h>
22 #include <oleidl.h>
23 #include <exdispid.h>
24 #include <exdisp.h>
25 #include <mshtml.h>
26 #include "wx/msw/registry.h"
27 #include "wx/msw/missing.h"
28 #include "wx/filesys.h"
29 #include "wx/dynlib.h"
30 #include <initguid.h>
31
32 /* These GUID definitions are our own implementation to support interfaces
33 * normally in urlmon.h. See include/wx/msw/webview_ie.h
34 */
35
36 namespace {
37
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);
41
42 }
43
44 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE, wxWebView);
45
46 BEGIN_EVENT_TABLE(wxWebViewIE, wxControl)
47 EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent)
48 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg)
49 END_EVENT_TABLE()
50
51 bool wxWebViewIE::Create(wxWindow* parent,
52 wxWindowID id,
53 const wxString& url,
54 const wxPoint& pos,
55 const wxSize& size,
56 long style,
57 const wxString& name)
58 {
59 if (!wxControl::Create(parent, id, pos, size, style,
60 wxDefaultValidator, name))
61 {
62 return false;
63 }
64
65 m_webBrowser = NULL;
66 m_isBusy = false;
67 m_historyLoadingFromList = false;
68 m_historyEnabled = true;
69 m_historyPosition = -1;
70 m_zoomType = wxWEB_VIEW_ZOOM_TYPE_TEXT;
71
72 if (::CoCreateInstance(CLSID_WebBrowser, NULL,
73 CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
74 IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
75 {
76 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
77 return false;
78 }
79
80 m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
81
82 m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
83 m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
84
85 m_uiHandler = new DocHostUIHandler;
86 m_uiHandler->AddRef();
87
88 m_container = new wxIEContainer(this, IID_IWebBrowser2, m_webBrowser, m_uiHandler);
89
90 LoadURL(url);
91 return true;
92 }
93
94 wxWebViewIE::~wxWebViewIE()
95 {
96 for(unsigned int i = 0; i < m_factories.size(); i++)
97 {
98 m_factories[i]->Release();
99 }
100
101 m_uiHandler->Release();
102 }
103
104 void wxWebViewIE::LoadURL(const wxString& url)
105 {
106 m_ie.CallMethod("Navigate", wxConvertStringToOle(url));
107 }
108
109 void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
110 {
111 BSTR bstr = SysAllocString(OLESTR(""));
112 SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
113 if (psaStrings != NULL)
114 {
115 VARIANT *param;
116 HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
117 param->vt = VT_BSTR;
118 param->bstrVal = bstr;
119
120 hr = SafeArrayUnaccessData(psaStrings);
121
122 IHTMLDocument2* document = GetDocument();
123 document->write(psaStrings);
124 document->close();
125 document->Release();
126
127 SafeArrayDestroy(psaStrings);
128
129 bstr = SysAllocString(html.wc_str());
130
131 // Creates a new one-dimensional array
132 psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
133 if (psaStrings != NULL)
134 {
135 hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
136 param->vt = VT_BSTR;
137 param->bstrVal = bstr;
138 hr = SafeArrayUnaccessData(psaStrings);
139
140 document = GetDocument();
141 document->write(psaStrings);
142 document->Release();
143
144 // SafeArrayDestroy calls SysFreeString for each BSTR
145 SafeArrayDestroy(psaStrings);
146
147 //We send the events when we are done to mimic webkit
148 //Navigated event
149 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
150 GetId(), baseUrl, "");
151 event.SetEventObject(this);
152 HandleWindowEvent(event);
153
154 //Document complete event
155 event.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED);
156 event.SetEventObject(this);
157 HandleWindowEvent(event);
158 }
159 else
160 {
161 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
162 }
163 }
164 else
165 {
166 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL during clear");
167 }
168 }
169
170 wxString wxWebViewIE::GetPageSource() const
171 {
172 IHTMLDocument2* document = GetDocument();
173 IHTMLElement *bodyTag = NULL;
174 IHTMLElement *htmlTag = NULL;
175 wxString source;
176 HRESULT hr = document->get_body(&bodyTag);
177 if(SUCCEEDED(hr))
178 {
179 hr = bodyTag->get_parentElement(&htmlTag);
180 if(SUCCEEDED(hr))
181 {
182 BSTR bstr;
183 htmlTag->get_outerHTML(&bstr);
184 source = wxString(bstr);
185 htmlTag->Release();
186 }
187 bodyTag->Release();
188 }
189
190 document->Release();
191 return source;
192 }
193
194 wxWebViewZoom wxWebViewIE::GetZoom() const
195 {
196 switch( m_zoomType )
197 {
198 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT:
199 return GetIEOpticalZoom();
200 case wxWEB_VIEW_ZOOM_TYPE_TEXT:
201 return GetIETextZoom();
202 default:
203 wxFAIL;
204 }
205
206 //Dummy return to stop compiler warnings
207 return wxWEB_VIEW_ZOOM_MEDIUM;
208
209 }
210
211 void wxWebViewIE::SetZoom(wxWebViewZoom zoom)
212 {
213 switch( m_zoomType )
214 {
215 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT:
216 SetIEOpticalZoom(zoom);
217 break;
218 case wxWEB_VIEW_ZOOM_TYPE_TEXT:
219 SetIETextZoom(zoom);
220 break;
221 default:
222 wxFAIL;
223 }
224 }
225
226 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level)
227 {
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
230 //enum values
231 VARIANT zoomVariant;
232 VariantInit (&zoomVariant);
233 V_VT(&zoomVariant) = VT_I4;
234 V_I4(&zoomVariant) = level;
235
236 #if wxDEBUG_LEVEL
237 HRESULT result =
238 #endif
239 m_webBrowser->ExecWB(OLECMDID_ZOOM,
240 OLECMDEXECOPT_DONTPROMPTUSER,
241 &zoomVariant, NULL);
242 wxASSERT(result == S_OK);
243 }
244
245 wxWebViewZoom wxWebViewIE::GetIETextZoom() const
246 {
247 VARIANT zoomVariant;
248 VariantInit (&zoomVariant);
249 V_VT(&zoomVariant) = VT_I4;
250
251 #if wxDEBUG_LEVEL
252 HRESULT result =
253 #endif
254 m_webBrowser->ExecWB(OLECMDID_ZOOM,
255 OLECMDEXECOPT_DONTPROMPTUSER,
256 NULL, &zoomVariant);
257 wxASSERT(result == S_OK);
258
259 //We can safely cast here as we know that the range matches our enum
260 return static_cast<wxWebViewZoom>(V_I4(&zoomVariant));
261 }
262
263 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level)
264 {
265 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
266 //is 10 to 1000 so the check is unnecessary
267 VARIANT zoomVariant;
268 VariantInit (&zoomVariant);
269 V_VT(&zoomVariant) = VT_I4;
270
271 //We make a somewhat arbitray map here, taken from values used by webkit
272 switch(level)
273 {
274 case wxWEB_VIEW_ZOOM_TINY:
275 V_I4(&zoomVariant) = 60;
276 break;
277 case wxWEB_VIEW_ZOOM_SMALL:
278 V_I4(&zoomVariant) = 80;
279 break;
280 case wxWEB_VIEW_ZOOM_MEDIUM:
281 V_I4(&zoomVariant) = 100;
282 break;
283 case wxWEB_VIEW_ZOOM_LARGE:
284 V_I4(&zoomVariant) = 130;
285 break;
286 case wxWEB_VIEW_ZOOM_LARGEST:
287 V_I4(&zoomVariant) = 160;
288 break;
289 default:
290 wxFAIL;
291 }
292
293 #if wxDEBUG_LEVEL
294 HRESULT result =
295 #endif
296 m_webBrowser->ExecWB((OLECMDID)63 /*OLECMDID_OPTICAL_ZOOM*/,
297 OLECMDEXECOPT_DODEFAULT,
298 &zoomVariant,
299 NULL);
300 wxASSERT(result == S_OK);
301 }
302
303 wxWebViewZoom wxWebViewIE::GetIEOpticalZoom() const
304 {
305 VARIANT zoomVariant;
306 VariantInit (&zoomVariant);
307 V_VT(&zoomVariant) = VT_I4;
308
309 #if wxDEBUG_LEVEL
310 HRESULT result =
311 #endif
312 m_webBrowser->ExecWB((OLECMDID)63 /*OLECMDID_OPTICAL_ZOOM*/,
313 OLECMDEXECOPT_DODEFAULT, NULL,
314 &zoomVariant);
315 wxASSERT(result == S_OK);
316
317 const int zoom = V_I4(&zoomVariant);
318
319 //We make a somewhat arbitray map here, taken from values used by webkit
320 if (zoom <= 65)
321 {
322 return wxWEB_VIEW_ZOOM_TINY;
323 }
324 else if (zoom > 65 && zoom <= 90)
325 {
326 return wxWEB_VIEW_ZOOM_SMALL;
327 }
328 else if (zoom > 90 && zoom <= 115)
329 {
330 return wxWEB_VIEW_ZOOM_MEDIUM;
331 }
332 else if (zoom > 115 && zoom <= 145)
333 {
334 return wxWEB_VIEW_ZOOM_LARGE;
335 }
336 else /*if (zoom > 145) */ //Using else removes a compiler warning
337 {
338 return wxWEB_VIEW_ZOOM_LARGEST;
339 }
340 }
341
342 void wxWebViewIE::SetZoomType(wxWebViewZoomType type)
343 {
344 m_zoomType = type;
345 }
346
347 wxWebViewZoomType wxWebViewIE::GetZoomType() const
348 {
349 return m_zoomType;
350 }
351
352 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type) const
353 {
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");
357 wxString value;
358 key.QueryValue("Version", value);
359
360 long version = wxAtoi(value.Left(1));
361 if(version <= 6 && type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
362 return false;
363 else
364 return true;
365 }
366
367 void wxWebViewIE::Print()
368 {
369 m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW,
370 OLECMDEXECOPT_DODEFAULT, NULL, NULL);
371 }
372
373 bool wxWebViewIE::CanGoBack() const
374 {
375 if(m_historyEnabled)
376 return m_historyPosition > 0;
377 else
378 return false;
379 }
380
381 bool wxWebViewIE::CanGoForward() const
382 {
383 if(m_historyEnabled)
384 return m_historyPosition != static_cast<int>(m_historyList.size()) - 1;
385 else
386 return false;
387 }
388
389 void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item)
390 {
391 int pos = -1;
392 for(unsigned int i = 0; i < m_historyList.size(); i++)
393 {
394 //We compare the actual pointers to find the correct item
395 if(m_historyList[i].get() == item.get())
396 pos = i;
397 }
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;
403 }
404
405 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewIE::GetBackwardHistory()
406 {
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++)
411 {
412 backhist.push_back(m_historyList[i]);
413 }
414 return backhist;
415 }
416
417 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewIE::GetForwardHistory()
418 {
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++)
423 {
424 forwardhist.push_back(m_historyList[i]);
425 }
426 return forwardhist;
427 }
428
429 void wxWebViewIE::GoBack()
430 {
431 LoadHistoryItem(m_historyList[m_historyPosition - 1]);
432 }
433
434 void wxWebViewIE::GoForward()
435 {
436 LoadHistoryItem(m_historyList[m_historyPosition + 1]);
437 }
438
439 void wxWebViewIE::Stop()
440 {
441 m_ie.CallMethod("Stop");
442 }
443
444 void wxWebViewIE::ClearHistory()
445 {
446 m_historyList.clear();
447 m_historyPosition = -1;
448 }
449
450 void wxWebViewIE::EnableHistory(bool enable)
451 {
452 m_historyEnabled = enable;
453 m_historyList.clear();
454 m_historyPosition = -1;
455 }
456
457 void wxWebViewIE::Reload(wxWebViewReloadFlags flags)
458 {
459 VARIANTARG level;
460 VariantInit(&level);
461 V_VT(&level) = VT_I2;
462
463 switch(flags)
464 {
465 case wxWEB_VIEW_RELOAD_DEFAULT:
466 V_I2(&level) = REFRESH_NORMAL;
467 break;
468 case wxWEB_VIEW_RELOAD_NO_CACHE:
469 V_I2(&level) = REFRESH_COMPLETELY;
470 break;
471 default:
472 wxFAIL_MSG("Unexpected reload type");
473 }
474
475 m_webBrowser->Refresh2(&level);
476 }
477
478 bool wxWebViewIE::IsOfflineMode()
479 {
480 wxVariant out = m_ie.GetProperty("Offline");
481
482 wxASSERT(out.GetType() == "bool");
483
484 return out.GetBool();
485 }
486
487 void wxWebViewIE::SetOfflineMode(bool offline)
488 {
489 // FIXME: the wxWidgets docs do not really document what the return
490 // parameter of PutProperty is
491 #if wxDEBUG_LEVEL
492 const bool success =
493 #endif
494 m_ie.PutProperty("Offline", (offline ?
495 VARIANT_TRUE :
496 VARIANT_FALSE));
497 wxASSERT(success);
498 }
499
500 bool wxWebViewIE::IsBusy() const
501 {
502 if (m_isBusy) return true;
503
504 wxVariant out = m_ie.GetProperty("Busy");
505
506 wxASSERT(out.GetType() == "bool");
507
508 return out.GetBool();
509 }
510
511 wxString wxWebViewIE::GetCurrentURL() const
512 {
513 wxVariant out = m_ie.GetProperty("LocationURL");
514
515 wxASSERT(out.GetType() == "string");
516 return out.GetString();
517 }
518
519 wxString wxWebViewIE::GetCurrentTitle() const
520 {
521 IHTMLDocument2* document = GetDocument();
522 BSTR title;
523
524 document->get_nameProp(&title);
525 document->Release();
526 return wxString(title);
527 }
528
529 bool wxWebViewIE::CanCut() const
530 {
531 return CanExecCommand("Cut");
532 }
533
534 bool wxWebViewIE::CanCopy() const
535 {
536 return CanExecCommand("Copy");
537 }
538 bool wxWebViewIE::CanPaste() const
539 {
540 return CanExecCommand("Paste");
541 }
542
543 void wxWebViewIE::Cut()
544 {
545 ExecCommand("Cut");
546 }
547
548 void wxWebViewIE::Copy()
549 {
550 ExecCommand("Copy");
551 }
552
553 void wxWebViewIE::Paste()
554 {
555 ExecCommand("Paste");
556 }
557
558 bool wxWebViewIE::CanUndo() const
559 {
560 return CanExecCommand("Undo");
561 }
562 bool wxWebViewIE::CanRedo() const
563 {
564 return CanExecCommand("Redo");
565 }
566
567 void wxWebViewIE::Undo()
568 {
569 ExecCommand("Undo");
570 }
571
572 void wxWebViewIE::Redo()
573 {
574 ExecCommand("Redo");
575 }
576
577 void wxWebViewIE::SetEditable(bool enable)
578 {
579 IHTMLDocument2* document = GetDocument();
580 if( enable )
581 document->put_designMode(SysAllocString(L"On"));
582 else
583 document->put_designMode(SysAllocString(L"Off"));
584
585 document->Release();
586 }
587
588 bool wxWebViewIE::IsEditable() const
589 {
590 IHTMLDocument2* document = GetDocument();
591 BSTR mode;
592 document->get_designMode(&mode);
593 document->Release();
594 if(wxString(mode) == "On")
595 return true;
596 else
597 return false;
598 }
599
600 void wxWebViewIE::SelectAll()
601 {
602 ExecCommand("SelectAll");
603 }
604
605 bool wxWebViewIE::HasSelection() const
606 {
607 IHTMLDocument2* document = GetDocument();
608 IHTMLSelectionObject* selection;
609 wxString sel;
610 HRESULT hr = document->get_selection(&selection);
611 if(SUCCEEDED(hr))
612 {
613 BSTR type;
614 selection->get_type(&type);
615 sel = wxString(type);
616 selection->Release();
617 }
618 document->Release();
619 return sel != "None";
620 }
621
622 void wxWebViewIE::DeleteSelection()
623 {
624 ExecCommand("Delete");
625 }
626
627 wxString wxWebViewIE::GetSelectedText() const
628 {
629 IHTMLDocument2* document = GetDocument();
630 IHTMLSelectionObject* selection;
631 wxString selected;
632 HRESULT hr = document->get_selection(&selection);
633 if(SUCCEEDED(hr))
634 {
635 IDispatch* disrange;
636 hr = selection->createRange(&disrange);
637 if(SUCCEEDED(hr))
638 {
639 IHTMLTxtRange* range;
640 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
641 if(SUCCEEDED(hr))
642 {
643 BSTR text;
644 range->get_text(&text);
645 selected = wxString(text);
646 range->Release();
647 }
648 disrange->Release();
649 }
650 selection->Release();
651 }
652 document->Release();
653 return selected;
654 }
655
656 wxString wxWebViewIE::GetSelectedSource() const
657 {
658 IHTMLDocument2* document = GetDocument();
659 IHTMLSelectionObject* selection;
660 wxString selected;
661 HRESULT hr = document->get_selection(&selection);
662 if(SUCCEEDED(hr))
663 {
664 IDispatch* disrange;
665 hr = selection->createRange(&disrange);
666 if(SUCCEEDED(hr))
667 {
668 IHTMLTxtRange* range;
669 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
670 if(SUCCEEDED(hr))
671 {
672 BSTR text;
673 range->get_htmlText(&text);
674 selected = wxString(text);
675 range->Release();
676 }
677 disrange->Release();
678 }
679 selection->Release();
680 }
681 document->Release();
682 return selected;
683 }
684
685 void wxWebViewIE::ClearSelection()
686 {
687 IHTMLDocument2* document = GetDocument();
688 IHTMLSelectionObject* selection;
689 wxString selected;
690 HRESULT hr = document->get_selection(&selection);
691 if(SUCCEEDED(hr))
692 {
693 selection->empty();
694 selection->Release();
695 }
696 document->Release();
697 }
698
699 wxString wxWebViewIE::GetPageText() const
700 {
701 IHTMLDocument2* document = GetDocument();
702 wxString text;
703 IHTMLElement* body;
704 HRESULT hr = document->get_body(&body);
705 if(SUCCEEDED(hr))
706 {
707 BSTR out;
708 body->get_innerText(&out);
709 text = wxString(out);
710 body->Release();
711 }
712 document->Release();
713 return text;
714 }
715
716 void wxWebViewIE::RunScript(const wxString& javascript)
717 {
718 IHTMLDocument2* document = GetDocument();
719 IHTMLWindow2* window;
720 wxString language = "javascript";
721 HRESULT hr = document->get_parentWindow(&window);
722 if(SUCCEEDED(hr))
723 {
724 VARIANT level;
725 VariantInit(&level);
726 V_VT(&level) = VT_EMPTY;
727 window->execScript(SysAllocString(javascript.wc_str()),
728 SysAllocString(language.wc_str()),
729 &level);
730 }
731 document->Release();
732 }
733
734 void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
735 {
736 wxDynamicLibrary urlMon(wxT("urlmon.dll"));
737 if(urlMon.HasSymbol(wxT("CoInternetGetSession")))
738 {
739 typedef HRESULT (WINAPI *CoInternetGetSession_t)(DWORD, wxIInternetSession**, DWORD);
740 wxDYNLIB_FUNCTION(CoInternetGetSession_t, CoInternetGetSession, urlMon);
741
742 ClassFactory* cf = new ClassFactory(handler);
743 wxIInternetSession* session;
744 HRESULT res = (*pfnCoInternetGetSession)(0, &session, 0);
745 if(FAILED(res))
746 {
747 wxFAIL_MSG("Could not retrive internet session");
748 }
749
750 HRESULT hr = session->RegisterNameSpace(cf, CLSID_FileProtocol,
751 handler->GetName().wc_str(),
752 0, NULL, 0);
753 if(FAILED(hr))
754 {
755 wxFAIL_MSG("Could not register protocol");
756 }
757 m_factories.push_back(cf);
758 }
759 else
760 {
761 wxFAIL_MSG("urlmon does not contain CoInternetGetSession");
762 }
763 }
764
765 bool wxWebViewIE::CanExecCommand(wxString command) const
766 {
767 IHTMLDocument2* document = GetDocument();
768 VARIANT_BOOL enabled;
769
770 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
771 document->Release();
772
773 return (enabled == VARIANT_TRUE);
774 }
775
776 void wxWebViewIE::ExecCommand(wxString command)
777 {
778 IHTMLDocument2* document = GetDocument();
779 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
780 document->Release();
781 }
782
783 IHTMLDocument2* wxWebViewIE::GetDocument() const
784 {
785 wxVariant variant = m_ie.GetProperty("Document");
786 IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr();
787
788 wxASSERT(document);
789
790 return document;
791 }
792
793 void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
794 {
795 if (m_webBrowser == NULL) return;
796
797 switch (evt.GetDispatchId())
798 {
799 case DISPID_BEFORENAVIGATE2:
800 {
801 m_isBusy = true;
802
803 wxString url = evt[1].GetString();
804 wxString target = evt[3].GetString();
805
806 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
807 GetId(), url, target);
808
809 //skip empty javascript events.
810 if(url == "javascript:\"\"" && target.IsEmpty())
811 {
812 event.Veto();
813 }
814 else
815 {
816 event.SetEventObject(this);
817 HandleWindowEvent(event);
818 }
819
820 if (!event.IsAllowed())
821 {
822 wxActiveXEventNativeMSW* nativeParams =
823 evt.GetNativeParameters();
824 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
825 }
826
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
830 // flag anymore.
831 m_isBusy = false;
832
833 break;
834 }
835
836 case DISPID_NAVIGATECOMPLETE2:
837 {
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);
845 break;
846 }
847
848 case DISPID_PROGRESSCHANGE:
849 {
850 // download progress
851 break;
852 }
853
854 case DISPID_DOCUMENTCOMPLETE:
855 {
856 //Only send a complete even if we are actually finished, this brings
857 //the event in to line with webkit
858 READYSTATE rs;
859 m_webBrowser->get_ReadyState( &rs );
860 if(rs != READYSTATE_COMPLETE)
861 break;
862
863 wxString url = evt[1].GetString();
864
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
869 //of url
870 if(m_historyEnabled && !m_historyLoadingFromList &&
871 (url == GetCurrentURL() ||
872 (GetCurrentURL().substr(0, 4) == "file" &&
873 wxFileSystem::URLToFileName(GetCurrentURL()).GetFullPath() == url)))
874 {
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)
878 {
879 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
880 m_historyList.end());
881 }
882 wxSharedPtr<wxWebViewHistoryItem> item(new wxWebViewHistoryItem(url, GetCurrentTitle()));
883 m_historyList.push_back(item);
884 m_historyPosition++;
885 }
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(),
891 url, target);
892 event.SetEventObject(this);
893 HandleWindowEvent(event);
894 break;
895 }
896
897 case DISPID_STATUSTEXTCHANGE:
898 {
899 break;
900 }
901
902 case DISPID_TITLECHANGE:
903 {
904 wxString title = evt[0].GetString();
905
906 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
907 GetId(), GetCurrentURL(), "");
908 event.SetString(title);
909 event.SetEventObject(this);
910 HandleWindowEvent(event);
911 break;
912 }
913
914 case DISPID_NAVIGATEERROR:
915 {
916 wxWebViewNavigationError errorType = wxWEB_NAV_ERR_OTHER;
917 wxString errorCode = "?";
918 switch (evt[3].GetLong())
919 {
920 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
921 errorCode = "INET_E_INVALID_URL";
922 errorType = wxWEB_NAV_ERR_REQUEST;
923 break;
924 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
925 errorCode = "INET_E_NO_SESSION";
926 errorType = wxWEB_NAV_ERR_CONNECTION;
927 break;
928 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
929 errorCode = "INET_E_CANNOT_CONNECT";
930 errorType = wxWEB_NAV_ERR_CONNECTION;
931 break;
932 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
933 errorCode = "INET_E_RESOURCE_NOT_FOUND";
934 errorType = wxWEB_NAV_ERR_NOT_FOUND;
935 break;
936 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
937 errorCode = "INET_E_OBJECT_NOT_FOUND";
938 errorType = wxWEB_NAV_ERR_NOT_FOUND;
939 break;
940 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
941 errorCode = "INET_E_DATA_NOT_AVAILABLE";
942 errorType = wxWEB_NAV_ERR_NOT_FOUND;
943 break;
944 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
945 errorCode = "INET_E_DOWNLOAD_FAILURE";
946 errorType = wxWEB_NAV_ERR_CONNECTION;
947 break;
948 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
949 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
950 errorType = wxWEB_NAV_ERR_AUTH;
951 break;
952 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
953 errorCode = "INET_E_NO_VALID_MEDIA";
954 errorType = wxWEB_NAV_ERR_REQUEST;
955 break;
956 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
957 errorCode = "INET_E_CONNECTION_TIMEOUT";
958 errorType = wxWEB_NAV_ERR_CONNECTION;
959 break;
960 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
961 errorCode = "INET_E_INVALID_REQUEST";
962 errorType = wxWEB_NAV_ERR_REQUEST;
963 break;
964 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
965 errorCode = "INET_E_UNKNOWN_PROTOCOL";
966 errorType = wxWEB_NAV_ERR_REQUEST;
967 break;
968 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
969 errorCode = "INET_E_SECURITY_PROBLEM";
970 errorType = wxWEB_NAV_ERR_SECURITY;
971 break;
972 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
973 errorCode = "INET_E_CANNOT_LOAD_DATA";
974 errorType = wxWEB_NAV_ERR_OTHER;
975 break;
976 case INET_E_CANNOT_INSTANTIATE_OBJECT:
977 // CoCreateInstance will return an error code if this happens,
978 // we'll handle this above.
979 return;
980 break;
981 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
982 errorCode = "INET_E_REDIRECT_FAILED";
983 errorType = wxWEB_NAV_ERR_OTHER;
984 break;
985 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
986 errorCode = "INET_E_REDIRECT_TO_DIR";
987 errorType = wxWEB_NAV_ERR_REQUEST;
988 break;
989 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
990 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
991 errorType = wxWEB_NAV_ERR_OTHER;
992 break;
993 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
994 errorCode = "INET_E_USE_EXTEND_BINDING";
995 errorType = wxWEB_NAV_ERR_OTHER;
996 break;
997 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
998 errorCode = "INET_E_TERMINATED_BIND";
999 errorType = wxWEB_NAV_ERR_OTHER;
1000 break;
1001 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
1002 errorCode = "INET_E_INVALID_CERTIFICATE";
1003 errorType = wxWEB_NAV_ERR_CERTIFICATE;
1004 break;
1005 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
1006 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
1007 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
1008 break;
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;
1013 break;
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;
1017 break;
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;
1021 break;
1022 case INET_E_CODE_INSTALL_SUPPRESSED:
1023 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
1024 errorType = wxWEB_NAV_ERR_SECURITY;
1025 break;
1026 }
1027
1028 wxString url = evt[1].GetString();
1029 wxString target = evt[2].GetString();
1030 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
1031 url, target);
1032 event.SetEventObject(this);
1033 event.SetInt(errorType);
1034 event.SetString(errorCode);
1035 HandleWindowEvent(event);
1036 break;
1037 }
1038 case DISPID_NEWWINDOW3:
1039 {
1040 wxString url = evt[4].GetString();
1041
1042 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
1043 GetId(), url, wxEmptyString);
1044 event.SetEventObject(this);
1045 HandleWindowEvent(event);
1046
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;
1051 break;
1052 }
1053 }
1054
1055 evt.Skip();
1056 }
1057
1058 VirtualProtocol::VirtualProtocol(wxSharedPtr<wxWebViewHandler> handler)
1059 {
1060 m_file = NULL;
1061 m_handler = handler;
1062 }
1063
1064 BEGIN_IID_TABLE(VirtualProtocol)
1065 ADD_IID(Unknown)
1066 ADD_RAW_IID(wxIID_IInternetProtocolRoot)
1067 ADD_RAW_IID(wxIID_IInternetProtocol)
1068 END_IID_TABLE;
1069
1070 IMPLEMENT_IUNKNOWN_METHODS(VirtualProtocol)
1071
1072 HRESULT VirtualProtocol::Start(LPCWSTR szUrl, wxIInternetProtocolSink *pOIProtSink,
1073 wxIInternetBindInfo *pOIBindInfo, DWORD grfPI,
1074 HANDLE_PTR dwReserved)
1075 {
1076 wxUnusedVar(szUrl);
1077 wxUnusedVar(pOIBindInfo);
1078 wxUnusedVar(grfPI);
1079 wxUnusedVar(dwReserved);
1080 m_protocolSink = pOIProtSink;
1081
1082 //We get the file itself from the protocol handler
1083 m_file = m_handler->GetFile(szUrl);
1084
1085
1086 if(!m_file)
1087 return INET_E_RESOURCE_NOT_FOUND;
1088
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,
1095 length, length);
1096 return S_OK;
1097 }
1098
1099 HRESULT VirtualProtocol::Read(void *pv, ULONG cb, ULONG *pcbRead)
1100 {
1101 //If the file is null we return false to indicte it is finished
1102 if(!m_file)
1103 return S_FALSE;
1104
1105 wxStreamError err = m_file->GetStream()->Read(pv, cb).GetLastError();
1106 *pcbRead = m_file->GetStream()->LastRead();
1107
1108 if(err == wxSTREAM_NO_ERROR)
1109 {
1110 if(*pcbRead < cb)
1111 {
1112 wxDELETE(m_file);
1113 m_protocolSink->ReportResult(S_OK, 0, NULL);
1114 }
1115 //As we are not eof there is more data
1116 return S_OK;
1117 }
1118 else if(err == wxSTREAM_EOF)
1119 {
1120 wxDELETE(m_file);
1121 m_protocolSink->ReportResult(S_OK, 0, NULL);
1122 //We are eof and so finished
1123 return S_OK;
1124 }
1125 else if(err == wxSTREAM_READ_ERROR)
1126 {
1127 wxDELETE(m_file);
1128 return INET_E_DOWNLOAD_FAILURE;
1129 }
1130 else
1131 {
1132 //Dummy return to surpress a compiler warning
1133 wxFAIL;
1134 return INET_E_DOWNLOAD_FAILURE;
1135 }
1136 }
1137
1138 BEGIN_IID_TABLE(ClassFactory)
1139 ADD_IID(Unknown)
1140 ADD_IID(ClassFactory)
1141 END_IID_TABLE;
1142
1143 IMPLEMENT_IUNKNOWN_METHODS(ClassFactory)
1144
1145 HRESULT ClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid,
1146 void ** ppvObject)
1147 {
1148 if (pUnkOuter)
1149 return CLASS_E_NOAGGREGATION;
1150 VirtualProtocol* vp = new VirtualProtocol(m_handler);
1151 vp->AddRef();
1152 HRESULT hr = vp->QueryInterface(riid, ppvObject);
1153 vp->Release();
1154 return hr;
1155
1156 }
1157
1158 STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
1159 {
1160 wxUnusedVar(fLock);
1161 return S_OK;
1162 }
1163
1164 wxIEContainer::wxIEContainer(wxWindow *parent, REFIID iid, IUnknown *pUnk,
1165 DocHostUIHandler* uiHandler) :
1166 wxActiveXContainer(parent,iid,pUnk)
1167 {
1168 m_uiHandler = uiHandler;
1169 }
1170
1171 wxIEContainer::~wxIEContainer()
1172 {
1173 }
1174
1175 bool wxIEContainer::QueryClientSiteInterface(REFIID iid, void **_interface,
1176 const char *&desc)
1177 {
1178 if (m_uiHandler && IsEqualIID(iid, wxIID_IDocHostUIHandler))
1179 {
1180 *_interface = (IUnknown *) (wxIDocHostUIHandler *) m_uiHandler;
1181 desc = "IDocHostUIHandler";
1182 return true;
1183 }
1184 return false;
1185 }
1186
1187 HRESULT DocHostUIHandler::ShowContextMenu(DWORD dwID, POINT *ppt,
1188 IUnknown *pcmdtReserved,
1189 IDispatch *pdispReserved)
1190 {
1191 wxUnusedVar(dwID);
1192 wxUnusedVar(ppt);
1193 wxUnusedVar(pcmdtReserved);
1194 wxUnusedVar(pdispReserved);
1195 return E_NOTIMPL;
1196 }
1197
1198 HRESULT DocHostUIHandler::GetHostInfo(DOCHOSTUIINFO *pInfo)
1199 {
1200 //don't show 3d border and ebales themes.
1201 pInfo->dwFlags = pInfo->dwFlags | DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_THEME;
1202 return S_OK;
1203 }
1204
1205 HRESULT DocHostUIHandler::ShowUI(DWORD dwID,
1206 IOleInPlaceActiveObject *pActiveObject,
1207 IOleCommandTarget *pCommandTarget,
1208 IOleInPlaceFrame *pFrame,
1209 IOleInPlaceUIWindow *pDoc)
1210 {
1211 wxUnusedVar(dwID);
1212 wxUnusedVar(pActiveObject);
1213 wxUnusedVar(pCommandTarget);
1214 wxUnusedVar(pFrame);
1215 wxUnusedVar(pDoc);
1216 return S_FALSE;
1217 }
1218
1219 HRESULT DocHostUIHandler::HideUI(void)
1220 {
1221 return E_NOTIMPL;
1222 }
1223
1224 HRESULT DocHostUIHandler::UpdateUI(void)
1225 {
1226 return E_NOTIMPL;
1227 }
1228
1229 HRESULT DocHostUIHandler::EnableModeless(BOOL fEnable)
1230 {
1231 wxUnusedVar(fEnable);
1232 return E_NOTIMPL;
1233 }
1234
1235 HRESULT DocHostUIHandler::OnDocWindowActivate(BOOL fActivate)
1236 {
1237 wxUnusedVar(fActivate);
1238 return E_NOTIMPL;
1239 }
1240
1241 HRESULT DocHostUIHandler::OnFrameWindowActivate(BOOL fActivate)
1242 {
1243 wxUnusedVar(fActivate);
1244 return E_NOTIMPL;
1245 }
1246
1247 HRESULT DocHostUIHandler::ResizeBorder(LPCRECT prcBorder,
1248 IOleInPlaceUIWindow *pUIWindow,
1249 BOOL fFrameWindow)
1250 {
1251 wxUnusedVar(prcBorder);
1252 wxUnusedVar(pUIWindow);
1253 wxUnusedVar(fFrameWindow);
1254 return E_NOTIMPL;
1255 }
1256
1257 HRESULT DocHostUIHandler::TranslateAccelerator(LPMSG lpMsg,
1258 const GUID *pguidCmdGroup,
1259 DWORD nCmdID)
1260 {
1261 if(lpMsg && lpMsg->message == WM_KEYDOWN)
1262 {
1263 //control is down?
1264 if((GetKeyState(VK_CONTROL) & 0x8000 ))
1265 {
1266 //skip CTRL-N, CTRL-F and CTRL-P
1267 if(lpMsg->wParam == 'N' || lpMsg->wParam == 'P' || lpMsg->wParam == 'F')
1268 {
1269 return S_OK;
1270 }
1271 }
1272 //skip F5
1273 if(lpMsg->wParam == VK_F5)
1274 {
1275 return S_OK;
1276 }
1277 }
1278
1279 wxUnusedVar(pguidCmdGroup);
1280 wxUnusedVar(nCmdID);
1281 return E_NOTIMPL;
1282 }
1283
1284 HRESULT DocHostUIHandler::GetOptionKeyPath(LPOLESTR *pchKey,DWORD dw)
1285 {
1286 wxUnusedVar(pchKey);
1287 wxUnusedVar(dw);
1288 return E_NOTIMPL;
1289 }
1290
1291 HRESULT DocHostUIHandler::GetDropTarget(IDropTarget *pDropTarget,
1292 IDropTarget **ppDropTarget)
1293 {
1294 wxUnusedVar(pDropTarget);
1295 wxUnusedVar(ppDropTarget);
1296 return E_NOTIMPL;
1297 }
1298
1299 HRESULT DocHostUIHandler::GetExternal(IDispatch **ppDispatch)
1300 {
1301 wxUnusedVar(ppDispatch);
1302 return E_NOTIMPL;
1303 }
1304
1305 HRESULT DocHostUIHandler::TranslateUrl(DWORD dwTranslate,
1306 OLECHAR *pchURLIn,
1307 OLECHAR **ppchURLOut)
1308 {
1309 wxUnusedVar(dwTranslate);
1310 wxUnusedVar(pchURLIn);
1311 wxUnusedVar(ppchURLOut);
1312 return E_NOTIMPL;
1313 }
1314
1315 HRESULT DocHostUIHandler::FilterDataObject(IDataObject *pDO, IDataObject **ppDORet)
1316 {
1317 wxUnusedVar(pDO);
1318 wxUnusedVar(ppDORet);
1319 return E_NOTIMPL;
1320 }
1321
1322 BEGIN_IID_TABLE(DocHostUIHandler)
1323 ADD_IID(Unknown)
1324 ADD_RAW_IID(wxIID_IDocHostUIHandler)
1325 END_IID_TABLE;
1326
1327 IMPLEMENT_IUNKNOWN_METHODS(DocHostUIHandler)
1328
1329 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE