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>
20 #include "wx/sharedptr.h"
21 #include "wx/vector.h"
23 class WXDLLIMPEXP_WEB wxWebHistoryItem
26 wxWebHistoryItem(const wxString
& url
, const wxString
& title
) :
27 m_url(url
), m_title(title
) {}
28 wxString
GetUrl() { return m_url
; }
29 wxString
GetTitle() { return m_title
; }
32 wxString m_url
, m_title
;
36 * Zoom level in web view component
41 wxWEB_VIEW_ZOOM_SMALL
,
42 wxWEB_VIEW_ZOOM_MEDIUM
,
43 wxWEB_VIEW_ZOOM_LARGE
,
44 wxWEB_VIEW_ZOOM_LARGEST
48 * The type of zooming that the web view control can perform
50 enum wxWebViewZoomType
52 /** The entire layout scales when zooming, including images */
53 wxWEB_VIEW_ZOOM_TYPE_LAYOUT
,
54 /** Only the text changes in size when zooming, images and other layout
55 * elements retain their initial size */
56 wxWEB_VIEW_ZOOM_TYPE_TEXT
59 /** Types of errors that can cause navigation to fail */
60 enum wxWebNavigationError
62 /** Connection error (timeout, etc.) */
63 wxWEB_NAV_ERR_CONNECTION
,
64 /** Invalid certificate */
65 wxWEB_NAV_ERR_CERTIFICATE
,
66 /** Authentication required */
68 /** Other security error */
69 wxWEB_NAV_ERR_SECURITY
,
70 /** Requested resource not found */
71 wxWEB_NAV_ERR_NOT_FOUND
,
72 /** Invalid request/parameters (e.g. bad URL, bad protocol,
73 * unsupported resource type) */
74 wxWEB_NAV_ERR_REQUEST
,
75 /** The user cancelled (e.g. in a dialog) */
76 wxWEB_NAV_ERR_USER_CANCELLED
,
77 /** Another (exotic) type of error that didn't fit in other categories*/
81 /** Type of refresh */
82 enum wxWebViewReloadFlags
84 /** Default reload, will access cache */
85 wxWEB_VIEW_RELOAD_DEFAULT
,
86 /** Reload the current view without accessing the cache */
87 wxWEB_VIEW_RELOAD_NO_CACHE
92 * List of available backends for wxWebView
96 /** Value that may be passed to wxWebView to let it pick an appropriate
97 * engine for the current platform*/
98 wxWEB_VIEW_BACKEND_DEFAULT
,
100 /** The OSX-native WebKit web engine */
101 wxWEB_VIEW_BACKEND_OSX_WEBKIT
,
103 /** The GTK port of the WebKit engine */
104 wxWEB_VIEW_BACKEND_GTK_WEBKIT
,
106 /** Use Microsoft Internet Explorer as web engine */
107 wxWEB_VIEW_BACKEND_IE
110 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr
[];
111 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr
[];
113 class WXDLLIMPEXP_WEB wxWebView
: public wxControl
118 * Creation function for two-step creation.
120 virtual bool Create(wxWindow
* parent
,
126 const wxString
& name
) = 0;
129 * Factory function to create a new wxWebView for two-step creation
130 * (you need to call wxWebView::Create on the returned object)
131 * @param backend which web engine to use as backend for wxWebView
132 * @return the created wxWebView, or NULL if the requested backend is
135 static wxWebView
* New(wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
);
137 // TODO: clarify what styles can do, or remove this flag
139 * Factory function to create a new wxWebView
140 * @param parent parent window to create this view in
141 * @param id ID of this control
142 * @param url URL to load by default in the web view
143 * @param pos position to create this control at
144 * (you may use wxDefaultPosition if you use sizers)
145 * @param size size to create this control with
146 * (you may use wxDefaultSize if you use sizers)
147 * @param backend which web engine to use as backend for wxWebView
148 * @return the created wxWebView, or NULL if the requested backend
151 static wxWebView
* New(wxWindow
* parent
,
153 const wxString
& url
= wxWebViewDefaultURLStr
,
154 const wxPoint
& pos
= wxDefaultPosition
,
155 const wxSize
& size
= wxDefaultSize
,
156 wxWebViewBackend backend
= wxWEB_VIEW_BACKEND_DEFAULT
,
158 const wxString
& name
= wxWebViewNameStr
);
161 /** Get whether it is possible to navigate back in the history of
164 virtual bool CanGoBack() = 0;
166 /** Get whether it is possible to navigate forward in the history of
169 virtual bool CanGoForward() = 0;
171 /** Navigate back in the history of visited pages.
172 * Only valid if CanGoBack() returned true.
174 virtual void GoBack() = 0;
176 /** Navigate forwardin the history of visited pages.
177 * Only valid if CanGoForward() returned true.
179 virtual void GoForward() = 0;
182 * Load a HTMl document (web page) from a URL
183 * @param url the URL where the HTML document to display can be found
184 * @note web engines generally report errors asynchronously, so if you wish
185 * to know whether loading the URL was successful, register to receive
186 * navigation error events
188 virtual void LoadUrl(const wxString
& url
) = 0;
190 virtual void ClearHistory() = 0;
191 virtual void EnableHistory(bool enable
= true) = 0;
192 virtual wxVector
<wxSharedPtr
<wxWebHistoryItem
> > GetBackwardHistory() = 0;
193 virtual wxVector
<wxSharedPtr
<wxWebHistoryItem
> > GetForwardHistory() = 0;
194 virtual void LoadHistoryItem(wxSharedPtr
<wxWebHistoryItem
> item
) = 0;
197 * Stop the current page loading process, if any.
198 * May trigger an error event of type wxWEB_NAV_ERR_USER_CANCELLED.
199 * TODO: make wxWEB_NAV_ERR_USER_CANCELLED errors uniform across ports.
201 virtual void Stop() = 0;
204 * Reload the currently displayed URL.
205 * @param flags A bit array that may optionnally contain reload options
207 virtual void Reload(wxWebViewReloadFlags flags
= wxWEB_VIEW_RELOAD_DEFAULT
) = 0;
211 * Get the URL of the currently displayed document
213 virtual wxString
GetCurrentURL() = 0;
216 * Get the title of the current web page, or its URL/path if title is not
219 virtual wxString
GetCurrentTitle() = 0;
221 // TODO: handle choosing a frame when calling GetPageSource()?
223 * Get the HTML source code of the currently displayed document
224 * @return the HTML source code, or an empty string if no page is currently
227 virtual wxString
GetPageSource() = 0;
230 * Get the zoom factor of the page
231 * @return How much the HTML document is zoomed (scaleed)
233 virtual wxWebViewZoom
GetZoom() = 0;
236 * Set the zoom factor of the page
237 * @param zoom How much to zoom (scale) the HTML document
239 virtual void SetZoom(wxWebViewZoom zoom
) = 0;
242 * Set how to interpret the zoom factor
243 * @param zoomType how the zoom factor should be interpreted by the
245 * @note invoke canSetZoomType() first, some HTML renderers may not
246 * support all zoom types
248 virtual void SetZoomType(wxWebViewZoomType zoomType
) = 0;
251 * Get how the zoom factor is currently interpreted
252 * @return how the zoom factor is currently interpreted by the HTML engine
254 virtual wxWebViewZoomType
GetZoomType() const = 0;
257 * Retrieve whether the current HTML engine supports a type of zoom
258 * @param type the type of zoom to test
259 * @return whether this type of zoom is supported by this HTML engine
260 * (and thus can be set through setZoomType())
262 virtual bool CanSetZoomType(wxWebViewZoomType type
) const = 0;
264 // TODO: allow 'SetPage' to find files (e.g. images) from a virtual file
265 // system if possible
267 * Set the displayed page source to the contents of the given string
268 * @param html the string that contains the HTML data to display
269 * @param baseUrl URL assigned to the HTML data, to be used to resolve
270 * relative paths, for instance
272 virtual void SetPage(const wxString
& html
, const wxString
& baseUrl
) = 0;
275 * Set the displayed page source to the contents of the given stream
276 * @param html the stream to read HTML data from
277 * @param baseUrl URL assigned to the HTML data, to be used to resolve
278 * relative paths, for instance
280 virtual void SetPage(wxInputStream
& html
, wxString baseUrl
)
282 wxStringOutputStream stream
;
284 SetPage(stream
.GetString(), baseUrl
);
288 // wxString GetSelection(); // maybe?
289 // void SetSelection(...); // maybe?
291 // void MakeEditable(bool enable = true); // maybe?
292 // bool IsEditable(); // maybe?
294 // void EnableJavascript(bool enabled); // maybe?
295 // wxString RunScript(const wxString& javascript); // maybe?
297 // void SetScrollPos(int pos); // maybe?
298 // int GetScrollPos(); // maybe?
300 // wxString GetStatusText(); // maybe?
301 // void SetStatusText(wxString text); // maybe?
302 // * status text changed event?
303 // * title changed event?
305 // virtual bool IsOfflineMode() = 0; // maybe?
306 // virtual void SetOfflineMode(bool offline) = 0; // maybe?
308 // TODO: offer API to control the opening of new frames
309 // (through <a target="..."> as well as through javascript), OR
310 // provide a behavior consistent across ports.
311 // - OSX : I receive an event for new frames opened with HTML target, and
312 // currently block them all.
313 // - IE : An event is recieved for new frames and they are currently
315 // - GTK : All frame open requests are blocked. A slot exists that I could
316 // connect to to be notified if ever needed
319 * Opens a print dialog so that the user may print the currently
322 virtual void Print() = 0;
325 * Returns whether the web control is currently busy (e.g. loading a page)
327 virtual bool IsBusy() = 0;
329 //Clipboard functions
330 virtual bool CanCut() = 0;
331 virtual bool CanCopy() = 0;
332 virtual bool CanPaste() = 0;
333 virtual void Cut() = 0;
334 virtual void Copy() = 0;
335 virtual void Paste() = 0;
337 //Undo / redo functionality
338 virtual bool CanUndo() = 0;
339 virtual bool CanRedo() = 0;
340 virtual void Undo() = 0;
341 virtual void Redo() = 0;
344 class WXDLLIMPEXP_WEB wxWebNavigationEvent
: public wxCommandEvent
347 wxWebNavigationEvent() {}
348 wxWebNavigationEvent(wxEventType type
, int id
, const wxString href
,
349 const wxString target
, bool canVeto
)
350 : wxCommandEvent(type
, id
)
359 * Get the URL being visited
361 const wxString
& GetHref() const { return m_href
; }
364 * Get the target (frame or window) in which the URL that caused this event
365 * is viewed, or an empty string if not available.
367 const wxString
& GetTarget() const { return m_target
; }
369 // default copy ctor, assignment operator and dtor are ok
370 virtual wxEvent
* Clone() const { return new wxWebNavigationEvent(*this); }
372 /** Get whether this event may be vetoed (stopped/prevented). Only
373 * meaningful for events fired before navigation takes place.
375 bool CanVeto() const { return m_canVeto
; }
377 /** Whether this event was vetoed (stopped/prevented). Only meaningful for
378 * events fired before navigation takes place.
380 bool IsVetoed() const { return m_vetoed
; }
382 /** Veto (prevent/stop) this event. Only meaningful for events fired
383 * before navigation takes place. Only valid if CanVeto() returned true.
385 void Veto() { wxASSERT(m_canVeto
); m_vetoed
= true; }
393 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebNavigationEvent
);
396 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATING
, wxWebNavigationEvent
);
397 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NAVIGATED
, wxWebNavigationEvent
);
398 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_LOADED
, wxWebNavigationEvent
);
399 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_ERROR
, wxWebNavigationEvent
);
400 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB
, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
, wxWebNavigationEvent
);
402 typedef void (wxEvtHandler::*wxWebNavigationEventFunction
)
403 (wxWebNavigationEvent
&);
405 #define wxWebNavigationEventHandler(func) \
406 wxEVENT_HANDLER_CAST(wxWebNavigationEventFunction, func)
408 #define EVT_WEB_VIEW_NAVIGATING(id, fn) \
409 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
410 wxHtmlNavigatingEventHandler(fn))
412 #define EVT_WEB_VIEW_NAVIGATED(id, fn) \
413 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
414 wxHtmlNavigatingEventHandler(fn))
416 #define EVT_WEB_VIEW_LOADED(id, fn) \
417 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
418 wxHtmlNavigatingEventHandler(fn))
420 #define EVT_WEB_VIEW_ERRROR(id, fn) \
421 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
422 wxHtmlNavigatingEventHandler(fn))
424 #define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
425 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
426 wxHtmlNavigatingEventHandler(fn))
430 #endif // _WX_WEB_VIEW_H_