webview_ie.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: include/wx/msw/webview_ie.h
  3. // Purpose: wxMSW IE wxWebView backend
  4. // Author: Marianne Gagnon
  5. // Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. #ifndef wxWebViewIE_H
  9. #define wxWebViewIE_H
  10. #include "wx/setup.h"
  11. #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE && defined(__WXMSW__)
  12. #include "wx/control.h"
  13. #include "wx/webview.h"
  14. #include "wx/msw/ole/automtn.h"
  15. #include "wx/msw/ole/activex.h"
  16. #include "wx/msw/ole/oleutils.h"
  17. #include "wx/msw/private/comptr.h"
  18. #include "wx/msw/wrapwin.h"
  19. #include "wx/msw/missing.h"
  20. #include "wx/msw/webview_missing.h"
  21. #include "wx/sharedptr.h"
  22. #include "wx/vector.h"
  23. struct IHTMLDocument2;
  24. struct IHTMLElement;
  25. struct IMarkupPointer;
  26. class wxFSFile;
  27. class ClassFactory;
  28. class wxIEContainer;
  29. class DocHostUIHandler;
  30. class wxFindPointers;
  31. class wxIInternetProtocol;
  32. class WXDLLIMPEXP_WEBVIEW wxWebViewIE : public wxWebView
  33. {
  34. public:
  35. wxWebViewIE() {}
  36. wxWebViewIE(wxWindow* parent,
  37. wxWindowID id,
  38. const wxString& url = wxWebViewDefaultURLStr,
  39. const wxPoint& pos = wxDefaultPosition,
  40. const wxSize& size = wxDefaultSize,
  41. long style = 0,
  42. const wxString& name = wxWebViewNameStr)
  43. {
  44. Create(parent, id, url, pos, size, style, name);
  45. }
  46. ~wxWebViewIE();
  47. bool Create(wxWindow* parent,
  48. wxWindowID id,
  49. const wxString& url = wxWebViewDefaultURLStr,
  50. const wxPoint& pos = wxDefaultPosition,
  51. const wxSize& size = wxDefaultSize,
  52. long style = 0,
  53. const wxString& name = wxWebViewNameStr);
  54. virtual void LoadURL(const wxString& url);
  55. virtual void LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item);
  56. virtual wxVector<wxSharedPtr<wxWebViewHistoryItem> > GetBackwardHistory();
  57. virtual wxVector<wxSharedPtr<wxWebViewHistoryItem> > GetForwardHistory();
  58. virtual bool CanGoForward() const;
  59. virtual bool CanGoBack() const;
  60. virtual void GoBack();
  61. virtual void GoForward();
  62. virtual void ClearHistory();
  63. virtual void EnableHistory(bool enable = true);
  64. virtual void Stop();
  65. virtual void Reload(wxWebViewReloadFlags flags = wxWEBVIEW_RELOAD_DEFAULT);
  66. virtual wxString GetPageSource() const;
  67. virtual wxString GetPageText() const;
  68. virtual bool IsBusy() const;
  69. virtual wxString GetCurrentURL() const;
  70. virtual wxString GetCurrentTitle() const;
  71. virtual void SetZoomType(wxWebViewZoomType);
  72. virtual wxWebViewZoomType GetZoomType() const;
  73. virtual bool CanSetZoomType(wxWebViewZoomType) const;
  74. virtual void Print();
  75. virtual wxWebViewZoom GetZoom() const;
  76. virtual void SetZoom(wxWebViewZoom zoom);
  77. //Clipboard functions
  78. virtual bool CanCut() const;
  79. virtual bool CanCopy() const;
  80. virtual bool CanPaste() const;
  81. virtual void Cut();
  82. virtual void Copy();
  83. virtual void Paste();
  84. //Undo / redo functionality
  85. virtual bool CanUndo() const;
  86. virtual bool CanRedo() const;
  87. virtual void Undo();
  88. virtual void Redo();
  89. //Find function
  90. virtual long Find(const wxString& text, int flags = wxWEBVIEW_FIND_DEFAULT);
  91. //Editing functions
  92. virtual void SetEditable(bool enable = true);
  93. virtual bool IsEditable() const;
  94. //Selection
  95. virtual void SelectAll();
  96. virtual bool HasSelection() const;
  97. virtual void DeleteSelection();
  98. virtual wxString GetSelectedText() const;
  99. virtual wxString GetSelectedSource() const;
  100. virtual void ClearSelection();
  101. virtual void RunScript(const wxString& javascript);
  102. //Virtual Filesystem Support
  103. virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler);
  104. virtual void* GetNativeBackend() const { return m_webBrowser; }
  105. // ---- IE-specific methods
  106. // FIXME: I seem to be able to access remote webpages even in offline mode...
  107. bool IsOfflineMode();
  108. void SetOfflineMode(bool offline);
  109. wxWebViewZoom GetIETextZoom() const;
  110. void SetIETextZoom(wxWebViewZoom level);
  111. wxWebViewZoom GetIEOpticalZoom() const;
  112. void SetIEOpticalZoom(wxWebViewZoom level);
  113. void onActiveXEvent(wxActiveXEvent& evt);
  114. void onEraseBg(wxEraseEvent&) {}
  115. DECLARE_EVENT_TABLE();
  116. protected:
  117. virtual void DoSetPage(const wxString& html, const wxString& baseUrl);
  118. private:
  119. wxIEContainer* m_container;
  120. wxAutomationObject m_ie;
  121. IWebBrowser2* m_webBrowser;
  122. DWORD m_dwCookie;
  123. wxCOMPtr<DocHostUIHandler> m_uiHandler;
  124. //We store the current zoom type;
  125. wxWebViewZoomType m_zoomType;
  126. /** The "Busy" property of IWebBrowser2 does not always return busy when
  127. * we'd want it to; this variable may be set to true in cases where the
  128. * Busy property is false but should be true.
  129. */
  130. bool m_isBusy;
  131. //We manage our own history, the history list contains the history items
  132. //which are added as documentcomplete events arrive, unless we are loading
  133. //an item from the history. The position is stored as an int, and reflects
  134. //where we are in the history list.
  135. wxVector<wxSharedPtr<wxWebViewHistoryItem> > m_historyList;
  136. wxVector<ClassFactory*> m_factories;
  137. int m_historyPosition;
  138. bool m_historyLoadingFromList;
  139. bool m_historyEnabled;
  140. //We store find flag, results and position.
  141. wxVector<wxFindPointers> m_findPointers;
  142. int m_findFlags;
  143. wxString m_findText;
  144. int m_findPosition;
  145. //Generic helper functions
  146. bool CanExecCommand(wxString command) const;
  147. void ExecCommand(wxString command);
  148. wxCOMPtr<IHTMLDocument2> GetDocument() const;
  149. bool IsElementVisible(wxCOMPtr<IHTMLElement> elm);
  150. //Find helper functions.
  151. void FindInternal(const wxString& text, int flags, int internal_flag);
  152. long FindNext(int direction = 1);
  153. void FindClear();
  154. //Toggles control features see INTERNETFEATURELIST for values.
  155. bool EnableControlFeature(long flag, bool enable = true);
  156. wxDECLARE_DYNAMIC_CLASS(wxWebViewIE);
  157. };
  158. class WXDLLIMPEXP_WEBVIEW wxWebViewFactoryIE : public wxWebViewFactory
  159. {
  160. public:
  161. virtual wxWebView* Create() { return new wxWebViewIE; }
  162. virtual wxWebView* Create(wxWindow* parent,
  163. wxWindowID id,
  164. const wxString& url = wxWebViewDefaultURLStr,
  165. const wxPoint& pos = wxDefaultPosition,
  166. const wxSize& size = wxDefaultSize,
  167. long style = 0,
  168. const wxString& name = wxWebViewNameStr)
  169. { return new wxWebViewIE(parent, id, url, pos, size, style, name); }
  170. };
  171. class VirtualProtocol : public wxIInternetProtocol
  172. {
  173. protected:
  174. wxIInternetProtocolSink* m_protocolSink;
  175. wxString m_html;
  176. VOID * fileP;
  177. wxFSFile* m_file;
  178. wxSharedPtr<wxWebViewHandler> m_handler;
  179. public:
  180. VirtualProtocol(wxSharedPtr<wxWebViewHandler> handler);
  181. virtual ~VirtualProtocol() {}
  182. //IUnknown
  183. DECLARE_IUNKNOWN_METHODS;
  184. //IInternetProtocolRoot
  185. HRESULT STDMETHODCALLTYPE Abort(HRESULT WXUNUSED(hrReason),
  186. DWORD WXUNUSED(dwOptions))
  187. { return E_NOTIMPL; }
  188. HRESULT STDMETHODCALLTYPE Continue(wxPROTOCOLDATA *WXUNUSED(pProtocolData))
  189. { return S_OK; }
  190. HRESULT STDMETHODCALLTYPE Resume() { return S_OK; }
  191. HRESULT STDMETHODCALLTYPE Start(LPCWSTR szUrl,
  192. wxIInternetProtocolSink *pOIProtSink,
  193. wxIInternetBindInfo *pOIBindInfo,
  194. DWORD grfPI,
  195. HANDLE_PTR dwReserved);
  196. HRESULT STDMETHODCALLTYPE Suspend() { return S_OK; }
  197. HRESULT STDMETHODCALLTYPE Terminate(DWORD WXUNUSED(dwOptions)) { return S_OK; }
  198. //IInternetProtocol
  199. HRESULT STDMETHODCALLTYPE LockRequest(DWORD WXUNUSED(dwOptions))
  200. { return S_OK; }
  201. HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead);
  202. HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER WXUNUSED(dlibMove),
  203. DWORD WXUNUSED(dwOrigin),
  204. ULARGE_INTEGER* WXUNUSED(plibNewPosition))
  205. { return E_FAIL; }
  206. HRESULT STDMETHODCALLTYPE UnlockRequest() { return S_OK; }
  207. };
  208. class ClassFactory : public IClassFactory
  209. {
  210. public:
  211. ClassFactory(wxSharedPtr<wxWebViewHandler> handler) : m_handler(handler)
  212. { AddRef(); }
  213. virtual ~ClassFactory() {}
  214. wxString GetName() { return m_handler->GetName(); }
  215. //IClassFactory
  216. HRESULT STDMETHODCALLTYPE CreateInstance(IUnknown* pUnkOuter,
  217. REFIID riid, void** ppvObject);
  218. HRESULT STDMETHODCALLTYPE LockServer(BOOL fLock);
  219. //IUnknown
  220. DECLARE_IUNKNOWN_METHODS;
  221. private:
  222. wxSharedPtr<wxWebViewHandler> m_handler;
  223. };
  224. class wxIEContainer : public wxActiveXContainer
  225. {
  226. public:
  227. wxIEContainer(wxWindow *parent, REFIID iid, IUnknown *pUnk, DocHostUIHandler* uiHandler = NULL);
  228. virtual ~wxIEContainer();
  229. virtual bool QueryClientSiteInterface(REFIID iid, void **_interface, const char *&desc);
  230. private:
  231. DocHostUIHandler* m_uiHandler;
  232. };
  233. class DocHostUIHandler : public wxIDocHostUIHandler
  234. {
  235. public:
  236. DocHostUIHandler(wxWebView* browser) { m_browser = browser; }
  237. virtual ~DocHostUIHandler() {}
  238. virtual HRESULT wxSTDCALL ShowContextMenu(DWORD dwID, POINT *ppt,
  239. IUnknown *pcmdtReserved,
  240. IDispatch *pdispReserved);
  241. virtual HRESULT wxSTDCALL GetHostInfo(DOCHOSTUIINFO *pInfo);
  242. virtual HRESULT wxSTDCALL ShowUI(DWORD dwID,
  243. IOleInPlaceActiveObject *pActiveObject,
  244. IOleCommandTarget *pCommandTarget,
  245. IOleInPlaceFrame *pFrame,
  246. IOleInPlaceUIWindow *pDoc);
  247. virtual HRESULT wxSTDCALL HideUI(void);
  248. virtual HRESULT wxSTDCALL UpdateUI(void);
  249. virtual HRESULT wxSTDCALL EnableModeless(BOOL fEnable);
  250. virtual HRESULT wxSTDCALL OnDocWindowActivate(BOOL fActivate);
  251. virtual HRESULT wxSTDCALL OnFrameWindowActivate(BOOL fActivate);
  252. virtual HRESULT wxSTDCALL ResizeBorder(LPCRECT prcBorder,
  253. IOleInPlaceUIWindow *pUIWindow,
  254. BOOL fRameWindow);
  255. virtual HRESULT wxSTDCALL TranslateAccelerator(LPMSG lpMsg,
  256. const GUID *pguidCmdGroup,
  257. DWORD nCmdID);
  258. virtual HRESULT wxSTDCALL GetOptionKeyPath(LPOLESTR *pchKey,
  259. DWORD dw);
  260. virtual HRESULT wxSTDCALL GetDropTarget(IDropTarget *pDropTarget,
  261. IDropTarget **ppDropTarget);
  262. virtual HRESULT wxSTDCALL GetExternal(IDispatch **ppDispatch);
  263. virtual HRESULT wxSTDCALL TranslateUrl(DWORD dwTranslate,
  264. OLECHAR *pchURLIn,
  265. OLECHAR **ppchURLOut);
  266. virtual HRESULT wxSTDCALL FilterDataObject(IDataObject *pDO,
  267. IDataObject **ppDORet);
  268. //IUnknown
  269. DECLARE_IUNKNOWN_METHODS;
  270. private:
  271. wxWebView* m_browser;
  272. };
  273. class wxFindPointers
  274. {
  275. public:
  276. wxFindPointers(wxIMarkupPointer *ptrBegin, wxIMarkupPointer *ptrEnd)
  277. {
  278. begin = ptrBegin;
  279. end = ptrEnd;
  280. }
  281. //The two markup pointers.
  282. wxIMarkupPointer *begin, *end;
  283. };
  284. #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE && defined(__WXMSW__)
  285. #endif // wxWebViewIE_H