8
public enum ImageSource
15
public class ImageInfo
28
if (source == ImageSource.File)
29
return Path.GetFileName (name);
39
public Gtk.IconSize ThemeIconSize {
43
public ImageSource Source {
44
get { return source; }
47
public override string ToString ()
49
if (source == ImageSource.Theme)
50
return "stock:" + name + " " + size;
51
else if (source == ImageSource.Resource)
52
return "resource:" + name;
54
return "file:" + name;
57
public static ImageInfo FromResource (string resourceName)
59
ImageInfo info = new ImageInfo ();
60
info.name = resourceName;
61
info.source = ImageSource.Resource;
65
public static ImageInfo FromTheme (string iconId, Gtk.IconSize size)
67
ImageInfo info = new ImageInfo ();
70
info.source = ImageSource.Theme;
74
public static ImageInfo FromFile (string file)
76
ImageInfo info = new ImageInfo ();
78
info.source = ImageSource.File;
82
public static ImageInfo FromString (string str)
84
ImageInfo info = new ImageInfo ();
85
if (str.StartsWith ("resource:")) {
86
info.source = ImageSource.Resource;
87
info.name = str.Substring (9);
88
} else if (str.StartsWith ("stock:")) {
89
info.source = ImageSource.Theme;
90
string[] s = str.Substring (6).Split (' ');
94
info.size = (Gtk.IconSize) Enum.Parse (typeof(Gtk.IconSize), s[1]);
95
} else if (str.StartsWith ("file:")) {
96
info.source = ImageSource.File;
97
info.name = str.Substring (5);
103
public Gdk.Pixbuf GetImage (IProject project)
109
case ImageSource.Resource:
110
if (project.ResourceProvider == null)
112
System.IO.Stream s = project.ResourceProvider.GetResourceStream (name);
116
return image = new Gdk.Pixbuf (s);
119
return WidgetUtils.MissingIcon;
122
case ImageSource.Theme:
123
return image = WidgetUtils.LoadIcon (name, size);
125
case ImageSource.File:
127
string file = Path.Combine (project.ImagesRootPath, name);
128
return image = new Gdk.Pixbuf (file);
130
return WidgetUtils.MissingIcon;
136
public Gdk.Pixbuf GetThumbnail (IProject project, int thumbnailSize)
138
Gdk.Pixbuf pix = GetImage (project);
142
if (pix.Width >= pix.Height && pix.Width > thumbnailSize) {
143
return ScaleImage (pix, thumbnailSize, thumbnailSize);
144
} else if (pix.Height > pix.Width && pix.Height > thumbnailSize) {
145
return ScaleImage (pix, thumbnailSize, thumbnailSize);
150
public Gdk.Pixbuf GetScaledImage (IProject project, Gtk.IconSize size)
153
Gtk.Icon.SizeLookup (size, out w, out h);
154
return GetScaledImage (project, w, h);
157
public Gdk.Pixbuf GetScaledImage (IProject project, int width, int height)
159
Gdk.Pixbuf pix = GetImage (project);
163
return ScaleImage (pix, width, height);
166
Gdk.Pixbuf ScaleImage (Gdk.Pixbuf pix, int width, int height)
168
if ((pix.Width - width) > (pix.Height - height)) {
169
if (pix.Width != width) {
170
float prop = (float) pix.Height / (float) pix.Width;
171
return pix.ScaleSimple (width, (int)(width * prop), Gdk.InterpType.Bilinear);
174
if (pix.Height != height) {
175
float prop = (float) pix.Width / (float) pix.Height;
176
return pix.ScaleSimple ((int)(height * prop), height, Gdk.InterpType.Bilinear);
182
public CodeExpression ToCodeExpression (GeneratorContext ctx)
185
case ImageSource.Resource:
186
return new CodeMethodInvokeExpression (
187
new CodeTypeReferenceExpression (typeof(Gdk.Pixbuf)),
189
new CodePrimitiveExpression (name)
192
case ImageSource.Theme:
193
return ctx.GenerateLoadPixbuf (name, size);
195
case ImageSource.File:
196
return new CodeObjectCreateExpression (
198
new CodeMethodInvokeExpression (
199
new CodeTypeReferenceExpression (typeof(System.IO.Path)),
201
new CodePropertyReferenceExpression (
202
new CodePropertyReferenceExpression (
203
new CodeTypeReferenceExpression (typeof(AppDomain)),
208
new CodePrimitiveExpression (name)
212
return new CodePrimitiveExpression (null);