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