- Added and implemented GetFontMetrics() method
[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
451                 [StructLayout(LayoutKind.Sequential)]\r
452                 internal struct TEXTMETRIC { \r
453                         internal int                    tmHeight;\r
454                         internal int                    tmAscent;\r
455                         internal int                    tmDescent;\r
456                         internal int                    tmInternalLeading;\r
457                         internal int                    tmExternalLeading;\r
458                         internal int                    tmAveCharWidth;\r
459                         internal int                    tmMaxCharWidth;\r
460                         internal int                    tmWeight;\r
461                         internal int                    tmOverhang;\r
462                         internal int                    tmDigitizedAspectX;\r
463                         internal int                    tmDigitizedAspectY;\r
464                         internal byte                   tmFirstChar; \r
465                         internal byte                   tmLastChar; \r
466                         internal byte                   tmDefaultChar; \r
467                         internal byte                   tmBreakChar; \r
468                         internal byte                   tmItalic; \r
469                         internal byte                   tmUnderlined; \r
470                         internal byte                   tmStruckOut; \r
471                         internal byte                   tmPitchAndFamily; \r
472                         internal byte                   tmCharSet; \r
473                 } \r
474
475                 #endregion
476
477                 #region Constructor & Destructor
478                 private XplatUIWin32() {
479                         WNDCLASS        wndClass;
480                         bool            result;
481
482                         // Handle singleton stuff first
483                         ref_count=0;
484
485                         // Now regular initialization
486                         mouse_state = MouseButtons.None;
487                         mouse_position = Point.Empty;
488
489                         themes_enabled = false;
490
491                         // Prepare 'our' window class
492                         wnd_proc = new WndProc(NativeWindow.WndProc);
493                         wndClass.style = (int)(ClassStyle.CS_OWNDC | ClassStyle.CS_DBLCLKS);
494                         wndClass.lpfnWndProc = wnd_proc;
495                         wndClass.cbClsExtra = 0;
496                         wndClass.cbWndExtra = 0;
497                         wndClass.hbrBackground = IntPtr.Zero;
498                         wndClass.hCursor = Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
499                         wndClass.hIcon = IntPtr.Zero;
500                         wndClass.hInstance = IntPtr.Zero;
501                         wndClass.lpszClassName = XplatUI.DefaultClassName;
502                         wndClass.lpszMenuName = "";
503
504                         result=Win32RegisterClass(ref wndClass);
505                         if (result==false) {
506                                 Win32MessageBox(IntPtr.Zero, "Could not register the "+XplatUI.DefaultClassName+" window class, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
507                         }
508
509                         FosterParent=Win32CreateWindow(0, "static", "Foster Parent Window", (int)WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
510
511                         if (FosterParent==IntPtr.Zero) {
512                                 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
513                         }
514
515                         handle_data = new Hashtable ();
516                         timer_list = new Hashtable ();
517                 }
518                 #endregion      // Constructor & Destructor
519
520                 #region Private Support Methods
521                 private static IntPtr DefWndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) {
522                         return Win32DefWindowProc(hWnd, msg, wParam, lParam);
523                 }
524
525                 private void EraseWindowBackground(IntPtr hWnd, IntPtr hDc) {
526                         IntPtr          hbr;
527                         LOGBRUSH        lb;
528                         uint            argb;
529                         RECT            rect;
530                                                 
531                         //msg.wParam
532                         argb = (uint)Win32GetWindowLong(hWnd, WindowLong.GWL_USERDATA);
533                         lb = new LOGBRUSH();
534                                                 
535                         lb.lbColor.B = (byte)((argb & 0xff0000)>>16);
536                         lb.lbColor.G = (byte)((argb & 0xff00)>>8);
537                         lb.lbColor.R = (byte)(argb & 0xff);
538
539                         lb.lbStyle = LogBrushStyle.BS_SOLID;
540                         hbr = Win32CreateBrushIndirect(ref lb);
541                         Win32GetClientRect(hWnd, out rect);
542                         Win32FillRect(hDc, ref rect, hbr);
543                         Win32DeleteObject(hbr);
544                 }
545
546                 #endregion      // Private Support Methods
547
548                 #region Static Properties
549                 internal override Keys ModifierKeys {
550                         get {
551                                 short   state;
552                                 Keys    key_state;
553
554                                 key_state = Keys.None;
555
556                                 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
557                                 if ((state & 0x8000) != 0) {
558                                         key_state |= Keys.Shift;
559                                 }
560                                 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
561                                 if ((state & 0x8000) != 0) {
562                                         key_state |= Keys.Control;
563                                 }
564                                 return key_state;
565                         }
566                 }
567
568                 internal override MouseButtons MouseButtons {
569                         get {
570                                 return mouse_state;
571                         }
572                 }
573
574                 internal override Point MousePosition {
575                         get {
576                                 return mouse_position;
577                         }
578                 }
579
580                 internal override bool DropTarget {
581                         get {
582                                 return false;
583                         }
584
585                         set {
586                                 if (value) {
587                                         throw new NotImplementedException("Need to figure out D'n'D for Win32");
588                                 }
589                         }
590                 }
591                 #endregion      // Static Properties
592
593                 #region Singleton Specific Code
594                 public static XplatUIWin32 GetInstance() {
595                         if (instance==null) {
596                                 instance=new XplatUIWin32();
597                         }
598                         ref_count++;
599                         return instance;
600                 }
601
602                 public int Reference {
603                         get {
604                                 return ref_count;
605                         }
606                 }
607                 #endregion
608
609                 #region Public Static Methods
610                 internal override IntPtr InitializeDriver() {\r
611                         mouse_state=MouseButtons.None;
612                         mouse_position=Point.Empty;
613
614                         Console.WriteLine("#region #line XplatUI Win32 Constructor called");
615 \r
616                         return IntPtr.Zero;\r
617                 }\r
618
619                 internal override void ShutdownDriver(IntPtr token) {\r
620                         Console.WriteLine("XplatUIWin32 ShutdownDriver called");
621                 }\r
622
623
624                 internal void Version() {
625                         Console.WriteLine("Xplat version $revision: $");
626                 }
627
628                 internal override void Exit() {
629                         Win32PostQuitMessage(0);
630                 }
631
632                 internal override void GetDisplaySize(out Size size) {
633                         RECT    rect;
634
635                         Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
636
637                         size = new Size(rect.right - rect.left, rect.bottom - rect.top);
638                 }
639
640                 internal override void EnableThemes() {
641                         themes_enabled=true;
642                 }
643
644                 internal override IntPtr CreateWindow(CreateParams cp) {
645                         IntPtr  WindowHandle;
646                         IntPtr  ParentHandle;
647
648                         ParentHandle=cp.Parent;
649
650                         if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
651                                 // We need to use our foster parent window until this poor child gets it's parent assigned
652                                 ParentHandle=FosterParent;
653                         }
654                         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);
655
656                         if (WindowHandle==IntPtr.Zero) {
657                                 uint error = Win32GetLastError();
658
659                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
660                         }
661
662                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (IntPtr)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
663
664                         return WindowHandle;
665                 }
666
667                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
668                         CreateParams create_params = new CreateParams();
669
670                         create_params.Caption = "";
671                         create_params.X = X;
672                         create_params.Y = Y;
673                         create_params.Width = Width;
674                         create_params.Height = Height;
675
676                         create_params.ClassName=XplatUI.DefaultClassName;
677                         create_params.ClassStyle = 0;
678                         create_params.ExStyle=0;
679                         create_params.Parent=IntPtr.Zero;
680                         create_params.Param=0;
681
682                         return CreateWindow(create_params);
683                 }
684
685                 internal override void DestroyWindow(IntPtr handle) {
686                         Win32DestroyWindow(handle);
687                         return;
688                 }
689
690                 internal override void RefreshWindow(IntPtr handle) {                   
691                         Win32InvalidateRect(handle, IntPtr.Zero, true);
692                         Win32UpdateWindow(handle);
693                 }
694
695                 internal override void SetWindowBackground(IntPtr handle, Color color) {
696                         Win32SetWindowLong(handle, WindowLong.GWL_USERDATA, (IntPtr)color.ToArgb());
697                 }
698
699                 [MonoTODO("Add support for internal table of windows/DCs for cleanup")]
700                 internal override PaintEventArgs PaintEventStart(IntPtr handle) {
701                         IntPtr          hdc;
702                         PAINTSTRUCT     ps;
703                         PaintEventArgs  paint_event;
704                         RECT            rect;
705                         Rectangle       clip_rect;
706
707                         clip_rect = new Rectangle();
708                         rect = new RECT();
709                         ps = new PAINTSTRUCT();
710
711                         if (Win32GetUpdateRect(handle, ref rect, false)) {
712                                 HandleData      data;
713
714                                 hdc = Win32BeginPaint(handle, ref ps);
715
716                                 data = (HandleData) handle_data [0];
717                                 if (data == null) {
718                                         data = new HandleData();
719                                         handle_data[0] = data;
720                                 }
721
722                                 data.DeviceContext=(Object)ps;
723
724                                 // FIXME: Figure out why the rectangle is always 0 size
725                                 clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
726 //                              clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
727
728                                 if (ps.fErase!=0) {
729                                         EraseWindowBackground(handle, hdc);
730                                 }
731                         } else {
732                                 hdc = Win32GetDC(handle);
733                                 // FIXME: Add the DC to internal list
734                                 clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
735                         }
736
737                         paint_event = new PaintEventArgs(Graphics.FromHdc(hdc), clip_rect);
738
739                         return paint_event;
740                 }
741
742                 internal override void PaintEventEnd(IntPtr handle) {\r
743                         HandleData      data;
744                         PAINTSTRUCT     ps;                     
745
746                         data = (HandleData) handle_data [0];
747                         if (data == null) {
748                                 data = new HandleData();
749                                 handle_data[0] = data;
750                         }
751
752                         //paint_event.Graphics.Dispose();
753                         if (data.DeviceContext != null) {
754                                 ps = (PAINTSTRUCT)data.DeviceContext;
755                                 Win32EndPaint(handle, ref ps);
756                         }
757                 }\r
758
759
760                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
761                         Win32MoveWindow(handle, x, y, width, height, true);
762                         return;
763                 }
764
765                 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) {
766                         RECT    rect;
767                         POINT   pt;
768
769                         Win32GetWindowRect(handle, out rect);
770                         width = rect.right - rect.left;
771                         height = rect.bottom - rect.top;
772
773                         pt.x=rect.left;
774                         pt.y=rect.top;
775                         Win32ScreenToClient(Win32GetParent(handle), ref pt);
776                         x = pt.x;
777                         y = pt.y;
778
779                         Win32GetClientRect(handle, out rect);
780                         client_width = rect.right - rect.left;
781                         client_height = rect.bottom - rect.top;
782                         return;
783                 }
784
785                 internal override void Activate(IntPtr handle) {
786                         Win32SetActiveWindow(handle);
787                 }
788
789                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
790                         RECT rect;
791
792                         rect.left=rc.Left;
793                         rect.top=rc.Top;
794                         rect.right=rc.Right;
795                         rect.bottom=rc.Bottom;
796                         Win32InvalidateRect(handle, ref rect, clear);
797                 }
798
799                 internal override IntPtr DefWndProc(ref Message msg) {
800                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
801                         return msg.Result;
802                 }
803
804                 internal override void HandleException(Exception e) {
805                         StackTrace st = new StackTrace(e);
806                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
807                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
808                 }
809
810                 internal override void DoEvents() {
811                         MSG msg = new MSG();
812
813                         while (Win32PeekMessage(ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)!=true) {
814                                 if (msg.message==Msg.WM_PAINT) {
815                                         XplatUI.TranslateMessage(ref msg);
816                                         XplatUI.DispatchMessage(ref msg);
817                                 }
818                         }
819                 }
820
821                 internal override bool PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
822                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
823                 }
824
825                 internal override bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
826                         HandleData      data;
827                         bool            result;
828                         data = (HandleData) handle_data [0];
829                         if ((data!=null) && data.GetMessage(ref msg)) {
830                                 return true;
831                         }
832
833                         result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
834
835                         // We need to fake WM_MOUSE_ENTER/WM_MOUSE_LEAVE
836                         switch (msg.message) {
837                                 case Msg.WM_LBUTTONDOWN: {
838                                         mouse_state |= MouseButtons.Left;
839                                         break;
840                                 }
841
842                                 case Msg.WM_MBUTTONDOWN: {
843                                         mouse_state |= MouseButtons.Middle;
844                                         break;
845                                 }
846
847                                 case Msg.WM_RBUTTONDOWN: {
848                                         mouse_state |= MouseButtons.Right;
849                                         break;
850                                 }
851
852                                 case Msg.WM_LBUTTONUP: {
853                                         mouse_state &= ~MouseButtons.Left;
854                                         break;
855                                 }
856
857                                 case Msg.WM_MBUTTONUP: {
858                                         mouse_state &= ~MouseButtons.Middle;
859                                         break;
860                                 }
861
862                                 case Msg.WM_RBUTTONUP: {
863                                         mouse_state &= ~MouseButtons.Right;
864                                         break;
865                                 }
866
867                                 case Msg.WM_ERASEBKGND: {
868                                         EraseWindowBackground(msg.hwnd, msg.wParam);
869                                         break;
870                                 }
871
872                                 case Msg.WM_ASYNC_MESSAGE: {
873                                         GCHandle handle = (GCHandle)msg.lParam;
874                                         AsyncMethodData asyncdata = (AsyncMethodData) handle.Target;
875                                         AsyncMethodResult asyncresult = asyncdata.Result.Target as AsyncMethodResult;
876                                         object ret = asyncdata.Method.DynamicInvoke (asyncdata.Args);
877                                         if (asyncresult != null) {
878                                                 asyncresult.Complete (ret);
879                                         }
880                                         handle.Free ();
881                                         break;
882                                 }
883
884                                 case Msg.WM_MOUSEMOVE: {
885                                         if (msg.hwnd != prev_mouse_hwnd) {
886                                                 TRACKMOUSEEVENT tme;
887
888                                                 if (data == null) {
889                                                         data = new HandleData();
890                                                         handle_data[0] = data;
891                                                 }
892
893                                                 // The current message will be sent out next time around
894                                                 data.StoreMessage(ref msg);
895
896                                                 // This is the message we want to send at this point
897                                                 msg.message = Msg.WM_MOUSE_ENTER;
898
899                                                 prev_mouse_hwnd = msg.hwnd;
900
901                                                 tme = new TRACKMOUSEEVENT();
902                                                 tme.size = Marshal.SizeOf(tme);
903                                                 tme.hWnd = msg.hwnd;
904                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
905                                                 Win32TrackMouseEvent(ref tme);
906                                                 return result;
907                                         }
908                                         break;
909                                 }
910
911                                 case Msg.WM_MOUSELEAVE: {
912                                         prev_mouse_hwnd = IntPtr.Zero;
913                                         msg.message=Msg.WM_MOUSE_LEAVE;
914                                         break;
915                                 }
916
917                                 case Msg.WM_TIMER: {
918                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
919
920                                         if (timer != null) {
921                                                 timer.FireTick();
922                                         }
923                                         break;
924                                 }
925                         }
926
927                         return result;
928                 }
929
930                 internal override bool TranslateMessage(ref MSG msg) {
931                         return Win32TranslateMessage(ref msg);
932                 }
933
934                 internal override IntPtr DispatchMessage(ref MSG msg) {
935                         return Win32DispatchMessage(ref msg);
936                 }
937
938                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
939                         if (Top) {
940                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
941                                 return true;
942                         } else if (!Bottom) {
943                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
944                         } else {
945                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
946                                 return true;
947                         }
948                         return false;
949                 }
950
951                 internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
952                         if (Enabled) {
953                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
954                                 return true;
955                         } else {
956                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
957                                 return true;
958                         }
959                 }
960
961                 internal override bool Text(IntPtr handle, string text) {
962                         Win32SetWindowText(handle, text);
963                         return true;
964                 }
965
966                 internal override bool GetText(IntPtr handle, out string text) {
967                         StringBuilder sb;
968
969                         sb = new StringBuilder(256);
970                         Win32GetWindowText(handle, sb, sb.Capacity);
971                         text = sb.ToString();
972                         return true;
973                 }
974
975                 internal override bool SetVisible(IntPtr handle, bool visible) {
976                         if (visible) {
977                                 Win32ShowWindow(handle, WindowPlacementFlags.SW_SHOWNORMAL);
978                         } else {
979                                 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
980                         }
981                         return true;
982                 }
983
984                 internal override bool IsVisible(IntPtr handle) {
985                         WINDOWPLACEMENT wndpl;
986
987                         wndpl = new WINDOWPLACEMENT();
988                         wndpl.length=(uint)Marshal.SizeOf(wndpl);
989                         Win32GetWindowPlacement(handle, ref wndpl);
990                         if ((wndpl.showCmd == WindowPlacementFlags.SW_SHOWMINIMIZED)) {
991                                 return false;
992                         }
993                         return true;
994                 }
995
996                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
997                         return Win32SetParent(handle, parent);
998                 }
999
1000                 internal override IntPtr GetParent(IntPtr handle) {
1001                         return Win32GetParent(handle);
1002                 }
1003 \r
1004                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1005                         grab_hwnd = hWnd;
1006                         Win32SetCapture(hWnd);
1007                 }
1008
1009                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1010                         hWnd = grab_hwnd;
1011                         GrabConfined = grab_confined;
1012                         GrabArea = grab_area;
1013                 }
1014
1015                 internal override void ReleaseWindow(IntPtr hWnd) {
1016                         Win32ReleaseCapture();
1017                         grab_hwnd = IntPtr.Zero;
1018                 }
1019
1020                 internal override bool CalculateWindowRect(IntPtr hWnd, ref Rectangle ClientRect, int Style, bool HasMenu, out Rectangle WindowRect) {
1021                         RECT    rect;
1022
1023                         rect.left=ClientRect.Left;
1024                         rect.top=ClientRect.Top;
1025                         rect.right=ClientRect.Right;
1026                         rect.bottom=ClientRect.Bottom;
1027
1028                         if (!Win32AdjustWindowRectEx(ref rect, Style, HasMenu, 0)) {
1029                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1030                                 return false;
1031                         }
1032
1033                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1034                         return true;
1035                 }
1036
1037                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1038                         Win32SetCursorPos(x, y);
1039                 }
1040
1041                 internal override void EnableWindow(IntPtr handle, bool Enable) {
1042                         Win32EnableWindow(handle, Enable);
1043                 }
1044
1045                 internal override void SetModal(IntPtr handle, bool Modal) {
1046                         // we do nothing on Win32; Application.cs simulates modal dialogs by disabling all toplevel windows
1047                 }
1048
1049                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
1050                         POINT   pt;
1051
1052                         Win32GetCursorPos(out pt);
1053
1054                         if (handle!=IntPtr.Zero) {
1055                                 Win32ScreenToClient(handle, ref pt);
1056                         }
1057
1058                         x=pt.x;
1059                         y=pt.y;
1060                 }
1061
1062                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
1063                 {
1064                         POINT pnt = new POINT();                        
1065
1066                         pnt.x = x;
1067                         pnt.y = y;
1068                         Win32ScreenToClient (handle, ref pnt);
1069
1070                         x = pnt.x;
1071                         y = pnt.y;
1072                 }
1073
1074                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
1075                         POINT   pnt = new POINT();                      
1076
1077                         pnt.x = x;
1078                         pnt.y = y;
1079
1080                         Win32ClientToScreen(handle, ref pnt);
1081
1082                         x = pnt.x;
1083                         y = pnt.y;
1084                 }
1085
1086                 internal override void SendAsyncMethod (AsyncMethodData method)
1087                 {
1088                         Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
1089                 }
1090
1091                 internal override void SetTimer (Timer timer)
1092                 {
1093                         int     index;
1094
1095                         index = timer.GetHashCode();
1096
1097                         lock (timer_list) {
1098                                 timer_list[index]=timer;
1099                         }
1100
1101                         Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
1102                 }
1103
1104                 internal override void KillTimer (Timer timer)
1105                 {
1106                         int     index;
1107
1108                         index = timer.GetHashCode();
1109
1110                         Win32KillTimer(FosterParent, index);
1111
1112                         lock (timer_list) {
1113                                 timer_list.Remove(index);
1114                         }
1115                 }
1116
1117
1118                 private void CaretCallback(object sender, EventArgs e) {\r
1119                         Console.WriteLine("CaretCallback hit");\r
1120                 }\r
1121
1122                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
1123                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
1124                 }
1125
1126                 internal override void DestroyCaret(IntPtr hwnd) {
1127                         Win32DestroyCaret();
1128                 }
1129
1130                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
1131                         Win32SetCaretPos(x, y);
1132                 }
1133
1134                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
1135                         if (visible) {
1136                                 Win32ShowCaret(hwnd);
1137                         } else {
1138                                 Win32HideCaret(hwnd);
1139                         }
1140                 }
1141
1142                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
1143                         IntPtr          dc;
1144                         TEXTMETRIC      tm;
1145
1146                         tm = new TEXTMETRIC();
1147
1148                         dc = Win32GetDC(IntPtr.Zero);
1149                         Win32SelectObject(dc, font.ToHfont());
1150                         if (Win32GetTextMetrics(dc, ref tm) == false) {
1151                                 Win32ReleaseDC(IntPtr.Zero, dc);
1152                                 ascent = 0;
1153                                 descent = 0;
1154                                 return false;
1155                         }
1156                         Win32ReleaseDC(IntPtr.Zero, dc);
1157
1158                         ascent = tm.tmAscent;
1159                         descent = tm.tmDescent;
1160
1161                         return true;
1162                 }
1163
1164                 internal override int KeyboardSpeed {
1165                         get {
1166                                 Console.WriteLine ("KeyboardSpeed: need to query Windows");
1167
1168                                 //
1169                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
1170                                 //
1171                                 return 0;
1172                         }
1173                 }
1174
1175                 internal override int KeyboardDelay {
1176                         get {
1177                                 Console.WriteLine ("KeyboardDelay: need to query Windows");
1178
1179                                 //
1180                                 // Return values must range from 0 to 4, 0 meaning 250ms,
1181                                 // and 4 meaning 1000 ms.
1182                                 //
1183                                 return 1;
1184                         }
1185                 }
1186                 
1187                 internal override event EventHandler Idle;
1188
1189                 // Santa's little helper
1190                 static void Where() {
1191                         Console.WriteLine("Here: {0}", new StackTrace().ToString());
1192                 }
1193                 #endregion      // Public Static Methods
1194
1195                 #region Win32 Imports
1196                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1197                 private extern static uint Win32GetLastError();
1198
1199                 [DllImport ("user32.dll", EntryPoint="CreateWindowExA", CallingConvention=CallingConvention.StdCall)]
1200                 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);
1201
1202                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
1203                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
1204
1205                 [DllImport ("user32.dll", EntryPoint="PeekMessageA", CallingConvention=CallingConvention.StdCall)]
1206                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
1207
1208                 [DllImport ("user32.dll", EntryPoint="GetMessageA", CallingConvention=CallingConvention.StdCall)]
1209                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
1210
1211                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
1212                 internal extern static bool Win32TranslateMessage(ref MSG msg);
1213
1214                 [DllImport ("user32.dll", EntryPoint="DispatchMessageA", CallingConvention=CallingConvention.StdCall)]
1215                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
1216
1217                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
1218                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
1219
1220                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
1221                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
1222
1223                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
1224                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
1225
1226                 [DllImport ("user32.dll", EntryPoint="SetWindowTextA", CallingConvention=CallingConvention.StdCall)]
1227                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
1228
1229                 [DllImport ("user32.dll", EntryPoint="GetWindowTextA", CallingConvention=CallingConvention.StdCall)]
1230                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
1231
1232                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
1233                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
1234
1235                 [DllImport ("user32.dll", EntryPoint="RegisterClassA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1236                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
1237
1238                 [DllImport ("user32.dll", EntryPoint="LoadCursorA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1239                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
1240
1241                 [DllImport ("user32.dll", EntryPoint="DefWindowProcA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1242                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
1243
1244                 [DllImport ("user32.dll", EntryPoint="DefDlgProcA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1245                 private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
1246
1247                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1248                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
1249
1250                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1251                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
1252
1253                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1254                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
1255
1256                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1257                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
1258
1259                 [DllImport ("user32.dll", EntryPoint="EndPaint", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1260                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
1261
1262                 [DllImport ("user32.dll", EntryPoint="GetDC", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1263                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
1264
1265                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1266                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
1267
1268                 [DllImport ("user32.dll", EntryPoint="MessageBoxA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1269                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
1270
1271                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1272                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
1273
1274                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1275                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
1276
1277                 [DllImport ("user32.dll", EntryPoint="SetCapture", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1278                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
1279
1280                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1281                 private extern static IntPtr Win32ReleaseCapture();
1282
1283                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1284                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
1285
1286                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1287                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
1288
1289                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1290                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
1291
1292                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1293                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
1294
1295                 [DllImport ("user32.dll", EntryPoint="GetParent", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1296                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
1297
1298                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1299                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
1300
1301                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1302                 private extern static IntPtr Win32PostQuitMessage(IntPtr hWnd);
1303
1304                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1305                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
1306
1307                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1308                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
1309
1310                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1311                 private extern static bool Win32SetCursorPos(int x, int y);
1312
1313                 [DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1314                 private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
1315
1316                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1317                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
1318
1319                 [DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1320                 private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
1321
1322                 [DllImport ("user32.dll", EntryPoint="FillRect", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1323                 private extern static int Win32FillRect(IntPtr hdc, ref RECT rect, IntPtr hbr);
1324
1325                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1326                 private extern static IntPtr Win32SetWindowLong(IntPtr hwnd, WindowLong index, IntPtr value);
1327
1328                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1329                 private extern static IntPtr Win32GetWindowLong(IntPtr hwnd, WindowLong index);
1330
1331                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1332                 private extern static bool Win32DeleteObject(IntPtr o);
1333
1334                 [DllImport ("user32.dll", EntryPoint="PostMessage", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1335                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
1336
1337                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1338                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
1339
1340                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1341                 private extern static IntPtr Win32GetDesktopWindow();
1342
1343                 [DllImport ("user32.dll", EntryPoint="SetTimer", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1344                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
1345
1346                 [DllImport ("user32.dll", EntryPoint="KillTimer", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1347                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
1348
1349                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1350                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
1351
1352                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1353                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
1354
1355                 [DllImport ("user32.dll", EntryPoint="SetFocus", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1356                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
1357
1358                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1359                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
1360
1361                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1362                 internal extern static bool Win32DestroyCaret();
1363
1364                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1365                 internal extern static bool Win32ShowCaret(IntPtr hwnd);
1366
1367                 [DllImport ("user32.dll", EntryPoint="HideCaret", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1368                 internal extern static bool Win32HideCaret(IntPtr hwnd);
1369
1370                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1371                 internal extern static bool Win32SetCaretPos(int X, int Y);
1372
1373                 [DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1374                 internal extern static uint Win32GetCaretBlinkTime();
1375 \r
1376                 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsA", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1377                 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
1378 \r
1379                 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
1380                 internal extern static bool Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
1381                 #endregion\r
1382         }
1383 }