1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Common interface and events for web view component
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon
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>
22 * Zoom level in web view component
27 wxWEB_VIEW_ZOOM_SMALL
,
28 wxWEB_VIEW_ZOOM_MEDIUM
,
29 wxWEB_VIEW_ZOOM_LARGE
,
30 wxWEB_VIEW_ZOOM_LARGEST
34 * The type of zooming that the web view control can perform
36 enum wxWebViewZoomType
38 /** The entire layout scales when zooming, including images */
39 wxWEB_VIEW_ZOOM_TYPE_LAYOUT
,
40 /** Only the text changes in size when zooming, images and other layout
41 * elements retain their initial size */
42 wxWEB_VIEW_ZOOM_TYPE_TEXT
45 /** Types of errors that can cause navigation to fail */
46 enum wxWebNavigationError
48 /** Connection error (timeout, etc.) */
49 wxWEB_NAV_ERR_CONNECTION
,
50 /** Invalid certificate */
51 wxWEB_NAV_ERR_CERTIFICATE
,
52 /** Authentication required */
54 /** Other security error */
55 wxWEB_NAV_ERR_SECURITY
,
56 /** Requested resource not found */
57 wxWEB_NAV_ERR_NOT_FOUND
,
58 /** Invalid request/parameters (e.g. bad URL, bad protocol,
59 * unsupported resource type) */
60 wxWEB_NAV_ERR_REQUEST
,
61 /** The user cancelled (e.g. in a dialog) */
62 wxWEB_NAV_ERR_USER_CANCELLED
,
63 /** Another (exotic) type of error that didn't fit in other categories*/
67 /** Type of refresh */
68 enum wxWebViewReloadFlags
70 /** Default reload, will access cache */
71 wxWEB_VIEW_RELOAD_DEFAULT
,
72 /** Reload the current view without accessing the cache */
73 wxWEB_VIEW_RELOAD_NO_CACHE
78 * List of available backends for wxWebView
82 /** Value that may be passed to wxWebView to let it pick an appropriate
83 * engine for the current platform*/
84 wxWEB_VIEW_BACKEND_DEFAULT
,
86 /** The OSX-native WebKit web engine */
87 wxWEB_VIEW_BACKEND_OSX_WEBKIT
,
89 /** The GTK port of the WebKit engine */
90 wxWEB_VIEW_BACKEND_GTK_WEBKIT
,
92 /** Use Microsoft Internet Explorer as web engine */
96 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr
[];
97 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr
[];
99 class WXDLLIMPEXP_WEB wxWebView
: public wxControl
104 * Creation function for two-step creation.
106 virtual bool Create(wxWindow
* parent
,
112 const wxString
& name
) = 0;
115 * Factory function to create a new wxWebView for two-step creation
116 * (you need to call wxWebView::Create on the returned object)
117 * @param backend which web engine to use as backend for wxWebView
118 * @return the created wxWebView, or NULL if the requested backend is
121 static wxWebView
* New(wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
);
123 // TODO: clarify what styles can do, or remove this flag
125 * Factory function to create a new wxWebView
126 * @param parent parent window to create this view in
127 * @param id ID of this control
128 * @param url URL to load by default in the web view
129 * @param pos position to create this control at
130 * (you may use wxDefaultPosition if you use sizers)
131 * @param size size to create this control with
132 * (you may use wxDefaultSize if you use sizers)
133 * @param backend which web engine to use as backend for wxWebView
134 * @return the created wxWebView, or NULL if the requested backend
137 static wxWebView
* New(wxWindow
* parent
,
139 const wxString
& url
= wxWebViewDefaultURLStr
,
140 const wxPoint
& pos
= wxDefaultPosition
,
141 const wxSize
& size
= wxDefaultSize
,
142 wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
,
144 const wxString
& name
= wxWebViewNameStr
);
147 /** Get whether it is possible to navigate back in the history of
150 virtual bool CanGoBack() = 0;
152 /** Get whether it is possible to navigate forward in the history of
155 virtual bool CanGoForward() = 0;
157 /** Navigate back in the history of visited pages.
158 * Only valid if CanGoBack() returned true.
160 virtual void GoBack() = 0;
162 /** Navigate forwardin the history of visited pages.
163 * Only valid if CanGoForward() returned true.
165 virtual void GoForward() = 0;
168 * Load a HTMl document (web page) from a URL
169 * @param url the URL where the HTML document to display can be found
170 * @note web engines generally report errors asynchronously, so if you wish
171 * to know whether loading the URL was successful, register to receive
172 * navigation error events
174 virtual void LoadUrl(const wxString
& url
) = 0;
176 virtual void ClearHistory() = 0;
177 virtual void EnableHistory(bool enable
= true) = 0;
180 * Stop the current page loading process, if any.
181 * May trigger an error event of type wxWEB_NAV_ERR_USER_CANCELLED.
182 * TODO: make wxWEB_NAV_ERR_USER_CANCELLED errors uniform across ports.
184 virtual void Stop() = 0;
187 * Reload the currently displayed URL.
188 * @param flags A bit array that may optionnally contain reload options
190 virtual void Reload(wxWebViewReloadFlags flags
= wxWEB_VIEW_RELOAD_DEFAULT
) = 0;
194 * Get the URL of the currently displayed document
196 virtual wxString
GetCurrentURL() = 0;
199 * Get the title of the current web page, or its URL/path if title is not
202 virtual wxString
GetCurrentTitle() = 0;
204 // TODO: handle choosing a frame when calling GetPageSource()?
206 * Get the HTML source code of the currently displayed document
207 * @return the HTML source code, or an empty string if no page is currently
210 virtual wxString
GetPageSource() = 0;
213 * Get the zoom factor of the page
214 * @return How much the HTML document is zoomed (scaleed)
216 virtual wxWebViewZoom
GetZoom() = 0;
219 * Set the zoom factor of the page
220 * @param zoom How much to zoom (scale) the HTML document
222 virtual void SetZoom(wxWebViewZoom zoom
) = 0;
225 * Set how to interpret the zoom factor
226 * @param zoomType how the zoom factor should be interpreted by the
228 * @note invoke canSetZoomType() first, some HTML renderers may not
229 * support all zoom types
231 virtual void SetZoomType(wxWebViewZoomType zoomType
) = 0;
234 * Get how the zoom factor is currently interpreted
235 * @return how the zoom factor is currently interpreted by the HTML engine
237 virtual wxWebViewZoomType
GetZoomType() const = 0;
240 * Retrieve whether the current HTML engine supports a type of zoom
241 * @param type the type of zoom to test
242 * @return whether this type of zoom is supported by this HTML engine
243 * (and thus can be set through setZoomType())
245 virtual bool CanSetZoomType(wxWebViewZoomType type
) const = 0;
247 // TODO: allow 'SetPage' to find files (e.g. images) from a virtual file
248 // system if possible
250 * Set the displayed page source to the contents of the given string
251 * @param html the string that contains the HTML data to display
252 * @param baseUrl URL assigned to the HTML data, to be used to resolve
253 * relative paths, for instance
255 virtual void SetPage(const wxString
& html
, const wxString
& baseUrl
) = 0;
258 * Set the displayed page source to the contents of the given stream
259 * @param html the stream to read HTML data from
260 * @param baseUrl URL assigned to the HTML data, to be used to resolve
261 * relative paths, for instance
263 virtual void SetPage(wxInputStream
& html
, wxString baseUrl
)
265 wxStringOutputStream stream
;
267 SetPage(stream
.GetString(), baseUrl
);
271 // wxString GetSelection(); // maybe?
272 // void SetSelection(...); // maybe?
274 // void MakeEditable(bool enable = true); // maybe?
275 // bool IsEditable(); // maybe?
277 // void EnableJavascript(bool enabled); // maybe?
278 // wxString RunScript(const wxString& javascript); // maybe?
280 // void SetScrollPos(int pos); // maybe?
281 // int GetScrollPos(); // maybe?
283 // wxString GetStatusText(); // maybe?
284 // void SetStatusText(wxString text); // maybe?
285 // * status text changed event?
286 // * title changed event?
288 // virtual bool IsOfflineMode() = 0; // maybe?
289 // virtual void SetOfflineMode(bool offline) = 0; // maybe?
291 // TODO: offer API to control the opening of new frames
292 // (through <a target="..."> as well as through javascript), OR
293 // provide a behavior consistent across ports.
294 // - OSX : I receive an event for new frames opened with HTML target, and
295 // currently block them all.
296 // - IE : An event is recieved for new frames and they are currently
298 // - GTK : All frame open requests are blocked. A slot exists that I could
299 // connect to to be notified if ever needed
302 * Opens a print dialog so that the user may print the currently
305 virtual void Print() = 0;
308 * Returns whether the web control is currently busy (e.g. loading a page)
310 virtual bool IsBusy() = 0;
313 class WXDLLIMPEXP_WEB wxWebNavigationEvent
: public wxCommandEvent
316 wxWebNavigationEvent() {}
317 wxWebNavigationEvent(wxEventType type
, int id
, const wxString href
,
318 const wxString target
, bool canVeto
)
319 : wxCommandEvent(type
, id
)
328 * Get the URL being visited
330 const wxString
& GetHref() const { return m_href
; }
333 * Get the target (frame or window) in which the URL that caused this event
334 * is viewed, or an empty string if not available.
336 const wxString
& GetTarget() const { return m_target
; }
338 // default copy ctor, assignment operator and dtor are ok
339 virtual wxEvent
* Clone() const { return new wxWebNavigationEvent(*this); }
341 /** Get whether this event may be vetoed (stopped/prevented). Only
342 * meaningful for events fired before navigation takes place.
344 bool CanVeto() const { return m_canVeto
; }
346 /** Whether this event was vetoed (stopped/prevented). Only meaningful for
347 * events fired before navigation takes place.
349 bool IsVetoed() const { return m_vetoed
; }
351 /** Veto (prevent/stop) this event. Only meaningful for events fired
352 * before navigation takes place. Only valid if CanVeto() returned true.
354 void Veto() { wxASSERT(m_canVeto
); m_vetoed
= true; }
362 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebNavigationEvent
);
365 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATING
, wxWebNavigationEvent
);
366 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATED
, wxWebNavigationEvent
);
367 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_LOADED
, wxWebNavigationEvent
);
368 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_ERROR
, wxWebNavigationEvent
);
369 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
, wxWebNavigationEvent
);
371 typedef void (wxEvtHandler::*wxWebNavigationEventFunction
)
372 (wxWebNavigationEvent
&);
374 #define wxWebNavigationEventHandler(func) \
375 wxEVENT_HANDLER_CAST(wxWebNavigationEventFunction, func)
377 #define EVT_WEB_VIEW_NAVIGATING(id, fn) \
378 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
379 wxHtmlNavigatingEventHandler(fn))
381 #define EVT_WEB_VIEW_NAVIGATED(id, fn) \
382 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
383 wxHtmlNavigatingEventHandler(fn))
385 #define EVT_WEB_VIEW_LOADED(id, fn) \
386 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
387 wxHtmlNavigatingEventHandler(fn))
389 #define EVT_WEB_VIEW_ERRROR(id, fn) \
390 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
391 wxHtmlNavigatingEventHandler(fn))
393 #define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
394 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
395 wxHtmlNavigatingEventHandler(fn))
399 #endif // _WX_WEB_VIEW_H_