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