From 83f98ab6b61f26db9efd218963fa714017bbbb8a Mon Sep 17 00:00:00 2001 From: Stephen Watson Date: Thu, 19 May 2005 21:00:00 +0000 Subject: [PATCH] Clean up of icon theme handling git-svn-id: https://rox.svn.sourceforge.net/svnroot/rox/trunk/ROX-Lib2@3939 66de3db3-b00d-0410-b41b-f4738ad19bea --- Help/Changes | 4 +++ python/rox/icon_theme.py | 71 +++++++++++++++++++++++++++++++++++++++++------- python/rox/saving.py | 24 ++++------------ 3 files changed, 70 insertions(+), 29 deletions(-) diff --git a/Help/Changes b/Help/Changes index 2a4b8af..5f82870 100644 --- a/Help/Changes +++ b/Help/Changes @@ -3,6 +3,10 @@ by Thomas Leonard http://rox.sourceforge.net +19-May-2005 +~~~~~~~~~~~ +Clean up of icon theme handling (Stephen Watson). + 18-May-2005 ~~~~~~~~~~~ Compatability with PyGTK 2.4 retaining backwards compatability with diff --git a/python/rox/icon_theme.py b/python/rox/icon_theme.py index 30111c1..65fb6af 100644 --- a/python/rox/icon_theme.py +++ b/python/rox/icon_theme.py @@ -68,14 +68,28 @@ class SubDir: class IconTheme: """Icon themes are located by searching through various directories. You can use an IconTheme to convert an icon name into a suitable image.""" - + def __init__(self, name): + """name = icon theme to load""" self.name = name - try: - self.gtk_theme=g.icon_theme_get_default() - except: - self.gtk_theme=None + def lookup_icon(self, iconname, size, flags=0): + """return path to required icon at specified size""" + pass + + def load_icon(self, iconname, size, flags=0): + """return gdk_pixbuf of icon""" + pass + +class IconThemeROX(IconTheme): + """Icon themes are located by searching through various directories. You can use an IconTheme + to convert an icon name into a suitable image. This implementation is for PyGTK 2.0 or 2.2""" + + def __init__(self, name): + if not name: + name='ROX' + + IconTheme.__init__(self, name) self.indexes = [] for leaf in theme_dirs: @@ -87,7 +101,7 @@ class IconTheme: except: rox.report_exception() - def lookup_icon(self, iconname, size): + def lookup_icon(self, iconname, size, flags=0): icon = self._lookup_this_theme(iconname, size) if icon: return icon # XXX: inherits @@ -115,9 +129,46 @@ class IconTheme: return filename return None - def load_icon(self, iconname, size, flags): - if self.gtk_theme: - return self.gtk_theme.load_icon(iconname, size, flags) + def load_icon(self, iconname, size, flags=0): + path=self.lookup_icon(iconname, size, flags) + if path: + if hasattr(rox.g.gdk, 'pixbuf_new_from_file_at_size'): + return rox.g.gdk.pixbuf_new_from_file_at_size(path, size, size) + else: + return rox.g.gdk.pixbuf_new_from_file(path) + return None + +class IconThemeGTK(IconTheme): + """Icon themes are located by searching through various directories. You can use an IconTheme + to convert an icon name into a suitable image. This implementation is for PyGTK 2.4 or later""" + + def __init__(self, name): + IconTheme.__init__(self, name) + + if not name: + self.gtk_theme=rox.g.icon_theme_get_default() + else: + self.gtk_theme=rox.g.IconTheme() + self.gtk_theme.set_custom_theme(name) + + + def lookup_icon(self, iconname, size, flags=0): + info=self.gtk_theme.lookup_icon(iconname, size, flags) + if info: + path=info.get_filename() + info.free() + return path return None -rox_theme = IconTheme('ROX') + def load_icon(self, iconname, size, flags=0): + return self.gtk_theme.load_icon(iconname, size, flags) + +def get_theme(name=None): + try: + theme=IconThemeGTK(name) + except: + theme=IconThemeROX(name) + + return theme + +rox_theme = get_theme('ROX') diff --git a/python/rox/saving.py b/python/rox/saving.py index 79e4339..0a39d59 100644 --- a/python/rox/saving.py +++ b/python/rox/saving.py @@ -37,7 +37,6 @@ def image_for_type(type, size=48, flags=0): from icon_theme import rox_theme media, subtype = type.split('/', 1) - at_size=hasattr(gdk, 'pixbuf_new_from_file_at_size') path = choices.load('MIME-icons', media + '_' + subtype + '.png') icon=None @@ -45,32 +44,19 @@ def image_for_type(type, size=48, flags=0): icon_name = 'mime-%s:%s' % (media, subtype) try: - icon=rox_theme.load_icon(icon_name, size, flags) - if icon: - return icon - except: - pass - - try: - path = rox_theme.lookup_icon(icon_name, 48) + path=rox_theme.lookup_icon(icon_name, size, flags) if not path: icon_name = 'mime-%s' % media - path = rox_theme.lookup_icon(icon_name, 48) + path = rox_theme.lookup_icon(icon_name, size) - try: - icon=rox_theme.load_icon(icon_name, size, - flags) - if icon: - return icon - except: - pass except: print "Error loading MIME icon" + if not path: path = choices.load('MIME-icons', media + '.png') if path: - if at_size: - return gdk.pixbuf_new_from_file_at_size(path, 48, 48) + if hasattr(gdk, 'pixbuf_new_from_file_at_size'): + return gdk.pixbuf_new_from_file_at_size(path, size, size) else: return gdk.pixbuf_new_from_file(path) else: -- 2.11.4.GIT