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