Esempio n. 1
0
        static Xwt.Drawing.Image LoadStockIcon(RuntimeAddin addin, string stockId, string resource, string imageFile, string iconId, Gtk.IconSize iconSize, string animation, bool forceWildcard)
        {
            try {
                AnimatedIcon    animatedIcon = null;
                Func <Stream[]> imageLoader  = null;

                Xwt.Drawing.Image img = null;

                if (!string.IsNullOrEmpty(resource) || !string.IsNullOrEmpty(imageFile))
                {
                    if (resource != null)
                    {
                        CustomImageLoader loader;
                        if (!imageLoaders.TryGetValue(addin, out loader))
                        {
                            loader = imageLoaders [addin] = new CustomImageLoader(addin);
                        }
                        img = Xwt.Drawing.Image.FromCustomLoader(loader, resource);
                    }
                    else
                    {
                        img = Xwt.Drawing.Image.FromFile(addin.GetFilePath(imageFile));
                    }
                }
                else if (!string.IsNullOrEmpty(iconId))
                {
                    var id = GetStockIdForImageSpec(addin, iconId, iconSize);
                    img = GetIcon(id, iconSize);
                    // This may be an animation, get it
                    animationFactory.TryGetValue(id, out animatedIcon);
                }
                else if (!string.IsNullOrEmpty(animation))
                {
                    string id = GetStockIdForImageSpec(addin, "animation:" + animation, iconSize);
                    img = GetIcon(id, iconSize);
                    // This *should* be an animation
                    animationFactory.TryGetValue(id, out animatedIcon);
                }

                if (animatedIcon != null)
                {
                    AddToAnimatedIconFactory(stockId, animatedIcon);
                }

                if (imageLoader != null)
                {
                    img.SetStreamSource(imageLoader);
                }

                return(img);
            } catch (Exception ex) {
                LoggingService.LogError(string.Format("Error loading icon '{0}'", stockId), ex);
                return(null);
            }
        }
Esempio n. 2
0
        public IAddinLocalizer CreateLocalizer(RuntimeAddin addin, NodeElement element)
        {
            string pkg = element.GetAttribute("catalog");

            if (pkg.Length == 0)
            {
                pkg = addin.Id;
            }
            string dir = element.GetAttribute("location");

            if (dir.Length == 0)
            {
                dir = "locale";
            }
            dir    = addin.GetFilePath(dir);
            domain = new GettextDomain();
            domain.Init(pkg, dir);
            return(this);
        }
Esempio n. 3
0
        static Xwt.Drawing.Image LoadStockIcon(RuntimeAddin addin, string stockId, string resource, string imageFile, string iconId, Gtk.IconSize iconSize, string animation, bool forceWildcard)
        {
            try {
                Gdk.Pixbuf      pixbuf = null, pixbuf2x = null;
                AnimatedIcon    animatedIcon = null;
                Func <Stream[]> imageLoader  = null;

                if (!string.IsNullOrEmpty(resource) || !string.IsNullOrEmpty(imageFile))
                {
                    // using the stream directly produces a gdk warning.
                    byte[] buffer;

                    if (resource != null)
                    {
                        imageLoader = delegate {
                            var stream   = addin.GetResource(resource);
                            var stream2x = addin.GetResource2x(resource);
                            if (stream2x == null)
                            {
                                return new [] { stream }
                            }
                            ;
                            else
                            {
                                return new [] { stream, stream2x }
                            };
                        };
                    }
                    else
                    {
                        imageLoader = delegate {
                            var    file     = addin.GetFilePath(imageFile);
                            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}'",
                                                    resource, addin.Id, 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(iconId))
                {
                    var id = GetStockIdForImageSpec(addin, iconId, iconSize);
                    pixbuf   = GetPixbuf(id, iconSize);
                    pixbuf2x = Get2xIconVariant(pixbuf);
                    // This may be an animation, get it
                    animationFactory.TryGetValue(id, out animatedIcon);
                }
                else if (!string.IsNullOrEmpty(animation))
                {
                    string id = GetStockIdForImageSpec(addin, "animation:" + animation, iconSize);
                    pixbuf = GetPixbuf(id, iconSize);
                    // This *should* be an animation
                    animationFactory.TryGetValue(id, out animatedIcon);
                }

                Gtk.IconSize size = forceWildcard? Gtk.IconSize.Invalid : iconSize;
                if (pixbuf != null)
                {
                    AddToIconFactory(stockId, pixbuf, pixbuf2x, size);
                }

                if (animatedIcon != null)
                {
                    AddToAnimatedIconFactory(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}'", stockId), ex);
                return(null);
            }
        }