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