~ubuntu-branches/ubuntu/quantal/basenji/quantal

« back to all changes in this revision

Viewing changes to Basenji/src/Icons/CustomIconTheme.cs

  • Committer: Package Import Robot
  • Author(s): Patrick Ulbrich
  • Date: 2011-10-02 18:01:08 UTC
  • Revision ID: package-import@ubuntu.com-20111002180108-alcxkgmgv0oj7ipq
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// CustomIconTheme.cs
 
2
// 
 
3
// Copyright (C) 2008 Patrick Ulbrich
 
4
//
 
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.
 
9
//
 
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.
 
14
//
 
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/>.
 
17
//
 
18
 
 
19
// TODO: faster? more mem usage? longer startup?
 
20
/*#define LOAD_PIXBUFS*/
 
21
 
 
22
using System;
 
23
using System.IO;
 
24
using System.Collections.Generic;
 
25
using System.Reflection;
 
26
using Gtk;
 
27
using Gdk;
 
28
using Platform.Common.Diagnostics;
 
29
 
 
30
namespace Basenji.Icons
 
31
{        
 
32
        public static class CustomIconTheme
 
33
        {
 
34
                public static void Load(string themePath) {
 
35
                        if (themePath == null)
 
36
                                throw new ArgumentNullException("themePath");
 
37
                        
 
38
                        if (themePath.Length == 0)
 
39
                                throw new ArgumentException("Argument is empty", "themePath");
 
40
                        
 
41
                        // gtk requires an absolute path
 
42
                        if (!Path.IsPathRooted(themePath))
 
43
                                throw new ArgumentException("Path must be absolute", "themePath");
 
44
                                
 
45
                        if (!Directory.Exists(themePath))
 
46
                                throw new DirectoryNotFoundException(string.Format("Path to theme \"{0}\" not found", themePath));
 
47
 
 
48
                        //IconSize[]                              iconSizes   = (IconSize[])Enum.GetValues(typeof(IconSize));
 
49
                        
 
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 */
 
55
                                                                        };
 
56
                        
 
57
                        Dictionary<string, string>      iconNames       = GetAllIconNames();                    
 
58
                        IconFactory                                     fac                     = new IconFactory();                    
 
59
 
 
60
                        foreach (KeyValuePair<string, string> namePair in iconNames) {
 
61
                                
 
62
                                string  name                            = namePair.Key;
 
63
                                string  nameInCustomTheme       = namePair.Value;
 
64
                                IconSet iconSet                         = new IconSet();
 
65
                                bool    setHasSources           = false;
 
66
                                
 
67
                                foreach (Gtk.IconSize size in iconSizes) {
 
68
 
 
69
                                        int    sz               = IconUtils.GetIconSizeVal(size);
 
70
                                        string fullPath = Path.Combine(Path.Combine(themePath, sz.ToString()), nameInCustomTheme);
 
71
 
 
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));
 
75
                                                }
 
76
                                                continue;
 
77
                                        }
 
78
                                        
 
79
                                        IconSource source = new IconSource();
 
80
                                        
 
81
#if LOAD_PIXBUFS
 
82
                                        source.Pixbuf = new Gdk.Pixbuf(fullPath);
 
83
#else
 
84
                                        source.Filename = fullPath;
 
85
#endif
 
86
                                        
 
87
                                        source.Size = size;
 
88
                                        //source.IconName = name;
 
89
                                        source.SizeWildcarded = false;
 
90
                                        
 
91
                                        iconSet.AddSource(source);
 
92
                                        setHasSources = true;
 
93
                                }
 
94
                                if (setHasSources)
 
95
                                        fac.Add(name, iconSet);
 
96
                        }
 
97
 
 
98
                        fac.AddDefault(); // add icon factory to the apps default factories
 
99
                }
 
100
                
 
101
                private static Dictionary<string, string> GetAllIconNames() {
 
102
                        Dictionary<string, string> names = new Dictionary<string, string>();
 
103
                        
 
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)) {
 
108
                                        // get default name                                        
 
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];
 
115
 
 
116
                                        names.Add(icon.Name, attr.Name);
 
117
                                }
 
118
                        }
 
119
                        return names;
 
120
                }
 
121
        }
 
122
        
 
123
//        public static class IconLoader
 
124
//        {
 
125
//                private static string customThemePath;
 
126
//                
 
127
//                private static bool enableCaching;
 
128
//                private static Dictionary<string, Pixbuf> iconCache;
 
129
//                
 
130
//                static IconLoader() {
 
131
//                        enableCaching = false;
 
132
//                        iconCache = new Dictionary<string, Pixbuf>();
 
133
//                }
 
134
//                
 
135
//                public static string CustomThemePath {
 
136
//                        get { return customThemePath; }
 
137
//                        set { customThemePath = value; }
 
138
//                }
 
139
//                
 
140
//                public static bool EnableCaching {
 
141
//                        get { return enableCaching; }
 
142
//                        set { enableCaching = value; }
 
143
//                }
 
144
//                
 
145
//                public static void ClearCache() {
 
146
//                        iconCache.Clear();            
 
147
//                }
 
148
//                
 
149
//                public static Pixbuf LoadIcon(string name, Gtk.IconSize size) {
 
150
//                        Pixbuf icon;
 
151
//                        int sz = GetSize(size);
 
152
//                        string iconKey = name + sz;
 
153
//                        
 
154
//                        if (enableCaching && iconCache.TryGetValue(iconKey, out icon))
 
155
//                                return icon;
 
156
//
 
157
//                        if (string.IsNullOrEmpty(customThemePath))
 
158
//                                icon = LoadIconFromSystemTheme(name, sz); // use system theme
 
159
//                        else
 
160
//                                icon = LoadIconFromCustomTheme(name, sz);
 
161
//                        
 
162
//                        if (enableCaching) {                    
 
163
//                                iconCache.Add(iconKey, icon);
 
164
//                                Debug.WriteLine(string.Format("IconLoader cached icon \"{0}\" (size = {1})", name, sz));
 
165
//                        }
 
166
//                        
 
167
//                        return icon;
 
168
//                }
 
169
//                
 
170
//                private static Pixbuf LoadIconFromSystemTheme(string name, int size) {
 
171
//                        return Gtk.IconTheme.Default.LoadIcon(name, size, 0);            
 
172
//                }
 
173
//                
 
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));
 
177
//                        
 
178
//                        string iconPath = Path.Combine(Path.Combine(customThemePath, size.ToString()), name);
 
179
//                        return new Pixbuf(iconPath);
 
180
//                }
 
181
//                
 
182
//                private static int GetSize(Gtk.IconSize size) {
 
183
//                        int sz;
 
184
//                        switch (size) {
 
185
//                                case Gtk.IconSize.Button:
 
186
//                                        sz = 20;
 
187
//                                        break;
 
188
//                                case Gtk.IconSize.Dialog:
 
189
//                                        sz = 48;
 
190
//                                        break;
 
191
//                                case Gtk.IconSize.Dnd:
 
192
//                                        sz = 32;
 
193
//                                        break;
 
194
//                                case Gtk.IconSize.LargeToolbar:
 
195
//                                        sz = 24;
 
196
//                                        break;
 
197
//                                case Gtk.IconSize.Menu:
 
198
//                                        sz = 16;
 
199
//                                        break;
 
200
//                                case Gtk.IconSize.SmallToolbar:
 
201
//                                        sz = 18;
 
202
//                                        break;
 
203
//                                default:
 
204
//                                        sz = 16;
 
205
//                                        break;
 
206
//                        }
 
207
//                        return sz;
 
208
//                }
 
209
//        }
 
210
}