Rearrange initialization to be clearer and shorter
authorChip Black <bytex64@bytex64.net>
Mon, 8 Jun 2009 03:25:32 +0000 (22:25 -0500)
committerChip Black <bytex64@bytex64.net>
Mon, 8 Jun 2009 03:25:32 +0000 (22:25 -0500)
WebThing.cs
plugins/DefaultPage.cs

index c1e1806..9429099 100644 (file)
@@ -48,44 +48,69 @@ namespace bytex64.WebThing {
         [DllImport ("SoupSettings.dll")]
         private static extern void soup_settings();
 
+        // Main setup
         public void Run() {
             Application.Init();
 
+            soup_settings();
+
             Config.ParseCommandLine();
             Config.Load();
             //Config.DumpOptions();
 
-            soup_settings();
-
+            // Initialize Window
             _Window = new Gtk.Window("WebThing");
-            _Window.SetWmclass("webthing", "WebThing");
+            // The GTK+ docs say not to use this, but the defaults are
+            // based on the executable name, which I don't like.
+            _Window.SetWmclass("webthing", "WebThing");  
             _Window.Role = "browser";
             _Window.Destroyed += delegate { Quit(); };
 
+            // Initialize WidgetGrid
+            // The WidgetGrid holds the Tabs (and thus WebKit) and
+            // AttachPoint.Interior in the center, and allows widgets to
+            // be added on the sides of the browser
             WidgetGrid = new Gtk.Table(3, 3, false);
             _Window.Add(WidgetGrid);
 
+            // Initialize Tabs
+            // Tabs are a core feature of WebThing, not a plugin.  By
+            // default, they do nothing -- Tab manipulation is left to
+            // plugins to define.
             _Tabs = new Gtk.Notebook();
             _Tabs.ShowBorder = false;
             _Tabs.Scrollable = true;
             _Tabs.SwitchPage += Tabs_SwitchPage;
             WidgetGrid.Attach(_Tabs, 1, 2, 1, 2);
 
+            // InteriorOverlay goes over top of the Tabs to contain
+            // widgets that hover over the content, like progress, or
+            // inline search.  This is not a very solid idea ATM.
             InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
             WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
 
             _Window.ShowAll();
 
-            WebThingView newview = NewWebThingView();
-
+            // Load Plugins
             Plugins = new PluginManager(this);
             Plugins.Load();
-            Plugins.WebViewSetup(newview.WebView);
-            newview.WebView.GrabFocus();
+
+            // Create a new, default WebThingView if one has not already
+            // been created.
+            if (Tabs.NPages == 0) {
+                WebThingView default_tab = NewTab();
+                default_tab.WebView.GrabFocus();
+            }
 
             Application.Run();
         }
 
+        public void Quit() {
+            // TODO: Create a way of shutting down plugins
+            Application.Quit();
+        }
+
+        // Widget attachment
         public void AttachWidget(Gtk.Widget widget, AttachPoint direction, AttachOptions xoptions, AttachOptions yoptions) {
             switch(direction) {
             case AttachPoint.N:
@@ -110,10 +135,11 @@ namespace bytex64.WebThing {
             AttachWidget(widget, direction, 0, 0);
         }
 
-        public WebView NewTab() {
+        // Tab management
+        public WebThingView NewTab() {
             WebThingView newview = NewWebThingView();
             Plugins.WebViewSetup(newview.WebView);
-            return newview.WebView;
+            return newview;
         }
 
         private WebThingView NewWebThingView() {
@@ -139,10 +165,17 @@ namespace bytex64.WebThing {
                     _Tabs.RemovePage(tab);
                     view.Dispose();
                 } catch (ArgumentOutOfRangeException) {
+                    Console.WriteLine("Attempted to close tab out of range: {0}", tab);
                 }
             }
         }
 
+        private void Tabs_SwitchPage(object o, SwitchPageArgs e) {
+            Gtk.Widget page = _Tabs.GetNthPage((int)e.PageNum);
+            _Window.Title = _Tabs.GetTabLabelText(page) + " - WebThing";
+        }
+
+        // Uri loading
         public string FixUri(string Uri) {
             if (!Regex.IsMatch(Uri, @"://")) {
                 return String.Format("http://{0}", Uri);
@@ -155,18 +188,8 @@ namespace bytex64.WebThing {
         }
 
         public void OpenUriTab(string Uri) {
-            WebView view = NewTab();
-            view.Open(FixUri(Uri));
-        }
-
-        public void Quit() {
-            // TODO: Create a way of shutting down plugins
-            Application.Quit();
-        }
-
-        private void Tabs_SwitchPage(object o, SwitchPageArgs e) {
-            Gtk.Widget page = _Tabs.GetNthPage((int)e.PageNum);
-            _Window.Title = _Tabs.GetTabLabelText(page) + " - WebThing";
+            WebThingView wtv = NewTab();
+            wtv.WebView.Open(FixUri(Uri));
         }
     }
 }
index f7076e5..5ec5bdd 100644 (file)
@@ -1,14 +1,18 @@
 using System;
+using System.Text.RegularExpressions;
 using bytex64.WebThing;
 
 public class DefaultPage : WebThingPlugin {
     public override void Init(WebThing wt) {
         if (Config.Arguments.Length > 0) {
-            wt.OpenUri(Config.Arguments[0]);
+            foreach (string arg in Config.Arguments)
+                wt.OpenUriTab(arg);
         } else if (Config.Options.ContainsKey("DefaultPage")) {
-            wt.OpenUri(Config.Options["DefaultPage"]);
+            string[] pages = Regex.Split(Config.Options["DefaultPage"], @"\s+");
+            foreach (string page in pages)
+                wt.OpenUriTab(page);
         } else {
-            wt.OpenUri("http://dominionofawesome.com/");
+            wt.OpenUriTab("http://dominionofawesome.com/");
         }
     }
 }