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