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