3
// Copyright (C) 2008 Patrick Ulbrich
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
// TODO: faster? more mem usage? longer startup?
20
/*#define LOAD_PIXBUFS*/
24
using System.Collections.Generic;
25
using System.Reflection;
28
using Platform.Common.Diagnostics;
30
namespace Basenji.Icons
32
public static class CustomIconTheme
34
public static void Load(string themePath) {
35
if (themePath == null)
36
throw new ArgumentNullException("themePath");
38
if (themePath.Length == 0)
39
throw new ArgumentException("Argument is empty", "themePath");
41
// gtk requires an absolute path
42
if (!Path.IsPathRooted(themePath))
43
throw new ArgumentException("Path must be absolute", "themePath");
45
if (!Directory.Exists(themePath))
46
throw new DirectoryNotFoundException(string.Format("Path to theme \"{0}\" not found", themePath));
48
//IconSize[] iconSizes = (IconSize[])Enum.GetValues(typeof(IconSize));
50
// all icon sizes the app uses
51
IconSize[] iconSizes = { IconSize.Menu, /* 16px */
52
IconSize.LargeToolbar, /* 24px */
53
IconSize.Button, /* 24px */
54
IconSize.Dialog /* 48px */
57
Dictionary<string, string> iconNames = GetAllIconNames();
58
IconFactory fac = new IconFactory();
60
foreach (KeyValuePair<string, string> namePair in iconNames) {
62
string name = namePair.Key;
63
string nameInCustomTheme = namePair.Value;
64
IconSet iconSet = new IconSet();
65
bool setHasSources = false;
67
foreach (Gtk.IconSize size in iconSizes) {
69
int sz = IconUtils.GetIconSizeVal(size);
70
string fullPath = Path.Combine(Path.Combine(themePath, sz.ToString()), nameInCustomTheme);
72
if (!File.Exists(fullPath)) {
73
if (Global.EnableDebugging) {
74
Debug.WriteLine(string.Format("IconTheme: could not find custom icon for \"{0}\" (size = {1}), using system default", name, sz));
79
IconSource source = new IconSource();
82
source.Pixbuf = new Gdk.Pixbuf(fullPath);
84
source.Filename = fullPath;
88
//source.IconName = name;
89
source.SizeWildcarded = false;
91
iconSet.AddSource(source);
95
fac.Add(name, iconSet);
98
fac.AddDefault(); // add icon factory to the apps default factories
101
private static Dictionary<string, string> GetAllIconNames() {
102
Dictionary<string, string> names = new Dictionary<string, string>();
104
Type t = typeof(Icons.Icon);
105
PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Static | BindingFlags.Public);
106
foreach(PropertyInfo pi in propInfos) {
107
if (pi.PropertyType == typeof(Icons.Icon)) {
109
Icon icon = (Icon)pi.GetValue(null, null);
110
// get name in a custom theme
111
object[] attribs = pi.GetCustomAttributes(typeof(Icons.NameInCustomIconThemeAttribute), true);
112
if (attribs.Length == 0)
113
throw new NotImplementedException(string.Format("Property \"{0}\" does not have the NameInCustomIconThemeAttribute", pi.Name));
114
Icons.NameInCustomIconThemeAttribute attr = (Icons.NameInCustomIconThemeAttribute)attribs[0];
116
names.Add(icon.Name, attr.Name);
123
// public static class IconLoader
125
// private static string customThemePath;
127
// private static bool enableCaching;
128
// private static Dictionary<string, Pixbuf> iconCache;
130
// static IconLoader() {
131
// enableCaching = false;
132
// iconCache = new Dictionary<string, Pixbuf>();
135
// public static string CustomThemePath {
136
// get { return customThemePath; }
137
// set { customThemePath = value; }
140
// public static bool EnableCaching {
141
// get { return enableCaching; }
142
// set { enableCaching = value; }
145
// public static void ClearCache() {
146
// iconCache.Clear();
149
// public static Pixbuf LoadIcon(string name, Gtk.IconSize size) {
151
// int sz = GetSize(size);
152
// string iconKey = name + sz;
154
// if (enableCaching && iconCache.TryGetValue(iconKey, out icon))
157
// if (string.IsNullOrEmpty(customThemePath))
158
// icon = LoadIconFromSystemTheme(name, sz); // use system theme
160
// icon = LoadIconFromCustomTheme(name, sz);
162
// if (enableCaching) {
163
// iconCache.Add(iconKey, icon);
164
// Debug.WriteLine(string.Format("IconLoader cached icon \"{0}\" (size = {1})", name, sz));
170
// private static Pixbuf LoadIconFromSystemTheme(string name, int size) {
171
// return Gtk.IconTheme.Default.LoadIcon(name, size, 0);
174
// private static Pixbuf LoadIconFromCustomTheme(string name, int size) {
175
// if (!Directory.Exists(customThemePath))
176
// throw new DirectoryNotFoundException(string.Format("Path to custom theme \"{0}\" not found", customThemePath));
178
// string iconPath = Path.Combine(Path.Combine(customThemePath, size.ToString()), name);
179
// return new Pixbuf(iconPath);
182
// private static int GetSize(Gtk.IconSize size) {
185
// case Gtk.IconSize.Button:
188
// case Gtk.IconSize.Dialog:
191
// case Gtk.IconSize.Dnd:
194
// case Gtk.IconSize.LargeToolbar:
197
// case Gtk.IconSize.Menu:
200
// case Gtk.IconSize.SmallToolbar: