~ubuntu-branches/ubuntu/trusty/gnome-do/trusty

« back to all changes in this revision

Viewing changes to Do.Platform/src/Do.Platform/Do.Platform.Common/AbstractKeyBindingService.cs

  • Committer: Package Import Robot
  • Author(s): Christopher James Halse Rogers
  • Date: 2012-03-26 11:12:21 UTC
  • mfrom: (0.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20120326111221-1jk143fy37zxi3e4
Tags: 0.9-1
* New upstream version no longer uses deprecated internal glib headers.
  (Closes: #665537)
* [59fa37b9] Fix watch file
* [63486516] Imported Upstream version 0.9
* [8c636d84] Disable testsuite for now; requires running dbus and gconf daemons
* [e46de4b9] Remove inaccurate README.Source
* [4591d677] Add git-buildpackage configuration to default to pristine-tar

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using System;
2
 
using System.Collections.Generic;
3
 
using System.Linq;
4
 
using System.Text;
5
 
 
6
 
using Mono.Unix;
7
 
 
8
 
using Do.Platform.Common;
9
 
 
10
 
namespace Do.Platform.Common
11
 
{
12
 
        public abstract class AbstractKeyBindingService : IKeyBindingService
13
 
        {
14
 
 
15
 
                public abstract bool RegisterOSKey (string keyString, EventCallback cb);
16
 
                public abstract bool UnRegisterOSKey (string keyString);
17
 
 
18
 
                IPreferences prefs;
19
 
 
20
 
#region IInitializedService
21
 
 
22
 
                public void Initialize () 
23
 
                {
24
 
                        Bindings = new List<KeyBinding> ();     
25
 
                        prefs = Services.Preferences.Get<AbstractKeyBindingService> ();
26
 
                }
27
 
 
28
 
#endregion
29
 
 
30
 
#region IKeyBindingService
31
 
 
32
 
                public List<KeyBinding> Bindings { get; private set; }
33
 
 
34
 
                public bool RegisterKeyBinding (KeyBinding binding) 
35
 
                {
36
 
                        //try to get the keystring from the prefs.  We default to the KeyBinding.KeyString, so we can later check
37
 
                        //if the prefs value matches that, we're using the default, otherwise we're using a user specified value
38
 
                        string prefsKeyString = prefs.Get (binding.PreferenceName, binding.KeyString);
39
 
                        
40
 
                        //if these values don't match then the user has specified a new keystring
41
 
                        //update the KeyEvent then continue
42
 
                        if (prefsKeyString != binding.KeyString)
43
 
                                binding.KeyString = prefsKeyString;
44
 
                        
45
 
                        //check if this keystring is already used
46
 
                        if (Bindings.Any (k => k.KeyString == binding.KeyString)) {
47
 
                                Log<AbstractKeyBindingService>.Error ("Key \"{0}\" is already mapped.", binding.KeyString);
48
 
                                binding.KeyString = "";
49
 
                        }
50
 
 
51
 
                        //if we are registering a key with the OS, do something special
52
 
                        if (binding.IsOSKey) {
53
 
                                //try to register the key from the prefs with the OS
54
 
                                if (!RegisterOSKey (binding.KeyString, binding.Callback)) {
55
 
                                        //if we fail to register the summon key, try again with the default binding
56
 
                                        if (RegisterOSKey (binding.DefaultKeyString, binding.Callback)) {
57
 
                                                binding.KeyString = binding.DefaultKeyString;
58
 
                                        } else {
59
 
                                                Log<AbstractKeyBindingService>.Error ("Failed to bind \"{0}\" to \"{1}\"", binding.Description, 
60
 
                                                        binding.KeyString);
61
 
                                                binding.KeyString = "";
62
 
                                        }
63
 
                                }
64
 
                        }
65
 
 
66
 
                        //add the event to the list of bindings
67
 
                        Bindings.Add (binding);
68
 
                        //set the bound keystring in the prefs
69
 
                        prefs.Set (binding.Description.Replace (' ', '_'), binding.KeyString);
70
 
 
71
 
                        return true;
72
 
                }
73
 
 
74
 
                public bool SetKeyString (KeyBinding binding, string newKeyString) 
75
 
                {
76
 
                        //first check if this keystring exists
77
 
                        if (!Bindings.Any (k => k.KeyString == binding.KeyString))
78
 
                                return false;
79
 
                        
80
 
                        //if this key should be registered with the OS
81
 
                        if (binding.IsOSKey) {
82
 
                                //remove the old keystring from the OS
83
 
                                UnRegisterOSKey (binding.KeyString);
84
 
                                //register again with the new keystring
85
 
                                RegisterOSKey (newKeyString, binding.Callback);
86
 
                        }
87
 
 
88
 
                        //set the new keystring
89
 
                        Bindings.First (k => k.Description == binding.Description).KeyString = newKeyString;
90
 
                        
91
 
                        //save the new value in the prefs
92
 
                        prefs.Set (binding.Description.Replace (' ', '_'), binding.KeyString);
93
 
 
94
 
                        if (!string.IsNullOrEmpty (binding.KeyString))
95
 
                            Log<AbstractKeyBindingService>.Debug ("\"{0}\" now mapped to \"{1}\"", binding.Description, binding.KeyString);
96
 
 
97
 
                        return true;
98
 
                }
99
 
                
100
 
                /// <summary>
101
 
                /// Converts a keypress into a human readable string for comparing
102
 
                /// against values in GConf.
103
 
                /// </summary>
104
 
                /// <param name="evnt">
105
 
                /// A <see cref="EventKey"/>
106
 
                /// </param>
107
 
                /// <returns>
108
 
                /// A <see cref="System.String"/> in the form "<Modifier>key"
109
 
                /// </returns>
110
 
                public string KeyEventToString (Gdk.EventKey evnt) {
111
 
                        string modifier = "";
112
 
                        if ((evnt.State & Gdk.ModifierType.ControlMask) != 0) {
113
 
                                modifier += "<Control>";
114
 
                        }
115
 
                        if ((evnt.State & Gdk.ModifierType.SuperMask) != 0) {
116
 
                                modifier += "<Super>";
117
 
                        }
118
 
                        if ((evnt.State & Gdk.ModifierType.Mod1Mask) != 0) {
119
 
                                modifier += "<Alt>";
120
 
                        }
121
 
                        if ((evnt.State & Gdk.ModifierType.ShiftMask) != 0) {
122
 
                                modifier += "<Shift>";
123
 
                                //if we're pressing shift, and the key is ISO_Left_Tab,
124
 
                                //just make it Tab
125
 
                                if (evnt.Key == Gdk.Key.ISO_Left_Tab)
126
 
                                        return string.Format ("{0}{1}", modifier, Gdk.Key.Tab);
127
 
                        }
128
 
                        return string.Format ("{0}{1}", modifier, Gtk.Accelerator.Name (evnt.KeyValue, Gdk.ModifierType.None));
129
 
                }
130
 
#endregion
131
 
        }
132
 
}
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Linq;
 
4
using System.Text;
 
5
 
 
6
using Mono.Unix;
 
7
 
 
8
namespace Do.Platform.Common
 
9
{
 
10
        public abstract class AbstractKeyBindingService : IKeyBindingService
 
11
        {
 
12
 
 
13
                public abstract bool RegisterOSKey (string keyString, EventCallback cb);
 
14
                public abstract bool UnRegisterOSKey (string keyString);
 
15
 
 
16
                IPreferences prefs;
 
17
 
 
18
#region IInitializedService
 
19
 
 
20
                public void Initialize () 
 
21
                {
 
22
                        Bindings = new List<KeyBinding> ();     
 
23
                        prefs = Services.Preferences.Get<AbstractKeyBindingService> ();
 
24
                }
 
25
 
 
26
#endregion
 
27
 
 
28
#region IKeyBindingService
 
29
 
 
30
                public List<KeyBinding> Bindings { get; private set; }
 
31
 
 
32
                public bool RegisterKeyBinding (KeyBinding binding) 
 
33
                {
 
34
                        //try to get the keystring from the prefs.  We default to the KeyBinding.KeyString, so we can later check
 
35
                        //if the prefs value matches that, we're using the default, otherwise we're using a user specified value
 
36
                        string prefsKeyString = prefs.Get (binding.PreferenceName, binding.KeyString);
 
37
                        
 
38
                        //if these values don't match then the user has specified a new keystring
 
39
                        //update the KeyEvent then continue
 
40
                        if (prefsKeyString != binding.KeyString)
 
41
                                binding.KeyString = prefsKeyString;
 
42
                        
 
43
                        //check if this keystring is already used
 
44
                        if (Bindings.Any (k => k.KeyString == binding.KeyString)) {
 
45
                                Log<AbstractKeyBindingService>.Error ("Key \"{0}\" is already mapped.", binding.KeyString);
 
46
                                binding.KeyString = "";
 
47
                        }
 
48
 
 
49
                        //if we are registering a key with the OS, do something special
 
50
                        if (binding.IsOSKey) {
 
51
                                //try to register the key from the prefs with the OS
 
52
                                if (!RegisterOSKey (binding.KeyString, binding.Callback)) {
 
53
                                        //if we fail to register the summon key, try again with the default binding
 
54
                                        if (!string.IsNullOrEmpty (binding.DefaultKeyString) && RegisterOSKey (binding.DefaultKeyString, binding.Callback)) {
 
55
                                                binding.KeyString = binding.DefaultKeyString;
 
56
                                        } else if (!string.IsNullOrEmpty (binding.KeyString) && !string.IsNullOrEmpty (binding.DefaultKeyString)) {
 
57
                                                Log<AbstractKeyBindingService>.Error ("Failed to bind \"{0}\" to \"{1}\"", binding.Description, 
 
58
                                                        binding.KeyString);
 
59
                                                binding.KeyString = "";
 
60
                                        }
 
61
                                }
 
62
                        }
 
63
 
 
64
                        //add the event to the list of bindings
 
65
                        Bindings.Add (binding);
 
66
                        //set the bound keystring in the prefs
 
67
                        prefs.Set (binding.PreferenceName, binding.KeyString);
 
68
 
 
69
                        return true;
 
70
                }
 
71
 
 
72
                public bool SetKeyString (KeyBinding binding, string newKeyString) 
 
73
                {
 
74
                        //first check if this keystring exists
 
75
                        if (!Bindings.Any (k => k.KeyString == binding.KeyString))
 
76
                                return false;
 
77
                        
 
78
                        //if this key should be registered with the OS
 
79
                        if (binding.IsOSKey) {
 
80
                                //remove the old keystring from the OS
 
81
                                UnRegisterOSKey (binding.KeyString);
 
82
                                //register again with the new keystring
 
83
                                RegisterOSKey (newKeyString, binding.Callback);
 
84
                        }
 
85
 
 
86
                        //set the new keystring
 
87
                        Bindings.First (k => k.Description == binding.Description).KeyString = newKeyString;
 
88
                        
 
89
                        //save the new value in the prefs
 
90
                        prefs.Set (binding.PreferenceName, binding.KeyString);
 
91
 
 
92
                        if (!string.IsNullOrEmpty (binding.KeyString))
 
93
                            Log<AbstractKeyBindingService>.Debug ("\"{0}\" now mapped to \"{1}\"", binding.Description, binding.KeyString);
 
94
 
 
95
                        return true;
 
96
                }
 
97
                
 
98
                /// <summary>
 
99
                /// Converts a keypress into a human readable string for comparing
 
100
                /// against values in GConf.
 
101
                /// </summary>
 
102
                /// <param name="evnt">
 
103
                /// A <see cref="EventKey"/>
 
104
                /// </param>
 
105
                /// <returns>
 
106
                /// A <see cref="System.String"/> in the form "<Modifier>key"
 
107
                /// </returns>
 
108
                public string KeyEventToString (uint keycode, uint modifierCode) {
 
109
                        // FIXME: This should really use Gtk.Accelerator.Name (key, modifier)
 
110
                        // Beware of bug #903566 when doing that!
 
111
                        
 
112
                        string modifier = "";
 
113
                        if ((modifierCode & (uint)Gdk.ModifierType.ControlMask) != 0) {
 
114
                                modifier += "<Control>";
 
115
                        }
 
116
                        if ((modifierCode & (uint)Gdk.ModifierType.SuperMask) != 0) {
 
117
                                modifier += "<Super>";
 
118
                        }
 
119
                        if ((modifierCode & (uint)Gdk.ModifierType.Mod1Mask) != 0) {
 
120
                                modifier += "<Alt>";
 
121
                        }
 
122
                        if ((modifierCode & (uint)Gdk.ModifierType.ShiftMask) != 0) {
 
123
                                modifier += "<Shift>";
 
124
                                //if we're pressing shift, and the key is ISO_Left_Tab,
 
125
                                //just make it Tab
 
126
                                if (keycode == (uint)Gdk.Key.ISO_Left_Tab)
 
127
                                        return string.Format ("{0}{1}", modifier, Gdk.Key.Tab);
 
128
                        }
 
129
                        return string.Format ("{0}{1}", modifier, Gtk.Accelerator.Name (keycode, Gdk.ModifierType.None));
 
130
                }
 
131
#endregion
 
132
        }
 
133
}