Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/canvas/IconItem.py
blob: b4ac20528994519918a8d263bdbff39c7c06be65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re

import gobject
import gtk
import goocanvas

from sugar.util import GObjectSingletonMeta

class IconCache(gobject.GObject):
	__metaclass__ = GObjectSingletonMeta

	def __init__(self):
		gobject.GObject.__init__(self)
		self._icons = {}

	def _create_icon(self, name, color, size):
		theme = gtk.icon_theme_get_default()
		info = theme.lookup_icon(name, size, 0)
		icon_file = open(info.get_filename(), 'r')
		data = icon_file.read()
		icon_file.close()

		if color != None:
			style = '.fill {fill:%s;stroke:%s;}' % (color, color)
			data = re.sub('\.fill \{.*\}', style, data)

			style = '.shape {stroke:%s;fill:%s;}' % ('black', 'black')
			data = re.sub('\.shape \{.*\}', style, data)

			style = '.shape-and-fill {fill:%s; stroke:%s;}' % (color, 'black')
			data = re.sub('\.shape-and-fill \{.*\}', style, data)

		loader = gtk.gdk.pixbuf_loader_new_with_mime_type('image/svg-xml')
		loader.set_size(size, size)
		loader.write(data)
		loader.close()
		
		return loader.get_pixbuf()

	def get_icon(self, name, color, size):
		key = (name, color, size)
		if self._icons.has_key(key):
			return self._icons[key]
		else:
			icon = self._create_icon(name, color, size)
			self._icons[key] = icon
			return icon

class IconItem(goocanvas.Image):
	def __init__(self, icon_name, color, size, **kwargs):
		goocanvas.Image.__init__(self, **kwargs)

		icon_cache = IconCache()
		pixbuf = icon_cache.get_icon(icon_name, color, size)
		self.set_property('pixbuf', pixbuf)