Changed 'Dismiss' to 'Close' (Chris Shaffer).
[rox-lib.git] / python / rox / icon_theme.py
blob9b9225202be9326c62d3ca336f36eb9870f4aff6
1 """This is an internal module. Do not use it. GTK 2.4 will contain functions
2 that replace those defined here."""
4 import os
5 import basedir
6 import rox
8 theme_dirs = [os.path.join(os.environ.get('HOME', '/'), '.icons')] + \
9 list(basedir.load_data_paths('icons'))
11 class Index:
12 def __init__(self, dir):
13 self.dir = dir
14 sections = file(os.path.join(dir, "index.theme")).read().split('\n[')
15 self.sections = {}
16 for s in sections:
17 lines = s.split('\n')
18 sname = lines[0].strip('[] \t')
19 section = self.sections[sname] = {}
20 for line in lines[1:]:
21 if not line.strip(): continue
22 if line.startswith('#'): continue
23 key, value = map(str.strip, line.split('=', 1))
24 section[key] = value
26 self.subdirs = [SubDir(self, d) for
27 d in self.get('Icon Theme', 'Directories').split(';')]
29 def get(self, section, key):
30 "None if not found"
31 return self.sections.get(section, {}).get(key, None)
33 class SubDir:
34 def __init__(self, index, subdir):
35 type = index.get(subdir, 'Type')
36 self.name = subdir
37 self.size = int(index.get(subdir, 'Size'))
38 if type == "Fixed":
39 self.min_size = self.max_size = self.size
40 elif type == "Threshold":
41 threshold = int(index.get(subdir, 'Threshold'))
42 self.min_size = self.size - threshold
43 self.max_size = self.size + threshold
44 elif type == "Scaled":
45 self.min_size = int(index.get(subdir, 'MinSize'))
46 self.max_size = int(index.get(subdir, 'MaxSize'))
47 else:
48 self.min_size = self.max_size = 100000
50 class IconTheme:
51 def __init__(self, name):
52 self.name = name
54 self.indexes = []
55 for dir in theme_dirs:
56 theme_dir = os.path.join(dir, name)
57 index_file = os.path.join(theme_dir, 'index.theme')
58 if os.path.exists(os.path.join(index_file)):
59 try:
60 self.indexes.append(Index(theme_dir))
61 except:
62 rox.report_error()
64 def lookup_icon(self, iconname, size):
65 icon = self._lookup_this_theme(iconname, size)
66 if icon: return icon
67 # XXX: inherits
69 def _lookup_this_theme(self, iconname, size):
70 dirs = []
71 for i in self.indexes:
72 for d in i.subdirs:
73 if size < d.min_size:
74 diff = d.min_size - size
75 elif size > d.max_size:
76 diff = size - d.max_size
77 else:
78 diff = 0
79 dirs.append((diff, os.path.join(i.dir, d.name)))
81 # Sort by closeness of size
82 dirs.sort()
84 minimal_size = 1000000
85 for _, subdir in dirs:
86 for extension in ("png", "svg"):
87 filename = os.path.join(subdir,
88 iconname + '.' + extension)
89 if os.path.exists(filename):
90 return filename
92 rox_theme = IconTheme('ROX')