4 // Copyright (C) 2004 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
29 using System
.Reflection
;
38 // This class is fully static.
41 static private Stream
GetStreamInner (string name
)
45 if (name
.StartsWith ("file://")) {
46 name
= name
.Substring ("file://".Length
);
47 if (File
.Exists (name
))
48 stream
= File
.OpenRead (name
);
49 } else if (name
.StartsWith ("/")) {
50 if (File
.Exists (name
))
51 stream
= File
.OpenRead (name
);
53 Assembly assembly
= Assembly
.GetExecutingAssembly ();
54 stream
= assembly
.GetManifestResourceStream (name
);
60 static public Stream
GetStream (string name
)
64 if (name
== null || name
.Length
== 0)
67 stream
= GetStreamInner (name
);
69 stream
= GetStreamInner (name
+ ".png");
71 stream
= GetStreamInner (name
+ ".jpg");
73 Console
.WriteLine ("Couldn't get stream for image '{0}'", name
);
77 static public Gdk
.Pixbuf
GetPixbuf (string name
)
79 Stream s
= GetStream (name
);
80 return s
!= null ? new Gdk
.Pixbuf (s
) : null;
83 static public Gdk
.Pixbuf
GetPixbuf (string name
, int maxWidth
, int maxHeight
)
85 Gdk
.Pixbuf pixbuf
= GetPixbuf (name
);
89 double scaleWidth
= maxWidth
/ (double)pixbuf
.Width
;
90 double scaleHeight
= maxHeight
/ (double)pixbuf
.Height
;
92 double s
= Math
.Min (scaleWidth
, scaleHeight
);
96 int w
= (int) Math
.Round (s
* pixbuf
.Width
);
97 int h
= (int) Math
.Round (s
* pixbuf
.Height
);
99 return pixbuf
.ScaleSimple (w
, h
, Gdk
.InterpType
.Bilinear
);
102 static public Gtk
.Widget
GetWidget (string name
)
104 Gdk
.Pixbuf pixbuf
= GetPixbuf (name
);
105 return pixbuf
!= null ? new Gtk
.Image (pixbuf
) : null;
108 static public Gtk
.Widget
GetWidget (string name
, int maxWidth
, int maxHeight
)
110 Gdk
.Pixbuf pixbuf
= GetPixbuf (name
, maxWidth
, maxHeight
);
111 return pixbuf
!= null ? new Gtk
.Image (pixbuf
) : null;