diff --git a/shell/HomeWindow.py b/shell/HomeWindow.py index ee567818..1609fe78 100644 --- a/shell/HomeWindow.py +++ b/shell/HomeWindow.py @@ -1,6 +1,8 @@ import gtk import goocanvas +from sugar.canvas.IconItem import IconItem + class Model(goocanvas.CanvasModelSimple): def __init__(self): goocanvas.CanvasModelSimple.__init__(self) @@ -9,7 +11,10 @@ class Model(goocanvas.CanvasModelSimple): item = goocanvas.Rect(x=0, y=0, width=693, height=520, fill_color="red") + root.add_child(item) + item = IconItem('buddy') + #item.set_color('blue') root.add_child(item) class HomeWindow(gtk.Window): diff --git a/sugar/canvas/IconItem.py b/sugar/canvas/IconItem.py new file mode 100644 index 00000000..54697b67 --- /dev/null +++ b/sugar/canvas/IconItem.py @@ -0,0 +1,40 @@ +import re + +import gtk +import goocanvas + +class IconItem(goocanvas.Image): + def __init__(self, icon_name): + goocanvas.Image.__init__(self) + + self._icon_name = icon_name + self._color = None + + # FIXME changing the icon color will cause + # the svg to be read in memory and rendered + # two times. + self._update_pixbuf() + + def set_parent(self, parent): + goocanvas.Image.set_parent(self, parent) + + def set_color(self, color): + self._color = color + self._update_pixbuf() + + def _update_pixbuf(self): + theme = gtk.icon_theme_get_default() + info = theme.lookup_icon(self._icon_name, 48, 0) + icon_file = open(info.get_filename(), 'r') + data = icon_file.read() + icon_file.close() + + if self._color != None: + style = '.icon-color {fill: %s;}' % self._color + data = re.sub('\.icon-color \{.*\}', style, data) + + loader = gtk.gdk.pixbuf_loader_new_with_mime_type('image/svg+xml') + loader.write(data) + loader.close() + + self.set_property('pixbuf', loader.get_pixbuf()) diff --git a/sugar/canvas/Makefile.am b/sugar/canvas/Makefile.am new file mode 100644 index 00000000..9120ba25 --- /dev/null +++ b/sugar/canvas/Makefile.am @@ -0,0 +1,4 @@ +sugardir = $(pythondir)/sugar/canvas +sugar_PYTHON = \ + __init__.py \ + IconItem.py diff --git a/sugar/canvas/__init__.py b/sugar/canvas/__init__.py new file mode 100644 index 00000000..e69de29b