diff --git a/include/wx/webview.h b/include/wx/webview.h index 365e7664e4..1d69d7ec60 100644 --- a/include/wx/webview.h +++ b/include/wx/webview.h @@ -363,6 +363,7 @@ wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATING, wx wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEB, wxEVT_COMMAND_WEB_VIEW_NAVIGATED, wxWebNavigationEvent ); 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 ); typedef void (wxEvtHandler::*wxWebNavigationEventFunction) (wxWebNavigationEvent&); @@ -386,6 +387,10 @@ typedef void (wxEvtHandler::*wxWebNavigationEventFunction) wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_ERROR, id, \ wxHtmlNavigatingEventHandler(fn)) +#define EVT_WEB_VIEW_NEWWINDOW(id, fn) \ + wx__DECLARE_EVT1(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, id, \ + wxHtmlNavigatingEventHandler(fn)) + #endif // wxUSE_WEB #endif // _WX_WEB_VIEW_H_ diff --git a/interface/wx/webview.h b/interface/wx/webview.h index aec5aac7fe..f93f42d452 100644 --- a/interface/wx/webview.h +++ b/interface/wx/webview.h @@ -119,6 +119,10 @@ enum wxWebViewBackend The integer associated with this event will be a wxWebNavigationError item. The string associated with this event may contain a backend-specific more precise error message/code. + @event{EVT_WEB_VIEW_NEWWINDOW(id, func)} + Process a @c wxEVT_COMMAND_WEB_VIEW_NEWWINDOW event, generated when a new + window is created. This event may be vetoed to prevent a new window showing, + for example if you want to open the url in the existing window or a new tab. @endEventTable @library{wxweb} @@ -333,6 +337,10 @@ public: The integer associated with this event will be a wxWebNavigationError item. The string associated with this event may contain a backend-specific more precise error message/code. + @event{EVT_WEB_VIEW_NEWWINDOW(id, func)} + Process a @c wxEVT_COMMAND_WEB_VIEW_NEWWINDOW event, generated when a new + window is created. This event may be vetoed to prevent a new window showing, + for example if you want to open the url in the existing window or a new tab. @endEventTable @library{wxweb} @@ -357,24 +365,25 @@ public: */ const wxString& GetTarget() const; - // default copy ctor, assignment operator and dtor are ok virtual wxEvent* Clone() const; /** Get whether this event may be vetoed (stopped/prevented). Only - meaningful for events fired before navigation takes place. + meaningful for events fired before navigation takes place or new + window events. */ bool CanVeto() const; /** Whether this event was vetoed (stopped/prevented). Only meaningful for - events fired before navigation takes place. + events fired before navigation takes place or new window events. */ bool IsVetoed() const; /** Veto (prevent/stop) this event. Only meaningful for events fired - before navigation takes place. Only valid if CanVeto() returned true. + before navigation takes place or new window events. Only valid + if CanVeto() returned true. */ void Veto(); }; \ No newline at end of file diff --git a/samples/web/web.cpp b/samples/web/web.cpp index a3194bb8b4..7e010be1b1 100644 --- a/samples/web/web.cpp +++ b/samples/web/web.cpp @@ -315,6 +315,17 @@ public: m_browser_ctrl->GetZoom(); } + + /** + * On new window, we veto to stop extra windows appearing + */ + void onNewWindow(wxWebNavigationEvent& evt) + { + wxLogMessage("%s", "New window; url='" + evt.GetHref() + "'"); + evt.Veto(); + + updateState(); + } /** * Invoked when user selects the "View Source" menu item @@ -626,6 +637,9 @@ bool wxMiniApp::OnInit() m_browser_ctrl->Connect(m_browser_ctrl->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR, wxWebNavigationEventHandler(wxMiniApp::onError), NULL, this); + m_browser_ctrl->Connect(m_browser_ctrl->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, + wxWebNavigationEventHandler(wxMiniApp::onNewWindow), NULL, this); + frame->Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(wxMiniApp::onClose), NULL, this); Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxMiniApp::onQuitMenu), NULL, this); diff --git a/src/common/webview.cpp b/src/common/webview.cpp index f9d9553803..f575b5460b 100644 --- a/src/common/webview.cpp +++ b/src/common/webview.cpp @@ -35,6 +35,7 @@ wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_NAVIGATING, wxWebNavigationEvent ); 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 ); // static wxWebView* wxWebView::New(wxWebViewBackend backend) diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 7838686bcc..d51a3e7f41 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -658,11 +658,21 @@ void wxWebViewIE::onActiveXEvent(wxActiveXEvent& evt) } break; } - case DISPID_NEWWINDOW2: + case DISPID_NEWWINDOW3: { - wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters(); - // Cancel the attempt to open a new window - *V_BOOLREF(&nativeParams->pDispParams->rgvarg[0]) = VARIANT_TRUE; + wxString url = evt[4].GetString(); + + wxWebNavigationEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, + GetId(), url, wxEmptyString, true); + event.SetEventObject(this); + HandleWindowEvent(event); + + //If we veto the event then we cancel the new window + if (event.IsVetoed()) + { + wxActiveXEventNativeMSW* nativeParams = evt.GetNativeParameters(); + *V_BOOLREF(&nativeParams->pDispParams->rgvarg[3]) = VARIANT_TRUE; + } break; } }