From c420d57be0b17f7242f291d9ceec0677d87dbe51 Mon Sep 17 00:00:00 2001 From: Steve Lamerton Date: Mon, 31 Dec 2012 13:21:21 +0000 Subject: [PATCH] Add context menu enabling and disabling to wxWebView, all backends supported. Closes #14789. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73326 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gtk/webview_webkit.h | 1 + include/wx/msw/webview_ie.h | 6 +++++- include/wx/webview.h | 13 +++++++++++++ interface/wx/webview.h | 14 ++++++++++++++ samples/webview/webview.cpp | 13 +++++++++++++ src/gtk/webview_webkit.cpp | 31 +++++++++++++++++++++++++++++++ src/msw/webview_ie.cpp | 7 +++++-- src/osx/webview_webkit.mm | 9 +++++++++ 8 files changed, 91 insertions(+), 3 deletions(-) diff --git a/include/wx/gtk/webview_webkit.h b/include/wx/gtk/webview_webkit.h index 2246303c50..c42f0ad564 100644 --- a/include/wx/gtk/webview_webkit.h +++ b/include/wx/gtk/webview_webkit.h @@ -63,6 +63,7 @@ public: virtual bool CanGoBack() const; virtual bool CanGoForward() const; virtual void ClearHistory(); + virtual void EnableContextMenu(bool enable = true); virtual void EnableHistory(bool enable = true); virtual wxVector > GetBackwardHistory(); virtual wxVector > GetForwardHistory(); diff --git a/include/wx/msw/webview_ie.h b/include/wx/msw/webview_ie.h index 9e038ebe3d..0d66b0fa30 100644 --- a/include/wx/msw/webview_ie.h +++ b/include/wx/msw/webview_ie.h @@ -271,8 +271,9 @@ private: class DocHostUIHandler : public wxIDocHostUIHandler { public: - DocHostUIHandler() {}; + DocHostUIHandler(wxWebView* browser) { m_browser = browser; } ~DocHostUIHandler() {}; + virtual HRESULT wxSTDCALL ShowContextMenu(DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved); @@ -319,6 +320,9 @@ public: IDataObject **ppDORet); //IUnknown DECLARE_IUNKNOWN_METHODS; + +private: + wxWebView* m_browser; }; class wxFindPointers diff --git a/include/wx/webview.h b/include/wx/webview.h index 050c31953e..0e319d7e97 100644 --- a/include/wx/webview.h +++ b/include/wx/webview.h @@ -103,6 +103,11 @@ extern WXDLLIMPEXP_DATA_WEBVIEW(const char) wxWebViewDefaultURLStr[]; class WXDLLIMPEXP_WEBVIEW wxWebView : public wxControl { public: + wxWebView() + { + m_showMenu = true; + } + virtual ~wxWebView() {} virtual bool Create(wxWindow* parent, @@ -124,12 +129,17 @@ public: const wxString& name = wxWebViewNameStr); //General methods + virtual void EnableContextMenu(bool enable = true) + { + m_showMenu = enable; + } virtual wxString GetCurrentTitle() const = 0; virtual wxString GetCurrentURL() const = 0; // TODO: handle choosing a frame when calling GetPageSource()? virtual wxString GetPageSource() const = 0; virtual wxString GetPageText() const = 0; virtual bool IsBusy() const = 0; + virtual bool IsContextMenuEnabled() const { return m_showMenu; } virtual bool IsEditable() const = 0; virtual void LoadURL(const wxString& url) = 0; virtual void Print() = 0; @@ -197,6 +207,9 @@ public: protected: virtual void DoSetPage(const wxString& html, const wxString& baseUrl) = 0; +private: + bool m_showMenu; + wxDECLARE_ABSTRACT_CLASS(wxWebView); }; diff --git a/interface/wx/webview.h b/interface/wx/webview.h index 27ac796d41..64e7d68829 100644 --- a/interface/wx/webview.h +++ b/interface/wx/webview.h @@ -487,6 +487,20 @@ public: */ virtual void Paste() = 0; + /** + @name Context Menu + */ + + /** + Enable or disbale the right click context menu. + */ + virtual void EnableContextMenu(bool enable = true); + + /** + Returns @true if a context menu will be shown on right click. + */ + virtual bool IsContextMenuEnabled() const; + /** @name History */ diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index 8589f4cbce..a5e56df68c 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -143,6 +143,7 @@ public: void OnFindDone(wxCommandEvent& evt); void OnFindText(wxCommandEvent& evt); void OnFindOptions(wxCommandEvent& evt); + void OnEnableContextMenu(wxCommandEvent& evt); private: wxTextCtrl* m_url; @@ -188,6 +189,7 @@ private: wxMenuItem* m_selection_clear; wxMenuItem* m_selection_delete; wxMenuItem* m_find; + wxMenuItem* m_context_menu; wxInfoBar *m_info; wxStaticText* m_info_text; @@ -403,6 +405,8 @@ WebFrame::WebFrame(const wxString& url) : wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example")); wxMenuItem* usememoryfs = m_tools_menu->Append(wxID_ANY, _("Memory File System Example")); + m_context_menu = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Context Menu")); + //By default we want to handle navigation and new windows m_tools_handle_navigation->Check(); m_tools_handle_new_window->Check(); @@ -509,6 +513,8 @@ WebFrame::WebFrame(const wxString& url) : wxCommandEventHandler(WebFrame::OnUseMemoryFS), NULL, this ); Connect(m_find->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WebFrame::OnFind), NULL, this ); + Connect(m_context_menu->GetId(), wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(WebFrame::OnEnableContextMenu), NULL, this ); //Connect the idle events Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this); @@ -659,6 +665,11 @@ void WebFrame::OnUseMemoryFS(wxCommandEvent& WXUNUSED(evt)) m_browser->LoadURL("memory:page1.htm"); } +void WebFrame::OnEnableContextMenu(wxCommandEvent& evt) +{ + m_browser->EnableContextMenu(evt.IsChecked()); +} + void WebFrame::OnFind(wxCommandEvent& WXUNUSED(evt)) { wxString value = m_browser->GetSelectedText(); @@ -850,6 +861,8 @@ void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt)) m_selection_clear->Enable(m_browser->HasSelection()); m_selection_delete->Enable(m_browser->HasSelection()); + m_context_menu->Check(m_browser->IsContextMenuEnabled()); + //Firstly we clear the existing menu items, then we add the current ones wxMenuHistoryMap::const_iterator it; for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it ) diff --git a/src/gtk/webview_webkit.cpp b/src/gtk/webview_webkit.cpp index 3e9008503f..5a3ad0cb04 100644 --- a/src/gtk/webview_webkit.cpp +++ b/src/gtk/webview_webkit.cpp @@ -378,6 +378,23 @@ wxgtk_webview_webkit_resource_req(WebKitWebView *, } } +#if WEBKIT_CHECK_VERSION(1, 10, 0) + +static gboolean +wxgtk_webview_webkit_context_menu(WebKitWebView *, + GtkWidget *, + WebKitHitTestResult *, + gboolean, + wxWebViewWebKit *webKitCtrl) +{ + if(webKitCtrl->IsContextMenuEnabled()) + return FALSE; + else + return TRUE; +} + +#endif + } // extern "C" //----------------------------------------------------------------------------- @@ -433,6 +450,11 @@ bool wxWebViewWebKit::Create(wxWindow *parent, g_signal_connect_after(m_web_view, "resource-request-starting", G_CALLBACK(wxgtk_webview_webkit_resource_req), this); + +#if WEBKIT_CHECK_VERSION(1, 10, 0) + g_signal_connect_after(m_web_view, "context-menu", + G_CALLBACK(wxgtk_webview_webkit_context_menu), this); +#endif m_parent->DoAddChild( this ); @@ -926,6 +948,15 @@ void wxWebViewWebKit::RegisterHandler(wxSharedPtr handler) m_handlerList.push_back(handler); } +void wxWebViewWebKit::EnableContextMenu(bool enable) +{ +#if !WEBKIT_CHECK_VERSION(1, 10, 0) //If we are using an older version + g_object_set(webkit_web_view_get_settings(m_web_view), + "enable-default-context-menu", enable, NULL); +#endif + wxWebView::EnableContextMenu(enable); +} + long wxWebViewWebKit::Find(const wxString& text, int flags) { bool newSearch = false; diff --git a/src/msw/webview_ie.cpp b/src/msw/webview_ie.cpp index 2f6ac73c3d..093707bb17 100644 --- a/src/msw/webview_ie.cpp +++ b/src/msw/webview_ie.cpp @@ -100,7 +100,7 @@ bool wxWebViewIE::Create(wxWindow* parent, m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE); m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE); - m_uiHandler = new DocHostUIHandler; + m_uiHandler = new DocHostUIHandler(this); m_container = new wxIEContainer(this, IID_IWebBrowser2, m_webBrowser, m_uiHandler); @@ -1549,7 +1549,10 @@ HRESULT wxSTDCALL DocHostUIHandler::ShowContextMenu(DWORD dwID, POINT *ppt, wxUnusedVar(ppt); wxUnusedVar(pcmdtReserved); wxUnusedVar(pdispReserved); - return E_NOTIMPL; + if(m_browser->IsContextMenuEnabled()) + return E_NOTIMPL; + else + return S_OK; } HRESULT wxSTDCALL DocHostUIHandler::GetHostInfo(DOCHOSTUIINFO *pInfo) diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index 3b869b6f32..83b1946c89 100644 --- a/src/osx/webview_webkit.mm +++ b/src/osx/webview_webkit.mm @@ -1346,6 +1346,15 @@ wxString nsErrorToWxHtmlError(NSError* error, wxWebViewNavigationError* out) webKitWindow->Print(); } + +- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element + defaultMenuItems:(NSArray *) defaultMenuItems +{ + if(webKitWindow->IsContextMenuEnabled()) + return defaultMenuItems; + else + return nil; +} @end #endif //wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT -- 2.45.2