Overhaul IE zoom functions to support both text and optical zoom.
[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 m_zoomType = wxWEB_VIEW_ZOOM_TYPE_TEXT;
100
101 if (::CoCreateInstance(CLSID_WebBrowser, NULL,
102 CLSCTX_INPROC_SERVER, // CLSCTX_INPROC,
103 IID_IWebBrowser2 , (void**)&m_webBrowser) != 0)
104 {
105 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
106 return false;
107 }
108
109 m_ie.SetDispatchPtr(m_webBrowser); // wxAutomationObject will release itself
110
111 m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
112 m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
113 //m_webBrowser->put_Silent(VARIANT_FALSE);
114
115 m_container = new wxActiveXContainer(this, IID_IWebBrowser2, m_webBrowser);
116
117 SetBackgroundStyle(wxBG_STYLE_PAINT);
118 SetDoubleBuffered(true);
119 LoadUrl(url);
120 return true;
121 }
122
123
124 void wxWebViewIE::LoadUrl(const wxString& url)
125 {
126 m_ie.CallMethod("Navigate", (BSTR) url.wc_str(), NULL, NULL, NULL, NULL);
127 }
128
129 void wxWebViewIE::SetPage(const wxString& html, const wxString&)
130 {
131 LoadUrl("about:blank");
132
133 // Let the wx events generated for navigation events be processed, so
134 // that the underlying IE component completes its Document object.
135 // FIXME: calling wxYield is not elegant nor very reliable probably
136 wxYield();
137
138 // TODO: consider the "baseUrl" parameter if possible
139 // TODO: consider encoding
140 BSTR bstr = SysAllocString(html.wc_str());
141
142 // Creates a new one-dimensional array
143 SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
144 if (psaStrings != NULL)
145 {
146 VARIANT *param;
147 HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
148 param->vt = VT_BSTR;
149 param->bstrVal = bstr;
150
151 hr = SafeArrayUnaccessData(psaStrings);
152 IHTMLDocument2* document = GetDocument();
153 document->write(psaStrings);
154
155 // SafeArrayDestroy calls SysFreeString for each BSTR
156 SafeArrayDestroy(psaStrings);
157 }
158 else
159 {
160 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
161 }
162
163 }
164
165 wxString wxWebViewIE::GetPageSource()
166 {
167 IHTMLDocument2* document = GetDocument();
168 IHTMLElement *bodyTag = NULL;
169 IHTMLElement *htmlTag = NULL;
170 BSTR bstr;
171 HRESULT hr = document->get_body(&bodyTag);
172 if(SUCCEEDED(hr))
173 {
174 hr = bodyTag->get_parentElement(&htmlTag);
175 if(SUCCEEDED(hr))
176 {
177 htmlTag->get_outerHTML(&bstr);
178 htmlTag->Release();
179 }
180 bodyTag->Release();
181 }
182
183 document->Release();
184 return wxString(bstr);
185 }
186
187 wxWebViewZoom wxWebViewIE::GetZoom()
188 {
189 if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_LAYOUT)
190 return GetIEOpticalZoom();
191 else if(m_zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT)
192 return GetIETextZoom();
193 else
194 wxFAIL;
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)
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) const
323 {
324 // both are supported
325 // TODO: IE6 only supports text zoom, check if it's IE6 first
326 return true;
327 }
328
329 void wxWebViewIE::Print()
330 {
331 m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW,
332 OLECMDEXECOPT_DODEFAULT, NULL, NULL);
333 }
334
335 bool wxWebViewIE::CanGoBack()
336 {
337 if(m_historyEnabled)
338 return m_historyPosition > 0;
339 else
340 return false;
341 }
342
343 bool wxWebViewIE::CanGoForward()
344 {
345 if(m_historyEnabled)
346 return m_historyPosition != static_cast<int>(m_historyList.size()) - 1;
347 else
348 return false;
349 }
350
351 void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item)
352 {
353 int pos = -1;
354 for(unsigned int i = 0; i < m_historyList.size(); i++)
355 {
356 //We compare the actual pointers to find the correct item
357 if(m_historyList[i].get() == item.get())
358 pos = i;
359 }
360 wxASSERT_MSG(pos != static_cast<int>(m_historyList.size()),
361 "invalid history item");
362 m_historyLoadingFromList = true;
363 LoadUrl(item->GetUrl());
364 m_historyPosition = pos;
365 }
366
367 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetBackwardHistory()
368 {
369 wxVector<wxSharedPtr<wxWebHistoryItem> > backhist;
370 //As we don't have std::copy or an iterator constructor in the wxwidgets
371 //native vector we construct it by hand
372 for(int i = 0; i < m_historyPosition; i++)
373 {
374 backhist.push_back(m_historyList[i]);
375 }
376 return backhist;
377 }
378
379 wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewIE::GetForwardHistory()
380 {
381 wxVector<wxSharedPtr<wxWebHistoryItem> > forwardhist;
382 //As we don't have std::copy or an iterator constructor in the wxwidgets
383 //native vector we construct it by hand
384 for(int i = m_historyPosition + 1; i < static_cast<int>(m_historyList.size()); i++)
385 {
386 forwardhist.push_back(m_historyList[i]);
387 }
388 return forwardhist;
389 }
390
391 void wxWebViewIE::GoBack()
392 {
393 LoadHistoryItem(m_historyList[m_historyPosition - 1]);
394 }
395
396 void wxWebViewIE::GoForward()
397 {
398 LoadHistoryItem(m_historyList[m_historyPosition + 1]);
399 }
400
401 void wxWebViewIE::Stop()
402 {
403 m_ie.CallMethod("Stop");
404 }
405
406 void wxWebViewIE::ClearHistory()
407 {
408 m_historyList.clear();
409 m_historyPosition = -1;
410 }
411
412 void wxWebViewIE::EnableHistory(bool enable)
413 {
414 m_historyEnabled = enable;
415 m_historyList.clear();
416 m_historyPosition = -1;
417 }
418
419 void wxWebViewIE::Reload(wxWebViewReloadFlags flags)
420 {
421 VARIANTARG level;
422 VariantInit(&level);
423 V_VT(&level) = VT_I2;
424
425 switch(flags)
426 {
427 case wxWEB_VIEW_RELOAD_DEFAULT:
428 V_I2(&level) = REFRESH_NORMAL;
429 break;
430 case wxWEB_VIEW_RELOAD_NO_CACHE:
431 V_I2(&level) = REFRESH_COMPLETELY;
432 break;
433 default:
434 wxFAIL_MSG("Unexpected reload type");
435 }
436
437 m_webBrowser->Refresh2(&level);
438 }
439
440 bool wxWebViewIE::IsOfflineMode()
441 {
442 wxVariant out = m_ie.GetProperty("Offline");
443
444 wxASSERT(out.GetType() == "bool");
445
446 return out.GetBool();
447 }
448
449 void wxWebViewIE::SetOfflineMode(bool offline)
450 {
451 // FIXME: the wxWidgets docs do not really document what the return
452 // parameter of PutProperty is
453 const bool success = m_ie.PutProperty("Offline", (offline ?
454 VARIANT_TRUE :
455 VARIANT_FALSE));
456 wxASSERT(success);
457 }
458
459 bool wxWebViewIE::IsBusy()
460 {
461 if (m_isBusy) return true;
462
463 wxVariant out = m_ie.GetProperty("Busy");
464
465 wxASSERT(out.GetType() == "bool");
466
467 return out.GetBool();
468 }
469
470 wxString wxWebViewIE::GetCurrentURL()
471 {
472 wxVariant out = m_ie.GetProperty("LocationURL");
473
474 wxASSERT(out.GetType() == "string");
475 return out.GetString();
476 }
477
478 wxString wxWebViewIE::GetCurrentTitle()
479 {
480 IHTMLDocument2* document = GetDocument();
481 BSTR title;
482
483 document->get_nameProp(&title);
484 document->Release();
485 return wxString(title);
486 }
487
488 bool wxWebViewIE::CanCut()
489 {
490 return CanExecCommand("Cut");
491 }
492
493 bool wxWebViewIE::CanCopy()
494 {
495 return CanExecCommand("Copy");
496 }
497 bool wxWebViewIE::CanPaste()
498 {
499 return CanExecCommand("Paste");
500 }
501
502 void wxWebViewIE::Cut()
503 {
504 ExecCommand("Cut");
505 }
506
507 void wxWebViewIE::Copy()
508 {
509 ExecCommand("Copy");
510 }
511
512 void wxWebViewIE::Paste()
513 {
514 ExecCommand("Paste");
515 }
516
517 bool wxWebViewIE::CanUndo()
518 {
519 return CanExecCommand("Undo");
520 }
521 bool wxWebViewIE::CanRedo()
522 {
523 return CanExecCommand("Redo");
524 }
525
526 void wxWebViewIE::Undo()
527 {
528 ExecCommand("Undo");
529 }
530
531 void wxWebViewIE::Redo()
532 {
533 ExecCommand("Redo");
534 }
535
536 void wxWebViewIE::SetEditable(bool enable)
537 {
538 IHTMLDocument2* document = GetDocument();
539 if( enable )
540 document->put_designMode(SysAllocString(L"On"));
541 else
542 document->put_designMode(SysAllocString(L"Off"));
543
544 document->Release();
545 }
546
547 bool wxWebViewIE::IsEditable()
548 {
549 IHTMLDocument2* document = GetDocument();
550 BSTR mode;
551 document->get_designMode(&mode);
552 if(wxString(mode) == "On")
553 return true;
554 else
555 return false;
556
557 document->Release();
558 }
559
560 void wxWebViewIE::SelectAll()
561 {
562 ExecCommand("SelectAll");
563 }
564
565 bool wxWebViewIE::HasSelection()
566 {
567 IHTMLDocument2* document = GetDocument();
568 IHTMLSelectionObject* selection;
569 BSTR type;
570 HRESULT hr = document->get_selection(&selection);
571 if(SUCCEEDED(hr))
572 {
573 selection->get_type(&type);
574 selection->Release();
575 }
576 document->Release();
577 return wxString(type) != "None";
578 }
579
580 void wxWebViewIE::DeleteSelection()
581 {
582 ExecCommand("Delete");
583 }
584
585 wxString wxWebViewIE::GetSelectedText()
586 {
587 IHTMLDocument2* document = GetDocument();
588 IHTMLSelectionObject* selection;
589 wxString selected;
590 HRESULT hr = document->get_selection(&selection);
591 if(SUCCEEDED(hr))
592 {
593 IDispatch* disrange;
594 hr = selection->createRange(&disrange);
595 if(SUCCEEDED(hr))
596 {
597 IHTMLTxtRange* range;
598 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
599 if(SUCCEEDED(hr))
600 {
601 BSTR text;
602 range->get_text(&text);
603 selected = wxString(text);
604 range->Release();
605 }
606 disrange->Release();
607 }
608 selection->Release();
609 }
610 document->Release();
611 return selected;
612 }
613
614 wxString wxWebViewIE::GetSelectedSource()
615 {
616 IHTMLDocument2* document = GetDocument();
617 IHTMLSelectionObject* selection;
618 wxString selected;
619 HRESULT hr = document->get_selection(&selection);
620 if(SUCCEEDED(hr))
621 {
622 IDispatch* disrange;
623 hr = selection->createRange(&disrange);
624 if(SUCCEEDED(hr))
625 {
626 IHTMLTxtRange* range;
627 hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
628 if(SUCCEEDED(hr))
629 {
630 BSTR text;
631 range->get_htmlText(&text);
632 selected = wxString(text);
633 range->Release();
634 }
635 disrange->Release();
636 }
637 selection->Release();
638 }
639 document->Release();
640 return selected;
641 }
642
643 wxString wxWebViewIE::GetPageText()
644 {
645 IHTMLDocument2* document = GetDocument();
646 BSTR out;
647 IHTMLElement* body;
648 HRESULT hr = document->get_body(&body);
649 if(SUCCEEDED(hr))
650 {
651 body->get_innerText(&out);
652 body->Release();
653 }
654 document->Release();
655 return wxString(out);
656 }
657
658 bool wxWebViewIE::CanExecCommand(wxString command)
659 {
660 IHTMLDocument2* document = GetDocument();
661 VARIANT_BOOL enabled;
662
663 document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
664 document->Release();
665
666 return (enabled == VARIANT_TRUE);
667 }
668
669 void wxWebViewIE::ExecCommand(wxString command)
670 {
671 IHTMLDocument2* document = GetDocument();
672 document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
673 document->Release();
674 }
675
676 IHTMLDocument2* wxWebViewIE::GetDocument()
677 {
678 wxVariant variant = m_ie.GetProperty("Document");
679 IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr();
680
681 wxASSERT(document);
682
683 return document;
684 }
685
686 void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt)
687 {
688 if (m_webBrowser == NULL) return;
689
690 switch (evt.GetDispatchId())
691 {
692 case DISPID_BEFORENAVIGATE2:
693 {
694 m_isBusy = true;
695
696 wxString url = evt[1].GetString();
697 wxString target = evt[3].GetString();
698
699 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
700 GetId(), url, target, true);
701 event.SetEventObject(this);
702 HandleWindowEvent(event);
703
704 if (event.IsVetoed())
705 {
706 wxActiveXEventNativeMSW* nativeParams =
707 evt.GetNativeParameters();
708 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE;
709 }
710
711 // at this point, either the navigation event has been cancelled
712 // and we're not busy, either it was accepted and IWebBrowser2's
713 // Busy property will be true; so we don't need our override
714 // flag anymore.
715 m_isBusy = false;
716
717 break;
718 }
719
720 case DISPID_NAVIGATECOMPLETE2:
721 {
722 wxString url = evt[1].GetString();
723 // TODO: set target parameter if possible
724 wxString target = wxEmptyString;
725 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
726 GetId(), url, target, false);
727 event.SetEventObject(this);
728 HandleWindowEvent(event);
729 break;
730 }
731
732 case DISPID_PROGRESSCHANGE:
733 {
734 // download progress
735 break;
736 }
737
738 case DISPID_DOCUMENTCOMPLETE:
739 {
740 //Only send a complete even if we are actually finished, this brings
741 //the event in to line with webkit
742 READYSTATE rs;
743 m_webBrowser->get_ReadyState( &rs );
744 if(rs != READYSTATE_COMPLETE)
745 break;
746
747 wxString url = evt[1].GetString();
748
749 //As we are complete we also add to the history list, but not if the
750 //page is not the main page, ie it is a subframe
751 if(m_historyEnabled && !m_historyLoadingFromList && url == GetCurrentURL())
752 {
753 //If we are not at the end of the list, then erase everything
754 //between us and the end before adding the new page
755 if(m_historyPosition != static_cast<int>(m_historyList.size()) - 1)
756 {
757 m_historyList.erase(m_historyList.begin() + m_historyPosition + 1,
758 m_historyList.end());
759 }
760 wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem(url, GetCurrentTitle()));
761 m_historyList.push_back(item);
762 m_historyPosition++;
763 }
764 //Reset as we are done now
765 m_historyLoadingFromList = false;
766 // TODO: set target parameter if possible
767 wxString target = wxEmptyString;
768 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, GetId(),
769 url, target, false);
770 event.SetEventObject(this);
771 HandleWindowEvent(event);
772 break;
773 }
774
775 case DISPID_STATUSTEXTCHANGE:
776 {
777 break;
778 }
779
780 case DISPID_TITLECHANGE:
781 {
782 break;
783 }
784
785 case DISPID_NAVIGATEERROR:
786 {
787 wxWebNavigationError errorType = wxWEB_NAV_ERR_OTHER;
788 wxString errorCode = "?";
789 switch (evt[3].GetLong())
790 {
791 case INET_E_INVALID_URL: // (0x800C0002L or -2146697214)
792 errorCode = "INET_E_INVALID_URL";
793 errorType = wxWEB_NAV_ERR_REQUEST;
794 break;
795 case INET_E_NO_SESSION: // (0x800C0003L or -2146697213)
796 errorCode = "INET_E_NO_SESSION";
797 errorType = wxWEB_NAV_ERR_CONNECTION;
798 break;
799 case INET_E_CANNOT_CONNECT: // (0x800C0004L or -2146697212)
800 errorCode = "INET_E_CANNOT_CONNECT";
801 errorType = wxWEB_NAV_ERR_CONNECTION;
802 break;
803 case INET_E_RESOURCE_NOT_FOUND: // (0x800C0005L or -2146697211)
804 errorCode = "INET_E_RESOURCE_NOT_FOUND";
805 errorType = wxWEB_NAV_ERR_NOT_FOUND;
806 break;
807 case INET_E_OBJECT_NOT_FOUND: // (0x800C0006L or -2146697210)
808 errorCode = "INET_E_OBJECT_NOT_FOUND";
809 errorType = wxWEB_NAV_ERR_NOT_FOUND;
810 break;
811 case INET_E_DATA_NOT_AVAILABLE: // (0x800C0007L or -2146697209)
812 errorCode = "INET_E_DATA_NOT_AVAILABLE";
813 errorType = wxWEB_NAV_ERR_NOT_FOUND;
814 break;
815 case INET_E_DOWNLOAD_FAILURE: // (0x800C0008L or -2146697208)
816 errorCode = "INET_E_DOWNLOAD_FAILURE";
817 errorType = wxWEB_NAV_ERR_CONNECTION;
818 break;
819 case INET_E_AUTHENTICATION_REQUIRED: // (0x800C0009L or -2146697207)
820 errorCode = "INET_E_AUTHENTICATION_REQUIRED";
821 errorType = wxWEB_NAV_ERR_AUTH;
822 break;
823 case INET_E_NO_VALID_MEDIA: // (0x800C000AL or -2146697206)
824 errorCode = "INET_E_NO_VALID_MEDIA";
825 errorType = wxWEB_NAV_ERR_REQUEST;
826 break;
827 case INET_E_CONNECTION_TIMEOUT: // (0x800C000BL or -2146697205)
828 errorCode = "INET_E_CONNECTION_TIMEOUT";
829 errorType = wxWEB_NAV_ERR_CONNECTION;
830 break;
831 case INET_E_INVALID_REQUEST: // (0x800C000CL or -2146697204)
832 errorCode = "INET_E_INVALID_REQUEST";
833 errorType = wxWEB_NAV_ERR_REQUEST;
834 break;
835 case INET_E_UNKNOWN_PROTOCOL: // (0x800C000DL or -2146697203)
836 errorCode = "INET_E_UNKNOWN_PROTOCOL";
837 errorType = wxWEB_NAV_ERR_REQUEST;
838 break;
839 case INET_E_SECURITY_PROBLEM: // (0x800C000EL or -2146697202)
840 errorCode = "INET_E_SECURITY_PROBLEM";
841 errorType = wxWEB_NAV_ERR_SECURITY;
842 break;
843 case INET_E_CANNOT_LOAD_DATA: // (0x800C000FL or -2146697201)
844 errorCode = "INET_E_CANNOT_LOAD_DATA";
845 errorType = wxWEB_NAV_ERR_OTHER;
846 break;
847 case INET_E_CANNOT_INSTANTIATE_OBJECT:
848 // CoCreateInstance will return an error code if this happens,
849 // we'll handle this above.
850 return;
851 break;
852 case INET_E_REDIRECT_FAILED: // (0x800C0014L or -2146697196)
853 errorCode = "INET_E_REDIRECT_FAILED";
854 errorType = wxWEB_NAV_ERR_OTHER;
855 break;
856 case INET_E_REDIRECT_TO_DIR: // (0x800C0015L or -2146697195)
857 errorCode = "INET_E_REDIRECT_TO_DIR";
858 errorType = wxWEB_NAV_ERR_REQUEST;
859 break;
860 case INET_E_CANNOT_LOCK_REQUEST: // (0x800C0016L or -2146697194)
861 errorCode = "INET_E_CANNOT_LOCK_REQUEST";
862 errorType = wxWEB_NAV_ERR_OTHER;
863 break;
864 case INET_E_USE_EXTEND_BINDING: // (0x800C0017L or -2146697193)
865 errorCode = "INET_E_USE_EXTEND_BINDING";
866 errorType = wxWEB_NAV_ERR_OTHER;
867 break;
868 case INET_E_TERMINATED_BIND: // (0x800C0018L or -2146697192)
869 errorCode = "INET_E_TERMINATED_BIND";
870 errorType = wxWEB_NAV_ERR_OTHER;
871 break;
872 case INET_E_INVALID_CERTIFICATE: // (0x800C0019L or -2146697191)
873 errorCode = "INET_E_INVALID_CERTIFICATE";
874 errorType = wxWEB_NAV_ERR_CERTIFICATE;
875 break;
876 case INET_E_CODE_DOWNLOAD_DECLINED: // (0x800C0100L or -2146696960)
877 errorCode = "INET_E_CODE_DOWNLOAD_DECLINED";
878 errorType = wxWEB_NAV_ERR_USER_CANCELLED;
879 break;
880 case INET_E_RESULT_DISPATCHED: // (0x800C0200L or -2146696704)
881 // cancel request cancelled...
882 errorCode = "INET_E_RESULT_DISPATCHED";
883 errorType = wxWEB_NAV_ERR_OTHER;
884 break;
885 case INET_E_CANNOT_REPLACE_SFP_FILE: // (0x800C0300L or -2146696448)
886 errorCode = "INET_E_CANNOT_REPLACE_SFP_FILE";
887 errorType = wxWEB_NAV_ERR_SECURITY;
888 break;
889 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY:
890 errorCode = "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
891 errorType = wxWEB_NAV_ERR_SECURITY;
892 break;
893 case INET_E_CODE_INSTALL_SUPPRESSED:
894 errorCode = "INET_E_CODE_INSTALL_SUPPRESSED";
895 errorType = wxWEB_NAV_ERR_SECURITY;
896 break;
897 }
898
899 wxString url = evt[1].GetString();
900 wxString target = evt[2].GetString();
901 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, GetId(),
902 url, target, false);
903 event.SetEventObject(this);
904 event.SetInt(errorType);
905 event.SetString(errorCode);
906 HandleWindowEvent(event);
907 break;
908 }
909
910 case DISPID_COMMANDSTATECHANGE:
911 {
912 long commandId = evt[0].GetLong();
913 bool enable = evt[1].GetBool();
914 if (commandId == CSC_NAVIGATEBACK)
915 {
916 m_canNavigateBack = enable;
917 }
918 else if (commandId == CSC_NAVIGATEFORWARD)
919 {
920 m_canNavigateForward = enable;
921 }
922 break;
923 }
924 case DISPID_NEWWINDOW3:
925 {
926 wxString url = evt[4].GetString();
927
928 wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
929 GetId(), url, wxEmptyString, true);
930 event.SetEventObject(this);
931 HandleWindowEvent(event);
932
933 //If we veto the event then we cancel the new window
934 if (event.IsVetoed())
935 {
936 wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters();
937 *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE;
938 }
939 break;
940 }
941 }
942
943 evt.Skip();
944 }
945
946 #endif