Fix scrolling functionality
[WebThing.git] / plugins / QuickSearch.cs
1 using System;
2 using Gtk;
3 using bytex64.WebThing;
4
5 public class QuickSearch : WebThingPlugin {
6     WebThing wt;
7     Gtk.Entry SearchEntry;
8
9     public override void Init(WebThing wt) {
10         SearchEntry = new Gtk.Entry();
11         SearchEntry.KeyPressEvent += SearchEntry_KeyPress;
12         SearchEntry.Changed += SearchEntry_Changed;
13         SearchEntry.Activated += SearchEntry_Activated;
14         wt.AttachWidget(SearchEntry, AttachPoint.S, AttachOptions.Fill, AttachOptions.Shrink);
15
16         wt.Window.KeyPressEvent += Window_KeyPress;
17         this.wt = wt;
18     }
19
20     private void Window_KeyPress(object o, Gtk.KeyPressEventArgs e) {
21         if (e.Event.State == Gdk.ModifierType.None &&
22             e.Event.Key == Gdk.Key.slash) {
23             SearchEntry.Show();
24             SearchEntry.GrabFocus();
25         }
26     }
27
28     private void SearchEntry_Changed(object o, EventArgs e) {
29         Console.WriteLine("Search {0}", SearchEntry.Text);
30         wt.WebView.SearchText(SearchEntry.Text, false, true, true);
31     }
32
33     private void SearchEntry_Activated(object o, EventArgs e) {
34         SearchEntry.Hide();
35         wt.WebView.GrabFocus();
36     }
37
38     private void SearchEntry_KeyPress(object o, Gtk.KeyPressEventArgs e) {
39         if ((e.Event.State & Gdk.ModifierType.ControlMask) != 0) {
40             switch(e.Event.Key) {
41             case Gdk.Key.g:
42                 wt.WebView.SearchText(SearchEntry.Text, false, true, true);
43                 break;
44             }
45         } else {
46             switch(e.Event.Key) {
47             case Gdk.Key.Escape:
48             case Gdk.Key.Return:
49                 SearchEntry.Hide();
50                 wt.WebView.GrabFocus();
51                 break;
52             }
53         }
54     }
55 }