Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / Util / XKeybinder.cs
blob404ad22a6e0197f827bc5065af549c9ed4d5cbb4
2 using System;
3 using System.Collections;
4 using System.Runtime.InteropServices;
6 namespace Beagle.Util
8 public class XKeybinder
10 [DllImport("libbeagleuiglue")]
11 static extern void tomboy_keybinder_init ();
13 [DllImport("libbeagleuiglue")]
14 static extern void tomboy_keybinder_bind (string keystring,
15 BindkeyHandler handler);
17 [DllImport("libbeagleuiglue")]
18 static extern void tomboy_keybinder_unbind (string keystring,
19 BindkeyHandler handler);
21 public delegate void BindkeyHandler (string key, IntPtr user_data);
23 ArrayList bindings;
24 BindkeyHandler key_handler;
26 struct Binding {
27 internal string keystring;
28 internal EventHandler handler;
31 public XKeybinder ()
32 : base ()
34 bindings = new ArrayList ();
35 key_handler = new BindkeyHandler (KeybindingPressed);
37 tomboy_keybinder_init ();
40 void KeybindingPressed (string keystring, IntPtr user_data)
42 foreach (Binding bind in bindings) {
43 if (bind.keystring == keystring) {
44 bind.handler (this, new EventArgs ());
49 public void Bind (string keystring,
50 EventHandler handler)
52 Binding bind = new Binding ();
53 bind.keystring = keystring;
54 bind.handler = handler;
55 bindings.Add (bind);
57 tomboy_keybinder_bind (bind.keystring, key_handler);
60 public void Unbind (string keystring)
62 foreach (Binding bind in bindings) {
63 if (bind.keystring == keystring) {
64 tomboy_keybinder_unbind (bind.keystring,
65 key_handler);
67 bindings.Remove (bind);
68 break;
73 public virtual void UnbindAll ()
75 foreach (Binding bind in bindings) {
76 tomboy_keybinder_unbind (bind.keystring, key_handler);
79 bindings.Clear ();
83 public class GConfXKeybinder : XKeybinder
85 GConf.Client client;
86 ArrayList bindings;
88 public GConfXKeybinder ()
90 client = new GConf.Client ();
91 bindings = new ArrayList ();
94 public void Bind (string gconf_path,
95 string default_binding,
96 EventHandler handler)
98 try {
99 Binding binding = new Binding (gconf_path,
100 default_binding,
101 handler,
102 this);
103 bindings.Add (binding);
104 } catch (Exception e) {
105 Logger.Log.Error ("Error Adding global keybinding:");
106 Logger.Log.Error (e);
110 public override void UnbindAll ()
112 try {
113 bindings.Clear ();
114 base.UnbindAll ();
115 } catch (Exception e) {
116 Logger.Log.Error ("Error Removing global keybinding:");
117 Logger.Log.Error (e);
121 class Binding
123 public string gconf_path;
124 public string key_sequence;
125 EventHandler handler;
126 GConfXKeybinder parent;
128 public Binding (string gconf_path,
129 string default_binding,
130 EventHandler handler,
131 GConfXKeybinder parent)
133 this.gconf_path = gconf_path;
134 this.key_sequence = default_binding;
135 this.handler = handler;
136 this.parent = parent;
138 try {
139 key_sequence = (string) parent.client.Get (gconf_path);
140 } catch (Exception e) {
141 Logger.Log.Warn ("GConf key '{0}' does not exist, using default.",
142 gconf_path);
145 SetBinding ();
147 parent.client.AddNotify (
148 gconf_path,
149 new GConf.NotifyEventHandler (BindingChanged));
152 void BindingChanged (object sender, GConf.NotifyEventArgs args)
154 if (args.Key == gconf_path) {
155 Logger.Log.Debug ("Binding for '{0}' changed to '{1}'!",
156 gconf_path,
157 args.Value);
159 UnsetBinding ();
161 key_sequence = (string) args.Value;
162 SetBinding ();
166 public void SetBinding ()
168 if (key_sequence == null ||
169 key_sequence == String.Empty ||
170 key_sequence == "disabled")
171 return;
173 Logger.Log.Debug ("Binding key '{0}' for '{1}'",
174 key_sequence,
175 gconf_path);
177 parent.Bind (key_sequence, handler);
180 public void UnsetBinding ()
182 if (key_sequence == null)
183 return;
185 Logger.Log.Debug ("Unbinding key '{0}' for '{1}'",
186 key_sequence,
187 gconf_path);
189 parent.Unbind (key_sequence);