示例#1
0
文件: Icon.cs 项目: balihb/basenji
        public Pixbuf Render(Widget w, Gtk.IconSize size)
        {
            // most basenji icons are gtk stock icons,
            // so try to render the icon via widget.RenderIcon() first
            // (RenderIcon return null if the stock_id wasn't knwon)
            Pixbuf pb = w.RenderIcon(this.name, size, string.Empty);

            if (pb == null)
            {
                // try to render the icon via Gtk.IconTheme
                if (Gtk.IconTheme.Default.HasIcon(this.name))
                {
                    pb = Gtk.IconTheme.Default.LoadIcon(this.name, IconUtils.GetIconSizeVal(size), 0);
                }
                else
                {
                    Debug.WriteLine(string.Format("Icon.Render(): Failed to render icon \"{0}\"", this.name));
                }
            }
            return(pb);
        }
示例#2
0
        public Pixbuf GetIcon(string mimeType, Gtk.IconSize size)
        {
            if (mimeType == null)
            {
                throw new ArgumentNullException("mimeType");
            }

            if (mimeType.Length == 0)
            {
                throw new ArgumentException("Argument is emtpy", "mimeType");
            }

            Pixbuf pb;
            string iconKey = mimeType + (int)size;

            if (mimeIconCache.TryGetValue(iconKey, out pb))
            {
                return(pb);
            }

            if (useCustomMimeIcons)
            {
                // render icons which are available in the custom theme
                Icon icon;
                if (customMimeMapping.TryGetIconForMimeType(mimeType, out icon))
                {
                    pb = icon.Render(widget, size);
                }
                else
                {
                    pb = defaultIcon.Render(widget, size);
                }
            }
            else
            {
                // render system mime icons dynamically
                Gtk.IconTheme iconTheme = Gtk.IconTheme.Default;
                string        iconName  = null;

                foreach (string name in ((GLib.ThemedIcon)GLib.Content.TypeGetIcon(mimeType)).Names)
                {
                    if (iconTheme.HasIcon(name))
                    {
                        iconName = name;
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(iconName))
                {
                    pb = iconTheme.LoadIcon(iconName, IconUtils.GetIconSizeVal(size), 0);
                }
                else
                {
                    Icon fbIcon;
                    if (fallbackIcons.TryGetValue(mimeType, out fbIcon))
                    {
                        iconName = fbIcon.Name;
                        pb       = fbIcon.Render(widget, size);
                    }
                    else
                    {
                        pb       = defaultIcon.Render(widget, size);
                        iconName = defaultIcon.Name;
                    }
                }
            }

            if (pb != null)
            {
                mimeIconCache.Add(iconKey, pb);
            }

            return(pb);
        }
示例#3
0
        public static void Load(string themePath)
        {
            if (themePath == null)
            {
                throw new ArgumentNullException("themePath");
            }

            if (themePath.Length == 0)
            {
                throw new ArgumentException("Argument is empty", "themePath");
            }

            // gtk requires an absolute path
            if (!Path.IsPathRooted(themePath))
            {
                throw new ArgumentException("Path must be absolute", "themePath");
            }

            if (!Directory.Exists(themePath))
            {
                throw new DirectoryNotFoundException(string.Format("Path to theme \"{0}\" not found", themePath));
            }

            //IconSize[]				  iconSizes   = (IconSize[])Enum.GetValues(typeof(IconSize));

            // all icon sizes the app uses
            IconSize[] iconSizes = { IconSize.Menu,                                     /* 16px */
                                     IconSize.LargeToolbar,                             /* 24px */
                                     IconSize.Button,                                   /* 24px */
                                     IconSize.Dialog                                    /* 48px */
            };

            Dictionary <string, string> iconNames = GetAllIconNames();
            IconFactory fac = new IconFactory();

            foreach (KeyValuePair <string, string> namePair in iconNames)
            {
                string  name = namePair.Key;
                string  nameInCustomTheme = namePair.Value;
                IconSet iconSet           = new IconSet();
                bool    setHasSources     = false;

                foreach (Gtk.IconSize size in iconSizes)
                {
                    int    sz       = IconUtils.GetIconSizeVal(size);
                    string fullPath = Path.Combine(Path.Combine(themePath, sz.ToString()), nameInCustomTheme);

                    if (!File.Exists(fullPath))
                    {
                        if (Global.EnableDebugging)
                        {
                            Debug.WriteLine(string.Format("IconTheme: could not find custom icon for \"{0}\" (size = {1}), using system default", name, sz));
                        }
                        continue;
                    }

                    IconSource source = new IconSource();

#if LOAD_PIXBUFS
                    source.Pixbuf = new Gdk.Pixbuf(fullPath);
#else
                    source.Filename = fullPath;
#endif

                    source.Size = size;
                    //source.IconName = name;
                    source.SizeWildcarded = false;

                    iconSet.AddSource(source);
                    setHasSources = true;
                }
                if (setHasSources)
                {
                    fac.Add(name, iconSet);
                }
            }

            fac.AddDefault();             // add icon factory to the apps default factories
        }