From f94a927e9c4cc9cc54bb4d06523f98b651efb4d6 Mon Sep 17 00:00:00 2001 From: gambas Date: Tue, 26 Jan 2021 16:28:47 +0100 Subject: [PATCH] WebView: Implementing the Menu event automatically disables the default popup menu. [GB.GTK3.WEBVIEW] * NEW: WebView: Implementing the Menu event automatically disables the default popup menu. [GB.QT4.WEBVIEW] * NEW: WebView: Implementing the Menu event automatically disables the default popup menu. [GB.QT5.WEBVIEW] * NEW: WebView: Implementing the Menu event automatically disables the default popup menu. --- gb.gtk3/src/webview/c_webview.c | 11 +++++++++++ gb.qt4/src/webview/c_webview.cpp | 15 +++++++++++++++ gb.qt4/src/webview/c_webview.h | 1 + gb.qt5/src/webview/c_webview.cpp | 22 +++++++++++++++++----- gb.qt5/src/webview/c_webview.h | 3 ++- 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/gb.gtk3/src/webview/c_webview.c b/gb.gtk3/src/webview/c_webview.c index 0ab26a13c..87e4452bf 100644 --- a/gb.gtk3/src/webview/c_webview.c +++ b/gb.gtk3/src/webview/c_webview.c @@ -53,6 +53,8 @@ DECLARE_EVENT(EVENT_ERROR); DECLARE_EVENT(EVENT_LINK); DECLARE_EVENT(EVENT_NEW_VIEW); +static int EVENT_MENU = -1; + static void cb_title(WebKitWebView *widget, GParamSpec *pspec, CWEBVIEW *_object) { GB.Raise(THIS, EVENT_TITLE, 0); @@ -212,6 +214,14 @@ static void cb_javascript_finished(WebKitWebView *widget, GAsyncResult *result, THIS->js_running = FALSE; } +static gboolean cb_context_menu(WebKitWebView *web_view, WebKitContextMenu *context_menu, GdkEvent *event, WebKitHitTestResult *hit_test_result, void *_object) +{ + if (EVENT_MENU < 0) + EVENT_MENU = GB.GetEvent(GB.GetClass(THIS), "Menu"); + + return GB.CanRaise(THIS, EVENT_MENU); +} + //--------------------------------------------------------------------------- #define must_patch(_widget) (true) @@ -244,6 +254,7 @@ static void create_widget(void *_object, void *parent) g_signal_connect(G_OBJECT(WIDGET), "mouse-target-changed", G_CALLBACK(cb_link), (gpointer)THIS); g_signal_connect(G_OBJECT(WIDGET), "create", G_CALLBACK(cb_create), (gpointer)THIS); g_signal_connect(G_OBJECT(WIDGET), "decide-policy", G_CALLBACK(cb_decide_policy), (gpointer)THIS); + g_signal_connect(G_OBJECT(WIDGET), "context-menu", G_CALLBACK(cb_context_menu), (gpointer)THIS); WEBVIEW_init_settings(THIS); } diff --git a/gb.qt4/src/webview/c_webview.cpp b/gb.qt4/src/webview/c_webview.cpp index e757a3e6e..cb1d8f173 100644 --- a/gb.qt4/src/webview/c_webview.cpp +++ b/gb.qt4/src/webview/c_webview.cpp @@ -53,6 +53,8 @@ DECLARE_EVENT(EVENT_ERROR); DECLARE_EVENT(EVENT_LINK); DECLARE_EVENT(EVENT_NEW_VIEW); +static int EVENT_MENU = -1; + //static QNetworkAccessManager *_network_access_manager = 0; static CWEBVIEW *_network_access_manager_view = 0; //static QT_COLOR_FUNC _old_after_set_color; @@ -528,6 +530,19 @@ QWebView *MyWebView::createWindow(QWebPage::WebWindowType type) return new_view; } +void MyWebEngineView::contextMenuEvent(QContextMenuEvent *event) +{ + void *_object = QT.GetObject(this); + + if (EVENT_MENU < 0) + EVENT_MENU = GB.GetEvent(GB.GetClass(THIS), "Menu"); + + if (!GB.CanRaise(THIS, EVENT_MENU)) + QWebView::contextMenuEvent(event); + + event->accept(); +} + /***************************************************************************/ CWebView CWebView::manager; diff --git a/gb.qt4/src/webview/c_webview.h b/gb.qt4/src/webview/c_webview.h index 9eb9191fb..f1ebef0e6 100644 --- a/gb.qt4/src/webview/c_webview.h +++ b/gb.qt4/src/webview/c_webview.h @@ -72,6 +72,7 @@ public: protected: virtual QWebView *createWindow(QWebPage::WebWindowType type); + virtual void contextMenuEvent(QContextMenuEvent *event); }; typedef diff --git a/gb.qt5/src/webview/c_webview.cpp b/gb.qt5/src/webview/c_webview.cpp index 795716df8..a0ce8d7d3 100644 --- a/gb.qt5/src/webview/c_webview.cpp +++ b/gb.qt5/src/webview/c_webview.cpp @@ -31,6 +31,7 @@ #include */ #include #include +#include /*#include "ccookiejar.h" #include "cwebsettings.h" @@ -65,6 +66,7 @@ DECLARE_EVENT(EVENT_ERROR); DECLARE_EVENT(EVENT_LINK); DECLARE_EVENT(EVENT_NEW_VIEW); +static int EVENT_MENU = -1; /*DECLARE_EVENT(EVENT_CLICK); DECLARE_EVENT(EVENT_LINK); DECLARE_EVENT(EVENT_NEW_FRAME); @@ -72,6 +74,7 @@ DECLARE_EVENT(EVENT_AUTH); DECLARE_EVENT(EVENT_DOWNLOAD);*/ + /* static QNetworkAccessManager *_network_access_manager = 0; static CWEBVIEW *_network_access_manager_view = 0; @@ -311,11 +314,6 @@ BEGIN_METHOD_VOID(WebView_free) END_METHOD -/*BEGIN_METHOD_VOID(WebView_init) - - _old_after_set_color = QT.AfterSetColor(after_set_color); - -END_METHOD*/ /*BEGIN_METHOD_VOID(WebView_exit) @@ -964,6 +962,20 @@ QWebEngineView *MyWebEngineView::createWindow(QWebEnginePage::WebWindowType type return new_view; } +void MyWebEngineView::contextMenuEvent(QContextMenuEvent *event) +{ + void *_object = QT.GetObject(this); + + if (EVENT_MENU < 0) + EVENT_MENU = GB.GetEvent(GB.GetClass(THIS), "Menu"); + + if (!GB.CanRaise(THIS, EVENT_MENU)) + QWebEngineView::contextMenuEvent(event); + + event->accept(); +} + + //------------------------------------------------------------------------- MyWebPage::MyWebPage(QObject *parent) : QWebEnginePage(parent) diff --git a/gb.qt5/src/webview/c_webview.h b/gb.qt5/src/webview/c_webview.h index e7459860c..e9f935513 100644 --- a/gb.qt5/src/webview/c_webview.h +++ b/gb.qt5/src/webview/c_webview.h @@ -81,7 +81,8 @@ public: MyWebEngineView(QWidget *parent); protected: - + + virtual void contextMenuEvent(QContextMenuEvent *event); virtual QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); };