Const correct the wxWebView api.
[wxWidgets.git] / include / wx / webview.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webview.h
3 // Purpose: Common interface and events for web view component
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_WEB_VIEW_H_
11 #define _WX_WEB_VIEW_H_
12
13 #include "wx/setup.h"
14
15 #if wxUSE_WEB
16
17 #include "wx/control.h"
18 #include "wx/event.h"
19 #include "wx/sstream.h"
20 #include "wx/sharedptr.h"
21 #include "wx/vector.h"
22
23 #include "wx/osx/webviewhistoryitem_webkit.h"
24 #include "wx/gtk/webviewhistoryitem_webkit.h"
25 #include "wx/msw/webviewhistoryitem_ie.h"
26
27 class wxFSFile;
28 class wxFileSystem;
29
30
31 /**
32 * Zoom level in web view component
33 */
34 enum wxWebViewZoom
35 {
36 wxWEB_VIEW_ZOOM_TINY,
37 wxWEB_VIEW_ZOOM_SMALL,
38 wxWEB_VIEW_ZOOM_MEDIUM,
39 wxWEB_VIEW_ZOOM_LARGE,
40 wxWEB_VIEW_ZOOM_LARGEST
41 };
42
43 /**
44 * The type of zooming that the web view control can perform
45 */
46 enum wxWebViewZoomType
47 {
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
53 };
54
55 /** Types of errors that can cause navigation to fail */
56 enum wxWebViewNavigationError
57 {
58 /** Connection error (timeout, etc.) */
59 wxWEB_NAV_ERR_CONNECTION,
60 /** Invalid certificate */
61 wxWEB_NAV_ERR_CERTIFICATE,
62 /** Authentication required */
63 wxWEB_NAV_ERR_AUTH,
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*/
74 wxWEB_NAV_ERR_OTHER
75 };
76
77 /** Type of refresh */
78 enum wxWebViewReloadFlags
79 {
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
84 };
85
86
87 /**
88 * List of available backends for wxWebView
89 */
90 enum wxWebViewBackend
91 {
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,
95
96 /** The WebKit web engine */
97 wxWEB_VIEW_BACKEND_WEBKIT,
98
99 /** Use Microsoft Internet Explorer as web engine */
100 wxWEB_VIEW_BACKEND_IE
101 };
102
103 //Base class for custom scheme handlers
104 class WXDLLIMPEXP_WEB wxWebViewHandler
105 {
106 public:
107 wxWebViewHandler(const wxString& scheme) : m_scheme(scheme) {}
108 virtual wxString GetName() const { return m_scheme; }
109 virtual wxFSFile* GetFile(const wxString &uri) = 0;
110 private:
111 wxString m_scheme;
112 };
113
114 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr[];
115 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr[];
116
117 class WXDLLIMPEXP_WEB wxWebView : public wxControl
118 {
119 public:
120
121 /**
122 * Creation function for two-step creation.
123 */
124 virtual bool Create(wxWindow* parent,
125 wxWindowID id,
126 const wxString& url,
127 const wxPoint& pos,
128 const wxSize& size,
129 long style,
130 const wxString& name) = 0;
131
132 /**
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
137 * not available
138 */
139 static wxWebView* New(wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT);
140
141 // TODO: clarify what styles can do, or remove this flag
142 /**
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
153 * is not available
154 */
155 static wxWebView* New(wxWindow* parent,
156 wxWindowID id,
157 const wxString& url = wxWebViewDefaultURLStr,
158 const wxPoint& pos = wxDefaultPosition,
159 const wxSize& size = wxDefaultSize,
160 wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT,
161 long style = 0,
162 const wxString& name = wxWebViewNameStr);
163
164
165 /** Get whether it is possible to navigate back in the history of
166 * visited pages
167 */
168 virtual bool CanGoBack() const = 0;
169
170 /** Get whether it is possible to navigate forward in the history of
171 * visited pages
172 */
173 virtual bool CanGoForward() const = 0;
174
175 /** Navigate back in the history of visited pages.
176 * Only valid if CanGoBack() returned true.
177 */
178 virtual void GoBack() = 0;
179
180 /** Navigate forwardin the history of visited pages.
181 * Only valid if CanGoForward() returned true.
182 */
183 virtual void GoForward() = 0;
184
185 /**
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
191 */
192 virtual void LoadUrl(const wxString& url) = 0;
193
194 virtual void ClearHistory() = 0;
195 virtual void EnableHistory(bool enable = true) = 0;
196 virtual wxVector<wxSharedPtr<wxWebViewHistoryItem> > GetBackwardHistory() = 0;
197 virtual wxVector<wxSharedPtr<wxWebViewHistoryItem> > GetForwardHistory() = 0;
198 virtual void LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item) = 0;
199
200 /**
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.
204 */
205 virtual void Stop() = 0;
206
207 /**
208 * Reload the currently displayed URL.
209 * @param flags A bit array that may optionnally contain reload options
210 */
211 virtual void Reload(wxWebViewReloadFlags flags = wxWEB_VIEW_RELOAD_DEFAULT) = 0;
212
213
214 /**
215 * Get the URL of the currently displayed document
216 */
217 virtual wxString GetCurrentURL() const = 0;
218
219 /**
220 * Get the title of the current web page, or its URL/path if title is not
221 * available
222 */
223 virtual wxString GetCurrentTitle() const = 0;
224
225 // TODO: handle choosing a frame when calling GetPageSource()?
226 /**
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
229 * shown
230 */
231 virtual wxString GetPageSource() const = 0;
232 virtual wxString GetPageText() const = 0;
233
234 /**
235 * Get the zoom factor of the page
236 * @return How much the HTML document is zoomed (scaleed)
237 */
238 virtual wxWebViewZoom GetZoom() const = 0;
239
240 /**
241 * Set the zoom factor of the page
242 * @param zoom How much to zoom (scale) the HTML document
243 */
244 virtual void SetZoom(wxWebViewZoom zoom) = 0;
245
246 /**
247 * Set how to interpret the zoom factor
248 * @param zoomType how the zoom factor should be interpreted by the
249 * HTML engine
250 * @note invoke canSetZoomType() first, some HTML renderers may not
251 * support all zoom types
252 */
253 virtual void SetZoomType(wxWebViewZoomType zoomType) = 0;
254
255 /**
256 * Get how the zoom factor is currently interpreted
257 * @return how the zoom factor is currently interpreted by the HTML engine
258 */
259 virtual wxWebViewZoomType GetZoomType() const = 0;
260
261 /**
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())
266 */
267 virtual bool CanSetZoomType(wxWebViewZoomType type) const = 0;
268
269 // TODO: allow 'SetPage' to find files (e.g. images) from a virtual file
270 // system if possible
271 /**
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
276 */
277 virtual void SetPage(const wxString& html, const wxString& baseUrl) = 0;
278
279 /**
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
284 */
285 virtual void SetPage(wxInputStream& html, wxString baseUrl)
286 {
287 wxStringOutputStream stream;
288 stream.Write(html);
289 SetPage(stream.GetString(), baseUrl);
290 }
291
292 virtual void SetEditable(bool enable = true) = 0;
293 virtual bool IsEditable() const = 0;
294
295 virtual void SelectAll() = 0;
296 virtual bool HasSelection() const = 0;
297 virtual void DeleteSelection() = 0;
298 virtual wxString GetSelectedText() const = 0;
299 virtual wxString GetSelectedSource() const = 0;
300 virtual void ClearSelection() = 0;
301
302 virtual void RunScript(const wxString& javascript) = 0;
303
304 // TODO:
305 // void EnableJavascript(bool enabled); // maybe?
306 // // maybe?
307
308 // void SetScrollPos(int pos); // maybe?
309 // int GetScrollPos(); // maybe?
310
311 // wxString GetStatusText(); // maybe?
312 // void SetStatusText(wxString text); // maybe?
313 // * status text changed event?
314 // * title changed event?
315
316 // virtual bool IsOfflineMode() = 0; // maybe?
317 // virtual void SetOfflineMode(bool offline) = 0; // maybe?
318
319 /**
320 * Opens a print dialog so that the user may print the currently
321 * displayed page.
322 */
323 virtual void Print() = 0;
324
325 /**
326 * Returns whether the web control is currently busy (e.g. loading a page)
327 */
328 virtual bool IsBusy() const = 0;
329
330 //Clipboard functions
331 virtual bool CanCut() const = 0;
332 virtual bool CanCopy() const = 0;
333 virtual bool CanPaste() const = 0;
334 virtual void Cut() = 0;
335 virtual void Copy() = 0;
336 virtual void Paste() = 0;
337
338 //Undo / redo functionality
339 virtual bool CanUndo() const = 0;
340 virtual bool CanRedo() const = 0;
341 virtual void Undo() = 0;
342 virtual void Redo() = 0;
343
344 //Virtual Filesystem Support
345 virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) = 0;
346
347 wxDECLARE_ABSTRACT_CLASS(wxWebView);
348 };
349
350 class WXDLLIMPEXP_WEB wxWebViewEvent : public wxCommandEvent
351 {
352 public:
353 wxWebViewEvent() {}
354 wxWebViewEvent(wxEventType type, int id, const wxString url,
355 const wxString target, bool canVeto)
356 : wxCommandEvent(type, id)
357 {
358 m_url = url;
359 m_target = target;
360 m_vetoed = false;
361 m_canVeto = canVeto;
362 }
363
364 /**
365 * Get the URL being visited
366 */
367 const wxString& GetURL() const { return m_url; }
368
369 /**
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.
372 */
373 const wxString& GetTarget() const { return m_target; }
374
375 // default copy ctor, assignment operator and dtor are ok
376 virtual wxEvent* Clone() const { return new wxWebViewEvent(*this); }
377
378 /** Get whether this event may be vetoed (stopped/prevented). Only
379 * meaningful for events fired before navigation takes place.
380 */
381 bool CanVeto() const { return m_canVeto; }
382
383 /** Whether this event was vetoed (stopped/prevented). Only meaningful for
384 * events fired before navigation takes place.
385 */
386 bool IsVetoed() const { return m_vetoed; }
387
388 /** Veto (prevent/stop) this event. Only meaningful for events fired
389 * before navigation takes place. Only valid if CanVeto() returned true.
390 */
391 void Veto() { wxASSERT(m_canVeto); m_vetoed = true; }
392
393 private:
394 wxString m_url;
395 wxString m_target;
396 bool m_canVeto;
397 bool m_vetoed;
398
399 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebViewEvent);
400 };
401
402 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATING, wxWebViewEvent );
403 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATED, wxWebViewEvent );
404 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_LOADED, wxWebViewEvent );
405 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_ERROR, wxWebViewEvent );
406 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, wxWebViewEvent );
407 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, wxWebViewEvent );
408
409 typedef void (wxEvtHandler::*wxWebViewEventFunction)
410 (wxWebViewEvent&);
411
412 #define wxWebViewEventHandler(func) \
413 wxEVENT_HANDLER_CAST(wxWebViewEventFunction, func)
414
415 #define EVT_WEB_VIEW_NAVIGATING(id, fn) \
416 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
417 wxWebViewEventHandler(fn))
418
419 #define EVT_WEB_VIEW_NAVIGATED(id, fn) \
420 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
421 wxWebViewEventHandler(fn))
422
423 #define EVT_WEB_VIEW_LOADED(id, fn) \
424 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
425 wxWebViewEventHandler(fn))
426
427 #define EVT_WEB_VIEW_ERRROR(id, fn) \
428 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
429 wxWebViewEventHandler(fn))
430
431 #define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
432 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
433 wxWebViewEventHandler(fn))
434
435 #define EVT_WEB_VIEW_TITLE_CHANGED(id, fn) \
436 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, id, \
437 wxWebViewEventHandler(fn))
438
439 #endif // wxUSE_WEB
440
441 #endif // _WX_WEB_VIEW_H_