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