Add a section about async notifications to the documentation.
[wxWidgets.git] / interface / wx / webview.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webview.h
3 // Purpose: interface of wxWebView
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 Zoom levels availiable in wxWebView
11 */
12 enum wxWebViewZoom
13 {
14 wxWEB_VIEW_ZOOM_TINY,
15 wxWEB_VIEW_ZOOM_SMALL,
16 wxWEB_VIEW_ZOOM_MEDIUM, //!< default size
17 wxWEB_VIEW_ZOOM_LARGE,
18 wxWEB_VIEW_ZOOM_LARGEST
19 };
20
21 /**
22 The type of zooming that the web view control can perform
23 */
24 enum wxWebViewZoomType
25 {
26 /**
27 The entire layout scales when zooming, including images
28 */
29 wxWEB_VIEW_ZOOM_TYPE_LAYOUT,
30 /**
31 Only the text changes in size when zooming, images and other layout
32 elements retain their initial size
33 */
34 wxWEB_VIEW_ZOOM_TYPE_TEXT
35 };
36
37 /**
38 Types of errors that can cause navigation to fail
39 */
40 enum wxWebNavigationError
41 {
42 /** Connection error (timeout, etc.) */
43 wxWEB_NAV_ERR_CONNECTION,
44 /** Invalid certificate */
45 wxWEB_NAV_ERR_CERTIFICATE,
46 /** Authentication required */
47 wxWEB_NAV_ERR_AUTH,
48 /** Other security error */
49 wxWEB_NAV_ERR_SECURITY,
50 /** Requested resource not found */
51 wxWEB_NAV_ERR_NOT_FOUND,
52 /** Invalid request/parameters (e.g. bad URL, bad protocol,
53 unsupported resource type) */
54 wxWEB_NAV_ERR_REQUEST,
55 /** The user cancelled (e.g. in a dialog) */
56 wxWEB_NAV_ERR_USER_CANCELLED,
57 /** Another (exotic) type of error that didn't fit in other categories*/
58 wxWEB_NAV_ERR_OTHER
59 };
60
61 /**
62 Type of refresh
63 */
64 enum wxWebViewReloadFlags
65 {
66 /** Default reload, will access cache */
67 wxWEB_VIEW_RELOAD_DEFAULT,
68 /** Reload the current view without accessing the cache */
69 wxWEB_VIEW_RELOAD_NO_CACHE
70 };
71
72
73 /**
74 * List of available backends for wxWebView
75 */
76 enum wxWebViewBackend
77 {
78 /** Value that may be passed to wxWebView to let it pick an appropriate
79 * engine for the current platform*/
80 wxWEB_VIEW_BACKEND_DEFAULT,
81
82 /** The WebKit web engine */
83 wxWEB_VIEW_BACKEND_WEBKIT,
84
85 /** Use Microsoft Internet Explorer as web engine */
86 wxWEB_VIEW_BACKEND_IE
87 };
88
89 /**
90 @class wxWebHistoryItem
91
92 A simple class that contains the URL and title of an element of the history
93 of a wxWebView.
94
95 @library{wxweb}
96 @category{web}
97
98 @see wxWebView
99 */
100 class wxWebHistoryItem
101 {
102 public:
103 /**
104 Construtor.
105 */
106 wxWebHistoryItem(const wxString& url, const wxString& title);
107
108 /**
109 @return The url of the page.
110 */
111 wxString GetUrl();
112
113 /**
114 @return The title of the page.
115 */
116 wxString GetTitle();
117 };
118
119 /**
120 @class wxWebHandler
121
122 The base class for handling custom schemes in wxWebView, for example to
123 allow virtual file system support.
124
125 @library{wxweb}
126 @category{web}
127
128 @see wxWebView
129 */
130 class wxWebHandler
131 {
132 public:
133 /**
134 @return A pointer to the file represented by @c uri.
135 */
136 virtual wxFSFile* GetFile(const wxString &uri) = 0;
137
138 /**
139 @return The name of the scheme, for example @c file or @c http.
140 */
141 virtual wxString GetName() const = 0;
142 };
143
144 /**
145 @class wxWebView
146
147 This control may be used to render web (HTML / CSS / javascript) documents.
148 Capabilities of the HTML renderer will depend upon the backed.
149
150 @c wxWEB_VIEW_BACKEND_IE uses the the Trident rendering engine, which
151 is also used by Internet Explorer. It is important to note that by default
152 it emulates Internet Explorer 7, this can be chaged with a registry
153 setting, see
154 <a href="http://msdn.microsoft.com/en-us/library/ee330730%28v=vs.85%29.aspx#browser_emulation">
155 this</a> article for more information.
156
157 @section async Asynchronous Notifications
158
159 Many of the methods in wxWebView are asynchronous, i.e. they return
160 immediately and perform their work in the background. This includes
161 functions such as LoadUrl() and Reload(). To receive notification of the
162 progress and completion of these functions you need to handle the events
163 that are provided. Specifically @c wxEVT_COMMAND_WEB_VIEW_LOADED notifies
164 when the page or a sub-frame has finished loading and
165 @c wxEVT_COMMAND_WEB_VIEW_ERROR notifies that an error has occurred.
166
167 @section vfs Virtual File Systems and Custom Schemes
168
169 wxWebView supports the registering of custom scheme handlers, for example
170 @c file or @c http. To do this create a new class which inherits from
171 wxWebHandler, where the wxWebHandler::GetName() method returns the scheme
172 you wish to handle and wxWebHandler::GetFile() returns a pointer to a
173 wxFSFile which represents the given url. You can then register your handler
174 with RegisterHandler() it will be called for all pages and resources.
175
176 wxWebFileHandler is provided to allow the navigation of pages inside a zip
177 archive. It overrides the @c file scheme and provides support for the
178 standard @c file syntax as well as paths to archives of the form
179 @c file:///C:/exmaple/docs.zip;protocol=zip/main.htm
180
181 @beginEventEmissionTable{wxWebNavigationEvent}
182 @event{EVT_WEB_VIEW_NAVIGATING(id, func)}
183 Process a @c wxEVT_COMMAND_WEB_VIEW_NAVIGATING event, generated before trying
184 to get a resource. This event may be vetoed to prevent navigating to this
185 resource. Note that if the displayed HTML document has several frames, one
186 such event will be generated per frame.
187 @event{EVT_WEB_VIEW_NAVIGATED(id, func)}
188 Process a @c wxEVT_COMMAND_WEB_VIEW_NAVIGATED event generated after it was
189 confirmed that a resource would be requested. This event may not be vetoed.
190 Note that if the displayed HTML document has several frames, one such event
191 will be generated per frame.
192 @event{EVT_WEB_VIEW_LOADED(id, func)}
193 Process a @c wxEVT_COMMAND_WEB_VIEW_LOADED event generated when the document
194 is fully loaded and displayed. Note that if the displayed HTML document has
195 several frames, one such event will be generated per frame.
196 @event{EVT_WEB_VIEW_ERRROR(id, func)}
197 Process a @c wxEVT_COMMAND_WEB_VIEW_ERROR event generated when a navigation
198 error occurs.
199 The integer associated with this event will be a wxWebNavigationError item.
200 The string associated with this event may contain a backend-specific more
201 precise error message/code.
202 @event{EVT_WEB_VIEW_NEWWINDOW(id, func)}
203 Process a @c wxEVT_COMMAND_WEB_VIEW_NEWWINDOW event, generated when a new
204 window is created. You must handle this event if you want anything to
205 happen, for example to load the page in a new window or tab.
206 @event{EVT_WEB_VIEW_TITLE_CHANGED(id, func)}
207 Process a @c wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED event, generated when
208 the page title changes. Use GetString to get the title.
209 @endEventTable
210
211 @library{wxweb}
212 @category{ctrl,web}
213 @see wxWebHandler, wxWebNavigationEvent
214 */
215 class wxWebView : public wxControl
216 {
217 public:
218
219 /**
220 Creation function for two-step creation.
221 */
222 virtual bool Create(wxWindow* parent,
223 wxWindowID id,
224 const wxString& url,
225 const wxPoint& pos,
226 const wxSize& size,
227 long style,
228 const wxString& name) = 0;
229
230 /**
231 Factory function to create a new wxWebView for two-step creation
232 (you need to call wxWebView::Create on the returned object)
233 @param backend which web engine to use as backend for wxWebView
234 @return the created wxWebView, or NULL if the requested backend is
235 not available
236 */
237 static wxWebView* New(wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT);
238
239 /**
240 Factory function to create a new wxWebView
241 @param parent parent window to create this view in
242 @param id ID of this control
243 @param url URL to load by default in the web view
244 @param pos position to create this control at
245 (you may use wxDefaultPosition if you use sizers)
246 @param size size to create this control with
247 (you may use wxDefaultSize if you use sizers)
248 @param backend which web engine to use as backend for wxWebView
249 @return the created wxWebView, or NULL if the requested backend
250 is not available
251 */
252 static wxWebView* New(wxWindow* parent,
253 wxWindowID id,
254 const wxString& url = wxWebViewDefaultURLStr,
255 const wxPoint& pos = wxDefaultPosition,
256 const wxSize& size = wxDefaultSize,
257 wxWebViewBackend backend = wxWEB_VIEW_BACKEND_DEFAULT,
258 long style = 0,
259 const wxString& name = wxWebViewNameStr);
260
261 /**
262 Get the title of the current web page, or its URL/path if title is not
263 available.
264 */
265 virtual wxString GetCurrentTitle() = 0;
266
267 /**
268 Get the URL of the currently displayed document.
269 */
270 virtual wxString GetCurrentURL() = 0;
271
272 /**
273 Get the HTML source code of the currently displayed document.
274 @return The HTML source code, or an empty string if no page is currently
275 shown.
276 */
277 virtual wxString GetPageSource() = 0;
278
279 /**
280 Get the text of the current page.
281 */
282 virtual wxString GetPageText() = 0;
283
284 /**
285 Returns whether the web control is currently busy (e.g. loading a page).
286 */
287 virtual bool IsBusy() = 0;
288
289 /**
290 Returns whether the web control is currently editable
291 */
292 virtual bool IsEditable() = 0;
293
294 /**
295 Load a web page from a URL
296 @param url The URL of the page to be loaded.
297 @note Web engines generally report errors asynchronously, so if you wish
298 to know whether loading the URL was successful, register to receive
299 navigation error events.
300 */
301 virtual void LoadUrl(const wxString& url) = 0;
302
303 /**
304 Opens a print dialog so that the user may print the currently
305 displayed page.
306 */
307 virtual void Print() = 0;
308
309 /**
310 Registers a custom scheme handler.
311 @param handler A pointer to a heap allocated wxWebHandler.
312 */
313 virtual void RegisterHandler(wxWebHandler* handler) = 0;
314
315 /**
316 Reload the currently displayed URL.
317 @param flags A bit array that may optionally contain reload options.
318 */
319 virtual void Reload(wxWebViewReloadFlags flags = wxWEB_VIEW_RELOAD_DEFAULT) = 0;
320
321 /**
322 Runs the given javascript code.
323 */
324 virtual void RunScript(const wxString& javascript) = 0;
325
326 /**
327 Set the editable property of the web control. Enabling allows the user
328 to edit the page even if the @c contenteditable attribute is not set.
329 The exact capabilities vary with the backend being used.
330 */
331 virtual void SetEditable(bool enable = true) = 0;
332
333 /**
334 Set the displayed page source to the contents of the given string.
335 @param html The string that contains the HTML data to display.
336 @param baseUrl URL assigned to the HTML data, to be used to resolve
337 relative paths, for instance.
338 */
339 virtual void SetPage(const wxString& html, const wxString& baseUrl) = 0;
340
341 /**
342 Set the displayed page source to the contents of the given stream.
343 @param html The stream to read HTML data from.
344 @param baseUrl URL assigned to the HTML data, to be used to resolve
345 relative paths, for instance.
346 */
347 virtual void SetPage(wxInputStream& html, wxString baseUrl);
348
349 /**
350 Stop the current page loading process, if any.
351 May trigger an error event of type @c wxWEB_NAV_ERR_USER_CANCELLED.
352 TODO: make @c wxWEB_NAV_ERR_USER_CANCELLED errors uniform across ports.
353 */
354 virtual void Stop() = 0;
355
356 /**
357 @name Clipboard
358 */
359
360 /**
361 Returns @true if the current selection can be copied.
362 */
363 virtual bool CanCopy() = 0;
364
365 /**
366 Returns @true if the current selection can be cut.
367 */
368 virtual bool CanCut() = 0;
369
370 /**
371 Returns @true if data can be pasted.
372 */
373 virtual bool CanPaste() = 0;
374
375 /**
376 Copies the current selection.
377 */
378 virtual void Copy() = 0;
379
380 /**
381 Cuts the current selection.
382 */
383 virtual void Cut() = 0;
384
385 /**
386 Pastes the current data.
387 */
388 virtual void Paste() = 0;
389
390 /**
391 @name History
392 */
393
394 /**
395 Returns @true if it is possible to navigate backward in the history of
396 visited pages.
397 */
398 virtual bool CanGoBack() = 0;
399
400 /**
401 Returns @true if it is possible to navigate forward in the history of
402 visited pages.
403 */
404 virtual bool CanGoForward() = 0;
405
406 /**
407 Clear the history, this will also remove the visible page.
408 */
409 virtual void ClearHistory() = 0;
410
411 /**
412 Enable or disable the history. This will also clear the history.
413 */
414 virtual void EnableHistory(bool enable = true) = 0;
415
416 /**
417 Returns a list of items in the back history. The first item in the
418 vector is the first page that was loaded by the control.
419 */
420 virtual wxVector<wxSharedPtr<wxWebHistoryItem> > GetBackwardHistory() = 0;
421
422 /**
423 Returns a list of items in the forward history. The first item in the
424 vector is the next item in the history with respect to the curently
425 loaded page.
426 */
427 virtual wxVector<wxSharedPtr<wxWebHistoryItem> > GetForwardHistory() = 0;
428
429 /**
430 Navigate back in the history of visited pages.
431 Only valid if CanGoBack() returns true.
432 */
433 virtual void GoBack() = 0;
434
435 /**
436 Navigate forward in the history of visited pages.
437 Only valid if CanGoForward() returns true.
438 */
439 virtual void GoForward() = 0;
440
441 /**
442 Loads a history item.
443 */
444 virtual void LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item) = 0;
445
446 /**
447 @name Selection
448 */
449
450 /**
451 Clears the current selection.
452 */
453 virtual void ClearSelection() = 0;
454
455 /**
456 Deletes the current selection. Note that for @c wxWEB_VIEW_BACKEND_WEBKIT
457 the selection must be editable, either through SetEditable or the
458 correct HTML attribute.
459 */
460 virtual void DeleteSelection() = 0;
461
462 /**
463 Returns the currently selected source, if any.
464 */
465 virtual wxString GetSelectedSource() = 0;
466
467 /**
468 Returns the currently selected text, if any.
469 */
470 virtual wxString GetSelectedText() = 0;
471
472 /**
473 Returns @true if there is a current selection.
474 */
475 virtual bool HasSelection() = 0;
476
477 /**
478 Selects the entire page.
479 */
480 virtual void SelectAll() = 0;
481
482 /**
483 @name Undo / Redo
484 */
485
486 /**
487 Returns @true if there is an action to redo.
488 */
489 virtual bool CanRedo() = 0;
490
491 /**
492 Returns @true if there is an action to undo.
493 */
494 virtual bool CanUndo() = 0;
495
496 /**
497 Redos the last action.
498 */
499 virtual void Redo() = 0;
500
501 /**
502 Undos the last action.
503 */
504 virtual void Undo() = 0;
505
506 /**
507 @name Zoom
508 */
509
510 /**
511 Retrieve whether the current HTML engine supports a zoom type.
512 @param type The zoom type to test.
513 @return Whether this type of zoom is supported by this HTML engine
514 (and thus can be set through SetZoomType()).
515 */
516 virtual bool CanSetZoomType(wxWebViewZoomType type) const = 0;
517
518 /**
519 Get the zoom factor of the page.
520 @return The current level of zoom.
521 */
522 virtual wxWebViewZoom GetZoom() = 0;
523
524 /**
525 Get how the zoom factor is currently interpreted.
526 @return How the zoom factor is currently interpreted by the HTML engine.
527 */
528 virtual wxWebViewZoomType GetZoomType() const = 0;
529
530 /**
531 Set the zoom factor of the page.
532 @param zoom How much to zoom (scale) the HTML document.
533 */
534 virtual void SetZoom(wxWebViewZoom zoom) = 0;
535
536 /**
537 Set how to interpret the zoom factor.
538 @param zoomType How the zoom factor should be interpreted by the
539 HTML engine.
540 @note invoke CanSetZoomType() first, some HTML renderers may not
541 support all zoom types.
542 */
543 virtual void SetZoomType(wxWebViewZoomType zoomType) = 0;
544 };
545
546
547
548
549 /**
550 @class wxWebNavigationEvent
551
552 A navigation event holds information about events associated with
553 wxWebView objects.
554
555 @beginEventEmissionTable{wxWebNavigationEvent}
556 @event{EVT_WEB_VIEW_NAVIGATING(id, func)}
557 Process a @c wxEVT_COMMAND_WEB_VIEW_NAVIGATING event, generated before trying
558 to get a resource. This event may be vetoed to prevent navigating to this
559 resource. Note that if the displayed HTML document has several frames, one
560 such event will be generated per frame.
561 @event{EVT_WEB_VIEW_NAVIGATED(id, func)}
562 Process a @c wxEVT_COMMAND_WEB_VIEW_NAVIGATED event generated after it was
563 confirmed that a resource would be requested. This event may not be vetoed.
564 Note that if the displayed HTML document has several frames, one such event
565 will be generated per frame.
566 @event{EVT_WEB_VIEW_LOADED(id, func)}
567 Process a @c wxEVT_COMMAND_WEB_VIEW_LOADED event generated when the document
568 is fully loaded and displayed. Note that if the displayed HTML document has
569 several frames, one such event will be generated per frame.
570 @event{EVT_WEB_VIEW_ERRROR(id, func)}
571 Process a @c wxEVT_COMMAND_WEB_VIEW_ERROR event generated when a navigation
572 error occurs.
573 The integer associated with this event will be a wxWebNavigationError item.
574 The string associated with this event may contain a backend-specific more
575 precise error message/code.
576 @event{EVT_WEB_VIEW_NEWWINDOW(id, func)}
577 Process a @c wxEVT_COMMAND_WEB_VIEW_NEWWINDOW event, generated when a new
578 window is created. You must handle this event if you want anything to
579 happen, for example to load the page in a new window or tab.
580 @event{EVT_WEB_VIEW_TITLE_CHANGED(id, func)}
581 Process a @c wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED event, generated when
582 the page title changes. Use GetString to get the title.
583 @endEventTable
584
585 @library{wxweb}
586 @category{events,web}
587
588 @see wxWebView
589 */
590 class wxWebNavigationEvent : public wxCommandEvent
591 {
592 public:
593 wxWebNavigationEvent();
594 wxWebNavigationEvent(wxEventType type, int id, const wxString href,
595 const wxString target, bool canVeto);
596
597 /**
598 Get the name of the target frame which the url of this event
599 has been or will be loaded into. This may return an emptry string
600 if the frame is not avaliable.
601 */
602 const wxString& GetTarget() const;
603
604 /**
605 Get the URL being visited
606 */
607 const wxString& GetURL() const;
608
609 virtual wxEvent* Clone() const;
610
611 /**
612 Get whether this event may be vetoed (stopped/prevented). Only
613 meaningful for events fired before navigation takes place.
614 */
615 bool CanVeto() const;
616
617 /**
618 Whether this event was vetoed (stopped/prevented). Only meaningful for
619 events fired before navigation takes place or new window events.
620 */
621 bool IsVetoed() const;
622
623 /**
624 Veto (prevent/stop) this event. Only meaningful for events fired
625 before navigation takes place. Only valid if CanVeto() returned true.
626 */
627 void Veto();
628 };