04ede6747e97ec6ebee7bc7eca0c3ad77565137d
[WebThing.git] / WebThing.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 using System.IO;
5 using Gtk;
6 using GtkSharp;
7 using WebKit;
8
9 namespace bytex64.WebThing {
10         public enum CompassDirection {
11                 N, NE, E, SE, S, SW, W, NW,
12                 Interior
13         }
14
15         public class WebThing {
16                 public Gtk.Window Window {
17                         get { return _Window; }
18                 }
19
20                 public Gtk.Window w {
21                         get { return _Window; }
22                 }
23
24                 public Gtk.ScrolledWindow ScrolledWindow {
25                         get { return _ScrolledWindow; }
26                 }
27
28                 public Gtk.ScrolledWindow sw {
29                         get { return _ScrolledWindow; }
30                 }
31
32                 public WebKit.WebView WebView {
33                         get { return _WebView; }
34                 }
35
36                 public WebKit.WebView wv {
37                         get { return _WebView; }
38                 }
39
40                 private Gtk.Window _Window;
41                 private ScrolledWindow _ScrolledWindow;
42                 private WebKit.WebView _WebView;
43                 private Gtk.Table WidgetGrid;
44                 private Gtk.Alignment InteriorOverlay;
45
46                 public Dictionary<string,object> Plugins;
47
48                 public void Run() {
49                         Application.Init();
50                         _Window = new Gtk.Window("WebThing");
51                         _Window.Destroyed += delegate { Application.Quit(); };
52
53                         WidgetGrid = new Gtk.Table(3, 3, false);
54                         _Window.Add(WidgetGrid);
55
56                         _ScrolledWindow = new Gtk.ScrolledWindow();
57                         WidgetGrid.Attach(_ScrolledWindow, 1, 2, 1, 2);
58
59                         InteriorOverlay = new Gtk.Alignment(1, 0, 0, 0);
60                         WidgetGrid.Attach(InteriorOverlay, 1, 2, 1, 2);
61
62                         _WebView = new WebKit.WebView();
63                         _WebView.TitleChanged += delegate(object o, TitleChangedArgs e) {
64                                 _Window.Title = e.Title + " - WebThing";
65                         };
66                         _ScrolledWindow.Add(_WebView);
67
68                         _Window.ShowAll();
69
70                         Plugins = new Dictionary<string, object>();
71                         // TODO: Conf.Get("plugins") instead of hard-coded path?
72                         using (TextReader f = new StreamReader("plugins.conf")) {
73                                 string line;
74                                 while ((line = f.ReadLine()) != null) {
75                                         line = line.Trim();
76                                         LoadPlugin(line);
77                                 }
78                         }
79
80                         Application.Run();
81                 }
82
83                 public void LoadPlugin(string assemblyname) {
84                         Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
85                         object obj = a.CreateInstance("Plugin", false, BindingFlags.ExactBinding, null, new object[] { this }, null, null);
86                         Plugins[assemblyname] = obj;
87                 }
88
89                 public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) {
90                         switch(direction) {
91                         case CompassDirection.N:
92                                 WidgetGrid.Attach(widget, 1, 2, 0, 1, xoptions, yoptions, 0, 0);
93                                 break;
94                         case CompassDirection.NE:
95                                 WidgetGrid.Attach(widget, 2, 3, 0, 1, xoptions, yoptions, 0, 0);
96                                 break;
97                         case CompassDirection.E:
98                                 WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0);
99                                 break;
100                         case CompassDirection.SE:
101                                 WidgetGrid.Attach(widget, 2, 3, 2, 3, xoptions, yoptions, 0, 0);
102                                 break;
103                         case CompassDirection.S:
104                                 WidgetGrid.Attach(widget, 1, 2, 2, 3, xoptions, yoptions, 0, 0);
105                                 break;
106                         case CompassDirection.SW:
107                                 WidgetGrid.Attach(widget, 0, 1, 2, 3, xoptions, yoptions, 0, 0);
108                                 break;
109                         case CompassDirection.W:
110                                 WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0);
111                                 break;
112                         case CompassDirection.NW:
113                                 WidgetGrid.Attach(widget, 0, 1, 0, 1, xoptions, yoptions, 0, 0);
114                                 break;
115                         case CompassDirection.Interior:
116                                 InteriorOverlay.Add(widget);
117                                 break;
118                         }
119                 }
120
121                 public void AttachWidget(Gtk.Widget widget, CompassDirection direction) {
122                         AttachWidget(widget, direction, 0, 0);
123                 }
124         }
125 }