Implemented theme-bg option.
[drlaunch] / src / icon.py
index f6a6099..d88a119 100755 (executable)
@@ -60,7 +60,13 @@ def getIcon(name, iconsize):
     if ico==None:
        ico=getIconPath(idef, iconsize)
 
-    ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, iconsize, iconsize)
+    try:
+       ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, iconsize, iconsize)
+    except:
+       # On error use the default icon
+       ico=getIconPath(idef, iconsize)
+       ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, iconsize, iconsize)
+       print "Icon with unhandled format:", iname
 
     return(ret)
 
@@ -74,6 +80,8 @@ class Icon(gobject.GObject):
        self.name=None
        self.icon=None
         self.sicon=None
+       self.sthemebg1=None
+       self.sthemebg2=None
        self.lastpress=0
        self.ispressed=False
 
@@ -186,6 +194,35 @@ class Icon(gobject.GObject):
 
         return(s)
 
+    def get_sthemebg(self, pressed):
+       """ Return the theme's background icon as a surface. Cache it. """
+       if not pressed and self.sthemebg1!=None:
+           return(self.sthemebg1)
+       if pressed and self.sthemebg2!=None:
+           return(self.sthemebg2)
+
+       fn="/etc/hildon/theme/images/"
+       if pressed:
+           fn+="ApplicationShortcutAppletPressed.png"
+       else:
+           fn+="ApplicationShortcutApplet.png"
+
+       w=self.config.themebgsize
+       buf=gtk.gdk.pixbuf_new_from_file_at_size(fn, w, w)
+       s=cairo.ImageSurface(cairo.FORMAT_ARGB32, w, w)
+       cr0=cairo.Context(s)
+       cr=gtk.gdk.CairoContext(cr0)
+
+       cr.set_source_pixbuf(buf, 0, 0)
+       cr.paint()
+
+       if not pressed:
+           self.sthemebg1=s
+       else:
+           self.sthemebg2=s
+
+       return(s)
+
     def get_sicon(self):
        """ Return the icon as a surface. Cache it. """
        if self.sicon!=None:
@@ -224,11 +261,25 @@ class Icon(gobject.GObject):
        cr=gtk.gdk.CairoContext(cr0)
 
        # Paint the background
-        s2=self.mkbg(t)
-       cr.save()
-       cr.set_source_surface(s2, 0, 0)
-       cr.paint()
-       cr.restore()
+       if self.config.getNoBg():
+           pass
+       elif self.config.getThemeBg():  # Use theme bg
+           s2=self.get_sthemebg(pressed)
+
+           # have in mind the size difference of iconsize+iconspace with
+           # the fixed themebgsize
+           xy0=int((w-self.config.themebgsize)/2)
+
+           cr.save()
+           cr.set_source_surface(s2, xy0, xy0)
+           cr.paint()
+           cr.restore()
+       else:
+           s2=self.mkbg(t)
+           cr.save()
+           cr.set_source_surface(s2, 0, 0)
+           cr.paint()
+           cr.restore()
 
        # If there is no icon then don't do anything more
        if self.icon!=None:
@@ -336,8 +387,9 @@ class Icon(gobject.GObject):
            return
 
        if self.draw_queued:
-           print "queued"
+#          print "queued"
            return
+
        self.draw_queued=True
        w=self.config.iconsize + self.config.iconspace
        rect=gdk.Rectangle(self.x, self.y, w, w)