Example #1
0
        public static QIcon LoadIcon(string name)
        {
            QIcon icon = new QIcon();
            int[] sizes = null;

            // FIXME: Need to remove Gtk dependency.
            if (Gtk.IconTheme.Default != null) {
                sizes = Gtk.IconTheme.Default.GetIconSizes(name);
                if (sizes != null && sizes.Length > 0) {
                    foreach (int size in sizes) {
                        var iconInfo = Gtk.IconTheme.Default.LookupIcon(name, size, 0);
                        if (iconInfo != null)
                            icon.AddFile(iconInfo.Filename, new QSize(size, size), QIcon.Mode.Normal, QIcon.State.On);
                    }
                }
            }

            // If icon wasn't found in theme, try loading from resource instead...
            if (sizes == null || sizes.Length == 0 || icon.IsNull()) {
                var assembly = Assembly.GetExecutingAssembly();
                foreach (string resourceName in assembly.GetManifestResourceNames()) {
                    string pattern =  "^" + Regex.Escape(name) + @"__(\d+)\.png$";
                    var match = Regex.Match(resourceName, pattern);
                    if (match.Success) {
                        icon.AddPixmap(new QPixmap("resource:/" + resourceName), QIcon.Mode.Normal, QIcon.State.On);
                    }
                }

                if (icon.IsNull()) {
                    Console.WriteLine(String.Format("Icon not found: {0}", name));
                }
            }

            return icon;
        }