webview.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: webview.h
  3. // Purpose: Common interface and events for web view component
  4. // Author: Marianne Gagnon
  5. // Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. #ifndef _WX_WEBVIEW_H_
  9. #define _WX_WEBVIEW_H_
  10. #include "wx/defs.h"
  11. #if wxUSE_WEBVIEW
  12. #include "wx/control.h"
  13. #include "wx/event.h"
  14. #include "wx/sstream.h"
  15. #include "wx/sharedptr.h"
  16. #include "wx/vector.h"
  17. #if defined(__WXOSX__)
  18. #include "wx/osx/webviewhistoryitem_webkit.h"
  19. #elif defined(__WXGTK__)
  20. #include "wx/gtk/webviewhistoryitem_webkit.h"
  21. #elif defined(__WXMSW__)
  22. #include "wx/msw/webviewhistoryitem_ie.h"
  23. #else
  24. #error "wxWebView not implemented on this platform."
  25. #endif
  26. class wxFSFile;
  27. class wxFileSystem;
  28. class wxWebView;
  29. enum wxWebViewZoom
  30. {
  31. wxWEBVIEW_ZOOM_TINY,
  32. wxWEBVIEW_ZOOM_SMALL,
  33. wxWEBVIEW_ZOOM_MEDIUM,
  34. wxWEBVIEW_ZOOM_LARGE,
  35. wxWEBVIEW_ZOOM_LARGEST
  36. };
  37. enum wxWebViewZoomType
  38. {
  39. //Scales entire page, including images
  40. wxWEBVIEW_ZOOM_TYPE_LAYOUT,
  41. wxWEBVIEW_ZOOM_TYPE_TEXT
  42. };
  43. enum wxWebViewNavigationError
  44. {
  45. wxWEBVIEW_NAV_ERR_CONNECTION,
  46. wxWEBVIEW_NAV_ERR_CERTIFICATE,
  47. wxWEBVIEW_NAV_ERR_AUTH,
  48. wxWEBVIEW_NAV_ERR_SECURITY,
  49. wxWEBVIEW_NAV_ERR_NOT_FOUND,
  50. wxWEBVIEW_NAV_ERR_REQUEST,
  51. wxWEBVIEW_NAV_ERR_USER_CANCELLED,
  52. wxWEBVIEW_NAV_ERR_OTHER
  53. };
  54. enum wxWebViewReloadFlags
  55. {
  56. //Default, may access cache
  57. wxWEBVIEW_RELOAD_DEFAULT,
  58. wxWEBVIEW_RELOAD_NO_CACHE
  59. };
  60. enum wxWebViewFindFlags
  61. {
  62. wxWEBVIEW_FIND_WRAP = 0x0001,
  63. wxWEBVIEW_FIND_ENTIRE_WORD = 0x0002,
  64. wxWEBVIEW_FIND_MATCH_CASE = 0x0004,
  65. wxWEBVIEW_FIND_HIGHLIGHT_RESULT = 0x0008,
  66. wxWEBVIEW_FIND_BACKWARDS = 0x0010,
  67. wxWEBVIEW_FIND_DEFAULT = 0
  68. };
  69. //Base class for custom scheme handlers
  70. class WXDLLIMPEXP_WEBVIEW wxWebViewHandler
  71. {
  72. public:
  73. wxWebViewHandler(const wxString& scheme) : m_scheme(scheme) {}
  74. virtual ~wxWebViewHandler() {}
  75. virtual wxString GetName() const { return m_scheme; }
  76. virtual wxFSFile* GetFile(const wxString &uri) = 0;
  77. private:
  78. wxString m_scheme;
  79. };
  80. extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewNameStr[];
  81. extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewDefaultURLStr[];
  82. extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendDefault[];
  83. extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendIE[];
  84. extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendWebKit[];
  85. class WXDLLIMPEXP_WEBVIEW wxWebViewFactory : public wxObject
  86. {
  87. public:
  88. virtual wxWebView* Create() = 0;
  89. virtual wxWebView* Create(wxWindow* parent,
  90. wxWindowID id,
  91. const wxString& url = wxWebViewDefaultURLStr,
  92. const wxPoint& pos = wxDefaultPosition,
  93. const wxSize& size = wxDefaultSize,
  94. long style = 0,
  95. const wxString& name = wxWebViewNameStr) = 0;
  96. };
  97. WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxWebViewFactory>, wxStringWebViewFactoryMap);
  98. class WXDLLIMPEXP_WEBVIEW wxWebView : public wxControl
  99. {
  100. public:
  101. wxWebView()
  102. {
  103. m_showMenu = true;
  104. }
  105. virtual ~wxWebView() {}
  106. virtual bool Create(wxWindow* parent,
  107. wxWindowID id,
  108. const wxString& url = wxWebViewDefaultURLStr,
  109. const wxPoint& pos = wxDefaultPosition,
  110. const wxSize& size = wxDefaultSize,
  111. long style = 0,
  112. const wxString& name = wxWebViewNameStr) = 0;
  113. // Factory methods allowing the use of custom factories registered with
  114. // RegisterFactory
  115. static wxWebView* New(const wxString& backend = wxWebViewBackendDefault);
  116. static wxWebView* New(wxWindow* parent,
  117. wxWindowID id,
  118. const wxString& url = wxWebViewDefaultURLStr,
  119. const wxPoint& pos = wxDefaultPosition,
  120. const wxSize& size = wxDefaultSize,
  121. const wxString& backend = wxWebViewBackendDefault,
  122. long style = 0,
  123. const wxString& name = wxWebViewNameStr);
  124. static void RegisterFactory(const wxString& backend,
  125. wxSharedPtr<wxWebViewFactory> factory);
  126. // General methods
  127. virtual void EnableContextMenu(bool enable = true)
  128. {
  129. m_showMenu = enable;
  130. }
  131. virtual wxString GetCurrentTitle() const = 0;
  132. virtual wxString GetCurrentURL() const = 0;
  133. // TODO: handle choosing a frame when calling GetPageSource()?
  134. virtual wxString GetPageSource() const = 0;
  135. virtual wxString GetPageText() const = 0;
  136. virtual bool IsBusy() const = 0;
  137. virtual bool IsContextMenuEnabled() const { return m_showMenu; }
  138. virtual bool IsEditable() const = 0;
  139. virtual void LoadURL(const wxString& url) = 0;
  140. virtual void Print() = 0;
  141. virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) = 0;
  142. virtual void Reload(wxWebViewReloadFlags flags = wxWEBVIEW_RELOAD_DEFAULT) = 0;
  143. virtual void RunScript(const wxString& javascript) = 0;
  144. virtual void SetEditable(bool enable = true) = 0;
  145. void SetPage(const wxString& html, const wxString& baseUrl)
  146. {
  147. DoSetPage(html, baseUrl);
  148. }
  149. void SetPage(wxInputStream& html, wxString baseUrl)
  150. {
  151. wxStringOutputStream stream;
  152. stream.Write(html);
  153. DoSetPage(stream.GetString(), baseUrl);
  154. }
  155. virtual void Stop() = 0;
  156. //History
  157. virtual bool CanGoBack() const = 0;
  158. virtual bool CanGoForward() const = 0;
  159. virtual void GoBack() = 0;
  160. virtual void GoForward() = 0;
  161. virtual void ClearHistory() = 0;
  162. virtual void EnableHistory(bool enable = true) = 0;
  163. virtual wxVector<wxSharedPtr<wxWebViewHistoryItem> > GetBackwardHistory() = 0;
  164. virtual wxVector<wxSharedPtr<wxWebViewHistoryItem> > GetForwardHistory() = 0;
  165. virtual void LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item) = 0;
  166. //Zoom
  167. virtual bool CanSetZoomType(wxWebViewZoomType type) const = 0;
  168. virtual wxWebViewZoom GetZoom() const = 0;
  169. virtual wxWebViewZoomType GetZoomType() const = 0;
  170. virtual void SetZoom(wxWebViewZoom zoom) = 0;
  171. virtual void SetZoomType(wxWebViewZoomType zoomType) = 0;
  172. //Selection
  173. virtual void SelectAll() = 0;
  174. virtual bool HasSelection() const = 0;
  175. virtual void DeleteSelection() = 0;
  176. virtual wxString GetSelectedText() const = 0;
  177. virtual wxString GetSelectedSource() const = 0;
  178. virtual void ClearSelection() = 0;
  179. //Clipboard functions
  180. virtual bool CanCut() const = 0;
  181. virtual bool CanCopy() const = 0;
  182. virtual bool CanPaste() const = 0;
  183. virtual void Cut() = 0;
  184. virtual void Copy() = 0;
  185. virtual void Paste() = 0;
  186. //Undo / redo functionality
  187. virtual bool CanUndo() const = 0;
  188. virtual bool CanRedo() const = 0;
  189. virtual void Undo() = 0;
  190. virtual void Redo() = 0;
  191. //Get the pointer to the underlying native engine.
  192. virtual void* GetNativeBackend() const = 0;
  193. //Find function
  194. virtual long Find(const wxString& text, int flags = wxWEBVIEW_FIND_DEFAULT) = 0;
  195. protected:
  196. virtual void DoSetPage(const wxString& html, const wxString& baseUrl) = 0;
  197. private:
  198. static void InitFactoryMap();
  199. static wxStringWebViewFactoryMap::iterator FindFactory(const wxString &backend);
  200. bool m_showMenu;
  201. static wxStringWebViewFactoryMap m_factoryMap;
  202. wxDECLARE_ABSTRACT_CLASS(wxWebView);
  203. };
  204. class WXDLLIMPEXP_WEBVIEW wxWebViewEvent : public wxNotifyEvent
  205. {
  206. public:
  207. wxWebViewEvent() {}
  208. wxWebViewEvent(wxEventType type, int id, const wxString url,
  209. const wxString target)
  210. : wxNotifyEvent(type, id), m_url(url), m_target(target)
  211. {}
  212. const wxString& GetURL() const { return m_url; }
  213. const wxString& GetTarget() const { return m_target; }
  214. virtual wxEvent* Clone() const { return new wxWebViewEvent(*this); }
  215. private:
  216. wxString m_url;
  217. wxString m_target;
  218. wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebViewEvent);
  219. };
  220. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_NAVIGATING, wxWebViewEvent );
  221. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_NAVIGATED, wxWebViewEvent );
  222. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_LOADED, wxWebViewEvent );
  223. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_ERROR, wxWebViewEvent );
  224. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_NEWWINDOW, wxWebViewEvent );
  225. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_TITLE_CHANGED, wxWebViewEvent );
  226. typedef void (wxEvtHandler::*wxWebViewEventFunction)
  227. (wxWebViewEvent&);
  228. #define wxWebViewEventHandler(func) \
  229. wxEVENT_HANDLER_CAST(wxWebViewEventFunction, func)
  230. #define EVT_WEBVIEW_NAVIGATING(id, fn) \
  231. wx__DECLARE_EVT1(wxEVT_WEBVIEW_NAVIGATING, id, \
  232. wxWebViewEventHandler(fn))
  233. #define EVT_WEBVIEW_NAVIGATED(id, fn) \
  234. wx__DECLARE_EVT1(wxEVT_WEBVIEW_NAVIGATED, id, \
  235. wxWebViewEventHandler(fn))
  236. #define EVT_WEBVIEW_LOADED(id, fn) \
  237. wx__DECLARE_EVT1(wxEVT_WEBVIEW_LOADED, id, \
  238. wxWebViewEventHandler(fn))
  239. #define EVT_WEBVIEW_ERROR(id, fn) \
  240. wx__DECLARE_EVT1(wxEVT_WEBVIEW_ERROR, id, \
  241. wxWebViewEventHandler(fn))
  242. #define EVT_WEBVIEW_NEWWINDOW(id, fn) \
  243. wx__DECLARE_EVT1(wxEVT_WEBVIEW_NEWWINDOW, id, \
  244. wxWebViewEventHandler(fn))
  245. #define EVT_WEBVIEW_TITLE_CHANGED(id, fn) \
  246. wx__DECLARE_EVT1(wxEVT_WEBVIEW_TITLE_CHANGED, id, \
  247. wxWebViewEventHandler(fn))
  248. // old wxEVT_COMMAND_* constants
  249. #define wxEVT_COMMAND_WEBVIEW_NAVIGATING wxEVT_WEBVIEW_NAVIGATING
  250. #define wxEVT_COMMAND_WEBVIEW_NAVIGATED wxEVT_WEBVIEW_NAVIGATED
  251. #define wxEVT_COMMAND_WEBVIEW_LOADED wxEVT_WEBVIEW_LOADED
  252. #define wxEVT_COMMAND_WEBVIEW_ERROR wxEVT_WEBVIEW_ERROR
  253. #define wxEVT_COMMAND_WEBVIEW_NEWWINDOW wxEVT_WEBVIEW_NEWWINDOW
  254. #define wxEVT_COMMAND_WEBVIEW_TITLE_CHANGED wxEVT_WEBVIEW_TITLE_CHANGED
  255. #endif // wxUSE_WEBVIEW
  256. #endif // _WX_WEBVIEW_H_