Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Workflow.ComponentModel / Shared / NativeMethods.cs
1 // Copyright (c) Microsoft Corporation. All rights reserved. 
2 //  
3 // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
4 // WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
5 // WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
6 // THE ENTIRE RISK OF USE OR RESULTS IN CONNECTION WITH THE USE OF THIS CODE 
7 // AND INFORMATION REMAINS WITH THE USER. 
8
9
10 /*********************************************************************
11  * NOTE: A copy of this file exists at: WF\Activities\Common
12  * The two files must be kept in sync.  Any change made here must also
13  * be made to WF\Activities\Common\NativeMethods.cs
14 *********************************************************************/
15 namespace System.Workflow.Interop
16 {
17     using System;
18     using System.Runtime.InteropServices;
19     using System.Diagnostics.CodeAnalysis;
20
21     static class NativeMethods
22     {
23         internal const int HDI_WIDTH = 0x0001;
24         internal const int HDI_HEIGHT = HDI_WIDTH;
25         internal const int HDI_TEXT = 0x0002;
26         internal const int HDI_FORMAT = 0x0004;
27         internal const int HDI_LPARAM = 0x0008;
28         internal const int HDI_BITMAP = 0x0010;
29         internal const int HDI_IMAGE = 0x0020;
30         internal const int HDI_DI_SETITEM = 0x0040;
31         internal const int HDI_ORDER = 0x0080;
32         internal const int HDI_FILTER = 0x0100;
33         internal const int HDF_LEFT = 0x0000;
34         internal const int HDF_RIGHT = 0x0001;
35         internal const int HDF_CENTER = 0x0002;
36         internal const int HDF_JUSTIFYMASK = 0x0003;
37         internal const int HDF_RTLREADING = 0x0004;
38         internal const int HDF_OWNERDRAW = 0x8000;
39         internal const int HDF_STRING = 0x4000;
40         internal const int HDF_BITMAP = 0x2000;
41         internal const int HDF_BITMAP_ON_RIGHT = 0x1000;
42         internal const int HDF_IMAGE = 0x0800;
43         internal const int HDF_SORTUP = 0x0400;
44         internal const int HDF_SORTDOWN = 0x0200;
45         internal const int LVM_GETHEADER = (0x1000 + 31);
46         internal const int HDM_GETITEM = (0x1200 + 11);
47         internal const int HDM_SETITEM = (0x1200 + 12);
48
49         internal const int HORZRES = 8;
50         internal const int VERTRES = 10;
51         internal const int LOGPIXELSX = 88;
52         internal const int LOGPIXELSY = 90;
53         internal const int PHYSICALWIDTH = 110;
54         internal const int PHYSICALHEIGHT = 111;
55         internal const int PHYSICALOFFSETX = 112;
56         internal const int PHYSICALOFFSETY = 113;
57         internal const int WM_SETREDRAW = 0x000B;
58         internal const int HOLLOW_BRUSH = 5;
59         internal const int OBJ_PEN = 1;
60         internal const int OBJ_BRUSH = 2;
61         internal const int OBJ_EXTPEN = 11;
62         internal const int GM_ADVANCED = 2;
63         internal const int PS_COSMETIC = 0x00000000;
64         internal const int PS_USERSTYLE = 7;
65         internal const int BS_SOLID = 0;
66         internal const int WS_POPUP = unchecked((int)0x80000000);
67         internal const int WS_EX_DLGMODALFRAME = 0x00000001;
68         internal const int WM_SETICON = 0x0080;
69         internal const int SMALL_ICON = 0;
70         internal const int LARGE_ICON = 1;
71         internal const int PS_SOLID = 0;
72         internal const int SWP_NOSIZE = unchecked((int)0x0001);
73         internal const int SWP_NOZORDER = unchecked((int)0x0004);
74         internal const int SWP_NOACTIVATE = unchecked((int)0x0010);
75         internal const int WM_NOTIFY = unchecked((int)0x004E);
76         internal const int WM_SETFONT = unchecked((int)0x0030);
77         internal const int WS_EX_TOPMOST = unchecked((int)0x00000008L);
78         internal const int WM_KEYDOWN = 0x100;
79         internal const int WM_KEYUP = 0x101;
80         internal const int WM_SYSKEYDOWN = 0x104;
81         internal const int WM_SYSKEYUP = 0x105;
82
83         internal const int TTF_IDISHWND = (0x0001);
84         internal const int TTF_CENTERTIP = (0x0002);
85         internal const int TTF_RTLREADING = (0x0004);
86         internal const int TTF_SUBCLASS = (0x0010);
87         internal const int TTF_TRACK = (0x0020);
88         internal const int TTF_ABSOLUTE = (0x0080);
89         internal const int TTF_TRANSPARENT = (0x0100);
90         internal const int TTF_PARSELINKS = (0x1000);
91         internal const int TTF_DI_SETITEM = (0x8000);
92
93         internal const int TTS_ALWAYSTIP = (0x01);
94         internal const int TTS_NOPREFIX = (0x02);
95         internal const int TTS_NOANIMATE = (0x10);
96         internal const int TTS_NOFADE = (0x20);
97         internal const int TTS_BALLOON = (0x40);
98         internal const int TTS_CLOSE = (0x80);
99
100         internal const int TTDT_AUTOMATIC = 0;
101         internal const int TTDT_RESHOW = 1;
102         internal const int TTDT_AUTOPOP = 2;
103         internal const int TTDT_INITIAL = 3;
104
105         internal const int TTI_NONE = 0;
106         internal const int TTI_INFO = 1;
107         internal const int TTI_WARNING = 2;
108         internal const int TTI_ERROR = 3;
109
110         internal static readonly int TTN_GETDISPINFO;
111         internal static readonly int TTN_NEEDTEXT;
112         internal static readonly int TTN_SHOW = ((0 - 520) - 1);
113         internal static readonly int TTN_POP = ((0 - 520) - 2);
114
115         internal static readonly int TTM_POP = (0x0400 + 28);
116         internal static readonly int TTM_ADDTOOL;
117         internal static readonly int TTM_SETTITLE;
118         internal static readonly int TTM_DELTOOL;
119         internal static readonly int TTM_NEWTOOLRECT;
120         internal static readonly int TTM_GETTOOLINFO;
121         internal static readonly int TTM_SETTOOLINFO;
122         internal static readonly int TTM_HITTEST;
123         internal static readonly int TTM_GETTEXT;
124         internal static readonly int TTM_UPDATETIPTEXT;
125         internal static readonly int TTM_ENUMTOOLS;
126         internal static readonly int TTM_GETCURRENTTOOL;
127         internal static readonly int TTM_TRACKACTIVATE = (0x0400 + 17);
128         internal static readonly int TTM_TRACKPOSITION = (0x0400 + 18);
129         internal static readonly int TTM_ACTIVATE = (0x0400 + 1);
130         internal static readonly int TTM_ADJUSTRECT = (0x400 + 31);
131         internal static readonly int TTM_SETDELAYTIME = (0x0400 + 3);
132         internal static readonly int TTM_RELAYEVENT = (0x0400 + 7);
133         internal static readonly int TTM_UPDATE = (0x0400 + 29);
134         internal static readonly int TTM_WINDOWFROMPOINT = (0x0400 + 16);
135         internal static readonly int TTM_GETDELAYTIME = (0x0400 + 21);
136         internal static readonly int TTM_SETMAXTIPWIDTH = (0x0400 + 24);
137
138         private const int TTN_GETDISPINFOA = ((0 - 520) - 0);
139         private const int TTN_GETDISPINFOW = ((0 - 520) - 10);
140         private const int TTN_NEEDTEXTA = ((0 - 520) - 0);
141         private const int TTN_NEEDTEXTW = ((0 - 520) - 10);
142
143         private const int TTM_SETTITLEA = (0x0400 + 32);
144         private const int TTM_SETTITLEW = (0x0400 + 33);
145         private const int TTM_ADDTOOLA = (0x0400 + 4);
146         private const int TTM_ADDTOOLW = (0x0400 + 50);
147         private const int TTM_DELTOOLA = (0x0400 + 5);
148         private const int TTM_DELTOOLW = (0x0400 + 51);
149         private const int TTM_NEWTOOLRECTA = (0x0400 + 6);
150         private const int TTM_NEWTOOLRECTW = (0x0400 + 52);
151         private const int TTM_GETTOOLINFOA = (0x0400 + 8);
152         private const int TTM_GETTOOLINFOW = (0x0400 + 53);
153         private const int TTM_SETTOOLINFOA = (0x0400 + 9);
154         private const int TTM_SETTOOLINFOW = (0x0400 + 54);
155         private const int TTM_HITTESTA = (0x0400 + 10);
156         private const int TTM_HITTESTW = (0x0400 + 55);
157         private const int TTM_GETTEXTA = (0x0400 + 11);
158         private const int TTM_GETTEXTW = (0x0400 + 56);
159         private const int TTM_UPDATETIPTEXTA = (0x0400 + 12);
160         private const int TTM_UPDATETIPTEXTW = (0x0400 + 57);
161         private const int TTM_ENUMTOOLSA = (0x0400 + 14);
162         private const int TTM_ENUMTOOLSW = (0x0400 + 58);
163         private const int TTM_GETCURRENTTOOLA = (0x0400 + 15);
164         private const int TTM_GETCURRENTTOOLW = (0x0400 + 59);
165
166         static NativeMethods()
167         {
168             if (Marshal.SystemDefaultCharSize == 1)
169             {
170                 TTN_GETDISPINFO = TTN_GETDISPINFOA;
171                 TTN_NEEDTEXT = TTN_NEEDTEXTA;
172
173                 TTM_ADDTOOL = TTM_ADDTOOLA;
174                 TTM_SETTITLE = TTM_SETTITLEA;
175                 TTM_DELTOOL = TTM_DELTOOLA;
176                 TTM_NEWTOOLRECT = TTM_NEWTOOLRECTA;
177                 TTM_GETTOOLINFO = TTM_GETTOOLINFOA;
178                 TTM_SETTOOLINFO = TTM_SETTOOLINFOA;
179                 TTM_HITTEST = TTM_HITTESTA;
180                 TTM_GETTEXT = TTM_GETTEXTA;
181                 TTM_UPDATETIPTEXT = TTM_UPDATETIPTEXTA;
182                 TTM_ENUMTOOLS = TTM_ENUMTOOLSA;
183                 TTM_GETCURRENTTOOL = TTM_GETCURRENTTOOLA;
184             }
185             else
186             {
187                 TTN_GETDISPINFO = TTN_GETDISPINFOW;
188                 TTN_NEEDTEXT = TTN_NEEDTEXTW;
189
190                 TTM_ADDTOOL = TTM_ADDTOOLW;
191                 TTM_SETTITLE = TTM_SETTITLEW;
192                 TTM_DELTOOL = TTM_DELTOOLW;
193                 TTM_NEWTOOLRECT = TTM_NEWTOOLRECTW;
194                 TTM_GETTOOLINFO = TTM_GETTOOLINFOW;
195                 TTM_SETTOOLINFO = TTM_SETTOOLINFOW;
196                 TTM_HITTEST = TTM_HITTESTW;
197                 TTM_GETTEXT = TTM_GETTEXTW;
198                 TTM_UPDATETIPTEXT = TTM_UPDATETIPTEXTW;
199                 TTM_ENUMTOOLS = TTM_ENUMTOOLSW;
200                 TTM_GETCURRENTTOOL = TTM_GETCURRENTTOOLW;
201             }
202         }
203
204         internal static bool Failed(int hr)
205         {
206             return (hr < 0);
207         }
208
209         internal static int ThrowOnFailure(int hr)
210         {
211             return ThrowOnFailure(hr, null);
212         }
213
214         internal static int ThrowOnFailure(int hr, params int[] expectedHRFailure)
215         {
216             if (Failed(hr))
217             {
218                 if ((null == expectedHRFailure) || (Array.IndexOf(expectedHRFailure, hr) < 0))
219                 {
220                     Marshal.ThrowExceptionForHR(hr);
221                 }
222             }
223
224             return hr;
225         }
226
227         internal static IntPtr ListView_GetHeader(IntPtr hWndLV)
228         {
229             return SendMessage(hWndLV, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
230         }
231
232         internal static bool Header_GetItem(IntPtr hWndHeader, int index, [In, Out] NativeMethods.HDITEM hdi)
233         {
234             IntPtr success = SendMessage(hWndHeader, HDM_GETITEM, new IntPtr(index), hdi);
235             return (success != IntPtr.Zero) ? true : false;
236         }
237
238         internal static bool Header_SetItem(IntPtr hWndHeader, int index, [In, Out] NativeMethods.HDITEM hdi)
239         {
240             IntPtr success = SendMessage(hWndHeader, HDM_SETITEM, new IntPtr(index), hdi);
241             return (success != IntPtr.Zero) ? true : false;
242         }
243
244         //[DllImport("gdi32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
245         //public static extern IntPtr CreateSolidBrush(int crColor);
246
247         //[DllImport("gdi32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
248         //internal static extern bool RoundRect(HandleRef hDC, int left, int top, int right, int bottom, int width, int height);
249
250         //[DllImport("gdi32.dll", ExactSpelling = true, EntryPoint = "CreatePen", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
251         //internal static extern IntPtr CreatePen(int nStyle, int nWidth, int crColor);
252
253         [DllImport("gdi32", EntryPoint = "DeleteObject", CharSet = CharSet.Auto)]
254         internal static extern bool DeleteObject(IntPtr hObject);
255
256         [System.Runtime.InteropServices.DllImport("gdi32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
257         public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);
258
259         [DllImport("user32", CharSet = CharSet.Auto)]
260         public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, [In, Out] NativeMethods.HDITEM lParam);
261
262         [DllImport("user32.dll", CharSet = CharSet.Auto)]
263         public extern static bool IsWindowVisible(IntPtr hWnd);
264
265         [DllImport("user32.dll", CharSet = CharSet.Auto)]
266         public extern static IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
267
268         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
269         public static extern bool LineTo(HandleRef hdc, int x, int y);
270
271         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
272         public static extern bool MoveToEx(HandleRef hdc, int x, int y, POINT pt);
273
274         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
275         public static extern IntPtr SelectObject(HandleRef hdc, HandleRef obj);
276
277         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
278         public static extern IntPtr GetCurrentObject(HandleRef hDC, uint uObjectType);
279
280         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
281         public static extern int DeleteObject(HandleRef hObject);
282
283         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
284         public static extern IntPtr ExtCreatePen(int style, int nWidth, LOGBRUSH logbrush, int styleArrayLength, int[] styleArray);
285
286         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
287         public static extern int SetWorldTransform(HandleRef hdc, XFORM xform);
288
289         [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
290         public static extern int SetGraphicsMode(HandleRef hdc, int iMode);
291
292         [DllImport("user32.dll")]
293         internal static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TOOLINFO ti);
294
295         [DllImport("user32.dll")]
296         internal static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref RECT rc);
297
298         [DllImport("user32.dll")]
299         internal static extern int SetWindowPos(IntPtr hWnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, int flags);
300
301         [System.Runtime.InteropServices.ComVisible(false), StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
302         internal class HDITEM
303         {
304             public int mask = 0;
305             public int cxy = 0;
306             public IntPtr pszText = IntPtr.Zero;
307             public IntPtr hbm = IntPtr.Zero;
308             public int cchTextMax = 0;
309             public int fmt = 0;
310             public int lParam = 0;
311             public int image = 0;
312             public int order = 0;
313             public int type = 0;
314             public IntPtr filter = IntPtr.Zero;
315         }
316
317         [StructLayout(LayoutKind.Sequential)]
318         internal class XFORM
319         {
320             //Default is identity matrix
321             public float eM11 = 1.0f;
322             public float eM12 = 0.0f;
323             public float eM21 = 0.0f;
324             public float eM22 = 1.0f;
325             public float eDx = 0.0f;
326             public float eDy = 0.0f;
327
328             public XFORM()
329             {
330
331             }
332
333             public XFORM(System.Drawing.Drawing2D.Matrix transform)
334             {
335                 this.eM11 = transform.Elements[0];
336                 this.eM12 = transform.Elements[1];
337                 this.eM21 = transform.Elements[2];
338                 this.eM22 = transform.Elements[3];
339                 this.eDx = transform.Elements[4];
340                 this.eDy = transform.Elements[5];
341             }
342         }
343
344         [StructLayout(LayoutKind.Sequential)]
345         internal class LOGBRUSH
346         {
347             public int lbStyle;
348             public int lbColor;
349             public long lbHatch;
350
351             public LOGBRUSH(int style, int color, int hatch)
352             {
353                 this.lbStyle = style;
354                 this.lbColor = color;
355                 this.lbHatch = hatch;
356             }
357         }
358
359         [StructLayout(LayoutKind.Sequential)]
360         internal class POINT
361         {
362             public int x;
363             public int y;
364         }
365
366         [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
367         internal class NMHDR
368         {
369             public IntPtr hwndFrom;
370             public int idFrom;
371             public int code;
372
373             public NMHDR()
374             {
375                 this.hwndFrom = IntPtr.Zero;
376                 this.idFrom = 0;
377                 this.code = 0;
378             }
379         }
380
381         [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
382         internal struct RECT
383         {
384             public int left;
385             public int top;
386             public int right;
387             public int bottom;
388         }
389
390         [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
391         internal struct TOOLINFO
392         {
393             public int size;
394             public int flags;
395             public IntPtr hwnd;
396             public IntPtr id;
397             public RECT rect;
398             public IntPtr hinst;
399             [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources", Justification = "Not a security threat since its used by designer scenarios only")]
400             public IntPtr text;
401             public IntPtr lParam;
402         }
403     }
404 }