X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7cf17ea27ba51d7b19cbae3cf21be6927852e07d..bb996f289574defb0ae4339ae8e46ff3cf6fa54c:/src/gtk/webview_webkit.cpp?ds=sidebyside diff --git a/src/gtk/webview_webkit.cpp b/src/gtk/webview_webkit.cpp index ca6fc05739..28754d298a 100644 --- a/src/gtk/webview_webkit.cpp +++ b/src/gtk/webview_webkit.cpp @@ -18,6 +18,7 @@ #include "wx/gtk/private.h" #include "wx/filesys.h" #include "wx/base64.h" +#include "wx/log.h" #include // ---------------------------------------------------------------------------- @@ -28,7 +29,7 @@ extern "C" { static void -wxgtk_webview_webkit_load_status(GtkWidget* widget, +wxgtk_webview_webkit_load_status(GtkWidget* widget, GParamSpec*, wxWebViewWebKit *webKitCtrl) { @@ -88,7 +89,7 @@ wxgtk_webview_webkit_navigation(WebKitWebView *, if(webKitCtrl->m_guard) { webKitCtrl->m_guard = false; - //We set this to make sure that we don't try to load the page again from + //We set this to make sure that we don't try to load the page again from //the resource request callback webKitCtrl->m_vfsurl = webkit_network_request_get_uri(request); webkit_web_policy_decision_use(policy_decision); @@ -128,7 +129,7 @@ wxgtk_webview_webkit_navigation(WebKitWebView *, handler = (*it); } } - //If we found a handler we can then use it to load the file directly + //If we found a handler we can then use it to load the file directly //ourselves if(handler) { @@ -337,10 +338,10 @@ wxgtk_webview_webkit_resource_req(WebKitWebView *, wxWebViewWebKit *webKitCtrl) { wxString uri = webkit_network_request_get_uri(request); - + wxSharedPtr handler; wxVector > hanlders = webKitCtrl->GetHandlers(); - + //We are not vetoed so see if we match one of the additional handlers for(wxVector >::iterator it = hanlders.begin(); it != hanlders.end(); ++it) @@ -350,7 +351,7 @@ wxgtk_webview_webkit_resource_req(WebKitWebView *, handler = (*it); } } - //If we found a handler we can then use it to load the file directly + //If we found a handler we can then use it to load the file directly //ourselves if(handler) { @@ -373,7 +374,7 @@ wxgtk_webview_webkit_resource_req(WebKitWebView *, //Then we can redirect the call webkit_network_request_set_uri(request, path.utf8_str()); } - + } } @@ -385,6 +386,11 @@ wxgtk_webview_webkit_resource_req(WebKitWebView *, wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxWebView); +wxWebViewWebKit::wxWebViewWebKit() +{ + m_web_view = NULL; +} + bool wxWebViewWebKit::Create(wxWindow *parent, wxWindowID id, const wxString &url, @@ -395,6 +401,11 @@ bool wxWebViewWebKit::Create(wxWindow *parent, { m_busy = false; m_guard = false; + FindClear(); + + // We currently unconditionally impose scrolling in both directions as it's + // necessary to show arbitrary pages. + style |= wxHSCROLL | wxVSCROLL; if (!PreCreation( parent, pos, size ) || !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) @@ -403,18 +414,14 @@ bool wxWebViewWebKit::Create(wxWindow *parent, return false; } - m_widget = gtk_scrolled_window_new(NULL, NULL); - g_object_ref(m_widget); m_web_view = WEBKIT_WEB_VIEW(webkit_web_view_new()); - - /* Place the WebKitWebView in the GtkScrolledWindow */ - gtk_container_add(GTK_CONTAINER(m_widget), GTK_WIDGET(m_web_view)); - gtk_widget_show(GTK_WIDGET(m_web_view)); + GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view)); + g_object_ref(m_widget); g_signal_connect_after(m_web_view, "navigation-policy-decision-requested", G_CALLBACK(wxgtk_webview_webkit_navigation), this); - g_signal_connect_after(m_web_view, "load-error", + g_signal_connect_after(m_web_view, "load-error", G_CALLBACK(wxgtk_webview_webkit_error), this); @@ -447,12 +454,18 @@ bool wxWebViewWebKit::Create(wxWindow *parent, return true; } +wxWebViewWebKit::~wxWebViewWebKit() +{ + if (m_web_view) + GTKDisconnect(m_web_view); +} + bool wxWebViewWebKit::Enable( bool enable ) { if (!wxControl::Enable(enable)) return false; - gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); + gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable); //if (enable) // GTKFixSensitivity(); @@ -555,10 +568,10 @@ void wxWebViewWebKit::EnableHistory(bool enable) wxVector > wxWebViewWebKit::GetBackwardHistory() { - wxVector > backhist; + wxVector > backhist; WebKitWebBackForwardList* history; history = webkit_web_view_get_back_forward_list(m_web_view); - GList* list = webkit_web_back_forward_list_get_back_list_with_limit(history, + GList* list = webkit_web_back_forward_list_get_back_list_with_limit(history, m_historyLimit); //We need to iterate in reverse to get the order we desire for(int i = g_list_length(list) - 1; i >= 0 ; i--) @@ -576,10 +589,10 @@ wxVector > wxWebViewWebKit::GetBackwardHistory wxVector > wxWebViewWebKit::GetForwardHistory() { - wxVector > forwardhist; + wxVector > forwardhist; WebKitWebBackForwardList* history; history = webkit_web_view_get_back_forward_list(m_web_view); - GList* list = webkit_web_back_forward_list_get_forward_list_with_limit(history, + GList* list = webkit_web_back_forward_list_get_forward_list_with_limit(history, m_historyLimit); for(guint i = 0; i < g_list_length(list); i++) { @@ -599,7 +612,7 @@ void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr item) WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)item->m_histItem; if(gtkitem) { - webkit_web_view_go_to_back_forward_item(m_web_view, + webkit_web_view_go_to_back_forward_item(m_web_view, WEBKIT_WEB_HISTORY_ITEM(gtkitem)); } } @@ -763,7 +776,7 @@ bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType) const return true; } -void wxWebViewWebKit::SetPage(const wxString& html, const wxString& baseUri) +void wxWebViewWebKit::DoSetPage(const wxString& html, const wxString& baseUri) { webkit_web_view_load_string (m_web_view, html.mb_str(wxConvUTF8), @@ -839,7 +852,7 @@ void wxWebViewWebKit::SelectAll() wxString wxWebViewWebKit::GetSelectedText() const { - WebKitDOMDocument* doc; + WebKitDOMDocument* doc; WebKitDOMDOMWindow* win; WebKitDOMDOMSelection* sel; WebKitDOMRange* range; @@ -847,15 +860,15 @@ wxString wxWebViewWebKit::GetSelectedText() const doc = webkit_web_view_get_dom_document(m_web_view); win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc)); sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win)); - range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), + range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), 0, NULL); - return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range)), + return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range)), wxConvUTF8); } wxString wxWebViewWebKit::GetSelectedSource() const { - WebKitDOMDocument* doc; + WebKitDOMDocument* doc; WebKitDOMDOMWindow* win; WebKitDOMDOMSelection* sel; WebKitDOMRange* range; @@ -866,7 +879,7 @@ wxString wxWebViewWebKit::GetSelectedSource() const doc = webkit_web_view_get_dom_document(m_web_view); win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc)); sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win)); - range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), + range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), 0, NULL); div = webkit_dom_document_create_element(WEBKIT_DOM_DOCUMENT(doc), "div", NULL); @@ -874,13 +887,13 @@ wxString wxWebViewWebKit::GetSelectedSource() const webkit_dom_node_append_child(&div->parent_instance, &clone->parent_instance, NULL); html = (WebKitDOMHTMLElement*)div; - return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html)), + return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html)), wxConvUTF8); } void wxWebViewWebKit::ClearSelection() { - WebKitDOMDocument* doc; + WebKitDOMDocument* doc; WebKitDOMDOMWindow* win; WebKitDOMDOMSelection* sel; @@ -893,18 +906,18 @@ void wxWebViewWebKit::ClearSelection() wxString wxWebViewWebKit::GetPageText() const { - WebKitDOMDocument* doc; + WebKitDOMDocument* doc; WebKitDOMHTMLElement* body; doc = webkit_web_view_get_dom_document(m_web_view); body = webkit_dom_document_get_body(WEBKIT_DOM_DOCUMENT(doc)); - return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body)), + return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body)), wxConvUTF8); } void wxWebViewWebKit::RunScript(const wxString& javascript) { - webkit_web_view_execute_script(m_web_view, + webkit_web_view_execute_script(m_web_view, javascript.mb_str(wxConvUTF8)); } @@ -913,6 +926,80 @@ void wxWebViewWebKit::RegisterHandler(wxSharedPtr handler) m_handlerList.push_back(handler); } +long wxWebViewWebKit::Find(const wxString& text, int flags) +{ + bool newSearch = false; + if(text != m_findText || + (flags & wxWEB_VIEW_FIND_MATCH_CASE) != (m_findFlags & wxWEB_VIEW_FIND_MATCH_CASE)) + { + newSearch = true; + //If it is a new search we need to clear existing highlights + webkit_web_view_unmark_text_matches(m_web_view); + webkit_web_view_set_highlight_text_matches(m_web_view, false); + } + + m_findFlags = flags; + m_findText = text; + + //If the search string is empty then we clear any selection and highlight + if(text == "") + { + webkit_web_view_unmark_text_matches(m_web_view); + webkit_web_view_set_highlight_text_matches(m_web_view, false); + ClearSelection(); + return wxNOT_FOUND; + } + + bool wrap = false, matchCase = false, forward = true; + if(flags & wxWEB_VIEW_FIND_WRAP) + wrap = true; + if(flags & wxWEB_VIEW_FIND_MATCH_CASE) + matchCase = true; + if(flags & wxWEB_VIEW_FIND_BACKWARDS) + forward = false; + + if(newSearch) + { + //Initially we mark the matches to know how many we have + m_findCount = webkit_web_view_mark_text_matches(m_web_view, wxGTK_CONV(text), matchCase, 0); + //In this case we return early to match IE behaviour + m_findPosition = -1; + return m_findCount; + } + else + { + if(forward) + m_findPosition++; + else + m_findPosition--; + if(m_findPosition < 0) + m_findPosition += m_findCount; + if(m_findPosition > m_findCount) + m_findPosition -= m_findCount; + } + + //Highlight them if needed + bool highlight = flags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT ? true : false; + webkit_web_view_set_highlight_text_matches(m_web_view, highlight); + + if(!webkit_web_view_search_text(m_web_view, wxGTK_CONV(text), matchCase, forward, wrap)) + { + m_findPosition = -1; + ClearSelection(); + return wxNOT_FOUND; + } + wxLogMessage(wxString::Format("Returning %d", m_findPosition)); + return newSearch ? m_findCount : m_findPosition; +} + +void wxWebViewWebKit::FindClear() +{ + m_findCount = 0; + m_findFlags = 0; + m_findText = ""; + m_findPosition = -1; +} + // static wxVisualAttributes wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))