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