1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Common interface and events for web view component
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_WEB_VIEW_H_
11 #define _WX_WEB_VIEW_H_
17 #include "wx/control.h"
19 #include "wx/sstream.h"
20 #include "wx/sharedptr.h"
21 #include "wx/vector.h"
23 #if defined(__WXOSX__)
24 #include "wx/osx/webviewhistoryitem_webkit.h"
25 #elif defined(__WXGTK__)
26 #include "wx/gtk/webviewhistoryitem_webkit.h"
27 #elif defined(__WXMSW__)
28 #include "wx/msw/webviewhistoryitem_ie.h"
30 #error "wxWebView not implemented on this platform."
40 wxWEB_VIEW_ZOOM_SMALL
,
41 wxWEB_VIEW_ZOOM_MEDIUM
,
42 wxWEB_VIEW_ZOOM_LARGE
,
43 wxWEB_VIEW_ZOOM_LARGEST
46 enum wxWebViewZoomType
48 //Scales entire page, including images
49 wxWEB_VIEW_ZOOM_TYPE_LAYOUT
,
50 wxWEB_VIEW_ZOOM_TYPE_TEXT
53 enum wxWebViewNavigationError
55 wxWEB_NAV_ERR_CONNECTION
,
56 wxWEB_NAV_ERR_CERTIFICATE
,
58 wxWEB_NAV_ERR_SECURITY
,
59 wxWEB_NAV_ERR_NOT_FOUND
,
60 wxWEB_NAV_ERR_REQUEST
,
61 wxWEB_NAV_ERR_USER_CANCELLED
,
65 enum wxWebViewReloadFlags
67 //Default, may access cache
68 wxWEB_VIEW_RELOAD_DEFAULT
,
69 wxWEB_VIEW_RELOAD_NO_CACHE
72 enum wxWebViewFindFlags
74 wxWEB_VIEW_FIND_WRAP
= 0x0001,
75 wxWEB_VIEW_FIND_ENTIRE_WORD
= 0x0002,
76 wxWEB_VIEW_FIND_MATCH_CASE
= 0x0004,
77 wxWEB_VIEW_FIND_HIGHLIGHT_RESULT
= 0x0008,
78 wxWEB_VIEW_FIND_BACKWARDS
= 0x0010,
79 wxWEB_VIEW_FIND_DEFAULT
= 0
82 //Base class for custom scheme handlers
83 class WXDLLIMPEXP_WEBVIEW wxWebViewHandler
86 wxWebViewHandler(const wxString
& scheme
) : m_scheme(scheme
) {}
87 virtual ~wxWebViewHandler() {}
88 virtual wxString
GetName() const { return m_scheme
; }
89 virtual wxFSFile
* GetFile(const wxString
&uri
) = 0;
94 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewNameStr
[];
95 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewDefaultURLStr
[];
96 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendDefault
[];
97 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendIE
[];
98 extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewBackendWebKit
[];
100 class WXDLLIMPEXP_WEBVIEW wxWebViewFactory
: public wxObject
103 virtual wxWebView
* Create() = 0;
104 virtual wxWebView
* Create(wxWindow
* parent
,
106 const wxString
& url
= wxWebViewDefaultURLStr
,
107 const wxPoint
& pos
= wxDefaultPosition
,
108 const wxSize
& size
= wxDefaultSize
,
110 const wxString
& name
= wxWebViewNameStr
) = 0;
113 WX_DECLARE_STRING_HASH_MAP(wxSharedPtr
<wxWebViewFactory
>, wxStringWebViewFactoryMap
);
115 class WXDLLIMPEXP_WEBVIEW wxWebView
: public wxControl
123 virtual ~wxWebView() {}
125 virtual bool Create(wxWindow
* parent
,
127 const wxString
& url
= wxWebViewDefaultURLStr
,
128 const wxPoint
& pos
= wxDefaultPosition
,
129 const wxSize
& size
= wxDefaultSize
,
131 const wxString
& name
= wxWebViewNameStr
) = 0;
133 // Factory methods allowing the use of custom factories registered with
135 static wxWebView
* New(const wxString
& backend
= wxWebViewBackendDefault
);
136 static wxWebView
* New(wxWindow
* parent
,
138 const wxString
& url
= wxWebViewDefaultURLStr
,
139 const wxPoint
& pos
= wxDefaultPosition
,
140 const wxSize
& size
= wxDefaultSize
,
141 const wxString
& backend
= wxWebViewBackendDefault
,
143 const wxString
& name
= wxWebViewNameStr
);
145 static void RegisterFactory(const wxString
& backend
,
146 wxSharedPtr
<wxWebViewFactory
> factory
);
149 virtual void EnableContextMenu(bool enable
= true)
153 virtual wxString
GetCurrentTitle() const = 0;
154 virtual wxString
GetCurrentURL() const = 0;
155 // TODO: handle choosing a frame when calling GetPageSource()?
156 virtual wxString
GetPageSource() const = 0;
157 virtual wxString
GetPageText() const = 0;
158 virtual bool IsBusy() const = 0;
159 virtual bool IsContextMenuEnabled() const { return m_showMenu
; }
160 virtual bool IsEditable() const = 0;
161 virtual void LoadURL(const wxString
& url
) = 0;
162 virtual void Print() = 0;
163 virtual void RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
) = 0;
164 virtual void Reload(wxWebViewReloadFlags flags
= wxWEB_VIEW_RELOAD_DEFAULT
) = 0;
165 virtual void RunScript(const wxString
& javascript
) = 0;
166 virtual void SetEditable(bool enable
= true) = 0;
167 void SetPage(const wxString
& html
, const wxString
& baseUrl
)
169 DoSetPage(html
, baseUrl
);
171 void SetPage(wxInputStream
& html
, wxString baseUrl
)
173 wxStringOutputStream stream
;
175 DoSetPage(stream
.GetString(), baseUrl
);
177 virtual void Stop() = 0;
180 virtual bool CanGoBack() const = 0;
181 virtual bool CanGoForward() const = 0;
182 virtual void GoBack() = 0;
183 virtual void GoForward() = 0;
184 virtual void ClearHistory() = 0;
185 virtual void EnableHistory(bool enable
= true) = 0;
186 virtual wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > GetBackwardHistory() = 0;
187 virtual wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > GetForwardHistory() = 0;
188 virtual void LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
) = 0;
191 virtual bool CanSetZoomType(wxWebViewZoomType type
) const = 0;
192 virtual wxWebViewZoom
GetZoom() const = 0;
193 virtual wxWebViewZoomType
GetZoomType() const = 0;
194 virtual void SetZoom(wxWebViewZoom zoom
) = 0;
195 virtual void SetZoomType(wxWebViewZoomType zoomType
) = 0;
198 virtual void SelectAll() = 0;
199 virtual bool HasSelection() const = 0;
200 virtual void DeleteSelection() = 0;
201 virtual wxString
GetSelectedText() const = 0;
202 virtual wxString
GetSelectedSource() const = 0;
203 virtual void ClearSelection() = 0;
205 //Clipboard functions
206 virtual bool CanCut() const = 0;
207 virtual bool CanCopy() const = 0;
208 virtual bool CanPaste() const = 0;
209 virtual void Cut() = 0;
210 virtual void Copy() = 0;
211 virtual void Paste() = 0;
213 //Undo / redo functionality
214 virtual bool CanUndo() const = 0;
215 virtual bool CanRedo() const = 0;
216 virtual void Undo() = 0;
217 virtual void Redo() = 0;
219 //Get the pointer to the underlying native engine.
220 virtual void* GetNativeBackend() const = 0;
222 virtual long Find(const wxString
& text
, int flags
= wxWEB_VIEW_FIND_DEFAULT
) = 0;
225 virtual void DoSetPage(const wxString
& html
, const wxString
& baseUrl
) = 0;
228 static void InitFactoryMap();
229 static wxStringWebViewFactoryMap::iterator
FindFactory(const wxString
&backend
);
232 static wxStringWebViewFactoryMap m_factoryMap
;
234 wxDECLARE_ABSTRACT_CLASS(wxWebView
);
237 class WXDLLIMPEXP_WEBVIEW wxWebViewEvent
: public wxNotifyEvent
241 wxWebViewEvent(wxEventType type
, int id
, const wxString url
,
242 const wxString target
)
243 : wxNotifyEvent(type
, id
), m_url(url
), m_target(target
)
247 const wxString
& GetURL() const { return m_url
; }
248 const wxString
& GetTarget() const { return m_target
; }
250 virtual wxEvent
* Clone() const { return new wxWebViewEvent(*this); }
255 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebViewEvent
);
258 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_COMMAND_WEB_VIEW_NAVIGATING
, wxWebViewEvent
);
259 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_COMMAND_WEB_VIEW_NAVIGATED
, wxWebViewEvent
);
260 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_COMMAND_WEB_VIEW_LOADED
, wxWebViewEvent
);
261 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_COMMAND_WEB_VIEW_ERROR
, wxWebViewEvent
);
262 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
, wxWebViewEvent
);
263 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW
, wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
, wxWebViewEvent
);
265 typedef void (wxEvtHandler::*wxWebViewEventFunction
)
268 #define wxWebViewEventHandler(func) \
269 wxEVENT_HANDLER_CAST(wxWebViewEventFunction, func)
271 #define EVT_WEB_VIEW_NAVIGATING(id, fn) \
272 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
273 wxWebViewEventHandler(fn))
275 #define EVT_WEB_VIEW_NAVIGATED(id, fn) \
276 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
277 wxWebViewEventHandler(fn))
279 #define EVT_WEB_VIEW_LOADED(id, fn) \
280 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
281 wxWebViewEventHandler(fn))
283 #define EVT_WEB_VIEW_ERROR(id, fn) \
284 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
285 wxWebViewEventHandler(fn))
287 #define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
288 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
289 wxWebViewEventHandler(fn))
291 #define EVT_WEB_VIEW_TITLE_CHANGED(id, fn) \
292 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, id, \
293 wxWebViewEventHandler(fn))
295 #endif // wxUSE_WEBVIEW
297 #endif // _WX_WEB_VIEW_H_