2 using System.Collections.Generic;
3 using System.Reflection;
5 using System.Text.RegularExpressions;
6 using System.Runtime.InteropServices;
11 namespace bytex64.WebThing {
12 public enum CompassDirection {
16 public class WebThing {
17 private Gtk.Window _Window;
18 public Gtk.Window Window {
19 get { return _Window; }
22 get { return _Window; }
25 public ScrolledWindow ScrolledWindow {
26 get { return Tabs.CurrentPageWidget as ScrolledWindow; }
28 public ScrolledWindow sw {
29 get { return ScrolledWindow; }
32 public WebView WebView {
33 get { return (Tabs.CurrentPageWidget as WebThingView).WebView; }
36 get { return WebView; }
39 private Gtk.Notebook _Tabs;
40 public Gtk.Notebook Tabs {
44 private Gtk.Table WidgetGrid;
45 private Gtk.Alignment InteriorOverlay;
47 public Dictionary<string,WebThingPlugin> Plugins;
49 [DllImport ("SoupSettings.dll")]
50 private static extern void soup_settings();
54 Plugins = new Dictionary<string,WebThingPlugin>();
56 Config.ParseCommandLine();
58 //Config.DumpOptions();
62 _Window = new Gtk.Window("WebThing");
63 _Window.SetWmclass("webthing", "WebThing");
64 _Window.Role = "browser";
65 _Window.Destroyed += delegate { Quit(); };
67 WidgetGrid = new Gtk.Table(3, 3, false);
68 _Window.Add(WidgetGrid);
70 _Tabs = new Gtk.Notebook();
71 _Tabs.ShowBorder = false;
72 _Tabs.SwitchPage += Tabs_SwitchPage;
73 WidgetGrid.Attach(_Tabs, 1, 2, 1, 2);
75 InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
76 WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
80 WebThingView newview = NewWebThingView();
82 // TODO: Conf.Get("plugins") instead of hard-coded path?
83 using (TextReader f = new StreamReader("plugins.conf")) {
85 while ((line = f.ReadLine()) != null) {
91 WebViewSetupPlugins(newview.WebView);
92 newview.WebView.GrabFocus();
97 public void LoadPlugin(string assemblyname) {
98 Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
99 Type[] types = a.GetTypes();
100 foreach (Type t in types) {
101 if (t.IsSubclassOf(typeof(WebThingPlugin))) {
102 WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
104 Plugins[t.FullName] = p;
105 Console.WriteLine("Successfully loaded {0}", t.FullName);
110 public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
112 case CompassDirection.N:
113 WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0);
115 case CompassDirection.E:
116 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
118 case CompassDirection.S:
119 WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0);
121 case CompassDirection.W:
122 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
124 case CompassDirection.Interior:
125 InteriorOverlay.Add(widget);
130 public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
131 AttachWidget(widget, direction, 0, 0);
134 public WebView NewTab() {
135 WebThingView newview = NewWebThingView();
136 WebViewSetupPlugins(newview.WebView);
137 return newview.WebView;
140 private WebThingView NewWebThingView() {
141 WebThingView newview = new WebThingView();
142 Tabs.AppendPage(newview, new Label("Blank"));
143 newview.WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
144 Tabs.SetTabLabelText((Gtk.Widget) newview, e.Title);
145 if (newview == Tabs.CurrentPageWidget)
146 _Window.Title = e.Title + " - WebThing";
152 private void WebViewSetupPlugins(WebView view) {
153 foreach (string key in Plugins.Keys) {
154 Plugins[key].InitWebView(view);
158 public void CloseTab() {
159 CloseTab(_Tabs.Page);
162 public void CloseTab(int tab) {
163 if (_Tabs.NPages > 1) {
165 WebThingView view = _Tabs.GetNthPage(tab) as WebThingView;
166 _Tabs.RemovePage(tab);
168 } catch (ArgumentOutOfRangeException) {
173 public string FixUri(string Uri) {
174 if (!Regex.IsMatch(Uri, @"://")) {
175 return String.Format("http://{0}", Uri);
180 public void OpenUri(string Uri) {
181 wv.Open(FixUri(Uri));
184 public void OpenUriTab(string Uri) {
185 WebView view = NewTab();
186 view.Open(FixUri(Uri));
190 // TODO: Create a way of shutting down plugins
194 private void Tabs_SwitchPage(object o, SwitchPageArgs e) {
195 Gtk.Widget page = _Tabs.GetNthPage((int)e.PageNum);
196 _Window.Title = _Tabs.GetTabLabelText(page) + " - WebThing";