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 wxWebHandler
107 virtual wxString
GetName() const = 0;
108 virtual wxFSFile
* GetFile(const wxString
&uri
) = 0;
109 virtual wxString
CombineURIs(const wxString
&baseuri
, const wxString
&newuri
) = 0;
112 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr
[];
113 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr
[];
115 class WXDLLIMPEXP_WEB wxWebView
: public wxControl
120 * Creation function for two-step creation.
122 virtual bool Create(wxWindow
* parent
,
128 const wxString
& name
) = 0;
131 * Factory function to create a new wxWebView for two-step creation
132 * (you need to call wxWebView::Create on the returned object)
133 * @param backend which web engine to use as backend for wxWebView
134 * @return the created wxWebView, or NULL if the requested backend is
137 static wxWebView
* New(wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
);
139 // TODO: clarify what styles can do, or remove this flag
141 * Factory function to create a new wxWebView
142 * @param parent parent window to create this view in
143 * @param id ID of this control
144 * @param url URL to load by default in the web view
145 * @param pos position to create this control at
146 * (you may use wxDefaultPosition if you use sizers)
147 * @param size size to create this control with
148 * (you may use wxDefaultSize if you use sizers)
149 * @param backend which web engine to use as backend for wxWebView
150 * @return the created wxWebView, or NULL if the requested backend
153 static wxWebView
* New(wxWindow
* parent
,
155 const wxString
& url
= wxWebViewDefaultURLStr
,
156 const wxPoint
& pos
= wxDefaultPosition
,
157 const wxSize
& size
= wxDefaultSize
,
158 wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
,
160 const wxString
& name
= wxWebViewNameStr
);
163 /** Get whether it is possible to navigate back in the history of
166 virtual bool CanGoBack() = 0;
168 /** Get whether it is possible to navigate forward in the history of
171 virtual bool CanGoForward() = 0;
173 /** Navigate back in the history of visited pages.
174 * Only valid if CanGoBack() returned true.
176 virtual void GoBack() = 0;
178 /** Navigate forwardin the history of visited pages.
179 * Only valid if CanGoForward() returned true.
181 virtual void GoForward() = 0;
184 * Load a HTMl document (web page) from a URL
185 * @param url the URL where the HTML document to display can be found
186 * @note web engines generally report errors asynchronously, so if you wish
187 * to know whether loading the URL was successful, register to receive
188 * navigation error events
190 virtual void LoadUrl(const wxString
& url
) = 0;
192 virtual void ClearHistory() = 0;
193 virtual void EnableHistory(bool enable
= true) = 0;
194 virtual wxVector
<wxSharedPtr
<wxWebHistoryItem
> > GetBackwardHistory() = 0;
195 virtual wxVector
<wxSharedPtr
<wxWebHistoryItem
> > GetForwardHistory() = 0;
196 virtual void LoadHistoryItem(wxSharedPtr
<wxWebHistoryItem
> item
) = 0;
199 * Stop the current page loading process, if any.
200 * May trigger an error event of type wxWEB_NAV_ERR_USER_CANCELLED.
201 * TODO: make wxWEB_NAV_ERR_USER_CANCELLED errors uniform across ports.
203 virtual void Stop() = 0;
206 * Reload the currently displayed URL.
207 * @param flags A bit array that may optionnally contain reload options
209 virtual void Reload(wxWebViewReloadFlags flags
= wxWEB_VIEW_RELOAD_DEFAULT
) = 0;
213 * Get the URL of the currently displayed document
215 virtual wxString
GetCurrentURL() = 0;
218 * Get the title of the current web page, or its URL/path if title is not
221 virtual wxString
GetCurrentTitle() = 0;
223 // TODO: handle choosing a frame when calling GetPageSource()?
225 * Get the HTML source code of the currently displayed document
226 * @return the HTML source code, or an empty string if no page is currently
229 virtual wxString
GetPageSource() = 0;
230 virtual wxString
GetPageText() = 0;
233 * Get the zoom factor of the page
234 * @return How much the HTML document is zoomed (scaleed)
236 virtual wxWebViewZoom
GetZoom() = 0;
239 * Set the zoom factor of the page
240 * @param zoom How much to zoom (scale) the HTML document
242 virtual void SetZoom(wxWebViewZoom zoom
) = 0;
245 * Set how to interpret the zoom factor
246 * @param zoomType how the zoom factor should be interpreted by the
248 * @note invoke canSetZoomType() first, some HTML renderers may not
249 * support all zoom types
251 virtual void SetZoomType(wxWebViewZoomType zoomType
) = 0;
254 * Get how the zoom factor is currently interpreted
255 * @return how the zoom factor is currently interpreted by the HTML engine
257 virtual wxWebViewZoomType
GetZoomType() const = 0;
260 * Retrieve whether the current HTML engine supports a type of zoom
261 * @param type the type of zoom to test
262 * @return whether this type of zoom is supported by this HTML engine
263 * (and thus can be set through setZoomType())
265 virtual bool CanSetZoomType(wxWebViewZoomType type
) const = 0;
267 // TODO: allow 'SetPage' to find files (e.g. images) from a virtual file
268 // system if possible
270 * Set the displayed page source to the contents of the given string
271 * @param html the string that contains the HTML data to display
272 * @param baseUrl URL assigned to the HTML data, to be used to resolve
273 * relative paths, for instance
275 virtual void SetPage(const wxString
& html
, const wxString
& baseUrl
) = 0;
278 * Set the displayed page source to the contents of the given stream
279 * @param html the stream to read HTML data from
280 * @param baseUrl URL assigned to the HTML data, to be used to resolve
281 * relative paths, for instance
283 virtual void SetPage(wxInputStream
& html
, wxString baseUrl
)
285 wxStringOutputStream stream
;
287 SetPage(stream
.GetString(), baseUrl
);
290 virtual void SetEditable(bool enable
= true) = 0;
291 virtual bool IsEditable() = 0;
293 virtual void SelectAll() = 0;
294 virtual bool HasSelection() = 0;
295 virtual void DeleteSelection() = 0;
296 virtual wxString
GetSelectedText() = 0;
297 virtual wxString
GetSelectedSource() = 0;
298 virtual void ClearSelection() = 0;
300 virtual void RunScript(const wxString
& javascript
) = 0;
303 // void EnableJavascript(bool enabled); // maybe?
306 // void SetScrollPos(int pos); // maybe?
307 // int GetScrollPos(); // maybe?
309 // wxString GetStatusText(); // maybe?
310 // void SetStatusText(wxString text); // maybe?
311 // * status text changed event?
312 // * title changed event?
314 // virtual bool IsOfflineMode() = 0; // maybe?
315 // virtual void SetOfflineMode(bool offline) = 0; // maybe?
318 * Opens a print dialog so that the user may print the currently
321 virtual void Print() = 0;
324 * Returns whether the web control is currently busy (e.g. loading a page)
326 virtual bool IsBusy() = 0;
328 //Clipboard functions
329 virtual bool CanCut() = 0;
330 virtual bool CanCopy() = 0;
331 virtual bool CanPaste() = 0;
332 virtual void Cut() = 0;
333 virtual void Copy() = 0;
334 virtual void Paste() = 0;
336 //Undo / redo functionality
337 virtual bool CanUndo() = 0;
338 virtual bool CanRedo() = 0;
339 virtual void Undo() = 0;
340 virtual void Redo() = 0;
342 //Virtual Filesystem Support
343 virtual void RegisterHandler(wxWebHandler
* handler
) = 0;
346 class WXDLLIMPEXP_WEB wxWebNavigationEvent
: public wxCommandEvent
349 wxWebNavigationEvent() {}
350 wxWebNavigationEvent(wxEventType type
, int id
, const wxString url
,
351 const wxString target
, bool canVeto
)
352 : wxCommandEvent(type
, id
)
361 * Get the URL being visited
363 const wxString
& GetURL() const { return m_url
; }
366 * Get the target (frame or window) in which the URL that caused this event
367 * is viewed, or an empty string if not available.
369 const wxString
& GetTarget() const { return m_target
; }
371 // default copy ctor, assignment operator and dtor are ok
372 virtual wxEvent
* Clone() const { return new wxWebNavigationEvent(*this); }
374 /** Get whether this event may be vetoed (stopped/prevented). Only
375 * meaningful for events fired before navigation takes place.
377 bool CanVeto() const { return m_canVeto
; }
379 /** Whether this event was vetoed (stopped/prevented). Only meaningful for
380 * events fired before navigation takes place.
382 bool IsVetoed() const { return m_vetoed
; }
384 /** Veto (prevent/stop) this event. Only meaningful for events fired
385 * before navigation takes place. Only valid if CanVeto() returned true.
387 void Veto() { wxASSERT(m_canVeto
); m_vetoed
= true; }
395 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebNavigationEvent
);
398 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATING
, wxWebNavigationEvent
);
399 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATED
, wxWebNavigationEvent
);
400 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_LOADED
, wxWebNavigationEvent
);
401 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_ERROR
, wxWebNavigationEvent
);
402 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
, wxWebNavigationEvent
);
403 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
, wxWebNavigationEvent
);
405 typedef void (wxEvtHandler::*wxWebNavigationEventFunction
)
406 (wxWebNavigationEvent
&);
408 #define wxWebNavigationEventHandler(func) \
409 wxEVENT_HANDLER_CAST(wxWebNavigationEventFunction, func)
411 #define EVT_WEB_VIEW_NAVIGATING(id, fn) \
412 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
413 wxHtmlNavigatingEventHandler(fn))
415 #define EVT_WEB_VIEW_NAVIGATED(id, fn) \
416 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
417 wxHtmlNavigatingEventHandler(fn))
419 #define EVT_WEB_VIEW_LOADED(id, fn) \
420 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
421 wxHtmlNavigatingEventHandler(fn))
423 #define EVT_WEB_VIEW_ERRROR(id, fn) \
424 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
425 wxHtmlNavigatingEventHandler(fn))
427 #define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
428 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
429 wxHtmlNavigatingEventHandler(fn))
431 #define EVT_WEB_VIEW_TITLE_CHANGED(id, fn) \
432 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, id, \
433 wxHtmlNavigatingEventHandler(fn))
437 #endif // _WX_WEB_VIEW_H_