s h a n a 's  h i d e o u t



home
programming
cats
tarot
books
rants
links
hit me

 

Browser Embedding

Let's start this one fast with a quick example of how to embed WebKit in a Gtk# app in 12 lines of code (well, 12 lines not counting class declarations and stuff... but who's counting anyway? :P)

using System;
using Gtk;
using WebKit;

namespace browserapp {
    class browser {

        public static void Main () {
            Application.Init ();
            Window window = new Window ("a browser in 12 lines...");
            window.Destroyed += delegate (object sender, EventArgs e) {
                Application.Quit ();
            };
            ScrolledWindow scrollWindow = new ScrolledWindow ();
            WebView webView = new WebView ();
            webView.Open ("http://mono-project.com");
            scrollWindow.Add (webView);
            window.Add (scrollWindow);
            window.ShowAll ();
            Application.Run ();
        }
    }
}

Pretty easy eh? This just creates a new top level Window, creates a ScrolledWindow, which is just a container that adds scrollbars, creates a WebView - our friendly neighbourhood WebKit widget, adds it to the ScrolledWindow so you have nice pretty scrollbars, adds the ScrolledWindow to the Window, and shows everything. In between, it also hooks up the Destroyed event of the Window, so we can quit the application when it gets destroyed, and opens an url on the webview so it starts loading immediately.


Building with MonoDevelop

If you want to do it from a nice ide, install the monodevelop package, which can be found in the same repository as mono (see Requirements below).

On MonoDevelop, create a new empty c# project, create a new class file and put the above code there.
Right-click on References in the project file list (on the left) and add the references: System, gtk-sharp and webkit-sharp.
Right-click on the project and make sure Compiler Options -> Target is set to Executable with GUI.
Build and Run.


Building on the command line

From the command line, assuming your source file is named "browser.cs", build:

gmcs -pkg:gtk-sharp-2.0 -pkg:webkit-sharp-1.0 browser.cs

Run:

mono browser.exe


Requirements

This simple example requires you to have Mono installed, obviously. It also requires Gtk# and WebKit.

OpenSuse, SLES, RedHat: http://www.go-mono.com/mono-downloads/download.html. WebKit and webkit-sharp are not here, see below.

OpenSuse 11: zypper install mono-devel gtk-sharp2 webkit-sharp libwebkit

  • Mono and Gtk# - if you don't have it yet, use the packages above or add the following repository:
    • http://download.opensuse.org/repositories/Mono/openSUSE_11.0
  • webkit-sharp - can be found in the following repository
    • http://download.opensuse.org/repositories/Mono:/Factory/openSUSE_Factory
  • WebKit - libwebkit can be found in the following repository
    • http://download.opensuse.org/repositories/Mono:/Community/openSUSE_11.0+Mono
You can find all the packages you need for OpenSuse at http://software.opensuse.org/search, if you need different versions

Debian and friends: apt-get install mono-2.0-devel gtk-sharp2 libwebkit1.0-cil


[home] [programming] [cats] [tarot] [books] [rants] [links] [hit me]