]> git.saurik.com Git - wxWidgets.git/blame - include/wx/webview.h
Initial work on virtual file system support for the WebKitGTK+ backend. It now suppor...
[wxWidgets.git] / include / wx / webview.h
CommitLineData
61b98a2d
SL
1/////////////////////////////////////////////////////////////////////////////
2// Name: webview.h
3// Purpose: Common interface and events for web view component
4// Author: Marianne Gagnon
5// Id: $Id$
153530af 6// Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton
61b98a2d
SL
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifndef _WX_WEB_VIEW_H_
11#define _WX_WEB_VIEW_H_
12
726cc869 13#include "wx/setup.h"
9c805dec
SL
14
15#if wxUSE_WEB
16
61b98a2d
SL
17#include <wx/control.h>
18#include <wx/event.h>
19#include <wx/sstream.h>
5cbda74b 20#include "wx/sharedptr.h"
ae26e17b 21#include "wx/vector.h"
5cbda74b 22
eafdb19c
SL
23#include "wx/osx/webhistoryitem_webkit.h"
24#include "wx/gtk/webhistoryitem_webkit.h"
25#include "wx/msw/webhistoryitem_ie.h"
26
29365629 27class wxFSFile;
eff8f795 28class wxFileSystem;
29365629 29
61b98a2d
SL
30
31/**
32 * Zoom level in web view component
33 */
34enum 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 */
46enum 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 */
56enum wxWebNavigationError
57{
58 /** Connection error (timeout, etc.) */
70554c32 59 wxWEB_NAV_ERR_CONNECTION,
61b98a2d 60 /** Invalid certificate */
70554c32 61 wxWEB_NAV_ERR_CERTIFICATE,
61b98a2d 62 /** Authentication required */
70554c32 63 wxWEB_NAV_ERR_AUTH,
61b98a2d 64 /** Other security error */
70554c32 65 wxWEB_NAV_ERR_SECURITY,
61b98a2d 66 /** Requested resource not found */
70554c32 67 wxWEB_NAV_ERR_NOT_FOUND,
61b98a2d
SL
68 /** Invalid request/parameters (e.g. bad URL, bad protocol,
69 * unsupported resource type) */
70554c32 70 wxWEB_NAV_ERR_REQUEST,
61b98a2d 71 /** The user cancelled (e.g. in a dialog) */
70554c32 72 wxWEB_NAV_ERR_USER_CANCELLED,
61b98a2d 73 /** Another (exotic) type of error that didn't fit in other categories*/
70554c32 74 wxWEB_NAV_ERR_OTHER
61b98a2d
SL
75};
76
77/** Type of refresh */
78enum wxWebViewReloadFlags
79{
384b8d9f 80 /** Default reload, will access cache */
70554c32 81 wxWEB_VIEW_RELOAD_DEFAULT,
384b8d9f 82 /** Reload the current view without accessing the cache */
a703012a 83 wxWEB_VIEW_RELOAD_NO_CACHE
61b98a2d
SL
84};
85
86
87/**
88 * List of available backends for wxWebView
89 */
90enum 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
9df97be2
SL
96 /** The WebKit web engine */
97 wxWEB_VIEW_BACKEND_WEBKIT,
61b98a2d
SL
98
99 /** Use Microsoft Internet Explorer as web engine */
100 wxWEB_VIEW_BACKEND_IE
101};
102
9e3d4a32
SL
103//Base class for custom scheme handlers
104class WXDLLIMPEXP_WEB wxWebHandler
29365629
SL
105{
106public:
9e3d4a32 107 virtual wxString GetName() const = 0;
29365629
SL
108 virtual wxFSFile* GetFile(const wxString &uri) = 0;
109 virtual wxString CombineURIs(const wxString &baseuri, const wxString &newuri) = 0;
110};
111
3046dbdc
SL
112extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr[];
113extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr[];
61b98a2d 114
3046dbdc 115class WXDLLIMPEXP_WEB wxWebView : public wxControl
61b98a2d
SL
116{
117public:
118
119 /**
120 * Creation function for two-step creation.
121 */
122 virtual bool Create(wxWindow* parent,
123 wxWindowID id,
124 const wxString& url,
125 const wxPoint& pos,
126 const wxSize& size,
127 long style,
128 const wxString& name) = 0;
129
130 /**
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
135 * not available
136 */
137 static wxWebView* New(wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT);
138
139 // TODO: clarify what styles can do, or remove this flag
140 /**
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
151 * is not available
152 */
153 static wxWebView* New(wxWindow* parent,
154 wxWindowID id,
155 const wxString& url = wxWebViewDefaultURLStr,
156 const wxPoint& pos = wxDefaultPosition,
157 const wxSize& size = wxDefaultSize,
158 wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT,
159 long style = 0,
160 const wxString& name = wxWebViewNameStr);
161
162
163 /** Get whether it is possible to navigate back in the history of
164 * visited pages
165 */
166 virtual bool CanGoBack() = 0;
167
168 /** Get whether it is possible to navigate forward in the history of
169 * visited pages
170 */
171 virtual bool CanGoForward() = 0;
172
173 /** Navigate back in the history of visited pages.
174 * Only valid if CanGoBack() returned true.
175 */
176 virtual void GoBack() = 0;
177
178 /** Navigate forwardin the history of visited pages.
179 * Only valid if CanGoForward() returned true.
180 */
181 virtual void GoForward() = 0;
182
183 /**
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
189 */
190 virtual void LoadUrl(const wxString& url) = 0;
191
152a5808
SL
192 virtual void ClearHistory() = 0;
193 virtual void EnableHistory(bool enable = true) = 0;
5cbda74b
SL
194 virtual wxVector<wxSharedPtr<wxWebHistoryItem> > GetBackwardHistory() = 0;
195 virtual wxVector<wxSharedPtr<wxWebHistoryItem> > GetForwardHistory() = 0;
196 virtual void LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item) = 0;
152a5808 197
61b98a2d
SL
198 /**
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.
202 */
203 virtual void Stop() = 0;
204
205 /**
206 * Reload the currently displayed URL.
207 * @param flags A bit array that may optionnally contain reload options
208 */
384b8d9f 209 virtual void Reload(wxWebViewReloadFlags flags = wxWEB_VIEW_RELOAD_DEFAULT) = 0;
61b98a2d
SL
210
211
212 /**
213 * Get the URL of the currently displayed document
214 */
215 virtual wxString GetCurrentURL() = 0;
216
217 /**
218 * Get the title of the current web page, or its URL/path if title is not
219 * available
220 */
221 virtual wxString GetCurrentTitle() = 0;
222
223 // TODO: handle choosing a frame when calling GetPageSource()?
224 /**
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
227 * shown
228 */
229 virtual wxString GetPageSource() = 0;
241b769f 230 virtual wxString GetPageText() = 0;
61b98a2d
SL
231
232 /**
233 * Get the zoom factor of the page
234 * @return How much the HTML document is zoomed (scaleed)
235 */
236 virtual wxWebViewZoom GetZoom() = 0;
237
238 /**
239 * Set the zoom factor of the page
240 * @param zoom How much to zoom (scale) the HTML document
241 */
242 virtual void SetZoom(wxWebViewZoom zoom) = 0;
243
244 /**
245 * Set how to interpret the zoom factor
246 * @param zoomType how the zoom factor should be interpreted by the
247 * HTML engine
248 * @note invoke canSetZoomType() first, some HTML renderers may not
249 * support all zoom types
250 */
251 virtual void SetZoomType(wxWebViewZoomType zoomType) = 0;
252
253 /**
254 * Get how the zoom factor is currently interpreted
255 * @return how the zoom factor is currently interpreted by the HTML engine
256 */
257 virtual wxWebViewZoomType GetZoomType() const = 0;
258
259 /**
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())
264 */
265 virtual bool CanSetZoomType(wxWebViewZoomType type) const = 0;
266
267 // TODO: allow 'SetPage' to find files (e.g. images) from a virtual file
268 // system if possible
269 /**
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
274 */
275 virtual void SetPage(const wxString& html, const wxString& baseUrl) = 0;
276
277 /**
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
282 */
283 virtual void SetPage(wxInputStream& html, wxString baseUrl)
284 {
285 wxStringOutputStream stream;
286 stream.Write(html);
287 SetPage(stream.GetString(), baseUrl);
288 }
289
c7cbe308
SL
290 virtual void SetEditable(bool enable = true) = 0;
291 virtual bool IsEditable() = 0;
292
63a65070
SL
293 virtual void SelectAll() = 0;
294 virtual bool HasSelection() = 0;
295 virtual void DeleteSelection() = 0;
c9355a3d 296 virtual wxString GetSelectedText() = 0;
97ba4d81 297 virtual wxString GetSelectedSource() = 0;
41933aa5 298 virtual void ClearSelection() = 0;
63a65070 299
c9ccc09c
SL
300 virtual void RunScript(const wxString& javascript) = 0;
301
61b98a2d 302 // TODO:
61b98a2d 303 // void EnableJavascript(bool enabled); // maybe?
c9ccc09c 304 // // maybe?
61b98a2d
SL
305
306 // void SetScrollPos(int pos); // maybe?
307 // int GetScrollPos(); // maybe?
308
309 // wxString GetStatusText(); // maybe?
310 // void SetStatusText(wxString text); // maybe?
311 // * status text changed event?
312 // * title changed event?
313
314 // virtual bool IsOfflineMode() = 0; // maybe?
315 // virtual void SetOfflineMode(bool offline) = 0; // maybe?
316
61b98a2d
SL
317 /**
318 * Opens a print dialog so that the user may print the currently
319 * displayed page.
320 */
321 virtual void Print() = 0;
322
323 /**
324 * Returns whether the web control is currently busy (e.g. loading a page)
325 */
326 virtual bool IsBusy() = 0;
4681a3ea
SL
327
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;
97e49559
SL
335
336 //Undo / redo functionality
337 virtual bool CanUndo() = 0;
338 virtual bool CanRedo() = 0;
339 virtual void Undo() = 0;
340 virtual void Redo() = 0;
eff8f795
SL
341
342 //Virtual Filesystem Support
9e3d4a32 343 virtual void RegisterHandler(wxWebHandler* handler) = 0;
cddf4541
SL
344
345 wxDECLARE_ABSTRACT_CLASS(wxWebView);
61b98a2d
SL
346};
347
97ad1425 348class WXDLLIMPEXP_WEB wxWebNavigationEvent : public wxCommandEvent
61b98a2d
SL
349{
350public:
351 wxWebNavigationEvent() {}
e40741b9 352 wxWebNavigationEvent(wxEventType type, int id, const wxString url,
61b98a2d
SL
353 const wxString target, bool canVeto)
354 : wxCommandEvent(type, id)
355 {
e40741b9 356 m_url = url;
61b98a2d
SL
357 m_target = target;
358 m_vetoed = false;
359 m_canVeto = canVeto;
360 }
361
362 /**
363 * Get the URL being visited
364 */
e40741b9 365 const wxString& GetURL() const { return m_url; }
61b98a2d
SL
366
367 /**
368 * Get the target (frame or window) in which the URL that caused this event
369 * is viewed, or an empty string if not available.
370 */
371 const wxString& GetTarget() const { return m_target; }
372
373 // default copy ctor, assignment operator and dtor are ok
374 virtual wxEvent* Clone() const { return new wxWebNavigationEvent(*this); }
375
376 /** Get whether this event may be vetoed (stopped/prevented). Only
377 * meaningful for events fired before navigation takes place.
378 */
379 bool CanVeto() const { return m_canVeto; }
380
381 /** Whether this event was vetoed (stopped/prevented). Only meaningful for
382 * events fired before navigation takes place.
383 */
384 bool IsVetoed() const { return m_vetoed; }
385
386 /** Veto (prevent/stop) this event. Only meaningful for events fired
387 * before navigation takes place. Only valid if CanVeto() returned true.
388 */
389 void Veto() { wxASSERT(m_canVeto); m_vetoed = true; }
390
391private:
e40741b9 392 wxString m_url;
61b98a2d
SL
393 wxString m_target;
394 bool m_canVeto;
395 bool m_vetoed;
396
397 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebNavigationEvent);
398};
399
9c805dec
SL
400wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATING, wxWebNavigationEvent );
401wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATED, wxWebNavigationEvent );
402wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_LOADED, wxWebNavigationEvent );
403wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_ERROR, wxWebNavigationEvent );
853b6cd0 404wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, wxWebNavigationEvent );
153530af 405wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, wxWebNavigationEvent );
61b98a2d
SL
406
407typedef void (wxEvtHandler::*wxWebNavigationEventFunction)
408 (wxWebNavigationEvent&);
409
410#define wxWebNavigationEventHandler(func) \
411 wxEVENT_HANDLER_CAST(wxWebNavigationEventFunction, func)
412
413#define EVT_WEB_VIEW_NAVIGATING(id, fn) \
384b8d9f 414 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
61b98a2d
SL
415 wxHtmlNavigatingEventHandler(fn))
416
417#define EVT_WEB_VIEW_NAVIGATED(id, fn) \
384b8d9f 418 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
61b98a2d
SL
419 wxHtmlNavigatingEventHandler(fn))
420
421#define EVT_WEB_VIEW_LOADED(id, fn) \
384b8d9f 422 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
61b98a2d
SL
423 wxHtmlNavigatingEventHandler(fn))
424
425#define EVT_WEB_VIEW_ERRROR(id, fn) \
384b8d9f 426 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
61b98a2d
SL
427 wxHtmlNavigatingEventHandler(fn))
428
853b6cd0
SL
429#define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
430 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
431 wxHtmlNavigatingEventHandler(fn))
432
153530af
SL
433#define EVT_WEB_VIEW_TITLE_CHANGED(id, fn) \
434 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, id, \
435 wxHtmlNavigatingEventHandler(fn))
436
9c805dec
SL
437#endif // wxUSE_WEB
438
439#endif // _WX_WEB_VIEW_H_