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;
114 static public string GetHtmlSourceForStock (string stockid
,
118 IconTheme icon_theme
= new IconTheme ();
119 string path
= icon_theme
.LookupIcon (stockid
, size
, IconData
.Zero
, out base_size
);
121 if (path
!= null && path
!= "") {
122 return "file://" + path
;
127 static public string GetHtmlSource (byte[] binary_data
,
130 string base64_string
=
131 System
.Convert
.ToBase64String(binary_data
,
135 string data
= "data:" + mime_type
+ ";base64," + base64_string
;
140 static public string GetHtmlSource (string name
,
143 if (name
== null || name
.Length
== 0) {
145 } else if (name
.StartsWith ("file://")) {
147 } else if (name
.StartsWith ("/")) {
148 return StringFu
.PathToQuotedFileUri (name
);
152 if (mime_type
== null || mime_type
== "")
153 throw new ArgumentException ();
155 // FIXME: it's probably worth caching these,
156 // since they'll probably be repeated a lot
158 stream
= GetStream (name
);
162 byte[] binary_data
= new Byte
[stream
.Length
];
163 stream
.Read(binary_data
, 0, (int) stream
.Length
);
165 return GetHtmlSource (binary_data
, mime_type
);