cvsimport
[beagle.git] / Util / XKeybinder.cs
blob552423838d2da65a881491c79c197e42c4aef0ba
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 (e, "Error Adding global keybinding:");
109 public override void UnbindAll ()
111 try {
112 bindings.Clear ();
113 base.UnbindAll ();
114 } catch (Exception e) {
115 Logger.Log.Error (e, "Error Removing global keybinding:");
119 class Binding
121 public string gconf_path;
122 public string key_sequence;
123 EventHandler handler;
124 GConfXKeybinder parent;
126 public Binding (string gconf_path,
127 string default_binding,
128 EventHandler handler,
129 GConfXKeybinder parent)
131 this.gconf_path = gconf_path;
132 this.key_sequence = default_binding;
133 this.handler = handler;
134 this.parent = parent;
136 try {
137 key_sequence = (string) parent.client.Get (gconf_path);
138 } catch (Exception e) {
139 Logger.Log.Warn ("GConf key '{0}' does not exist, using default.",
140 gconf_path);
143 SetBinding ();
145 parent.client.AddNotify (
146 gconf_path,
147 new GConf.NotifyEventHandler (BindingChanged));
150 void BindingChanged (object sender, GConf.NotifyEventArgs args)
152 if (args.Key == gconf_path) {
153 Logger.Log.Debug ("Binding for '{0}' changed to '{1}'!",
154 gconf_path,
155 args.Value);
157 UnsetBinding ();
159 key_sequence = (string) args.Value;
160 SetBinding ();
164 public void SetBinding ()
166 if (key_sequence == null ||
167 key_sequence == String.Empty ||
168 key_sequence == "disabled")
169 return;
171 Logger.Log.Debug ("Binding key '{0}' for '{1}'",
172 key_sequence,
173 gconf_path);
175 parent.Bind (key_sequence, handler);
178 public void UnsetBinding ()
180 if (key_sequence == null)
181 return;
183 Logger.Log.Debug ("Unbinding key '{0}' for '{1}'",
184 key_sequence,
185 gconf_path);
187 parent.Unbind (key_sequence);