From 153530afb595110592776c461e52be99b1559439 Mon Sep 17 00:00:00 2001 From: Steve Lamerton Date: Sat, 30 Jul 2011 11:26:55 +0000 Subject: [PATCH] Add new wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED event. Implement for all backends, extend the sample to demonstrate it and document. Also update some copyright notices. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68458 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/webview_ie.h | 2 +- include/wx/webview.h | 7 ++++++- interface/wx/webview.h | 6 ++++++ samples/web/web.cpp | 9 +++++++++ src/common/webview.cpp | 3 ++- src/gtk/webview_webkit.cpp | 23 ++++++++++++++++++++++- src/msw/webview_ie.cpp | 9 ++++++++- src/osx/webview_webkit.mm | 10 ++++++++++ 8 files changed, 64 insertions(+), 5 deletions(-) diff --git a/include/wx/msw/webview_ie.h b/include/wx/msw/webview_ie.h index ff0500cb23..f8245644fc 100644 --- a/include/wx/msw/webview_ie.h +++ b/include/wx/msw/webview_ie.h @@ -3,7 +3,7 @@ // Purpose: wxMSW IE wxWebView backend // Author: Marianne Gagnon // Id: $Id$ -// Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton +// Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// diff --git a/include/wx/webview.h b/include/wx/webview.h index 43bb52bab8..da306c1be1 100644 --- a/include/wx/webview.h +++ b/include/wx/webview.h @@ -3,7 +3,7 @@ // Purpose: Common interface and events for web view component // Author: Marianne Gagnon // Id: $Id$ -// Copyright: (c) 2010 Marianne Gagnon +// Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -421,6 +421,7 @@ wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATED, wxW wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_LOADED, wxWebNavigationEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_ERROR, wxWebNavigationEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, wxWebNavigationEvent ); +wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, wxWebNavigationEvent ); typedef void (wxEvtHandler::*wxWebNavigationEventFunction) (wxWebNavigationEvent&); @@ -448,6 +449,10 @@ typedef void (wxEvtHandler::*wxWebNavigationEventFunction) wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \ wxHtmlNavigatingEventHandler(fn)) +#define EVT_WEB_VIEW_TITLE_CHANGED(id, fn) \ + wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, id, \ + wxHtmlNavigatingEventHandler(fn)) + #endif // wxUSE_WEB #endif // _WX_WEB_VIEW_H_ diff --git a/interface/wx/webview.h b/interface/wx/webview.h index 7d5f96da90..cf5b9bd75a 100644 --- a/interface/wx/webview.h +++ b/interface/wx/webview.h @@ -157,6 +157,9 @@ public: Process a @c wxEVT_COMMAND_WEB_VIEW_NEWWINDOW event, generated when a new window is created. You must handle this event if you want anything to happen, for example to load the page in a new window or tab. + @event{EVT_WEB_VIEW_TITLE_CHANGED(id, func)} + Process a @c wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED event, generated when + the page title changes. Use GetString to get the title. @endEventTable @library{wxweb} @@ -521,6 +524,9 @@ public: Process a @c wxEVT_COMMAND_WEB_VIEW_NEWWINDOW event, generated when a new window is created. You must handle this event if you want anything to happen, for example to load the page in a new window or tab. + @event{EVT_WEB_VIEW_TITLE_CHANGED(id, func)} + Process a @c wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED event, generated when + the page title changes. Use GetString to get the title. @endEventTable @library{wxweb} diff --git a/samples/web/web.cpp b/samples/web/web.cpp index 48d1859c4a..97f20bf99a 100644 --- a/samples/web/web.cpp +++ b/samples/web/web.cpp @@ -68,6 +68,7 @@ public: void OnNavigationComplete(wxWebNavigationEvent& evt); void OnDocumentLoaded(wxWebNavigationEvent& evt); void OnNewWindow(wxWebNavigationEvent& evt); + void OnTitleChanged(wxWebNavigationEvent& evt); void OnViewSourceRequest(wxCommandEvent& evt); void OnToolsClicked(wxCommandEvent& evt); void OnSetZoom(wxCommandEvent& evt); @@ -266,6 +267,8 @@ WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") wxWebNavigationEventHandler(WebFrame::OnError), NULL, this); Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, wxWebNavigationEventHandler(WebFrame::OnNewWindow), NULL, this); + Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, + wxWebNavigationEventHandler(WebFrame::OnTitleChanged), NULL, this); // Connect the menu events Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED, @@ -509,6 +512,12 @@ void WebFrame::OnNewWindow(wxWebNavigationEvent& evt) UpdateState(); } +void WebFrame::OnTitleChanged(wxWebNavigationEvent& evt) +{ + wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'"); + UpdateState(); +} + /** * Invoked when user selects the "View Source" menu item */ diff --git a/src/common/webview.cpp b/src/common/webview.cpp index 9c0c88f0ba..113f96ad68 100644 --- a/src/common/webview.cpp +++ b/src/common/webview.cpp @@ -3,7 +3,7 @@ // Purpose: Common interface and events for web view component // Author: Marianne Gagnon // Id: $Id$ -// Copyright: (c) 2010 Marianne Gagnon +// Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -38,6 +38,7 @@ wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_NAVIGATED, wxWebNavigationEvent ); wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_LOADED, wxWebNavigationEvent ); wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_ERROR, wxWebNavigationEvent ); wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, wxWebNavigationEvent ); +wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, wxWebNavigationEvent ); //Taken from wx/filesys.cpp static wxString EscapeFileNameCharsInURL(const char *in) diff --git a/src/gtk/webview_webkit.cpp b/src/gtk/webview_webkit.cpp index 27879488df..c1493d21e6 100644 --- a/src/gtk/webview_webkit.cpp +++ b/src/gtk/webview_webkit.cpp @@ -65,7 +65,7 @@ static gboolean wxgtk_webview_webkit_navigation(WebKitWebView*, WebKitWebFrame *frame, WebKitNetworkRequest *request, - WebKitWebNavigationAction*, + WebKitWebNavigationAction *, WebKitWebPolicyDecision *policy_decision, wxWebViewWebKit *webKitCtrl) { @@ -263,6 +263,24 @@ wxgtk_webview_webkit_new_window(WebKitWebView*, return TRUE; } +static void +wxgtk_webview_webkit_title_changed(WebKitWebView *webView, + WebKitWebFrame *frame, + gchar *title, + wxWebViewWebKit *webKitCtrl) +{ + wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, + webKitCtrl->GetId(), + webKitCtrl->GetCurrentURL(), + "", + true); + thisEvent.SetString(wxString(title, wxConvUTF8)); + + if (webKitCtrl && webKitCtrl->GetEventHandler()) + webKitCtrl->GetEventHandler()->ProcessEvent(thisEvent); + +} + } // extern "C" //----------------------------------------------------------------------------- @@ -314,6 +332,9 @@ bool wxWebViewWebKit::Create(wxWindow *parent, g_signal_connect_after(web_view, "new-window-policy-decision-requested", G_CALLBACK(wxgtk_webview_webkit_new_window), this); + g_signal_connect_after(web_view, "title-changed", + G_CALLBACK(wxgtk_webview_webkit_title_changed), this); + m_parent->DoAddChild( this ); PostCreation(size); diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 0250dd85f0..07cda9f9d5 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -3,7 +3,7 @@ // Purpose: wxMSW wxWebViewIE class implementation for web view component // Author: Marianne Gagnon // Id: $Id$ -// Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton +// Copyright: (c) 2010 Marianne Gagnon, 2011 Steven Lamerton // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -803,6 +803,13 @@ void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt) case DISPID_TITLECHANGE: { + wxString title = evt[0].GetString(); + + wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, + GetId(), GetCurrentURL(), wxEmptyString, true); + event.SetString(title); + event.SetEventObject(this); + HandleWindowEvent(event); break; } diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index daae499d70..0be86709bc 100644 --- a/src/osx/webview_webkit.mm +++ b/src/osx/webview_webkit.mm @@ -1132,6 +1132,16 @@ wxString nsErrorToWxHtmlError(NSError* error, wxWebNavigationError* out) { webKitWindow->SetPageTitle(wxStringWithNSString( title )); } + wxString target = wxStringWithNSString([frame name]); + wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, + wx_webviewctrls[sender]->GetId(), + wx_webviewctrls[sender]->GetCurrentURL(), + target, true); + + thisEvent.SetString(wxStringWithNSString(title)); + + if (webKitWindow && webKitWindow->GetEventHandler()) + webKitWindow->GetEventHandler()->ProcessEvent(thisEvent); } @end -- 2.45.2