2005-09-08 Peter Dennis Bartok <pbartok@novell.com>
[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 int Compare(object a, object b) {
49                         if (a is string && b is string) {
50                                 return String.Compare((string) a, (string)b);
51                         }
52                         return String.Compare(a.ToString(), b.ToString());
53                 }
54
55                 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
56                         if (value is string) {
57                                 string[]        keys;
58                                 Keys            key;
59
60                                 keys = ((string)value).Split(new char[] {'+'});
61                                 key = Keys.None;
62
63                                 if (keys.Length > 1) {
64                                         for (int i = 0; i < keys.Length - 1; i++) {
65                                                 if (keys[i].Equals("Ctrl")) {
66                                                         key |= Keys.Control;
67                                                 } else {
68                                                         key |= (Keys)Enum.Parse(typeof(Keys), keys[i], true);
69                                                 }
70                                         }
71                                 }
72                                 key |= (Keys)Enum.Parse(typeof(Keys), keys[keys.Length - 1], true);
73                                 return key;
74                         }
75                         return base.ConvertFrom (context, culture, value);
76                 }
77
78                 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
79                         if (destinationType == typeof(string)) {
80                                 StringBuilder   sb;
81                                 Keys            key;
82
83                                 sb = new StringBuilder();
84                                 key = (Keys)value;
85
86                                 // Modifiers first
87                                 if ((key & Keys.Control) != 0) {
88                                         sb.Append("Ctrl+");
89                                 }
90
91                                 if ((key & Keys.Alt) != 0) {
92                                         sb.Append("Alt+");
93                                 }
94
95                                 if ((key & Keys.Shift) != 0) {
96                                         sb.Append("Shift+");
97                                 }
98
99                                 // Keycode last
100                                 sb.Append(Enum.GetName(typeof(Keys), key & Keys.KeyCode));
101
102                                 return sb.ToString();
103                         }
104                         return base.ConvertTo (context, culture, value, destinationType);
105                 }
106
107                 public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {\r
108                         string [] stdVal = new string [] {
109                                 "Alt", "Back", "Control", "Delete", "End", "Enter", "F1", "F10", "F11", "F12", "F2",
110                                 "F3", "F4", "F5", "F6", "F7", "F8", "F9", "Home", "Insert", "Next", "Prior", "Shift"};
111
112                         return new TypeConverter.StandardValuesCollection (stdVal);
113                 }\r
114 \r
115                 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {\r
116                         \r
117                         return false;\r
118                 }\r
119 \r
120                 public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {\r
121                         return true;\r
122                 }\r
123 \r
124                 #endregion      // Public Instance Methods
125         }
126 }