]>
Commit | Line | Data |
---|---|---|
61b98a2d | 1 | ///////////////////////////////////////////////////////////////////////////// |
8290e3cd | 2 | // Name: src/gtk/webview_webkit.cpp |
61b98a2d SL |
3 | // Purpose: GTK WebKit backend for web view component |
4 | // Author: Marianne Gagnon, Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 2010 Marianne Gagnon, 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
ea179539 | 13 | #if wxUSE_WEBVIEW_WEBKIT |
61b98a2d SL |
14 | |
15 | #include "wx/stockitem.h" | |
8290e3cd | 16 | #include "wx/gtk/webview_webkit.h" |
61b98a2d SL |
17 | #include "wx/gtk/control.h" |
18 | #include "wx/gtk/private.h" | |
19 | #include "webkit/webkit.h" | |
20 | ||
21 | // ---------------------------------------------------------------------------- | |
22 | // GTK callbacks | |
23 | // ---------------------------------------------------------------------------- | |
24 | ||
25 | extern "C" | |
26 | { | |
27 | ||
28 | static void | |
3dffc2ae | 29 | wxgtk_webkitctrl_load_status_callback(GtkWidget* widget, GParamSpec*, |
b64b4e70 | 30 | wxWebViewWebKit *webKitCtrl) |
61b98a2d SL |
31 | { |
32 | if (!webKitCtrl->m_ready) return; | |
33 | ||
34 | wxString url = webKitCtrl->GetCurrentURL(); | |
35 | ||
36 | WebKitLoadStatus status; | |
37 | g_object_get(G_OBJECT(widget), "load-status", &status, NULL); | |
38 | ||
39 | wxString target; // TODO: get target (if possible) | |
40 | ||
41 | if (status == WEBKIT_LOAD_FINISHED) | |
42 | { | |
43 | webKitCtrl->m_busy = false; | |
44 | wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_LOADED, | |
45 | webKitCtrl->GetId(), | |
46 | url, target, false); | |
47 | ||
48 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
49 | webKitCtrl->GetEventHandler()->ProcessEvent(thisEvent); | |
50 | } | |
51 | else if (status == WEBKIT_LOAD_COMMITTED) | |
52 | { | |
53 | webKitCtrl->m_busy = true; | |
54 | wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, | |
55 | webKitCtrl->GetId(), | |
56 | url, target, false); | |
57 | ||
58 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
59 | webKitCtrl->GetEventHandler()->ProcessEvent(thisEvent); | |
60 | } | |
61 | } | |
62 | ||
63 | static WebKitNavigationResponse | |
3dffc2ae | 64 | wxgtk_webkitctrl_navigation_requ_callback(WebKitWebView*, |
61b98a2d SL |
65 | WebKitWebFrame *frame, |
66 | WebKitNetworkRequest *request, | |
b64b4e70 | 67 | wxWebViewWebKit *webKitCtrl) |
61b98a2d SL |
68 | { |
69 | webKitCtrl->m_busy = true; | |
70 | ||
71 | const gchar* uri = webkit_network_request_get_uri(request); | |
72 | ||
73 | wxString target = webkit_web_frame_get_name (frame); | |
74 | wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, | |
75 | webKitCtrl->GetId(), | |
76 | wxString( uri, wxConvUTF8 ), | |
77 | target, | |
78 | true); | |
79 | ||
80 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
81 | webKitCtrl->GetEventHandler()->ProcessEvent(thisEvent); | |
82 | ||
83 | if (thisEvent.IsVetoed()) | |
84 | { | |
85 | webKitCtrl->m_busy = false; | |
86 | return WEBKIT_NAVIGATION_RESPONSE_IGNORE; | |
87 | } | |
88 | else | |
89 | { | |
90 | return WEBKIT_NAVIGATION_RESPONSE_ACCEPT; | |
91 | } | |
92 | } | |
93 | ||
94 | static gboolean | |
3dffc2ae SL |
95 | wxgtk_webkitctrl_error (WebKitWebView*, |
96 | WebKitWebFrame*, | |
61b98a2d SL |
97 | gchar *uri, |
98 | gpointer web_error, | |
b64b4e70 | 99 | wxWebViewWebKit* webKitWindow) |
61b98a2d SL |
100 | { |
101 | webKitWindow->m_busy = false; | |
102 | wxWebNavigationError type = wxWEB_NAV_ERR_OTHER; | |
103 | ||
104 | GError* error = (GError*)web_error; | |
105 | wxString description(error->message, wxConvUTF8); | |
106 | ||
107 | if (strcmp(g_quark_to_string(error->domain), "soup_http_error_quark") == 0) | |
108 | { | |
109 | switch (error->code) | |
110 | { | |
111 | case SOUP_STATUS_CANCELLED: | |
112 | type = wxWEB_NAV_ERR_USER_CANCELLED; | |
113 | break; | |
114 | ||
115 | case SOUP_STATUS_CANT_RESOLVE: | |
116 | case SOUP_STATUS_NOT_FOUND: | |
117 | type = wxWEB_NAV_ERR_NOT_FOUND; | |
118 | break; | |
119 | ||
120 | case SOUP_STATUS_CANT_RESOLVE_PROXY: | |
121 | case SOUP_STATUS_CANT_CONNECT: | |
122 | case SOUP_STATUS_CANT_CONNECT_PROXY: | |
123 | case SOUP_STATUS_SSL_FAILED: | |
124 | case SOUP_STATUS_IO_ERROR: | |
125 | type = wxWEB_NAV_ERR_CONNECTION; | |
126 | break; | |
127 | ||
128 | case SOUP_STATUS_MALFORMED: | |
129 | //case SOUP_STATUS_TOO_MANY_REDIRECTS: | |
130 | type = wxWEB_NAV_ERR_REQUEST; | |
131 | break; | |
132 | ||
133 | //case SOUP_STATUS_NO_CONTENT: | |
134 | //case SOUP_STATUS_RESET_CONTENT: | |
135 | ||
136 | case SOUP_STATUS_BAD_REQUEST: | |
137 | type = wxWEB_NAV_ERR_REQUEST; | |
138 | break; | |
139 | ||
140 | case SOUP_STATUS_UNAUTHORIZED: | |
141 | case SOUP_STATUS_FORBIDDEN: | |
142 | type = wxWEB_NAV_ERR_AUTH; | |
143 | break; | |
144 | ||
145 | case SOUP_STATUS_METHOD_NOT_ALLOWED: | |
146 | case SOUP_STATUS_NOT_ACCEPTABLE: | |
147 | type = wxWEB_NAV_ERR_SECURITY; | |
148 | break; | |
149 | ||
150 | case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED: | |
151 | type = wxWEB_NAV_ERR_AUTH; | |
152 | break; | |
153 | ||
154 | case SOUP_STATUS_REQUEST_TIMEOUT: | |
155 | type = wxWEB_NAV_ERR_CONNECTION; | |
156 | break; | |
157 | ||
158 | //case SOUP_STATUS_PAYMENT_REQUIRED: | |
159 | case SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE: | |
160 | case SOUP_STATUS_REQUEST_URI_TOO_LONG: | |
161 | case SOUP_STATUS_UNSUPPORTED_MEDIA_TYPE: | |
162 | type = wxWEB_NAV_ERR_REQUEST; | |
163 | break; | |
164 | ||
165 | case SOUP_STATUS_BAD_GATEWAY: | |
166 | case SOUP_STATUS_SERVICE_UNAVAILABLE: | |
167 | case SOUP_STATUS_GATEWAY_TIMEOUT: | |
168 | type = wxWEB_NAV_ERR_CONNECTION; | |
169 | break; | |
170 | ||
171 | case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED: | |
172 | type = wxWEB_NAV_ERR_REQUEST; | |
173 | break; | |
174 | //case SOUP_STATUS_INSUFFICIENT_STORAGE: | |
175 | //case SOUP_STATUS_NOT_EXTENDED: | |
176 | } | |
177 | } | |
178 | else if (strcmp(g_quark_to_string(error->domain), | |
179 | "webkit-network-error-quark") == 0) | |
180 | { | |
181 | switch (error->code) | |
182 | { | |
183 | //WEBKIT_NETWORK_ERROR_FAILED: | |
184 | //WEBKIT_NETWORK_ERROR_TRANSPORT: | |
185 | ||
186 | case WEBKIT_NETWORK_ERROR_UNKNOWN_PROTOCOL: | |
187 | type = wxWEB_NAV_ERR_REQUEST; | |
188 | break; | |
189 | ||
190 | case WEBKIT_NETWORK_ERROR_CANCELLED: | |
191 | type = wxWEB_NAV_ERR_USER_CANCELLED; | |
192 | break; | |
193 | ||
194 | case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST: | |
195 | type = wxWEB_NAV_ERR_NOT_FOUND; | |
196 | break; | |
197 | } | |
198 | } | |
199 | else if (strcmp(g_quark_to_string(error->domain), | |
200 | "webkit-policy-error-quark") == 0) | |
201 | { | |
202 | switch (error->code) | |
203 | { | |
204 | //case WEBKIT_POLICY_ERROR_FAILED: | |
205 | //case WEBKIT_POLICY_ERROR_CANNOT_SHOW_MIME_TYPE: | |
206 | //case WEBKIT_POLICY_ERROR_CANNOT_SHOW_URL: | |
207 | //case WEBKIT_POLICY_ERROR_FRAME_LOAD_INTERRUPTED_BY_POLICY_CHANGE: | |
208 | case WEBKIT_POLICY_ERROR_CANNOT_USE_RESTRICTED_PORT: | |
209 | type = wxWEB_NAV_ERR_SECURITY; | |
210 | break; | |
211 | } | |
212 | } | |
213 | /* | |
214 | webkit_plugin_error_quark | |
215 | else | |
216 | { | |
217 | printf("Error domain %s\n", g_quark_to_string(error->domain)); | |
218 | } | |
219 | */ | |
220 | ||
221 | wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_ERROR, | |
222 | webKitWindow->GetId(), | |
223 | uri, | |
224 | wxEmptyString, | |
225 | false); | |
226 | thisEvent.SetString(description); | |
227 | thisEvent.SetInt(type); | |
228 | ||
229 | if (webKitWindow && webKitWindow->GetEventHandler()) | |
230 | { | |
231 | webKitWindow->GetEventHandler()->ProcessEvent(thisEvent); | |
232 | } | |
233 | ||
234 | return FALSE; | |
235 | } | |
236 | ||
0016bf1d | 237 | static gboolean |
3dffc2ae | 238 | wxgtk_webkitctrl_new_window(WebKitWebView*, |
0016bf1d SL |
239 | WebKitWebFrame *frame, |
240 | WebKitNetworkRequest *request, | |
3dffc2ae | 241 | WebKitWebNavigationAction*, |
0016bf1d SL |
242 | WebKitWebPolicyDecision *policy_decision, |
243 | wxWebViewWebKit *webKitCtrl) | |
244 | { | |
245 | const gchar* uri = webkit_network_request_get_uri(request); | |
246 | ||
247 | wxString target = webkit_web_frame_get_name (frame); | |
248 | wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, | |
249 | webKitCtrl->GetId(), | |
250 | wxString( uri, wxConvUTF8 ), | |
251 | target, | |
252 | true); | |
253 | ||
254 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
255 | webKitCtrl->GetEventHandler()->ProcessEvent(thisEvent); | |
256 | ||
257 | if (thisEvent.IsVetoed()) | |
258 | { | |
259 | webkit_web_policy_decision_ignore(policy_decision); | |
260 | } | |
261 | else | |
262 | { | |
263 | webkit_web_policy_decision_use(policy_decision); | |
264 | } | |
265 | return TRUE; | |
266 | } | |
61b98a2d SL |
267 | |
268 | } // extern "C" | |
269 | ||
270 | //----------------------------------------------------------------------------- | |
b64b4e70 | 271 | // wxWebViewWebKit |
61b98a2d SL |
272 | //----------------------------------------------------------------------------- |
273 | ||
b64b4e70 | 274 | //IMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxControl) |
61b98a2d | 275 | |
b64b4e70 | 276 | bool wxWebViewWebKit::Create(wxWindow *parent, |
61b98a2d SL |
277 | wxWindowID id, |
278 | const wxString &url, | |
279 | const wxPoint& pos, | |
280 | const wxSize& size, | |
281 | long style, | |
282 | const wxString& name) | |
283 | { | |
284 | m_ready = false; | |
285 | m_busy = false; | |
286 | ||
287 | if (!PreCreation( parent, pos, size ) || | |
288 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
289 | { | |
b64b4e70 | 290 | wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") ); |
61b98a2d SL |
291 | return false; |
292 | } | |
293 | ||
294 | GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL); | |
295 | web_view = webkit_web_view_new (); | |
296 | g_object_ref(web_view); // TODO: check memory management | |
297 | ||
298 | m_widget = scrolled_window; | |
299 | g_object_ref(m_widget); // TODO: check memory management | |
300 | ||
301 | /* Place the WebKitWebView in the GtkScrolledWindow */ | |
302 | gtk_container_add (GTK_CONTAINER (scrolled_window), web_view); | |
303 | gtk_widget_show(m_widget); | |
304 | gtk_widget_show(web_view); | |
305 | ||
306 | g_signal_connect_after(web_view, "notify::load-status", | |
307 | G_CALLBACK(wxgtk_webkitctrl_load_status_callback), | |
308 | this); | |
309 | g_signal_connect_after(web_view, "navigation-requested", | |
310 | G_CALLBACK(wxgtk_webkitctrl_navigation_requ_callback), | |
311 | this); | |
312 | g_signal_connect_after(web_view, "load-error", | |
313 | G_CALLBACK(wxgtk_webkitctrl_error), | |
314 | this); | |
315 | ||
0016bf1d SL |
316 | g_signal_connect_after(web_view, "new-window-policy-decision-requested", |
317 | G_CALLBACK(wxgtk_webkitctrl_new_window), this); | |
61b98a2d SL |
318 | |
319 | m_parent->DoAddChild( this ); | |
320 | ||
321 | PostCreation(size); | |
322 | ||
323 | /* Open a webpage */ | |
324 | webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), url); | |
325 | ||
152a5808 SL |
326 | //Get the initial history limit so we can enable and disable it later |
327 | WebKitWebBackForwardList* history; | |
328 | history = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(web_view)); | |
329 | m_historyLimit = webkit_web_back_forward_list_get_limit(history); | |
330 | ||
61b98a2d SL |
331 | m_ready = true; |
332 | ||
333 | return true; | |
334 | } | |
335 | ||
b64b4e70 | 336 | bool wxWebViewWebKit::Enable( bool enable ) |
61b98a2d SL |
337 | { |
338 | if (!wxControl::Enable(enable)) | |
339 | return false; | |
340 | ||
341 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); | |
342 | ||
343 | //if (enable) | |
344 | // GTKFixSensitivity(); | |
345 | ||
346 | return true; | |
347 | } | |
348 | ||
349 | GdkWindow* | |
b64b4e70 | 350 | wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
61b98a2d SL |
351 | { |
352 | GdkWindow* window = gtk_widget_get_parent_window(m_widget); | |
353 | return window; | |
354 | } | |
355 | ||
b64b4e70 | 356 | void wxWebViewWebKit::ZoomIn() |
61b98a2d SL |
357 | { |
358 | webkit_web_view_zoom_in (WEBKIT_WEB_VIEW(web_view)); | |
359 | } | |
360 | ||
b64b4e70 | 361 | void wxWebViewWebKit::ZoomOut() |
61b98a2d SL |
362 | { |
363 | webkit_web_view_zoom_out (WEBKIT_WEB_VIEW(web_view)); | |
364 | } | |
365 | ||
b64b4e70 | 366 | void wxWebViewWebKit::SetWebkitZoom(float level) |
61b98a2d SL |
367 | { |
368 | webkit_web_view_set_zoom_level (WEBKIT_WEB_VIEW(web_view), level); | |
369 | } | |
370 | ||
b64b4e70 | 371 | float wxWebViewWebKit::GetWebkitZoom() |
61b98a2d SL |
372 | { |
373 | return webkit_web_view_get_zoom_level (WEBKIT_WEB_VIEW(web_view)); | |
374 | } | |
375 | ||
b64b4e70 | 376 | void wxWebViewWebKit::Stop() |
61b98a2d SL |
377 | { |
378 | webkit_web_view_stop_loading (WEBKIT_WEB_VIEW(web_view)); | |
379 | } | |
380 | ||
b64b4e70 | 381 | void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags) |
61b98a2d SL |
382 | { |
383 | if (flags & wxWEB_VIEW_RELOAD_NO_CACHE) | |
384 | { | |
385 | webkit_web_view_reload_bypass_cache (WEBKIT_WEB_VIEW(web_view)); | |
386 | } | |
387 | else | |
388 | { | |
389 | webkit_web_view_reload (WEBKIT_WEB_VIEW(web_view)); | |
390 | } | |
391 | } | |
392 | ||
b64b4e70 | 393 | void wxWebViewWebKit::LoadUrl(const wxString& url) |
61b98a2d | 394 | { |
b5374b5c | 395 | webkit_web_view_load_uri(WEBKIT_WEB_VIEW(web_view), wxGTK_CONV(url)); |
61b98a2d SL |
396 | } |
397 | ||
398 | ||
b64b4e70 | 399 | void wxWebViewWebKit::GoBack() |
61b98a2d SL |
400 | { |
401 | webkit_web_view_go_back (WEBKIT_WEB_VIEW(web_view)); | |
402 | } | |
403 | ||
b64b4e70 | 404 | void wxWebViewWebKit::GoForward() |
61b98a2d SL |
405 | { |
406 | webkit_web_view_go_forward (WEBKIT_WEB_VIEW(web_view)); | |
407 | } | |
408 | ||
409 | ||
b64b4e70 | 410 | bool wxWebViewWebKit::CanGoBack() |
61b98a2d SL |
411 | { |
412 | return webkit_web_view_can_go_back (WEBKIT_WEB_VIEW(web_view)); | |
413 | } | |
414 | ||
415 | ||
b64b4e70 | 416 | bool wxWebViewWebKit::CanGoForward() |
61b98a2d SL |
417 | { |
418 | return webkit_web_view_can_go_forward (WEBKIT_WEB_VIEW(web_view)); | |
419 | } | |
420 | ||
152a5808 SL |
421 | void wxWebViewWebKit::ClearHistory() |
422 | { | |
423 | WebKitWebBackForwardList* history; | |
424 | history = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(web_view)); | |
425 | webkit_web_back_forward_list_clear(history); | |
426 | } | |
427 | ||
428 | void wxWebViewWebKit::EnableHistory(bool enable) | |
429 | { | |
430 | WebKitWebBackForwardList* history; | |
431 | history = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(web_view)); | |
432 | if(enable) | |
433 | { | |
434 | webkit_web_back_forward_list_set_limit(history, m_historyLimit); | |
435 | } | |
436 | else | |
437 | { | |
438 | webkit_web_back_forward_list_set_limit(history, 0); | |
439 | } | |
440 | } | |
61b98a2d | 441 | |
19fc1a2f SL |
442 | wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewWebKit::GetBackwardHistory() |
443 | { | |
444 | wxVector<wxSharedPtr<wxWebHistoryItem> > backhist; | |
445 | WebKitWebBackForwardList* history; | |
446 | history = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(web_view)); | |
447 | GList* list = webkit_web_back_forward_list_get_back_list_with_limit(history, | |
448 | m_historyLimit); | |
449 | //We need to iterate in reverse to get the order we desire | |
450 | for(int i = g_list_length(list) - 1; i >= 0 ; i--) | |
451 | { | |
452 | WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)g_list_nth_data(list, i); | |
453 | wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem( | |
454 | webkit_web_history_item_get_uri(gtkitem), | |
455 | webkit_web_history_item_get_title(gtkitem))); | |
456 | backhist.push_back(item); | |
457 | m_historyMap[item] = gtkitem; | |
458 | } | |
459 | return backhist; | |
460 | } | |
461 | ||
462 | wxVector<wxSharedPtr<wxWebHistoryItem> > wxWebViewWebKit::GetForwardHistory() | |
463 | { | |
464 | wxVector<wxSharedPtr<wxWebHistoryItem> > forwardhist; | |
465 | WebKitWebBackForwardList* history; | |
466 | history = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(web_view)); | |
467 | GList* list = webkit_web_back_forward_list_get_forward_list_with_limit(history, | |
468 | m_historyLimit); | |
469 | for(guint i = 0; i < g_list_length(list); i++) | |
470 | { | |
471 | WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)g_list_nth_data(list, i); | |
472 | wxSharedPtr<wxWebHistoryItem> item(new wxWebHistoryItem( | |
473 | webkit_web_history_item_get_uri(gtkitem), | |
474 | webkit_web_history_item_get_title(gtkitem))); | |
475 | forwardhist.push_back(item); | |
476 | m_historyMap[item] = gtkitem; | |
477 | } | |
478 | return forwardhist; | |
479 | } | |
480 | ||
481 | void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr<wxWebHistoryItem> item) | |
482 | { | |
483 | WebKitWebHistoryItem* gtkitem = m_historyMap[item]; | |
484 | if(gtkitem) | |
485 | { | |
486 | WebKitWebBackForwardList* history; | |
487 | history = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(web_view)); | |
488 | webkit_web_back_forward_list_go_to_item(history, gtkitem); | |
489 | } | |
490 | } | |
491 | ||
ae26e17b SL |
492 | bool wxWebViewWebKit::CanCut() |
493 | { | |
494 | return webkit_web_view_can_cut_clipboard(WEBKIT_WEB_VIEW(web_view)); | |
495 | } | |
496 | ||
497 | bool wxWebViewWebKit::CanCopy() | |
498 | { | |
499 | return webkit_web_view_can_copy_clipboard(WEBKIT_WEB_VIEW(web_view)); | |
500 | } | |
501 | ||
502 | bool wxWebViewWebKit::CanPaste() | |
503 | { | |
504 | return webkit_web_view_can_paste_clipboard(WEBKIT_WEB_VIEW(web_view)); | |
505 | } | |
506 | ||
507 | void wxWebViewWebKit::Cut() | |
508 | { | |
509 | webkit_web_view_cut_clipboard(WEBKIT_WEB_VIEW(web_view)); | |
510 | } | |
511 | ||
512 | void wxWebViewWebKit::Copy() | |
513 | { | |
514 | webkit_web_view_copy_clipboard(WEBKIT_WEB_VIEW(web_view)); | |
515 | } | |
516 | ||
517 | void wxWebViewWebKit::Paste() | |
518 | { | |
519 | webkit_web_view_paste_clipboard(WEBKIT_WEB_VIEW(web_view)); | |
520 | } | |
521 | ||
97e49559 SL |
522 | bool wxWebViewWebKit::CanUndo() |
523 | { | |
524 | return webkit_web_view_can_undo(WEBKIT_WEB_VIEW(web_view)); | |
525 | } | |
526 | ||
527 | bool wxWebViewWebKit::CanRedo() | |
528 | { | |
529 | return webkit_web_view_can_redo(WEBKIT_WEB_VIEW(web_view)); | |
530 | } | |
531 | ||
532 | void wxWebViewWebKit::Undo() | |
533 | { | |
534 | webkit_web_view_undo(WEBKIT_WEB_VIEW(web_view)); | |
535 | } | |
536 | ||
537 | void wxWebViewWebKit::Redo() | |
538 | { | |
539 | webkit_web_view_redo(WEBKIT_WEB_VIEW(web_view)); | |
540 | } | |
541 | ||
b64b4e70 | 542 | wxString wxWebViewWebKit::GetCurrentURL() |
61b98a2d SL |
543 | { |
544 | // FIXME: check which encoding the web kit control uses instead of | |
545 | // assuming UTF8 (here and elsewhere too) | |
546 | return wxString::FromUTF8(webkit_web_view_get_uri( | |
547 | WEBKIT_WEB_VIEW(web_view))); | |
548 | } | |
549 | ||
550 | ||
b64b4e70 | 551 | wxString wxWebViewWebKit::GetCurrentTitle() |
61b98a2d SL |
552 | { |
553 | return wxString::FromUTF8(webkit_web_view_get_title( | |
554 | WEBKIT_WEB_VIEW(web_view))); | |
555 | } | |
556 | ||
557 | ||
b64b4e70 | 558 | wxString wxWebViewWebKit::GetPageSource() |
61b98a2d SL |
559 | { |
560 | WebKitWebFrame* frame = webkit_web_view_get_main_frame( | |
561 | WEBKIT_WEB_VIEW(web_view)); | |
562 | WebKitWebDataSource* src = webkit_web_frame_get_data_source (frame); | |
563 | ||
564 | // TODO: check encoding with | |
565 | // const gchar* | |
566 | // webkit_web_data_source_get_encoding(WebKitWebDataSource *data_source); | |
567 | return wxString(webkit_web_data_source_get_data (src)->str, wxConvUTF8); | |
568 | } | |
569 | ||
570 | ||
b64b4e70 | 571 | wxWebViewZoom wxWebViewWebKit::GetZoom() |
61b98a2d SL |
572 | { |
573 | float zoom = GetWebkitZoom(); | |
574 | ||
575 | // arbitrary way to map float zoom to our common zoom enum | |
576 | if (zoom <= 0.65) | |
577 | { | |
578 | return wxWEB_VIEW_ZOOM_TINY; | |
579 | } | |
580 | else if (zoom > 0.65 && zoom <= 0.90) | |
581 | { | |
582 | return wxWEB_VIEW_ZOOM_SMALL; | |
583 | } | |
584 | else if (zoom > 0.90 && zoom <= 1.15) | |
585 | { | |
586 | return wxWEB_VIEW_ZOOM_MEDIUM; | |
587 | } | |
588 | else if (zoom > 1.15 && zoom <= 1.45) | |
589 | { | |
590 | return wxWEB_VIEW_ZOOM_LARGE; | |
591 | } | |
592 | else if (zoom > 1.45) | |
593 | { | |
594 | return wxWEB_VIEW_ZOOM_LARGEST; | |
595 | } | |
596 | ||
597 | // to shut up compilers, this can never be reached logically | |
598 | wxASSERT(false); | |
599 | return wxWEB_VIEW_ZOOM_MEDIUM; | |
600 | } | |
601 | ||
602 | ||
b64b4e70 | 603 | void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom) |
61b98a2d SL |
604 | { |
605 | // arbitrary way to map our common zoom enum to float zoom | |
606 | switch (zoom) | |
607 | { | |
608 | case wxWEB_VIEW_ZOOM_TINY: | |
609 | SetWebkitZoom(0.6f); | |
610 | break; | |
611 | ||
612 | case wxWEB_VIEW_ZOOM_SMALL: | |
613 | SetWebkitZoom(0.8f); | |
614 | break; | |
615 | ||
616 | case wxWEB_VIEW_ZOOM_MEDIUM: | |
617 | SetWebkitZoom(1.0f); | |
618 | break; | |
619 | ||
620 | case wxWEB_VIEW_ZOOM_LARGE: | |
621 | SetWebkitZoom(1.3); | |
622 | break; | |
623 | ||
624 | case wxWEB_VIEW_ZOOM_LARGEST: | |
625 | SetWebkitZoom(1.6); | |
626 | break; | |
627 | ||
628 | default: | |
629 | wxASSERT(false); | |
630 | } | |
631 | } | |
632 | ||
b64b4e70 | 633 | void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type) |
61b98a2d SL |
634 | { |
635 | webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(web_view), | |
636 | (type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT ? | |
637 | TRUE : FALSE)); | |
638 | } | |
639 | ||
b64b4e70 | 640 | wxWebViewZoomType wxWebViewWebKit::GetZoomType() const |
61b98a2d SL |
641 | { |
642 | gboolean fczoom = webkit_web_view_get_full_content_zoom( | |
643 | WEBKIT_WEB_VIEW(web_view)); | |
644 | ||
645 | if (fczoom) return wxWEB_VIEW_ZOOM_TYPE_LAYOUT; | |
646 | else return wxWEB_VIEW_ZOOM_TYPE_TEXT; | |
647 | } | |
648 | ||
b64b4e70 | 649 | bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType) const |
61b98a2d SL |
650 | { |
651 | // this port supports all zoom types | |
652 | return true; | |
653 | } | |
654 | ||
b64b4e70 | 655 | void wxWebViewWebKit::SetPage(const wxString& html, const wxString& baseUri) |
61b98a2d SL |
656 | { |
657 | webkit_web_view_load_string (WEBKIT_WEB_VIEW(web_view), | |
658 | html.mb_str(wxConvUTF8), | |
659 | "text/html", | |
660 | "UTF-8", | |
661 | baseUri.mb_str(wxConvUTF8)); | |
662 | } | |
663 | ||
b64b4e70 | 664 | void wxWebViewWebKit::Print() |
61b98a2d SL |
665 | { |
666 | WebKitWebFrame* frame = webkit_web_view_get_main_frame( | |
667 | WEBKIT_WEB_VIEW(web_view)); | |
668 | webkit_web_frame_print (frame); | |
669 | ||
670 | // GtkPrintOperationResult webkit_web_frame_print_full | |
671 | // (WebKitWebFrame *frame, | |
672 | // GtkPrintOperation *operation, | |
673 | // GtkPrintOperationAction action, | |
674 | // GError **error); | |
675 | ||
676 | } | |
677 | ||
678 | ||
b64b4e70 | 679 | bool wxWebViewWebKit::IsBusy() |
61b98a2d SL |
680 | { |
681 | return m_busy; | |
682 | ||
683 | // This code looks nice but returns true after a page was cancelled | |
684 | /* | |
685 | WebKitLoadStatus status = webkit_web_view_get_load_status | |
686 | (WEBKIT_WEB_VIEW(web_view)); | |
687 | ||
688 | ||
689 | #if WEBKIT_CHECK_VERSION(1,1,16) | |
690 | // WEBKIT_LOAD_FAILED is new in webkit 1.1.16 | |
691 | if (status == WEBKIT_LOAD_FAILED) | |
692 | { | |
693 | return false; | |
694 | } | |
695 | #endif | |
696 | if (status == WEBKIT_LOAD_FINISHED) | |
697 | { | |
698 | return false; | |
699 | } | |
700 | ||
701 | return true; | |
702 | */ | |
703 | } | |
704 | ||
c7cbe308 SL |
705 | void wxWebViewWebKit::SetEditable(bool enable) |
706 | { | |
707 | webkit_web_view_set_editable(WEBKIT_WEB_VIEW(web_view), enable); | |
708 | } | |
709 | ||
710 | bool wxWebViewWebKit::IsEditable() | |
711 | { | |
712 | return webkit_web_view_get_editable(WEBKIT_WEB_VIEW(web_view)); | |
713 | } | |
714 | ||
63a65070 SL |
715 | void wxWebViewWebKit::DeleteSelection() |
716 | { | |
717 | webkit_web_view_delete_selection(WEBKIT_WEB_VIEW(web_view)); | |
718 | } | |
719 | ||
720 | bool wxWebViewWebKit::HasSelection() | |
721 | { | |
722 | return webkit_web_view_has_selection(WEBKIT_WEB_VIEW(web_view)); | |
723 | } | |
724 | ||
725 | void wxWebViewWebKit::SelectAll() | |
726 | { | |
727 | webkit_web_view_select_all(WEBKIT_WEB_VIEW(web_view)); | |
728 | } | |
729 | ||
c9355a3d SL |
730 | wxString wxWebViewWebKit::GetSelectedText() |
731 | { | |
732 | WebKitDOMDocument* doc; | |
733 | WebKitDOMDOMWindow* win; | |
734 | WebKitDOMDOMSelection* sel; | |
735 | WebKitDOMRange* range; | |
736 | ||
737 | doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(web_view)); | |
738 | win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc)); | |
739 | sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win)); | |
740 | range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), | |
741 | 0, NULL); | |
742 | return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range)), | |
743 | wxConvUTF8); | |
744 | } | |
63a65070 | 745 | |
97ba4d81 | 746 | wxString wxWebViewWebKit::GetSelectedSource() |
0fe8a1b6 SL |
747 | { |
748 | WebKitDOMDocument* doc; | |
749 | WebKitDOMDOMWindow* win; | |
750 | WebKitDOMDOMSelection* sel; | |
751 | WebKitDOMRange* range; | |
752 | WebKitDOMElement* div; | |
753 | WebKitDOMDocumentFragment* clone; | |
754 | WebKitDOMHTMLElement* html; | |
755 | ||
756 | doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(web_view)); | |
757 | win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc)); | |
758 | sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win)); | |
759 | range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), | |
760 | 0, NULL); | |
761 | div = webkit_dom_document_create_element(WEBKIT_DOM_DOCUMENT(doc), "div", NULL); | |
762 | ||
763 | clone = webkit_dom_range_clone_contents(WEBKIT_DOM_RANGE(range), NULL); | |
764 | webkit_dom_node_append_child(&div->parent_instance, &clone->parent_instance, NULL); | |
765 | html = (WebKitDOMHTMLElement*)div; | |
766 | ||
767 | return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html)), | |
768 | wxConvUTF8); | |
769 | } | |
770 | ||
241b769f SL |
771 | wxString wxWebViewWebKit::GetPageText() |
772 | { | |
773 | WebKitDOMDocument* doc; | |
774 | WebKitDOMHTMLElement* body; | |
775 | ||
776 | doc = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(web_view)); | |
777 | body = webkit_dom_document_get_body(WEBKIT_DOM_DOCUMENT(doc)); | |
778 | return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body)), | |
779 | wxConvUTF8); | |
780 | } | |
781 | ||
61b98a2d SL |
782 | // static |
783 | wxVisualAttributes | |
b64b4e70 | 784 | wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
61b98a2d SL |
785 | { |
786 | return GetDefaultAttributesFromGTKWidget(webkit_web_view_new); | |
787 | } | |
788 | ||
789 | ||
790 | #endif // wxHAVE_WEB_BACKEND_GTK_WEBKIT |