2004-12-07 Geoff Norton <gnorton@customerdna.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUI.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) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26 // $Revision: 1.26 $
27 // $Modtime: $
28 // $Log: XplatUI.cs,v $
29 // Revision 1.26  2004/11/08 20:53:03  pbartok
30 // - Added argument to SetTopmost
31 //
32 // Revision 1.25  2004/10/18 04:50:54  pbartok
33 // - Added method for enabling/disabling windows
34 // - Added method for setting window modality
35 // - Added method for setting topmost window
36 //
37 // Revision 1.24  2004/10/02 19:07:36  pbartok
38 // - Added ClientToScreen coordinate translation method
39 //
40 // Revision 1.23  2004/09/21 00:54:15  jackson
41 // New message loop that uses poll so we don't get a busy loop
42 //
43 // Revision 1.22  2004/09/13 21:18:32  pbartok
44 // - Added Z-Ordering methods
45 //
46 // Revision 1.21  2004/09/11 00:57:35  pbartok
47 // - Added method to retrieve text from window
48 //
49 // Revision 1.20  2004/08/24 11:29:44  jackson
50 // Move timers to the driver level. On X they are queued by the driver and checked on idle.
51 //
52 // Revision 1.19  2004/08/23 19:39:30  pbartok
53 // - Added method to move mouse cursor
54 //
55 // Revision 1.18  2004/08/21 20:51:27  pbartok
56 // - Added method to get default display size
57 //
58 // Revision 1.17  2004/08/21 20:23:56  pbartok
59 // - Added method to query current grab state
60 // - Added argument to allow confining a grab to a window
61 //
62 // Revision 1.16  2004/08/20 20:03:20  pbartok
63 // - Added method for setting the window background
64 //
65 // Revision 1.15  2004/08/20 19:14:35  jackson
66 // Expose functionality to send async messages through the driver
67 //
68 // Revision 1.14  2004/08/13 21:42:15  pbartok
69 // - Changed signature for GetCursorPos
70 //
71 // Revision 1.13  2004/08/13 19:00:15  jordi
72 // implements PointToClient (ScreenToClient)
73 //
74 // Revision 1.12  2004/08/13 18:53:14  pbartok
75 // - Changed GetWindowPos to also provide client area size
76 //
77 // Revision 1.11  2004/08/12 22:59:03  pbartok
78 // - Implemented method to get current mouse position
79 //
80 // Revision 1.10  2004/08/11 22:20:59  pbartok
81 // - Signature fixes
82 //
83 // Revision 1.9  2004/08/11 19:19:44  pbartok
84 // - We had SetWindowPos and MoveWindow to set window positions and size,
85 //   removed MoveWindow. We have GetWindowPos, so it made sense to keep
86 //   SetWindowPos as matching counterpart
87 // - Added some X11 sanity checking
88 //
89 // Revision 1.8  2004/08/11 18:55:46  pbartok
90 // - Added method to calculate difference between decorated window and raw
91 //   client area
92 //
93 // Revision 1.7  2004/08/10 17:39:22  pbartok
94 // - Added GetWindowPos method
95 //
96 // Revision 1.6  2004/08/09 20:55:59  pbartok
97 // - Removed Run method, was only required for initial development
98 //
99 // Revision 1.5  2004/08/09 20:51:25  pbartok
100 // - Implemented GrabWindow/ReleaseWindow methods to allow pointer capture
101 //
102 // Revision 1.4  2004/08/09 17:02:29  jackson
103 // Get default window properties from the theme
104 //
105 // Revision 1.3  2004/08/09 15:56:44  jackson
106 // Remove defaults, these are handled by the theme now.
107 //
108 // Revision 1.2  2004/08/04 20:11:24  pbartok
109 // - Added Invalidate handling
110 //
111 // Revision 1.1  2004/07/09 05:21:25  pbartok
112 // - Initial check-in
113 //
114 //
115
116 // NOT COMPLETE
117
118 using System;
119 using System.Drawing;
120 using System.ComponentModel;
121 using System.Collections;
122 using System.Diagnostics;
123 using System.Runtime.InteropServices;
124
125 //
126 // Random architecture notes
127 // We need
128 // * windows
129 //   - create
130 //   - set location/size
131 //   - define cursor
132 //   - destroy
133 //   - reparent?
134 //   - show/hide
135 // * Keyboard
136 // * Mouse
137 //
138
139 /// X11 Version
140 namespace System.Windows.Forms {
141         internal class XplatUI {
142                 #region Local Variables
143                 static XplatUIDriver            driver;
144                 static String                   default_class_name;
145                 #endregion      // Local Variables
146
147                 #region Subclasses
148
149                 public class State {
150                         static public Keys ModifierKeys {
151                                 get {
152                                         return driver.ModifierKeys;
153                                 }
154                         }
155
156                         static public MouseButtons MouseButtons {
157                                 get {
158                                         return driver.MouseButtons;
159                                 }
160                         }
161
162                         static public Point MousePosition {
163                                 get {
164                                         return driver.MousePosition;
165                                 }
166                         }
167
168                         static public bool DropTarget {
169                                 get {
170                                         return driver.DropTarget;
171                                 }
172
173                                 set {
174                                         driver.DropTarget=value;
175                                 }
176                         }
177                 }
178                 #endregion      // Subclasses
179
180                 #region Constructor & Destructor
181                 static XplatUI() {
182                         // Don't forget to throw the mac in here somewhere, too
183                         default_class_name="SWFClass";
184
185                         if (Environment.OSVersion.Platform == (PlatformID)128) {
186                                 if (Environment.GetEnvironmentVariable ("MONO_MWF_USE_QUARTZ_BACKEND") != null)
187                                         driver=XplatUIOSX.GetInstance();
188                                 else
189                                         driver=XplatUIX11.GetInstance();
190                         } else {
191                                 driver=XplatUIWin32.GetInstance();
192                         }
193
194                         Console.WriteLine("#region #line XplatUI Constructor called");
195                 }
196
197                 ~XplatUI() {
198                         Console.WriteLine("XplatUI Destructor called");
199                 }
200                 #endregion      // Constructor & Destructor
201
202                 #region Public Static Properties
203                 internal static string DefaultClassName {
204                         get {
205                                 return default_class_name;
206                         }
207
208                         set {
209                                 default_class_name=value;
210                         }
211                 }
212                 #endregion      // Public Static Properties
213
214                 internal static event EventHandler Idle {
215                         add {
216                                 driver.Idle += value;
217                         }
218                         remove {
219                                 driver.Idle -= value;
220                         }
221                 }
222                 
223                 #region Public Static Methods
224                 internal static void Exit() {
225                         driver.Exit();
226                 }
227
228                 internal static void GetDisplaySize(out Size size) {
229                         driver.GetDisplaySize(out size);
230                 }
231
232                 internal static void EnableThemes() {
233                         driver.EnableThemes();
234                 }
235
236                 internal static bool Text(IntPtr hWnd, string text) {
237                         return driver.Text(hWnd, text);
238                 }
239
240                 internal static bool GetText(IntPtr hWnd, out string text) {
241                         return driver.GetText(hWnd, out text);
242                 }
243
244                 internal static bool SetVisible(IntPtr hWnd, bool visible) {
245                         return driver.SetVisible(hWnd, visible);
246                 }
247
248                 internal static bool IsVisible(IntPtr hWnd) {
249                         return driver.IsVisible(hWnd);
250                 }
251
252                 internal static IntPtr SetParent(IntPtr hWnd, IntPtr hParent) {
253                         return driver.SetParent(hWnd, hParent);
254                 }
255
256                 internal static IntPtr GetParent(IntPtr hWnd) {
257                         return driver.GetParent(hWnd);
258                 }
259
260                 internal static void Version() {
261                         Console.WriteLine("Xplat version $revision: $");
262                 }
263
264                 internal static IntPtr CreateWindow(CreateParams cp) {
265                         return driver.CreateWindow(cp);
266                 }
267
268                 internal static IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
269                         return driver.CreateWindow(Parent, X, Y, Width, Height);
270                 }
271
272                 internal static void DestroyWindow(IntPtr handle) {
273                         driver.DestroyWindow(handle);
274                 }
275
276                 internal static void RefreshWindow(IntPtr handle) {
277                         driver.RefreshWindow(handle);
278                 }
279
280                 internal static void SetWindowBackground(IntPtr handle, Color color) {
281                         driver.SetWindowBackground(handle, color);
282                 }
283                         
284                 internal static PaintEventArgs PaintEventStart(IntPtr handle) {
285                         return driver.PaintEventStart(handle);
286                 }
287
288                 internal static void PaintEventEnd(IntPtr handle) {
289                         driver.PaintEventEnd(handle);
290                 }
291
292                 internal static void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
293                         driver.SetWindowPos(handle, x, y, width, height);
294                 }
295
296                 internal static void GetWindowPos(IntPtr handle, out int x, out int y, out int width, out int height, out int client_width, out int client_height) {
297                         driver.GetWindowPos(handle, out x, out y, out width, out height, out client_width, out client_height);
298                 }
299
300                 internal static void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
301                         driver.Invalidate(handle, rc, clear);
302                 }
303
304                 internal static void Activate(IntPtr handle) {
305                         driver.Activate(handle);
306                 }
307
308                 internal static void EnableWindow(IntPtr handle, bool Enable) {
309                         driver.EnableWindow(handle, Enable);
310                 }
311
312                 internal static void SetModal(IntPtr handle, bool Modal) {
313                         driver.SetModal(handle, Modal);
314                 }
315
316                 internal static IntPtr DefWndProc(ref Message msg) {
317                         return driver.DefWndProc(ref msg);
318                 }
319
320                 internal static void HandleException(Exception e) {
321                         driver.HandleException(e);
322                 }
323
324                 internal static void DoEvents() {
325                         driver.DoEvents();
326                 }
327
328                 internal static bool PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
329                         return driver.PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
330                 }
331
332                 internal static bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
333                         return driver.GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
334                 }
335
336                 internal static bool TranslateMessage(ref MSG msg) {
337                         return driver.TranslateMessage(ref msg);
338                 }
339
340                 internal static IntPtr DispatchMessage(ref MSG msg) {
341                         return driver.DispatchMessage(ref msg);
342                 }
343
344                 internal static void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
345                         driver.GrabWindow(hWnd, ConfineToHwnd);
346                 }
347
348                 internal static void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
349                         driver.GrabInfo(out hWnd, out GrabConfined, out GrabArea);
350                 }
351
352                 internal static void ReleaseWindow(IntPtr hWnd) {
353                         driver.ReleaseWindow(hWnd);
354                 }
355
356                 internal static bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
357                         return driver.SetZOrder(hWnd, AfterhWnd, Top, Bottom);
358                 }
359
360                 internal static bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
361                         return driver.SetTopmost(hWnd, hWndOwner, Enabled);
362                 }
363
364                 internal static bool CalculateWindowRect(IntPtr hWnd, ref Rectangle ClientRect, int Style, bool HasMenu, out Rectangle WindowRect) {
365                         return driver.CalculateWindowRect(hWnd, ref ClientRect, Style, HasMenu, out WindowRect);
366                 }
367
368                 internal static void SetCursorPos(IntPtr handle, int x, int y) {
369                         driver.SetCursorPos(handle, x, y);
370                 }
371
372                 internal static void GetCursorPos(IntPtr handle, out int x, out int y) {
373                         driver.GetCursorPos(handle, out x, out y);
374                 }
375
376                 internal static void ScreenToClient(IntPtr handle, ref int x, ref int y) {
377                         driver.ScreenToClient (handle, ref x, ref y);
378                 }
379
380                 internal static void ClientToScreen(IntPtr handle, ref int x, ref int y) {
381                         driver.ClientToScreen(handle, ref x, ref y);
382                 }
383
384                 internal static void SendAsyncMethod (AsyncMethodData data) {
385                         driver.SendAsyncMethod (data);
386                 }
387
388                 internal static void SetTimer (Timer timer)
389                 {
390                         driver.SetTimer (timer);
391                 }
392
393                 internal static void KillTimer (Timer timer)
394                 {
395                         driver.KillTimer (timer);
396                 }
397
398                 internal static int KeyboardSpeed {
399                         get {
400                                 return driver.KeyboardSpeed;
401                         }
402                 }
403
404                 internal static int KeyboardDelay {
405                         get {
406                                 return driver.KeyboardSpeed;
407                         }
408                 }
409                 
410                 // Santa's little helper
411                 internal static void Where() {
412                         Console.WriteLine("Here: {0}", new StackTrace().ToString());
413                 }
414                 #endregion      // Public Static Methods
415
416         }
417 }