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 #include "wx/osx/webhistoryitem_webkit.h"
24 #include "wx/gtk/webhistoryitem_webkit.h"
25 #include "wx/msw/webhistoryitem_ie.h"
32 * Zoom level in web view component
37 wxWEB_VIEW_ZOOM_SMALL
,
38 wxWEB_VIEW_ZOOM_MEDIUM
,
39 wxWEB_VIEW_ZOOM_LARGE
,
40 wxWEB_VIEW_ZOOM_LARGEST
44 * The type of zooming that the web view control can perform
46 enum wxWebViewZoomType
48 /** The entire layout scales when zooming, including images */
49 wxWEB_VIEW_ZOOM_TYPE_LAYOUT
,
50 /** Only the text changes in size when zooming, images and other layout
51 * elements retain their initial size */
52 wxWEB_VIEW_ZOOM_TYPE_TEXT
55 /** Types of errors that can cause navigation to fail */
56 enum wxWebNavigationError
58 /** Connection error (timeout, etc.) */
59 wxWEB_NAV_ERR_CONNECTION
,
60 /** Invalid certificate */
61 wxWEB_NAV_ERR_CERTIFICATE
,
62 /** Authentication required */
64 /** Other security error */
65 wxWEB_NAV_ERR_SECURITY
,
66 /** Requested resource not found */
67 wxWEB_NAV_ERR_NOT_FOUND
,
68 /** Invalid request/parameters (e.g. bad URL, bad protocol,
69 * unsupported resource type) */
70 wxWEB_NAV_ERR_REQUEST
,
71 /** The user cancelled (e.g. in a dialog) */
72 wxWEB_NAV_ERR_USER_CANCELLED
,
73 /** Another (exotic) type of error that didn't fit in other categories*/
77 /** Type of refresh */
78 enum wxWebViewReloadFlags
80 /** Default reload, will access cache */
81 wxWEB_VIEW_RELOAD_DEFAULT
,
82 /** Reload the current view without accessing the cache */
83 wxWEB_VIEW_RELOAD_NO_CACHE
88 * List of available backends for wxWebView
92 /** Value that may be passed to wxWebView to let it pick an appropriate
93 * engine for the current platform*/
94 wxWEB_VIEW_BACKEND_DEFAULT
,
96 /** The WebKit web engine */
97 wxWEB_VIEW_BACKEND_WEBKIT
,
99 /** Use Microsoft Internet Explorer as web engine */
100 wxWEB_VIEW_BACKEND_IE
103 //Base class for custom scheme handlers
104 class WXDLLIMPEXP_WEB wxWebViewHandler
107 wxWebViewHandler(const wxString
& scheme
) : m_scheme(scheme
) {}
108 virtual wxString
GetName() const { return m_scheme
; }
109 virtual wxFSFile
* GetFile(const wxString
&uri
) = 0;
114 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr
[];
115 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr
[];
117 class WXDLLIMPEXP_WEB wxWebView
: public wxControl
122 * Creation function for two-step creation.
124 virtual bool Create(wxWindow
* parent
,
130 const wxString
& name
) = 0;
133 * Factory function to create a new wxWebView for two-step creation
134 * (you need to call wxWebView::Create on the returned object)
135 * @param backend which web engine to use as backend for wxWebView
136 * @return the created wxWebView, or NULL if the requested backend is
139 static wxWebView
* New(wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
);
141 // TODO: clarify what styles can do, or remove this flag
143 * Factory function to create a new wxWebView
144 * @param parent parent window to create this view in
145 * @param id ID of this control
146 * @param url URL to load by default in the web view
147 * @param pos position to create this control at
148 * (you may use wxDefaultPosition if you use sizers)
149 * @param size size to create this control with
150 * (you may use wxDefaultSize if you use sizers)
151 * @param backend which web engine to use as backend for wxWebView
152 * @return the created wxWebView, or NULL if the requested backend
155 static wxWebView
* New(wxWindow
* parent
,
157 const wxString
& url
= wxWebViewDefaultURLStr
,
158 const wxPoint
& pos
= wxDefaultPosition
,
159 const wxSize
& size
= wxDefaultSize
,
160 wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
,
162 const wxString
& name
= wxWebViewNameStr
);
165 /** Get whether it is possible to navigate back in the history of
168 virtual bool CanGoBack() = 0;
170 /** Get whether it is possible to navigate forward in the history of
173 virtual bool CanGoForward() = 0;
175 /** Navigate back in the history of visited pages.
176 * Only valid if CanGoBack() returned true.
178 virtual void GoBack() = 0;
180 /** Navigate forwardin the history of visited pages.
181 * Only valid if CanGoForward() returned true.
183 virtual void GoForward() = 0;
186 * Load a HTMl document (web page) from a URL
187 * @param url the URL where the HTML document to display can be found
188 * @note web engines generally report errors asynchronously, so if you wish
189 * to know whether loading the URL was successful, register to receive
190 * navigation error events
192 virtual void LoadUrl(const wxString
& url
) = 0;
194 virtual void ClearHistory() = 0;
195 virtual void EnableHistory(bool enable
= true) = 0;
196 virtual wxVector
<wxSharedPtr
<wxWebHistoryItem
> > GetBackwardHistory() = 0;
197 virtual wxVector
<wxSharedPtr
<wxWebHistoryItem
> > GetForwardHistory() = 0;
198 virtual void LoadHistoryItem(wxSharedPtr
<wxWebHistoryItem
> item
) = 0;
201 * Stop the current page loading process, if any.
202 * May trigger an error event of type wxWEB_NAV_ERR_USER_CANCELLED.
203 * TODO: make wxWEB_NAV_ERR_USER_CANCELLED errors uniform across ports.
205 virtual void Stop() = 0;
208 * Reload the currently displayed URL.
209 * @param flags A bit array that may optionnally contain reload options
211 virtual void Reload(wxWebViewReloadFlags flags
= wxWEB_VIEW_RELOAD_DEFAULT
) = 0;
215 * Get the URL of the currently displayed document
217 virtual wxString
GetCurrentURL() = 0;
220 * Get the title of the current web page, or its URL/path if title is not
223 virtual wxString
GetCurrentTitle() = 0;
225 // TODO: handle choosing a frame when calling GetPageSource()?
227 * Get the HTML source code of the currently displayed document
228 * @return the HTML source code, or an empty string if no page is currently
231 virtual wxString
GetPageSource() = 0;
232 virtual wxString
GetPageText() = 0;
235 * Get the zoom factor of the page
236 * @return How much the HTML document is zoomed (scaleed)
238 virtual wxWebViewZoom
GetZoom() = 0;
241 * Set the zoom factor of the page
242 * @param zoom How much to zoom (scale) the HTML document
244 virtual void SetZoom(wxWebViewZoom zoom
) = 0;
247 * Set how to interpret the zoom factor
248 * @param zoomType how the zoom factor should be interpreted by the
250 * @note invoke canSetZoomType() first, some HTML renderers may not
251 * support all zoom types
253 virtual void SetZoomType(wxWebViewZoomType zoomType
) = 0;
256 * Get how the zoom factor is currently interpreted
257 * @return how the zoom factor is currently interpreted by the HTML engine
259 virtual wxWebViewZoomType
GetZoomType() const = 0;
262 * Retrieve whether the current HTML engine supports a type of zoom
263 * @param type the type of zoom to test
264 * @return whether this type of zoom is supported by this HTML engine
265 * (and thus can be set through setZoomType())
267 virtual bool CanSetZoomType(wxWebViewZoomType type
) const = 0;
269 // TODO: allow 'SetPage' to find files (e.g. images) from a virtual file
270 // system if possible
272 * Set the displayed page source to the contents of the given string
273 * @param html the string that contains the HTML data to display
274 * @param baseUrl URL assigned to the HTML data, to be used to resolve
275 * relative paths, for instance
277 virtual void SetPage(const wxString
& html
, const wxString
& baseUrl
) = 0;
280 * Set the displayed page source to the contents of the given stream
281 * @param html the stream to read HTML data from
282 * @param baseUrl URL assigned to the HTML data, to be used to resolve
283 * relative paths, for instance
285 virtual void SetPage(wxInputStream
& html
, wxString baseUrl
)
287 wxStringOutputStream stream
;
289 SetPage(stream
.GetString(), baseUrl
);
292 virtual void SetEditable(bool enable
= true) = 0;
293 virtual bool IsEditable() = 0;
295 virtual void SelectAll() = 0;
296 virtual bool HasSelection() = 0;
297 virtual void DeleteSelection() = 0;
298 virtual wxString
GetSelectedText() = 0;
299 virtual wxString
GetSelectedSource() = 0;
300 virtual void ClearSelection() = 0;
302 virtual void RunScript(const wxString
& javascript
) = 0;
305 // void EnableJavascript(bool enabled); // maybe?
308 // void SetScrollPos(int pos); // maybe?
309 // int GetScrollPos(); // maybe?
311 // wxString GetStatusText(); // maybe?
312 // void SetStatusText(wxString text); // maybe?
313 // * status text changed event?
314 // * title changed event?
316 // virtual bool IsOfflineMode() = 0; // maybe?
317 // virtual void SetOfflineMode(bool offline) = 0; // maybe?
320 * Opens a print dialog so that the user may print the currently
323 virtual void Print() = 0;
326 * Returns whether the web control is currently busy (e.g. loading a page)
328 virtual bool IsBusy() = 0;
330 //Clipboard functions
331 virtual bool CanCut() = 0;
332 virtual bool CanCopy() = 0;
333 virtual bool CanPaste() = 0;
334 virtual void Cut() = 0;
335 virtual void Copy() = 0;
336 virtual void Paste() = 0;
338 //Undo / redo functionality
339 virtual bool CanUndo() = 0;
340 virtual bool CanRedo() = 0;
341 virtual void Undo() = 0;
342 virtual void Redo() = 0;
344 //Virtual Filesystem Support
345 virtual void RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
) = 0;
347 wxDECLARE_ABSTRACT_CLASS(wxWebView
);
350 class WXDLLIMPEXP_WEB wxWebNavigationEvent
: public wxCommandEvent
353 wxWebNavigationEvent() {}
354 wxWebNavigationEvent(wxEventType type
, int id
, const wxString url
,
355 const wxString target
, bool canVeto
)
356 : wxCommandEvent(type
, id
)
365 * Get the URL being visited
367 const wxString
& GetURL() const { return m_url
; }
370 * Get the target (frame or window) in which the URL that caused this event
371 * is viewed, or an empty string if not available.
373 const wxString
& GetTarget() const { return m_target
; }
375 // default copy ctor, assignment operator and dtor are ok
376 virtual wxEvent
* Clone() const { return new wxWebNavigationEvent(*this); }
378 /** Get whether this event may be vetoed (stopped/prevented). Only
379 * meaningful for events fired before navigation takes place.
381 bool CanVeto() const { return m_canVeto
; }
383 /** Whether this event was vetoed (stopped/prevented). Only meaningful for
384 * events fired before navigation takes place.
386 bool IsVetoed() const { return m_vetoed
; }
388 /** Veto (prevent/stop) this event. Only meaningful for events fired
389 * before navigation takes place. Only valid if CanVeto() returned true.
391 void Veto() { wxASSERT(m_canVeto
); m_vetoed
= true; }
399 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebNavigationEvent
);
402 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATING
, wxWebNavigationEvent
);
403 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATED
, wxWebNavigationEvent
);
404 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_LOADED
, wxWebNavigationEvent
);
405 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_ERROR
, wxWebNavigationEvent
);
406 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
, wxWebNavigationEvent
);
407 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
, wxWebNavigationEvent
);
409 typedef void (wxEvtHandler::*wxWebNavigationEventFunction
)
410 (wxWebNavigationEvent
&);
412 #define wxWebNavigationEventHandler(func) \
413 wxEVENT_HANDLER_CAST(wxWebNavigationEventFunction, func)
415 #define EVT_WEB_VIEW_NAVIGATING(id, fn) \
416 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
417 wxHtmlNavigatingEventHandler(fn))
419 #define EVT_WEB_VIEW_NAVIGATED(id, fn) \
420 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
421 wxHtmlNavigatingEventHandler(fn))
423 #define EVT_WEB_VIEW_LOADED(id, fn) \
424 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
425 wxHtmlNavigatingEventHandler(fn))
427 #define EVT_WEB_VIEW_ERRROR(id, fn) \
428 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
429 wxHtmlNavigatingEventHandler(fn))
431 #define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
432 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
433 wxHtmlNavigatingEventHandler(fn))
435 #define EVT_WEB_VIEW_TITLE_CHANGED(id, fn) \
436 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, id, \
437 wxHtmlNavigatingEventHandler(fn))
441 #endif // _WX_WEB_VIEW_H_