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";
68 _Window.Destroyed += delegate { Quit(); };
70 // Initialize WidgetGrid
71 // The WidgetGrid holds the Tabs (and thus WebKit) and
72 // AttachPoint.Interior in the center, and allows widgets to
73 // be added on the sides of the browser
74 WidgetGrid = new Gtk.Table(3, 3, false);
75 _Window.Add(WidgetGrid);
78 // Tabs are a core feature of WebThing, not a plugin. By
79 // default, they do nothing -- Tab manipulation is left to
81 _Tabs = new Gtk.Notebook();
82 _Tabs.ShowBorder = false;
83 _Tabs.Scrollable = true;
84 _Tabs.SwitchPage += Tabs_SwitchPage;
85 WidgetGrid.Attach(_Tabs, 1, 2, 1, 2);
87 // InteriorOverlay goes over top of the Tabs to contain
88 // widgets that hover over the content, like progress, or
89 // inline search. This is not a very solid idea ATM.
90 InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
91 WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
96 Plugins = new PluginManager(this);
99 // Load Search Handlers
100 Search = new SearchHandler(this);
102 // Create a new, default WebThingView if one has not already
104 if (Tabs.NPages == 0)
105 OpenTab("http://dominionofawesome.com/");
117 public void AttachWidget(Gtk.Widget widget, AttachPoint direction, AttachOptions xoptions, AttachOptions yoptions) {
120 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
123 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
126 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
129 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
131 case AttachPoint.Interior:
132 InteriorOverlay.Add(widget);
137 public void AttachWidget(Gtk.Widget widget, AttachPoint direction) {
138 AttachWidget(widget, direction, 0, 0);
142 public WebThingView NewTab() {
143 WebThingView newview = NewWebThingView();
144 Plugins.WebViewSetup(newview.WebView);
148 private WebThingView NewWebThingView() {
149 WebThingView newview = new WebThingView();
150 Tabs.AppendPage(newview, new Label("Blank"));
151 newview.WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
152 Tabs.SetTabLabelText((Gtk.Widget) newview, e.Title);
153 if (newview == Tabs.CurrentPageWidget)
154 _Window.Title = e.Title + " - WebThing";
160 public void CloseTab() {
161 CloseTab(_Tabs.Page);
164 public void CloseTab(int tab) {
165 if (_Tabs.NPages > 1) {
167 WebThingView view = _Tabs.GetNthPage(tab) as WebThingView;
168 _Tabs.RemovePage(tab);
170 } catch (ArgumentOutOfRangeException) {
171 Console.WriteLine("Attempted to close tab out of range: {0}", tab);
176 private void Tabs_SwitchPage(object o, SwitchPageArgs e) {
177 Gtk.Widget page = _Tabs.GetNthPage((int)e.PageNum);
178 _Window.Title = _Tabs.GetTabLabelText(page) + " - WebThing";
182 private string GetUri(string query) {
187 } catch(UriFormatException) {
189 u = new Uri(String.Format("http://{0}", query));
191 } catch (UriFormatException) {
192 return Search.Transform(query);
197 public bool Open(string query) {
198 string uri = GetUri(query);
199 if (uri == null) return false;
205 public bool OpenTab(string query) {
206 WebThingView wtv = NewTab();
207 string uri = GetUri(query);
208 if (uri == null) return false;
210 wtv.WebView.Open(uri);