diff --git a/gb.gtk3/src/webview/c_webview.c b/gb.gtk3/src/webview/c_webview.c index 959d86f5c..fc7bd73b0 100644 --- a/gb.gtk3/src/webview/c_webview.c +++ b/gb.gtk3/src/webview/c_webview.c @@ -298,6 +298,28 @@ static gboolean cb_context_menu(WebKitWebView *web_view, WebKitContextMenu *cont return GB.CanRaise(THIS, EVENT_MENU); } +static void update_language(void *_object) +{ + if (THIS->language && *THIS->language) + { + gchar **languages = g_strsplit(THIS->language, ",", -1); + webkit_web_context_set_preferred_languages(THIS->context, (const gchar **)languages); + g_strfreev(languages); + } + else + { + const gchar *languages[2] = { NULL, NULL }; + char *lang, *p; + + lang = g_strdup(GB.System.Language()); + p = index(lang, '_'); + if (p) *p = '-'; + languages[0] = lang; + webkit_web_context_set_preferred_languages(THIS->context, languages); + g_free(lang); + } +} + //--------------------------------------------------------------------------- #define must_patch(_widget) (true) @@ -307,7 +329,8 @@ PATCH_DECLARE(WEBKIT_TYPE_WEB_VIEW) static void create_widget(void *_object, void *parent) { - THIS->widget = webkit_web_view_new(); + THIS->context = webkit_web_context_new_ephemeral(); + THIS->widget = webkit_web_view_new_with_context(THIS->context); GTK.CreateControl(THIS, parent, THIS->widget, CCF_HAS_INPUT_METHOD); @@ -340,16 +363,20 @@ static void create_widget(void *_object, void *parent) BEGIN_METHOD(WebView_new, GB_OBJECT parent) create_widget(THIS, VARG(parent)); + update_language(THIS); END_METHOD BEGIN_METHOD_VOID(WebView_free) GB.FreeString(&THIS->link); + GB.FreeString(&THIS->language); GB.Unref(POINTER(&THIS->icon)); GB.Unref(POINTER(&THIS->new_view)); + g_object_unref(THIS->context); + END_METHOD BEGIN_PROPERTY(WebView_Url) @@ -489,7 +516,19 @@ BEGIN_METHOD_VOID(WebView_GetHtml) run_callback(THIS, "Unable to retrieve HTML contents: &1"); - END_METHOD +END_METHOD + +BEGIN_PROPERTY(WebView_Language) + + if (READ_PROPERTY) + GB.ReturnString(THIS->language); + else + { + GB.StoreString(PROP(GB_STRING), &THIS->language); + update_language(THIS); + } + +END_PROPERTY //--------------------------------------------------------------------------- @@ -605,6 +644,7 @@ GB_DESC WebViewDesc[] = GB_PROPERTY_READ("Progress", "f", WebView_Progress), GB_PROPERTY("NewView", "WebView", WebView_NewView), GB_PROPERTY_READ("Link", "s", WebView_Link), + GB_PROPERTY("Language", "s", WebView_Language), GB_METHOD("SetHtml", NULL, WebView_SetHtml, "(Html)s[(Root)s]"), GB_METHOD("GetHtml", "s", WebView_GetHtml, NULL), diff --git a/gb.gtk3/src/webview/c_webview.h b/gb.gtk3/src/webview/c_webview.h index 1d2613b67..adec25d70 100644 --- a/gb.gtk3/src/webview/c_webview.h +++ b/gb.gtk3/src/webview/c_webview.h @@ -31,11 +31,13 @@ typedef struct { GTK_CONTROL control; GtkWidget *widget; + WebKitWebContext *context; WebKitBackForwardListItem *item; GTK_PICTURE icon; void *new_view; char *link; char *cb_result; + char *language; unsigned error : 1; unsigned accept_next : 1; unsigned got_load_event : 1; diff --git a/gb.qt4/src/webview/c_webview.cpp b/gb.qt4/src/webview/c_webview.cpp index 923e66715..60ee646d6 100644 --- a/gb.qt4/src/webview/c_webview.cpp +++ b/gb.qt4/src/webview/c_webview.cpp @@ -128,6 +128,11 @@ static void set_link(void *_object, const QString &link) THIS->link = QT.NewString(link); } +static void update_language(void *_object) +{ + // TODO: it seems that Qt4 QWebView does not support setting the language. +} + //------------------------------------------------------------------------- BEGIN_METHOD(WebView_new, GB_OBJECT parent) @@ -180,6 +185,9 @@ BEGIN_METHOD(WebView_new, GB_OBJECT parent) /*QObject::connect(wid->page()->networkAccessManager(), SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)), &CWebView::manager, SLOT(authenticationRequired(QNetworkReply *, QAuthenticator *)));*/ + + update_language(THIS); + END_METHOD BEGIN_METHOD_VOID(WebView_free) @@ -190,6 +198,7 @@ BEGIN_METHOD_VOID(WebView_free) //GB.FreeString(&THIS->status); //GB.FreeString(&THIS->userAgent); GB.FreeString(&THIS->link); + GB.FreeString(&THIS->language); GB.Unref(POINTER(&THIS->icon)); GB.Unref(POINTER(&THIS->new_view)); @@ -342,6 +351,17 @@ BEGIN_METHOD_VOID(WebView_GetHtml) END_METHOD +BEGIN_PROPERTY(WebView_Language) + + if (READ_PROPERTY) + GB.ReturnString(THIS->language); + else + { + GB.StoreString(PROP(GB_STRING), &THIS->language); + update_language(THIS); + } + +END_PROPERTY //------------------------------------------------------------------------- @@ -500,11 +520,12 @@ GB_DESC WebViewDesc[] = GB_PROPERTY_READ("Progress", "f", WebView_Progress), GB_PROPERTY("NewView", "WebView", WebView_NewView), GB_PROPERTY_READ("Link", "s", WebView_Link), + GB_PROPERTY("Language", "s", WebView_Language), GB_METHOD("SetHtml", NULL, WebView_SetHtml, "(Html)s[(Root)s]"), GB_METHOD("GetHtml", "s", WebView_GetHtml, NULL), - GB_METHOD("Clear", NULL, WebView_Clear, NULL), + GB_METHOD("Clear", NULL, WebView_Clear, NULL), GB_METHOD("Back", NULL, WebView_Back, NULL), GB_METHOD("Forward", NULL, WebView_Forward, NULL), diff --git a/gb.qt4/src/webview/c_webview.h b/gb.qt4/src/webview/c_webview.h index d6a159f15..d23ba9502 100644 --- a/gb.qt4/src/webview/c_webview.h +++ b/gb.qt4/src/webview/c_webview.h @@ -83,6 +83,7 @@ typedef QT_PICTURE icon; void *new_view; char *link; + char *language; int history; int progress; unsigned cancel : 1; diff --git a/gb.qt5/src/webview/c_webview.cpp b/gb.qt5/src/webview/c_webview.cpp index b1027c128..02d93c793 100644 --- a/gb.qt5/src/webview/c_webview.cpp +++ b/gb.qt5/src/webview/c_webview.cpp @@ -284,6 +284,18 @@ static void cb_html_finished(const QString &result) _cb_running = FALSE; } +static void update_language(void *_object) +{ + QString lang; + + if (THIS->language && *THIS->language) + lang = TO_QSTRING(THIS->language); + else + lang = TO_QSTRING(GB.System.Language()).replace("_", "-"); + + WIDGET->page()->profile()->setHttpAcceptLanguage(lang); +} + //------------------------------------------------------------------------- BEGIN_METHOD_VOID(WebView_exit) @@ -333,6 +345,7 @@ BEGIN_METHOD(WebView_new, GB_OBJECT parent) //QObject::connect(wid, SIGNAL(destroyed()), &WebViewSignalManager::manager, SLOT(destroy())); wid->clearPage(false); + update_language(THIS); //QObject::connect(wid, SIGNAL(linkClicked(const QUrl &)), &CWebView::manager, SLOT(linkClicked(const QUrl &))); #if 0 @@ -354,6 +367,7 @@ END_METHOD BEGIN_METHOD_VOID(WebView_free) GB.FreeString(&THIS->link); + GB.FreeString(&THIS->language); GB.Unref(POINTER(&THIS->icon)); GB.Unref(POINTER(&THIS->new_view)); @@ -507,6 +521,18 @@ BEGIN_METHOD_VOID(WebView_GetHtml) END_METHOD +BEGIN_PROPERTY(WebView_Language) + + if (READ_PROPERTY) + GB.ReturnString(THIS->language); + else + { + GB.StoreString(PROP(GB_STRING), &THIS->language); + update_language(THIS); + } + +END_PROPERTY + #if 0 BEGIN_PROPERTY(WebView_HTML) @@ -967,6 +993,7 @@ GB_DESC WebViewDesc[] = GB_PROPERTY_READ("Progress", "f", WebView_Progress), GB_PROPERTY("NewView", "WebView", WebView_NewView), GB_PROPERTY_READ("Link", "s", WebView_Link), + GB_PROPERTY("Language", "s", WebView_Language), GB_METHOD("SetHtml", NULL, WebView_SetHtml, "(Html)s[(Root)s]"), GB_METHOD("GetHtml", "s", WebView_GetHtml, NULL), @@ -1004,8 +1031,15 @@ GB_DESC WebViewDesc[] = MyWebEngineView::MyWebEngineView(QWidget *parent) : QWebEngineView(parent) { + profile = new QWebEngineProfile(this); //settings()->setFontFamily(QWebSettings::FixedFont, "monospace"); - setPage(new MyWebPage(this)); + //setPage(new MyWebPage(profile, this)); +} + +MyWebEngineView::~MyWebEngineView() +{ + delete page(); + delete profile; } void MyWebEngineView::clearPage(bool destroy) @@ -1014,7 +1048,7 @@ void MyWebEngineView::clearPage(bool destroy) if (destroy) p = page(); - setPage(new MyWebPage(this)); + setPage(new MyWebPage(profile, this)); QObject::connect(page(), SIGNAL(linkHovered(const QString &)), &WebViewSignalManager::manager, SLOT(linkHovered(const QString &))); if (destroy && p) @@ -1054,7 +1088,7 @@ void MyWebEngineView::contextMenuEvent(QContextMenuEvent *event) //------------------------------------------------------------------------- -MyWebPage::MyWebPage(QObject *parent) : QWebEnginePage(parent) +MyWebPage::MyWebPage(QWebEngineProfile *profile, QObject *parent) : QWebEnginePage(profile, parent) { } diff --git a/gb.qt5/src/webview/c_webview.h b/gb.qt5/src/webview/c_webview.h index e7b1cfd0a..3f112b7ce 100644 --- a/gb.qt5/src/webview/c_webview.h +++ b/gb.qt5/src/webview/c_webview.h @@ -25,6 +25,7 @@ #define __C_WEBVIEW_H #include +#include #include #include @@ -56,7 +57,7 @@ typedef char *link; int history; int progress; - char *cb_result; + char *language; unsigned cancel : 1; } CWEBVIEW; @@ -67,7 +68,7 @@ class MyWebPage : public QWebEnginePage public: - MyWebPage(QObject *parent); + MyWebPage(QWebEngineProfile *profile, QObject *parent); protected: @@ -81,12 +82,18 @@ class MyWebEngineView : public QWebEngineView public: MyWebEngineView(QWidget *parent); + ~MyWebEngineView(); void clearPage(bool destroy); protected: virtual void contextMenuEvent(QContextMenuEvent *event); virtual QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); + + +private: + + QWebEngineProfile *profile; }; class WebViewSignalManager : public QObject