Add support for retrieving the currently selected text. Implement on all backends...
[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
27 // Various definitions are missing from mingw
28 #ifdef __MINGW32__
29 typedef enum CommandStateChangeConstants {
30 CSC_UPDATECOMMANDS = (int) 0xFFFFFFFF,
31 CSC_NAVIGATEFORWARD = 0x1,
32 CSC_NAVIGATEBACK = 0x2
33 } CommandStateChangeConstants;
34
35 #define DISPID_COMMANDSTATECHANGE 105
36 #define DISPID_NAVIGATECOMPLETE2 252
37 #define DISPID_NAVIGATEERROR 271
38 #define DISPID_NEWWINDOW3 273
39 #define OLECMDID_OPTICAL_ZOOM 63
40 #define INET_E_ERROR_FIRST 0x800C0002L
41 #define INET_E_INVALID_URL 0x800C0002L
42 #define INET_E_NO_SESSION 0x800C0003L
43 #define INET_E_CANNOT_CONNECT 0x800C0004L
44 #define INET_E_RESOURCE_NOT_FOUND 0x800C0005L
45 #define INET_E_OBJECT_NOT_FOUND 0x800C0006L
46 #define INET_E_DATA_NOT_AVAILABLE 0x800C0007L
47 #define INET_E_DOWNLOAD_FAILURE 0x800C0008L
48 #define INET_E_AUTHENTICATION_REQUIRED 0x800C0009L
49 #define INET_E_NO_VALID_MEDIA 0x800C000AL
50 #define INET_E_CONNECTION_TIMEOUT 0x800C000BL
51 #define INET_E_INVALID_REQUEST 0x800C000CL
52 #define INET_E_UNKNOWN_PROTOCOL 0x800C000DL
53 #define INET_E_SECURITY_PROBLEM 0x800C000EL
54 #define INET_E_CANNOT_LOAD_DATA 0x800C000FL
55 #define INET_E_CANNOT_INSTANTIATE_OBJECT 0x800C0010L
56 #define INET_E_QUERYOPTION_UNKNOWN 0x800C0013L
57 #define INET_E_REDIRECT_FAILED 0x800C0014L
58 #define INET_E_REDIRECT_TO_DIR 0x800C0015L
59 #define INET_E_CANNOT_LOCK_REQUEST 0x800C0016L
60 #define INET_E_USE_EXTEND_BINDING 0x800C0017L
61 #define INET_E_TERMINATED_BIND 0x800C0018L
62 #define INET_E_INVALID_CERTIFICATE 0x800C0019L
63 #define INET_E_CODE_DOWNLOAD_DECLINED 0x800C0100L
64 #define INET_E_RESULT_DISPATCHED 0x800C0200L
65 #define INET_E_CANNOT_REPLACE_SFP_FILE 0x800C0300L
66 #define INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY 0x800C0500L
67 #define INET_E_CODE_INSTALL_SUPPRESSED 0x800C0400L
68
69 #define REFRESH_NORMAL 0
70 #define REFRESH_COMPLETELY 3
71 #endif
72
73 BEGIN_EVENT_TABLE(wxWebViewIE, wxControl)
74 EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent)
75 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg)
76 END_EVENT_TABLE()
77
78 bool wxWebViewIE::Create(wxWindow* parent,
79 wxWindowID id,
80 const wxString& url,
81 const wxPoint& pos,
82 const wxSize& size,
83 long style,
84 const wxString& name)
85 {
86 if (!wxControl::Create(parent, id, pos, size, style,
87 wxDefaultValidator, name))
88 {
89 return false;
90 }
91
92 m_webBrowser = NULL;
93 m_canNavigateBack = false;
94 m_canNavigateForward = false;
95 m_isBusy = false;
96 m_historyLoadingFromList = false;
97 m_historyEnabled = true;
98 m_historyPosition = -1;
99
100 if (::CoCreateInstance(CLSID_WebBrowser, NULL,
101 CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
102 IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
103 {
104 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
105 return false;
106 }
107
108 m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
109
110 m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
111 m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
112 //m_webBrowser->put_Silent(VARIANT_FALSE);
113
114 m_container = new wxActiveXContainer(this, IID_IWebBrowser2, m_webBrowser);
115
116 SetBackgroundStyle(wxBG_STYLE_PAINT);
117 SetDoubleBuffered(true);
118 LoadUrl(url);
119 return true;
120 }
121
122
123 void wxWebViewIE::LoadUrl(const wxString& url)
124 {
125 wxVariant out = m_ie.CallMethod("Navigate", (BSTR) url.wc_str(),
126 NULL, NULL, NULL, NULL);
127
128 // FIXME: why is out value null??
129 //(HRESULT)(out.GetLong()) == S_OK;
130 }
131
132 void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
133 {
134 LoadUrl("about:blank");
135
136 // Let the wx events generated for navigation events be processed, so
137 // that the underlying IE component completes its Document object.
138 // FIXME: calling wxYield is not elegant nor very reliable probably
139 wxYield();
140
141 // TODO: consider the "baseUrl" parameter if possible
142 // TODO: consider encoding
143 BSTR bstr = SysAllocString(html.wc_str());
144
145 // Creates a new one-dimensional array
146 SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
147 if (psaStrings != NULL)
148 {
149 VARIANT *param;
150 HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
151 param->vt = VT_BSTR;
152 param->bstrVal = bstr;
153
154 hr = SafeArrayUnaccessData(psaStrings);
155 IHTMLDocument2* document = GetDocument();
156 document->write(psaStrings);
157
158 // SafeArrayDestroy calls SysFreeString for each BSTR
159 SafeArrayDestroy(psaStrings);
160 }
161 else
162 {
163 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
164 }
165
166 }
167
168 wxString wxWebViewIE::GetPageSource()
169 {
170 IHTMLDocument2* document = GetDocument();
171 IHTMLElement *bodyTag = NULL;
172 IHTMLElement *htmlTag = NULL;
173 document->get_body(&bodyTag);
174 wxASSERT(bodyTag != NULL);
175
176 document->Release();
177 bodyTag->get_parentElement(&htmlTag);
178 wxASSERT(htmlTag != NULL);
179
180 BSTR bstr;
181 htmlTag->get_outerHTML(&bstr);
182
183 bodyTag->Release();
184 htmlTag->Release();
185
186 //wxMessageBox(wxString(bstr));
187
188 // TODO: check encoding
189 return wxString(bstr);
190 }
191
192 // FIXME? retrieve OLECMDID_GETZOOMRANGE instead of hardcoding range 0-4
193 wxWebViewZoom wxWebViewIE::GetZoom()
194 {
195 const int zoom = GetIETextZoom();
196
197 switch (zoom)
198 {
199 case 0:
200 return wxWEB_VIEW_ZOOM_TINY;
201 break;
202 case 1:
203 return wxWEB_VIEW_ZOOM_SMALL;
204 break;
205 case 2:
206 return wxWEB_VIEW_ZOOM_MEDIUM;
207 break;
208 case 3:
209 return wxWEB_VIEW_ZOOM_LARGE;
210 break;
211 case 4:
212 return wxWEB_VIEW_ZOOM_LARGEST;
213 break;
214 default:
215 wxASSERT(false);
216 return wxWEB_VIEW_ZOOM_MEDIUM;
217 }
218 }
219 void wxWebViewIE::SetZoom(wxWebViewZoom zoom)
220 {
221 // I know I could cast from enum to int since wxWebViewZoom happens to
222 // match with IE's zoom levels, but I don't like doing that, what if enum
223 // values change...
224 switch (zoom)
225 {
226 case wxWEB_VIEW_ZOOM_TINY:
227 SetIETextZoom(0);
228 break;
229 case wxWEB_VIEW_ZOOM_SMALL:
230 SetIETextZoom(1);
231 break;
232 case wxWEB_VIEW_ZOOM_MEDIUM:
233 SetIETextZoom(2);
234 break;
235 case wxWEB_VIEW_ZOOM_LARGE:
236 SetIETextZoom(3);
237 break;
238 case wxWEB_VIEW_ZOOM_LARGEST:
239 SetIETextZoom(4);
240 break;
241 default:
242 wxASSERT(false);
243 }
244 }
245
246 void wxWebViewIE::SetIETextZoom(int level)
247 {
248 VARIANT zoomVariant;
249 VariantInit (&zoomVariant);
250 V_VT(&zoomVariant) = VT_I4;
251 V_I4(&zoomVariant) = level;
252
253 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
254 OLECMDEXECOPT_DONTPROMPTUSER,
255 &zoomVariant, NULL);
256 wxASSERT (result == S_OK);
257
258 VariantClear (&zoomVariant);
259 }
260
261 int wxWebViewIE::GetIETextZoom()
262 {
263 VARIANT zoomVariant;
264 VariantInit (&zoomVariant);
265 V_VT(&zoomVariant) = VT_I4;
266 V_I4(&zoomVariant) = 4;
267
268 HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM,
269 OLECMDEXECOPT_DONTPROMPTUSER,
270 NULL, &zoomVariant);
271 wxASSERT (result == S_OK);
272
273 int zoom = V_I4(&zoomVariant);
274 // wxMessageBox(wxString::Format("Zoom : %i", zoom));
275 VariantClear (&zoomVariant);
276
277 return zoom;
278 }
279
280 void wxWebViewIE::SetIEOpticalZoom(float zoom)
281 {
282 // TODO: add support for optical zoom (IE7+ only)
283
284 // TODO: get range from OLECMDID_OPTICAL_GETZOOMRANGE instead of hardcoding?
285 wxASSERT(zoom >= 10.0f);
286 wxASSERT(zoom <= 1000.0f);
287
288 VARIANT zoomVariant;
289 VariantInit (&zoomVariant);
290 V_VT(&zoomVariant) = VT_I4;
291 V_I4(&zoomVariant) = (zoom * 100.0f);
292
293 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
294 OLECMDEXECOPT_DODEFAULT,
295 &zoomVariant,
296 NULL);
297 wxASSERT (result == S_OK);
298 }
299
300 float wxWebViewIE::GetIEOpticalZoom()
301 {
302 // TODO: add support for optical zoom (IE7+ only)
303
304 VARIANT zoomVariant;
305 VariantInit (&zoomVariant);
306 V_VT(&zoomVariant) = VT_I4;
307 V_I4(&zoomVariant) = -1;
308
309 HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM,
310 OLECMDEXECOPT_DODEFAULT, NULL,
311 &zoomVariant);
312 wxASSERT (result == S_OK);
313
314 const int zoom = V_I4(&zoomVariant);
315 VariantClear (&zoomVariant);
316
317 return zoom / 100.0f;
318 }
319
320 void wxWebViewIE::SetZoomType(wxWebViewZoomType)
321 {
322 // TODO: add support for optical zoom (IE7+ only)
323 wxASSERT(false);
324 }
325
326 wxWebViewZoomType wxWebViewIE::GetZoomType() const
327 {
328 // TODO: add support for optical zoom (IE7+ only)
329 return wxWEB_VIEW_ZOOM_TYPE_TEXT;
330 }
331
332 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType) const
333 {
334 // both are supported
335 // TODO: IE6 only supports text zoom, check if it's IE6 first
336 return true;
337 }
338
339 void wxWebViewIE::Print()
340 {
341 m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW,
342 OLECMDEXECOPT_DODEFAULT, NULL, NULL);
343 }
344
345 bool wxWebViewIE::CanGoBack()
346 {
347 if(m_historyEnabled)
348 return m_historyPosition > 0;
349 else
350 return false;
351 }
352
353 bool wxWebViewIE::CanGoForward()
354 {
355 if(m_historyEnabled)
356 return m_historyPosition != m_historyList.size() - 1;
357 else
358 return false;
359 }
360
361 void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item)
362 {
363 int pos = -1;
364 for(unsigned int i = 0; i < m_historyList.size(); i++)
365 {
366 //We compare the actual pointers to find the correct item
367 if(m_historyList[i].get() == item.get())
368 pos = i;
369 }
370 wxASSERT_MSG(pos != m_historyList.size(), "invalid history item");
371 m_historyLoadingFromList = true;
372 LoadUrl(item->GetUrl());
373 m_historyPosition = pos;
374 }
375
376 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetBackwardHistory()
377 {
378 wxVector<wxSharedPtr<wxWebHistoryItem> > backhist;
379 //As we don't have std::copy or an iterator constructor in the wxwidgets
380 //native vector we construct it by hand
381 for(int i = 0; i < m_historyPosition; i++)
382 {
383 backhist.push_back(m_historyList[i]);
384 }
385 return backhist;
386 }
387
388 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetForwardHistory()
389 {
390 wxVector<wxSharedPtr<wxWebHistoryItem> > forwardhist;
391 //As we don't have std::copy or an iterator constructor in the wxwidgets
392 //native vector we construct it by hand
393 for(int i = m_historyPosition + 1; i < m_historyList.size(); i++)
394 {
395 forwardhist.push_back(m_historyList[i]);
396 }
397 return forwardhist;
398 }
399
400 void wxWebViewIE::GoBack()
401 {
402 LoadHistoryItem(m_historyList[m_historyPosition - 1]);
403 }
404
405 void wxWebViewIE::GoForward()
406 {
407 LoadHistoryItem(m_historyList[m_historyPosition + 1]);
408 }
409
410 void wxWebViewIE::Stop()
411 {
412 wxVariant out = m_ie.CallMethod("Stop");
413
414 // FIXME: why is out value null??
415 //return (HRESULT)(out.GetLong()) == S_OK;
416 }
417
418 void wxWebViewIE::ClearHistory()
419 {
420 m_historyList.clear();
421 m_historyPosition = -1;
422 }
423
424 void wxWebViewIE::EnableHistory(bool enable)
425 {
426 m_historyEnabled = enable;
427 m_historyList.clear();
428 m_historyPosition = -1;
429 }
430
431 void wxWebViewIE::Reload(wxWebViewReloadFlags flags)
432 {
433 VARIANTARG level;
434 VariantInit(&level);
435 V_VT(&level) = VT_I2;
436
437 switch(flags)
438 {
439 case wxWEB_VIEW_RELOAD_DEFAULT:
440 V_I2(&level) = REFRESH_NORMAL;
441 break;
442 case wxWEB_VIEW_RELOAD_NO_CACHE:
443 V_I2(&level) = REFRESH_COMPLETELY;
444 break;
445 default:
446 wxFAIL_MSG("Unexpected reload type");
447 }
448
449 m_webBrowser->Refresh2(&level);
450 }
451
452 bool wxWebViewIE::IsOfflineMode()
453 {
454 wxVariant out = m_ie.GetProperty("Offline");
455
456 wxASSERT(out.GetType() == "bool");
457
458 return out.GetBool();
459 }
460
461 void wxWebViewIE::SetOfflineMode(bool offline)
462 {
463 // FIXME: the wxWidgets docs do not really document what the return
464 // parameter of PutProperty is
465 const bool success = m_ie.PutProperty("Offline", (offline ?
466 VARIANT_TRUE :
467 VARIANT_FALSE));
468 wxASSERT(success);
469 }
470
471 bool wxWebViewIE::IsBusy()
472 {
473 if (m_isBusy) return true;
474
475 wxVariant out = m_ie.GetProperty("Busy");
476
477 wxASSERT(out.GetType() == "bool");
478
479 return out.GetBool();
480 }
481
482 wxString wxWebViewIE::GetCurrentURL()
483 {
484 wxVariant out = m_ie.GetProperty("LocationURL");
485
486 wxASSERT(out.GetType() == "string");
487 return out.GetString();
488 }
489
490 wxString wxWebViewIE::GetCurrentTitle()
491 {
492 IHTMLDocument2* document = GetDocument();
493
494 BSTR title;
495 document->get_nameProp(&title);
496 return wxString(title);
497 }
498
499 bool wxWebViewIE::CanCut()
500 {
501 return CanExecCommand("Cut");
502 }
503
504 bool wxWebViewIE::CanCopy()
505 {
506 return CanExecCommand("Copy");
507 }
508 bool wxWebViewIE::CanPaste()
509 {
510 return CanExecCommand("Paste");
511 }
512
513 void wxWebViewIE::Cut()
514 {
515 ExecCommand("Cut");
516 }
517
518 void wxWebViewIE::Copy()
519 {
520 ExecCommand("Copy");
521 }
522
523 void wxWebViewIE::Paste()
524 {
525 ExecCommand("Paste");
526 }
527
528 bool wxWebViewIE::CanUndo()
529 {
530 return CanExecCommand("Undo");
531 }
532 bool wxWebViewIE::CanRedo()
533 {
534 return CanExecCommand("Redo");
535 }
536
537 void wxWebViewIE::Undo()
538 {
539 ExecCommand("Undo");
540 }
541
542 void wxWebViewIE::Redo()
543 {
544 ExecCommand("Redo");
545 }
546
547 void wxWebViewIE::SetEditable(bool enable)
548 {
549 IHTMLDocument2* document = GetDocument();
550 if( enable )
551 document->put_designMode(SysAllocString(L"On"));
552 else
553 document->put_designMode(SysAllocString(L"Off"));
554 }
555
556 bool wxWebViewIE::IsEditable()
557 {
558 IHTMLDocument2* document = GetDocument();
559 BSTR mode;
560 document->get_designMode(&mode);
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 document->get_selection(&selection);
577 BSTR type;
578 selection->get_type(&type);
579 return wxString(type) != "None";
580 }
581
582 void wxWebViewIE::DeleteSelection()
583 {
584 ExecCommand("Delete");
585 }
586
587 wxString wxWebViewIE::GetSelectedText()
588 {
589 IHTMLDocument2* document = GetDocument();
590 IHTMLSelectionObject* selection;
591 wxString selected;
592 HRESULT hr = document->get_selection(&selection);
593 if(SUCCEEDED(hr))
594 {
595 IDispatch* disrange;
596 hr = selection->createRange(&disrange);
597 if(SUCCEEDED(hr))
598 {
599 IHTMLTxtRange* range;
600 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
601 if(SUCCEEDED(hr))
602 {
603 BSTR text;
604 range->get_text(&text);
605 selected = wxString(text);
606 range->Release();
607 }
608 disrange->Release();
609 }
610 selection->Release();
611 }
612 document->Release();
613 return selected;
614 }
615
616 bool wxWebViewIE::CanExecCommand(wxString command)
617 {
618 IHTMLDocument2* document = GetDocument();
619
620 VARIANT_BOOL enabled;
621 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
622
623 return (enabled == VARIANT_TRUE);
624 }
625
626 void wxWebViewIE::ExecCommand(wxString command)
627 {
628 IHTMLDocument2* document = GetDocument();
629 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
630 }
631
632 IHTMLDocument2* wxWebViewIE::GetDocument()
633 {
634 wxVariant variant = m_ie.GetProperty("Document");
635 IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr();
636
637 wxASSERT(document);
638
639 return document;
640 }
641
642 void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
643 {
644 if (m_webBrowser == NULL) return;
645
646 switch (evt.GetDispatchId())
647 {
648 case DISPID_BEFORENAVIGATE2:
649 {
650 m_isBusy = true;
651
652 wxString url = evt[1].GetString();
653 wxString target = evt[3].GetString();
654
655 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
656 GetId(), url, target, true);
657 event.SetEventObject(this);
658 HandleWindowEvent(event);
659
660 if (event.IsVetoed())
661 {
662 wxActiveXEventNativeMSW* nativeParams =
663 evt.GetNativeParameters();
664 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
665 }
666
667 // at this point, either the navigation event has been cancelled
668 // and we're not busy, either it was accepted and IWebBrowser2's
669 // Busy property will be true; so we don't need our override
670 // flag anymore.
671 m_isBusy = false;
672
673 break;
674 }
675
676 case DISPID_NAVIGATECOMPLETE2:
677 {
678 wxString url = evt[1].GetString();
679 // TODO: set target parameter if possible
680 wxString target = wxEmptyString;
681 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
682 GetId(), url, target, false);
683 event.SetEventObject(this);
684 HandleWindowEvent(event);
685 break;
686 }
687
688 case DISPID_PROGRESSCHANGE:
689 {
690 // download progress
691 break;
692 }
693
694 case DISPID_DOCUMENTCOMPLETE:
695 {
696 //Only send a complete even if we are actually finished, this brings
697 //the event in to line with webkit
698 READYSTATE rs;
699 m_webBrowser->get_ReadyState( &rs );
700 if(rs != READYSTATE_COMPLETE)
701 break;
702
703 wxString url = evt[1].GetString();
704
705 //As we are complete we also add to the history list, but not if the
706 //page is not the main page, ie it is a subframe
707 if(m_historyEnabled && !m_historyLoadingFromList && url == GetCurrentURL())
708 {
709 //If we are not at the end of the list, then erase everything
710 //between us and the end before adding the new page
711 if(m_historyPosition != m_historyList.size() - 1)
712 {
713 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
714 m_historyList.end());
715 }
716 wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem(url, GetCurrentTitle()));
717 m_historyList.push_back(item);
718 m_historyPosition++;
719 }
720 //Reset as we are done now
721 m_historyLoadingFromList = false;
722 // TODO: set target parameter if possible
723 wxString target = wxEmptyString;
724 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, GetId(),
725 url, target, false);
726 event.SetEventObject(this);
727 HandleWindowEvent(event);
728 break;
729 }
730
731 case DISPID_STATUSTEXTCHANGE:
732 {
733 break;
734 }
735
736 case DISPID_TITLECHANGE:
737 {
738 break;
739 }
740
741 case DISPID_NAVIGATEERROR:
742 {
743 wxWebNavigationError errorType = wxWEB_NAV_ERR_OTHER;
744 wxString errorCode = "?";
745 switch (evt[3].GetLong())
746 {
747 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
748 errorCode = "INET_E_INVALID_URL";
749 errorType = wxWEB_NAV_ERR_REQUEST;
750 break;
751 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
752 errorCode = "INET_E_NO_SESSION";
753 errorType = wxWEB_NAV_ERR_CONNECTION;
754 break;
755 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
756 errorCode = "INET_E_CANNOT_CONNECT";
757 errorType = wxWEB_NAV_ERR_CONNECTION;
758 break;
759 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
760 errorCode = "INET_E_RESOURCE_NOT_FOUND";
761 errorType = wxWEB_NAV_ERR_NOT_FOUND;
762 break;
763 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
764 errorCode = "INET_E_OBJECT_NOT_FOUND";
765 errorType = wxWEB_NAV_ERR_NOT_FOUND;
766 break;
767 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
768 errorCode = "INET_E_DATA_NOT_AVAILABLE";
769 errorType = wxWEB_NAV_ERR_NOT_FOUND;
770 break;
771 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
772 errorCode = "INET_E_DOWNLOAD_FAILURE";
773 errorType = wxWEB_NAV_ERR_CONNECTION;
774 break;
775 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
776 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
777 errorType = wxWEB_NAV_ERR_AUTH;
778 break;
779 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
780 errorCode = "INET_E_NO_VALID_MEDIA";
781 errorType = wxWEB_NAV_ERR_REQUEST;
782 break;
783 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
784 errorCode = "INET_E_CONNECTION_TIMEOUT";
785 errorType = wxWEB_NAV_ERR_CONNECTION;
786 break;
787 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
788 errorCode = "INET_E_INVALID_REQUEST";
789 errorType = wxWEB_NAV_ERR_REQUEST;
790 break;
791 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
792 errorCode = "INET_E_UNKNOWN_PROTOCOL";
793 errorType = wxWEB_NAV_ERR_REQUEST;
794 break;
795 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
796 errorCode = "INET_E_SECURITY_PROBLEM";
797 errorType = wxWEB_NAV_ERR_SECURITY;
798 break;
799 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
800 errorCode = "INET_E_CANNOT_LOAD_DATA";
801 errorType = wxWEB_NAV_ERR_OTHER;
802 break;
803 case INET_E_CANNOT_INSTANTIATE_OBJECT:
804 // CoCreateInstance will return an error code if this happens,
805 // we'll handle this above.
806 return;
807 break;
808 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
809 errorCode = "INET_E_REDIRECT_FAILED";
810 errorType = wxWEB_NAV_ERR_OTHER;
811 break;
812 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
813 errorCode = "INET_E_REDIRECT_TO_DIR";
814 errorType = wxWEB_NAV_ERR_REQUEST;
815 break;
816 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
817 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
818 errorType = wxWEB_NAV_ERR_OTHER;
819 break;
820 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
821 errorCode = "INET_E_USE_EXTEND_BINDING";
822 errorType = wxWEB_NAV_ERR_OTHER;
823 break;
824 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
825 errorCode = "INET_E_TERMINATED_BIND";
826 errorType = wxWEB_NAV_ERR_OTHER;
827 break;
828 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
829 errorCode = "INET_E_INVALID_CERTIFICATE";
830 errorType = wxWEB_NAV_ERR_CERTIFICATE;
831 break;
832 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
833 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
834 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
835 break;
836 case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704)
837 // cancel request cancelled...
838 errorCode = "INET_E_RESULT_DISPATCHED";
839 errorType = wxWEB_NAV_ERR_OTHER;
840 break;
841 case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448)
842 errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE";
843 errorType = wxWEB_NAV_ERR_SECURITY;
844 break;
845 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY:
846 errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
847 errorType = wxWEB_NAV_ERR_SECURITY;
848 break;
849 case INET_E_CODE_INSTALL_SUPPRESSED:
850 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
851 errorType = wxWEB_NAV_ERR_SECURITY;
852 break;
853 }
854
855 wxString url = evt[1].GetString();
856 wxString target = evt[2].GetString();
857 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
858 url, target, false);
859 event.SetEventObject(this);
860 event.SetInt(errorType);
861 event.SetString(errorCode);
862 HandleWindowEvent(event);
863 break;
864 }
865
866 case DISPID_COMMANDSTATECHANGE:
867 {
868 long commandId = evt[0].GetLong();
869 bool enable = evt[1].GetBool();
870 if (commandId == CSC_NAVIGATEBACK)
871 {
872 m_canNavigateBack = enable;
873 }
874 else if (commandId == CSC_NAVIGATEFORWARD)
875 {
876 m_canNavigateForward = enable;
877 }
878 break;
879 }
880 case DISPID_NEWWINDOW3:
881 {
882 wxString url = evt[4].GetString();
883
884 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
885 GetId(), url, wxEmptyString, true);
886 event.SetEventObject(this);
887 HandleWindowEvent(event);
888
889 //If we veto the event then we cancel the new window
890 if (event.IsVetoed())
891 {
892 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
893 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE;
894 }
895 break;
896 }
897 }
898
899 evt.Skip();
900 }
901
902 #endif