webview / webview

Tiny cross-platform webview library for C/C++. Uses WebKit (GTK/Cocoa) and Edge WebView2 (Windows).

Home Page:https://webview.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to correctly render the web page?

raphael10-collab opened this issue · comments

Following the indications found here: https://wiki.gnome.org/Projects/WebKitGtk/ProgrammingGuide/Tutorial I'm trying to render a web page within a webview window:

#include "webview.h"
#include "stdlib.h"


#ifdef _WIN32
int WINAPI WinMain(HINSTANCE /*hInst*/, HINSTANCE /*hPrevInst*/,
                   LPSTR /*lpCmdLine*/, int /*nCmdShow*/) {
#else
int main(int argc, char* argv[]) {
#endif

    webview::webview w(false, nullptr);
    w.set_title("Basic Example");
    w.set_size(480, 320, WEBVIEW_HINT_NONE);
    w.set_html("Thanks for using webview!");

    WebKitWebView *wk2_webView = WEBKIT_WEB_VIEW(webkit_web_view_new());

    gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(webView));

    w.run();

    return 0;
}

But I'm getting the error : error: invalid cast from type ‘webview::webview’ to type ‘GTypeInstance*’ {aka ‘_GTypeInstance*’} :

raphy@raohy:~/webview-prj$ g++ basic.cc -std=c++11 -Ilibs/webview $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0) -o build/basic && ./build/basic
In file included from basic.cc:1:
libs/webview/webview.h: In member function ‘void webview::detail::gtk_webkit_engine::eval(const string&)’:
libs/webview/webview.h:689:35: warning: ‘void webkit_web_view_run_javascript(WebKitWebView*, const gchar*, GCancellable*, GAsyncReadyCallback, gpointer)’ is deprecated: Use 'webkit_web_view_evaluate_javascript' instead [-Wdeprecated-declarations]
  689 |     webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(m_webview), js.c_str(),
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  690 |                                    nullptr, nullptr, nullptr);
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/webkitgtk-4.0/webkit/WebKitPrintOperation.h:29,
                 from /usr/include/webkitgtk-4.0/webkit2/webkit2.h:74,
                 from libs/webview/webview.h:579,
                 from basic.cc:1:
/usr/include/webkitgtk-4.0/webkit/WebKitWebView.h:526:1: note: declared here
  526 | webkit_web_view_run_javascript                       (WebKitWebView             *web_view,
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/glib-2.0/gobject/gobject.h:24,
                 from /usr/include/glib-2.0/gobject/gbinding.h:29,
                 from /usr/include/glib-2.0/glib-object.h:22,
                 from /usr/include/glib-2.0/gio/gioenums.h:28,
                 from /usr/include/glib-2.0/gio/giotypes.h:28,
                 from /usr/include/glib-2.0/gio/gio.h:26,
                 from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:28,
                 from /usr/include/gtk-3.0/gdk/gdk.h:32,
                 from /usr/include/gtk-3.0/gtk/gtk.h:30,
                 from libs/webview/webview.h:578,
                 from basic.cc:1:
basic.cc: In function ‘int main(int, char**)’:
/usr/include/glib-2.0/gobject/gtype.h:2458:49: error: invalid cast from type ‘webview::webview’ to type ‘GTypeInstance*’ {aka ‘_GTypeInstance*’}
 2458 |     ((ct*) (void *) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
      |                                                 ^
/usr/include/glib-2.0/gobject/gtype.h:501:66: note: in expansion of macro ‘_G_TYPE_CIC’
  501 | #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type)    (_G_TYPE_CIC ((instance), (g_type), c_type))
      |                                                                  ^~~~~~~~~~~
/usr/include/gtk-3.0/gtk/gtkcontainer.h:38:42: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_CAST’
   38 | #define GTK_CONTAINER(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_CONTAINER, GtkContainer))
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
basic.cc:28:23: note: in expansion of macro ‘GTK_CONTAINER’
   28 |     gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(webView));
      |                       ^~~~~~~~~~~~~
basic.cc:28:52: error: ‘webView’ was not declared in this scope; did you mean ‘webview’?
   28 |     gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(webView));
      |                                                    ^~~~~~~
/usr/include/glib-2.0/gobject/gtype.h:2458:66: note: in definition of macro ‘_G_TYPE_CIC’
 2458 |     ((ct*) (void *) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
      |                                                                  ^~
/usr/include/gtk-3.0/gtk/gtkwidget.h:58:44: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_CAST’
   58 | #define GTK_WIDGET(widget)                (G_TYPE_CHECK_INSTANCE_CAST ((widget), GTK_TYPE_WIDGET, GtkWidget))
      |                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~
basic.cc:28:41: note: in expansion of macro ‘GTK_WIDGET’
   28 |     gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(webView));
      |                                         ^~~~~~~~~~
raphy@raohy:~/webview-prj$ 

If I create a GtkWidget* mainwindow :

int main(int argc, char* argv[]) {
#endif

    webview::webview w(false, nullptr);
    w.set_title("Basic Example");
    w.set_size(480, 320, WEBVIEW_HINT_NONE);
    w.set_html("Thanks for using webview!");

    // Create a browser instance
    WebKitWebView *wk2_webView = WEBKIT_WEB_VIEW(webkit_web_view_new());

    GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(main_window), 800, 600);

    gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(wk2_webView));

    webkit_web_view_load_uri(wk2_webView, "http://www.webkitgtk.org/");

    w.run();

    return 0;
}

I do not get any errors, but the web page is not rendered:

image

gcc version :

raphy@raohy:~/webview-prj$ gcc --version
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

O.S. : Ubuntu 22.04

How to correctly render the web page?

Why don't you just call w.navigate(...) instead of w.set_html(...) and remove all of the GTK/WebKit code?

@SteffenL . Yes! With w.navigate("https://example.org") the page is rendered

I can already go forward and backward, but to work with the browser history ?
Where can I find information about how to handle the navigate(...) properties and settings?

The most up-to-date documentation is in the source code, but currently browser history is neither implemented nor documented.

Either it would need to be implemented in this library or you would need to use API of the underlying browser engine.

Given that the original issue was answered and solved then I'm going to close this.