]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/webview_ie.cpp | |
3 | // Purpose: wxMSW wxWebViewIE class implementation for web view component | |
4 | // Author: Marianne Gagnon | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if defined(__BORLANDC__) | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #include "wx/msw/webview_ie.h" | |
18 | ||
19 | #if wxUSE_WEBVIEW_IE | |
20 | ||
21 | #include <olectl.h> | |
22 | #include <oleidl.h> | |
23 | #include <exdispid.h> | |
24 | #include <exdisp.h> | |
25 | #include <mshtml.h> | |
26 | #include "wx/msw/registry.h" | |
27 | #include "wx/msw/missing.h" | |
28 | ||
29 | BEGIN_EVENT_TABLE(wxWebViewIE, wxControl) | |
30 | EVT_ACTIVEX(wxID_ANY, wxWebViewIE::onActiveXEvent) | |
31 | EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg) | |
32 | END_EVENT_TABLE() | |
33 | ||
34 | bool 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; | |
52 | m_historyLoadingFromList = false; | |
53 | m_historyEnabled = true; | |
54 | m_historyPosition = -1; | |
55 | m_zoomType = wxWEB_VIEW_ZOOM_TYPE_TEXT; | |
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); | |
75 | LoadUrl(url); | |
76 | return true; | |
77 | } | |
78 | ||
79 | ||
80 | void wxWebViewIE::LoadUrl(const wxString& url) | |
81 | { | |
82 | m_ie.CallMethod("Navigate", (BSTR) url.wc_str(), NULL, NULL, NULL, NULL); | |
83 | } | |
84 | ||
85 | void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl) | |
86 | { | |
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; | |
94 | ||
95 | HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m); | |
96 | param->vt = VT_BSTR; | |
97 | param->bstrVal = bstr; | |
98 | hr = SafeArrayUnaccessData(psaStrings); | |
99 | ||
100 | IHTMLDocument2* document = GetDocument(); | |
101 | document->write(psaStrings); | |
102 | document->Release(); | |
103 | ||
104 | // SafeArrayDestroy calls SysFreeString for each BSTR | |
105 | SafeArrayDestroy(psaStrings); | |
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); | |
118 | } | |
119 | else | |
120 | { | |
121 | wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL"); | |
122 | } | |
123 | ||
124 | } | |
125 | ||
126 | wxString wxWebViewIE::GetPageSource() | |
127 | { | |
128 | IHTMLDocument2* document = GetDocument(); | |
129 | IHTMLElement *bodyTag = NULL; | |
130 | IHTMLElement *htmlTag = NULL; | |
131 | wxString source; | |
132 | HRESULT hr = document->get_body(&bodyTag); | |
133 | if(SUCCEEDED(hr)) | |
134 | { | |
135 | hr = bodyTag->get_parentElement(&htmlTag); | |
136 | if(SUCCEEDED(hr)) | |
137 | { | |
138 | BSTR bstr; | |
139 | htmlTag->get_outerHTML(&bstr); | |
140 | source = wxString(bstr); | |
141 | htmlTag->Release(); | |
142 | } | |
143 | bodyTag->Release(); | |
144 | } | |
145 | ||
146 | document->Release(); | |
147 | return source; | |
148 | } | |
149 | ||
150 | wxWebViewZoom wxWebViewIE::GetZoom() | |
151 | { | |
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; | |
158 | ||
159 | //Dummy return to stop compiler warnings | |
160 | return wxWEB_VIEW_ZOOM_MEDIUM; | |
161 | ||
162 | } | |
163 | ||
164 | void wxWebViewIE::SetZoom(wxWebViewZoom zoom) | |
165 | { | |
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; | |
172 | } | |
173 | ||
174 | void wxWebViewIE::SetIETextZoom(wxWebViewZoom level) | |
175 | { | |
176 | //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range | |
177 | //is 0 to 4 so the check is unnecessary, these match exactly with the | |
178 | //enum values | |
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); | |
187 | wxASSERT(result == S_OK); | |
188 | } | |
189 | ||
190 | wxWebViewZoom wxWebViewIE::GetIETextZoom() | |
191 | { | |
192 | VARIANT zoomVariant; | |
193 | VariantInit (&zoomVariant); | |
194 | V_VT(&zoomVariant) = VT_I4; | |
195 | ||
196 | HRESULT result = m_webBrowser->ExecWB(OLECMDID_ZOOM, | |
197 | OLECMDEXECOPT_DONTPROMPTUSER, | |
198 | NULL, &zoomVariant); | |
199 | wxASSERT(result == S_OK); | |
200 | ||
201 | //We can safely cast here as we know that the range matches our enum | |
202 | return static_cast<wxWebViewZoom>(V_I4(&zoomVariant)); | |
203 | } | |
204 | ||
205 | void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level) | |
206 | { | |
207 | //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range | |
208 | //is 10 to 1000 so the check is unnecessary | |
209 | VARIANT zoomVariant; | |
210 | VariantInit (&zoomVariant); | |
211 | V_VT(&zoomVariant) = VT_I4; | |
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 | } | |
234 | ||
235 | HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM, | |
236 | OLECMDEXECOPT_DODEFAULT, | |
237 | &zoomVariant, | |
238 | NULL); | |
239 | wxASSERT(result == S_OK); | |
240 | } | |
241 | ||
242 | wxWebViewZoom wxWebViewIE::GetIEOpticalZoom() | |
243 | { | |
244 | VARIANT zoomVariant; | |
245 | VariantInit (&zoomVariant); | |
246 | V_VT(&zoomVariant) = VT_I4; | |
247 | ||
248 | HRESULT result = m_webBrowser->ExecWB((OLECMDID)OLECMDID_OPTICAL_ZOOM, | |
249 | OLECMDEXECOPT_DODEFAULT, NULL, | |
250 | &zoomVariant); | |
251 | wxASSERT(result == S_OK); | |
252 | ||
253 | const int zoom = V_I4(&zoomVariant); | |
254 | ||
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 | } | |
272 | else /*if (zoom > 145) */ //Using else removes a compiler warning | |
273 | { | |
274 | return wxWEB_VIEW_ZOOM_LARGEST; | |
275 | } | |
276 | } | |
277 | ||
278 | void wxWebViewIE::SetZoomType(wxWebViewZoomType type) | |
279 | { | |
280 | m_zoomType = type; | |
281 | } | |
282 | ||
283 | wxWebViewZoomType wxWebViewIE::GetZoomType() const | |
284 | { | |
285 | return m_zoomType; | |
286 | } | |
287 | ||
288 | bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type) const | |
289 | { | |
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; | |
301 | } | |
302 | ||
303 | void wxWebViewIE::Print() | |
304 | { | |
305 | m_webBrowser->ExecWB(OLECMDID_PRINTPREVIEW, | |
306 | OLECMDEXECOPT_DODEFAULT, NULL, NULL); | |
307 | } | |
308 | ||
309 | bool wxWebViewIE::CanGoBack() | |
310 | { | |
311 | if(m_historyEnabled) | |
312 | return m_historyPosition > 0; | |
313 | else | |
314 | return false; | |
315 | } | |
316 | ||
317 | bool wxWebViewIE::CanGoForward() | |
318 | { | |
319 | if(m_historyEnabled) | |
320 | return m_historyPosition != static_cast<int>(m_historyList.size()) - 1; | |
321 | else | |
322 | return false; | |
323 | } | |
324 | ||
325 | void wxWebViewIE::LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item) | |
326 | { | |
327 | int pos = -1; | |
328 | for(unsigned int i = 0; i < m_historyList.size(); i++) | |
329 | { | |
330 | //We compare the actual pointers to find the correct item | |
331 | if(m_historyList[i].get() == item.get()) | |
332 | pos = i; | |
333 | } | |
334 | wxASSERT_MSG(pos != static_cast<int>(m_historyList.size()), | |
335 | "invalid history item"); | |
336 | m_historyLoadingFromList = true; | |
337 | LoadUrl(item->GetUrl()); | |
338 | m_historyPosition = pos; | |
339 | } | |
340 | ||
341 | wxVector<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 | ||
353 | wxVector<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 | |
358 | for(int i = m_historyPosition + 1; i < static_cast<int>(m_historyList.size()); i++) | |
359 | { | |
360 | forwardhist.push_back(m_historyList[i]); | |
361 | } | |
362 | return forwardhist; | |
363 | } | |
364 | ||
365 | void wxWebViewIE::GoBack() | |
366 | { | |
367 | LoadHistoryItem(m_historyList[m_historyPosition - 1]); | |
368 | } | |
369 | ||
370 | void wxWebViewIE::GoForward() | |
371 | { | |
372 | LoadHistoryItem(m_historyList[m_historyPosition + 1]); | |
373 | } | |
374 | ||
375 | void wxWebViewIE::Stop() | |
376 | { | |
377 | m_ie.CallMethod("Stop"); | |
378 | } | |
379 | ||
380 | void wxWebViewIE::ClearHistory() | |
381 | { | |
382 | m_historyList.clear(); | |
383 | m_historyPosition = -1; | |
384 | } | |
385 | ||
386 | void wxWebViewIE::EnableHistory(bool enable) | |
387 | { | |
388 | m_historyEnabled = enable; | |
389 | m_historyList.clear(); | |
390 | m_historyPosition = -1; | |
391 | } | |
392 | ||
393 | void wxWebViewIE::Reload(wxWebViewReloadFlags flags) | |
394 | { | |
395 | VARIANTARG level; | |
396 | VariantInit(&level); | |
397 | V_VT(&level) = VT_I2; | |
398 | ||
399 | switch(flags) | |
400 | { | |
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"); | |
409 | } | |
410 | ||
411 | m_webBrowser->Refresh2(&level); | |
412 | } | |
413 | ||
414 | bool wxWebViewIE::IsOfflineMode() | |
415 | { | |
416 | wxVariant out = m_ie.GetProperty("Offline"); | |
417 | ||
418 | wxASSERT(out.GetType() == "bool"); | |
419 | ||
420 | return out.GetBool(); | |
421 | } | |
422 | ||
423 | void 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 | ||
433 | bool 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 | ||
444 | wxString wxWebViewIE::GetCurrentURL() | |
445 | { | |
446 | wxVariant out = m_ie.GetProperty("LocationURL"); | |
447 | ||
448 | wxASSERT(out.GetType() == "string"); | |
449 | return out.GetString(); | |
450 | } | |
451 | ||
452 | wxString wxWebViewIE::GetCurrentTitle() | |
453 | { | |
454 | IHTMLDocument2* document = GetDocument(); | |
455 | BSTR title; | |
456 | ||
457 | document->get_nameProp(&title); | |
458 | document->Release(); | |
459 | return wxString(title); | |
460 | } | |
461 | ||
462 | bool wxWebViewIE::CanCut() | |
463 | { | |
464 | return CanExecCommand("Cut"); | |
465 | } | |
466 | ||
467 | bool wxWebViewIE::CanCopy() | |
468 | { | |
469 | return CanExecCommand("Copy"); | |
470 | } | |
471 | bool wxWebViewIE::CanPaste() | |
472 | { | |
473 | return CanExecCommand("Paste"); | |
474 | } | |
475 | ||
476 | void wxWebViewIE::Cut() | |
477 | { | |
478 | ExecCommand("Cut"); | |
479 | } | |
480 | ||
481 | void wxWebViewIE::Copy() | |
482 | { | |
483 | ExecCommand("Copy"); | |
484 | } | |
485 | ||
486 | void wxWebViewIE::Paste() | |
487 | { | |
488 | ExecCommand("Paste"); | |
489 | } | |
490 | ||
491 | bool wxWebViewIE::CanUndo() | |
492 | { | |
493 | return CanExecCommand("Undo"); | |
494 | } | |
495 | bool wxWebViewIE::CanRedo() | |
496 | { | |
497 | return CanExecCommand("Redo"); | |
498 | } | |
499 | ||
500 | void wxWebViewIE::Undo() | |
501 | { | |
502 | ExecCommand("Undo"); | |
503 | } | |
504 | ||
505 | void wxWebViewIE::Redo() | |
506 | { | |
507 | ExecCommand("Redo"); | |
508 | } | |
509 | ||
510 | void 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")); | |
517 | ||
518 | document->Release(); | |
519 | } | |
520 | ||
521 | bool wxWebViewIE::IsEditable() | |
522 | { | |
523 | IHTMLDocument2* document = GetDocument(); | |
524 | BSTR mode; | |
525 | document->get_designMode(&mode); | |
526 | document->Release(); | |
527 | if(wxString(mode) == "On") | |
528 | return true; | |
529 | else | |
530 | return false; | |
531 | } | |
532 | ||
533 | void wxWebViewIE::SelectAll() | |
534 | { | |
535 | ExecCommand("SelectAll"); | |
536 | } | |
537 | ||
538 | bool wxWebViewIE::HasSelection() | |
539 | { | |
540 | IHTMLDocument2* document = GetDocument(); | |
541 | IHTMLSelectionObject* selection; | |
542 | wxString sel; | |
543 | HRESULT hr = document->get_selection(&selection); | |
544 | if(SUCCEEDED(hr)) | |
545 | { | |
546 | BSTR type; | |
547 | selection->get_type(&type); | |
548 | sel = wxString(type); | |
549 | selection->Release(); | |
550 | } | |
551 | document->Release(); | |
552 | return sel != "None"; | |
553 | } | |
554 | ||
555 | void wxWebViewIE::DeleteSelection() | |
556 | { | |
557 | ExecCommand("Delete"); | |
558 | } | |
559 | ||
560 | wxString 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 | ||
589 | wxString wxWebViewIE::GetSelectedSource() | |
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 | ||
618 | void 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 | ||
632 | wxString wxWebViewIE::GetPageText() | |
633 | { | |
634 | IHTMLDocument2* document = GetDocument(); | |
635 | wxString text; | |
636 | IHTMLElement* body; | |
637 | HRESULT hr = document->get_body(&body); | |
638 | if(SUCCEEDED(hr)) | |
639 | { | |
640 | BSTR out; | |
641 | body->get_innerText(&out); | |
642 | text = wxString(out); | |
643 | body->Release(); | |
644 | } | |
645 | document->Release(); | |
646 | return text; | |
647 | } | |
648 | ||
649 | void 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 | ||
665 | bool wxWebViewIE::CanExecCommand(wxString command) | |
666 | { | |
667 | IHTMLDocument2* document = GetDocument(); | |
668 | VARIANT_BOOL enabled; | |
669 | ||
670 | document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled); | |
671 | document->Release(); | |
672 | ||
673 | return (enabled == VARIANT_TRUE); | |
674 | } | |
675 | ||
676 | void wxWebViewIE::ExecCommand(wxString command) | |
677 | { | |
678 | IHTMLDocument2* document = GetDocument(); | |
679 | document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL); | |
680 | document->Release(); | |
681 | } | |
682 | ||
683 | IHTMLDocument2* wxWebViewIE::GetDocument() | |
684 | { | |
685 | wxVariant variant = m_ie.GetProperty("Document"); | |
686 | IHTMLDocument2* document = (IHTMLDocument2*)variant.GetVoidPtr(); | |
687 | ||
688 | wxASSERT(document); | |
689 | ||
690 | return document; | |
691 | } | |
692 | ||
693 | void 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 | { | |
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 | ||
754 | wxString url = evt[1].GetString(); | |
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()) | |
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 | |
762 | if(m_historyPosition != static_cast<int>(m_historyList.size()) - 1) | |
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; | |
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 | } | |
929 | break; | |
930 | } | |
931 | case DISPID_NEWWINDOW3: | |
932 | { | |
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 | } | |
946 | break; | |
947 | } | |
948 | } | |
949 | ||
950 | evt.Skip(); | |
951 | } | |
952 | ||
953 | #endif |