static void LoadStockIcon (StockIconCodon iconCodon) { try { Gdk.Pixbuf pixbuf = null; if (!string.IsNullOrEmpty (iconCodon.Resource) || !string.IsNullOrEmpty (iconCodon.File)) { // using the stream directly produces a gdk warning. byte[] buffer; Stream stream; if (iconCodon.Resource != null) stream = iconCodon.Addin.GetResource (iconCodon.Resource); else stream = File.OpenRead (iconCodon.Addin.GetFilePath (iconCodon.File)); using (stream) { if (stream == null || stream.Length < 0) { LoggingService.LogError ("Did not find resource '{0}' in addin '{1}' for icon '{2}'", iconCodon.Resource, iconCodon.Addin.Id, iconCodon.StockId); return; } buffer = new byte [stream.Length]; stream.Read (buffer, 0, (int)stream.Length); } pixbuf = new Gdk.Pixbuf (buffer); } else if (!string.IsNullOrEmpty (iconCodon.IconId)) { pixbuf = GetPixbuf (InternalGetStockId (iconCodon.Addin, iconCodon.IconId, iconCodon.IconSize), iconCodon.IconSize); } if (pixbuf != null) AddToIconFactory (iconCodon.StockId, pixbuf, iconCodon.IconSize); } catch (Exception ex) { LoggingService.LogError (string.Format ("Error loading icon '{0}'", iconCodon.StockId), ex); } }
static void LoadStockIcon (StockIconCodon iconCodon, bool forceWildcard) { try { Gdk.Pixbuf pixbuf = null, pixbuf2x = null; AnimatedIcon animatedIcon = null; if (!string.IsNullOrEmpty (iconCodon.Resource) || !string.IsNullOrEmpty (iconCodon.File)) { // using the stream directly produces a gdk warning. byte[] buffer; Stream stream, stream2x = null; if (iconCodon.Resource != null) { stream = iconCodon.Addin.GetResource (iconCodon.Resource); stream2x = iconCodon.Addin.GetResource2x (iconCodon.Resource); } else { var file = iconCodon.Addin.GetFilePath (iconCodon.File); stream = File.OpenRead (file); var file2x = Path.Combine (Path.GetDirectoryName (file), Path.GetFileNameWithoutExtension (file) + "@2x" + Path.GetExtension (file)); if (File.Exists (file2x)) stream2x = File.OpenRead (file2x); else { file2x = file + "@2x"; if (File.Exists (file2x)) stream2x = File.OpenRead (file2x); } } using (stream) { if (stream == null || stream.Length < 0) { LoggingService.LogError ("Did not find resource '{0}' in addin '{1}' for icon '{2}'", iconCodon.Resource, iconCodon.Addin.Id, iconCodon.StockId); return; } buffer = new byte [stream.Length]; stream.Read (buffer, 0, (int)stream.Length); } pixbuf = new Gdk.Pixbuf (buffer); using (stream2x) { if (stream2x != null && stream2x.Length >= 0) { buffer = new byte [stream2x.Length]; stream2x.Read (buffer, 0, (int)stream2x.Length); pixbuf2x = new Gdk.Pixbuf (buffer); } } } else if (!string.IsNullOrEmpty (iconCodon.IconId)) { var id = GetStockIdForImageSpec (iconCodon.Addin, iconCodon.IconId, iconCodon.IconSize); pixbuf = GetPixbuf (id, iconCodon.IconSize); pixbuf2x = Get2xIconVariant (pixbuf); // This may be an animation, get it animationFactory.TryGetValue (id, out animatedIcon); } else if (!string.IsNullOrEmpty (iconCodon.Animation)) { string id = GetStockIdForImageSpec (iconCodon.Addin, "animation:" + iconCodon.Animation, iconCodon.IconSize); pixbuf = GetPixbuf (id, iconCodon.IconSize); // This *should* be an animation animationFactory.TryGetValue (id, out animatedIcon); } Gtk.IconSize size = forceWildcard? Gtk.IconSize.Invalid : iconCodon.IconSize; if (pixbuf != null) AddToIconFactory (iconCodon.StockId, pixbuf, pixbuf2x, size); if (animatedIcon != null) AddToAnimatedIconFactory (iconCodon.StockId, animatedIcon); } catch (Exception ex) { LoggingService.LogError (string.Format ("Error loading icon '{0}'", iconCodon.StockId), ex); } }
static Xwt.Drawing.Image LoadStockIcon (StockIconCodon iconCodon, bool forceWildcard) { return LoadStockIcon (iconCodon.Addin, iconCodon.StockId, iconCodon.Resource, iconCodon.File, iconCodon.IconId, iconCodon.IconSize, iconCodon.Animation, forceWildcard); }
static Xwt.Drawing.Image LoadStockIcon (StockIconCodon iconCodon, bool forceWildcard) { try { Gdk.Pixbuf pixbuf = null, pixbuf2x = null; AnimatedIcon animatedIcon = null; Func<Stream[]> imageLoader = null; if (!string.IsNullOrEmpty (iconCodon.Resource) || !string.IsNullOrEmpty (iconCodon.File)) { // using the stream directly produces a gdk warning. byte[] buffer; if (iconCodon.Resource != null) { imageLoader = delegate { var stream = iconCodon.Addin.GetResource (iconCodon.Resource); var stream2x = iconCodon.Addin.GetResource2x (iconCodon.Resource); if (stream2x == null) return new [] { stream }; else return new [] { stream, stream2x }; }; } else { imageLoader = delegate { var file = iconCodon.Addin.GetFilePath (iconCodon.File); var stream = File.OpenRead (file); Stream stream2x = null; var file2x = Path.Combine (Path.GetDirectoryName (file), Path.GetFileNameWithoutExtension (file) + "@2x" + Path.GetExtension (file)); if (File.Exists (file2x)) stream2x = File.OpenRead (file2x); else { file2x = file + "@2x"; if (File.Exists (file2x)) stream2x = File.OpenRead (file2x); } if (stream2x == null) return new [] { stream }; else return new [] { stream, stream2x }; }; } var streams = imageLoader (); var st = streams[0]; var st2x = streams.Length > 1 ? streams[1] : null; using (st) { if (st == null || st.Length < 0) { LoggingService.LogError ("Did not find resource '{0}' in addin '{1}' for icon '{2}'", iconCodon.Resource, iconCodon.Addin.Id, iconCodon.StockId); return null; } buffer = new byte [st.Length]; st.Read (buffer, 0, (int)st.Length); } pixbuf = new Gdk.Pixbuf (buffer); using (st2x) { if (st2x != null && st2x.Length >= 0) { buffer = new byte [st2x.Length]; st2x.Read (buffer, 0, (int)st2x.Length); pixbuf2x = new Gdk.Pixbuf (buffer); } } } else if (!string.IsNullOrEmpty (iconCodon.IconId)) { var id = GetStockIdForImageSpec (iconCodon.Addin, iconCodon.IconId, iconCodon.IconSize); pixbuf = GetPixbuf (id, iconCodon.IconSize); pixbuf2x = Get2xIconVariant (pixbuf); // This may be an animation, get it animationFactory.TryGetValue (id, out animatedIcon); } else if (!string.IsNullOrEmpty (iconCodon.Animation)) { string id = GetStockIdForImageSpec (iconCodon.Addin, "animation:" + iconCodon.Animation, iconCodon.IconSize); pixbuf = GetPixbuf (id, iconCodon.IconSize); // This *should* be an animation animationFactory.TryGetValue (id, out animatedIcon); } Gtk.IconSize size = forceWildcard? Gtk.IconSize.Invalid : iconCodon.IconSize; if (pixbuf != null) AddToIconFactory (iconCodon.StockId, pixbuf, pixbuf2x, size); if (animatedIcon != null) AddToAnimatedIconFactory (iconCodon.StockId, animatedIcon); var img = Xwt.Toolkit.CurrentEngine.WrapImage (pixbuf); if (pixbuf2x != null) { var img2x = Xwt.Toolkit.CurrentEngine.WrapImage (pixbuf2x); img = Xwt.Drawing.Image.CreateMultiResolutionImage (new [] { img, img2x }); } if (imageLoader != null) img.SetStreamSource (imageLoader); return img; } catch (Exception ex) { LoggingService.LogError (string.Format ("Error loading icon '{0}'", iconCodon.StockId), ex); return null; } }