* TreeView.cs: Don't draw the selected node when we lose
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / SendKeys.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 // NOT COMPLETE
28
29 using System.Collections;
30
31 namespace System.Windows.Forms {
32         public class SendKeys {
33                 #region Local variables
34                 private static Queue    keys = new Queue();
35                 #endregion
36
37                 #region Private methods
38                 private SendKeys() {
39                 }
40
41                 private static IntPtr CharToVKey(char key) {
42                         // FIXME - build a table to translate between vkeys and chars
43                         throw new NotImplementedException();
44                 }
45
46                 private static IntPtr SymbolToVKey(string symbol) {
47                         // FIXME - build a table to translate between vkeys and symbols
48                         throw new NotImplementedException();
49                 }
50
51                 private static void SendSymbol(IntPtr hwnd, string symbol, int repeat_count, bool down) {
52                         MSG     msg;
53
54                         if (down) {
55                                 for (int i = 0; i < repeat_count; i++ ) {
56                                         msg = new MSG();
57                                         msg.hwnd = hwnd;
58                                         msg.message = Msg.WM_KEYDOWN;
59                                         msg.wParam = SymbolToVKey(symbol);
60                                         msg.lParam = IntPtr.Zero;
61
62                                         keys.Enqueue(msg);
63                                 }
64                         } else {
65                                 msg = new MSG();
66                                 msg.hwnd = hwnd;
67                                 msg.message = Msg.WM_KEYUP;
68                                 msg.wParam = SymbolToVKey(symbol);
69                                 msg.lParam = IntPtr.Zero;
70
71                                 keys.Enqueue(msg);
72                         }
73                 }
74
75                 private static void SendKey(IntPtr hwnd, char key, int repeat_count) {
76                         MSG     msg;
77
78                         for (int i = 0; i < repeat_count; i++ ) {
79                                 msg = new MSG();
80                                 msg.hwnd = hwnd;
81                                 msg.message = Msg.WM_KEYDOWN;
82                                 msg.wParam = CharToVKey(key);
83                                 msg.lParam = IntPtr.Zero;
84
85                                 keys.Enqueue(msg);
86                         }
87
88                         msg = new MSG();
89                         msg.hwnd = hwnd;
90                         msg.message = Msg.WM_KEYUP;
91                         msg.wParam = CharToVKey(key);
92                         msg.lParam = IntPtr.Zero;
93
94                         keys.Enqueue(msg);
95                 }
96
97                 #endregion      // Private Methods
98
99                 #region Public Static Methods
100                 public static void Flush() {
101                         MSG msg;
102
103                         // FIXME - we only send to our own app, instead of the active app
104                         while (keys.Count > 0) {
105                                 msg = (MSG)keys.Dequeue();
106                                 XplatUI.TranslateMessage (ref msg);
107                                 XplatUI.DispatchMessage (ref msg);
108                         }
109                 }
110
111                 [MonoTODO("Finish")]
112                 public static void Send(string key_string) {
113                         IntPtr  hwnd;
114                         int     shift_reset;
115                         int     control_reset;
116                         int     alt_reset;
117
118                         hwnd = XplatUI.GetActive();
119
120                         shift_reset = 0;
121                         control_reset = 0;
122                         alt_reset = 0;
123
124                         for (int i = 0; i < key_string.Length; i++) {
125                                 switch(key_string[i]) {
126                                         case '+': {
127                                                 SendSymbol(hwnd, "SHIFT", 1, true);
128                                                 shift_reset = 2;
129                                                 break;
130                                         }
131
132                                         case '^': {
133                                                 SendSymbol(hwnd, "CONTROL", 1, true);
134                                                 control_reset = 2;
135                                                 break;
136                                         }
137
138                                         case '%': {
139                                                 SendSymbol(hwnd, "ALT", 1, true);
140                                                 alt_reset = 2;
141                                                 break;
142                                         }
143
144                                         case '~': {
145                                                 SendSymbol(hwnd, "ENTER", 1, true);
146                                                 SendSymbol(hwnd, "ENTER", 1, false);
147                                                 break;
148                                         }
149
150                                         case '(':
151                                         case ')': {
152                                                 // FIXME - implement group parser
153                                                 break;
154                                         }
155
156                                         case '{':
157                                         case '}': {
158                                                 // FIXME - implement symbol parser
159                                                 break;
160                                         }
161
162                                         default: {
163                                                 SendKey(hwnd, key_string[i], 1);
164                                                 break;
165                                         }
166                                 }
167
168                                 
169
170                                 if (shift_reset > 0) {
171                                         shift_reset--;
172                                         if (shift_reset == 0) {
173                                                 SendSymbol(hwnd, "SHIFT", 1, false);
174                                         }
175                                 }
176
177                                 if (control_reset > 0) {
178                                         control_reset--;
179                                         if (control_reset == 0) {
180                                                 SendSymbol(hwnd, "CONTROL", 1, false);
181                                         }
182                                 }
183
184                                 if (alt_reset > 0) {
185                                         alt_reset--;
186                                         if (alt_reset == 0) {
187                                                 SendSymbol(hwnd, "ALT", 1, false);
188                                         }
189                                 }
190                         }
191                 }
192
193                 public static void SendWait(string keys) {
194                         Send(keys);
195                         Flush();
196                 }
197                 #endregion      // Public Instance Properties
198         }
199 }