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