Fix scrolling functionality
[WebThing.git] / WebThing.cs
index fd2eb99..ff51354 100644 (file)
@@ -44,6 +44,7 @@ namespace bytex64.WebThing {
         private Gtk.Alignment InteriorOverlay;
 
         public PluginManager Plugins;
+        public SearchHandler Search;
 
         [DllImport ("SoupSettings.dll")]
         private static extern void soup_settings();
@@ -64,6 +65,21 @@ namespace bytex64.WebThing {
             // based on the executable name, which I don't like.
             _Window.SetWmclass("webthing", "WebThing");  
             _Window.Role = "browser";
+            {
+                int w, h;
+
+                if (Config.Options.ContainsKey("Width"))
+                    w = Convert.ToInt32(Config.Options["Width"]);
+                else
+                    w = 640;
+
+                if (Config.Options.ContainsKey("Height"))
+                    h = Convert.ToInt32(Config.Options["Height"]);
+                else
+                    h = 480;
+
+                _Window.DefaultSize = new Gdk.Size(w, h);
+            }
             _Window.Destroyed += delegate { Quit(); };
 
             // Initialize WidgetGrid
@@ -95,17 +111,20 @@ namespace bytex64.WebThing {
             Plugins = new PluginManager(this);
             Plugins.Load();
 
+            // Load Search Handlers
+            Search = new SearchHandler(this);
+
             // Create a new, default WebThingView if one has not already
             // been created.
             if (Tabs.NPages == 0)
-                OpenUriTab("http://dominionofawesome.com/");
+                OpenTab("http://dominionofawesome.com/");
             WebView.GrabFocus();
 
             Application.Run();
         }
 
         public void Quit() {
-            // TODO: Create a way of shutting down plugins
+            Plugins.Deinit();
             Application.Quit();
         }
 
@@ -175,20 +194,49 @@ namespace bytex64.WebThing {
         }
 
         // Uri loading
-        public string FixUri(string Uri) {
-            if (!Regex.IsMatch(Uri, @"://")) {
-                return String.Format("http://{0}", Uri);
+        private string GetUri(string query) {
+            Uri u;
+            try {
+                u = new Uri(query);
+                return u.ToString();
+            } catch(UriFormatException) {
+                try {
+                    u = new Uri(String.Format("http://{0}", query));
+                    return u.ToString();
+                } catch (UriFormatException) {
+                    return Search.Transform(query);
+                }
             }
-            return Uri;
         }
 
-        public void OpenUri(string Uri) {
-            wv.Open(FixUri(Uri));
+        public bool Open(string query) {
+            string uri = GetUri(query);
+            if (uri == null) return false;
+            
+            wv.Open(uri);
+            return true;
         }
 
-        public void OpenUriTab(string Uri) {
+        public bool OpenTab(string query) {
             WebThingView wtv = NewTab();
-            wtv.WebView.Open(FixUri(Uri));
+            string uri = GetUri(query);
+            if (uri == null) return false;
+
+            wtv.WebView.Open(uri);
+            return true;
+        }
+
+        public void Scroll(double x, double y) {
+            ScrolledWindow.Hadjustment.Value += x;
+            if (ScrolledWindow.Hadjustment.Value > ScrolledWindow.Hadjustment.Upper - ScrolledWindow.Hadjustment.PageSize)
+                ScrolledWindow.Hadjustment.Value = ScrolledWindow.Hadjustment.Upper - ScrolledWindow.Hadjustment.PageSize;
+            ScrolledWindow.Vadjustment.Value += y;
+            if (ScrolledWindow.Vadjustment.Value > ScrolledWindow.Vadjustment.Upper - ScrolledWindow.Vadjustment.PageSize)
+                ScrolledWindow.Vadjustment.Value = ScrolledWindow.Vadjustment.Upper - ScrolledWindow.Vadjustment.PageSize;
+        }
+
+        public void Bump(int x, int y) {
+            Scroll(x * ScrolledWindow.Hadjustment.StepIncrement, y * ScrolledWindow.Vadjustment.StepIncrement);
         }
     }
 }