Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / KeysConverter.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 // COMPLETE
28
29 using System.Collections;
30 using System.ComponentModel;
31 using System.Text;
32
33 namespace System.Windows.Forms {
34         public class KeysConverter : TypeConverter, IComparer {
35                 #region Public Constructors
36                 public KeysConverter() {
37                 }
38                 #endregion      // Public Constructors
39
40                 #region Public Instance Methods
41                 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
42                         if (sourceType == typeof(string)) {
43                                 return true;
44                         }
45                         return false;
46                 }
47
48                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
49                 {
50                         if (destinationType == typeof (Enum[]))
51                                 return true;
52                                 
53                         return base.CanConvertTo (context, destinationType);
54                 }
55
56                 public int Compare(object a, object b) {
57                         if (a is string && b is string) {
58                                 return String.Compare((string) a, (string)b);
59                         }
60                         return String.Compare(a.ToString(), b.ToString());
61                 }
62
63                 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
64                         if (value is string) {
65                                 string[]        keys;
66                                 Keys            key;
67
68                                 keys = ((string)value).Split(new char[] {'+'});
69                                 key = Keys.None;
70
71                                 if (keys.Length > 1) {
72                                         for (int i = 0; i < keys.Length - 1; i++) {
73                                                 if (keys[i].Equals("Ctrl")) {
74                                                         key |= Keys.Control;
75                                                 } else {
76                                                         key |= (Keys)Enum.Parse(typeof(Keys), keys[i], true);
77                                                 }
78                                         }
79                                 }
80                                 if (keys [keys.Length - 1].Equals ("Ctrl"))
81                                         key |= Keys.Control;
82                                 else
83                                         key |= (Keys)Enum.Parse(typeof(Keys), keys[keys.Length - 1], true);
84                                 return key;
85                         }
86                         return base.ConvertFrom (context, culture, value);
87                 }
88
89                 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
90                         if (destinationType == typeof(string)) {
91                                 StringBuilder   sb;
92                                 Keys            key;
93
94                                 sb = new StringBuilder();
95                                 key = (Keys)value;
96
97                                 // Modifiers first
98                                 if ((key & Keys.Control) != 0) {
99                                         sb.Append("Ctrl+");
100                                 }
101
102                                 if ((key & Keys.Alt) != 0) {
103                                         sb.Append("Alt+");
104                                 }
105
106                                 if ((key & Keys.Shift) != 0) {
107                                         sb.Append("Shift+");
108                                 }
109
110                                 // Keycode last
111                                 sb.Append(Enum.GetName(typeof(Keys), key & Keys.KeyCode));
112
113                                 return sb.ToString();
114                         }
115                         return base.ConvertTo (context, culture, value, destinationType);
116                 }
117
118                 public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
119                         Keys [] stdVal = new Keys [] { Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7,
120                         Keys.D8, Keys.D9, Keys.Alt, Keys.Back, Keys.Control, Keys.Delete, Keys.End, Keys.Return, Keys.F1,
121                         Keys.F10, Keys.F11, Keys.F12, Keys.F2, Keys.F3, Keys.F4, Keys.F5, Keys.F6, Keys.F7, Keys.F8, Keys.F9,
122                         Keys.Home, Keys.Insert, Keys.Next, Keys.PageUp, Keys.Shift };
123
124                         return new TypeConverter.StandardValuesCollection (stdVal);
125                 }
126
127                 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
128                         
129                         return false;
130                 }
131
132                 public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
133                         return true;
134                 }
135
136                 #endregion      // Public Instance Methods
137         }
138 }