[Gtk-sharp-list] html example

John Luke jluke@cfl.rr.com
13 Jul 2003 00:57:40 -0400


--=-7ODiODZzub5Vrz2n8zwA
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello,

I forgot I wrote this a couple months go as a test before I started
GtkSharpRSS.  It is a "browser" in gtkhtml.  Not very useful because of
no CSS, etc. but it may be useful as an example.  Should I add it to the
gtk-sharp/samples directory?

John

--=-7ODiODZzub5Vrz2n8zwA
Content-Disposition: attachment; filename=HtmlTest.cs
Content-Type: text/x-csharp; name=HtmlTest.cs; charset=UTF-8
Content-Transfer-Encoding: 7bit

using System;
using System.Net;
using System.IO;
using Gtk;
using GtkSharp;

namespace HtmlTest
{
	class HtmlTest
	{
		HTML html;
		Entry entry;
		string currentUrl;
		
		static void Main (string[] args)
		{
			new HtmlTest();
		}
		
		HtmlTest()
		{
			Application.Init ();
			
			Window win = new Window ("HtmlTest");
			win.SetDefaultSize (800, 600);
			win.DeleteEvent += new DeleteEventHandler (window_delete);
			
			VBox vbox = new VBox (false, 1);
			win.Add (vbox);
			
			HBox hbox = new HBox (false, 1);
			
			Label label = new Label ("http://");
			
			entry = new Entry ("URL");
			entry.Activated += new EventHandler (entry_activated);
			
			Button button = new Button ("GO!");
			button.Clicked += new EventHandler (button_clicked);
			
			hbox.PackStart (label, false, false, 1);
			hbox.PackStart (entry, true, true, 1);
			hbox.PackStart (button, false, false, 1);
			
			vbox.PackStart (hbox, false, false, 1);
			
			ScrolledWindow sw = new ScrolledWindow ();
			sw.VscrollbarPolicy = PolicyType.Always;
			sw.HscrollbarPolicy = PolicyType.Always;
			vbox.PackStart(sw, true, true, 1);
			
			html = new HTML ();
			html.LinkClicked += new LinkClickedHandler (link_clicked);
			sw.Add (html);
			
			HBox hbox2 = new HBox (false, 1);
			vbox.PackStart (hbox2, false, false, 1);
			
			Statusbar sb = new Statusbar ();
			sb.Push (1, "Welcome!");
			hbox2.Add (sb);
			
			ProgressBar pb = new ProgressBar ();
			hbox2.Add (pb);
			
			win.ShowAll();
			Application.Run ();
		
		}
		
		void window_delete (object obj, DeleteEventArgs args)
		{
			Application.Quit();
		}
		
		void button_clicked (object obj, EventArgs args)
		{
			currentUrl = entry.Text.Trim();
			LoadHtml (currentUrl);
		}
		
		void entry_activated (object obj, EventArgs args)
		{
			button_clicked (obj, args);
		}
		
		void link_clicked (object obj, LinkClickedArgs args)
		{
			string newUrl;
			
			// decide absolute or relative
			if (args.Url.StartsWith("http://")) {
				newUrl = args.Url;
			} else {
				newUrl = currentUrl + args.Url;
			}
			
			LoadHtml (newUrl);
			currentUrl = newUrl;
		}
		
		void LoadHtml (string URL)
		{
			HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create (URL);
			HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse ();
			StreamReader streamReader = new StreamReader (webResponse.GetResponseStream (), System.Text.Encoding.ASCII);
			string outputString = streamReader.ReadToEnd ();
			html.LoadFromString (outputString);
		}
	
	}

}

--=-7ODiODZzub5Vrz2n8zwA--