Update handling and documentation for new window events. Clarify that you must handle...
[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, 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_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
30 //Taken from wx/filesys.cpp
31 static wxString EscapeFileNameCharsInURL(const char *in)
32 {
33 wxString s;
34
35 for ( const unsigned char *p = (const unsigned char*)in; *p; ++p )
36 {
37 const unsigned char c = *p;
38
39 if ( c == '/' || c == '-' || c == '.' || c == '_' || c == '~' ||
40 (c >= '0' && c <= '9') ||
41 (c >= 'a' && c <= 'z') ||
42 (c >= 'A' && c <= 'Z') )
43 {
44 s << c;
45 }
46 else
47 {
48 s << wxString::Format("%%%02x", c);
49 }
50 }
51
52 return s;
53 }
54
55 BEGIN_EVENT_TABLE(wxWebViewIE, wxControl)
56 EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent)
57 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg)
58 END_EVENT_TABLE()
59
60 bool wxWebViewIE::Create(wxWindow* parent,
61 wxWindowID id,
62 const wxString& url,
63 const wxPoint& pos,
64 const wxSize& size,
65 long style,
66 const wxString& name)
67 {
68 if (!wxControl::Create(parent, id, pos, size, style,
69 wxDefaultValidator, name))
70 {
71 return false;
72 }
73
74 m_webBrowser = NULL;
75 m_isBusy = false;
76 m_historyLoadingFromList = false;
77 m_historyEnabled = true;
78 m_historyPosition = -1;
79 m_zoomType = wxWEB_VIEW_ZOOM_TYPE_TEXT;
80
81 if (::CoCreateInstance(CLSID_WebBrowser, NULL,
82 CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
83 IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
84 {
85 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
86 return false;
87 }
88
89 m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
90
91 m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
92 m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
93 //m_webBrowser->put_Silent(VARIANT_FALSE);
94
95 //We register a custom handler for the file protocol so we can handle
96 //Virtual file systems
97 ClassFactory* cf = new ClassFactory;
98 IInternetSession* session;
99 if(CoInternetGetSession(0, &session, 0) != S_OK)
100 return false;
101 HRESULT hr = session->RegisterNameSpace(cf, CLSID_FileProtocol, L"file", 0, NULL, 0);
102 if(FAILED(hr))
103 return false;
104
105 m_container = new wxActiveXContainer(this, IID_IWebBrowser2, m_webBrowser);
106
107 SetBackgroundStyle(wxBG_STYLE_PAINT);
108 SetDoubleBuffered(true);
109 LoadUrl(url);
110 return true;
111 }
112
113
114 void wxWebViewIE::LoadUrl(const wxString& url)
115 {
116 m_ie.CallMethod("Navigate", (BSTR) url.wc_str(), NULL, NULL, NULL, NULL);
117 }
118
119 void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
120 {
121 BSTR bstr = SysAllocString(html.wc_str());
122
123 // Creates a new one-dimensional array
124 SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
125 if (psaStrings != NULL)
126 {
127 VARIANT *param;
128
129 HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
130 param->vt = VT_BSTR;
131 param->bstrVal = bstr;
132 hr = SafeArrayUnaccessData(psaStrings);
133
134 IHTMLDocument2* document = GetDocument();
135 document->write(psaStrings);
136 document->Release();
137
138 // SafeArrayDestroy calls SysFreeString for each BSTR
139 SafeArrayDestroy(psaStrings);
140
141 //We send the events when we are done to mimic webkit
142 //Navigated event
143 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
144 GetId(), baseUrl, "", false);
145 event.SetEventObject(this);
146 HandleWindowEvent(event);
147
148 //Document complete event
149 event.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED);
150 event.SetEventObject(this);
151 HandleWindowEvent(event);
152 }
153 else
154 {
155 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
156 }
157
158 }
159
160 wxString wxWebViewIE::GetPageSource()
161 {
162 IHTMLDocument2* document = GetDocument();
163 IHTMLElement *bodyTag = NULL;
164 IHTMLElement *htmlTag = NULL;
165 wxString source;
166 HRESULT hr = document->get_body(&bodyTag);
167 if(SUCCEEDED(hr))
168 {
169 hr = bodyTag->get_parentElement(&htmlTag);
170 if(SUCCEEDED(hr))
171 {
172 BSTR bstr;
173 htmlTag->get_outerHTML(&bstr);
174 source = wxString(bstr);
175 htmlTag->Release();
176 }
177 bodyTag->Release();
178 }
179
180 document->Release();
181 return source;
182 }
183
184 wxWebViewZoom wxWebViewIE::GetZoom()
185 {
186 if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
187 return GetIEOpticalZoom();
188 else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
189 return GetIETextZoom();
190 else
191 wxFAIL;
192
193 //Dummy return to stop compiler warnings
194 return wxWEB_VIEW_ZOOM_MEDIUM;
195
196 }
197
198 void wxWebViewIE::SetZoom(wxWebViewZoom zoom)
199 {
200 if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
201 SetIEOpticalZoom(zoom);
202 else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
203 SetIETextZoom(zoom);
204 else
205 wxFAIL;
206 }
207
208 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level)
209 {
210 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
211 //is 0 to 4 so the check is unnecessary, these match exactly with the
212 //enum values
213 VARIANT zoomVariant;
214 VariantInit (&zoomVariant);
215 V_VT(&zoomVariant) = VT_I4;
216 V_I4(&zoomVariant) = level;
217
218 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
219 OLECMDEXECOPT_DONTPROMPTUSER,
220 &zoomVariant, NULL);
221 wxASSERT(result == S_OK);
222 }
223
224 wxWebViewZoom wxWebViewIE::GetIETextZoom()
225 {
226 VARIANT zoomVariant;
227 VariantInit (&zoomVariant);
228 V_VT(&zoomVariant) = VT_I4;
229
230 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
231 OLECMDEXECOPT_DONTPROMPTUSER,
232 NULL, &zoomVariant);
233 wxASSERT(result == S_OK);
234
235 //We can safely cast here as we know that the range matches our enum
236 return static_cast<wxWebViewZoom>(V_I4(&zoomVariant));
237 }
238
239 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level)
240 {
241 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
242 //is 10 to 1000 so the check is unnecessary
243 VARIANT zoomVariant;
244 VariantInit (&zoomVariant);
245 V_VT(&zoomVariant) = VT_I4;
246
247 //We make a somewhat arbitray map here, taken from values used by webkit
248 switch(level)
249 {
250 case wxWEB_VIEW_ZOOM_TINY:
251 V_I4(&zoomVariant) = 60;
252 break;
253 case wxWEB_VIEW_ZOOM_SMALL:
254 V_I4(&zoomVariant) = 80;
255 break;
256 case wxWEB_VIEW_ZOOM_MEDIUM:
257 V_I4(&zoomVariant) = 100;
258 break;
259 case wxWEB_VIEW_ZOOM_LARGE:
260 V_I4(&zoomVariant) = 130;
261 break;
262 case wxWEB_VIEW_ZOOM_LARGEST:
263 V_I4(&zoomVariant) = 160;
264 break;
265 default:
266 wxFAIL;
267 }
268
269 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
270 OLECMDEXECOPT_DODEFAULT,
271 &zoomVariant,
272 NULL);
273 wxASSERT(result == S_OK);
274 }
275
276 wxWebViewZoom wxWebViewIE::GetIEOpticalZoom()
277 {
278 VARIANT zoomVariant;
279 VariantInit (&zoomVariant);
280 V_VT(&zoomVariant) = VT_I4;
281
282 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
283 OLECMDEXECOPT_DODEFAULT, NULL,
284 &zoomVariant);
285 wxASSERT(result == S_OK);
286
287 const int zoom = V_I4(&zoomVariant);
288
289 //We make a somewhat arbitray map here, taken from values used by webkit
290 if (zoom <= 65)
291 {
292 return wxWEB_VIEW_ZOOM_TINY;
293 }
294 else if (zoom > 65 && zoom <= 90)
295 {
296 return wxWEB_VIEW_ZOOM_SMALL;
297 }
298 else if (zoom > 90 && zoom <= 115)
299 {
300 return wxWEB_VIEW_ZOOM_MEDIUM;
301 }
302 else if (zoom > 115 && zoom <= 145)
303 {
304 return wxWEB_VIEW_ZOOM_LARGE;
305 }
306 else /*if (zoom > 145) */ //Using else removes a compiler warning
307 {
308 return wxWEB_VIEW_ZOOM_LARGEST;
309 }
310 }
311
312 void wxWebViewIE::SetZoomType(wxWebViewZoomType type)
313 {
314 m_zoomType = type;
315 }
316
317 wxWebViewZoomType wxWebViewIE::GetZoomType() const
318 {
319 return m_zoomType;
320 }
321
322 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type) const
323 {
324 //IE 6 and below only support text zoom, so check the registry to see what
325 //version we actually have
326 wxRegKey key(wxRegKey::HKLM, "Software\\Microsoft\\Internet Explorer");
327 wxString value;
328 key.QueryValue("Version", value);
329
330 long version = wxAtoi(value.Left(1));
331 if(version <= 6 && type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
332 return false;
333 else
334 return true;
335 }
336
337 void wxWebViewIE::Print()
338 {
339 m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW,
340 OLECMDEXECOPT_DODEFAULT, NULL, NULL);
341 }
342
343 bool wxWebViewIE::CanGoBack()
344 {
345 if(m_historyEnabled)
346 return m_historyPosition > 0;
347 else
348 return false;
349 }
350
351 bool wxWebViewIE::CanGoForward()
352 {
353 if(m_historyEnabled)
354 return m_historyPosition != static_cast<int>(m_historyList.size()) - 1;
355 else
356 return false;
357 }
358
359 void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item)
360 {
361 int pos = -1;
362 for(unsigned int i = 0; i < m_historyList.size(); i++)
363 {
364 //We compare the actual pointers to find the correct item
365 if(m_historyList[i].get() == item.get())
366 pos = i;
367 }
368 wxASSERT_MSG(pos != static_cast<int>(m_historyList.size()),
369 "invalid history item");
370 m_historyLoadingFromList = true;
371 LoadUrl(item->GetUrl());
372 m_historyPosition = pos;
373 }
374
375 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetBackwardHistory()
376 {
377 wxVector<wxSharedPtr<wxWebHistoryItem> > backhist;
378 //As we don't have std::copy or an iterator constructor in the wxwidgets
379 //native vector we construct it by hand
380 for(int i = 0; i < m_historyPosition; i++)
381 {
382 backhist.push_back(m_historyList[i]);
383 }
384 return backhist;
385 }
386
387 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetForwardHistory()
388 {
389 wxVector<wxSharedPtr<wxWebHistoryItem> > forwardhist;
390 //As we don't have std::copy or an iterator constructor in the wxwidgets
391 //native vector we construct it by hand
392 for(int i = m_historyPosition + 1; i < static_cast<int>(m_historyList.size()); i++)
393 {
394 forwardhist.push_back(m_historyList[i]);
395 }
396 return forwardhist;
397 }
398
399 void wxWebViewIE::GoBack()
400 {
401 LoadHistoryItem(m_historyList[m_historyPosition - 1]);
402 }
403
404 void wxWebViewIE::GoForward()
405 {
406 LoadHistoryItem(m_historyList[m_historyPosition + 1]);
407 }
408
409 void wxWebViewIE::Stop()
410 {
411 m_ie.CallMethod("Stop");
412 }
413
414 void wxWebViewIE::ClearHistory()
415 {
416 m_historyList.clear();
417 m_historyPosition = -1;
418 }
419
420 void wxWebViewIE::EnableHistory(bool enable)
421 {
422 m_historyEnabled = enable;
423 m_historyList.clear();
424 m_historyPosition = -1;
425 }
426
427 void wxWebViewIE::Reload(wxWebViewReloadFlags flags)
428 {
429 VARIANTARG level;
430 VariantInit(&level);
431 V_VT(&level) = VT_I2;
432
433 switch(flags)
434 {
435 case wxWEB_VIEW_RELOAD_DEFAULT:
436 V_I2(&level) = REFRESH_NORMAL;
437 break;
438 case wxWEB_VIEW_RELOAD_NO_CACHE:
439 V_I2(&level) = REFRESH_COMPLETELY;
440 break;
441 default:
442 wxFAIL_MSG("Unexpected reload type");
443 }
444
445 m_webBrowser->Refresh2(&level);
446 }
447
448 bool wxWebViewIE::IsOfflineMode()
449 {
450 wxVariant out = m_ie.GetProperty("Offline");
451
452 wxASSERT(out.GetType() == "bool");
453
454 return out.GetBool();
455 }
456
457 void wxWebViewIE::SetOfflineMode(bool offline)
458 {
459 // FIXME: the wxWidgets docs do not really document what the return
460 // parameter of PutProperty is
461 const bool success = m_ie.PutProperty("Offline", (offline ?
462 VARIANT_TRUE :
463 VARIANT_FALSE));
464 wxASSERT(success);
465 }
466
467 bool wxWebViewIE::IsBusy()
468 {
469 if (m_isBusy) return true;
470
471 wxVariant out = m_ie.GetProperty("Busy");
472
473 wxASSERT(out.GetType() == "bool");
474
475 return out.GetBool();
476 }
477
478 wxString wxWebViewIE::GetCurrentURL()
479 {
480 wxVariant out = m_ie.GetProperty("LocationURL");
481
482 wxASSERT(out.GetType() == "string");
483 return out.GetString();
484 }
485
486 wxString wxWebViewIE::GetCurrentTitle()
487 {
488 IHTMLDocument2* document = GetDocument();
489 BSTR title;
490
491 document->get_nameProp(&title);
492 document->Release();
493 return wxString(title);
494 }
495
496 bool wxWebViewIE::CanCut()
497 {
498 return CanExecCommand("Cut");
499 }
500
501 bool wxWebViewIE::CanCopy()
502 {
503 return CanExecCommand("Copy");
504 }
505 bool wxWebViewIE::CanPaste()
506 {
507 return CanExecCommand("Paste");
508 }
509
510 void wxWebViewIE::Cut()
511 {
512 ExecCommand("Cut");
513 }
514
515 void wxWebViewIE::Copy()
516 {
517 ExecCommand("Copy");
518 }
519
520 void wxWebViewIE::Paste()
521 {
522 ExecCommand("Paste");
523 }
524
525 bool wxWebViewIE::CanUndo()
526 {
527 return CanExecCommand("Undo");
528 }
529 bool wxWebViewIE::CanRedo()
530 {
531 return CanExecCommand("Redo");
532 }
533
534 void wxWebViewIE::Undo()
535 {
536 ExecCommand("Undo");
537 }
538
539 void wxWebViewIE::Redo()
540 {
541 ExecCommand("Redo");
542 }
543
544 void wxWebViewIE::SetEditable(bool enable)
545 {
546 IHTMLDocument2* document = GetDocument();
547 if( enable )
548 document->put_designMode(SysAllocString(L"On"));
549 else
550 document->put_designMode(SysAllocString(L"Off"));
551
552 document->Release();
553 }
554
555 bool wxWebViewIE::IsEditable()
556 {
557 IHTMLDocument2* document = GetDocument();
558 BSTR mode;
559 document->get_designMode(&mode);
560 document->Release();
561 if(wxString(mode) == "On")
562 return true;
563 else
564 return false;
565 }
566
567 void wxWebViewIE::SelectAll()
568 {
569 ExecCommand("SelectAll");
570 }
571
572 bool wxWebViewIE::HasSelection()
573 {
574 IHTMLDocument2* document = GetDocument();
575 IHTMLSelectionObject* selection;
576 wxString sel;
577 HRESULT hr = document->get_selection(&selection);
578 if(SUCCEEDED(hr))
579 {
580 BSTR type;
581 selection->get_type(&type);
582 sel = wxString(type);
583 selection->Release();
584 }
585 document->Release();
586 return sel != "None";
587 }
588
589 void wxWebViewIE::DeleteSelection()
590 {
591 ExecCommand("Delete");
592 }
593
594 wxString wxWebViewIE::GetSelectedText()
595 {
596 IHTMLDocument2* document = GetDocument();
597 IHTMLSelectionObject* selection;
598 wxString selected;
599 HRESULT hr = document->get_selection(&selection);
600 if(SUCCEEDED(hr))
601 {
602 IDispatch* disrange;
603 hr = selection->createRange(&disrange);
604 if(SUCCEEDED(hr))
605 {
606 IHTMLTxtRange* range;
607 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
608 if(SUCCEEDED(hr))
609 {
610 BSTR text;
611 range->get_text(&text);
612 selected = wxString(text);
613 range->Release();
614 }
615 disrange->Release();
616 }
617 selection->Release();
618 }
619 document->Release();
620 return selected;
621 }
622
623 wxString wxWebViewIE::GetSelectedSource()
624 {
625 IHTMLDocument2* document = GetDocument();
626 IHTMLSelectionObject* selection;
627 wxString selected;
628 HRESULT hr = document->get_selection(&selection);
629 if(SUCCEEDED(hr))
630 {
631 IDispatch* disrange;
632 hr = selection->createRange(&disrange);
633 if(SUCCEEDED(hr))
634 {
635 IHTMLTxtRange* range;
636 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
637 if(SUCCEEDED(hr))
638 {
639 BSTR text;
640 range->get_htmlText(&text);
641 selected = wxString(text);
642 range->Release();
643 }
644 disrange->Release();
645 }
646 selection->Release();
647 }
648 document->Release();
649 return selected;
650 }
651
652 void wxWebViewIE::ClearSelection()
653 {
654 IHTMLDocument2* document = GetDocument();
655 IHTMLSelectionObject* selection;
656 wxString selected;
657 HRESULT hr = document->get_selection(&selection);
658 if(SUCCEEDED(hr))
659 {
660 selection->empty();
661 selection->Release();
662 }
663 document->Release();
664 }
665
666 wxString wxWebViewIE::GetPageText()
667 {
668 IHTMLDocument2* document = GetDocument();
669 wxString text;
670 IHTMLElement* body;
671 HRESULT hr = document->get_body(&body);
672 if(SUCCEEDED(hr))
673 {
674 BSTR out;
675 body->get_innerText(&out);
676 text = wxString(out);
677 body->Release();
678 }
679 document->Release();
680 return text;
681 }
682
683 void wxWebViewIE::RunScript(const wxString& javascript)
684 {
685 IHTMLDocument2* document = GetDocument();
686 IHTMLWindow2* window;
687 wxString language = "javascript";
688 HRESULT hr = document->get_parentWindow(&window);
689 if(SUCCEEDED(hr))
690 {
691 VARIANT level;
692 VariantInit(&level);
693 V_VT(&level) = VT_EMPTY;
694 window->execScript(SysAllocString(javascript), SysAllocString(language), &level);
695 }
696 document->Release();
697 }
698
699 bool wxWebViewIE::CanExecCommand(wxString command)
700 {
701 IHTMLDocument2* document = GetDocument();
702 VARIANT_BOOL enabled;
703
704 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
705 document->Release();
706
707 return (enabled == VARIANT_TRUE);
708 }
709
710 void wxWebViewIE::ExecCommand(wxString command)
711 {
712 IHTMLDocument2* document = GetDocument();
713 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
714 document->Release();
715 }
716
717 IHTMLDocument2* wxWebViewIE::GetDocument()
718 {
719 wxVariant variant = m_ie.GetProperty("Document");
720 IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr();
721
722 wxASSERT(document);
723
724 return document;
725 }
726
727 void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
728 {
729 if (m_webBrowser == NULL) return;
730
731 switch (evt.GetDispatchId())
732 {
733 case DISPID_BEFORENAVIGATE2:
734 {
735 m_isBusy = true;
736
737 wxString url = evt[1].GetString();
738 wxString target = evt[3].GetString();
739
740 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
741 GetId(), url, target, true);
742 event.SetEventObject(this);
743 HandleWindowEvent(event);
744
745 if (event.IsVetoed())
746 {
747 wxActiveXEventNativeMSW* nativeParams =
748 evt.GetNativeParameters();
749 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
750 }
751
752 // at this point, either the navigation event has been cancelled
753 // and we're not busy, either it was accepted and IWebBrowser2's
754 // Busy property will be true; so we don't need our override
755 // flag anymore.
756 m_isBusy = false;
757
758 break;
759 }
760
761 case DISPID_NAVIGATECOMPLETE2:
762 {
763 wxString url = evt[1].GetString();
764 // TODO: set target parameter if possible
765 wxString target = wxEmptyString;
766 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
767 GetId(), url, target, false);
768 event.SetEventObject(this);
769 HandleWindowEvent(event);
770 break;
771 }
772
773 case DISPID_PROGRESSCHANGE:
774 {
775 // download progress
776 break;
777 }
778
779 case DISPID_DOCUMENTCOMPLETE:
780 {
781 //Only send a complete even if we are actually finished, this brings
782 //the event in to line with webkit
783 READYSTATE rs;
784 m_webBrowser->get_ReadyState( &rs );
785 if(rs != READYSTATE_COMPLETE)
786 break;
787
788 wxString url = evt[1].GetString();
789
790 //As we are complete we also add to the history list, but not if the
791 //page is not the main page, ie it is a subframe
792 if(m_historyEnabled && !m_historyLoadingFromList && url == GetCurrentURL())
793 {
794 //If we are not at the end of the list, then erase everything
795 //between us and the end before adding the new page
796 if(m_historyPosition != static_cast<int>(m_historyList.size()) - 1)
797 {
798 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
799 m_historyList.end());
800 }
801 wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem(url, GetCurrentTitle()));
802 m_historyList.push_back(item);
803 m_historyPosition++;
804 }
805 //Reset as we are done now
806 m_historyLoadingFromList = false;
807 // TODO: set target parameter if possible
808 wxString target = wxEmptyString;
809 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, GetId(),
810 url, target, false);
811 event.SetEventObject(this);
812 HandleWindowEvent(event);
813 break;
814 }
815
816 case DISPID_STATUSTEXTCHANGE:
817 {
818 break;
819 }
820
821 case DISPID_TITLECHANGE:
822 {
823 break;
824 }
825
826 case DISPID_NAVIGATEERROR:
827 {
828 wxWebNavigationError errorType = wxWEB_NAV_ERR_OTHER;
829 wxString errorCode = "?";
830 switch (evt[3].GetLong())
831 {
832 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
833 errorCode = "INET_E_INVALID_URL";
834 errorType = wxWEB_NAV_ERR_REQUEST;
835 break;
836 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
837 errorCode = "INET_E_NO_SESSION";
838 errorType = wxWEB_NAV_ERR_CONNECTION;
839 break;
840 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
841 errorCode = "INET_E_CANNOT_CONNECT";
842 errorType = wxWEB_NAV_ERR_CONNECTION;
843 break;
844 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
845 errorCode = "INET_E_RESOURCE_NOT_FOUND";
846 errorType = wxWEB_NAV_ERR_NOT_FOUND;
847 break;
848 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
849 errorCode = "INET_E_OBJECT_NOT_FOUND";
850 errorType = wxWEB_NAV_ERR_NOT_FOUND;
851 break;
852 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
853 errorCode = "INET_E_DATA_NOT_AVAILABLE";
854 errorType = wxWEB_NAV_ERR_NOT_FOUND;
855 break;
856 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
857 errorCode = "INET_E_DOWNLOAD_FAILURE";
858 errorType = wxWEB_NAV_ERR_CONNECTION;
859 break;
860 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
861 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
862 errorType = wxWEB_NAV_ERR_AUTH;
863 break;
864 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
865 errorCode = "INET_E_NO_VALID_MEDIA";
866 errorType = wxWEB_NAV_ERR_REQUEST;
867 break;
868 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
869 errorCode = "INET_E_CONNECTION_TIMEOUT";
870 errorType = wxWEB_NAV_ERR_CONNECTION;
871 break;
872 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
873 errorCode = "INET_E_INVALID_REQUEST";
874 errorType = wxWEB_NAV_ERR_REQUEST;
875 break;
876 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
877 errorCode = "INET_E_UNKNOWN_PROTOCOL";
878 errorType = wxWEB_NAV_ERR_REQUEST;
879 break;
880 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
881 errorCode = "INET_E_SECURITY_PROBLEM";
882 errorType = wxWEB_NAV_ERR_SECURITY;
883 break;
884 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
885 errorCode = "INET_E_CANNOT_LOAD_DATA";
886 errorType = wxWEB_NAV_ERR_OTHER;
887 break;
888 case INET_E_CANNOT_INSTANTIATE_OBJECT:
889 // CoCreateInstance will return an error code if this happens,
890 // we'll handle this above.
891 return;
892 break;
893 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
894 errorCode = "INET_E_REDIRECT_FAILED";
895 errorType = wxWEB_NAV_ERR_OTHER;
896 break;
897 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
898 errorCode = "INET_E_REDIRECT_TO_DIR";
899 errorType = wxWEB_NAV_ERR_REQUEST;
900 break;
901 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
902 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
903 errorType = wxWEB_NAV_ERR_OTHER;
904 break;
905 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
906 errorCode = "INET_E_USE_EXTEND_BINDING";
907 errorType = wxWEB_NAV_ERR_OTHER;
908 break;
909 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
910 errorCode = "INET_E_TERMINATED_BIND";
911 errorType = wxWEB_NAV_ERR_OTHER;
912 break;
913 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
914 errorCode = "INET_E_INVALID_CERTIFICATE";
915 errorType = wxWEB_NAV_ERR_CERTIFICATE;
916 break;
917 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
918 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
919 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
920 break;
921 case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704)
922 // cancel request cancelled...
923 errorCode = "INET_E_RESULT_DISPATCHED";
924 errorType = wxWEB_NAV_ERR_OTHER;
925 break;
926 case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448)
927 errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE";
928 errorType = wxWEB_NAV_ERR_SECURITY;
929 break;
930 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY:
931 errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
932 errorType = wxWEB_NAV_ERR_SECURITY;
933 break;
934 case INET_E_CODE_INSTALL_SUPPRESSED:
935 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
936 errorType = wxWEB_NAV_ERR_SECURITY;
937 break;
938 }
939
940 wxString url = evt[1].GetString();
941 wxString target = evt[2].GetString();
942 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
943 url, target, false);
944 event.SetEventObject(this);
945 event.SetInt(errorType);
946 event.SetString(errorCode);
947 HandleWindowEvent(event);
948 break;
949 }
950 case DISPID_NEWWINDOW3:
951 {
952 wxString url = evt[4].GetString();
953
954 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
955 GetId(), url, wxEmptyString, true);
956 event.SetEventObject(this);
957 HandleWindowEvent(event);
958
959 //We always cancel this event otherwise an Internet Exporer window
960 //is opened for the url
961 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
962 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE;
963 break;
964 }
965 }
966
967 evt.Skip();
968 }
969
970 VirtualProtocol::VirtualProtocol()
971 {
972 m_refCount = 0;
973 m_file = NULL;
974 m_fileSys = new wxFileSystem;
975 }
976
977 VirtualProtocol::~VirtualProtocol()
978 {
979 wxDELETE(m_fileSys);
980 }
981
982 ULONG VirtualProtocol::AddRef()
983 {
984 m_refCount++;
985 return m_refCount;
986 }
987
988 HRESULT VirtualProtocol::QueryInterface(REFIID riid, void **ppvObject)
989 {
990 if ((riid == IID_IUnknown) || (riid == IID_IInternetProtocol)
991 || (riid == IID_IInternetProtocolRoot))
992 {
993 *ppvObject = this;
994 AddRef();
995 return S_OK;
996 }
997 else
998 {
999 *ppvObject = NULL;
1000 return E_POINTER;
1001 }
1002 }
1003
1004 ULONG VirtualProtocol::Release()
1005 {
1006 m_refCount--;
1007 if (m_refCount > 0)
1008 {
1009 return m_refCount;
1010 }
1011 else
1012 {
1013 delete this;
1014 return 0;
1015 }
1016 }
1017
1018 HRESULT VirtualProtocol::Start(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSink,
1019 IInternetBindInfo *pOIBindInfo, DWORD grfPI,
1020 HANDLE_PTR dwReserved)
1021 {
1022 m_protocolSink = pOIProtSink;
1023 //We have to clean up incoming paths from the webview control as they are
1024 //not properly escaped, see also the comment in filesys.cpp line 668
1025 wxString path = wxString(szUrl).BeforeFirst(':') + ":" +
1026 EscapeFileNameCharsInURL(wxString(szUrl).AfterFirst(':'));
1027 path.Replace("///", "/");
1028 m_file = m_fileSys->OpenFile(path);
1029
1030 if(!m_file)
1031 return INET_E_RESOURCE_NOT_FOUND;
1032
1033 //We return the stream length for current and total size as we can always
1034 //read the whole file from the stream
1035 m_protocolSink->ReportData(BSCF_FIRSTDATANOTIFICATION |
1036 BSCF_DATAFULLYAVAILABLE |
1037 BSCF_LASTDATANOTIFICATION,
1038 m_file->GetStream()->GetLength(),
1039 m_file->GetStream()->GetLength());
1040 return S_OK;
1041 }
1042
1043 HRESULT VirtualProtocol::Read(void *pv, ULONG cb, ULONG *pcbRead)
1044 {
1045 //If the file is null we return false to indicte it is finished
1046 if(!m_file)
1047 return S_FALSE;
1048
1049 wxStreamError err = m_file->GetStream()->Read(pv, cb).GetLastError();
1050 *pcbRead = m_file->GetStream()->LastRead();
1051
1052 if(err == wxSTREAM_NO_ERROR)
1053 {
1054 if(*pcbRead < cb)
1055 {
1056 wxDELETE(m_file);
1057 m_protocolSink->ReportResult(S_OK, 0, NULL);
1058 }
1059 //As we are not eof there is more data
1060 return S_OK;
1061 }
1062 else if(err == wxSTREAM_EOF)
1063 {
1064 wxDELETE(m_file);
1065 m_protocolSink->ReportResult(S_OK, 0, NULL);
1066 //We are eof and so finished
1067 return S_OK;
1068 }
1069 else if(err == wxSTREAM_READ_ERROR)
1070 {
1071 wxDELETE(m_file);
1072 return INET_E_DOWNLOAD_FAILURE;
1073 }
1074 }
1075
1076 HRESULT ClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid,
1077 void ** ppvObject)
1078 {
1079 if (pUnkOuter)
1080 return CLASS_E_NOAGGREGATION;
1081 VirtualProtocol* vp = new VirtualProtocol;
1082 vp->AddRef();
1083 HRESULT hr = vp->QueryInterface(riid, ppvObject);
1084 vp->Release();
1085 return hr;
1086
1087 }
1088
1089 STDMETHODIMP ClassFactory::LockServer(BOOL fLock)
1090 {
1091 return S_OK;
1092
1093 }
1094
1095 ULONG ClassFactory::AddRef(void)
1096 {
1097 m_refCount++;
1098 return m_refCount;
1099 }
1100
1101 HRESULT ClassFactory::QueryInterface(REFIID riid, void **ppvObject)
1102 {
1103 if ((riid == IID_IUnknown) || (riid == IID_IClassFactory))
1104 {
1105 *ppvObject = this;
1106 AddRef();
1107 return S_OK;
1108 }
1109 else
1110 {
1111 *ppvObject = NULL;
1112 return E_POINTER;
1113 }
1114
1115 }
1116
1117 ULONG ClassFactory::Release(void)
1118 {
1119 m_refCount--;
1120 if (m_refCount > 0)
1121 {
1122 return m_refCount;
1123 }
1124 else
1125 {
1126 delete this;
1127 return 0;
1128 }
1129
1130 }
1131
1132 #endif