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