* X11Keyboard.cs: Detect and use the num lock mask.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUIWin32.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.43 $
27 // $Modtime: $
28 // $Log: XplatUIWin32.cs,v $
29 // Revision 1.43  2004/11/08 20:55:33  pbartok
30 // - Added argument to SetTopmost method
31 // - Fixed broken ClientToScreen function
32 //
33 // Revision 1.42  2004/10/20 03:58:05  pbartok
34 // - Small sanity check
35 //
36 // Revision 1.41  2004/10/18 05:17:32  pbartok
37 // - Removed VirtualKeys to XplatUIStructs
38 // - Implemented SetTopMost method
39 // - Implemented EnableWindow method
40 // - Bugfix in ScreenToClient()
41 // - Bugfixes in ClientToScreen()
42 //
43 // Revision 1.40  2004/10/13 20:18:21  pbartok
44 // - Added code to destroy a window
45 //
46 // Revision 1.39  2004/10/06 09:59:05  jordi
47 // removes warnings from compilation
48 //
49 // Revision 1.38  2004/10/02 19:08:38  pbartok
50 // - Added Win32EnableWindow method (test for implementing modal dialogs)
51 // - Added ClientToScreen method and imports
52 //
53 // Revision 1.37  2004/09/21 04:14:29  pbartok
54 // - Fixed accessibility level for Idle handler
55 //
56 // Revision 1.36  2004/09/21 00:54:15  jackson
57 // New message loop that uses poll so we don't get a busy loop
58 //
59 // Revision 1.35  2004/09/16 23:45:09  pbartok
60 // - Fixed sending a window to the front
61 // - Added overload for SetWindowPos to avoid casting
62 //
63 // Revision 1.34  2004/09/13 21:18:32  pbartok
64 // - Added Z-Ordering methods
65 //
66 // Revision 1.33  2004/09/11 00:57:35  pbartok
67 // - Added method to retrieve text from window
68 //
69 // Revision 1.32  2004/08/25 18:33:08  pbartok
70 // - Fixed timer handling, now seems to work
71 // - Improved error message for window creation
72 //
73 // Revision 1.31  2004/08/24 17:17:27  pbartok
74 // - Implemented SetTimer() and KillTimer()
75 //
76 // Revision 1.30  2004/08/24 11:29:44  jackson
77 // Move timers to the driver level. On X they are queued by the driver and checked on idle.
78 //
79 // Revision 1.29  2004/08/24 00:19:36  ravindra
80 // Removed the unwanted destructor.
81 //
82 // Revision 1.28  2004/08/23 19:39:30  pbartok
83 // - Added method to move mouse cursor
84 //
85 // Revision 1.27  2004/08/21 20:51:27  pbartok
86 // - Added method to get default display size
87 //
88 // Revision 1.26  2004/08/21 20:23:56  pbartok
89 // - Added method to query current grab state
90 // - Added argument to allow confining a grab to a window
91 //
92 // Revision 1.25  2004/08/21 18:35:38  pbartok
93 // - Fixed bug with Async message handling
94 // - Implemented getting the ModifierKeys
95 //
96 // Revision 1.24  2004/08/21 17:31:21  pbartok
97 // - Drivers now return proper mouse state
98 //
99 // Revision 1.23  2004/08/20 20:39:07  pbartok
100 // - Added jackson's Async code from X11 to Win32
101 //
102 // Revision 1.22  2004/08/20 20:02:45  pbartok
103 // - Added method for setting the background color
104 // - Added handling for erasing the window background
105 //
106 // Revision 1.21  2004/08/20 19:14:35  jackson
107 // Expose functionality to send async messages through the driver
108 //
109 // Revision 1.20  2004/08/20 01:37:47  pbartok
110 // - Added generation of MouseEnter, MouseLeave and MouseHover events
111 // - Added cleanup on EndPaint
112 //
113 // Revision 1.19  2004/08/18 19:16:53  jordi
114 // Move colors to a table
115 //
116 // Revision 1.18  2004/08/17 21:24:03  pbartok
117 // - Finished IsVisible
118 // - Added Win32GetWindowPlacement
119 //
120 // Revision 1.17  2004/08/13 21:42:15  pbartok
121 // - Changed signature for GetCursorPos
122 //
123 // Revision 1.16  2004/08/13 19:00:15  jordi
124 // implements PointToClient (ScreenToClient)
125 //
126 // Revision 1.15  2004/08/13 18:53:57  pbartok
127 // - Changed GetWindowPos to also provide client area size
128 // - Fixed broken prototypes for several win32 functions
129 //
130 // Revision 1.14  2004/08/12 22:59:03  pbartok
131 // - Implemented method to get current mouse position
132 //
133 // Revision 1.13  2004/08/11 22:20:59  pbartok
134 // - Signature fixes
135 //
136 // Revision 1.12  2004/08/11 19:41:38  jordi
137 // Fixes ClientRect
138 //
139 // Revision 1.11  2004/08/11 19:19:44  pbartok
140 // - We had SetWindowPos and MoveWindow to set window positions and size,
141 //   removed MoveWindow. We have GetWindowPos, so it made sense to keep
142 //   SetWindowPos as matching counterpart
143 // - Added some X11 sanity checking
144 //
145 // Revision 1.10  2004/08/11 18:55:46  pbartok
146 // - Added method to calculate difference between decorated window and raw
147 //   client area
148 //
149 // Revision 1.9  2004/08/10 18:47:16  jordi
150 // Calls InvalidateRect before UpdateWindow
151 //
152 // Revision 1.8  2004/08/10 17:36:17  pbartok
153 // - Implemented several methods
154 //
155 // Revision 1.7  2004/08/09 20:55:59  pbartok
156 // - Removed Run method, was only required for initial development
157 //
158 // Revision 1.6  2004/08/09 20:51:25  pbartok
159 // - Implemented GrabWindow/ReleaseWindow methods to allow pointer capture
160 //
161 // Revision 1.5  2004/08/09 16:05:16  jackson
162 // These properties are handled by the theme now.
163 //
164 // Revision 1.4  2004/08/06 15:53:39  jordi
165 // X11 keyboard navigation
166 //
167 // Revision 1.3  2004/08/04 20:11:24  pbartok
168 // - Added Invalidate handling
169 //
170 // Revision 1.2  2004/07/21 16:19:17  jordi
171 // LinkLabel control implementation
172 //
173 // Revision 1.1  2004/07/09 05:21:25  pbartok
174 // - Initial check-in
175 //
176 //
177
178 // NOT COMPLETE
179
180 using System;
181 using System.Drawing;
182 using System.ComponentModel;
183 using System.Collections;
184 using System.Diagnostics;
185 using System.Runtime.InteropServices;
186 using System.Text;
187
188 /// Win32 Version
189 namespace System.Windows.Forms {
190         internal class XplatUIWin32 : XplatUIDriver {
191                 #region Local Variables
192                 private static XplatUIWin32     instance;
193                 private static int              ref_count;
194                 private static IntPtr           FosterParent;
195
196                 internal static MouseButtons    mouse_state;
197                 internal static Point           mouse_position;
198                 internal static bool            grab_confined;
199                 internal static IntPtr          grab_hwnd;
200                 internal static Rectangle       grab_area;
201                 internal static WndProc         wnd_proc;
202                 internal static IntPtr          prev_mouse_hwnd;
203
204                 internal static bool            themes_enabled;
205                 private static Hashtable        handle_data;
206                 private Hashtable               timer_list;
207                 #endregion      // Local Variables
208
209                 #region Private Structs
210                 [StructLayout(LayoutKind.Sequential)]\r
211                 private struct WNDCLASS {\r
212                         internal int            style;\r
213                         internal WndProc        lpfnWndProc;\r
214                         internal int            cbClsExtra;\r
215                         internal int            cbWndExtra;\r
216                         internal IntPtr         hInstance;\r
217                         internal IntPtr         hIcon;\r
218                         internal IntPtr         hCursor;\r
219                         internal IntPtr         hbrBackground;\r
220                         internal string         lpszMenuName;\r
221                         internal string         lpszClassName;\r
222                 }
223
224                 [StructLayout(LayoutKind.Sequential)]\r
225                 private struct RECT {\r
226                         internal int            left;\r
227                         internal int            top;\r
228                         internal int            right;\r
229                         internal int            bottom;\r
230                 }
231 \r
232                 [StructLayout(LayoutKind.Sequential)]\r
233                 private struct POINT {\r
234                         internal int            x;\r
235                         internal int            y;\r
236                 }
237
238                 internal enum WindowPlacementFlags {
239                         SW_HIDE                 = 0,\r
240                         SW_SHOWNORMAL           = 1,\r
241                         SW_NORMAL               = 1,\r
242                         SW_SHOWMINIMIZED        = 2,\r
243                         SW_SHOWMAXIMIZED        = 3,\r
244                         SW_MAXIMIZE             = 3,\r
245                         SW_SHOWNOACTIVATE       = 4,\r
246                         SW_SHOW                 = 5,\r
247                         SW_MINIMIZE             = 6,\r
248                         SW_SHOWMINNOACTIVE      = 7,\r
249                         SW_SHOWNA               = 8,\r
250                         SW_RESTORE              = 9,\r
251                         SW_SHOWDEFAULT          = 10,\r
252                         SW_FORCEMINIMIZE        = 11,\r
253                         SW_MAX                  = 11
254                 }
255
256                 [StructLayout(LayoutKind.Sequential)]\r
257                 private struct WINDOWPLACEMENT {\r
258                         internal uint                   length;\r
259                         internal uint                   flags;\r
260                         internal WindowPlacementFlags   showCmd;\r
261                         internal POINT                  ptMinPosition;\r
262                         internal POINT                  ptMaxPosition;\r
263                         internal RECT                   rcNormalPosition;\r
264                 }\r
265 \r
266                 [Flags]\r
267                 private enum TMEFlags {\r
268                         TME_HOVER               = 0x00000001,\r
269                         TME_LEAVE               = 0x00000002,\r
270                         TME_QUERY               = unchecked((int)0x40000000),\r
271                         TME_CANCEL              = unchecked((int)0x80000000)\r
272                 }\r
273 \r
274                 [StructLayout(LayoutKind.Sequential)]\r
275                 private struct TRACKMOUSEEVENT {\r
276                         internal int            size;\r
277                         internal TMEFlags       dwFlags;\r
278                         internal IntPtr         hWnd;\r
279                         internal int            dwHoverTime;\r
280                 }\r
281 \r
282                 [StructLayout(LayoutKind.Sequential)]\r
283                 private struct PAINTSTRUCT {\r
284                         internal IntPtr         hdc;\r
285                         internal int            fErase;\r
286                         internal RECT           rcPaint;\r
287                         internal int            fRestore;\r
288                         internal int            fIncUpdate;\r
289                         internal int            Reserved1;\r
290                         internal int            Reserved2;\r
291                         internal int            Reserved3;\r
292                         internal int            Reserved4;\r
293                         internal int            Reserved5;\r
294                         internal int            Reserved6;\r
295                         internal int            Reserved7;\r
296                         internal int            Reserved8;\r
297                 }
298 \r
299                 internal enum ClassStyle {\r
300                         CS_VREDRAW                      = 0x00000001,\r
301                         CS_HREDRAW                      = 0x00000002,\r
302                         CS_KEYCVTWINDOW                 = 0x00000004,\r
303                         CS_DBLCLKS                      = 0x00000008,\r
304                         CS_OWNDC                        = 0x00000020,\r
305                         CS_CLASSDC                      = 0x00000040,\r
306                         CS_PARENTDC                     = 0x00000080,\r
307                         CS_NOKEYCVT                     = 0x00000100,\r
308                         CS_NOCLOSE                      = 0x00000200,\r
309                         CS_SAVEBITS                     = 0x00000800,\r
310                         CS_BYTEALIGNCLIENT              = 0x00001000,\r
311                         CS_BYTEALIGNWINDOW              = 0x00002000,\r
312                         CS_GLOBALCLASS                  = 0x00004000,\r
313                         CS_IME                          = 0x00010000\r
314                 }\r
315 \r
316                 internal enum PeekMessageFlags {\r
317                         PM_NOREMOVE                     = 0x00000000,\r
318                         PM_REMOVE                       = 0x00000001,\r
319                         PM_NOYIELD                      = 0x00000002\r
320                 }
321
322                 internal enum SetWindowPosZOrder {
323                         HWND_TOP                        = 0,
324                         HWND_BOTTOM                     = 1,
325                         HWND_TOPMOST                    = -1,
326                         HWND_NOTOPMOST                  = -2
327                 }
328
329                 [Flags]
330                 internal enum SetWindowPosFlags {
331                         SWP_ASYNCWINDOWPOS              = 0x4000, 
332                         SWP_DEFERERASE                  = 0x2000,
333                         SWP_DRAWFRAME                   = 0x0020,
334                         SWP_FRAMECHANGED                = 0x0020,
335                         SWP_HIDEWINDOW                  = 0x0080,
336                         SWP_NOACTIVATE                  = 0x0010,
337                         SWP_NOCOPYBITS                  = 0x0100,
338                         SWP_NOMOVE                      = 0x0002,
339                         SWP_NOOWNERZORDER               = 0x0200,
340                         SWP_NOREDRAW                    = 0x0008,
341                         SWP_NOREPOSITION                = 0x0200,
342                         SWP_NOENDSCHANGING              = 0x0400,
343                         SWP_NOSIZE                      = 0x0001,
344                         SWP_NOZORDER                    = 0x0004,
345                         SWP_SHOWWINDOW                  = 0x0040
346                 }
347
348                 internal enum GetSysColorIndex {\r
349                         COLOR_SCROLLBAR                 =0,\r
350                         COLOR_BACKGROUND                =1,\r
351                         COLOR_ACTIVECAPTION             =2,\r
352                         COLOR_INACTIVECAPTION           =3,\r
353                         COLOR_MENU                      =4,\r
354                         COLOR_WINDOW                    =5,\r
355                         COLOR_WINDOWFRAME               =6,\r
356                         COLOR_MENUTEXT                  =7,\r
357                         COLOR_WINDOWTEXT                =8,\r
358                         COLOR_CAPTIONTEXT               =9,\r
359                         COLOR_ACTIVEBORDER              =10,\r
360                         COLOR_INACTIVEBORDER            =11,\r
361                         COLOR_APPWORKSPACE              =12,\r
362                         COLOR_HIGHLIGHT                 =13,\r
363                         COLOR_HIGHLIGHTTEXT             =14,\r
364                         COLOR_BTNFACE                   =15,\r
365                         COLOR_BTNSHADOW                 =16,\r
366                         COLOR_GRAYTEXT                  =17,\r
367                         COLOR_BTNTEXT                   =18,\r
368                         COLOR_INACTIVECAPTIONTEXT       =19,\r
369                         COLOR_BTNHIGHLIGHT              =20,\r
370                         COLOR_3DDKSHADOW                =21,\r
371                         COLOR_3DLIGHT                   =22,\r
372                         COLOR_INFOTEXT                  =23,\r
373                         COLOR_INFOBK                    =24,\r
374                         COLOR_DESKTOP                   =1,\r
375                         COLOR_3DFACE                    =16,\r
376                         COLOR_3DSHADOW                  =16,\r
377                         COLOR_3DHIGHLIGHT               =20,\r
378                         COLOR_3DHILIGHT                 =20,\r
379                         COLOR_BTNHILIGHT                =20,
380                         COLOR_MAXVALUE                  =24,/* Maximum value */\r
381                 }       \r
382 \r
383                 private enum LoadCursorType {\r
384                         IDC_ARROW                       =32512,\r
385                         IDC_IBEAM                       =32513,\r
386                         IDC_WAIT                        =32514,\r
387                         IDC_CROSS                       =32515,\r
388                         IDC_UPARROW                     =32516,\r
389                         IDC_SIZE                        =32640,\r
390                         IDC_ICON                        =32641,\r
391                         IDC_SIZENWSE                    =32642,\r
392                         IDC_SIZENESW                    =32643,\r
393                         IDC_SIZEWE                      =32644,\r
394                         IDC_SIZENS                      =32645,\r
395                         IDC_SIZEALL                     =32646,\r
396                         IDC_NO                          =32648,\r
397                         IDC_HAND                        =32649,\r
398                         IDC_APPSTARTING                 =32650,\r
399                         IDC_HELP                        =32651\r
400                 }
401 \r
402                 [Flags]\r
403                 private enum WindowLong {\r
404                         GWL_WNDPROC                     = -4,\r
405                         GWL_HINSTANCE                   = -6,\r
406                         GWL_HWNDPARENT                  = -8,\r
407                         GWL_STYLE                       = -16,\r
408                         GWL_EXSTYLE                     = -20,\r
409                         GWL_USERDATA                    = -21,\r
410                         GWL_ID                          = -12\r
411                 }\r
412 \r
413                 [Flags]\r
414                 private enum LogBrushStyle {\r
415                         BS_SOLID                        = 0,\r
416                         BS_NULL                         = 1,\r
417                         BS_HATCHED                      = 2,\r
418                         BS_PATTERN                      = 3,\r
419                         BS_INDEXED                      = 4,\r
420                         BS_DIBPATTERN                   = 5,\r
421                         BS_DIBPATTERNPT                 = 6,\r
422                         BS_PATTERN8X8                   = 7,\r
423                         BS_DIBPATTERN8X8                = 8,\r
424                         BS_MONOPATTERN                  = 9\r
425                 }\r
426 \r
427                 [Flags]\r
428                 private enum LogBrushHatch {\r
429                         HS_HORIZONTAL                   = 0,       /* ----- */\r
430                         HS_VERTICAL                     = 1,       /* ||||| */\r
431                         HS_FDIAGONAL                    = 2,       /* \\\\\ */\r
432                         HS_BDIAGONAL                    = 3,       /* ///// */\r
433                         HS_CROSS                        = 4,       /* +++++ */\r
434                         HS_DIAGCROSS                    = 5,       /* xxxxx */\r
435                 }\r
436 \r
437                 private struct COLORREF {\r
438                         internal byte                   B;\r
439                         internal byte                   G;\r
440                         internal byte                   R;\r
441                         internal byte                   A;\r
442                 }\r
443 \r
444                 [StructLayout(LayoutKind.Sequential)]\r
445                 private struct LOGBRUSH {\r
446                         internal LogBrushStyle          lbStyle;\r
447                         internal COLORREF               lbColor;\r
448                         internal LogBrushHatch          lbHatch;\r
449                 }\r
450                 #endregion
451
452                 #region Constructor & Destructor
453                 private XplatUIWin32() {
454                         WNDCLASS        wndClass;
455                         bool            result;
456
457                         // Handle singleton stuff first
458                         ref_count=0;
459
460                         // Now regular initialization
461                         mouse_state = MouseButtons.None;
462                         mouse_position = Point.Empty;
463
464                         themes_enabled = false;
465
466                         // Prepare 'our' window class
467                         wnd_proc = new WndProc(NativeWindow.WndProc);
468                         wndClass.style = (int)(ClassStyle.CS_OWNDC | ClassStyle.CS_DBLCLKS);
469                         wndClass.lpfnWndProc = wnd_proc;
470                         wndClass.cbClsExtra = 0;
471                         wndClass.cbWndExtra = 0;
472                         wndClass.hbrBackground = IntPtr.Zero;
473                         wndClass.hCursor = Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
474                         wndClass.hIcon = IntPtr.Zero;
475                         wndClass.hInstance = IntPtr.Zero;
476                         wndClass.lpszClassName = XplatUI.DefaultClassName;
477                         wndClass.lpszMenuName = "";
478
479                         result=Win32RegisterClass(ref wndClass);
480                         if (result==false) {
481                                 Win32MessageBox(IntPtr.Zero, "Could not register the "+XplatUI.DefaultClassName+" window class, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
482                         }
483
484                         FosterParent=Win32CreateWindow(0, "static", "Foster Parent Window", (int)WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
485
486                         if (FosterParent==IntPtr.Zero) {
487                                 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
488                         }
489
490                         handle_data = new Hashtable ();
491                         timer_list = new Hashtable ();
492                 }
493                 #endregion      // Constructor & Destructor
494
495                 #region Private Support Methods
496                 private static IntPtr DefWndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) {
497                         return Win32DefWindowProc(hWnd, msg, wParam, lParam);
498                 }
499
500                 private void EraseWindowBackground(IntPtr hWnd, IntPtr hDc) {
501                         IntPtr          hbr;
502                         LOGBRUSH        lb;
503                         uint            argb;
504                         RECT            rect;
505                                                 
506                         //msg.wParam
507                         argb = (uint)Win32GetWindowLong(hWnd, WindowLong.GWL_USERDATA);
508                         lb = new LOGBRUSH();
509                                                 
510                         lb.lbColor.B = (byte)((argb & 0xff0000)>>16);
511                         lb.lbColor.G = (byte)((argb & 0xff00)>>8);
512                         lb.lbColor.R = (byte)(argb & 0xff);
513
514                         lb.lbStyle = LogBrushStyle.BS_SOLID;
515                         hbr = Win32CreateBrushIndirect(ref lb);
516                         Win32GetClientRect(hWnd, out rect);
517                         Win32FillRect(hDc, ref rect, hbr);
518                         Win32DeleteObject(hbr);
519                 }
520
521                 #endregion      // Private Support Methods
522
523                 #region Static Properties
524                 internal override Keys ModifierKeys {
525                         get {
526                                 short   state;
527                                 Keys    key_state;
528
529                                 key_state = Keys.None;
530
531                                 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
532                                 if ((state & 0x8000) != 0) {
533                                         key_state |= Keys.Shift;
534                                 }
535                                 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
536                                 if ((state & 0x8000) != 0) {
537                                         key_state |= Keys.Control;
538                                 }
539                                 return key_state;
540                         }
541                 }
542
543                 internal override MouseButtons MouseButtons {
544                         get {
545                                 return mouse_state;
546                         }
547                 }
548
549                 internal override Point MousePosition {
550                         get {
551                                 return mouse_position;
552                         }
553                 }
554
555                 internal override bool DropTarget {
556                         get {
557                                 return false;
558                         }
559
560                         set {
561                                 if (value) {
562                                         throw new NotImplementedException("Need to figure out D'n'D for Win32");
563                                 }
564                         }
565                 }
566                 #endregion      // Static Properties
567
568                 #region Singleton Specific Code
569                 public static XplatUIWin32 GetInstance() {
570                         if (instance==null) {
571                                 instance=new XplatUIWin32();
572                         }
573                         ref_count++;
574                         return instance;
575                 }
576
577                 public int Reference {
578                         get {
579                                 return ref_count;
580                         }
581                 }
582                 #endregion
583
584                 #region Public Static Methods
585                 internal override IntPtr InitializeDriver() {\r
586                         mouse_state=MouseButtons.None;
587                         mouse_position=Point.Empty;
588
589                         Console.WriteLine("#region #line XplatUI Win32 Constructor called");
590 \r
591                         return IntPtr.Zero;\r
592                 }\r
593
594                 internal override void ShutdownDriver(IntPtr token) {\r
595                         Console.WriteLine("XplatUIWin32 ShutdownDriver called");
596                 }\r
597
598
599                 internal void Version() {
600                         Console.WriteLine("Xplat version $revision: $");
601                 }
602
603                 internal override void Exit() {
604                         Win32PostQuitMessage(0);
605                 }
606
607                 internal override void GetDisplaySize(out Size size) {
608                         RECT    rect;
609
610                         Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
611
612                         size = new Size(rect.right - rect.left, rect.bottom - rect.top);
613                 }
614
615                 internal override void EnableThemes() {
616                         themes_enabled=true;
617                 }
618
619                 internal override IntPtr CreateWindow(CreateParams cp) {
620                         IntPtr  WindowHandle;
621                         IntPtr  ParentHandle;
622
623                         ParentHandle=cp.Parent;
624
625                         if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
626                                 // We need to use our foster parent window until this poor child gets it's parent assigned
627                                 ParentHandle=FosterParent;
628                         }
629                         WindowHandle = Win32CreateWindow((uint)cp.ExStyle, cp.ClassName, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
630
631                         if (WindowHandle==IntPtr.Zero) {
632                                 uint error = Win32GetLastError();
633
634                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
635                         }
636
637                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (IntPtr)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
638
639                         return WindowHandle;
640                 }
641
642                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
643                         CreateParams create_params = new CreateParams();
644
645                         create_params.Caption = "";
646                         create_params.X = X;
647                         create_params.Y = Y;
648                         create_params.Width = Width;
649                         create_params.Height = Height;
650
651                         create_params.ClassName=XplatUI.DefaultClassName;
652                         create_params.ClassStyle = 0;
653                         create_params.ExStyle=0;
654                         create_params.Parent=IntPtr.Zero;
655                         create_params.Param=0;
656
657                         return CreateWindow(create_params);
658                 }
659
660                 internal override void DestroyWindow(IntPtr handle) {
661                         Win32DestroyWindow(handle);
662                         return;
663                 }
664
665                 internal override void RefreshWindow(IntPtr handle) {                   
666                         Win32InvalidateRect(handle, IntPtr.Zero, true);
667                         Win32UpdateWindow(handle);
668                 }
669
670                 internal override void SetWindowBackground(IntPtr handle, Color color) {
671                         Win32SetWindowLong(handle, WindowLong.GWL_USERDATA, (IntPtr)color.ToArgb());
672                 }
673
674                 [MonoTODO("Add support for internal table of windows/DCs for cleanup")]
675                 internal override PaintEventArgs PaintEventStart(IntPtr handle) {
676                         IntPtr          hdc;
677                         PAINTSTRUCT     ps;
678                         PaintEventArgs  paint_event;
679                         RECT            rect;
680                         Rectangle       clip_rect;
681
682                         clip_rect = new Rectangle();
683                         rect = new RECT();
684                         ps = new PAINTSTRUCT();
685
686                         if (Win32GetUpdateRect(handle, ref rect, false)) {
687                                 HandleData      data;
688
689                                 hdc = Win32BeginPaint(handle, ref ps);
690
691                                 data = (HandleData) handle_data [0];
692                                 if (data == null) {
693                                         data = new HandleData();
694                                         handle_data[0] = data;
695                                 }
696
697                                 data.DeviceContext=(Object)ps;
698
699                                 // FIXME: Figure out why the rectangle is always 0 size
700                                 clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
701 //                              clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
702
703                                 if (ps.fErase!=0) {
704                                         EraseWindowBackground(handle, hdc);
705                                 }
706                         } else {
707                                 hdc = Win32GetDC(handle);
708                                 // FIXME: Add the DC to internal list
709                                 clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
710                         }
711
712                         paint_event = new PaintEventArgs(Graphics.FromHdc(hdc), clip_rect);
713
714                         return paint_event;
715                 }
716
717                 internal override void PaintEventEnd(IntPtr handle) {\r
718                         HandleData      data;
719                         PAINTSTRUCT     ps;                     
720
721                         data = (HandleData) handle_data [0];
722                         if (data == null) {
723                                 data = new HandleData();
724                                 handle_data[0] = data;
725                         }
726
727                         //paint_event.Graphics.Dispose();
728                         if (data.DeviceContext != null) {
729                                 ps = (PAINTSTRUCT)data.DeviceContext;
730                                 Win32EndPaint(handle, ref ps);
731                         }
732                 }\r
733
734
735                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
736                         Win32MoveWindow(handle, x, y, width, height, true);
737                         return;
738                 }
739
740                 internal override void GetWindowPos(IntPtr handle, out int x, out int y, out int width, out int height, out int client_width, out int client_height) {
741                         RECT    rect;
742                         POINT   pt;
743
744                         Win32GetWindowRect(handle, out rect);
745                         width = rect.right - rect.left;
746                         height = rect.bottom - rect.top;
747
748                         pt.x=rect.left;
749                         pt.y=rect.top;
750                         Win32ScreenToClient(Win32GetParent(handle), ref pt);
751                         x = pt.x;
752                         y = pt.y;
753
754                         Win32GetClientRect(handle, out rect);
755                         client_width = rect.right - rect.left;
756                         client_height = rect.bottom - rect.top;
757                         return;
758                 }
759
760                 internal override void Activate(IntPtr handle) {
761                         Win32SetActiveWindow(handle);
762                 }
763
764                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
765                         RECT rect;
766
767                         rect.left=rc.Left;
768                         rect.top=rc.Top;
769                         rect.right=rc.Right;
770                         rect.bottom=rc.Bottom;
771                         Win32InvalidateRect(handle, ref rect, clear);
772                 }
773
774                 internal override IntPtr DefWndProc(ref Message msg) {
775                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
776                         return msg.Result;
777                 }
778
779                 internal override void HandleException(Exception e) {
780                         StackTrace st = new StackTrace(e);
781                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
782                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
783                 }
784
785                 internal override void DoEvents() {
786                         MSG msg = new MSG();
787
788                         while (Win32PeekMessage(ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)!=true) {
789                                 if (msg.message==Msg.WM_PAINT) {
790                                         XplatUI.TranslateMessage(ref msg);
791                                         XplatUI.DispatchMessage(ref msg);
792                                 }
793                         }
794                 }
795
796                 internal override bool PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
797                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
798                 }
799
800                 internal override bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
801                         HandleData      data;
802                         bool            result;
803                         data = (HandleData) handle_data [0];
804                         if ((data!=null) && data.GetMessage(ref msg)) {
805                                 return true;
806                         }
807
808                         result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
809
810                         // We need to fake WM_MOUSE_ENTER/WM_MOUSE_LEAVE
811                         switch (msg.message) {
812                                 case Msg.WM_LBUTTONDOWN: {
813                                         mouse_state |= MouseButtons.Left;
814                                         break;
815                                 }
816
817                                 case Msg.WM_MBUTTONDOWN: {
818                                         mouse_state |= MouseButtons.Middle;
819                                         break;
820                                 }
821
822                                 case Msg.WM_RBUTTONDOWN: {
823                                         mouse_state |= MouseButtons.Right;
824                                         break;
825                                 }
826
827                                 case Msg.WM_LBUTTONUP: {
828                                         mouse_state &= ~MouseButtons.Left;
829                                         break;
830                                 }
831
832                                 case Msg.WM_MBUTTONUP: {
833                                         mouse_state &= ~MouseButtons.Middle;
834                                         break;
835                                 }
836
837                                 case Msg.WM_RBUTTONUP: {
838                                         mouse_state &= ~MouseButtons.Right;
839                                         break;
840                                 }
841
842                                 case Msg.WM_ERASEBKGND: {
843                                         EraseWindowBackground(msg.hwnd, msg.wParam);
844                                         break;
845                                 }
846
847                                 case Msg.WM_ASYNC_MESSAGE: {
848                                         GCHandle handle = (GCHandle)msg.lParam;
849                                         AsyncMethodData asyncdata = (AsyncMethodData) handle.Target;
850                                         AsyncMethodResult asyncresult = asyncdata.Result.Target as AsyncMethodResult;
851                                         object ret = asyncdata.Method.DynamicInvoke (asyncdata.Args);
852                                         if (asyncresult != null) {
853                                                 asyncresult.Complete (ret);
854                                         }
855                                         handle.Free ();
856                                         break;
857                                 }
858
859                                 case Msg.WM_MOUSEMOVE: {
860                                         if (msg.hwnd != prev_mouse_hwnd) {
861                                                 TRACKMOUSEEVENT tme;
862
863                                                 if (data == null) {
864                                                         data = new HandleData();
865                                                         handle_data[0] = data;
866                                                 }
867
868                                                 // The current message will be sent out next time around
869                                                 data.StoreMessage(ref msg);
870
871                                                 // This is the message we want to send at this point
872                                                 msg.message = Msg.WM_MOUSE_ENTER;
873
874                                                 prev_mouse_hwnd = msg.hwnd;
875
876                                                 tme = new TRACKMOUSEEVENT();
877                                                 tme.size = Marshal.SizeOf(tme);
878                                                 tme.hWnd = msg.hwnd;
879                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
880                                                 Win32TrackMouseEvent(ref tme);
881                                                 return result;
882                                         }
883                                         break;
884                                 }
885
886                                 case Msg.WM_MOUSELEAVE: {
887                                         prev_mouse_hwnd = IntPtr.Zero;
888                                         msg.message=Msg.WM_MOUSE_LEAVE;
889                                         break;
890                                 }
891
892                                 case Msg.WM_TIMER: {
893                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
894
895                                         if (timer != null) {
896                                                 timer.FireTick();
897                                         }
898                                         break;
899                                 }
900                         }
901
902                         return result;
903                 }
904
905                 internal override bool TranslateMessage(ref MSG msg) {
906                         return Win32TranslateMessage(ref msg);
907                 }
908
909                 internal override IntPtr DispatchMessage(ref MSG msg) {
910                         return Win32DispatchMessage(ref msg);
911                 }
912
913                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
914                         if (Top) {
915                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
916                                 return true;
917                         } else if (!Bottom) {
918                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
919                         } else {
920                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
921                                 return true;
922                         }
923                         return false;
924                 }
925
926                 internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
927                         if (Enabled) {
928                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
929                                 return true;
930                         } else {
931                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
932                                 return true;
933                         }
934                 }
935
936                 internal override bool Text(IntPtr handle, string text) {
937                         Win32SetWindowText(handle, text);
938                         return true;
939                 }
940
941                 internal override bool GetText(IntPtr handle, out string text) {
942                         StringBuilder sb;
943
944                         sb = new StringBuilder(256);
945                         Win32GetWindowText(handle, sb, sb.Capacity);
946                         text = sb.ToString();
947                         return true;
948                 }
949
950                 internal override bool SetVisible(IntPtr handle, bool visible) {
951                         if (visible) {
952                                 Win32ShowWindow(handle, WindowPlacementFlags.SW_SHOWNORMAL);
953                         } else {
954                                 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
955                         }
956                         return true;
957                 }
958
959                 internal override bool IsVisible(IntPtr handle) {
960                         WINDOWPLACEMENT wndpl;
961
962                         wndpl = new WINDOWPLACEMENT();
963                         wndpl.length=(uint)Marshal.SizeOf(wndpl);
964                         Win32GetWindowPlacement(handle, ref wndpl);
965                         if ((wndpl.showCmd == WindowPlacementFlags.SW_SHOWMINIMIZED)) {
966                                 return false;
967                         }
968                         return true;
969                 }
970
971                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
972                         return Win32SetParent(handle, parent);
973                 }
974
975                 internal override IntPtr GetParent(IntPtr handle) {
976                         return Win32GetParent(handle);
977                 }
978 \r
979                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
980                         grab_hwnd = hWnd;
981                         Win32SetCapture(hWnd);
982                 }
983
984                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
985                         hWnd = grab_hwnd;
986                         GrabConfined = grab_confined;
987                         GrabArea = grab_area;
988                 }
989
990                 internal override void ReleaseWindow(IntPtr hWnd) {
991                         Win32ReleaseCapture();
992                         grab_hwnd = IntPtr.Zero;
993                 }
994
995                 internal override bool CalculateWindowRect(IntPtr hWnd, ref Rectangle ClientRect, int Style, bool HasMenu, out Rectangle WindowRect) {
996                         RECT    rect;
997
998                         rect.left=ClientRect.Left;
999                         rect.top=ClientRect.Top;
1000                         rect.right=ClientRect.Right;
1001                         rect.bottom=ClientRect.Bottom;
1002
1003                         if (!Win32AdjustWindowRectEx(ref rect, Style, HasMenu, 0)) {
1004                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1005                                 return false;
1006                         }
1007
1008                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1009                         return true;
1010                 }
1011
1012                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1013                         Win32SetCursorPos(x, y);
1014                 }
1015
1016                 internal override void EnableWindow(IntPtr handle, bool Enable) {
1017                         Win32EnableWindow(handle, Enable);
1018                 }
1019
1020                 internal override void SetModal(IntPtr handle, bool Modal) {
1021                         // we do nothing on Win32; Application.cs simulates modal dialogs by disabling all toplevel windows
1022                 }
1023
1024                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
1025                         POINT   pt;
1026
1027                         Win32GetCursorPos(out pt);
1028
1029                         if (handle!=IntPtr.Zero) {
1030                                 Win32ScreenToClient(handle, ref pt);
1031                         }
1032
1033                         x=pt.x;
1034                         y=pt.y;
1035                 }
1036
1037                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
1038                 {
1039                         POINT pnt = new POINT();                        
1040
1041                         pnt.x = x;
1042                         pnt.y = y;
1043                         Win32ScreenToClient (handle, ref pnt);
1044
1045                         x = pnt.x;
1046                         y = pnt.y;
1047                 }
1048
1049                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
1050                         POINT   pnt = new POINT();                      
1051
1052                         pnt.x = x;
1053                         pnt.y = y;
1054
1055                         Win32ClientToScreen(handle, ref pnt);
1056
1057                         x = pnt.x;
1058                         y = pnt.y;
1059                 }
1060
1061                 internal override void SendAsyncMethod (AsyncMethodData method)
1062                 {
1063                         Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
1064                 }
1065
1066                 internal override void SetTimer (Timer timer)
1067                 {
1068                         int     index;
1069
1070                         index = timer.GetHashCode();
1071
1072                         lock (timer_list) {
1073                                 timer_list[index]=timer;
1074                         }
1075
1076                         Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
1077                 }
1078
1079                 internal override void KillTimer (Timer timer)
1080                 {
1081                         int     index;
1082
1083                         index = timer.GetHashCode();
1084
1085                         Win32KillTimer(FosterParent, index);
1086
1087                         lock (timer_list) {
1088                                 timer_list.Remove(index);
1089                         }
1090                 }
1091
1092
1093                 private void CaretCallback(object sender, EventArgs e) {\r
1094                         Console.WriteLine("CaretCallback hit");\r
1095                 }\r
1096
1097                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
1098                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
1099                 }
1100
1101                 internal override void DestroyCaret(IntPtr hwnd) {
1102                         Win32DestroyCaret();
1103                 }
1104
1105                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
1106                         Win32SetCaretPos(x, y);
1107                 }
1108
1109                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
1110                         if (visible) {
1111                                 Win32ShowCaret(hwnd);
1112                         } else {
1113                                 Win32HideCaret(hwnd);
1114                         }
1115                 }
1116
1117                 internal override int KeyboardSpeed {
1118                         get {
1119                                 Console.WriteLine ("KeyboardSpeed: need to query Windows");
1120
1121                                 //
1122                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
1123                                 //
1124                                 return 0;
1125                         }
1126                 }
1127
1128                 internal override int KeyboardDelay {
1129                         get {
1130                                 Console.WriteLine ("KeyboardDelay: need to query Windows");
1131
1132                                 //
1133                                 // Return values must range from 0 to 4, 0 meaning 250ms,
1134                                 // and 4 meaning 1000 ms.
1135                                 //
1136                                 return 1;
1137                         }
1138                 }
1139                 
1140                 internal override event EventHandler Idle;
1141
1142                 // Santa's little helper
1143                 static void Where() {
1144                         Console.WriteLine("Here: {0}", new StackTrace().ToString());
1145                 }
1146                 #endregion      // Public Static Methods
1147
1148                 #region Win32 Imports
1149                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1150                 private extern static uint Win32GetLastError();
1151
1152                 [DllImport ("user32.dll", EntryPoint="CreateWindowExA", CallingConvention=CallingConvention.StdCall)]
1153                 internal extern static IntPtr Win32CreateWindow(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
1154
1155                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
1156                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
1157
1158                 [DllImport ("user32.dll", EntryPoint="PeekMessageA", CallingConvention=CallingConvention.StdCall)]
1159                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
1160
1161                 [DllImport ("user32.dll", EntryPoint="GetMessageA", CallingConvention=CallingConvention.StdCall)]
1162                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
1163
1164                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
1165                 internal extern static bool Win32TranslateMessage(ref MSG msg);
1166
1167                 [DllImport ("user32.dll", EntryPoint="DispatchMessageA", CallingConvention=CallingConvention.StdCall)]
1168                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
1169
1170                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
1171                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
1172
1173                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
1174                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
1175
1176                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
1177                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
1178
1179                 [DllImport ("user32.dll", EntryPoint="SetWindowTextA", CallingConvention=CallingConvention.StdCall)]
1180                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
1181
1182                 [DllImport ("user32.dll", EntryPoint="GetWindowTextA", CallingConvention=CallingConvention.StdCall)]
1183                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
1184
1185                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
1186                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
1187
1188                 [DllImport ("user32.dll", EntryPoint="RegisterClassA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1189                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
1190
1191                 [DllImport ("user32.dll", EntryPoint="LoadCursorA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1192                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
1193
1194                 [DllImport ("user32.dll", EntryPoint="DefWindowProcA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1195                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
1196
1197                 [DllImport ("user32.dll", EntryPoint="DefDlgProcA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1198                 private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
1199
1200                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1201                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
1202
1203                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1204                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
1205
1206                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1207                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
1208
1209                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1210                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
1211
1212                 [DllImport ("user32.dll", EntryPoint="EndPaint", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1213                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
1214
1215                 [DllImport ("user32.dll", EntryPoint="GetDC", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1216                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
1217
1218                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1219                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
1220
1221                 [DllImport ("user32.dll", EntryPoint="MessageBoxA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1222                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
1223
1224                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1225                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
1226
1227                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1228                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
1229
1230                 [DllImport ("user32.dll", EntryPoint="SetCapture", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1231                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
1232
1233                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1234                 private extern static IntPtr Win32ReleaseCapture();
1235
1236                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1237                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
1238
1239                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1240                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
1241
1242                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1243                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
1244
1245                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1246                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
1247
1248                 [DllImport ("user32.dll", EntryPoint="GetParent", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1249                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
1250
1251                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1252                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
1253
1254                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1255                 private extern static IntPtr Win32PostQuitMessage(IntPtr hWnd);
1256
1257                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1258                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
1259
1260                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1261                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
1262
1263                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1264                 private extern static bool Win32SetCursorPos(int x, int y);
1265
1266                 [DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1267                 private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
1268
1269                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1270                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
1271
1272                 [DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1273                 private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
1274
1275                 [DllImport ("user32.dll", EntryPoint="FillRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1276                 private extern static int Win32FillRect(IntPtr hdc, ref RECT rect, IntPtr hbr);
1277
1278                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1279                 private extern static IntPtr Win32SetWindowLong(IntPtr hwnd, WindowLong index, IntPtr value);
1280
1281                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1282                 private extern static IntPtr Win32GetWindowLong(IntPtr hwnd, WindowLong index);
1283
1284                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1285                 private extern static bool Win32DeleteObject(IntPtr o);
1286
1287                 [DllImport ("user32.dll", EntryPoint="PostMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1288                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
1289
1290                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1291                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
1292
1293                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1294                 private extern static IntPtr Win32GetDesktopWindow();
1295
1296                 [DllImport ("user32.dll", EntryPoint="SetTimer", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1297                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
1298
1299                 [DllImport ("user32.dll", EntryPoint="KillTimer", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1300                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
1301
1302                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1303                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
1304
1305                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1306                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
1307
1308                 [DllImport ("user32.dll", EntryPoint="SetFocus", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1309                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
1310
1311                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1312                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
1313
1314                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1315                 internal extern static bool Win32DestroyCaret();
1316
1317                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1318                 internal extern static bool Win32ShowCaret(IntPtr hwnd);
1319
1320                 [DllImport ("user32.dll", EntryPoint="HideCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1321                 internal extern static bool Win32HideCaret(IntPtr hwnd);
1322
1323                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1324                 internal extern static bool Win32SetCaretPos(int X, int Y);
1325
1326                 [DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1327                 internal extern static uint Win32GetCaretBlinkTime();
1328                 #endregion\r
1329         }
1330 }