~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/lib/stetic/libstetic/ImageInfo.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
using System;
3
 
using System.IO;
4
 
using System.CodeDom;
5
 
 
6
 
namespace Stetic
7
 
{
8
 
        public enum ImageSource
9
 
        {
10
 
                Theme,
11
 
                Resource,
12
 
                File
13
 
        }
14
 
        
15
 
        public class ImageInfo
16
 
        {
17
 
                ImageSource source;
18
 
                string name;
19
 
                Gtk.IconSize size;
20
 
                Gdk.Pixbuf image;
21
 
                
22
 
                private ImageInfo ()
23
 
                {
24
 
                }
25
 
                
26
 
                public string Label {
27
 
                        get {
28
 
                                if (source == ImageSource.File)
29
 
                                        return Path.GetFileName (name);
30
 
                                else
31
 
                                        return name; 
32
 
                        }
33
 
                }
34
 
                
35
 
                public string Name {
36
 
                        get { return name; }
37
 
                }
38
 
                
39
 
                public Gtk.IconSize ThemeIconSize {
40
 
                        get { return size; }
41
 
                }
42
 
                
43
 
                public ImageSource Source {
44
 
                        get { return source; }
45
 
                }
46
 
                
47
 
                public override string ToString ()
48
 
                {
49
 
                        if (source == ImageSource.Theme)
50
 
                                return "stock:" + name + " " + size;
51
 
                        else if (source == ImageSource.Resource)
52
 
                                return "resource:" + name;
53
 
                        else
54
 
                                return "file:" + name;
55
 
                }
56
 
                
57
 
                public static ImageInfo FromResource (string resourceName)
58
 
                {
59
 
                        ImageInfo info = new ImageInfo ();
60
 
                        info.name = resourceName;
61
 
                        info.source = ImageSource.Resource;
62
 
                        return info;
63
 
                }
64
 
                
65
 
                public static ImageInfo FromTheme (string iconId, Gtk.IconSize size)
66
 
                {
67
 
                        ImageInfo info = new ImageInfo ();
68
 
                        info.name = iconId;
69
 
                        info.size = size;
70
 
                        info.source = ImageSource.Theme;
71
 
                        return info;
72
 
                }
73
 
                
74
 
                public static ImageInfo FromFile (string file)
75
 
                {
76
 
                        ImageInfo info = new ImageInfo ();
77
 
                        info.name = file;
78
 
                        info.source = ImageSource.File;
79
 
                        return info;
80
 
                }
81
 
                
82
 
                public static ImageInfo FromString (string str)
83
 
                {
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 (' ');
91
 
                                if (s.Length != 2)
92
 
                                        return null;
93
 
                                info.name = s[0];
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);
98
 
                        } else
99
 
                                return null;
100
 
                        return info;
101
 
                }
102
 
                
103
 
                public Gdk.Pixbuf GetImage (IProject project)
104
 
                {
105
 
                        if (image != null)
106
 
                                return image;
107
 
 
108
 
                        switch (source) {
109
 
                                case ImageSource.Resource:
110
 
                                        if (project.ResourceProvider == null)
111
 
                                                return null;
112
 
                                        System.IO.Stream s = project.ResourceProvider.GetResourceStream (name);
113
 
                                        if (s == null)
114
 
                                                return null;
115
 
                                        try {
116
 
                                                return image = new Gdk.Pixbuf (s);
117
 
                                        } catch {
118
 
                                                // Not a valid image
119
 
                                                return WidgetUtils.MissingIcon;
120
 
                                        }
121
 
                                        
122
 
                                case ImageSource.Theme:
123
 
                                        return image = WidgetUtils.LoadIcon (name, size);
124
 
                                        
125
 
                                case ImageSource.File:
126
 
                                        try {
127
 
                                                string file = Path.Combine (project.ImagesRootPath, name);
128
 
                                                return image = new Gdk.Pixbuf (file);
129
 
                                        } catch {
130
 
                                                return WidgetUtils.MissingIcon;
131
 
                                        }
132
 
                        }
133
 
                        return null;
134
 
                }
135
 
                
136
 
                public Gdk.Pixbuf GetThumbnail (IProject project, int thumbnailSize)
137
 
                {
138
 
                        Gdk.Pixbuf pix = GetImage (project);
139
 
                        if (pix == null)
140
 
                                return null;
141
 
 
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);
146
 
                        }
147
 
                        return pix;
148
 
                }
149
 
                
150
 
                public Gdk.Pixbuf GetScaledImage (IProject project, Gtk.IconSize size)
151
 
                {
152
 
                        int w, h;
153
 
                        Gtk.Icon.SizeLookup (size, out w, out h);
154
 
                        return GetScaledImage (project, w, h);
155
 
                }
156
 
                
157
 
                public Gdk.Pixbuf GetScaledImage (IProject project, int width, int height)
158
 
                {
159
 
                        Gdk.Pixbuf pix = GetImage (project);
160
 
                        if (pix == null)
161
 
                                return null;
162
 
                        else
163
 
                                return ScaleImage (pix, width, height);
164
 
                }
165
 
                
166
 
                Gdk.Pixbuf ScaleImage (Gdk.Pixbuf pix, int width, int height)
167
 
                {
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);
172
 
                                }
173
 
                        } else {
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);
177
 
                                }
178
 
                        }
179
 
                        return pix;
180
 
                }
181
 
                
182
 
                public CodeExpression ToCodeExpression (GeneratorContext ctx)
183
 
                {
184
 
                        switch (source) {
185
 
                                case ImageSource.Resource:
186
 
                                        return new CodeMethodInvokeExpression (
187
 
                                                new CodeTypeReferenceExpression (typeof(Gdk.Pixbuf)),
188
 
                                                "LoadFromResource",
189
 
                                                new CodePrimitiveExpression (name)
190
 
                                        );
191
 
                                        
192
 
                                case ImageSource.Theme:
193
 
                                        return ctx.GenerateLoadPixbuf (name, size);
194
 
                                        
195
 
                                case ImageSource.File:
196
 
                                        return new CodeObjectCreateExpression (
197
 
                                                typeof(Gdk.Pixbuf),
198
 
                                                new CodeMethodInvokeExpression (
199
 
                                                        new CodeTypeReferenceExpression (typeof(System.IO.Path)),
200
 
                                                        "Combine",
201
 
                                                        new CodePropertyReferenceExpression (
202
 
                                                                new CodePropertyReferenceExpression (
203
 
                                                                        new CodeTypeReferenceExpression (typeof(AppDomain)),
204
 
                                                                        "CurrentDomain"
205
 
                                                                ),
206
 
                                                                "BaseDirectory"
207
 
                                                        ),
208
 
                                                        new CodePrimitiveExpression (name)
209
 
                                                )
210
 
                                        );
211
 
                        }
212
 
                        return new CodePrimitiveExpression (null);
213
 
                }
214
 
        }
215
 
}