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