Added tabs
[WebThing.git] / plugins / LoadProgress.cs
1 using System;
2 using Gtk;
3 using WebKit;
4 using bytex64.WebThing;
5
6 public class LoadThrobber : Gtk.DrawingArea {
7     public enum Mode {
8         LoadStarted,
9         LoadInProgress,
10         LoadFinished
11     };
12     public Mode LoadState;
13     int r;
14     uint idletimer;
15
16     public LoadThrobber(WebThing wt) {
17         LoadState = Mode.LoadStarted;
18
19         SetSizeRequest(64, 64);
20     }
21
22     public void AttachSignals(WebView wv) {
23         wv.LoadStarted += WebView_LoadStarted;
24         wv.LoadCommitted += WebView_LoadCommitted;
25         wv.LoadProgressChanged += WebView_LoadProgressChanged;
26         wv.LoadFinished += WebView_LoadFinished;
27     }
28
29     protected override bool OnExposeEvent(Gdk.EventExpose e) {
30         Gdk.Window w = e.Window;
31         Gdk.GC gc = new Gdk.GC(w);
32         int width, height;
33         w.GetSize(out width, out height);
34
35         gc.Foreground = new Gdk.Color(0, 0, 0);
36         gc.SetLineAttributes(3, Gdk.LineStyle.Solid, Gdk.CapStyle.Butt, Gdk.JoinStyle.Round);
37         w.Clear();
38
39         switch(LoadState) {
40         case Mode.LoadStarted:
41             w.DrawArc(gc, false, 4, 4, width-8, width-8, -r*64, 90*64);
42             w.DrawArc(gc, false, 4, 4, width-8, width-8, -r*64 + 180*64, 90*64);
43             r += 5;
44             break;
45         case Mode.LoadInProgress:
46         case Mode.LoadFinished:
47             w.DrawArc(gc, false, 4, 4, width-8, width-8, 90*64, -r*64);
48             break;
49         }
50         return true;
51     }
52
53     void WebView_LoadStarted(object o, LoadStartedArgs e) {
54         this.Show();
55         Console.WriteLine("Loading Started");
56         LoadState = Mode.LoadStarted;
57         idletimer = GLib.Timeout.Add(100, delegate {
58             QueueDraw();
59             return true;
60         });
61     }
62
63     void WebView_LoadCommitted(object o, LoadCommittedArgs e) {
64         Console.WriteLine("Loading Committed");
65         LoadState = Mode.LoadInProgress;
66         GLib.Source.Remove(idletimer);
67         r = 0;
68         QueueDraw();
69     }
70
71     void WebView_LoadProgressChanged(object o, LoadProgressChangedArgs e) {
72         Console.WriteLine("Loading Progress: {0}", e.Progress);
73         r = (int) ((e.Progress / 100.0) * 360);
74         QueueDraw();
75     }
76
77     void WebView_LoadFinished(object o, LoadFinishedArgs e) {
78         Console.WriteLine("Loading Finished");
79         LoadState = Mode.LoadFinished;
80         this.Hide();
81     }
82 }
83
84 public class LoadThrobberPlugin : WebThingPlugin {
85     LoadThrobber lt;
86
87     public override void Init(WebThing wt) {
88         lt = new LoadThrobber(wt);
89         wt.AttachWidget(lt, CompassDirection.Interior);
90     }
91
92     public override void InitWebView(WebView wv) {
93         lt.AttachSignals(wv);
94     }
95 }