[Gtk-sharp-list] Webkit GTK# For Windows

scocpt tottlecp at cardiff.ac.uk
Tue Mar 4 14:24:09 UTC 2014


Thanks to Cody for the following answer to my post

http://mono.1490590.n4.nabble.com/Embedding-an-IE-Window-td4662025.html

I decided to tackle the problem from a slightly different angle. If my app
was running on Windows then embed IE as the web browser, and if I was
running Linux then embed WebKit. Below is my undocumented code that I wrote
in #Develop (Windows 7) and tested on openSUSE 13.1

My main references for this project are:

http://shana.worldofcoding.com/en/browser.html
https://github.com/bratsche/gtkie

I hope that others find this code of use.

Chris

TWGTKWeb.cs (wrapper for IE and WebKit)
#############################

/*
 * Created by SharpDevelop.
 * User: Chris
 * Date: 04/03/2014
 * Time: 13:43
 * 
 * To change this template use Tools | Options | Coding | Edit Standard
Headers.
 */
using System;
using System.Windows.Forms;
using Gtk;
using WebKit; //Add reference to project, set "Aliases" to global and "Copy
Local" to false (webkit-sharp.dll).

//http://shana.worldofcoding.com/en/browser.html
//https://github.com/bratsche/gtkie

namespace TottleWare.LinuxGUI.WebBrowser
{
	public class TWWebBrowserIE
	{
		[System.Runtime.InteropServices.DllImportAttribute("user32.dll",
EntryPoint = "SetParent")]
		internal static extern System.IntPtr
SetParent([System.Runtime.InteropServices.InAttribute()] System.IntPtr
hWndChild, [System.Runtime.InteropServices.InAttribute()] System.IntPtr
hWndNewParent);

		public System.Windows.Forms.WebBrowser wb = null;
		public Gtk.Socket socket = new Gtk.Socket();

		public void InitIE(Gtk.Window w)
		{

			wb = new System.Windows.Forms.WebBrowser ();
			wb.Height = 600;
			wb.Width  = 600;

			w.Add (socket);
			socket.SetSizeRequest(600, 600);
			socket.Realize();
			socket.Show();

			var browser_handle = wb.Handle;
			IntPtr window_handle = (IntPtr) socket.Id;
			SetParent(browser_handle, window_handle);
		}

		public void Navigate(string uri)
		{
			wb.Navigate(uri);
		}

		public TWWebBrowserIE(Gtk.Window w)
		{
			InitIE (w);
		}
    }

	//=======================================================================

    public class TWWebBrowserWK
    {
        public WebView wb = null;
        public ScrolledWindow scrollWindow = new ScrolledWindow();

        public void InitWebKit(Gtk.Window w)
        {
            wb = new WebView();
            scrollWindow.Add(wb);
            w.Add(scrollWindow);
            w.ShowAll();
        }

        public void Navigate(string uri)
        {
            wb.Open(uri);
        }

        public TWWebBrowserWK(Gtk.Window w)
        {
            InitWebKit(w);
        }
    }

   
//=======================================================================
}


Program.cs (main program)
##################

/*
 * Created by SharpDevelop.
 * User: Chris
 * Date: 04/03/2014
 * Time: 13:43
 * 
 * To change this template use Tools | Options | Coding | Edit Standard
Headers.
 */
using System;
using Gtk;
using TottleWare.LinuxGUI.WebBrowser;

namespace TestWeb
{
	public partial class MainWindow: Gtk.Window
	{
		protected virtual void Build ()
		{
			this.Name = "MainWindow";
			this.WindowPosition = ((global::Gtk.WindowPosition)(1));
			if ((this.Child != null)) {	this.Child.ShowAll (); }
			this.DefaultWidth = 976;
			this.DefaultHeight = 667;
			this.Show ();
			this.DeleteEvent += new global::Gtk.DeleteEventHandler
(this.OnDeleteEvent);
		}
		
		public MainWindow () : base (Gtk.WindowType.Toplevel)
		{
			dynamic web;
			Build ();
			
			if (Environment.OSVersion.Platform.ToString() == "Unix") { web = new
TWWebBrowserWK(this); }
			else { web = new TWWebBrowserIE(this); }
			
			web.Navigate("http://www.cardiff.ac.uk");
		}
		
		protected void OnDeleteEvent (object sender, DeleteEventArgs a)
		{
			Application.Quit ();
		    a.RetVal = true;
		}
	}

			
	class Program
	{
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Init ();
			MainWindow win = new MainWindow ();
			win.Show ();
			Application.Run ();
		}
	}
}



--
View this message in context: http://mono.1490590.n4.nabble.com/Webkit-GTK-For-Windows-tp4661981p4662118.html
Sent from the Mono - Gtk# mailing list archive at Nabble.com.


More information about the Gtk-sharp-list mailing list