Added tabs
[WebThing.git] / plugins / Vimish.cs
1 using System;
2 using System.Text.RegularExpressions;
3 using Gtk;
4 using WebKit;
5 using bytex64.WebThing;
6
7 public class Vimish : WebThingPlugin {
8     WebThing wt;
9     Gtk.Entry commandline;
10
11     public override void Init(WebThing wt) {
12         this.wt = wt;
13
14         wt.Window.KeyPressEvent += Window_KeyPress;
15
16         commandline = new Gtk.Entry();
17         commandline.Activated += command_Activate;
18         commandline.KeyPressEvent += command_KeyPress;
19         wt.AttachWidget(commandline, CompassDirection.S, AttachOptions.Fill, AttachOptions.Shrink);
20
21         commandline.Hide();
22     }
23
24     public override void InitWebView(WebView wv) {
25         wv.KeyPressEvent += WebView_KeyPress;
26     }
27
28     private void Window_KeyPress(object o, KeyPressEventArgs e) {
29         switch(e.Event.Key) {
30         case Gdk.Key.o:
31             CommandStart("open ");
32             break;
33         case Gdk.Key.t:
34             CommandStart("tabopen ");
35             break;
36         case Gdk.Key.colon:
37             CommandlineShow();
38             break;
39         }
40     }
41
42     private void WebView_KeyPress(object o, KeyPressEventArgs e) {
43         Console.WriteLine(e.Event.Key);
44         if ((Gdk.ModifierType.ControlMask & e.Event.State) != 0) {
45             switch(e.Event.Key) {
46             case Gdk.Key.n:
47                 wt.Tabs.NextPage();
48                 break;
49             case Gdk.Key.p:
50                 wt.Tabs.PrevPage();
51                 break;
52             }
53         } else {
54             switch(e.Event.Key) {
55             case Gdk.Key.j:
56                 wt.ScrolledWindow.Vadjustment.Value += wt.ScrolledWindow.Vadjustment.StepIncrement;
57                 break;
58             case Gdk.Key.k:
59                 wt.ScrolledWindow.Vadjustment.Value -= wt.ScrolledWindow.Vadjustment.StepIncrement;
60                 break;
61             case Gdk.Key.l:
62                 wt.ScrolledWindow.Hadjustment.Value += wt.ScrolledWindow.Hadjustment.StepIncrement;
63                 break;
64             case Gdk.Key.h:
65                 wt.ScrolledWindow.Hadjustment.Value -= wt.ScrolledWindow.Hadjustment.StepIncrement;
66                 break;
67             case Gdk.Key.r:
68                 wt.WebView.Reload();
69                 break;
70             case Gdk.Key.Escape:
71                 wt.WebView.ExecuteScript("document.activeElement.blur()");
72                 break;
73             }
74         }
75     }
76
77     public void CommandStart(string text) {
78         commandline.Text = text;
79         commandline.GrabFocus();
80         commandline.Position = text.Length;
81         commandline.Show();
82     }
83
84     public void CommandlineShow() {
85         commandline.Show();
86         commandline.GrabFocus();
87     }
88
89     public void CommandlineHide() {
90         commandline.Hide();
91         wt.WebView.GrabFocus();
92     }
93
94     private void command_Activate(object o, EventArgs e) {
95         string[] args = Regex.Split(commandline.Text, @"\s+");
96         switch(args[0]) {
97         case "close":
98             wt.CloseTab();
99             break;
100         case "open":
101             if (args.Length < 2) return;
102             wt.OpenUri(args[1]);
103             break;
104         case "tabopen":
105             if (args.Length < 2) return;
106             wt.OpenUriTab(args[1]);
107             wt.Tabs.CurrentPage = wt.Tabs.NPages - 1;
108             break;
109         case "n":
110             wt.Tabs.NextPage();
111             break;
112         case "p":
113             wt.Tabs.PrevPage();
114             break;
115         case "q":
116             wt.Quit();
117             break;
118         }
119         CommandlineHide();
120     }
121
122     private void command_KeyPress(object o, KeyPressEventArgs e) {
123         if (e.Event.Key == Gdk.Key.Escape)
124             CommandlineHide();
125     }
126 }