1 /////////////////////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////////////////////
10 #define _WX_WEBVIEW_H_
16 #include "wx/control.h"
18 #include "wx/sstream.h"
19 #include "wx/sharedptr.h"
20 #include "wx/vector.h"
22 #if defined(__WXOSX__)
23 #include "wx/osx/webviewhistoryitem_webkit.h"
24 #elif defined(__WXGTK__)
25 #include "wx/gtk/webviewhistoryitem_webkit.h"
26 #elif defined(__WXMSW__)
27 #include "wx/msw/webviewhistoryitem_ie.h"
29 #error "wxWebView not implemented on this platform."
40 wxWEBVIEW_ZOOM_MEDIUM
,
42 wxWEBVIEW_ZOOM_LARGEST
45 enum wxWebViewZoomType
47 //Scales entire page, including images
48 wxWEBVIEW_ZOOM_TYPE_LAYOUT
,
49 wxWEBVIEW_ZOOM_TYPE_TEXT
52 enum wxWebViewNavigationError
54 wxWEBVIEW_NAV_ERR_CONNECTION
,
55 wxWEBVIEW_NAV_ERR_CERTIFICATE
,
56 wxWEBVIEW_NAV_ERR_AUTH
,
57 wxWEBVIEW_NAV_ERR_SECURITY
,
58 wxWEBVIEW_NAV_ERR_NOT_FOUND
,
59 wxWEBVIEW_NAV_ERR_REQUEST
,
60 wxWEBVIEW_NAV_ERR_USER_CANCELLED
,
61 wxWEBVIEW_NAV_ERR_OTHER
64 enum wxWebViewReloadFlags
66 //Default, may access cache
67 wxWEBVIEW_RELOAD_DEFAULT
,
68 wxWEBVIEW_RELOAD_NO_CACHE
71 enum wxWebViewFindFlags
73 wxWEBVIEW_FIND_WRAP
= 0x0001,
74 wxWEBVIEW_FIND_ENTIRE_WORD
= 0x0002,
75 wxWEBVIEW_FIND_MATCH_CASE
= 0x0004,
76 wxWEBVIEW_FIND_HIGHLIGHT_RESULT
= 0x0008,
77 wxWEBVIEW_FIND_BACKWARDS
= 0x0010,
78 wxWEBVIEW_FIND_DEFAULT
= 0
81 //Base class for custom scheme handlers
82 class WXDLLIMPEXP_WEBVIEW wxWebViewHandler
85 wxWebViewHandler(const wxString
& scheme
) : m_scheme(scheme
) {}
86 virtual ~wxWebViewHandler() {}
87 virtual wxString
GetName() const { return m_scheme
; }
88 virtual wxFSFile
* GetFile(const wxString
&uri
) = 0;
93 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewNameStr
[];
94 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewDefaultURLStr
[];
95 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendDefault
[];
96 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendIE
[];
97 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendWebKit
[];
99 class WXDLLIMPEXP_WEBVIEW wxWebViewFactory
: public wxObject
102 virtual wxWebView
* Create() = 0;
103 virtual wxWebView
* Create(wxWindow
* parent
,
105 const wxString
& url
= wxWebViewDefaultURLStr
,
106 const wxPoint
& pos
= wxDefaultPosition
,
107 const wxSize
& size
= wxDefaultSize
,
109 const wxString
& name
= wxWebViewNameStr
) = 0;
112 WX_DECLARE_STRING_HASH_MAP(wxSharedPtr
<wxWebViewFactory
>, wxStringWebViewFactoryMap
);
114 class WXDLLIMPEXP_WEBVIEW wxWebView
: public wxControl
122 virtual ~wxWebView() {}
124 virtual bool Create(wxWindow
* parent
,
126 const wxString
& url
= wxWebViewDefaultURLStr
,
127 const wxPoint
& pos
= wxDefaultPosition
,
128 const wxSize
& size
= wxDefaultSize
,
130 const wxString
& name
= wxWebViewNameStr
) = 0;
132 // Factory methods allowing the use of custom factories registered with
134 static wxWebView
* New(const wxString
& backend
= wxWebViewBackendDefault
);
135 static wxWebView
* New(wxWindow
* parent
,
137 const wxString
& url
= wxWebViewDefaultURLStr
,
138 const wxPoint
& pos
= wxDefaultPosition
,
139 const wxSize
& size
= wxDefaultSize
,
140 const wxString
& backend
= wxWebViewBackendDefault
,
142 const wxString
& name
= wxWebViewNameStr
);
144 static void RegisterFactory(const wxString
& backend
,
145 wxSharedPtr
<wxWebViewFactory
> factory
);
148 virtual void EnableContextMenu(bool enable
= true)
152 virtual wxString
GetCurrentTitle() const = 0;
153 virtual wxString
GetCurrentURL() const = 0;
154 // TODO: handle choosing a frame when calling GetPageSource()?
155 virtual wxString
GetPageSource() const = 0;
156 virtual wxString
GetPageText() const = 0;
157 virtual bool IsBusy() const = 0;
158 virtual bool IsContextMenuEnabled() const { return m_showMenu
; }
159 virtual bool IsEditable() const = 0;
160 virtual void LoadURL(const wxString
& url
) = 0;
161 virtual void Print() = 0;
162 virtual void RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
) = 0;
163 virtual void Reload(wxWebViewReloadFlags flags
= wxWEBVIEW_RELOAD_DEFAULT
) = 0;
164 virtual void RunScript(const wxString
& javascript
) = 0;
165 virtual void SetEditable(bool enable
= true) = 0;
166 void SetPage(const wxString
& html
, const wxString
& baseUrl
)
168 DoSetPage(html
, baseUrl
);
170 void SetPage(wxInputStream
& html
, wxString baseUrl
)
172 wxStringOutputStream stream
;
174 DoSetPage(stream
.GetString(), baseUrl
);
176 virtual void Stop() = 0;
179 virtual bool CanGoBack() const = 0;
180 virtual bool CanGoForward() const = 0;
181 virtual void GoBack() = 0;
182 virtual void GoForward() = 0;
183 virtual void ClearHistory() = 0;
184 virtual void EnableHistory(bool enable
= true) = 0;
185 virtual wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > GetBackwardHistory() = 0;
186 virtual wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > GetForwardHistory() = 0;
187 virtual void LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
) = 0;
190 virtual bool CanSetZoomType(wxWebViewZoomType type
) const = 0;
191 virtual wxWebViewZoom
GetZoom() const = 0;
192 virtual wxWebViewZoomType
GetZoomType() const = 0;
193 virtual void SetZoom(wxWebViewZoom zoom
) = 0;
194 virtual void SetZoomType(wxWebViewZoomType zoomType
) = 0;
197 virtual void SelectAll() = 0;
198 virtual bool HasSelection() const = 0;
199 virtual void DeleteSelection() = 0;
200 virtual wxString
GetSelectedText() const = 0;
201 virtual wxString
GetSelectedSource() const = 0;
202 virtual void ClearSelection() = 0;
204 //Clipboard functions
205 virtual bool CanCut() const = 0;
206 virtual bool CanCopy() const = 0;
207 virtual bool CanPaste() const = 0;
208 virtual void Cut() = 0;
209 virtual void Copy() = 0;
210 virtual void Paste() = 0;
212 //Undo / redo functionality
213 virtual bool CanUndo() const = 0;
214 virtual bool CanRedo() const = 0;
215 virtual void Undo() = 0;
216 virtual void Redo() = 0;
218 //Get the pointer to the underlying native engine.
219 virtual void* GetNativeBackend() const = 0;
221 virtual long Find(const wxString
& text
, int flags
= wxWEBVIEW_FIND_DEFAULT
) = 0;
224 virtual void DoSetPage(const wxString
& html
, const wxString
& baseUrl
) = 0;
227 static void InitFactoryMap();
228 static wxStringWebViewFactoryMap::iterator
FindFactory(const wxString
&backend
);
231 static wxStringWebViewFactoryMap m_factoryMap
;
233 wxDECLARE_ABSTRACT_CLASS(wxWebView
);
236 class WXDLLIMPEXP_WEBVIEW wxWebViewEvent
: public wxNotifyEvent
240 wxWebViewEvent(wxEventType type
, int id
, const wxString url
,
241 const wxString target
)
242 : wxNotifyEvent(type
, id
), m_url(url
), m_target(target
)
246 const wxString
& GetURL() const { return m_url
; }
247 const wxString
& GetTarget() const { return m_target
; }
249 virtual wxEvent
* Clone() const { return new wxWebViewEvent(*this); }
254 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebViewEvent
);
257 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_WEBVIEW_NAVIGATING
, wxWebViewEvent
);
258 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_WEBVIEW_NAVIGATED
, wxWebViewEvent
);
259 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_WEBVIEW_LOADED
, wxWebViewEvent
);
260 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_WEBVIEW_ERROR
, wxWebViewEvent
);
261 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_WEBVIEW_NEWWINDOW
, wxWebViewEvent
);
262 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_WEBVIEW_TITLE_CHANGED
, wxWebViewEvent
);
264 typedef void (wxEvtHandler::*wxWebViewEventFunction
)
267 #define wxWebViewEventHandler(func) \
268 wxEVENT_HANDLER_CAST(wxWebViewEventFunction, func)
270 #define EVT_WEBVIEW_NAVIGATING(id, fn) \
271 wx__DECLARE_EVT1(wxEVT_WEBVIEW_NAVIGATING, id, \
272 wxWebViewEventHandler(fn))
274 #define EVT_WEBVIEW_NAVIGATED(id, fn) \
275 wx__DECLARE_EVT1(wxEVT_WEBVIEW_NAVIGATED, id, \
276 wxWebViewEventHandler(fn))
278 #define EVT_WEBVIEW_LOADED(id, fn) \
279 wx__DECLARE_EVT1(wxEVT_WEBVIEW_LOADED, id, \
280 wxWebViewEventHandler(fn))
282 #define EVT_WEBVIEW_ERROR(id, fn) \
283 wx__DECLARE_EVT1(wxEVT_WEBVIEW_ERROR, id, \
284 wxWebViewEventHandler(fn))
286 #define EVT_WEBVIEW_NEWWINDOW(id, fn) \
287 wx__DECLARE_EVT1(wxEVT_WEBVIEW_NEWWINDOW, id, \
288 wxWebViewEventHandler(fn))
290 #define EVT_WEBVIEW_TITLE_CHANGED(id, fn) \
291 wx__DECLARE_EVT1(wxEVT_WEBVIEW_TITLE_CHANGED, id, \
292 wxWebViewEventHandler(fn))
294 // old wxEVT_COMMAND_* constants
295 #define wxEVT_COMMAND_WEBVIEW_NAVIGATING wxEVT_WEBVIEW_NAVIGATING
296 #define wxEVT_COMMAND_WEBVIEW_NAVIGATED wxEVT_WEBVIEW_NAVIGATED
297 #define wxEVT_COMMAND_WEBVIEW_LOADED wxEVT_WEBVIEW_LOADED
298 #define wxEVT_COMMAND_WEBVIEW_ERROR wxEVT_WEBVIEW_ERROR
299 #define wxEVT_COMMAND_WEBVIEW_NEWWINDOW wxEVT_WEBVIEW_NEWWINDOW
300 #define wxEVT_COMMAND_WEBVIEW_TITLE_CHANGED wxEVT_WEBVIEW_TITLE_CHANGED
302 #endif // wxUSE_WEBVIEW
304 #endif // _WX_WEBVIEW_H_