~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/awt/native.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2007, 2008, 2010 Jeroen Frijters
 
3
  Copyright (C) 2009 - 2012 Volker Berlin (i-net software)
 
4
  Copyright (C) 2010 Karsten Heinrich (i-net software)
 
5
 
 
6
  This software is provided 'as-is', without any express or implied
 
7
  warranty.  In no event will the authors be held liable for any damages
 
8
  arising from the use of this software.
 
9
 
 
10
  Permission is granted to anyone to use this software for any purpose,
 
11
  including commercial applications, and to alter it and redistribute it
 
12
  freely, subject to the following restrictions:
 
13
 
 
14
  1. The origin of this software must not be misrepresented; you must not
 
15
     claim that you wrote the original software. If you use this software
 
16
     in a product, an acknowledgment in the product documentation would be
 
17
     appreciated but is not required.
 
18
  2. Altered source versions must be plainly marked as such, and must not be
 
19
     misrepresented as being the original software.
 
20
  3. This notice may not be removed or altered from any source distribution.
 
21
 
 
22
  Jeroen Frijters
 
23
  jeroen@frijters.net
 
24
  
 
25
*/
 
26
using System;
 
27
using System.Runtime.InteropServices;
 
28
using System.Text;
 
29
using System.Drawing;
 
30
using System.Drawing.Imaging;
 
31
using awt;
 
32
 
 
33
namespace IKVM.NativeCode.sun.awt
 
34
{
 
35
        static class KeyboardFocusManagerPeerImpl
 
36
        {
 
37
                public static object getNativeFocusedWindow() { return null; }
 
38
                public static object getNativeFocusOwner() { return null; }
 
39
                public static void clearNativeGlobalFocusOwner(object activeWindow) { }
 
40
        }
 
41
 
 
42
        static class SunToolkit
 
43
        {
 
44
                public static void closeSplashScreen() { }
 
45
        }
 
46
}
 
47
 
 
48
namespace IKVM.NativeCode.sun.awt.shell
 
49
{
 
50
        /// <summary>
 
51
        /// This class should use only on Windows that we can access shell32.dll
 
52
        /// </summary>
 
53
        public static class Win32ShellFolder2
 
54
        {
 
55
                private const uint IMAGE_BITMAP = 0;
 
56
                private const uint IMAGE_ICON = 1;
 
57
 
 
58
        private const int HINST_COMMCTRL = -1;
 
59
        private const int IDB_VIEW_SMALL_COLOR = 4;
 
60
 
 
61
        private const uint WM_USER = 0x0400;
 
62
        private const uint TB_GETIMAGELIST = WM_USER + 49;
 
63
        private const uint TB_LOADIMAGES = WM_USER + 50;
 
64
 
 
65
        private const uint ILD_TRANSPARENT = 0x00000001;
 
66
 
 
67
                private static readonly IntPtr hmodShell32;
 
68
                private static readonly bool isXP;
 
69
 
 
70
                [System.Security.SecuritySafeCritical]
 
71
                static Win32ShellFolder2()
 
72
                {
 
73
                        hmodShell32 = LoadLibrary("shell32.dll");
 
74
                        isXP = Environment.OSVersion.Version >= new Version(5, 1);
 
75
                }
 
76
 
 
77
                [System.Security.SecurityCritical]
 
78
                private sealed class SafeGdiObjectHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
 
79
                {
 
80
                        private SafeGdiObjectHandle()
 
81
                                : base(true)
 
82
                        {
 
83
                        }
 
84
 
 
85
                        [System.Security.SecurityCritical]
 
86
                        protected override bool ReleaseHandle()
 
87
                        {
 
88
                                return DeleteObject(handle);
 
89
                        }
 
90
                }
 
91
 
 
92
                [System.Security.SecurityCritical]
 
93
                private sealed class SafeDeviceContextHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
 
94
                {
 
95
                        [DllImport("user32.dll")]
 
96
                        private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
 
97
 
 
98
                        [DllImport("user32.dll")]
 
99
                        private static extern SafeDeviceContextHandle GetDC(IntPtr hwnd);
 
100
 
 
101
                        internal static SafeDeviceContextHandle Get()
 
102
                        {
 
103
                                return GetDC(IntPtr.Zero);
 
104
                        }
 
105
 
 
106
                        private SafeDeviceContextHandle()
 
107
                                : base(true)
 
108
                        {
 
109
                        }
 
110
 
 
111
                        [System.Security.SecurityCritical]
 
112
                        protected override bool ReleaseHandle()
 
113
                        {
 
114
                                return ReleaseDC(IntPtr.Zero, handle) == 1;
 
115
                        }
 
116
                }
 
117
 
 
118
                [DllImport("gdi32.dll")]
 
119
                private static extern int GetDIBits(SafeDeviceContextHandle hdc, IntPtr hbmp, uint uStartScan, uint cScanLines, int[] lpvBits, ref BITMAPINFO lpbmi, uint uUsage);
 
120
 
 
121
                [DllImport("gdi32.dll")]
 
122
                private static extern int GetDIBits(SafeDeviceContextHandle hdc, SafeGdiObjectHandle hbmp, uint uStartScan, uint cScanLines, int[] lpvBits, ref BITMAPINFO lpbmi, uint uUsage);
 
123
 
 
124
                [DllImport("gdi32.dll")]
 
125
                private static extern int GetObject(SafeGdiObjectHandle hgdiobj, int cbBuffer, ref BITMAPINFO lpvObject);
 
126
 
 
127
                [DllImport("shlwapi.dll")]
 
128
        private static extern int StrRetToBuf(ref ShellApi.STRRET pstr, IntPtr pIDL, StringBuilder pszBuf, uint cchBuf);
 
129
 
 
130
                [StructLayout(LayoutKind.Sequential)]
 
131
                private struct ICONINFO
 
132
                {
 
133
                        internal bool fIcon;
 
134
                        internal int xHotspot;
 
135
                        internal int yHotspot;
 
136
                        internal IntPtr hbmMask;
 
137
                        internal IntPtr hbmColor;
 
138
                }
 
139
 
 
140
                [DllImport("user32.dll")]
 
141
                static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
 
142
 
 
143
        [DllImport("user32.dll", SetLastError = true)]
 
144
        static extern IntPtr CreateWindowEx(
 
145
           uint dwExStyle,
 
146
           string lpClassName,
 
147
           string lpWindowName,
 
148
           uint dwStyle,
 
149
           int x,
 
150
           int y,
 
151
           int nWidth,
 
152
           int nHeight,
 
153
           IntPtr hWndParent,
 
154
           IntPtr hMenu,
 
155
           IntPtr hInstance,
 
156
           IntPtr lpParam);
 
157
 
 
158
        [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 
159
        [return: MarshalAs(UnmanagedType.Bool)]
 
160
        static extern bool DestroyWindow(IntPtr hwnd);
 
161
 
 
162
        [DllImport("user32.dll")]
 
163
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
 
164
 
 
165
        [DllImport("comctl32.dll", SetLastError = true)]
 
166
        public static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);
 
167
 
 
168
        [DllImport("comctl32")]
 
169
        [return: MarshalAs(UnmanagedType.Bool)]
 
170
        static extern bool ImageList_Destroy(IntPtr himl);
 
171
 
 
172
        [DllImport("user32")]
 
173
        public static extern int DestroyIcon(IntPtr hIcon);
 
174
 
 
175
        [StructLayout(LayoutKind.Sequential)]
 
176
                private struct BITMAPINFO
 
177
                {
 
178
                        internal uint biSize;
 
179
                        internal int biWidth;
 
180
                        internal int biHeight;
 
181
                        internal ushort biPlanes;
 
182
                        internal ushort biBitCount;
 
183
                        internal uint biCompression;
 
184
                        internal uint biSizeImage;
 
185
                        internal int biXPelsPerMeter;
 
186
                        internal int biYPelsPerMeter;
 
187
                        internal uint biClrUsed;
 
188
                        internal uint biClrImportant;
 
189
                        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
 
190
                        internal uint[] cols;
 
191
                }
 
192
 
 
193
                [DllImport("user32.dll")]
 
194
                private static extern SafeGdiObjectHandle LoadImage(IntPtr hInstance, IntPtr uID, uint type, int width, int height, int load);
 
195
 
 
196
                [DllImport("user32.dll")]
 
197
                private static extern SafeGdiObjectHandle LoadImage(IntPtr hInstance, string lpszName, uint type, int width, int height, int load);
 
198
 
 
199
                [DllImport("kernel32.dll")]
 
200
                private static extern IntPtr LoadLibrary(string Library);
 
201
 
 
202
                [DllImport("gdi32.dll")]
 
203
                private static extern bool DeleteObject(IntPtr hDc);
 
204
 
 
205
                /// <summary>
 
206
                /// Get the program to execute or open the file. If it is a exe then it is self
 
207
                /// </summary>
 
208
                /// <param name="path">path to the file</param>
 
209
                /// <returns></returns>
 
210
                [System.Security.SecuritySafeCritical]
 
211
                public static string getExecutableType(string path)
 
212
                {
 
213
                        StringBuilder objResultBuffer = new StringBuilder(1024);
 
214
                        int result = ShellApi.FindExecutable(path, path, objResultBuffer);
 
215
                        if (result >= 32)
 
216
                        {
 
217
                                return objResultBuffer.ToString();
 
218
                        }
 
219
                        return null;
 
220
                }
 
221
 
 
222
                /// <summary>
 
223
                /// Get the type of a file or folder. On a file it depends on its extension.
 
224
                /// </summary>
 
225
                /// <param name="path">the path of the file or folder</param>
 
226
                /// <returns>The type in readable form or null, if the path cannot be resolved</returns>
 
227
                [System.Security.SecuritySafeCritical]
 
228
                public static string getFolderType(string path)
 
229
                {
 
230
            ShellApi.SHFILEINFO shinfo = new ShellApi.SHFILEINFO();
 
231
                        if (ShellApi.SHGetFileInfo(path, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), ShellApi.SHGFI.SHGFI_TYPENAME) == IntPtr.Zero)
 
232
                        {
 
233
                                return null;
 
234
                        }
 
235
                        return shinfo.szTypeName;
 
236
                }
 
237
 
 
238
                [System.Security.SecurityCritical]
 
239
        public static Bitmap getIconBits(IntPtr hIcon, int iconSize)
 
240
                {
 
241
                        ICONINFO iconInfo;
 
242
                        if (GetIconInfo(hIcon, out iconInfo))
 
243
                        {
 
244
                                using (SafeDeviceContextHandle dc = SafeDeviceContextHandle.Get())
 
245
                                {
 
246
                                        BITMAPINFO bmi = new BITMAPINFO();
 
247
                                        bmi.biSize = 40;
 
248
                                        bmi.biWidth = iconSize;
 
249
                                        bmi.biHeight = -iconSize;
 
250
                                        bmi.biPlanes = 1;
 
251
                                        bmi.biBitCount = 32;
 
252
                                        bmi.biCompression = 0;
 
253
                                        int intArrSize = iconSize * iconSize;
 
254
                                        int[] iconBits = new int[intArrSize];
 
255
                                        GetDIBits(dc, iconInfo.hbmColor, 0, (uint)iconSize, iconBits, ref bmi, 0);
 
256
                                        bool hasAlpha = false;
 
257
                                        if (isXP)
 
258
                                        {
 
259
                                                for (int i = 0; i < iconBits.Length; i++)
 
260
                                                {
 
261
                                                        if ((iconBits[i] & 0xFF000000) != 0)
 
262
                                                        {
 
263
                                                                hasAlpha = true;
 
264
                                                                break;
 
265
                                                        }
 
266
                                                }
 
267
                                        }
 
268
                                        if (!hasAlpha)
 
269
                                        {
 
270
                                                int[] maskBits = new int[intArrSize];
 
271
                                                GetDIBits(dc, iconInfo.hbmMask, 0, (uint)iconSize, maskBits, ref bmi, 0);
 
272
                                                for (int i = 0; i < iconBits.Length; i++)
 
273
                                                {
 
274
                                                        if (maskBits[i] == 0)
 
275
                                                        {
 
276
                                                                iconBits[i] = (int)((uint)iconBits[i] | 0xFF000000);
 
277
                                                        }
 
278
                                                }
 
279
                                        }
 
280
                                        DeleteObject(iconInfo.hbmColor);
 
281
                                        DeleteObject(iconInfo.hbmMask);
 
282
 
 
283
                    DeleteObject(hIcon);
 
284
                    Bitmap bitmap = new Bitmap(iconSize, iconSize, PixelFormat.Format32bppArgb);
 
285
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, iconSize, iconSize), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
 
286
                    Marshal.Copy(iconBits, 0, bitmapData.Scan0, iconBits.Length);
 
287
                    bitmap.UnlockBits(bitmapData);
 
288
                    return bitmap;
 
289
                                }
 
290
                        }
 
291
                        return null;
 
292
                }
 
293
 
 
294
        /// <summary>
 
295
        /// Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.
 
296
        /// </summary>
 
297
        /// <param name="path">The path of the file system object</param>
 
298
        /// <returns>The SHGFI flags with the attributes</returns>
 
299
                [System.Security.SecuritySafeCritical]
 
300
                public static int getAttribute(string path)
 
301
                {
 
302
            ShellApi.SHFILEINFO shinfo = new ShellApi.SHFILEINFO();
 
303
            if (ShellApi.SHGetFileInfo(path, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), ShellApi.SHGFI.SHGFI_ATTRIBUTES) == IntPtr.Zero)
 
304
                        {
 
305
                                return 0;
 
306
                        }
 
307
                        return (int)shinfo.dwAttributes;
 
308
                }
 
309
 
 
310
        /// <summary>Returns the link target as a pIDL relative to the desktop without resolving the link</summary>
 
311
        /// <param name="path">The path of the .lnk file</param>
 
312
        /// <returns>the link target as a pIDL relative to the desktop</returns>
 
313
                [System.Security.SecuritySafeCritical]
 
314
                public static string getLinkLocation(string path )
 
315
                {
 
316
                        using (ShellLink link = new ShellLink())
 
317
                        {
 
318
                                link.Load(path);
 
319
                                return link.GetPath();
 
320
                        }
 
321
                }
 
322
        /// <summary>Returns the link target as a pIDL relative to the desktop</summary>
 
323
        /// <param name="path">The path of the .lnk file</param>
 
324
        /// <param name="resolve">If true, attempts to find the target of a Shell link, 
 
325
        /// even if it has been moved or renamed. This may open a file chooser</param>
 
326
        /// <returns>the link target as a pIDL relative to the desktop</returns>
 
327
        [System.Security.SecuritySafeCritical]
 
328
        public static IntPtr getLinkLocation(string path, Boolean resolve)
 
329
        {
 
330
            using (ShellLink link = new ShellLink())
 
331
            {
 
332
                link.Load(path);
 
333
                if (resolve)
 
334
                {
 
335
                    link.Resolve();
 
336
                }                
 
337
                return link.GetIDList();
 
338
            }
 
339
        }
 
340
 
 
341
        // Code copied from Java_sun_awt_shell_Win32ShellFolder2_getStandardViewButton0
 
342
        [System.Security.SecuritySafeCritical]
 
343
        public static Bitmap getStandardViewButton0(int iconIndex)
 
344
        {
 
345
            Bitmap result = null;
 
346
            using (new ThemingActivationContext())
 
347
            {
 
348
                // Create a toolbar
 
349
                IntPtr hWndToolbar = CreateWindowEx(0, "ToolbarWindow32", null, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
 
350
                if (hWndToolbar != IntPtr.Zero)
 
351
                {
 
352
                    SendMessage(hWndToolbar, TB_LOADIMAGES, IDB_VIEW_SMALL_COLOR, HINST_COMMCTRL);
 
353
 
 
354
                    IntPtr hImageList = SendMessage(hWndToolbar, TB_GETIMAGELIST, 0, 0);
 
355
                    if (hImageList != IntPtr.Zero)
 
356
                    {
 
357
                        IntPtr hIcon = ImageList_GetIcon(hImageList, iconIndex, ILD_TRANSPARENT);
 
358
                        if (hIcon != IntPtr.Zero)
 
359
                        {
 
360
                            Icon icon = Icon.FromHandle(hIcon);
 
361
                            result = icon.ToBitmap();
 
362
                            icon.Dispose();
 
363
                            DestroyIcon(hIcon);
 
364
                        }
 
365
                        ImageList_Destroy(hImageList);
 
366
                    }
 
367
                    DestroyWindow(hWndToolbar);
 
368
                }
 
369
            }
 
370
            return result;
 
371
        }
 
372
 
 
373
        /// <summary>
 
374
        /// Retrieves an icon from the shell32.dell
 
375
        /// </summary>
 
376
        /// <param name="iconID">the index of the icon</param>
 
377
        /// <returns>The icon or null, if there is no icon at the given index</returns>
 
378
                [System.Security.SecuritySafeCritical]
 
379
        public static Bitmap getShell32IconResourceAsBitmap(int iconID, bool getLargeIcon)
 
380
                {
 
381
                        if (hmodShell32 == IntPtr.Zero)
 
382
                        {
 
383
                                return null;
 
384
                        }
 
385
            int size = getLargeIcon ? 32 : 16;
 
386
            using (SafeGdiObjectHandle hicon = LoadImage(hmodShell32, (IntPtr)iconID, IMAGE_ICON, size, size, 0))
 
387
                        {
 
388
                                if (hicon != null)
 
389
                                {
 
390
                    return getIconBits(hicon.DangerousGetHandle(), 16);
 
391
                                }
 
392
                        }
 
393
                        return null;
 
394
                }
 
395
 
 
396
        /// <summary>
 
397
        /// Returns the pIDL of the desktop itself
 
398
        /// </summary>
 
399
        [System.Security.SecurityCritical]
 
400
        public static IntPtr initDesktopPIDL()
 
401
        {
 
402
            IntPtr pidl = new IntPtr();
 
403
 
 
404
            // get the root shell folder
 
405
            ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, ShellApi.CSIDL.CSIDL_DESKTOP, ref pidl);
 
406
 
 
407
            return pidl;
 
408
        }
 
409
 
 
410
        /// <summary>
 
411
        /// Returns an IShellFolder for the desktop
 
412
        /// </summary>
 
413
        [System.Security.SecurityCritical]
 
414
        public static Object initDesktopFolder()
 
415
        {
 
416
            ShellApi.IShellFolder rootShell = null;
 
417
 
 
418
            // get the root shell folder
 
419
            ShellApi.SHGetDesktopFolder(ref rootShell);
 
420
 
 
421
            return rootShell;
 
422
        }
 
423
 
 
424
        /// <summary>
 
425
        /// Returns the desktop relative pIDL of a special folder
 
426
        /// </summary>
 
427
        /// <param name="desktopIShellFolder">The IShellFolder instance of the Desktop</param>
 
428
        /// <param name="csidl">The CSIDL of the special folder</param>
 
429
        /// <returns>the desktop relative pIDL of a special folder</returns>
 
430
        [System.Security.SecurityCritical]
 
431
        public static IntPtr initSpecialPIDL(Object desktopIShellFolder, int csidl)
 
432
        {
 
433
            IntPtr result = new IntPtr();        
 
434
            ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, (ShellApi.CSIDL)csidl, ref result);
 
435
            return result;
 
436
        }
 
437
 
 
438
        /// <summary>
 
439
        /// Creates an IShellFolder for a special folder
 
440
        /// </summary>
 
441
        /// <param name="desktopIShellFolder">The IShellFolder instance of the Desktop</param>
 
442
        /// <param name="pidl">The desktop relative pIDL of the special folder</param>
 
443
        /// <returns>The IShellFolder for a special folder</returns>
 
444
        [System.Security.SecurityCritical]
 
445
        public static Object initSpecialFolder(Object desktopIShellFolder, IntPtr pidl)
 
446
        {
 
447
            try
 
448
            {
 
449
                // get desktop instance
 
450
                ShellApi.IShellFolder desktop = (ShellApi.IShellFolder)desktopIShellFolder;
 
451
                // call BindToObject of the desktop
 
452
                ShellApi.IShellFolder specialFolder = null;
 
453
                desktop.BindToObject(pidl, IntPtr.Zero, ref ShellApi.GUID_ISHELLFOLDER, out specialFolder);
 
454
                return specialFolder;
 
455
            }
 
456
            catch (System.ArgumentException )
 
457
            {
 
458
                return 0;
 
459
            }
 
460
        }
 
461
 
 
462
        /// <summary>
 
463
        /// Goes down one entry in the given pIDL
 
464
        /// </summary>
 
465
        /// <param name="pIDL">the pIDL to operate on</param>
 
466
        /// <returns>the next entry in the pIDL</returns>
 
467
                [System.Security.SecurityCritical]
 
468
                public static IntPtr getNextPIDLEntry(IntPtr pIDL)
 
469
        {
 
470
            if (pIDL == IntPtr.Zero)
 
471
            {
 
472
                return IntPtr.Zero;
 
473
            }
 
474
 
 
475
            int length = Marshal.ReadInt16(pIDL);
 
476
            if (length == 0) // defined as terminator of a ITEMIDLIST
 
477
            {
 
478
                return IntPtr.Zero;
 
479
            }
 
480
            IntPtr newpIDL = new IntPtr(pIDL.ToInt64() + length);
 
481
            if (Marshal.ReadInt16(newpIDL) == 0)
 
482
            {
 
483
                return IntPtr.Zero;
 
484
            }
 
485
            else
 
486
            {
 
487
                return newpIDL;
 
488
            }
 
489
        }
 
490
        /// <summary>
 
491
        /// Copies the first entry in the given pIDL into a new relative pIDL (with terminator)
 
492
        /// </summary>
 
493
        /// <param name="pIDL">The pIDL to copy from</param>
 
494
        /// <returns>the relative pIDL of the first entry</returns>
 
495
                [System.Security.SecurityCritical]
 
496
                public static IntPtr copyFirstPIDLEntry(IntPtr pIDL)
 
497
        {
 
498
            if (pIDL == IntPtr.Zero)
 
499
            {
 
500
                return IntPtr.Zero;
 
501
            }
 
502
            int length = Marshal.ReadInt16(pIDL) + 2; // +2 for the terminator
 
503
            byte[] buffer = new byte[length];
 
504
            IntPtr newpIDLptr = Marshal.AllocCoTaskMem(length); // create pointer to new pIDL
 
505
            Marshal.Copy(pIDL, buffer, 0, length - 2); // copy content
 
506
            Marshal.Copy(buffer, 0, newpIDLptr, length); // copy content
 
507
            return newpIDLptr;
 
508
        }
 
509
 
 
510
        /// <summary>
 
511
        /// Concatinates two pIDLs
 
512
        /// </summary>
 
513
        /// <param name="ppIDL">a pIDL, if IntPtr.Zero, IntPtr.Zero will be returned</param>
 
514
        /// <param name="pIDL">a pIDL, if IntPtr.Zero, IntPtr.Zero will be returned</param>
 
515
        /// <returns>the concatination of ppIDL and pIDL</returns>
 
516
                [System.Security.SecurityCritical]
 
517
                public static IntPtr combinePIDLs(IntPtr ppIDL, IntPtr pIDL)
 
518
        {
 
519
            if (ppIDL == IntPtr.Zero)
 
520
            {
 
521
                return IntPtr.Zero;
 
522
            }
 
523
            if (pIDL == IntPtr.Zero)
 
524
            {
 
525
                return IntPtr.Zero;
 
526
            }
 
527
            int lengthP = getPIDLlength(ppIDL);
 
528
            int lengthR = getPIDLlength(pIDL);
 
529
            byte[] newPIDL = new byte[lengthP + lengthR + 2];
 
530
            Marshal.Copy(ppIDL, newPIDL, 0, lengthP);
 
531
            Marshal.Copy(pIDL, newPIDL, lengthP, lengthR);
 
532
            IntPtr newpIDLptr = Marshal.AllocCoTaskMem(lengthP + lengthR + 2); // create pointer to new pIDL
 
533
            Marshal.Copy(newPIDL,0,newpIDLptr,newPIDL.Length); // set pointer to new pIDL and delete local structure
 
534
            return newpIDLptr;
 
535
        }
 
536
 
 
537
        /// <summary>
 
538
        /// Calculates the size of a ITEMIDLIST without the two bytes of the terminator
 
539
        /// </summary>
 
540
        /// <param name="pIDL">a pointer to the IDL to get the length of</param>
 
541
        /// <returns>the length in bytes</returns>
 
542
                [System.Security.SecurityCritical]
 
543
                public static int getPIDLlength(IntPtr pIDL)
 
544
        {
 
545
            if( pIDL == IntPtr.Zero )
 
546
            {
 
547
                return 0;
 
548
            }
 
549
            int length = Marshal.ReadInt16(pIDL);
 
550
            int offset = length;
 
551
            while (length > 0)
 
552
            {
 
553
                length = Marshal.ReadInt16(new IntPtr( pIDL.ToInt64() + offset));
 
554
                offset += length;
 
555
            }
 
556
            return offset;
 
557
        }
 
558
        /// <summary>
 
559
        /// Releases the allocted memory of a pIDL
 
560
        /// </summary>
 
561
        /// <param name="pIDL">The pIDL to be released</param>
 
562
                [System.Security.SecurityCritical]
 
563
                public static void releasePIDL(IntPtr pIDL)
 
564
        {
 
565
            if (pIDL == IntPtr.Zero)
 
566
            {
 
567
                return;
 
568
            }
 
569
            Marshal.Release(pIDL);
 
570
        }
 
571
 
 
572
                /// <summary>
 
573
        /// Releases an IShellFolder COM object
 
574
        /// </summary>
 
575
        /// <param name="pIShellFolder">The IShellFolder to be released, must not be null</param>
 
576
                [System.Security.SecurityCritical]
 
577
                public static void releaseIShellFolder(Object pIShellFolder)
 
578
        {
 
579
            if (pIShellFolder == null)
 
580
            {
 
581
                return;
 
582
            }
 
583
            Marshal.ReleaseComObject(pIShellFolder);
 
584
        }
 
585
 
 
586
                [System.Security.SecurityCritical]
 
587
                public static int compareIDs(Object pParentIShellFolder, IntPtr pidl1, IntPtr pidl2)
 
588
        {
 
589
            if (pParentIShellFolder == null)
 
590
            {
 
591
                return 0;
 
592
            }
 
593
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pParentIShellFolder;
 
594
            return folder.CompareIDs(0, pidl1, pidl2);
 
595
        }
 
596
 
 
597
                [System.Security.SecurityCritical]
 
598
                public static int getAttributes0(Object pParentIShellFolder, IntPtr pIDL, int attrsMask)
 
599
        {
 
600
            if (pParentIShellFolder == null || pIDL == IntPtr.Zero )
 
601
            {
 
602
                return 0;
 
603
            }
 
604
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pParentIShellFolder;
 
605
            ShellApi.SFGAOF[] atts = new ShellApi.SFGAOF[]{ (ShellApi.SFGAOF)attrsMask };
 
606
            IntPtr[] pIDLs = new IntPtr[] { pIDL };
 
607
            folder.GetAttributesOf(1, pIDLs, atts);
 
608
            return (int)atts[0];
 
609
        }
 
610
 
 
611
        [System.Security.SecurityCritical]
 
612
        public static String getFileSystemPath(int csidl)
 
613
        {
 
614
            IntPtr pIDL = new IntPtr();
 
615
            int hRes = ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, (ShellApi.CSIDL)csidl, ref pIDL);
 
616
            if (hRes != 0)
 
617
            {
 
618
                //throw Marshal.ThrowExceptionForHR(hRes);
 
619
                // TODO exception for hRes
 
620
                return null;
 
621
            }
 
622
            StringBuilder builder = new StringBuilder( 1024 );
 
623
            if (ShellApi.SHGetPathFromIDList(pIDL, builder))
 
624
            {
 
625
                return builder.ToString();
 
626
            }
 
627
            else
 
628
            {
 
629
                return null;
 
630
            }
 
631
        }
 
632
 
 
633
        [System.Security.SecurityCritical]
 
634
        public static Object getEnumObjects(Object pIShellFolder, Boolean isDesktop, Boolean includeHiddenFiles)
 
635
        {
 
636
            if (pIShellFolder == null)
 
637
            {
 
638
                return null;
 
639
            }
 
640
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pIShellFolder;
 
641
            ShellApi.SHCONTF flags = ShellApi.SHCONTF.SHCONTF_FOLDERS | ShellApi.SHCONTF.SHCONTF_NONFOLDERS;
 
642
            if( includeHiddenFiles )
 
643
            {
 
644
                flags |= ShellApi.SHCONTF.SHCONTF_INCLUDEHIDDEN;
 
645
            }
 
646
 
 
647
            ShellApi.IEnumIDList list = null;
 
648
            folder.EnumObjects(IntPtr.Zero, flags, out list);
 
649
            return list;
 
650
        }
 
651
 
 
652
        /// <summary>
 
653
        /// Returns the next pIDL in an IEnumIDList
 
654
        /// </summary>
 
655
        /// <param name="pEnumObjects">The IEnumIDList to get the next element of</param>
 
656
        /// <returns>a pIDL or IntPtr.Zero in case the end of the enum is reached</returns>
 
657
        [System.Security.SecurityCritical]
 
658
        public static IntPtr getNextChild(Object pEnumObjects)
 
659
        {
 
660
            if (pEnumObjects == null)
 
661
            {
 
662
                return IntPtr.Zero;
 
663
            }
 
664
            ShellApi.IEnumIDList list = (ShellApi.IEnumIDList)pEnumObjects;
 
665
            IntPtr pIDL = new IntPtr();
 
666
            int pceltFetched; // can be ignored, if celt = 1
 
667
            uint hRes = list.Next(1, out pIDL, out pceltFetched);
 
668
            if ( hRes != 0 || pceltFetched == 0 )
 
669
            {
 
670
                return IntPtr.Zero;
 
671
            }
 
672
            else
 
673
            {
 
674
                return pIDL;
 
675
            }
 
676
        }
 
677
 
 
678
        /// <summary>
 
679
        /// Releases an IEnumIDList
 
680
        /// </summary>
 
681
        /// <param name="pEnumObjects">The IEnumIDList to be released</param>
 
682
                [System.Security.SecurityCritical]
 
683
                public static void releaseEnumObjects(Object pEnumObjects)
 
684
        {
 
685
            if (pEnumObjects != null)
 
686
            {
 
687
                Marshal.ReleaseComObject(pEnumObjects);
 
688
            }
 
689
        }
 
690
 
 
691
        /// <summary>
 
692
        /// Binds an IShellFolder to the child of a given shell folder
 
693
        /// </summary>
 
694
        /// <param name="parentIShellFolder">the parent IShellFolder</param>
 
695
        /// <param name="pIDL">the relative pIDL to the child</param>
 
696
        /// <returns>The IShellFolder of the child or null, if there is no such child</returns>
 
697
                [System.Security.SecurityCritical]
 
698
                public static Object bindToObject(Object parentIShellFolder, IntPtr pIDL)
 
699
        {
 
700
            if (parentIShellFolder == null || pIDL == IntPtr.Zero )
 
701
            {
 
702
                return null;
 
703
            }
 
704
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)parentIShellFolder;
 
705
            ShellApi.IShellFolder newFolder = null;
 
706
            folder.BindToObject(pIDL, IntPtr.Zero, ref ShellApi.GUID_ISHELLFOLDER, out newFolder);
 
707
            return newFolder;
 
708
        }
 
709
 
 
710
        /// <summary>
 
711
        /// Parses the displayname of a child of a given folder
 
712
        /// </summary>
 
713
        /// <param name="pIShellFolder">The IShellFolder to get the chilf of</param>
 
714
        /// <param name="name">The display name of the child</param>
 
715
        /// <returns>the relative pIDL of the child or IntPrt.Zero in case there is no such child</returns>
 
716
        [System.Security.SecurityCritical]
 
717
        public static IntPtr parseDisplayName0(Object pIShellFolder, String name)
 
718
        {
 
719
            if (pIShellFolder == null)
 
720
            {
 
721
                return IntPtr.Zero;
 
722
            }
 
723
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pIShellFolder;
 
724
            IntPtr pIDL = new IntPtr();
 
725
            uint pchEaten;
 
726
            uint pdwAttribute = 0;
 
727
            folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, name, out pchEaten, out pIDL, ref pdwAttribute);
 
728
            return pIDL;
 
729
        }
 
730
 
 
731
                [System.Security.SecurityCritical]
 
732
                public static String getDisplayNameOf(Object parentIShellFolder, IntPtr relativePIDL, int attrs)
 
733
        {
 
734
            if (parentIShellFolder == null || relativePIDL == IntPtr.Zero)
 
735
            {
 
736
                return null;
 
737
            }
 
738
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)parentIShellFolder;
 
739
            ShellApi.STRRET result;
 
740
            uint hRes = folder.GetDisplayNameOf(relativePIDL, (ShellApi.SHGDN)attrs, out result);
 
741
            if ( hRes == 0 )
 
742
            {
 
743
                StringBuilder name = new StringBuilder( 1024 );
 
744
                StrRetToBuf(ref result, relativePIDL, name, 1024);
 
745
                string stringName = name.ToString();
 
746
                return stringName;
 
747
            }
 
748
            return null;
 
749
        }
 
750
 
 
751
                [System.Security.SecurityCritical]
 
752
                public static String getFolderType(IntPtr pIDL)
 
753
        {
 
754
            ShellApi.SHFILEINFO fileInfo = new ShellApi.SHFILEINFO();
 
755
            ShellApi.SHGetFileInfo(pIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo), ShellApi.SHGFI.SHGFI_PIDL | ShellApi.SHGFI.SHGFI_TYPENAME);
 
756
            return fileInfo.szTypeName;
 
757
        }
 
758
 
 
759
        public static Object getIShellIcon(Object pIShellFolder)
 
760
        {
 
761
            if (pIShellFolder is ShellApi.IShellIcon)
 
762
            {
 
763
                return pIShellFolder;
 
764
            }
 
765
            return null;
 
766
        }
 
767
 
 
768
                [System.Security.SecurityCritical]
 
769
                public static int getIconIndex(Object parentIShellFolder, IntPtr relativePIDL)
 
770
        {
 
771
            if (parentIShellFolder is ShellApi.IShellIcon)
 
772
            {
 
773
                ShellApi.IShellIcon shellIcon = (ShellApi.IShellIcon)parentIShellFolder;
 
774
                int index = 0;
 
775
                if( shellIcon.GetIconOf(relativePIDL, (uint)ShellApi.GIL.GIL_FORSHELL, out index) == 0 )
 
776
                {
 
777
                    return index;
 
778
                }
 
779
            }
 
780
            return 0;
 
781
        }
 
782
 
 
783
        [System.Security.SecurityCritical]
 
784
        public static IntPtr getIcon(String absolutePath, Boolean getLargeIcon) 
 
785
        {
 
786
            ShellApi.SHFILEINFO shinfo = new ShellApi.SHFILEINFO();
 
787
            if (ShellApi.SHGetFileInfo(absolutePath, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), ShellApi.SHGFI.SHGFI_ICON | (getLargeIcon ? ShellApi.SHGFI.SHGFI_LARGEICON : ShellApi.SHGFI.SHGFI_SMALLICON)) == IntPtr.Zero)
 
788
            {
 
789
                return IntPtr.Zero;
 
790
            }
 
791
            return shinfo.hIcon;
 
792
        }
 
793
 
 
794
                [System.Security.SecurityCritical]
 
795
                public static IntPtr extractIcon(Object parentIShellFolder, IntPtr relativePIDL, Boolean getLargeIcon)
 
796
        {
 
797
            if (parentIShellFolder == null || relativePIDL == IntPtr.Zero)
 
798
            {
 
799
                return IntPtr.Zero;
 
800
            }
 
801
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)parentIShellFolder;
 
802
            Guid guid = new Guid("000214fa-0000-0000-c000-000000000046");
 
803
            object ppv;
 
804
            if (folder.GetUIObjectOf(IntPtr.Zero, 1, new IntPtr[] { relativePIDL }, ref guid, IntPtr.Zero, out ppv) == 0)
 
805
            {
 
806
                ShellApi.IExtractIcon extractor = (ShellApi.IExtractIcon)ppv;
 
807
                int size = 1024;
 
808
                StringBuilder path = new StringBuilder( size );
 
809
                int piIndex;
 
810
                uint pwFlags;
 
811
                if (extractor.GetIconLocation((uint)ShellApi.GIL.GIL_FORSHELL, path, size, out piIndex, out pwFlags) == 0)
 
812
                {
 
813
                    IntPtr hIconL = new IntPtr();
 
814
                    IntPtr hIconS = new IntPtr();
 
815
                    if (extractor.Extract(path.ToString(), (uint)piIndex, out hIconL, out hIconS, (16 << 16) + 32) == 0)
 
816
                    {
 
817
                        if (getLargeIcon)
 
818
                        {
 
819
                            ShellApi.DestroyIcon(hIconS);
 
820
                            return hIconL;
 
821
                        }
 
822
                        else
 
823
                        {
 
824
                            ShellApi.DestroyIcon(hIconL);
 
825
                            return hIconS;
 
826
                        }
 
827
                    }
 
828
                }
 
829
            }
 
830
            return IntPtr.Zero;
 
831
        }
 
832
 
 
833
                [System.Security.SecurityCritical]
 
834
                public static void disposeIcon(IntPtr hIcon)
 
835
        {
 
836
            ShellApi.DestroyIcon(hIcon);
 
837
        }
 
838
 
 
839
                public static Object doGetColumnInfo(Object iShellFolder2)
 
840
        {
 
841
            // TODO Dummy
 
842
            return null;
 
843
        }
 
844
 
 
845
                public static Object doGetColumnValue(Object parentIShellFolder2, IntPtr childPIDL, int columnIdx)
 
846
        {
 
847
            // TODO Dummy
 
848
            return null;
 
849
        }
 
850
 
 
851
                public static int compareIDsByColumn(Object pParentIShellFolder, IntPtr pidl1, IntPtr pidl2, int columnIdx)
 
852
        {
 
853
            // TODO Dummy
 
854
            return 0;
 
855
        }
 
856
        }
 
857
 
 
858
        [System.Security.SecurityCritical]
 
859
        class ShellLink : IDisposable
 
860
        {
 
861
                [ComImport]
 
862
                [Guid("0000010B-0000-0000-C000-000000000046")]
 
863
                [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 
864
                internal interface IPersistFile
 
865
                {
 
866
                        [PreserveSig]
 
867
                        void GetClassID(out Guid pClassID);
 
868
                        [PreserveSig]
 
869
                        void IsDirty();
 
870
                        [PreserveSig]
 
871
                        void Load([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, uint dwMode);
 
872
                        [PreserveSig]
 
873
                        void Save([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, [MarshalAs(UnmanagedType.Bool)] bool fRemember);
 
874
                        [PreserveSig]
 
875
                        void SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
 
876
                        [PreserveSig]
 
877
                        void GetCurFile([MarshalAs(UnmanagedType.LPWStr)] out string ppszFileName);
 
878
                }
 
879
 
 
880
                [ComImport]
 
881
                [Guid("000214F9-0000-0000-C000-000000000046")]
 
882
                [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 
883
                private interface IShellLinkW
 
884
                {
 
885
                        void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, IntPtr pfd, uint fFlags);
 
886
                        void GetIDList(out IntPtr ppidl);
 
887
                        void SetIDList(IntPtr pidl);
 
888
                        void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxName);
 
889
                        void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
 
890
                        void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
 
891
                        void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
 
892
                        void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
 
893
                        void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
 
894
                        void GetHotkey(out short pwHotkey);
 
895
                        void SetHotkey(short pwHotkey);
 
896
                        void GetShowCmd(out uint piShowCmd);
 
897
                        void SetShowCmd(uint piShowCmd);
 
898
                        void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
 
899
                        void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
 
900
                        void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, uint dwReserved);
 
901
            /// <summary>Attempts to find the target of a Shell link, even if it has been moved or renamed.</summary>
 
902
                        void Resolve(IntPtr hWnd, uint fFlags);
 
903
                        void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
 
904
                }
 
905
 
 
906
                [Guid("00021401-0000-0000-C000-000000000046")]
 
907
                [ClassInterfaceAttribute(ClassInterfaceType.None)]
 
908
                [ComImport]
 
909
                private class CShellLink { }
 
910
 
 
911
                [Flags]
 
912
                public enum EShowWindowFlags : uint
 
913
                {
 
914
                        SW_HIDE = 0,
 
915
                        SW_SHOWNORMAL = 1,
 
916
                        SW_NORMAL = 1,
 
917
                        SW_SHOWMINIMIZED = 2,
 
918
                        SW_SHOWMAXIMIZED = 3,
 
919
                        SW_MAXIMIZE = 3,
 
920
                        SW_SHOWNOACTIVATE = 4,
 
921
                        SW_SHOW = 5,
 
922
                        SW_MINIMIZE = 6,
 
923
                        SW_SHOWMINNOACTIVE = 7,
 
924
                        SW_SHOWNA = 8,
 
925
                        SW_RESTORE = 9,
 
926
                        SW_SHOWDEFAULT = 10,
 
927
                        SW_MAX = 10
 
928
                }
 
929
 
 
930
                private IShellLinkW linkW = (IShellLinkW)new CShellLink();
 
931
 
 
932
                [System.Security.SecuritySafeCritical]
 
933
                public void Dispose()
 
934
                {
 
935
                        if (linkW != null)
 
936
                        {
 
937
                                Marshal.ReleaseComObject(linkW);
 
938
                                linkW = null;
 
939
                        }
 
940
                }
 
941
 
 
942
                public void SetPath(string path)
 
943
                {
 
944
                        linkW.SetPath(path);
 
945
                }
 
946
 
 
947
                public void SetDescription(string description)
 
948
                {
 
949
                        linkW.SetDescription(description);
 
950
                }
 
951
 
 
952
                public void SetWorkingDirectory(string dir)
 
953
                {
 
954
                        linkW.SetWorkingDirectory(dir);
 
955
                }
 
956
 
 
957
                public void SetArguments(string args)
 
958
                {
 
959
                        linkW.SetArguments(args);
 
960
                }
 
961
 
 
962
                public void SetShowCmd(EShowWindowFlags cmd)
 
963
                {
 
964
                        linkW.SetShowCmd((uint)cmd);
 
965
                }
 
966
 
 
967
                public void Save(string linkFile)
 
968
                {
 
969
                        ((IPersistFile)linkW).Save(linkFile, true);
 
970
                }
 
971
 
 
972
                public void Load(string linkFile)
 
973
                {
 
974
                        ((IPersistFile)linkW).Load(linkFile, 0);
 
975
                }
 
976
 
 
977
                public string GetArguments()
 
978
                {
 
979
                        StringBuilder sb = new StringBuilder(512);
 
980
                        linkW.GetArguments(sb, sb.Capacity);
 
981
                        return sb.ToString();
 
982
                }
 
983
 
 
984
        public void Resolve()
 
985
        {
 
986
            linkW.Resolve(IntPtr.Zero, 0);
 
987
        }
 
988
 
 
989
        public IntPtr GetIDList(){
 
990
            IntPtr ppidl;
 
991
            linkW.GetIDList( out ppidl );
 
992
            return ppidl;
 
993
        }
 
994
 
 
995
                public string GetPath()
 
996
                {
 
997
                        StringBuilder sb = new StringBuilder(512);
 
998
                        linkW.GetPath(sb, sb.Capacity, IntPtr.Zero, 0);
 
999
                        return sb.ToString();
 
1000
                }
 
1001
        }
 
1002
 
 
1003
 
 
1004
}
 
1005
 
 
1006
namespace IKVM.NativeCode.sun.java2d
 
1007
{
 
1008
        static class DefaultDisposerRecord
 
1009
        {
 
1010
                public static void invokeNativeDispose(long disposerMethodPointer, long dataPointer)
 
1011
                {
 
1012
                        throw new NotImplementedException();
 
1013
                }
 
1014
        }
 
1015
 
 
1016
        static class Disposer
 
1017
        {
 
1018
                public static void initIDs()
 
1019
                {
 
1020
                }
 
1021
        }
 
1022
}
 
1023
 
 
1024
namespace IKVM.NativeCode.sun.java2d.pipe
 
1025
{
 
1026
        static class Region
 
1027
        {
 
1028
                public static void initIDs() { }
 
1029
        }
 
1030
 
 
1031
        static class RenderBuffer
 
1032
        {
 
1033
                public static void copyFromArray(object srcArray, long srcPos, long dstAddr, long length)
 
1034
                {
 
1035
                        throw new NotImplementedException();
 
1036
                }
 
1037
        }
 
1038
}