using System; using Gtk; using Glade; using Gecko; // 2007, Igor Guerrero Fonseca // A simple Gecko(Mozilla) browser // using the Gtk# toolkit and Glade#(XML GUI for Gnome) tool // and Gecko#(Mozilla C# binding) // TODO: There's a lot of things that can be done in this program but, // I think that for a quick hack is good and stable. namespace Monobrowser { class Monobrowser { // Attributes added by glade [Widget] Window mainWindow; // GTK widgets in a Glade attribute [Widget] Button back; [Widget] Button forward; [Widget] Button home; [Widget] Button refresh; [Widget] Statusbar status; [Widget] ProgressBar progress; [Widget] Entry url; [Widget] Button ok; [Widget] VBox webControlArea; // Area where we going to add the webcontrol(mozilla) WebControl web; static void Main(string[] args) { new Monobrowser(); } public Monobrowser() { Application.Init(); // GTK entry point Glade.XML gxml= new Glade.XML("gui.glade", "mainWindow"); // load the XML file string mozEnv = System.Environment.GetEnvironmentVariable("GECKOSHILLA_BASEPATH"); if(mozEnv != null && mozEnv.Length != 0) // Windows only Gecko.WebControl.CompPath = mozEnv; gxml.Autoconnect(this); // Adding the Glade interface to this GTK application web = new WebControl("/tmp/moztest/", "Mozilla new Browser in GTK#"); // Events web.LinkMsg += new EventHandler(OnWebLinkMsg); web.Progress += new ProgressHandler(OnWebProgress); web.ProgressAll += new ProgressAllHandler(OnWebProgressAll); web.LocChange += new EventHandler(OnWebLocChange); web.StatusChange += new StatusChangeHandler(OnWebStatusChange); web.TitleChange += new EventHandler(OnWebTitleChange); web.NetStop += new EventHandler(OnWebNetStop); LoadAddress(); status.Push(1, "Ready"); web.Show(); webControlArea.Add(web); mainWindow.DeleteEvent += delegate { Application.Quit(); }; // Showing a delegate method mainWindow.ShowAll(); Application.Run(); // The app loop begin } private void LoadAddress(string address) { web.LoadUrl(address); } private void LoadAddress() { // Overloaded method web.LoadUrl("http://www.google.com/"); } private void UpdateLocation() { url.Text = web.Location; status.Pop(1); status.Push(1, web.LinkMessage); } // Web object handlers private void OnWebLinkMsg(object o, EventArgs args) { // Add a status of the link status.Pop(1); status.Push(1, web.LinkMessage); } private void OnWebProgress(object o, ProgressArgs args) { // Increse the progressbar if(progress.Fraction >= 1.00) progress.Fraction = 0.00; else progress.Fraction += 0.05; } private void OnWebProgressAll(object o, ProgressAllArgs args) { if(progress.Fraction >= 1.00) progress.Fraction = 0.00; else progress.Fraction += 0.05; } private void OnWebLocChange(object o, EventArgs args) { UpdateLocation(); } private void OnWebStatusChange(object o, StatusChangeArgs args) { UpdateLocation(); } private void OnWebTitleChange(object o, EventArgs args) { mainWindow.Title = "Gtk Mono Browser :: " + web.Title; } private void OnWebNetStop(object o, EventArgs args) { // The webcontrol stop loading a page progress.Fraction = 1.00; } // Events in glade file private void OnBackClicked(object o, EventArgs args) { web.GoBack(); UpdateLocation(); } private void OnForwardClicked(object o, EventArgs args) { web.GoForward(); UpdateLocation(); } private void OnOkClicked(object o, EventArgs args) { LoadAddress(url.Text.Trim()); UpdateLocation(); } private void OnHomeClicked(object o, EventArgs args) { LoadAddress(); UpdateLocation(); } private void OnRefreshClicked(object o, EventArgs args) { web.Reload((int)Gecko.ReloadFlags.Reloadnormal); UpdateLocation(); } private void OnEntryActivate(object o, EventArgs args) { OnOkClicked(o, args); UpdateLocation(); } private void OnStopClicked(object o, EventArgs args) { web.StopLoad(); UpdateLocation(); } } }