2 using System.Collections.Generic;
4 using System.Text.RegularExpressions;
5 using System.Runtime.InteropServices;
10 namespace bytex64.WebThing {
11 public enum AttachPoint {
15 public class WebThing {
16 private Gtk.Window _Window;
17 public Gtk.Window Window {
18 get { return _Window; }
21 get { return _Window; }
24 public ScrolledWindow ScrolledWindow {
25 get { return Tabs.CurrentPageWidget as ScrolledWindow; }
27 public ScrolledWindow sw {
28 get { return ScrolledWindow; }
31 public WebView WebView {
32 get { return (Tabs.CurrentPageWidget as WebThingView).WebView; }
35 get { return WebView; }
38 private Gtk.Notebook _Tabs;
39 public Gtk.Notebook Tabs {
43 private Gtk.Table WidgetGrid;
44 private Gtk.Alignment InteriorOverlay;
46 public PluginManager Plugins;
47 public SearchHandler Search;
49 [DllImport ("SoupSettings.dll")]
50 private static extern void soup_settings();
58 Config.ParseCommandLine();
60 //Config.DumpOptions();
63 _Window = new Gtk.Window("WebThing");
64 // The GTK+ docs say not to use this, but the defaults are
65 // based on the executable name, which I don't like.
66 _Window.SetWmclass("webthing", "WebThing");
67 _Window.Role = "browser";
71 if (Config.Options.ContainsKey("Width"))
72 w = Convert.ToInt32(Config.Options["Width"]);
76 if (Config.Options.ContainsKey("Height"))
77 h = Convert.ToInt32(Config.Options["Height"]);
81 _Window.DefaultSize = new Gdk.Size(w, h);
83 _Window.Destroyed += delegate { Quit(); };
85 // Initialize WidgetGrid
86 // The WidgetGrid holds the Tabs (and thus WebKit) and
87 // AttachPoint.Interior in the center, and allows widgets to
88 // be added on the sides of the browser
89 WidgetGrid = new Gtk.Table(3, 3, false);
90 _Window.Add(WidgetGrid);
93 // Tabs are a core feature of WebThing, not a plugin. By
94 // default, they do nothing -- Tab manipulation is left to
96 _Tabs = new Gtk.Notebook();
97 _Tabs.ShowBorder = false;
98 _Tabs.Scrollable = true;
99 _Tabs.SwitchPage += Tabs_SwitchPage;
100 WidgetGrid.Attach(_Tabs, 1, 2, 1, 2);
102 // InteriorOverlay goes over top of the Tabs to contain
103 // widgets that hover over the content, like progress, or
104 // inline search. This is not a very solid idea ATM.
105 InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
106 WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
111 Plugins = new PluginManager(this);
114 // Load Search Handlers
115 Search = new SearchHandler(this);
117 // Create a new, default WebThingView if one has not already
119 if (Tabs.NPages == 0)
120 OpenTab("http://dominionofawesome.com/");
132 public void AttachWidget(Gtk.Widget widget, AttachPoint direction, AttachOptions xoptions, AttachOptions yoptions) {
135 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
138 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
141 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
144 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
146 case AttachPoint.Interior:
147 InteriorOverlay.Add(widget);
152 public void AttachWidget(Gtk.Widget widget, AttachPoint direction) {
153 AttachWidget(widget, direction, 0, 0);
157 public WebThingView NewTab() {
158 WebThingView newview = NewWebThingView();
159 Plugins.WebViewSetup(newview.WebView);
163 private WebThingView NewWebThingView() {
164 WebThingView newview = new WebThingView();
165 Tabs.AppendPage(newview, new Label("Blank"));
166 newview.WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
167 Tabs.SetTabLabelText((Gtk.Widget) newview, e.Title);
168 if (newview == Tabs.CurrentPageWidget)
169 _Window.Title = e.Title + " - WebThing";
175 public void CloseTab() {
176 CloseTab(_Tabs.Page);
179 public void CloseTab(int tab) {
180 if (_Tabs.NPages > 1) {
182 WebThingView view = _Tabs.GetNthPage(tab) as WebThingView;
183 _Tabs.RemovePage(tab);
185 } catch (ArgumentOutOfRangeException) {
186 Console.WriteLine("Attempted to close tab out of range: {0}", tab);
191 private void Tabs_SwitchPage(object o, SwitchPageArgs e) {
192 Gtk.Widget page = _Tabs.GetNthPage((int)e.PageNum);
193 _Window.Title = _Tabs.GetTabLabelText(page) + " - WebThing";
197 private string GetUri(string query) {
202 } catch(UriFormatException) {
204 u = new Uri(String.Format("http://{0}", query));
206 } catch (UriFormatException) {
207 return Search.Transform(query);
212 public bool Open(string query) {
213 string uri = GetUri(query);
214 if (uri == null) return false;
220 public bool OpenTab(string query) {
221 WebThingView wtv = NewTab();
222 string uri = GetUri(query);
223 if (uri == null) return false;
225 wtv.WebView.Open(uri);
229 public void Scroll(double x, double y) {
230 ScrolledWindow.Hadjustment.Value += x;
231 if (ScrolledWindow.Hadjustment.Value > ScrolledWindow.Hadjustment.Upper - ScrolledWindow.Hadjustment.PageSize)
232 ScrolledWindow.Hadjustment.Value = ScrolledWindow.Hadjustment.Upper - ScrolledWindow.Hadjustment.PageSize;
233 ScrolledWindow.Vadjustment.Value += y;
234 if (ScrolledWindow.Vadjustment.Value > ScrolledWindow.Vadjustment.Upper - ScrolledWindow.Vadjustment.PageSize)
235 ScrolledWindow.Vadjustment.Value = ScrolledWindow.Vadjustment.Upper - ScrolledWindow.Vadjustment.PageSize;
238 public void Bump(int x, int y) {
239 Scroll(x * ScrolledWindow.Hadjustment.StepIncrement, y * ScrolledWindow.Vadjustment.StepIncrement);