Mithenks
Mithenks

Reputation: 323

Example of using webkitgtk with gtkmm 3.0

I've searched a lot, but I wasn't able to find a good example explaining how to use webkitgtk with gtkmm 3.0.

Anyone knows about it? Is there some good tutorial, or sample code?

Thanks in advance!

Upvotes: 4

Views: 7352

Answers (2)

Mike
Mike

Reputation: 2761

Actually, it's a lot simpler since Web View is a widget. A bare minimum implementation might look like this:

class CWebView : public Gtk::Widget
{
public:

    CWebView ()     //  Might want to protect this
        : Gtk::Widget ((webkit_web_view_new()) {}
    virtual ~CWebView () {};

    operator WebKitWebView * () //  Allow this to stand in for a Web View
    {   return WEBKIT_WEB_VIEW(gobj()); }

    //  Wrap any functions you want to use like this:
    void load_uri (const gchar *strUri)
    {   webkit_web_view_load_uri(*this, strUri);    }  
    // Note that, thanks to the cast operator, *this can
    // replace WebKitWebView pointers
};

So the relevant parts of the above code would be:

CWebView   *pView = new CWebView;
window.add (*pView);
pView->load_uri ("http://stackoverflow.com/questions/17039942/example-of-using-webkitgtk-with-gtkmm-3-0");

You might want to protect the constructor and add a static create () function to make sure it's always allocated with new () and you might want to allow it to "wrap" existing web_view objects instead of always allocating a new one but you get the picture.

Upvotes: 5

The Unholy Metal Machine
The Unholy Metal Machine

Reputation: 1103

I know this question is quiet old but I just made a small program using webkitgtk and gtkmm-3. It could be useful for some other people, so I share it :

#include <gtkmm.h>
#include <webkit2/webkit2.h>

int main( int argc
        , char **argv
        )
{
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create( argc, argv, "" );

  Gtk::Window window;
  window.set_default_size( 800, 600 );

  WebKitWebView * one =  WEBKIT_WEB_VIEW( webkit_web_view_new() );
  /*
   * the next line does some tricks :
   * GTK_WIDGET( one ) -> convert WebKitWebView to GtkWidget (one->two)
   * Glib::wrap( GTK_WIDGET( one ) ) -> convert GtkWidget to Gtk::Widget (two->three)
   */
  Gtk::Widget * three = Glib::wrap( GTK_WIDGET( one ) );

  window.add( *three );
  webkit_web_view_load_uri(one, "http://stackoverflow.com/questions/17039942/example-of-using-webkitgtk-with-gtkmm-3-0");

  window.show_all();

  app->run( window );  
  exit( 0 );
}

I can't tell if it's the good way to do it, but it works so far!

compile command :

$ g++ main.cc `pkg-config gtkmm-3.0 --libs --cflags` `pkg-config webkit2gtk-4.0 --libs --cflags` -std=c++1

for MS Windows users, I'm not racist, but I don't know how to compile on windows. Any comment would be apreciate, I made this piece of code by my own. Thx to report any error.

you should see something like this : enter image description here

take care of using a version of webkit2gtk using gtk3 otherwise you'll get an error :

 (a.out:5783): Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported

Upvotes: 11

Related Questions