Rename GetHref to GetURL in wxWebNavigationEvent, this brings it into line with the...
[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
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 class WXDLLIMPEXP_WEB wxWebHistoryItem
24 {
25 public:
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; }
30
31 private:
32 wxString m_url, m_title;
33 };
34
35 /**
36 * Zoom level in web view component
37 */
38 enum wxWebViewZoom
39 {
40 wxWEB_VIEW_ZOOM_TINY,
41 wxWEB_VIEW_ZOOM_SMALL,
42 wxWEB_VIEW_ZOOM_MEDIUM,
43 wxWEB_VIEW_ZOOM_LARGE,
44 wxWEB_VIEW_ZOOM_LARGEST
45 };
46
47 /**
48 * The type of zooming that the web view control can perform
49 */
50 enum wxWebViewZoomType
51 {
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
57 };
58
59 /** Types of errors that can cause navigation to fail */
60 enum wxWebNavigationError
61 {
62 /** Connection error (timeout, etc.) */
63 wxWEB_NAV_ERR_CONNECTION,
64 /** Invalid certificate */
65 wxWEB_NAV_ERR_CERTIFICATE,
66 /** Authentication required */
67 wxWEB_NAV_ERR_AUTH,
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*/
78 wxWEB_NAV_ERR_OTHER
79 };
80
81 /** Type of refresh */
82 enum wxWebViewReloadFlags
83 {
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
88 };
89
90
91 /**
92 * List of available backends for wxWebView
93 */
94 enum wxWebViewBackend
95 {
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,
99
100 /** The WebKit web engine */
101 wxWEB_VIEW_BACKEND_WEBKIT,
102
103 /** Use Microsoft Internet Explorer as web engine */
104 wxWEB_VIEW_BACKEND_IE
105 };
106
107 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr[];
108 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr[];
109
110 class WXDLLIMPEXP_WEB wxWebView : public wxControl
111 {
112 public:
113
114 /**
115 * Creation function for two-step creation.
116 */
117 virtual bool Create(wxWindow* parent,
118 wxWindowID id,
119 const wxString& url,
120 const wxPoint& pos,
121 const wxSize& size,
122 long style,
123 const wxString& name) = 0;
124
125 /**
126 * Factory function to create a new wxWebView for two-step creation
127 * (you need to call wxWebView::Create on the returned object)
128 * @param backend which web engine to use as backend for wxWebView
129 * @return the created wxWebView, or NULL if the requested backend is
130 * not available
131 */
132 static wxWebView* New(wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT);
133
134 // TODO: clarify what styles can do, or remove this flag
135 /**
136 * Factory function to create a new wxWebView
137 * @param parent parent window to create this view in
138 * @param id ID of this control
139 * @param url URL to load by default in the web view
140 * @param pos position to create this control at
141 * (you may use wxDefaultPosition if you use sizers)
142 * @param size size to create this control with
143 * (you may use wxDefaultSize if you use sizers)
144 * @param backend which web engine to use as backend for wxWebView
145 * @return the created wxWebView, or NULL if the requested backend
146 * is not available
147 */
148 static wxWebView* New(wxWindow* parent,
149 wxWindowID id,
150 const wxString& url = wxWebViewDefaultURLStr,
151 const wxPoint& pos = wxDefaultPosition,
152 const wxSize& size = wxDefaultSize,
153 wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT,
154 long style = 0,
155 const wxString& name = wxWebViewNameStr);
156
157
158 /** Get whether it is possible to navigate back in the history of
159 * visited pages
160 */
161 virtual bool CanGoBack() = 0;
162
163 /** Get whether it is possible to navigate forward in the history of
164 * visited pages
165 */
166 virtual bool CanGoForward() = 0;
167
168 /** Navigate back in the history of visited pages.
169 * Only valid if CanGoBack() returned true.
170 */
171 virtual void GoBack() = 0;
172
173 /** Navigate forwardin the history of visited pages.
174 * Only valid if CanGoForward() returned true.
175 */
176 virtual void GoForward() = 0;
177
178 /**
179 * Load a HTMl document (web page) from a URL
180 * @param url the URL where the HTML document to display can be found
181 * @note web engines generally report errors asynchronously, so if you wish
182 * to know whether loading the URL was successful, register to receive
183 * navigation error events
184 */
185 virtual void LoadUrl(const wxString& url) = 0;
186
187 virtual void ClearHistory() = 0;
188 virtual void EnableHistory(bool enable = true) = 0;
189 virtual wxVector<wxSharedPtr<wxWebHistoryItem> > GetBackwardHistory() = 0;
190 virtual wxVector<wxSharedPtr<wxWebHistoryItem> > GetForwardHistory() = 0;
191 virtual void LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item) = 0;
192
193 /**
194 * Stop the current page loading process, if any.
195 * May trigger an error event of type wxWEB_NAV_ERR_USER_CANCELLED.
196 * TODO: make wxWEB_NAV_ERR_USER_CANCELLED errors uniform across ports.
197 */
198 virtual void Stop() = 0;
199
200 /**
201 * Reload the currently displayed URL.
202 * @param flags A bit array that may optionnally contain reload options
203 */
204 virtual void Reload(wxWebViewReloadFlags flags = wxWEB_VIEW_RELOAD_DEFAULT) = 0;
205
206
207 /**
208 * Get the URL of the currently displayed document
209 */
210 virtual wxString GetCurrentURL() = 0;
211
212 /**
213 * Get the title of the current web page, or its URL/path if title is not
214 * available
215 */
216 virtual wxString GetCurrentTitle() = 0;
217
218 // TODO: handle choosing a frame when calling GetPageSource()?
219 /**
220 * Get the HTML source code of the currently displayed document
221 * @return the HTML source code, or an empty string if no page is currently
222 * shown
223 */
224 virtual wxString GetPageSource() = 0;
225 virtual wxString GetPageText() = 0;
226
227 /**
228 * Get the zoom factor of the page
229 * @return How much the HTML document is zoomed (scaleed)
230 */
231 virtual wxWebViewZoom GetZoom() = 0;
232
233 /**
234 * Set the zoom factor of the page
235 * @param zoom How much to zoom (scale) the HTML document
236 */
237 virtual void SetZoom(wxWebViewZoom zoom) = 0;
238
239 /**
240 * Set how to interpret the zoom factor
241 * @param zoomType how the zoom factor should be interpreted by the
242 * HTML engine
243 * @note invoke canSetZoomType() first, some HTML renderers may not
244 * support all zoom types
245 */
246 virtual void SetZoomType(wxWebViewZoomType zoomType) = 0;
247
248 /**
249 * Get how the zoom factor is currently interpreted
250 * @return how the zoom factor is currently interpreted by the HTML engine
251 */
252 virtual wxWebViewZoomType GetZoomType() const = 0;
253
254 /**
255 * Retrieve whether the current HTML engine supports a type of zoom
256 * @param type the type of zoom to test
257 * @return whether this type of zoom is supported by this HTML engine
258 * (and thus can be set through setZoomType())
259 */
260 virtual bool CanSetZoomType(wxWebViewZoomType type) const = 0;
261
262 // TODO: allow 'SetPage' to find files (e.g. images) from a virtual file
263 // system if possible
264 /**
265 * Set the displayed page source to the contents of the given string
266 * @param html the string that contains the HTML data to display
267 * @param baseUrl URL assigned to the HTML data, to be used to resolve
268 * relative paths, for instance
269 */
270 virtual void SetPage(const wxString& html, const wxString& baseUrl) = 0;
271
272 /**
273 * Set the displayed page source to the contents of the given stream
274 * @param html the stream to read HTML data from
275 * @param baseUrl URL assigned to the HTML data, to be used to resolve
276 * relative paths, for instance
277 */
278 virtual void SetPage(wxInputStream& html, wxString baseUrl)
279 {
280 wxStringOutputStream stream;
281 stream.Write(html);
282 SetPage(stream.GetString(), baseUrl);
283 }
284
285 virtual void SetEditable(bool enable = true) = 0;
286 virtual bool IsEditable() = 0;
287
288 virtual void SelectAll() = 0;
289 virtual bool HasSelection() = 0;
290 virtual void DeleteSelection() = 0;
291 virtual wxString GetSelectedText() = 0;
292 virtual wxString GetSelectedSource() = 0;
293 virtual void ClearSelection() = 0;
294
295 virtual void RunScript(const wxString& javascript) = 0;
296
297 // TODO:
298 // void EnableJavascript(bool enabled); // maybe?
299 // // maybe?
300
301 // void SetScrollPos(int pos); // maybe?
302 // int GetScrollPos(); // maybe?
303
304 // wxString GetStatusText(); // maybe?
305 // void SetStatusText(wxString text); // maybe?
306 // * status text changed event?
307 // * title changed event?
308
309 // virtual bool IsOfflineMode() = 0; // maybe?
310 // virtual void SetOfflineMode(bool offline) = 0; // maybe?
311
312 /**
313 * Opens a print dialog so that the user may print the currently
314 * displayed page.
315 */
316 virtual void Print() = 0;
317
318 /**
319 * Returns whether the web control is currently busy (e.g. loading a page)
320 */
321 virtual bool IsBusy() = 0;
322
323 //Clipboard functions
324 virtual bool CanCut() = 0;
325 virtual bool CanCopy() = 0;
326 virtual bool CanPaste() = 0;
327 virtual void Cut() = 0;
328 virtual void Copy() = 0;
329 virtual void Paste() = 0;
330
331 //Undo / redo functionality
332 virtual bool CanUndo() = 0;
333 virtual bool CanRedo() = 0;
334 virtual void Undo() = 0;
335 virtual void Redo() = 0;
336 };
337
338 class WXDLLIMPEXP_WEB wxWebNavigationEvent : public wxCommandEvent
339 {
340 public:
341 wxWebNavigationEvent() {}
342 wxWebNavigationEvent(wxEventType type, int id, const wxString url,
343 const wxString target, bool canVeto)
344 : wxCommandEvent(type, id)
345 {
346 m_url = url;
347 m_target = target;
348 m_vetoed = false;
349 m_canVeto = canVeto;
350 }
351
352 /**
353 * Get the URL being visited
354 */
355 const wxString& GetURL() const { return m_url; }
356
357 /**
358 * Get the target (frame or window) in which the URL that caused this event
359 * is viewed, or an empty string if not available.
360 */
361 const wxString& GetTarget() const { return m_target; }
362
363 // default copy ctor, assignment operator and dtor are ok
364 virtual wxEvent* Clone() const { return new wxWebNavigationEvent(*this); }
365
366 /** Get whether this event may be vetoed (stopped/prevented). Only
367 * meaningful for events fired before navigation takes place.
368 */
369 bool CanVeto() const { return m_canVeto; }
370
371 /** Whether this event was vetoed (stopped/prevented). Only meaningful for
372 * events fired before navigation takes place.
373 */
374 bool IsVetoed() const { return m_vetoed; }
375
376 /** Veto (prevent/stop) this event. Only meaningful for events fired
377 * before navigation takes place. Only valid if CanVeto() returned true.
378 */
379 void Veto() { wxASSERT(m_canVeto); m_vetoed = true; }
380
381 private:
382 wxString m_url;
383 wxString m_target;
384 bool m_canVeto;
385 bool m_vetoed;
386
387 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWebNavigationEvent);
388 };
389
390 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATING, wxWebNavigationEvent );
391 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATED, wxWebNavigationEvent );
392 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_LOADED, wxWebNavigationEvent );
393 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_ERROR, wxWebNavigationEvent );
394 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, wxWebNavigationEvent );
395
396 typedef void (wxEvtHandler::*wxWebNavigationEventFunction)
397 (wxWebNavigationEvent&);
398
399 #define wxWebNavigationEventHandler(func) \
400 wxEVENT_HANDLER_CAST(wxWebNavigationEventFunction, func)
401
402 #define EVT_WEB_VIEW_NAVIGATING(id, fn) \
403 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, id, \
404 wxHtmlNavigatingEventHandler(fn))
405
406 #define EVT_WEB_VIEW_NAVIGATED(id, fn) \
407 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, id, \
408 wxHtmlNavigatingEventHandler(fn))
409
410 #define EVT_WEB_VIEW_LOADED(id, fn) \
411 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_LOADED, id, \
412 wxHtmlNavigatingEventHandler(fn))
413
414 #define EVT_WEB_VIEW_ERRROR(id, fn) \
415 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \
416 wxHtmlNavigatingEventHandler(fn))
417
418 #define EVT_WEB_VIEW_NEWWINDOW(id, fn) \
419 wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \
420 wxHtmlNavigatingEventHandler(fn))
421
422 #endif // wxUSE_WEB
423
424 #endif // _WX_WEB_VIEW_H_