1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xwt.GtkBackend;
using System.Runtime.InteropServices;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace Xwt.Gtk.Windows
{
class GtkWindowsDesktopBackend: GtkDesktopBackend
{
Dictionary<string, Gdk.Pixbuf> icons = new Dictionary<string, Gdk.Pixbuf> ();
public override object GetFileIcon (string filename)
{
var normal = GetIcon (filename, 0);
if (normal == null)
return null;
var frames = new List<Gdk.Pixbuf> ();
frames.Add (normal);
var small = GetIcon (filename, Win32.SHGFI_SMALLICON);
if (small != null && !frames.Contains (small))
frames.Add (small);
var shell = GetIcon (filename, Win32.SHGFI_SHELLICONSIZE);
if (shell != null && !frames.Contains (shell))
frames.Add (shell);
var large = GetIcon (filename, Win32.SHGFI_LARGEICON);
if (large != null && !frames.Contains (large))
frames.Add (large);
return new GtkImage (frames);
}
Gdk.Pixbuf GetIcon (string filename, uint size)
{
SHFILEINFO shinfo = new SHFILEINFO ();
Win32.SHGetFileInfoW (filename, Win32.FILE_ATTRIBUTES_NORMAL, ref shinfo, (uint)Marshal.SizeOf (shinfo), Win32.SHGFI_USEFILEATTRIBUTES | Win32.SHGFI_ICON | Win32.SHGFI_ICONLOCATION | Win32.SHGFI_TYPENAME | size);
if (shinfo.iIcon == 0) {
Win32.DestroyIcon (shinfo.hIcon);
return null;
}
var icon = Icon.FromHandle (shinfo.hIcon);
string key = shinfo.iIcon + " - " + shinfo.szDisplayName + " - " + icon.Width;
Gdk.Pixbuf pix;
if (!icons.TryGetValue (key, out pix)) {
pix = CreateFromResource (icon.ToBitmap ());
icons[key] = pix;
}
Win32.DestroyIcon (shinfo.hIcon);
return pix;
}
public Gdk.Pixbuf CreateFromResource (Bitmap bitmap)
{
MemoryStream ms = new MemoryStream ();
bitmap.Save (ms, ImageFormat.Png);
ms.Position = 0;
return new Gdk.Pixbuf (ms);
}
}
}
|