* roottypes.cs: Rename from tree.cs.
[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-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System;
30 using System.Drawing;
31 using System.Drawing.Imaging;
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.IO;
36 using System.Runtime.InteropServices;
37 using System.Text;
38
39
40 /// Win32 Version
41 namespace System.Windows.Forms {
42         internal class XplatUIWin32 : XplatUIDriver {
43                 #region Local Variables
44                 private static XplatUIWin32     instance;
45                 private static int              ref_count;
46                 private static IntPtr           FosterParent;
47
48                 internal static MouseButtons    mouse_state;
49                 internal static Point           mouse_position;
50                 internal static bool            grab_confined;
51                 internal static IntPtr          grab_hwnd;
52                 internal static Rectangle       grab_area;
53                 internal static WndProc         wnd_proc;
54                 internal static IntPtr          prev_mouse_hwnd;
55                 internal static IntPtr          override_cursor;
56                 internal static bool            caret_visible;
57
58                 internal static bool            themes_enabled;
59                 private Hashtable               timer_list;
60                 private static Queue            message_queue;
61                 private static IntPtr           clip_magic = new IntPtr(27051977);
62                 private static int              scroll_width;
63                 private static int              scroll_height;
64
65                 private static Win32DnD         DnD;
66                 #endregion      // Local Variables
67
68                 #region Private Structs
69                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
70                 private struct WNDCLASS {
71                         internal int            style;
72                         internal WndProc        lpfnWndProc;
73                         internal int            cbClsExtra;
74                         internal int            cbWndExtra;
75                         internal IntPtr         hInstance;
76                         internal IntPtr         hIcon;
77                         internal IntPtr         hCursor;
78                         internal IntPtr         hbrBackground;
79                         [MarshalAs(UnmanagedType.LPWStr)]
80                         internal string         lpszMenuName;
81                         [MarshalAs(UnmanagedType.LPWStr)]
82                         internal string         lpszClassName;
83                 }
84
85                 [StructLayout(LayoutKind.Sequential)]
86                 internal struct RECT {
87                         internal int            left;
88                         internal int            top;
89                         internal int            right;
90                         internal int            bottom;
91                         public override string ToString() {
92                                 return String.Format("RECT left={0}, top={1}, right={2}, bottom={3}, width={4}, height={5}", left, top, right, bottom, right-left, bottom-top);
93                         }
94
95                 }
96
97                 [StructLayout(LayoutKind.Sequential)]
98                 internal struct POINT {
99                         internal int            x;
100                         internal int            y;
101                 }
102
103                 internal enum SPIAction {
104                         SPI_GETWORKAREA         = 0x0030,
105                         SPI_GETMOUSEHOVERWIDTH  = 0x0062,
106                         SPI_GETMOUSEHOVERHEIGHT = 0x0064,
107                         SPI_GETMOUSEHOVERTIME   = 0x0066,
108                 }
109
110                 internal enum WindowPlacementFlags {
111                         SW_HIDE                 = 0,
112                         SW_SHOWNORMAL           = 1,
113                         SW_NORMAL               = 1,
114                         SW_SHOWMINIMIZED        = 2,
115                         SW_SHOWMAXIMIZED        = 3,
116                         SW_MAXIMIZE             = 3,
117                         SW_SHOWNOACTIVATE       = 4,
118                         SW_SHOW                 = 5,
119                         SW_MINIMIZE             = 6,
120                         SW_SHOWMINNOACTIVE      = 7,
121                         SW_SHOWNA               = 8,
122                         SW_RESTORE              = 9,
123                         SW_SHOWDEFAULT          = 10,
124                         SW_FORCEMINIMIZE        = 11,
125                         SW_MAX                  = 11
126                 }
127
128                 [StructLayout(LayoutKind.Sequential)]
129                 private struct WINDOWPLACEMENT {
130                         internal uint                   length;
131                         internal uint                   flags;
132                         internal WindowPlacementFlags   showCmd;
133                         internal POINT                  ptMinPosition;
134                         internal POINT                  ptMaxPosition;
135                         internal RECT                   rcNormalPosition;
136                 }
137
138                 [StructLayout(LayoutKind.Sequential)]
139                 internal struct NCCALCSIZE_PARAMS {
140                         internal RECT           rgrc1;
141                         internal RECT           rgrc2;
142                         internal RECT           rgrc3;
143                         internal IntPtr         lppos;
144                 }
145
146                 [Flags]
147                 private enum TMEFlags {
148                         TME_HOVER               = 0x00000001,
149                         TME_LEAVE               = 0x00000002,
150                         TME_QUERY               = unchecked((int)0x40000000),
151                         TME_CANCEL              = unchecked((int)0x80000000)
152                 }
153
154                 [StructLayout(LayoutKind.Sequential)]
155                 private struct TRACKMOUSEEVENT {
156                         internal int            size;
157                         internal TMEFlags       dwFlags;
158                         internal IntPtr         hWnd;
159                         internal int            dwHoverTime;
160                 }
161
162                 [StructLayout(LayoutKind.Sequential)]
163                 private struct PAINTSTRUCT {
164                         internal IntPtr         hdc;
165                         internal int            fErase;
166                         internal RECT           rcPaint;
167                         internal int            fRestore;
168                         internal int            fIncUpdate;
169                         internal int            Reserved1;
170                         internal int            Reserved2;
171                         internal int            Reserved3;
172                         internal int            Reserved4;
173                         internal int            Reserved5;
174                         internal int            Reserved6;
175                         internal int            Reserved7;
176                         internal int            Reserved8;
177                 }
178
179                 internal enum ClassStyle {
180                         CS_VREDRAW                      = 0x00000001,
181                         CS_HREDRAW                      = 0x00000002,
182                         CS_KEYCVTWINDOW                 = 0x00000004,
183                         CS_DBLCLKS                      = 0x00000008,
184                         CS_OWNDC                        = 0x00000020,
185                         CS_CLASSDC                      = 0x00000040,
186                         CS_PARENTDC                     = 0x00000080,
187                         CS_NOKEYCVT                     = 0x00000100,
188                         CS_NOCLOSE                      = 0x00000200,
189                         CS_SAVEBITS                     = 0x00000800,
190                         CS_BYTEALIGNCLIENT              = 0x00001000,
191                         CS_BYTEALIGNWINDOW              = 0x00002000,
192                         CS_GLOBALCLASS                  = 0x00004000,
193                         CS_IME                          = 0x00010000
194                 }
195
196                 internal enum SetWindowPosZOrder {
197                         HWND_TOP                        = 0,
198                         HWND_BOTTOM                     = 1,
199                         HWND_TOPMOST                    = -1,
200                         HWND_NOTOPMOST                  = -2
201                 }
202
203                 [Flags]
204                 internal enum SetWindowPosFlags {
205                         SWP_ASYNCWINDOWPOS              = 0x4000, 
206                         SWP_DEFERERASE                  = 0x2000,
207                         SWP_DRAWFRAME                   = 0x0020,
208                         SWP_FRAMECHANGED                = 0x0020,
209                         SWP_HIDEWINDOW                  = 0x0080,
210                         SWP_NOACTIVATE                  = 0x0010,
211                         SWP_NOCOPYBITS                  = 0x0100,
212                         SWP_NOMOVE                      = 0x0002,
213                         SWP_NOOWNERZORDER               = 0x0200,
214                         SWP_NOREDRAW                    = 0x0008,
215                         SWP_NOREPOSITION                = 0x0200,
216                         SWP_NOENDSCHANGING              = 0x0400,
217                         SWP_NOSIZE                      = 0x0001,
218                         SWP_NOZORDER                    = 0x0004,
219                         SWP_SHOWWINDOW                  = 0x0040
220                 }
221
222                 internal enum GetSysColorIndex {
223                         COLOR_SCROLLBAR                 = 0,
224                         COLOR_BACKGROUND                = 1,
225                         COLOR_ACTIVECAPTION             = 2,
226                         COLOR_INACTIVECAPTION           = 3,
227                         COLOR_MENU                      = 4,
228                         COLOR_WINDOW                    = 5,
229                         COLOR_WINDOWFRAME               = 6,
230                         COLOR_MENUTEXT                  = 7,
231                         COLOR_WINDOWTEXT                = 8,
232                         COLOR_CAPTIONTEXT               = 9,
233                         COLOR_ACTIVEBORDER              = 10,
234                         COLOR_INACTIVEBORDER            = 11,
235                         COLOR_APPWORKSPACE              = 12,
236                         COLOR_HIGHLIGHT                 = 13,
237                         COLOR_HIGHLIGHTTEXT             = 14,
238                         COLOR_BTNFACE                   = 15,
239                         COLOR_BTNSHADOW                 = 16,
240                         COLOR_GRAYTEXT                  = 17,
241                         COLOR_BTNTEXT                   = 18,
242                         COLOR_INACTIVECAPTIONTEXT       = 19,
243                         COLOR_BTNHIGHLIGHT              = 20,
244                         COLOR_3DDKSHADOW                = 21,
245                         COLOR_3DLIGHT                   = 22,
246                         COLOR_INFOTEXT                  = 23,
247                         COLOR_INFOBK                    = 24,
248                         
249                         COLOR_HOTLIGHT                  = 26,
250                         COLOR_GRADIENTACTIVECAPTION     = 27,
251                         COLOR_GRADIENTINACTIVECAPTION   = 28,
252                         COLOR_MENUHIGHLIGHT             = 29,
253                         COLOR_MENUBAR                   = 30,
254
255                         COLOR_DESKTOP                   = 1,
256                         COLOR_3DFACE                    = 16,
257                         COLOR_3DSHADOW                  = 16,
258                         COLOR_3DHIGHLIGHT               = 20,
259                         COLOR_3DHILIGHT                 = 20,
260                         COLOR_BTNHILIGHT                = 20,
261                         COLOR_MAXVALUE                  = 24,/* Maximum value */
262                 }       
263
264                 private enum LoadCursorType {
265                         First                           = 32512,
266                         IDC_ARROW                       = 32512,
267                         IDC_IBEAM                       = 32513,
268                         IDC_WAIT                        = 32514,
269                         IDC_CROSS                       = 32515,
270                         IDC_UPARROW                     = 32516,
271                         IDC_SIZE                        = 32640,
272                         IDC_ICON                        = 32641,
273                         IDC_SIZENWSE                    = 32642,
274                         IDC_SIZENESW                    = 32643,
275                         IDC_SIZEWE                      = 32644,
276                         IDC_SIZENS                      = 32645,
277                         IDC_SIZEALL                     = 32646,
278                         IDC_NO                          = 32648,
279                         IDC_HAND                        = 32649,
280                         IDC_APPSTARTING                 = 32650,
281                         IDC_HELP                        = 32651,
282                         Last                            = 32651
283                 }
284
285                 [Flags]
286                 private enum WindowLong {
287                         GWL_WNDPROC                     = -4,
288                         GWL_HINSTANCE                   = -6,
289                         GWL_HWNDPARENT                  = -8,
290                         GWL_STYLE                       = -16,
291                         GWL_EXSTYLE                     = -20,
292                         GWL_USERDATA                    = -21,
293                         GWL_ID                          = -12
294                 }
295
296                 [Flags]
297                 private enum LogBrushStyle {
298                         BS_SOLID                        = 0,
299                         BS_NULL                         = 1,
300                         BS_HATCHED                      = 2,
301                         BS_PATTERN                      = 3,
302                         BS_INDEXED                      = 4,
303                         BS_DIBPATTERN                   = 5,
304                         BS_DIBPATTERNPT                 = 6,
305                         BS_PATTERN8X8                   = 7,
306                         BS_DIBPATTERN8X8                = 8,
307                         BS_MONOPATTERN                  = 9
308                 }
309
310                 [Flags]
311                 private enum LogBrushHatch {
312                         HS_HORIZONTAL                   = 0,       /* ----- */
313                         HS_VERTICAL                     = 1,       /* ||||| */
314                         HS_FDIAGONAL                    = 2,       /* \\\\\ */
315                         HS_BDIAGONAL                    = 3,       /* ///// */
316                         HS_CROSS                        = 4,       /* +++++ */
317                         HS_DIAGCROSS                    = 5,       /* xxxxx */
318                 }
319
320                 internal struct COLORREF {
321                         internal byte                   R;
322                         internal byte                   G;
323                         internal byte                   B;
324                         internal byte                   A;
325                 }
326
327                 [StructLayout(LayoutKind.Sequential)]
328                 private struct LOGBRUSH {
329                         internal LogBrushStyle          lbStyle;
330                         internal COLORREF               lbColor;
331                         internal LogBrushHatch          lbHatch;
332                 }
333
334                 [StructLayout(LayoutKind.Sequential)]
335                 internal struct TEXTMETRIC { 
336                         internal int                    tmHeight;
337                         internal int                    tmAscent;
338                         internal int                    tmDescent;
339                         internal int                    tmInternalLeading;
340                         internal int                    tmExternalLeading;
341                         internal int                    tmAveCharWidth;
342                         internal int                    tmMaxCharWidth;
343                         internal int                    tmWeight;
344                         internal int                    tmOverhang;
345                         internal int                    tmDigitizedAspectX;
346                         internal int                    tmDigitizedAspectY;
347                         internal short                  tmFirstChar; 
348                         internal short                  tmLastChar; 
349                         internal short                  tmDefaultChar; 
350                         internal short                  tmBreakChar; 
351                         internal byte                   tmItalic; 
352                         internal byte                   tmUnderlined; 
353                         internal byte                   tmStruckOut; 
354                         internal byte                   tmPitchAndFamily; 
355                         internal byte                   tmCharSet; 
356                 } 
357
358                 [Flags]
359                 private enum ScrollWindowExFlags {
360                         SW_NONE                         = 0x0000,
361                         SW_SCROLLCHILDREN               = 0x0001,
362                         SW_INVALIDATE                   = 0x0002,
363                         SW_ERASE                        = 0x0004,
364                         SW_SMOOTHSCROLL                 = 0x0010
365                 }
366
367                 internal enum SystemMetrics {
368                         SM_CXSCREEN                     = 0,
369                         SM_CYSCREEN                     = 1,
370                         SM_CXVSCROLL                    = 2,
371                         SM_CYHSCROLL                    = 3,
372                         SM_CYCAPTION                    = 4,
373                         SM_CXBORDER                     = 5,
374                         SM_CYBORDER                     = 6,
375                         SM_CXDLGFRAME                   = 7,
376                         SM_CYDLGFRAME                   = 8,
377                         SM_CYVTHUMB                     = 9,
378                         SM_CXHTHUMB                     = 10,
379                         SM_CXICON                       = 11,
380                         SM_CYICON                       = 12,
381                         SM_CXCURSOR                     = 13,
382                         SM_CYCURSOR                     = 14,
383                         SM_CYMENU                       = 15,
384                         SM_CXFULLSCREEN                 = 16,
385                         SM_CYFULLSCREEN                 = 17,
386                         SM_CYKANJIWINDOW                = 18,
387                         SM_MOUSEPRESENT                 = 19,
388                         SM_CYVSCROLL                    = 20,
389                         SM_CXHSCROLL                    = 21,
390                         SM_DEBUG                        = 22,
391                         SM_SWAPBUTTON                   = 23,
392                         SM_RESERVED1                    = 24,
393                         SM_RESERVED2                    = 25,
394                         SM_RESERVED3                    = 26,
395                         SM_RESERVED4                    = 27,
396                         SM_CXMIN                        = 28,
397                         SM_CYMIN                        = 29,
398                         SM_CXSIZE                       = 30,
399                         SM_CYSIZE                       = 31,
400                         SM_CXFRAME                      = 32,
401                         SM_CYFRAME                      = 33,
402                         SM_CXMINTRACK                   = 34,
403                         SM_CYMINTRACK                   = 35,
404                         SM_CXDOUBLECLK                  = 36,
405                         SM_CYDOUBLECLK                  = 37,
406                         SM_CXICONSPACING                = 38,
407                         SM_CYICONSPACING                = 39,
408                         SM_MENUDROPALIGNMENT            = 40,
409                         SM_PENWINDOWS                   = 41,
410                         SM_DBCSENABLED                  = 42,
411                         SM_CMOUSEBUTTONS                = 43,
412                         SM_CXFIXEDFRAME                 = SM_CXDLGFRAME,
413                         SM_CYFIXEDFRAME                 = SM_CYDLGFRAME,
414                         SM_CXSIZEFRAME                  = SM_CXFRAME,
415                         SM_CYSIZEFRAME                  = SM_CYFRAME,
416                         SM_SECURE                       = 44,
417                         SM_CXEDGE                       = 45,
418                         SM_CYEDGE                       = 46,
419                         SM_CXMINSPACING                 = 47,
420                         SM_CYMINSPACING                 = 48,
421                         SM_CXSMICON                     = 49,
422                         SM_CYSMICON                     = 50,
423                         SM_CYSMCAPTION                  = 51,
424                         SM_CXSMSIZE                     = 52,
425                         SM_CYSMSIZE                     = 53,
426                         SM_CXMENUSIZE                   = 54,
427                         SM_CYMENUSIZE                   = 55,
428                         SM_ARRANGE                      = 56,
429                         SM_CXMINIMIZED                  = 57,
430                         SM_CYMINIMIZED                  = 58,
431                         SM_CXMAXTRACK                   = 59,
432                         SM_CYMAXTRACK                   = 60,
433                         SM_CXMAXIMIZED                  = 61,
434                         SM_CYMAXIMIZED                  = 62,
435                         SM_NETWORK                      = 63,
436                         SM_CLEANBOOT                    = 67,
437                         SM_CXDRAG                       = 68,
438                         SM_CYDRAG                       = 69,
439                         SM_SHOWSOUNDS                   = 70,
440                         SM_CXMENUCHECK                  = 71,
441                         SM_CYMENUCHECK                  = 72,
442                         SM_SLOWMACHINE                  = 73,
443                         SM_MIDEASTENABLED               = 74,
444                         SM_MOUSEWHEELPRESENT            = 75,
445                         SM_XVIRTUALSCREEN               = 76,
446                         SM_YVIRTUALSCREEN               = 77,
447                         SM_CXVIRTUALSCREEN              = 78,
448                         SM_CYVIRTUALSCREEN              = 79,
449                         SM_CMONITORS                    = 80,
450                         SM_SAMEDISPLAYFORMAT            = 81,
451                         SM_IMMENABLED                   = 82,
452                         SM_CXFOCUSBORDER                = 83,
453                         SM_CYFOCUSBORDER                = 84,
454                         SM_TABLETPC                     = 86,
455                         SM_MEDIACENTER                  = 87,
456                         SM_CMETRICS                     = 88
457                 }
458
459                 // We'll only support _WIN32_IE < 0x0500 for now
460                 internal enum NotifyIconMessage {
461                         NIM_ADD                         = 0x00000000,
462                         NIM_MODIFY                      = 0x00000001,
463                         NIM_DELETE                      = 0x00000002,
464                 }
465
466                 [Flags]
467                 internal enum NotifyIconFlags {
468                         NIF_MESSAGE                     = 0x00000001,
469                         NIF_ICON                        = 0x00000002,
470                         NIF_TIP                         = 0x00000004,
471                 }
472
473                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
474                 internal struct NOTIFYICONDATA {
475                         internal uint                   cbSize;
476                         internal IntPtr                 hWnd;
477                         internal uint                   uID;
478                         internal NotifyIconFlags        uFlags;
479                         internal uint                   uCallbackMessage;
480                         internal IntPtr                 hIcon;
481                         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
482                         internal string                 szTip;
483                 }
484
485                 [Flags]
486                 internal enum DCExFlags {
487                         DCX_WINDOW                      = 0x00000001,
488                         DCX_CACHE                       = 0x00000002,
489                         DCX_NORESETATTRS                = 0x00000004,
490                         DCX_CLIPCHILDREN                = 0x00000008,
491                         DCX_CLIPSIBLINGS                = 0x00000010,
492                         DCX_PARENTCLIP                  = 0x00000020,
493                         DCX_EXCLUDERGN                  = 0x00000040,
494                         DCX_INTERSECTRGN                = 0x00000080,
495                         DCX_EXCLUDEUPDATE               = 0x00000100,
496                         DCX_INTERSECTUPDATE             = 0x00000200,
497                         DCX_LOCKWINDOWUPDATE            = 0x00000400,
498                         DCX_USESTYLE                    = 0x00010000,
499                         DCX_VALIDATE                    = 0x00200000
500                 }
501
502                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
503                 internal struct CLIENTCREATESTRUCT {
504                         internal IntPtr                 hWindowMenu;
505                         internal uint                   idFirstChild;
506                 }
507
508                 private enum ClassLong : int {
509                         GCL_MENUNAME                    = -8,
510                         GCL_HBRBACKGROUND               = -10,
511                         GCL_HCURSOR                     = -12,
512                         GCL_HICON                       = -14,
513                         GCL_HMODULE                     = -16,
514                         GCL_CBWNDEXTRA                  = -18,
515                         GCL_CBCLSEXTRA                  = -20,
516                         GCL_WNDPROC                     = -24,
517                         GCL_STYLE                       = -26,
518                         GCW_ATOM                        = -32,
519                         GCL_HICONSM                     = -34
520                 }
521
522                 [Flags]
523                 internal enum GAllocFlags : uint {
524                         GMEM_FIXED                      = 0x0000,
525                         GMEM_MOVEABLE                   = 0x0002,
526                         GMEM_NOCOMPACT                  = 0x0010,
527                         GMEM_NODISCARD                  = 0x0020,
528                         GMEM_ZEROINIT                   = 0x0040,
529                         GMEM_MODIFY                     = 0x0080,
530                         GMEM_DISCARDABLE                = 0x0100,
531                         GMEM_NOT_BANKED                 = 0x1000,
532                         GMEM_SHARE                      = 0x2000,
533                         GMEM_DDESHARE                   = 0x2000,
534                         GMEM_NOTIFY                     = 0x4000,
535                         GMEM_LOWER                      = GMEM_NOT_BANKED,
536                         GMEM_VALID_FLAGS                = 0x7F72,
537                         GMEM_INVALID_HANDLE             = 0x8000,
538                         GHND                            = (GMEM_MOVEABLE | GMEM_ZEROINIT),
539                         GPTR                            = (GMEM_FIXED | GMEM_ZEROINIT)
540                 }
541
542                 internal enum ROP2DrawMode : int {
543                         R2_BLACK                        = 1,
544                         R2_NOTMERGEPEN                  = 2,
545                         R2_MASKNOTPEN                   = 3,
546                         R2_NOTCOPYPEN                   = 4,
547                         R2_MASKPENNOT                   = 5,
548                         R2_NOT                          = 6,
549                         R2_XORPEN                       = 7,
550                         R2_NOTMASKPEN                   = 8,
551                         R2_MASKPEN                      = 9,
552                         R2_NOTXORPEN                    = 10,
553                         R2_NOP                          = 11,
554                         R2_MERGENOTPEN                  = 12,
555                         R2_COPYPEN                      = 13,
556                         R2_MERGEPENNOT                  = 14,
557                         R2_MERGEPEN                     = 15,
558                         R2_WHITE                        = 16,
559                         R2_LAST                         = 16
560                 }
561
562                 internal enum PenStyle : int {
563                         PS_SOLID                        = 0,
564                         PS_DASH                         = 1,
565                         PS_DOT                          = 2,
566                         PS_DASHDOT                      = 3,
567                         PS_DASHDOTDOT                   = 4,
568                         PS_NULL                         = 5,
569                         PS_INSIDEFRAME                  = 6,
570                         PS_USERSTYLE                    = 7,
571                         PS_ALTERNATE                    = 8
572                 }
573
574                 internal enum StockObject : int {
575                         WHITE_BRUSH                     = 0,
576                         LTGRAY_BRUSH                    = 1,
577                         GRAY_BRUSH                      = 2,
578                         DKGRAY_BRUSH                    = 3,
579                         BLACK_BRUSH                     = 4,
580                         NULL_BRUSH                      = 5,
581                         HOLLOW_BRUSH                    = NULL_BRUSH,
582                         WHITE_PEN                       = 6,
583                         BLACK_PEN                       = 7,
584                         NULL_PEN                        = 8,
585                         OEM_FIXED_FONT                  = 10,
586                         ANSI_FIXED_FONT                 = 11,
587                         ANSI_VAR_FONT                   = 12,
588                         SYSTEM_FONT                     = 13,
589                         DEVICE_DEFAULT_FONT             = 14,
590                         DEFAULT_PALETTE                 = 15,
591                         SYSTEM_FIXED_FONT               = 16
592                 }
593
594                 internal enum HatchStyle : int {
595                         HS_HORIZONTAL                   = 0,
596                         HS_VERTICAL                     = 1,
597                         HS_FDIAGONAL                    = 2,
598                         HS_BDIAGONAL                    = 3,
599                         HS_CROSS                        = 4,
600                         HS_DIAGCROSS                    = 5
601                 }
602
603                 [Flags]
604                 internal enum SndFlags : int {
605                         SND_SYNC                        = 0x0000,
606                         SND_ASYNC                       = 0x0001,
607                         SND_NODEFAULT                   = 0x0002,
608                         SND_MEMORY                      = 0x0004,
609                         SND_LOOP                        = 0x0008,
610                         SND_NOSTOP                      = 0x0010,
611                         SND_NOWAIT                      = 0x00002000,
612                         SND_ALIAS                       = 0x00010000,
613                         SND_ALIAS_ID                    = 0x00110000,
614                         SND_FILENAME                    = 0x00020000,
615                         SND_RESOURCE                    = 0x00040004,
616                         SND_PURGE                       = 0x0040,
617                         SND_APPLICATION                 = 0x0080,
618                 }
619
620                 [Flags]
621                 internal enum LayeredWindowAttributes : int {
622                         LWA_COLORKEY            = 0x1,
623                         LWA_ALPHA                       = 0x2,
624                 }
625
626                 
627                 #endregion
628
629                 #region Constructor & Destructor
630                 private XplatUIWin32() {
631                         WNDCLASS        wndClass;
632                         bool            result;
633
634                         // Handle singleton stuff first
635                         ref_count=0;
636
637                         DnD = new Win32DnD();
638
639                         mouse_state = MouseButtons.None;
640                         mouse_position = Point.Empty;
641
642                         message_queue = new Queue();
643
644                         themes_enabled = false;
645
646                         // Prepare 'our' window class
647                         wnd_proc = new WndProc(NativeWindow.WndProc);
648                         wndClass.style = (int)(ClassStyle.CS_OWNDC | ClassStyle.CS_DBLCLKS);
649                         wndClass.lpfnWndProc = wnd_proc;
650                         wndClass.cbClsExtra = 0;
651                         wndClass.cbWndExtra = 0;
652                         wndClass.hbrBackground = IntPtr.Zero;
653                         wndClass.hCursor = Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
654                         wndClass.hIcon = IntPtr.Zero;
655                         wndClass.hInstance = IntPtr.Zero;
656                         wndClass.lpszClassName = XplatUI.DefaultClassName;
657                         wndClass.lpszMenuName = "";
658
659                         result=Win32RegisterClass(ref wndClass);
660                         if (result==false) {
661                                 Win32MessageBox(IntPtr.Zero, "Could not register the "+XplatUI.DefaultClassName+" window class, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
662                         }
663
664                         FosterParent=Win32CreateWindow((int)WindowExStyles.WS_EX_TOOLWINDOW, "static", "Foster Parent Window", (int)WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
665
666                         if (FosterParent==IntPtr.Zero) {
667                                 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
668                         }
669
670                         scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
671                         scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
672
673                         timer_list = new Hashtable ();
674                 }
675                 #endregion      // Constructor & Destructor
676
677                 #region Private Support Methods
678                 private static IntPtr DefWndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) {
679                         return Win32DefWindowProc(hWnd, msg, wParam, lParam);
680                 }
681
682                 private static bool MessageWaiting {
683                         get {
684                                 if (message_queue.Count == 0) {
685                                         return false;
686                                 }
687                                 return true;
688                         }
689                 }
690
691                 private static bool RetrieveMessage(ref MSG msg) {
692                         MSG     message;
693
694                         if (message_queue.Count == 0) {
695                                 return false;
696                         }
697
698                         message = (MSG)message_queue.Dequeue();
699                         msg = message;
700
701                         return true;
702                 }
703
704                 private static bool StoreMessage(ref MSG msg) {
705                         MSG message = new MSG();
706
707                         message = msg;
708                         message_queue.Enqueue(message);
709
710                         return true;
711                 }
712
713                 internal static String AnsiToString(IntPtr ansi_data) {
714                         return (string)Marshal.PtrToStringAnsi(ansi_data);
715                 }
716
717                 internal static String UnicodeToString(IntPtr unicode_data) {
718                         return (string)Marshal.PtrToStringUni(unicode_data);
719                 }
720
721                 internal static Image DIBtoImage(IntPtr dib_data) {
722                         BITMAPINFOHEADER        bmi;
723                         int                     ncolors;
724                         int                     imagesize;
725                         //int                   palettesize;
726                         Bitmap                  bmp;
727                         BitmapData              bits;
728                         ColorPalette            pal;
729                         int[]                   palette;
730                         byte[]                  imagebits;
731                         int                     bytesPerLine;
732
733                         bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
734
735                         ncolors = (int)bmi.biClrUsed;
736                         if (ncolors == 0) {
737                                 if (bmi.biBitCount < 24) {
738                                         ncolors = (int)(1 << bmi.biBitCount);
739                                 }
740                         }
741                         //palettesize = ncolors * 4;
742
743                         imagesize = (int)bmi.biSizeImage;
744                         if (imagesize == 0) {
745                                 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
746                         }
747
748                         switch(bmi.biBitCount) {
749                                 case 1: {       // Monochrome
750                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
751                                         palette = new int[2];
752                                         break;
753                                 }
754
755                                 case 4: {       // 4bpp
756                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
757                                         palette = new int[16];
758                                         break;
759                                 }
760
761                                 case 8: {       // 8bpp
762                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
763                                         palette = new int[256];
764                                         break;
765                                 }
766
767                                 case 24:
768                                 case 32: {      // 32bpp
769                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
770                                         palette = new int[0];
771                                         break;
772                                 }
773
774                                 default: {
775                                         throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
776                                 }
777                         }
778
779                         if (bmi.biBitCount < 24) {
780                                 pal = bmp.Palette;                              // Managed palette
781                                 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
782
783                                 for (int i = 0; i < ncolors; i++) {
784                                         pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
785                                 }
786                                 bmp.Palette = pal;
787                         }
788
789                         bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
790                         bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
791
792                         imagebits = new byte[bytesPerLine];
793
794                         for (int y = 0; y < bmi.biHeight; y++) {
795                                 // Copy from source to managed
796                                 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
797
798                                 // Copy from managed to dest
799                                 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
800                         }
801
802                         bmp.UnlockBits(bits);
803
804                         return bmp;
805                 }
806
807                 internal static byte[] ImageToDIB(Image image) {
808                         MemoryStream    ms;
809                         byte[]          buffer;
810                         byte[]          retbuf;
811
812                         ms = new MemoryStream();
813                         image.Save(ms, ImageFormat.Bmp);
814                         buffer = ms.GetBuffer();
815
816                         // Filter out the file header
817                         retbuf = new byte[buffer.Length];
818                         Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
819                         return retbuf;
820                 }
821
822                 internal static IntPtr DupGlobalMem(IntPtr mem) {
823                         IntPtr  dup;
824                         IntPtr  dup_ptr;
825                         IntPtr  mem_ptr;
826                         uint    len;
827
828                         len = Win32GlobalSize(mem);
829                         mem_ptr = Win32GlobalLock(mem);
830
831                         dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
832                         dup_ptr = Win32GlobalLock(dup);
833
834                         Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
835
836                         Win32GlobalUnlock(mem);
837                         Win32GlobalUnlock(dup);
838
839                         return dup;
840                 }
841                 #endregion      // Private Support Methods
842
843                 #region Static Properties
844                 internal override Keys ModifierKeys {
845                         get {
846                                 short   state;
847                                 Keys    key_state;
848
849                                 key_state = Keys.None;
850
851                                 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
852                                 if ((state & 0x8000) != 0) {
853                                         key_state |= Keys.Shift;
854                                 }
855                                 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
856                                 if ((state & 0x8000) != 0) {
857                                         key_state |= Keys.Control;
858                                 }
859
860                                 state = Win32GetKeyState(VirtualKeys.VK_MENU);
861                                 if ((state & 0x8000) != 0) {
862                                         key_state |= Keys.Alt;
863                                 }
864
865                                 return key_state;
866                         }
867                 }
868
869                 internal override MouseButtons MouseButtons {
870                         get {
871                                 return mouse_state;
872                         }
873                 }
874
875                 internal override Point MousePosition {
876                         get {
877                                 return mouse_position;
878                         }
879                 }
880
881                 internal override Size MouseHoverSize {
882                         get {
883                                 int     width = 4;
884                                 int     height = 4;
885
886                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
887                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
888                                 return new Size(width, height);
889                         }
890                 }
891
892                 internal override int MouseHoverTime {
893                         get {
894                                 int time = 500;
895
896                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
897                                 return time;
898                         }
899                 }
900
901                 internal override int HorizontalScrollBarHeight {
902                         get {
903                                 return scroll_height;
904                         }
905                 }
906
907                 internal override bool UserClipWontExposeParent {
908                         get {
909                                 return false;
910                         }
911                 }
912
913
914                 internal override int VerticalScrollBarWidth {
915                         get {
916                                 return scroll_width;
917                         }
918                 }
919
920                 internal override int MenuHeight {
921                         get {
922                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
923                         }
924                 }
925
926                 internal override bool DropTarget {
927                         get {
928                                 return false;
929                         }
930
931                         set {
932                                 if (value) {
933                                         //throw new NotImplementedException("Need to figure out D'n'D for Win32");
934                                 }
935                         }
936                 }
937
938                 internal override int Caption {
939                         get {
940                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
941                         }
942                 }
943
944                 internal override Size CursorSize {
945                         get {
946                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
947                         }
948                 }
949
950                 internal override bool DragFullWindows {
951                         get {
952                                 return true;
953                         }
954                 }
955
956                 internal override Size DragSize {
957                         get {
958                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
959                         }
960                 }
961
962                 internal override Size FrameBorderSize { 
963                         get {
964                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
965                         }
966                 }
967
968                 internal override Size IconSize {
969                         get {
970                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
971                         }
972                 }
973
974                 internal override Size MaxWindowTrackSize {
975                         get {
976                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
977                         }
978                 }
979
980                 internal override Size MinimizedWindowSize {
981                         get {
982                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
983                         }
984                 }
985
986                 internal override Size MinimizedWindowSpacingSize {
987                         get {
988                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
989                         }
990                 }
991
992                 internal override Size MinimumWindowSize {
993                         get {
994                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
995                         }
996                 }
997
998                 internal override Size MinWindowTrackSize {
999                         get {
1000                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1001                         }
1002                 }
1003
1004                 internal override Size SmallIconSize {
1005                         get {
1006                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1007                         }
1008                 }
1009
1010                 internal override int MouseButtonCount {
1011                         get {
1012                                 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1013                         }
1014                 }
1015
1016                 internal override bool MouseButtonsSwapped {
1017                         get {
1018                                 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1019                         }
1020                 }
1021
1022                 internal override bool MouseWheelPresent {
1023                         get {
1024                                 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1025                         }
1026                 }
1027
1028                 internal override Rectangle VirtualScreen {
1029                         get {
1030                                 return new Rectangle(   Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1031                                                         Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1032                         }
1033                 }
1034
1035                 internal override Rectangle WorkingArea {
1036                         get {
1037                                 RECT    rect;
1038
1039                                 rect = new RECT();
1040                                 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1041                                 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1042                                 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1043                         }
1044                 }
1045                 #endregion      // Static Properties
1046
1047                 #region Singleton Specific Code
1048                 public static XplatUIWin32 GetInstance() {
1049                         if (instance==null) {
1050                                 instance=new XplatUIWin32();
1051                         }
1052                         ref_count++;
1053                         return instance;
1054                 }
1055
1056                 public int Reference {
1057                         get {
1058                                 return ref_count;
1059                         }
1060                 }
1061                 #endregion
1062
1063                 #region Public Static Methods
1064                 internal override IntPtr InitializeDriver() {
1065                         return IntPtr.Zero;
1066                 }
1067
1068                 internal override void ShutdownDriver(IntPtr token) {
1069                         Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1070                 }
1071
1072
1073                 internal void Version() {
1074                         Console.WriteLine("Xplat version $revision: $");
1075                 }
1076
1077                 internal override void AudibleAlert() {
1078                         Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1079                 }
1080
1081                 internal override void GetDisplaySize(out Size size) {
1082                         RECT    rect;
1083
1084                         Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1085
1086                         size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1087                 }
1088
1089                 internal override void EnableThemes() {
1090                         themes_enabled=true;
1091                 }
1092
1093                 internal override IntPtr CreateWindow(CreateParams cp) {
1094                         IntPtr  WindowHandle;
1095                         IntPtr  ParentHandle;
1096                         Hwnd    hwnd;
1097
1098                         hwnd = new Hwnd();
1099
1100                         ParentHandle=cp.Parent;
1101
1102                         if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1103                                 // We need to use our foster parent window until this poor child gets it's parent assigned
1104                                 ParentHandle = FosterParent;
1105                         }
1106
1107                         if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1108                                 // If we want to be hidden from the taskbar we need to be 'owned' by 
1109                                 // something not on the taskbar. FosterParent is just that
1110                                 ParentHandle = FosterParent;
1111                         }
1112
1113                         // Since we fake MDI dont tell Windows that this is a real MDI window
1114                         if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
1115                                 SetMdiStyles (cp);
1116                         }
1117
1118                         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);
1119
1120                         if (WindowHandle==IntPtr.Zero) {
1121                                 uint error = Win32GetLastError();
1122
1123                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1124                         }
1125
1126                         hwnd.ClientWindow = WindowHandle;
1127
1128                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1129
1130                         return WindowHandle;
1131                 }
1132
1133                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1134                         CreateParams create_params = new CreateParams();
1135
1136                         create_params.Caption = "";
1137                         create_params.X = X;
1138                         create_params.Y = Y;
1139                         create_params.Width = Width;
1140                         create_params.Height = Height;
1141
1142                         create_params.ClassName=XplatUI.DefaultClassName;
1143                         create_params.ClassStyle = 0;
1144                         create_params.ExStyle=0;
1145                         create_params.Parent=IntPtr.Zero;
1146                         create_params.Param=0;
1147
1148                         return CreateWindow(create_params);
1149                 }
1150
1151                 internal override void DestroyWindow(IntPtr handle) {
1152                         Hwnd    hwnd;
1153
1154                         hwnd = Hwnd.ObjectFromHandle(handle);
1155                         Win32DestroyWindow(handle);
1156                         hwnd.Dispose();
1157                         return;
1158                 }
1159
1160                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1161                         // We do nothing, Form has to handle WM_GETMINMAXINFO
1162                 }
1163
1164
1165                 internal override FormWindowState GetWindowState(IntPtr handle) {
1166                         uint style;
1167
1168                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1169                         if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1170                                 return FormWindowState.Maximized;
1171                         } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1172                                 return FormWindowState.Minimized;
1173                         }
1174                         return FormWindowState.Normal;
1175                 }
1176
1177                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1178                         switch(state) {
1179                                 case FormWindowState.Normal: {
1180                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1181                                         return;
1182                                 }
1183
1184                                 case FormWindowState.Minimized: {
1185                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1186                                         return;
1187                                 }
1188
1189                                 case FormWindowState.Maximized: {
1190                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1191                                         return;
1192                                 }
1193                         }
1194                 }
1195
1196                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1197
1198                         if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
1199                                 SetMdiStyles (cp);
1200                         }
1201
1202                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1203                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1204                 }
1205
1206                 
1207                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1208                         LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1209                         byte opacity = (byte)(transparency*255);
1210                         COLORREF clrRef = new COLORREF();
1211                         if (key != Color.Empty) {
1212                                 clrRef.R = key.R;
1213                                 clrRef.G = key.G;
1214                                 clrRef.B = key.B;
1215                                 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1216                         }
1217                         RECT rc;
1218                         rc.right = 1000;
1219                         rc.bottom = 1000;
1220                         Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1221                 }
1222
1223                 internal override bool SupportsTransparency() {
1224                         // We might check with the OS, but I think we're only >=W2k
1225                         return true;
1226                 }
1227
1228                 internal override void UpdateWindow(IntPtr handle) {
1229                         Win32UpdateWindow(handle);
1230                 }
1231
1232                 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1233                         IntPtr          hdc;
1234                         PAINTSTRUCT     ps;
1235                         PaintEventArgs  paint_event;
1236                         RECT            rect;
1237                         Rectangle       clip_rect;
1238                         Hwnd            hwnd;
1239
1240                         clip_rect = new Rectangle();
1241                         rect = new RECT();
1242                         ps = new PAINTSTRUCT();
1243
1244                         hwnd = Hwnd.ObjectFromHandle(handle);
1245
1246                         if (client) {
1247                                 if (Win32GetUpdateRect(handle, ref rect, false)) {
1248                                         hdc = Win32BeginPaint(handle, ref ps);
1249
1250                                         hwnd.user_data = (object)ps;
1251
1252                                         clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1253                                 } else {
1254                                         hdc = Win32GetDC(handle);
1255                                         // FIXME: Add the DC to internal list
1256                                         clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1257                                 }
1258                         } else {
1259                                 hdc = Win32GetWindowDC (handle);
1260                                 hwnd.user_data = (object)hdc;
1261
1262                                 // HACK this in for now
1263                                 Win32GetWindowRect (handle, out rect);
1264                                 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1265                         }
1266
1267                         hwnd.client_dc = Graphics.FromHdc(hdc);
1268                         paint_event = new PaintEventArgs(hwnd.client_dc, clip_rect);
1269
1270                         return paint_event;
1271                 }
1272
1273                 internal override void PaintEventEnd(IntPtr handle, bool client) {
1274                         Hwnd            hwnd;
1275                         PAINTSTRUCT     ps;
1276
1277                         hwnd = Hwnd.ObjectFromHandle(handle);
1278                         hwnd.client_dc.Dispose();
1279
1280                         if (client) {
1281                                 if (hwnd.user_data != null) {
1282                                         ps = (PAINTSTRUCT)hwnd.user_data;
1283                                         Win32EndPaint(handle, ref ps);
1284                                         hwnd.user_data = null;
1285                                 }
1286                         } else {
1287                                 if (hwnd.user_data != null) {
1288                                         Win32ReleaseDC(handle, (IntPtr)hwnd.user_data);
1289                                         hwnd.user_data = null;
1290                                 }
1291                         }
1292                 }
1293
1294
1295                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1296                         Win32MoveWindow(handle, x, y, width, height, true);
1297                         return;
1298                 }
1299
1300                 internal override void GetWindowPos(IntPtr handle, bool is_toplevel, out int x, out int y, out int width, out int height, out int client_width, out int client_height) {
1301                         IntPtr  parent;
1302                         RECT    rect;
1303                         POINT   pt;
1304
1305                         Win32GetWindowRect(handle, out rect);
1306                         width = rect.right - rect.left;
1307                         height = rect.bottom - rect.top;
1308
1309                         pt.x=rect.left;
1310                         pt.y=rect.top;
1311
1312                         parent = Win32GetParent(handle);
1313                         if (parent == FosterParent) {
1314                                 Win32ScreenToClient(IntPtr.Zero, ref pt);
1315                         } else {
1316                                 Win32ScreenToClient(parent, ref pt);
1317                         }
1318
1319                         x = pt.x;
1320                         y = pt.y;
1321
1322                         Win32GetClientRect(handle, out rect);
1323                         client_width = rect.right - rect.left;
1324                         client_height = rect.bottom - rect.top;
1325                         return;
1326                 }
1327
1328                 internal override void Activate(IntPtr handle) {
1329                         Win32SetActiveWindow(handle);
1330                 }
1331
1332                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1333                         RECT rect;
1334
1335                         rect.left=rc.Left;
1336                         rect.top=rc.Top;
1337                         rect.right=rc.Right;
1338                         rect.bottom=rc.Bottom;
1339                         Win32InvalidateRect(handle, ref rect, clear);
1340                 }
1341
1342                 internal override IntPtr DefWndProc(ref Message msg) {
1343                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1344                         return msg.Result;
1345                 }
1346
1347                 internal override void HandleException(Exception e) {
1348                         StackTrace st = new StackTrace(e);
1349                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1350                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1351                 }
1352
1353                 internal override void DoEvents() {
1354                         MSG msg = new MSG();
1355
1356                         if (override_cursor != IntPtr.Zero) {
1357                                 Cursor.Current = null;
1358                         }
1359
1360                         while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1361                                 XplatUI.TranslateMessage(ref msg);
1362                                 XplatUI.DispatchMessage(ref msg);
1363                         }
1364                 }
1365
1366                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1367                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1368                 }
1369
1370                 internal override void PostQuitMessage(int exitCode) {
1371                         Win32PostQuitMessage(exitCode);
1372                 }
1373
1374                 internal override void RequestNCRecalc(IntPtr handle) {
1375                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOMOVE);
1376                 }
1377
1378                 internal override void ResetMouseHover(IntPtr handle) {
1379                         TRACKMOUSEEVENT tme;
1380
1381                         tme = new TRACKMOUSEEVENT();
1382                         tme.size = Marshal.SizeOf(tme);
1383                         tme.hWnd = handle;
1384                         tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1385                         Win32TrackMouseEvent(ref tme);
1386                 }
1387
1388
1389                 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1390                         return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1391                 }
1392
1393                 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1394                         bool            result;
1395
1396                         if (RetrieveMessage(ref msg)) {
1397                                 return true;
1398                         }
1399
1400                         if (blocking) {
1401                                 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1402                         } else {
1403                                 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1404                                 if (!result) {
1405                                         return false;
1406                                 }
1407                         }
1408
1409                         // We need to fake WM_MOUSE_ENTER/WM_MOUSE_LEAVE
1410                         switch (msg.message) {
1411                                 case Msg.WM_LBUTTONDOWN: {
1412                                         mouse_state |= MouseButtons.Left;
1413                                         break;
1414                                 }
1415
1416                                 case Msg.WM_MBUTTONDOWN: {
1417                                         mouse_state |= MouseButtons.Middle;
1418                                         break;
1419                                 }
1420
1421                                 case Msg.WM_RBUTTONDOWN: {
1422                                         mouse_state |= MouseButtons.Right;
1423                                         break;
1424                                 }
1425
1426                                 case Msg.WM_LBUTTONUP: {
1427                                         mouse_state &= ~MouseButtons.Left;
1428                                         break;
1429                                 }
1430
1431                                 case Msg.WM_MBUTTONUP: {
1432                                         mouse_state &= ~MouseButtons.Middle;
1433                                         break;
1434                                 }
1435
1436                                 case Msg.WM_RBUTTONUP: {
1437                                         mouse_state &= ~MouseButtons.Right;
1438                                         break;
1439                                 }
1440
1441                                 case Msg.WM_ASYNC_MESSAGE: {
1442                                         XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1443                                         break;
1444                                 }
1445
1446                                 case Msg.WM_MOUSEMOVE: {
1447                                         if (msg.hwnd != prev_mouse_hwnd) {
1448                                                 TRACKMOUSEEVENT tme;
1449
1450                                                 // The current message will be sent out next time around
1451                                                 StoreMessage(ref msg);
1452
1453                                                 // This is the message we want to send at this point
1454                                                 msg.message = Msg.WM_MOUSE_ENTER;
1455
1456                                                 prev_mouse_hwnd = msg.hwnd;
1457
1458                                                 tme = new TRACKMOUSEEVENT();
1459                                                 tme.size = Marshal.SizeOf(tme);
1460                                                 tme.hWnd = msg.hwnd;
1461                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1462                                                 Win32TrackMouseEvent(ref tme);
1463                                                 return result;
1464                                         }
1465                                         break;
1466                                 }
1467
1468                                 case Msg.WM_DROPFILES: {
1469                                         return Win32DnD.HandleWMDropFiles(ref msg);
1470                                 }
1471
1472                                 case Msg.WM_MOUSELEAVE: {
1473                                         prev_mouse_hwnd = IntPtr.Zero;
1474                                         msg.message=Msg.WM_MOUSE_LEAVE;
1475                                         break;
1476                                 }
1477
1478                                 case Msg.WM_TIMER: {
1479                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
1480
1481                                         if (timer != null) {
1482                                                 timer.FireTick();
1483                                         }
1484                                         break;
1485                                 }
1486                         }
1487
1488                         return result;
1489                 }
1490
1491                 internal override bool TranslateMessage(ref MSG msg) {
1492                         return Win32TranslateMessage(ref msg);
1493                 }
1494
1495                 internal override IntPtr DispatchMessage(ref MSG msg) {
1496                         return Win32DispatchMessage(ref msg);
1497                 }
1498
1499                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1500                         if (Top) {
1501                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1502                                 return true;
1503                         } else if (!Bottom) {
1504                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1505                         } else {
1506                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1507                                 return true;
1508                         }
1509                         return false;
1510                 }
1511
1512                 internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
1513                         if (Enabled) {
1514                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1515                                 return true;
1516                         } else {
1517                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1518                                 return true;
1519                         }
1520                 }
1521
1522                 internal override bool Text(IntPtr handle, string text) {
1523                         Win32SetWindowText(handle, text);
1524                         return true;
1525                 }
1526
1527                 internal override bool GetText(IntPtr handle, out string text) {
1528                         StringBuilder sb;
1529
1530                         sb = new StringBuilder(256);
1531                         Win32GetWindowText(handle, sb, sb.Capacity);
1532                         text = sb.ToString();
1533                         return true;
1534                 }
1535
1536                 internal override bool SetVisible(IntPtr handle, bool visible) {
1537                         if (visible) {
1538                                 if (Control.FromHandle(handle) is Form) {
1539                                         Form f;
1540
1541                                         f = (Form)Control.FromHandle(handle);
1542                                         switch (f.WindowState) {
1543                                                 case FormWindowState.Normal:    Win32ShowWindow(handle, WindowPlacementFlags.SW_SHOWNORMAL); break;
1544                                                 case FormWindowState.Minimized: Win32ShowWindow(handle, WindowPlacementFlags.SW_MINIMIZE); break;
1545                                                 case FormWindowState.Maximized: Win32ShowWindow(handle, WindowPlacementFlags.SW_MAXIMIZE); break;
1546                                         }
1547                                 } else {
1548                                         Win32ShowWindow(handle, WindowPlacementFlags.SW_SHOWNORMAL);
1549                                 }
1550                         } else {
1551                                 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1552                         }
1553                         return true;
1554                 }
1555
1556                 internal override bool IsEnabled(IntPtr handle) {
1557                         return IsWindowEnabled (handle);
1558                 }
1559                 
1560                 internal override bool IsVisible(IntPtr handle) {
1561                         return IsWindowVisible (handle);
1562                 }
1563
1564                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1565                         return Win32SetParent(handle, parent);
1566                 }
1567
1568                 internal override IntPtr GetParent(IntPtr handle) {
1569                         return Win32GetParent(handle);
1570                 }
1571
1572                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1573                         grab_hwnd = hWnd;
1574                         Win32SetCapture(hWnd);
1575                 }
1576
1577                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1578                         hWnd = grab_hwnd;
1579                         GrabConfined = grab_confined;
1580                         GrabArea = grab_area;
1581                 }
1582
1583                 internal override void UngrabWindow(IntPtr hWnd) {
1584                         Win32ReleaseCapture();
1585                         grab_hwnd = IntPtr.Zero;
1586                 }
1587
1588                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect) {
1589                         RECT    rect;
1590
1591                         rect.left=ClientRect.Left;
1592                         rect.top=ClientRect.Top;
1593                         rect.right=ClientRect.Right;
1594                         rect.bottom=ClientRect.Bottom;
1595
1596                         if (!Win32AdjustWindowRectEx(ref rect, Style, menu != null, ExStyle)) {
1597                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1598                                 return false;
1599                         }
1600
1601                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1602                         return true;
1603                 }
1604
1605                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1606                         Win32SetCursor(cursor);
1607                         return;
1608                 }
1609
1610                 internal override void ShowCursor(bool show) {
1611                         Win32ShowCursor(show);
1612                 }
1613
1614                 internal override void OverrideCursor(IntPtr cursor) {
1615                         Win32SetCursor(cursor);
1616                 }
1617
1618                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1619                         IntPtr  cursor;
1620                         Bitmap  cursor_bitmap;
1621                         Bitmap  cursor_mask;
1622                         Byte[]  cursor_bits;
1623                         Byte[]  mask_bits;
1624                         Color   pixel;
1625                         int     width;
1626                         int     height;
1627
1628                         // Win32 only allows creation cursors of a certain size
1629                         if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
1630                                 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1631                                 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1632                         } else {
1633                                 cursor_bitmap = bitmap;
1634                                 cursor_mask = mask;
1635                         }
1636
1637                         width = cursor_bitmap.Width;
1638                         height = cursor_bitmap.Height;
1639
1640                         cursor_bits = new Byte[(width / 8) * height];
1641                         mask_bits = new Byte[(width / 8) * height];
1642
1643                         for (int y = 0; y < height; y++) {
1644                                 for (int x = 0; x < width; x++) {
1645                                         pixel = cursor_bitmap.GetPixel(x, y);
1646
1647                                         if (pixel == cursor_pixel) {
1648                                                 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1649                                         }
1650
1651                                         pixel = cursor_mask.GetPixel(x, y);
1652
1653                                         if (pixel == mask_pixel) {
1654                                                 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1655                                         }
1656                                 }
1657                         }
1658
1659                         cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
1660
1661                         return cursor;
1662                 }
1663
1664                 [MonoTODO("Define the missing cursors")]
1665                 internal override IntPtr DefineStdCursor(StdCursor id) {
1666                         switch(id) {
1667                                 case StdCursor.AppStarting:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
1668                                 case StdCursor.Arrow:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1669                                 case StdCursor.Cross:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
1670                                 case StdCursor.Default:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1671                                 case StdCursor.Hand:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
1672                                 case StdCursor.Help:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
1673                                 case StdCursor.HSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1674                                 case StdCursor.IBeam:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
1675                                 case StdCursor.No:              return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
1676                                 case StdCursor.NoMove2D:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1677                                 case StdCursor.NoMoveHoriz:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1678                                 case StdCursor.NoMoveVert:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1679                                 case StdCursor.PanEast:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1680                                 case StdCursor.PanNE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1681                                 case StdCursor.PanNorth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1682                                 case StdCursor.PanNW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1683                                 case StdCursor.PanSE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1684                                 case StdCursor.PanSouth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1685                                 case StdCursor.PanSW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1686                                 case StdCursor.PanWest:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1687                                 case StdCursor.SizeAll:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
1688                                 case StdCursor.SizeNESW:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
1689                                 case StdCursor.SizeNS:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
1690                                 case StdCursor.SizeNWSE:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
1691                                 case StdCursor.SizeWE:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
1692                                 case StdCursor.UpArrow:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
1693                                 case StdCursor.VSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1694                                 case StdCursor.WaitCursor:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
1695                         }
1696                         throw new NotImplementedException ();
1697                 }
1698
1699                 internal override void DestroyCursor(IntPtr cursor) {
1700                         if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
1701                                 Win32DestroyCursor(cursor);
1702                         }
1703                 }
1704
1705                 [MonoTODO]
1706                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
1707                         width = 20;
1708                         height = 20;
1709                         hotspot_x = 0;
1710                         hotspot_y = 0;
1711                 }
1712
1713                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1714                         Win32SetCursorPos(x, y);
1715                 }
1716
1717                 internal override Region GetClipRegion(IntPtr hwnd) {
1718                         Region region;
1719
1720                         region = new Region();
1721
1722                         Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
1723
1724                         return region;
1725                 }
1726
1727                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
1728                         Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
1729                 }
1730
1731                 internal override void EnableWindow(IntPtr handle, bool Enable) {
1732                         Win32EnableWindow(handle, Enable);
1733                 }
1734
1735                 internal override void EndLoop(System.Threading.Thread thread) {
1736                         // Nothing to do
1737                 }
1738
1739                 internal override object StartLoop(System.Threading.Thread thread) {
1740                         return null;
1741                 }
1742
1743                 internal override void SetModal(IntPtr handle, bool Modal) {
1744                         // we do nothing on Win32
1745                 }
1746
1747                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
1748                         POINT   pt;
1749
1750                         Win32GetCursorPos(out pt);
1751
1752                         if (handle!=IntPtr.Zero) {
1753                                 Win32ScreenToClient(handle, ref pt);
1754                         }
1755
1756                         x=pt.x;
1757                         y=pt.y;
1758                 }
1759
1760                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
1761                 {
1762                         POINT pnt = new POINT();                        
1763
1764                         pnt.x = x;
1765                         pnt.y = y;
1766                         Win32ScreenToClient (handle, ref pnt);
1767
1768                         x = pnt.x;
1769                         y = pnt.y;
1770                 }
1771
1772                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
1773                         POINT   pnt = new POINT();                      
1774
1775                         pnt.x = x;
1776                         pnt.y = y;
1777
1778                         Win32ClientToScreen(handle, ref pnt);
1779
1780                         x = pnt.x;
1781                         y = pnt.y;
1782                 }
1783
1784                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
1785                         RECT    rect;
1786  
1787                         Win32GetWindowRect(handle, out rect);
1788                         x -= rect.left + SystemInformation.FrameBorderSize.Width;
1789                         y -= rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
1790                         return;
1791                 }
1792   
1793                 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {                      
1794                         RECT    rect;
1795  
1796                         Win32GetWindowRect(handle, out rect);
1797                         x += rect.left + SystemInformation.FrameBorderSize.Width;
1798                         y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
1799                         return;
1800                 }
1801   
1802                 internal override void SendAsyncMethod (AsyncMethodData method)
1803                 {
1804                         Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
1805                 }
1806
1807                 internal override void SetTimer (Timer timer)
1808                 {
1809                         int     index;
1810
1811                         index = timer.GetHashCode();
1812
1813                         lock (timer_list) {
1814                                 timer_list[index]=timer;
1815                         }
1816
1817                         Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
1818                 }
1819
1820                 internal override void KillTimer (Timer timer)
1821                 {
1822                         int     index;
1823
1824                         index = timer.GetHashCode();
1825
1826                         Win32KillTimer(FosterParent, index);
1827
1828                         lock (timer_list) {
1829                                 timer_list.Remove(index);
1830                         }
1831                 }
1832
1833
1834                 private void CaretCallback(object sender, EventArgs e) {
1835                         Console.WriteLine("CaretCallback hit");
1836                 }
1837
1838                 private void SetMdiStyles (CreateParams cp)
1839                 {
1840                         cp.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
1841                         cp.ExStyle = 0;
1842                 }
1843         
1844                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
1845                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
1846                         caret_visible = false;
1847                 }
1848
1849                 internal override void DestroyCaret(IntPtr hwnd) {
1850                         Win32DestroyCaret();
1851                 }
1852
1853                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
1854                         Win32SetCaretPos(x, y);
1855                 }
1856
1857                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
1858                         if (visible) {
1859                                 if (!caret_visible) {
1860                                         Win32ShowCaret(hwnd);
1861                                         caret_visible = true;
1862                                 }
1863                         } else {
1864                                 if (caret_visible) {
1865                                         Win32HideCaret(hwnd);
1866                                         caret_visible = false;
1867                                 }
1868                         }
1869                 }
1870
1871                 internal override IntPtr GetFocus() {
1872                         return Win32GetFocus();
1873                 }
1874
1875                 internal override void SetFocus(IntPtr hwnd) {
1876                         Win32SetFocus(hwnd);
1877                 }
1878
1879                 internal override IntPtr GetActive() {
1880                         return Win32GetActiveWindow();
1881                 }
1882
1883                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
1884                         IntPtr          dc;
1885                         TEXTMETRIC      tm;
1886
1887                         tm = new TEXTMETRIC();
1888
1889                         dc = Win32GetDC(IntPtr.Zero);
1890                         Win32SelectObject(dc, font.ToHfont());
1891                         if (Win32GetTextMetrics(dc, ref tm) == false) {
1892                                 Win32ReleaseDC(IntPtr.Zero, dc);
1893                                 ascent = 0;
1894                                 descent = 0;
1895                                 return false;
1896                         }
1897                         Win32ReleaseDC(IntPtr.Zero, dc);
1898
1899                         ascent = tm.tmAscent;
1900                         descent = tm.tmDescent;
1901
1902                         return true;
1903                 }
1904
1905                 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
1906                         RECT    rect;
1907
1908                         rect = new RECT();
1909                         rect.left = rectangle.X;
1910                         rect.top = rectangle.Y;
1911                         rect.right = rectangle.Right;
1912                         rect.bottom = rectangle.Bottom;
1913
1914                         Win32ScrollWindowEx(hwnd, XAmount, YAmount, ref rect, ref rect, IntPtr.Zero, IntPtr.Zero, ScrollWindowExFlags.SW_INVALIDATE | ScrollWindowExFlags.SW_ERASE | (with_children ? ScrollWindowExFlags.SW_SCROLLCHILDREN : ScrollWindowExFlags.SW_NONE));
1915                         Win32UpdateWindow(hwnd);
1916                 }
1917
1918                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
1919                         Win32ScrollWindowEx(hwnd, XAmount, YAmount, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ScrollWindowExFlags.SW_INVALIDATE | ScrollWindowExFlags.SW_ERASE | (with_children ? ScrollWindowExFlags.SW_SCROLLCHILDREN : ScrollWindowExFlags.SW_NONE));
1920                 }
1921
1922                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
1923                         NOTIFYICONDATA  nid;
1924
1925                         nid = new NOTIFYICONDATA();
1926
1927                         nid.cbSize = (uint)Marshal.SizeOf(nid);
1928                         nid.hWnd = hwnd;
1929                         nid.uID = 1;
1930                         nid.uCallbackMessage = (uint)Msg.WM_USER;
1931                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
1932
1933                         if (tip != null) {       
1934                                 nid.szTip = tip;
1935                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
1936                         }
1937
1938                         if (icon != null) {
1939                                 nid.hIcon = icon.Handle;
1940                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
1941                         }
1942
1943                         tt = null;
1944
1945                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
1946                 }
1947
1948                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
1949                         NOTIFYICONDATA  nid;
1950
1951                         nid = new NOTIFYICONDATA();
1952
1953                         nid.cbSize = (uint)Marshal.SizeOf(nid);
1954                         nid.hIcon = icon.Handle;
1955                         nid.hWnd = hwnd;
1956                         nid.uID = 1;
1957                         nid.uCallbackMessage = (uint)Msg.WM_USER;
1958                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
1959
1960                         if (tip != null) {
1961                                 nid.szTip = tip;
1962                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
1963                         }
1964
1965                         if (icon != null) {
1966                                 nid.hIcon = icon.Handle;
1967                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
1968                         }
1969
1970                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
1971                 }
1972
1973                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
1974                         NOTIFYICONDATA  nid;
1975
1976                         nid = new NOTIFYICONDATA();
1977
1978                         nid.cbSize = (uint)Marshal.SizeOf(nid);
1979                         nid.hWnd = hwnd;
1980                         nid.uID = 1;
1981                         nid.uFlags = 0;
1982
1983                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
1984                 }
1985
1986
1987                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
1988                         uint    style;
1989                         uint    exstyle;
1990
1991                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1992                         exstyle = Win32GetWindowLong(handle, WindowLong.GWL_EXSTYLE);
1993
1994                         switch (border_style) {
1995                                 case FormBorderStyle.None: {
1996                                         style &= ~(uint)WindowStyles.WS_BORDER;
1997                                         exstyle &= ~(uint)WindowExStyles.WS_EX_CLIENTEDGE;
1998                                         break;
1999                                 }
2000
2001                                 case FormBorderStyle.FixedSingle: {
2002                                         style |= (uint)WindowStyles.WS_BORDER;
2003                                         exstyle &= ~(uint)WindowExStyles.WS_EX_CLIENTEDGE;
2004                                         break;
2005                                 }
2006
2007                                 case FormBorderStyle.Fixed3D: {
2008                                         style |= (uint)WindowStyles.WS_BORDER;
2009                                         exstyle |= (uint)WindowExStyles.WS_EX_CLIENTEDGE;
2010                                         break;
2011                                 }
2012                         }
2013
2014                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, style);
2015                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, exstyle);
2016                         
2017                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, 
2018                                 SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2019                 }
2020
2021                 internal override void SetMenu(IntPtr handle, Menu menu) {
2022                         // Trigger WM_NCCALC
2023                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2024                 }
2025
2026
2027                 internal override Point GetMenuOrigin(IntPtr handle) {
2028                         return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2029                 }
2030
2031                 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2032                         Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon.Handle); // 1 = large icon (0 would be small)
2033                 }
2034
2035                 internal override void ClipboardClose(IntPtr handle) {
2036                         if (handle != clip_magic) {
2037                                 throw new ArgumentException("handle is not a valid clipboard handle");
2038                         }
2039                         Win32CloseClipboard();
2040                 }
2041
2042                 internal override int ClipboardGetID(IntPtr handle, string format) {
2043                         if (handle != clip_magic) {
2044                                 throw new ArgumentException("handle is not a valid clipboard handle");
2045                         }
2046                         if (format == "Text" ) return 1;
2047                         else if (format == "Bitmap" ) return 2;
2048                         else if (format == "MetaFilePict" ) return 3;
2049                         else if (format == "SymbolicLink" ) return 4;
2050                         else if (format == "DataInterchangeFormat" ) return 5;
2051                         else if (format == "Tiff" ) return 6;
2052                         else if (format == "OEMText" ) return 7;
2053                         else if (format == "DeviceIndependentBitmap" ) return 8;
2054                         else if (format == "Palette" ) return 9;
2055                         else if (format == "PenData" ) return 10;
2056                         else if (format == "RiffAudio" ) return 11;
2057                         else if (format == "WaveAudio" ) return 12;
2058                         else if (format == "UnicodeText" ) return 13;
2059                         else if (format == "EnhancedMetafile" ) return 14;
2060                         else if (format == "FileDrop" ) return 15;
2061                         else if (format == "Locale" ) return 16;
2062
2063                         return (int)Win32RegisterClipboardFormat(format);
2064                 }
2065
2066                 internal override IntPtr ClipboardOpen() {
2067                         Win32OpenClipboard(FosterParent);
2068                         return clip_magic;
2069                 }
2070
2071                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2072                         uint    format;
2073                         int[]   result;
2074                         int     count;
2075
2076                         if (handle != clip_magic) {
2077                                 return null;
2078                         }
2079
2080                         // Count first
2081                         count = 0;
2082                         format = 0;
2083                         do {
2084                                 format = Win32EnumClipboardFormats(format);
2085                                 if (format != 0) {
2086                                         count++;
2087                                 }
2088                         } while (format != 0);
2089
2090                         // Now assign
2091                         result = new int[count];
2092                         count = 0;
2093                         format = 0;
2094                         do {
2095                                 format = Win32EnumClipboardFormats(format);
2096                                 if (format != 0) {
2097                                         result[count++] = (int)format;
2098                                 }
2099                         } while (format != 0);
2100
2101                         return result;
2102                 }
2103
2104
2105                 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2106                         IntPtr  hmem;
2107                         IntPtr  data;
2108                         object  obj;
2109
2110                         if (handle != clip_magic) {
2111                                 throw new ArgumentException("handle is not a valid clipboard handle");
2112                         }
2113
2114                         hmem = Win32GetClipboardData((uint)type);
2115                         if (hmem == IntPtr.Zero) {
2116                                 return null;
2117                         }
2118
2119                         data = Win32GlobalLock(hmem);
2120                         if (data == IntPtr.Zero) {
2121                                 uint error = Win32GetLastError();
2122                                 Console.WriteLine("Error: {0}", error);
2123                                 return null;
2124                         }
2125
2126                         obj = null;
2127
2128                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2129                                 obj = AnsiToString(data);
2130                         } else switch ((ClipboardFormats)type) {
2131                                 case ClipboardFormats.CF_TEXT: {
2132                                         obj = AnsiToString(data);
2133                                         break;
2134                                 }
2135
2136                                 case ClipboardFormats.CF_DIB: {
2137                                         obj = DIBtoImage(data);
2138                                         break;
2139                                 }
2140
2141                                 case ClipboardFormats.CF_UNICODETEXT: {
2142                                         obj = UnicodeToString(data);
2143                                         break;
2144                                 }
2145
2146                                 default: {
2147                                         if (converter != null && !converter(type, data, out obj)) {
2148                                                 obj = null;
2149                                         }
2150                                         break;
2151                                 }
2152                         }
2153                         Win32GlobalUnlock(hmem);
2154
2155                         return obj;
2156
2157                 }
2158
2159                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2160                         byte[]  data;
2161                         IntPtr  hmem;
2162                         IntPtr  hmem_ptr;
2163
2164                         if (handle != clip_magic) {
2165                                 throw new ArgumentException("handle is not a valid clipboard handle");
2166                         }
2167
2168                         if (obj == null) {
2169                                 // Just clear it
2170                                 Win32EmptyClipboard();
2171                                 return;
2172                         }
2173
2174                         if (type == -1) {
2175                                 if (obj is string) {
2176                                         type = (int)ClipboardFormats.CF_UNICODETEXT;
2177                                 } else if (obj is Image) {
2178                                         type = (int)ClipboardFormats.CF_DIB;
2179                                 }
2180                         }
2181
2182                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2183                                 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2184                                 Win32SetClipboardData((uint)type, hmem);
2185                                 return;
2186                         } else switch((ClipboardFormats)type) {
2187                                 case ClipboardFormats.CF_UNICODETEXT: {
2188                                         hmem = Marshal.StringToHGlobalUni((string)obj);
2189                                         Win32SetClipboardData((uint)type, hmem);
2190                                         return;
2191                                 }
2192
2193                                 case ClipboardFormats.CF_TEXT: {
2194                                         hmem = Marshal.StringToHGlobalAnsi((string)obj);
2195                                         Win32SetClipboardData((uint)type, hmem);
2196                                         return;
2197                                 }
2198
2199                                 case ClipboardFormats.CF_BITMAP:
2200                                 case ClipboardFormats.CF_DIB: {
2201                                         data = ImageToDIB((Image)obj);
2202
2203                                         hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2204                                         hmem_ptr = Win32GlobalLock(hmem);
2205                                         Marshal.Copy(data, 0, hmem_ptr, data.Length);
2206                                         Win32GlobalUnlock(hmem);
2207                                         Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2208                                         return;
2209                                 }
2210
2211                                 default: {
2212                                         if (converter != null && converter(ref type, obj, out data)) {
2213                                                 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2214                                                 hmem_ptr = Win32GlobalLock(hmem);
2215                                                 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2216                                                 Win32GlobalUnlock(hmem);
2217                                                 Win32SetClipboardData((uint)type, hmem);
2218                                         }
2219                                         return;
2220                                 }
2221                         }
2222                 }
2223
2224                 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2225                         if (allowed) {
2226                                 Win32DnD.RegisterDropTarget(hwnd);
2227                         } else {
2228                                 Win32DnD.UnregisterDropTarget(hwnd);
2229                         }
2230                 }
2231
2232                 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2233                         return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2234                 }
2235
2236                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2237                         IntPtr          hdc;
2238                         IntPtr          pen;
2239                         IntPtr          oldpen;
2240                         POINT           pt;
2241
2242                         pt = new POINT();
2243                         pt.x = 0;
2244                         pt.y = 0;
2245                         Win32ClientToScreen(handle, ref pt);
2246
2247                         // If we want the standard hatch pattern we would
2248                         // need to create a brush
2249
2250                         // Grab a pen
2251                         pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2252
2253                         hdc = Win32GetDC(IntPtr.Zero);
2254                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2255                         oldpen = Win32SelectObject(hdc, pen);
2256
2257                         // We might need to add clipping to the WindowRect of 'handle' - right now we're drawing on the desktop
2258
2259                         Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2260                         if ((rect.Width > 0) && (rect.Height > 0)) {
2261                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2262                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2263                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2264                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2265                         } else {
2266                                 if (rect.Width > 0) {
2267                                         Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2268                                 } else {
2269                                         Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2270                                 }
2271                         }
2272
2273                         Win32SelectObject(hdc, oldpen);
2274                         Win32DeleteObject(pen);
2275                         Win32ReleaseDC(IntPtr.Zero, hdc);
2276                 }
2277
2278                 internal override SizeF GetAutoScaleSize(Font font) {
2279                         Graphics        g;
2280                         float           width;
2281                         string          magic_string = "The quick brown fox jumped over the lazy dog.";
2282                         double          magic_number = 44.549996948242189;
2283
2284                         g = Graphics.FromHwnd(FosterParent);
2285
2286                         width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2287                         return new SizeF(width, font.Height);
2288                 }
2289
2290                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2291                         return Win32SendMessage(hwnd, message, wParam, lParam);
2292                 }
2293
2294                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2295                         return Win32PostMessage(hwnd, message, wParam, lParam);
2296                 }
2297
2298                 internal override int KeyboardSpeed {
2299                         get {
2300                                 Console.WriteLine ("KeyboardSpeed: need to query Windows");
2301
2302                                 //
2303                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2304                                 //
2305                                 return 0;
2306                         }
2307                 }
2308
2309                 internal override int KeyboardDelay {
2310                         get {
2311                                 Console.WriteLine ("KeyboardDelay: need to query Windows");
2312
2313                                 //
2314                                 // Return values must range from 0 to 4, 0 meaning 250ms,
2315                                 // and 4 meaning 1000 ms.
2316                                 //
2317                                 return 1;
2318                         }
2319                 }
2320                 
2321                 internal override event EventHandler Idle;
2322
2323                 // Santa's little helper
2324                 static void Where() {
2325                         Console.WriteLine("Here: {0}", new StackTrace().ToString());
2326                 }
2327                 #endregion      // Public Static Methods
2328
2329                 #region Win32 Imports
2330                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2331                 private extern static uint Win32GetLastError();
2332
2333                 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2334                 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);
2335
2336                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2337                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2338
2339                 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2340                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2341
2342                 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2343                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2344
2345                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2346                 internal extern static bool Win32TranslateMessage(ref MSG msg);
2347
2348                 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2349                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2350
2351                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2352                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2353
2354                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2355                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2356
2357                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2358                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2359
2360                 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2361                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2362
2363                 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2364                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2365
2366                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2367                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2368
2369                 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2370                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2371
2372                 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2373                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2374
2375                 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2376                 private extern static IntPtr Win32ShowCursor(bool bShow);
2377
2378                 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2379                 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2380
2381                 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2382                 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2383
2384                 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2385                 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2386
2387                 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2388                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2389
2390                 [DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2391                 private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2392
2393                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2394                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2395
2396                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2397                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2398
2399                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2400                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2401
2402                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2403                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2404
2405                 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2406                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2407
2408                 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2409                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2410
2411                 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2412                 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2413
2414                 [DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2415                 private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2416
2417                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2418                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2419
2420                 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2421                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
2422
2423                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2424                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
2425
2426                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2427                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
2428
2429                 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
2430                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
2431
2432                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
2433                 private extern static IntPtr Win32ReleaseCapture();
2434
2435                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
2436                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
2437
2438                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
2439                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
2440
2441                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
2442                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
2443
2444                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
2445                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
2446
2447                 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
2448                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
2449
2450                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2451                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
2452
2453                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
2454                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
2455
2456                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
2457                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
2458
2459                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
2460                 private extern static bool Win32SetCursorPos(int x, int y);
2461
2462                 [DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
2463                 private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
2464
2465                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
2466                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
2467
2468                 [DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
2469                 private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
2470
2471                 [DllImport ("user32.dll", EntryPoint="FillRect", CallingConvention=CallingConvention.StdCall)]
2472                 private extern static int Win32FillRect(IntPtr hdc, ref RECT rect, IntPtr hbr);
2473
2474                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
2475                 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
2476
2477                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
2478                 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
2479
2480                 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2481                 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
2482
2483                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
2484                 private extern static bool Win32DeleteObject(IntPtr o);
2485
2486                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
2487                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
2488
2489                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
2490                 private extern static IntPtr Win32GetDesktopWindow();
2491
2492                 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
2493                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
2494
2495                 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
2496                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
2497
2498                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
2499                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
2500
2501                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
2502                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
2503
2504                 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
2505                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
2506
2507                 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
2508                 internal extern static IntPtr Win32GetFocus();
2509
2510                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
2511                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
2512
2513                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
2514                 private  extern static bool Win32DestroyCaret();
2515
2516                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
2517                 private  extern static bool Win32ShowCaret(IntPtr hwnd);
2518
2519                 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
2520                 private  extern static bool Win32HideCaret(IntPtr hwnd);
2521
2522                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
2523                 private  extern static bool Win32SetCaretPos(int X, int Y);
2524
2525                 [DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
2526                 private  extern static uint Win32GetCaretBlinkTime();
2527
2528                 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2529                 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
2530
2531                 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
2532                 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
2533
2534                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2535                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2536
2537                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2538                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2539
2540                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2541                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2542
2543                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2544                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2545
2546                 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2547                 private extern static IntPtr Win32GetActiveWindow();
2548
2549                 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
2550                 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
2551
2552                 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2553                 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
2554
2555                 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
2556                 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
2557
2558                 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
2559                 private extern static bool IsWindowEnabled(IntPtr hwnd);
2560
2561                 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
2562                 private extern static bool IsWindowVisible(IntPtr hwnd);
2563
2564                 [DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
2565                 private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
2566
2567                 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2568                 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
2569
2570                 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2571                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
2572
2573                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2574                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
2575
2576                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2577                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
2578
2579                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2580                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
2581
2582                 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
2583                 private extern static bool Win32OpenClipboard(IntPtr hwnd);
2584
2585                 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
2586                 private extern static bool Win32EmptyClipboard();
2587
2588                 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2589                 private extern static uint Win32RegisterClipboardFormat(string format);
2590
2591                 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
2592                 private extern static bool Win32CloseClipboard();
2593
2594                 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
2595                 private extern static uint Win32EnumClipboardFormats(uint format);
2596
2597                 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
2598                 private extern static IntPtr Win32GetClipboardData(uint format);
2599
2600                 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
2601                 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
2602
2603                 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
2604                 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
2605
2606                 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
2607                 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
2608
2609                 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
2610                 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
2611
2612                 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
2613                 internal extern static uint Win32GlobalSize(IntPtr hMem);
2614
2615                 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
2616                 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
2617
2618                 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
2619                 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
2620
2621                 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
2622                 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
2623
2624                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
2625                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
2626
2627                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
2628                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
2629
2630                 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
2631                 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
2632
2633                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
2634                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
2635
2636                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
2637                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
2638
2639                 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
2640                 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
2641
2642                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
2643                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
2644
2645                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
2646                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
2647
2648                 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2649                 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
2650
2651                 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2652                 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
2653
2654                 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2655                 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
2656                 #endregion
2657         }
2658 }