1 """This is an internal module. Do not use it. GTK 2.4 will contain functions
2 that replace those defined here."""
3 from __future__
import generators
9 theme_dirs
= [os
.path
.join(os
.environ
.get('HOME', '/'), '.icons')] + \
10 list(basedir
.load_data_paths('icons'))
12 def _ini_parser(stream
):
13 """Yields a sequence of (section, key, value) triples."""
17 if line
.startswith("#") or not line
: continue
18 if line
.startswith("[") and line
.endswith("]"):
21 key
, value
= map(str.strip
, line
.split('=', 1))
22 yield section
, key
, value
24 raise Exception("Error in file '%s': Expected '[SECTION]' but got '%s'" %
28 """A theme's index.theme file."""
29 def __init__(self
, dir):
32 for section
, key
, value
in _ini_parser(file(os
.path
.join(dir, "index.theme"))):
34 self
.sections
[section
][key
] = value
36 assert section
not in self
.sections
37 self
.sections
[section
] = {}
38 self
.sections
[section
][key
] = value
40 subdirs
= self
.get('Icon Theme', 'Directories')
42 subdirs
= subdirs
.replace(';', ',') # Just in case...
44 self
.subdirs
= [SubDir(self
, d
) for d
in subdirs
.split(',')]
46 def get(self
, section
, key
):
48 return self
.sections
.get(section
, {}).get(key
, None)
51 """A subdirectory within a theme."""
52 def __init__(self
, index
, subdir
):
53 icontype
= index
.get(subdir
, 'Type')
55 self
.size
= int(index
.get(subdir
, 'Size'))
56 if icontype
== "Fixed":
57 self
.min_size
= self
.max_size
= self
.size
58 elif icontype
== "Threshold":
59 threshold
= int(index
.get(subdir
, 'Threshold'))
60 self
.min_size
= self
.size
- threshold
61 self
.max_size
= self
.size
+ threshold
62 elif icontype
== "Scaled":
63 self
.min_size
= int(index
.get(subdir
, 'MinSize'))
64 self
.max_size
= int(index
.get(subdir
, 'MaxSize'))
66 self
.min_size
= self
.max_size
= 100000
69 """Icon themes are located by searching through various directories. You can use an IconTheme
70 to convert an icon name into a suitable image."""
72 def __init__(self
, name
):
73 """name = icon theme to load"""
76 def lookup_icon(self
, iconname
, size
, flags
=0):
77 """return path to required icon at specified size"""
80 def load_icon(self
, iconname
, size
, flags
=0):
81 """return gdk_pixbuf of icon"""
84 class IconThemeROX(IconTheme
):
85 """Icon themes are located by searching through various directories. You can use an IconTheme
86 to convert an icon name into a suitable image. This implementation is for PyGTK 2.0 or 2.2"""
88 def __init__(self
, name
):
92 IconTheme
.__init
__(self
, name
)
95 for leaf
in theme_dirs
:
96 theme_dir
= os
.path
.join(leaf
, name
)
97 index_file
= os
.path
.join(theme_dir
, 'index.theme')
98 if os
.path
.exists(os
.path
.join(index_file
)):
100 self
.indexes
.append(Index(theme_dir
))
102 rox
.report_exception()
104 def lookup_icon(self
, iconname
, size
, flags
=0):
105 icon
= self
._lookup
_this
_theme
(iconname
, size
)
109 def _lookup_this_theme(self
, iconname
, size
):
111 for i
in self
.indexes
:
113 if size
< d
.min_size
:
114 diff
= d
.min_size
- size
115 elif size
> d
.max_size
:
116 diff
= size
- d
.max_size
119 dirs
.append((diff
, os
.path
.join(i
.dir, d
.name
)))
121 # Sort by closeness of size
124 for _
, subdir
in dirs
:
125 for extension
in ("png", "svg"):
126 filename
= os
.path
.join(subdir
,
127 iconname
+ '.' + extension
)
128 if os
.path
.exists(filename
):
132 def load_icon(self
, iconname
, size
, flags
=0):
133 path
=self
.lookup_icon(iconname
, size
, flags
)
135 if hasattr(rox
.g
.gdk
, 'pixbuf_new_from_file_at_size'):
136 return rox
.g
.gdk
.pixbuf_new_from_file_at_size(path
, size
, size
)
138 return rox
.g
.gdk
.pixbuf_new_from_file(path
)
141 class IconThemeGTK(IconTheme
):
142 """Icon themes are located by searching through various directories. You can use an IconTheme
143 to convert an icon name into a suitable image. This implementation is for PyGTK 2.4 or later"""
145 def __init__(self
, name
):
146 IconTheme
.__init
__(self
, name
)
149 self
.gtk_theme
=rox
.g
.icon_theme_get_default()
151 self
.gtk_theme
=rox
.g
.IconTheme()
152 self
.gtk_theme
.set_custom_theme(name
)
155 def lookup_icon(self
, iconname
, size
, flags
=0):
156 info
=self
.gtk_theme
.lookup_icon(iconname
, size
, flags
)
158 path
=info
.get_filename()
159 #if rox.g.pygtk_version[0]==2 and rox.g.pygtk_version[1]<4:
164 def load_icon(self
, iconname
, size
, flags
=0):
165 return self
.gtk_theme
.load_icon(iconname
, size
, flags
)
167 def get_theme(name
=None):
169 theme
=IconThemeGTK(name
)
171 theme
=IconThemeROX(name
)
175 rox_theme
= get_theme('ROX')
177 from rox
import options
178 ogrp
=options
.OptionGroup('ROX-Filer', 'Options', 'rox.sourceforge.net')
179 theme_name
= options
.Option('icon_theme', 'ROX', ogrp
)
180 ogrp
.notify(warn_unused
=False)
181 users_theme
= get_theme(theme_name
.value
)
183 users_theme
= rox_theme