3 using System
.Collections
;
4 using System
.Runtime
.InteropServices
;
9 public class XKeybinder
11 [DllImport("libkeyglue")]
12 static extern void tomboy_keybinder_init ();
14 [DllImport("libkeyglue")]
15 static extern void tomboy_keybinder_bind (string keystring
,
16 BindkeyHandler handler
);
18 [DllImport("libkeyglue")]
19 static extern void tomboy_keybinder_unbind (string keystring
,
20 BindkeyHandler handler
);
22 public delegate void BindkeyHandler (string key
, IntPtr user_data
);
25 BindkeyHandler key_handler
;
28 internal string keystring
;
29 internal EventHandler handler
;
35 bindings
= new ArrayList ();
36 key_handler
= new BindkeyHandler (KeybindingPressed
);
38 tomboy_keybinder_init ();
41 void KeybindingPressed (string keystring
, IntPtr user_data
)
43 foreach (Binding bind
in bindings
) {
44 if (bind
.keystring
== keystring
) {
45 bind
.handler (this, new EventArgs ());
50 public void Bind (string keystring
,
53 Binding bind
= new Binding ();
54 bind
.keystring
= keystring
;
55 bind
.handler
= handler
;
58 tomboy_keybinder_bind (bind
.keystring
, key_handler
);
61 public void Unbind (string keystring
)
63 foreach (Binding bind
in bindings
) {
64 if (bind
.keystring
== keystring
) {
65 tomboy_keybinder_unbind (bind
.keystring
,
68 bindings
.Remove (bind
);
74 public virtual void UnbindAll ()
76 foreach (Binding bind
in bindings
) {
77 tomboy_keybinder_unbind (bind
.keystring
, key_handler
);
84 public class GConfXKeybinder
: XKeybinder
89 public GConfXKeybinder ()
91 client
= new GConf
.Client ();
92 bindings
= new ArrayList ();
95 public void Bind (string gconf_path
,
96 string default_binding
,
100 Binding binding
= new Binding (gconf_path
,
104 bindings
.Add (binding
);
105 } catch (Exception e
) {
106 Logger
.Log
.Error ("Error Adding global keybinding:");
107 Logger
.Log
.Error (e
);
111 public override void UnbindAll ()
116 } catch (Exception e
) {
117 Logger
.Log
.Error ("Error Removing global keybinding:");
118 Logger
.Log
.Error (e
);
124 public string gconf_path
;
125 public string key_sequence
;
126 EventHandler handler
;
127 GConfXKeybinder parent
;
129 public Binding (string gconf_path
,
130 string default_binding
,
131 EventHandler handler
,
132 GConfXKeybinder parent
)
134 this.gconf_path
= gconf_path
;
135 this.key_sequence
= default_binding
;
136 this.handler
= handler
;
137 this.parent
= parent
;
140 key_sequence
= (string) parent
.client
.Get (gconf_path
);
141 } catch (Exception e
) {
142 Logger
.Log
.Warn ("GConf key '{0}' does not exist, using default.",
148 parent
.client
.AddNotify (
150 new GConf
.NotifyEventHandler (BindingChanged
));
153 void BindingChanged (object sender
, GConf
.NotifyEventArgs args
)
155 if (args
.Key
== gconf_path
) {
156 Logger
.Log
.Debug ("Binding for '{0}' changed to '{1}'!",
162 key_sequence
= (string) args
.Value
;
167 public void SetBinding ()
169 if (key_sequence
== null ||
170 key_sequence
== String
.Empty
||
171 key_sequence
== "disabled")
174 Logger
.Log
.Debug ("Binding key '{0}' for '{1}'",
178 parent
.Bind (key_sequence
, handler
);
181 public void UnsetBinding ()
183 if (key_sequence
== null)
186 Logger
.Log
.Debug ("Unbinding key '{0}' for '{1}'",
190 parent
.Unbind (key_sequence
);