2006-11-16 Jonathan Pobst <monkey@jpobst.com
[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
1046                 internal override bool ThemesEnabled {
1047                         get {
1048                                 return XplatUIWin32.themes_enabled;
1049                         }
1050                 }
1051  
1052
1053                 #endregion      // Static Properties
1054
1055                 #region Singleton Specific Code
1056                 public static XplatUIWin32 GetInstance() {
1057                         if (instance==null) {
1058                                 instance=new XplatUIWin32();
1059                         }
1060                         ref_count++;
1061                         return instance;
1062                 }
1063
1064                 public int Reference {
1065                         get {
1066                                 return ref_count;
1067                         }
1068                 }
1069                 #endregion
1070
1071                 #region Public Static Methods
1072                 internal override IntPtr InitializeDriver() {
1073                         return IntPtr.Zero;
1074                 }
1075
1076                 internal override void ShutdownDriver(IntPtr token) {
1077                         Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1078                 }
1079
1080
1081                 internal void Version() {
1082                         Console.WriteLine("Xplat version $revision: $");
1083                 }
1084
1085                 internal override void AudibleAlert() {
1086                         Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1087                 }
1088
1089                 internal override void GetDisplaySize(out Size size) {
1090                         RECT    rect;
1091
1092                         Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1093
1094                         size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1095                 }
1096
1097                 internal override void EnableThemes() {
1098                         themes_enabled=true;
1099                 }
1100
1101                 internal override IntPtr CreateWindow(CreateParams cp) {
1102                         IntPtr  WindowHandle;
1103                         IntPtr  ParentHandle;
1104                         Hwnd    hwnd;
1105
1106                         hwnd = new Hwnd();
1107
1108                         ParentHandle=cp.Parent;
1109
1110                         if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1111                                 // We need to use our foster parent window until this poor child gets it's parent assigned
1112                                 ParentHandle = FosterParent;
1113                         }
1114
1115                         if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1116                                 // If we want to be hidden from the taskbar we need to be 'owned' by 
1117                                 // something not on the taskbar. FosterParent is just that
1118                                 ParentHandle = FosterParent;
1119                         }
1120
1121                         // Since we fake MDI dont tell Windows that this is a real MDI window
1122                         if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
1123                                 SetMdiStyles (cp);
1124                         }
1125
1126                         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);
1127
1128                         if (WindowHandle==IntPtr.Zero) {
1129                                 uint error = Win32GetLastError();
1130
1131                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1132                         }
1133
1134                         hwnd.ClientWindow = WindowHandle;
1135
1136                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1137
1138                         return WindowHandle;
1139                 }
1140
1141                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1142                         CreateParams create_params = new CreateParams();
1143
1144                         create_params.Caption = "";
1145                         create_params.X = X;
1146                         create_params.Y = Y;
1147                         create_params.Width = Width;
1148                         create_params.Height = Height;
1149
1150                         create_params.ClassName=XplatUI.DefaultClassName;
1151                         create_params.ClassStyle = 0;
1152                         create_params.ExStyle=0;
1153                         create_params.Parent=IntPtr.Zero;
1154                         create_params.Param=0;
1155
1156                         return CreateWindow(create_params);
1157                 }
1158
1159                 internal override void DestroyWindow(IntPtr handle) {
1160                         Hwnd    hwnd;
1161
1162                         hwnd = Hwnd.ObjectFromHandle(handle);
1163                         Win32DestroyWindow(handle);
1164                         hwnd.Dispose();
1165                         return;
1166                 }
1167
1168                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1169                         // We do nothing, Form has to handle WM_GETMINMAXINFO
1170                 }
1171
1172
1173                 internal override FormWindowState GetWindowState(IntPtr handle) {
1174                         uint style;
1175
1176                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1177                         if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1178                                 return FormWindowState.Maximized;
1179                         } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1180                                 return FormWindowState.Minimized;
1181                         }
1182                         return FormWindowState.Normal;
1183                 }
1184
1185                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1186                         switch(state) {
1187                                 case FormWindowState.Normal: {
1188                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1189                                         return;
1190                                 }
1191
1192                                 case FormWindowState.Minimized: {
1193                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1194                                         return;
1195                                 }
1196
1197                                 case FormWindowState.Maximized: {
1198                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1199                                         return;
1200                                 }
1201                         }
1202                 }
1203
1204                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1205
1206                         if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
1207                                 SetMdiStyles (cp);
1208                         }
1209
1210                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1211                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1212                 }
1213
1214                 
1215                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1216                         LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1217                         byte opacity = (byte)(transparency*255);
1218                         COLORREF clrRef = new COLORREF();
1219                         if (key != Color.Empty) {
1220                                 clrRef.R = key.R;
1221                                 clrRef.G = key.G;
1222                                 clrRef.B = key.B;
1223                                 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1224                         }
1225                         RECT rc;
1226                         rc.right = 1000;
1227                         rc.bottom = 1000;
1228                         Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1229                 }
1230
1231                 internal override bool SupportsTransparency() {
1232                         // We might check with the OS, but I think we're only >=W2k
1233                         return true;
1234                 }
1235
1236                 internal override void UpdateWindow(IntPtr handle) {
1237                         Win32UpdateWindow(handle);
1238                 }
1239
1240                 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1241                         IntPtr          hdc;
1242                         PAINTSTRUCT     ps;
1243                         PaintEventArgs  paint_event;
1244                         RECT            rect;
1245                         Rectangle       clip_rect;
1246                         Hwnd            hwnd;
1247
1248                         clip_rect = new Rectangle();
1249                         rect = new RECT();
1250                         ps = new PAINTSTRUCT();
1251
1252                         hwnd = Hwnd.ObjectFromHandle(handle);
1253
1254                         if (client) {
1255                                 if (Win32GetUpdateRect(handle, ref rect, false)) {
1256                                         hdc = Win32BeginPaint(handle, ref ps);
1257
1258                                         hwnd.drawing_stack.Push (ps);
1259
1260                                         clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1261                                 } else {
1262                                         hdc = Win32GetDC(handle);
1263                                         // FIXME: Add the DC to internal list
1264
1265                                         hwnd.drawing_stack.Push (null);
1266
1267                                         clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1268                                 }
1269                         } else {
1270                                 hdc = Win32GetWindowDC (handle);
1271
1272                                 hwnd.drawing_stack.Push (hdc);
1273
1274                                 // HACK this in for now
1275                                 Win32GetWindowRect (handle, out rect);
1276                                 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1277                         }
1278
1279                         Graphics dc = Graphics.FromHdc(hdc);
1280                         hwnd.drawing_stack.Push (dc);
1281
1282                         paint_event = new PaintEventArgs(dc, clip_rect);
1283
1284                         return paint_event;
1285                 }
1286
1287                 internal override void PaintEventEnd(IntPtr handle, bool client) {
1288                         Hwnd            hwnd;
1289
1290                         hwnd = Hwnd.ObjectFromHandle(handle);
1291
1292                         Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1293                         dc.Dispose ();
1294
1295                         if (client) {
1296                                 object o = hwnd.drawing_stack.Pop();
1297                                 if (o != null) {
1298                                         PAINTSTRUCT ps = (PAINTSTRUCT)o;
1299                                         Win32EndPaint(handle, ref ps);
1300                                 }
1301                         } else {
1302                                 IntPtr hdc = (IntPtr)hwnd.drawing_stack.Pop();
1303                                 Win32ReleaseDC(handle, hdc);
1304                         }
1305                 }
1306
1307
1308                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1309                         Win32MoveWindow(handle, x, y, width, height, true);
1310                         return;
1311                 }
1312
1313                 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) {
1314                         IntPtr  parent;
1315                         RECT    rect;
1316                         POINT   pt;
1317
1318                         Win32GetWindowRect(handle, out rect);
1319                         width = rect.right - rect.left;
1320                         height = rect.bottom - rect.top;
1321
1322                         pt.x=rect.left;
1323                         pt.y=rect.top;
1324
1325                         parent = Win32GetParent(handle);
1326                         Win32ScreenToClient(parent, ref pt);
1327
1328                         x = pt.x;
1329                         y = pt.y;
1330
1331                         Win32GetClientRect(handle, out rect);
1332                         client_width = rect.right - rect.left;
1333                         client_height = rect.bottom - rect.top;
1334                         return;
1335                 }
1336
1337                 internal override void Activate(IntPtr handle) {
1338                         Win32SetActiveWindow(handle);
1339                 }
1340
1341                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1342                         RECT rect;
1343
1344                         rect.left=rc.Left;
1345                         rect.top=rc.Top;
1346                         rect.right=rc.Right;
1347                         rect.bottom=rc.Bottom;
1348                         Win32InvalidateRect(handle, ref rect, clear);
1349                 }
1350
1351                 internal override IntPtr DefWndProc(ref Message msg) {
1352                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1353                         return msg.Result;
1354                 }
1355
1356                 internal override void HandleException(Exception e) {
1357                         StackTrace st = new StackTrace(e);
1358                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1359                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1360                 }
1361
1362                 internal override void DoEvents() {
1363                         MSG msg = new MSG();
1364
1365                         if (override_cursor != IntPtr.Zero) {
1366                                 Cursor.Current = null;
1367                         }
1368
1369                         while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1370                                 XplatUI.TranslateMessage(ref msg);
1371                                 XplatUI.DispatchMessage(ref msg);
1372                         }
1373                 }
1374
1375                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1376                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1377                 }
1378
1379                 internal override void PostQuitMessage(int exitCode) {
1380                         Win32PostQuitMessage(exitCode);
1381                 }
1382
1383                 internal override void RequestNCRecalc(IntPtr handle) {
1384                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOMOVE);
1385                 }
1386
1387                 internal override void ResetMouseHover(IntPtr handle) {
1388                         TRACKMOUSEEVENT tme;
1389
1390                         tme = new TRACKMOUSEEVENT();
1391                         tme.size = Marshal.SizeOf(tme);
1392                         tme.hWnd = handle;
1393                         tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1394                         Win32TrackMouseEvent(ref tme);
1395                 }
1396
1397
1398                 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1399                         return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1400                 }
1401
1402                 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1403                         bool            result;
1404
1405                         if (RetrieveMessage(ref msg)) {
1406                                 return true;
1407                         }
1408
1409                         if (blocking) {
1410                                 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1411                         } else {
1412                                 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1413                                 if (!result) {
1414                                         return false;
1415                                 }
1416                         }
1417
1418                         // We need to fake WM_MOUSE_ENTER/WM_MOUSE_LEAVE
1419                         switch (msg.message) {
1420                                 case Msg.WM_LBUTTONDOWN: {
1421                                         mouse_state |= MouseButtons.Left;
1422                                         break;
1423                                 }
1424
1425                                 case Msg.WM_MBUTTONDOWN: {
1426                                         mouse_state |= MouseButtons.Middle;
1427                                         break;
1428                                 }
1429
1430                                 case Msg.WM_RBUTTONDOWN: {
1431                                         mouse_state |= MouseButtons.Right;
1432                                         break;
1433                                 }
1434
1435                                 case Msg.WM_LBUTTONUP: {
1436                                         mouse_state &= ~MouseButtons.Left;
1437                                         break;
1438                                 }
1439
1440                                 case Msg.WM_MBUTTONUP: {
1441                                         mouse_state &= ~MouseButtons.Middle;
1442                                         break;
1443                                 }
1444
1445                                 case Msg.WM_RBUTTONUP: {
1446                                         mouse_state &= ~MouseButtons.Right;
1447                                         break;
1448                                 }
1449
1450                                 case Msg.WM_ASYNC_MESSAGE: {
1451                                         XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1452                                         break;
1453                                 }
1454
1455                                 case Msg.WM_MOUSEMOVE: {
1456                                         if (msg.hwnd != prev_mouse_hwnd) {
1457                                                 TRACKMOUSEEVENT tme;
1458
1459                                                 // The current message will be sent out next time around
1460                                                 StoreMessage(ref msg);
1461
1462                                                 // This is the message we want to send at this point
1463                                                 msg.message = Msg.WM_MOUSE_ENTER;
1464
1465                                                 prev_mouse_hwnd = msg.hwnd;
1466
1467                                                 tme = new TRACKMOUSEEVENT();
1468                                                 tme.size = Marshal.SizeOf(tme);
1469                                                 tme.hWnd = msg.hwnd;
1470                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1471                                                 Win32TrackMouseEvent(ref tme);
1472                                                 return result;
1473                                         }
1474                                         break;
1475                                 }
1476
1477                                 case Msg.WM_DROPFILES: {
1478                                         return Win32DnD.HandleWMDropFiles(ref msg);
1479                                 }
1480
1481                                 case Msg.WM_MOUSELEAVE: {
1482                                         prev_mouse_hwnd = IntPtr.Zero;
1483                                         msg.message=Msg.WM_MOUSE_LEAVE;
1484                                         break;
1485                                 }
1486
1487                                 case Msg.WM_TIMER: {
1488                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
1489
1490                                         if (timer != null) {
1491                                                 timer.FireTick();
1492                                         }
1493                                         break;
1494                                 }
1495                         }
1496
1497                         return result;
1498                 }
1499
1500                 internal override bool TranslateMessage(ref MSG msg) {
1501                         return Win32TranslateMessage(ref msg);
1502                 }
1503
1504                 internal override IntPtr DispatchMessage(ref MSG msg) {
1505                         return Win32DispatchMessage(ref msg);
1506                 }
1507
1508                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1509                         if (Top) {
1510                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1511                                 return true;
1512                         } else if (!Bottom) {
1513                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1514                         } else {
1515                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1516                                 return true;
1517                         }
1518                         return false;
1519                 }
1520
1521                 internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
1522                         if (Enabled) {
1523                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1524                                 return true;
1525                         } else {
1526                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1527                                 return true;
1528                         }
1529                 }
1530
1531                 internal override bool Text(IntPtr handle, string text) {
1532                         Win32SetWindowText(handle, text);
1533                         return true;
1534                 }
1535
1536                 internal override bool GetText(IntPtr handle, out string text) {
1537                         StringBuilder sb;
1538
1539                         sb = new StringBuilder(256);
1540                         Win32GetWindowText(handle, sb, sb.Capacity);
1541                         text = sb.ToString();
1542                         return true;
1543                 }
1544
1545                 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
1546                 {
1547                         if (visible) {
1548                                 if (Control.FromHandle (handle) is Form) {
1549                                         Form f;
1550
1551                                         f = (Form)Control.FromHandle (handle);
1552                                         WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
1553                                         switch (f.WindowState) {
1554                                                 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
1555                                                 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
1556                                                 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
1557                                         }
1558                                         
1559                                         if (Hwnd.ObjectFromHandle (handle).no_activate)
1560                                                 flags |= WindowPlacementFlags.SW_SHOWNOACTIVATE;
1561                                                 
1562                                         Win32ShowWindow (handle, flags);
1563                                 }
1564                                 else {
1565                                         if (Hwnd.ObjectFromHandle (handle).no_activate)
1566                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
1567                                         else
1568                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
1569                                 }
1570                         }
1571                         else {
1572                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
1573                         }
1574                         return true;
1575                 }
1576
1577                 internal override bool IsEnabled(IntPtr handle) {
1578                         return IsWindowEnabled (handle);
1579                 }
1580                 
1581                 internal override bool IsVisible(IntPtr handle) {
1582                         return IsWindowVisible (handle);
1583                 }
1584
1585                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1586                         Control c = Control.FromHandle (handle);
1587                         if (parent == IntPtr.Zero) {
1588                                 if (!(c is Form)) {
1589                                         Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1590                                 }
1591                         }
1592                         else
1593                                 SetVisible (handle, c.is_visible, true);
1594                                 
1595                         if (parent == IntPtr.Zero)
1596                                 return Win32SetParent (handle, FosterParent);
1597                         else
1598                                 return Win32SetParent(handle, parent);
1599                 }
1600
1601                 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
1602                 internal override IntPtr GetParent(IntPtr handle) {
1603                         return Win32GetParent(handle);
1604                 }
1605
1606                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1607                         grab_hwnd = hWnd;
1608                         Win32SetCapture(hWnd);
1609                 }
1610
1611                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1612                         hWnd = grab_hwnd;
1613                         GrabConfined = grab_confined;
1614                         GrabArea = grab_area;
1615                 }
1616
1617                 internal override void UngrabWindow(IntPtr hWnd) {
1618                         Win32ReleaseCapture();
1619                         grab_hwnd = IntPtr.Zero;
1620                 }
1621
1622                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect) {
1623                         RECT    rect;
1624
1625                         rect.left=ClientRect.Left;
1626                         rect.top=ClientRect.Top;
1627                         rect.right=ClientRect.Right;
1628                         rect.bottom=ClientRect.Bottom;
1629
1630                         if (!Win32AdjustWindowRectEx(ref rect, Style, menu != null, ExStyle)) {
1631                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1632                                 return false;
1633                         }
1634
1635                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1636                         return true;
1637                 }
1638
1639                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1640                         Win32SetCursor(cursor);
1641                         return;
1642                 }
1643
1644                 internal override void ShowCursor(bool show) {
1645                         Win32ShowCursor(show);
1646                 }
1647
1648                 internal override void OverrideCursor(IntPtr cursor) {
1649                         Win32SetCursor(cursor);
1650                 }
1651
1652                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1653                         IntPtr  cursor;
1654                         Bitmap  cursor_bitmap;
1655                         Bitmap  cursor_mask;
1656                         Byte[]  cursor_bits;
1657                         Byte[]  mask_bits;
1658                         Color   pixel;
1659                         int     width;
1660                         int     height;
1661
1662                         // Win32 only allows creation cursors of a certain size
1663                         if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
1664                                 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1665                                 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1666                         } else {
1667                                 cursor_bitmap = bitmap;
1668                                 cursor_mask = mask;
1669                         }
1670
1671                         width = cursor_bitmap.Width;
1672                         height = cursor_bitmap.Height;
1673
1674                         cursor_bits = new Byte[(width / 8) * height];
1675                         mask_bits = new Byte[(width / 8) * height];
1676
1677                         for (int y = 0; y < height; y++) {
1678                                 for (int x = 0; x < width; x++) {
1679                                         pixel = cursor_bitmap.GetPixel(x, y);
1680
1681                                         if (pixel == cursor_pixel) {
1682                                                 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1683                                         }
1684
1685                                         pixel = cursor_mask.GetPixel(x, y);
1686
1687                                         if (pixel == mask_pixel) {
1688                                                 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1689                                         }
1690                                 }
1691                         }
1692
1693                         cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
1694
1695                         return cursor;
1696                 }
1697
1698                 [MonoTODO("Define the missing cursors")]
1699                 internal override IntPtr DefineStdCursor(StdCursor id) {
1700                         switch(id) {
1701                                 case StdCursor.AppStarting:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
1702                                 case StdCursor.Arrow:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1703                                 case StdCursor.Cross:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
1704                                 case StdCursor.Default:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1705                                 case StdCursor.Hand:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
1706                                 case StdCursor.Help:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
1707                                 case StdCursor.HSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1708                                 case StdCursor.IBeam:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
1709                                 case StdCursor.No:              return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
1710                                 case StdCursor.NoMove2D:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1711                                 case StdCursor.NoMoveHoriz:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1712                                 case StdCursor.NoMoveVert:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1713                                 case StdCursor.PanEast:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1714                                 case StdCursor.PanNE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1715                                 case StdCursor.PanNorth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1716                                 case StdCursor.PanNW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1717                                 case StdCursor.PanSE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1718                                 case StdCursor.PanSouth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1719                                 case StdCursor.PanSW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1720                                 case StdCursor.PanWest:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1721                                 case StdCursor.SizeAll:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
1722                                 case StdCursor.SizeNESW:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
1723                                 case StdCursor.SizeNS:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
1724                                 case StdCursor.SizeNWSE:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
1725                                 case StdCursor.SizeWE:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
1726                                 case StdCursor.UpArrow:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
1727                                 case StdCursor.VSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
1728                                 case StdCursor.WaitCursor:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
1729                         }
1730                         throw new NotImplementedException ();
1731                 }
1732
1733                 internal override void DestroyCursor(IntPtr cursor) {
1734                         if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
1735                                 Win32DestroyCursor(cursor);
1736                         }
1737                 }
1738
1739                 [MonoTODO]
1740                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
1741                         width = 20;
1742                         height = 20;
1743                         hotspot_x = 0;
1744                         hotspot_y = 0;
1745                 }
1746
1747                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1748                         Win32SetCursorPos(x, y);
1749                 }
1750
1751                 internal override Region GetClipRegion(IntPtr hwnd) {
1752                         Region region;
1753
1754                         region = new Region();
1755
1756                         Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
1757
1758                         return region;
1759                 }
1760
1761                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
1762                         Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
1763                 }
1764
1765                 internal override void EnableWindow(IntPtr handle, bool Enable) {
1766                         Win32EnableWindow(handle, Enable);
1767                 }
1768
1769                 internal override void EndLoop(System.Threading.Thread thread) {
1770                         // Nothing to do
1771                 }
1772
1773                 internal override object StartLoop(System.Threading.Thread thread) {
1774                         return null;
1775                 }
1776
1777                 internal override void SetModal(IntPtr handle, bool Modal) {
1778                         // we do nothing on Win32
1779                 }
1780
1781                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
1782                         POINT   pt;
1783
1784                         Win32GetCursorPos(out pt);
1785
1786                         if (handle!=IntPtr.Zero) {
1787                                 Win32ScreenToClient(handle, ref pt);
1788                         }
1789
1790                         x=pt.x;
1791                         y=pt.y;
1792                 }
1793
1794                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
1795                 {
1796                         POINT pnt = new POINT();                        
1797
1798                         pnt.x = x;
1799                         pnt.y = y;
1800                         Win32ScreenToClient (handle, ref pnt);
1801
1802                         x = pnt.x;
1803                         y = pnt.y;
1804                 }
1805
1806                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
1807                         POINT   pnt = new POINT();                      
1808
1809                         pnt.x = x;
1810                         pnt.y = y;
1811
1812                         Win32ClientToScreen(handle, ref pnt);
1813
1814                         x = pnt.x;
1815                         y = pnt.y;
1816                 }
1817
1818                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
1819                         RECT    rect;
1820  
1821                         Win32GetWindowRect(handle, out rect);
1822                         x -= rect.left + SystemInformation.FrameBorderSize.Width;
1823                         y -= rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
1824                         return;
1825                 }
1826   
1827                 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {                      
1828                         RECT    rect;
1829  
1830                         Win32GetWindowRect(handle, out rect);
1831                         x += rect.left + SystemInformation.FrameBorderSize.Width;
1832                         y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
1833                         return;
1834                 }
1835   
1836                 internal override void SendAsyncMethod (AsyncMethodData method)
1837                 {
1838                         Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
1839                 }
1840
1841                 internal override void SetTimer (Timer timer)
1842                 {
1843                         int     index;
1844
1845                         index = timer.GetHashCode();
1846
1847                         lock (timer_list) {
1848                                 timer_list[index]=timer;
1849                         }
1850
1851                         Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
1852                 }
1853
1854                 internal override void KillTimer (Timer timer)
1855                 {
1856                         int     index;
1857
1858                         index = timer.GetHashCode();
1859
1860                         Win32KillTimer(FosterParent, index);
1861
1862                         lock (timer_list) {
1863                                 timer_list.Remove(index);
1864                         }
1865                 }
1866
1867
1868                 private void CaretCallback(object sender, EventArgs e) {
1869                         Console.WriteLine("CaretCallback hit");
1870                 }
1871
1872                 private void SetMdiStyles (CreateParams cp)
1873                 {
1874                         cp.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
1875                         cp.ExStyle = 0;
1876                 }
1877         
1878                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
1879                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
1880                         caret_visible = false;
1881                 }
1882
1883                 internal override void DestroyCaret(IntPtr hwnd) {
1884                         Win32DestroyCaret();
1885                 }
1886
1887                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
1888                         Win32SetCaretPos(x, y);
1889                 }
1890
1891                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
1892                         if (visible) {
1893                                 if (!caret_visible) {
1894                                         Win32ShowCaret(hwnd);
1895                                         caret_visible = true;
1896                                 }
1897                         } else {
1898                                 if (caret_visible) {
1899                                         Win32HideCaret(hwnd);
1900                                         caret_visible = false;
1901                                 }
1902                         }
1903                 }
1904
1905                 internal override IntPtr GetFocus() {
1906                         return Win32GetFocus();
1907                 }
1908
1909                 internal override void SetFocus(IntPtr hwnd) {
1910                         Win32SetFocus(hwnd);
1911                 }
1912
1913                 internal override IntPtr GetActive() {
1914                         return Win32GetActiveWindow();
1915                 }
1916
1917                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
1918                         IntPtr          dc;
1919                         TEXTMETRIC      tm;
1920
1921                         tm = new TEXTMETRIC();
1922
1923                         dc = Win32GetDC(IntPtr.Zero);
1924                         Win32SelectObject(dc, font.ToHfont());
1925                         if (Win32GetTextMetrics(dc, ref tm) == false) {
1926                                 Win32ReleaseDC(IntPtr.Zero, dc);
1927                                 ascent = 0;
1928                                 descent = 0;
1929                                 return false;
1930                         }
1931                         Win32ReleaseDC(IntPtr.Zero, dc);
1932
1933                         ascent = tm.tmAscent;
1934                         descent = tm.tmDescent;
1935
1936                         return true;
1937                 }
1938
1939                 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
1940                         RECT    rect;
1941
1942                         rect = new RECT();
1943                         rect.left = rectangle.X;
1944                         rect.top = rectangle.Y;
1945                         rect.right = rectangle.Right;
1946                         rect.bottom = rectangle.Bottom;
1947
1948                         Win32ScrollWindowEx(hwnd, XAmount, YAmount, IntPtr.Zero, ref rect, IntPtr.Zero, IntPtr.Zero, ScrollWindowExFlags.SW_INVALIDATE | ScrollWindowExFlags.SW_ERASE | (with_children ? ScrollWindowExFlags.SW_SCROLLCHILDREN : ScrollWindowExFlags.SW_NONE));
1949                         Win32UpdateWindow(hwnd);
1950                 }
1951
1952                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
1953                         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));
1954                 }
1955
1956                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
1957                         NOTIFYICONDATA  nid;
1958
1959                         nid = new NOTIFYICONDATA();
1960
1961                         nid.cbSize = (uint)Marshal.SizeOf(nid);
1962                         nid.hWnd = hwnd;
1963                         nid.uID = 1;
1964                         nid.uCallbackMessage = (uint)Msg.WM_USER;
1965                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
1966
1967                         if (tip != null) {       
1968                                 nid.szTip = tip;
1969                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
1970                         }
1971
1972                         if (icon != null) {
1973                                 nid.hIcon = icon.Handle;
1974                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
1975                         }
1976
1977                         tt = null;
1978
1979                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
1980                 }
1981
1982                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
1983                         NOTIFYICONDATA  nid;
1984
1985                         nid = new NOTIFYICONDATA();
1986
1987                         nid.cbSize = (uint)Marshal.SizeOf(nid);
1988                         nid.hIcon = icon.Handle;
1989                         nid.hWnd = hwnd;
1990                         nid.uID = 1;
1991                         nid.uCallbackMessage = (uint)Msg.WM_USER;
1992                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
1993
1994                         if (tip != null) {
1995                                 nid.szTip = tip;
1996                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
1997                         }
1998
1999                         if (icon != null) {
2000                                 nid.hIcon = icon.Handle;
2001                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2002                         }
2003
2004                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2005                 }
2006
2007                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2008                         NOTIFYICONDATA  nid;
2009
2010                         nid = new NOTIFYICONDATA();
2011
2012                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2013                         nid.hWnd = hwnd;
2014                         nid.uID = 1;
2015                         nid.uFlags = 0;
2016
2017                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2018                 }
2019
2020
2021                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2022                         uint    style;
2023                         uint    exstyle;
2024
2025                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
2026                         exstyle = Win32GetWindowLong(handle, WindowLong.GWL_EXSTYLE);
2027
2028
2029                         switch (border_style) {
2030                                 case FormBorderStyle.None: {
2031                                         style &= ~(uint)WindowStyles.WS_BORDER;
2032                                         exstyle &= ~(uint)WindowExStyles.WS_EX_CLIENTEDGE;
2033                                         break;
2034                                 }
2035
2036                                 case FormBorderStyle.FixedSingle: {
2037                                         style |= (uint)WindowStyles.WS_BORDER;
2038                                         exstyle &= ~(uint)WindowExStyles.WS_EX_CLIENTEDGE;
2039                                         break;
2040                                 }
2041
2042                                 case FormBorderStyle.Fixed3D: {
2043                                         style &= ~(uint)WindowStyles.WS_BORDER;
2044                                         exstyle |= (uint)WindowExStyles.WS_EX_CLIENTEDGE;
2045                                         break;
2046                                 }
2047                         }
2048
2049                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, style);
2050                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, exstyle);
2051                         
2052                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, 
2053                                 SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOZORDER);
2054                 }
2055
2056                 internal override void SetMenu(IntPtr handle, Menu menu) {
2057                         // Trigger WM_NCCALC
2058                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2059                 }
2060
2061
2062                 internal override Point GetMenuOrigin(IntPtr handle) {
2063                         return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2064                 }
2065
2066                 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2067                         Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon.Handle); // 1 = large icon (0 would be small)
2068                 }
2069
2070                 internal override void ClipboardClose(IntPtr handle) {
2071                         if (handle != clip_magic) {
2072                                 throw new ArgumentException("handle is not a valid clipboard handle");
2073                         }
2074                         Win32CloseClipboard();
2075                 }
2076
2077                 internal override int ClipboardGetID(IntPtr handle, string format) {
2078                         if (handle != clip_magic) {
2079                                 throw new ArgumentException("handle is not a valid clipboard handle");
2080                         }
2081                         if (format == "Text" ) return 1;
2082                         else if (format == "Bitmap" ) return 2;
2083                         else if (format == "MetaFilePict" ) return 3;
2084                         else if (format == "SymbolicLink" ) return 4;
2085                         else if (format == "DataInterchangeFormat" ) return 5;
2086                         else if (format == "Tiff" ) return 6;
2087                         else if (format == "OEMText" ) return 7;
2088                         else if (format == "DeviceIndependentBitmap" ) return 8;
2089                         else if (format == "Palette" ) return 9;
2090                         else if (format == "PenData" ) return 10;
2091                         else if (format == "RiffAudio" ) return 11;
2092                         else if (format == "WaveAudio" ) return 12;
2093                         else if (format == "UnicodeText" ) return 13;
2094                         else if (format == "EnhancedMetafile" ) return 14;
2095                         else if (format == "FileDrop" ) return 15;
2096                         else if (format == "Locale" ) return 16;
2097
2098                         return (int)Win32RegisterClipboardFormat(format);
2099                 }
2100
2101                 internal override IntPtr ClipboardOpen(bool primary_selection) {
2102                         // Win32 does not have primary selection
2103                         Win32OpenClipboard(FosterParent);
2104                         return clip_magic;
2105                 }
2106
2107                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2108                         uint    format;
2109                         int[]   result;
2110                         int     count;
2111
2112                         if (handle != clip_magic) {
2113                                 return null;
2114                         }
2115
2116                         // Count first
2117                         count = 0;
2118                         format = 0;
2119                         do {
2120                                 format = Win32EnumClipboardFormats(format);
2121                                 if (format != 0) {
2122                                         count++;
2123                                 }
2124                         } while (format != 0);
2125
2126                         // Now assign
2127                         result = new int[count];
2128                         count = 0;
2129                         format = 0;
2130                         do {
2131                                 format = Win32EnumClipboardFormats(format);
2132                                 if (format != 0) {
2133                                         result[count++] = (int)format;
2134                                 }
2135                         } while (format != 0);
2136
2137                         return result;
2138                 }
2139
2140
2141                 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2142                         IntPtr  hmem;
2143                         IntPtr  data;
2144                         object  obj;
2145
2146                         if (handle != clip_magic) {
2147                                 throw new ArgumentException("handle is not a valid clipboard handle");
2148                         }
2149
2150                         hmem = Win32GetClipboardData((uint)type);
2151                         if (hmem == IntPtr.Zero) {
2152                                 return null;
2153                         }
2154
2155                         data = Win32GlobalLock(hmem);
2156                         if (data == IntPtr.Zero) {
2157                                 uint error = Win32GetLastError();
2158                                 Console.WriteLine("Error: {0}", error);
2159                                 return null;
2160                         }
2161
2162                         obj = null;
2163
2164                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2165                                 obj = AnsiToString(data);
2166                         } else switch ((ClipboardFormats)type) {
2167                                 case ClipboardFormats.CF_TEXT: {
2168                                         obj = AnsiToString(data);
2169                                         break;
2170                                 }
2171
2172                                 case ClipboardFormats.CF_DIB: {
2173                                         obj = DIBtoImage(data);
2174                                         break;
2175                                 }
2176
2177                                 case ClipboardFormats.CF_UNICODETEXT: {
2178                                         obj = UnicodeToString(data);
2179                                         break;
2180                                 }
2181
2182                                 default: {
2183                                         if (converter != null && !converter(type, data, out obj)) {
2184                                                 obj = null;
2185                                         }
2186                                         break;
2187                                 }
2188                         }
2189                         Win32GlobalUnlock(hmem);
2190
2191                         return obj;
2192
2193                 }
2194
2195                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2196                         byte[]  data;
2197                         IntPtr  hmem;
2198                         IntPtr  hmem_ptr;
2199
2200                         if (handle != clip_magic) {
2201                                 throw new ArgumentException("handle is not a valid clipboard handle");
2202                         }
2203
2204                         if (obj == null) {
2205                                 // Just clear it
2206                                 Win32EmptyClipboard();
2207                                 return;
2208                         }
2209
2210                         if (type == -1) {
2211                                 if (obj is string) {
2212                                         type = (int)ClipboardFormats.CF_UNICODETEXT;
2213                                 } else if (obj is Image) {
2214                                         type = (int)ClipboardFormats.CF_DIB;
2215                                 }
2216                         }
2217
2218                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2219                                 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2220                                 Win32SetClipboardData((uint)type, hmem);
2221                                 return;
2222                         } else switch((ClipboardFormats)type) {
2223                                 case ClipboardFormats.CF_UNICODETEXT: {
2224                                         hmem = Marshal.StringToHGlobalUni((string)obj);
2225                                         Win32SetClipboardData((uint)type, hmem);
2226                                         return;
2227                                 }
2228
2229                                 case ClipboardFormats.CF_TEXT: {
2230                                         hmem = Marshal.StringToHGlobalAnsi((string)obj);
2231                                         Win32SetClipboardData((uint)type, hmem);
2232                                         return;
2233                                 }
2234
2235                                 case ClipboardFormats.CF_BITMAP:
2236                                 case ClipboardFormats.CF_DIB: {
2237                                         data = ImageToDIB((Image)obj);
2238
2239                                         hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2240                                         hmem_ptr = Win32GlobalLock(hmem);
2241                                         Marshal.Copy(data, 0, hmem_ptr, data.Length);
2242                                         Win32GlobalUnlock(hmem);
2243                                         Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2244                                         return;
2245                                 }
2246
2247                                 default: {
2248                                         if (converter != null && converter(ref type, obj, out data)) {
2249                                                 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2250                                                 hmem_ptr = Win32GlobalLock(hmem);
2251                                                 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2252                                                 Win32GlobalUnlock(hmem);
2253                                                 Win32SetClipboardData((uint)type, hmem);
2254                                         }
2255                                         return;
2256                                 }
2257                         }
2258                 }
2259
2260                 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2261                         if (allowed) {
2262                                 Win32DnD.RegisterDropTarget(hwnd);
2263                         } else {
2264                                 Win32DnD.UnregisterDropTarget(hwnd);
2265                         }
2266                 }
2267
2268                 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2269                         return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2270                 }
2271
2272                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2273                         IntPtr          hdc;
2274                         IntPtr          pen;
2275                         IntPtr          oldpen;
2276                         POINT           pt;
2277
2278                         pt = new POINT();
2279                         pt.x = 0;
2280                         pt.y = 0;
2281                         Win32ClientToScreen(handle, ref pt);
2282
2283                         // If we want the standard hatch pattern we would
2284                         // need to create a brush
2285
2286                         // Grab a pen
2287                         pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2288
2289                         hdc = Win32GetDC(IntPtr.Zero);
2290                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2291                         oldpen = Win32SelectObject(hdc, pen);
2292
2293                         Control c = Control.FromHandle (handle);
2294                         if (c != null) {
2295                                 Region r = new Region(new Rectangle(c.PointToScreen (c.Location), c.Size));
2296
2297                                 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
2298                         }
2299
2300                         Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2301                         if ((rect.Width > 0) && (rect.Height > 0)) {
2302                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2303                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2304                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2305                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2306                         } else {
2307                                 if (rect.Width > 0) {
2308                                         Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2309                                 } else {
2310                                         Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2311                                 }
2312                         }
2313
2314                         Win32SelectObject(hdc, oldpen);
2315                         Win32DeleteObject(pen);
2316                         if (c != null)
2317                                 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
2318
2319                         Win32ReleaseDC(IntPtr.Zero, hdc);
2320                 }
2321
2322                 internal override SizeF GetAutoScaleSize(Font font) {
2323                         Graphics        g;
2324                         float           width;
2325                         string          magic_string = "The quick brown fox jumped over the lazy dog.";
2326                         double          magic_number = 44.549996948242189;
2327
2328                         g = Graphics.FromHwnd(FosterParent);
2329
2330                         width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2331                         return new SizeF(width, font.Height);
2332                 }
2333
2334                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2335                         return Win32SendMessage(hwnd, message, wParam, lParam);
2336                 }
2337
2338                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2339                         return Win32PostMessage(hwnd, message, wParam, lParam);
2340                 }
2341
2342                 internal override int KeyboardSpeed {
2343                         get {
2344                                 Console.WriteLine ("KeyboardSpeed: need to query Windows");
2345
2346                                 //
2347                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2348                                 //
2349                                 return 0;
2350                         }
2351                 }
2352
2353                 internal override int KeyboardDelay {
2354                         get {
2355                                 Console.WriteLine ("KeyboardDelay: need to query Windows");
2356
2357                                 //
2358                                 // Return values must range from 0 to 4, 0 meaning 250ms,
2359                                 // and 4 meaning 1000 ms.
2360                                 //
2361                                 return 1;
2362                         }
2363                 }
2364                 
2365                 internal override event EventHandler Idle;
2366
2367                 // Santa's little helper
2368                 static void Where() {
2369                         Console.WriteLine("Here: {0}", new StackTrace().ToString());
2370                 }
2371                 #endregion      // Public Static Methods
2372
2373                 #region Win32 Imports
2374                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2375                 private extern static uint Win32GetLastError();
2376
2377                 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2378                 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);
2379
2380                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2381                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2382
2383                 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2384                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2385
2386                 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2387                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2388
2389                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2390                 internal extern static bool Win32TranslateMessage(ref MSG msg);
2391
2392                 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2393                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2394
2395                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2396                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2397
2398                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2399                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2400
2401                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2402                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2403
2404                 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2405                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2406
2407                 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2408                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2409
2410                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2411                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2412
2413                 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2414                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2415
2416                 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2417                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2418
2419                 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2420                 private extern static IntPtr Win32ShowCursor(bool bShow);
2421
2422                 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2423                 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2424
2425                 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2426                 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2427
2428                 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2429                 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2430
2431                 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2432                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2433
2434                 [DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2435                 private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2436
2437                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2438                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2439
2440                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2441                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2442
2443                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2444                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2445
2446                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2447                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2448
2449                 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2450                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2451
2452                 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2453                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2454
2455                 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2456                 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2457
2458                 [DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2459                 private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2460
2461                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2462                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2463
2464                 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2465                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
2466
2467                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2468                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
2469
2470                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2471                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
2472
2473                 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
2474                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
2475
2476                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
2477                 private extern static IntPtr Win32ReleaseCapture();
2478
2479                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
2480                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
2481
2482                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
2483                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
2484
2485                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
2486                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
2487
2488                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
2489                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
2490
2491                 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
2492                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
2493
2494                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2495                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
2496
2497                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
2498                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
2499
2500                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
2501                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
2502
2503                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
2504                 private extern static bool Win32SetCursorPos(int x, int y);
2505
2506                 [DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
2507                 private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
2508
2509                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
2510                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
2511
2512                 [DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
2513                 private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
2514
2515                 [DllImport ("user32.dll", EntryPoint="FillRect", CallingConvention=CallingConvention.StdCall)]
2516                 private extern static int Win32FillRect(IntPtr hdc, ref RECT rect, IntPtr hbr);
2517
2518                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
2519                 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
2520
2521                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
2522                 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
2523
2524                 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2525                 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
2526
2527                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
2528                 private extern static bool Win32DeleteObject(IntPtr o);
2529
2530                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
2531                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
2532
2533                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
2534                 private extern static IntPtr Win32GetDesktopWindow();
2535
2536                 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
2537                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
2538
2539                 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
2540                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
2541
2542                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
2543                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
2544
2545                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
2546                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
2547
2548                 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
2549                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
2550
2551                 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
2552                 internal extern static IntPtr Win32GetFocus();
2553
2554                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
2555                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
2556
2557                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
2558                 private  extern static bool Win32DestroyCaret();
2559
2560                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
2561                 private  extern static bool Win32ShowCaret(IntPtr hwnd);
2562
2563                 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
2564                 private  extern static bool Win32HideCaret(IntPtr hwnd);
2565
2566                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
2567                 private  extern static bool Win32SetCaretPos(int X, int Y);
2568
2569                 [DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
2570                 private  extern static uint Win32GetCaretBlinkTime();
2571
2572                 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2573                 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
2574
2575                 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
2576                 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
2577
2578                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2579                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2580
2581                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2582                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2583
2584                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2585                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2586
2587                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2588                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2589
2590                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2591                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2592
2593                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2594                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2595
2596                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2597                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2598
2599                 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2600                 private extern static IntPtr Win32GetActiveWindow();
2601
2602                 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
2603                 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
2604
2605                 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2606                 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
2607
2608                 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
2609                 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
2610
2611                 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
2612                 private extern static bool IsWindowEnabled(IntPtr hwnd);
2613
2614                 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
2615                 private extern static bool IsWindowVisible(IntPtr hwnd);
2616
2617                 [DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
2618                 private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
2619
2620                 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2621                 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
2622
2623                 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2624                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
2625
2626                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2627                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
2628
2629                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2630                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
2631
2632                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2633                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
2634
2635                 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
2636                 private extern static bool Win32OpenClipboard(IntPtr hwnd);
2637
2638                 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
2639                 private extern static bool Win32EmptyClipboard();
2640
2641                 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2642                 private extern static uint Win32RegisterClipboardFormat(string format);
2643
2644                 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
2645                 private extern static bool Win32CloseClipboard();
2646
2647                 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
2648                 private extern static uint Win32EnumClipboardFormats(uint format);
2649
2650                 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
2651                 private extern static IntPtr Win32GetClipboardData(uint format);
2652
2653                 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
2654                 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
2655
2656                 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
2657                 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
2658
2659                 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
2660                 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
2661
2662                 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
2663                 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
2664
2665                 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
2666                 internal extern static uint Win32GlobalSize(IntPtr hMem);
2667
2668                 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
2669                 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
2670
2671                 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
2672                 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
2673
2674                 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
2675                 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
2676
2677                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
2678                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
2679
2680                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
2681                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
2682
2683                 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
2684                 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
2685
2686                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
2687                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
2688
2689                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
2690                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
2691
2692                 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
2693                 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
2694
2695                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
2696                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
2697
2698                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
2699                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
2700
2701                 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
2702                 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top,  int right, int bottom);
2703
2704                 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
2705                 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
2706
2707                 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2708                 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
2709
2710                 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2711                 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
2712
2713                 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2714                 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
2715                 #endregion
2716         }
2717 }