[maemo-developers] Semi-transparent background for Desktop Widget (Python + Cairo)

From: Marc Ordinas i Llopis marc.ordinasillopis at collabora.co.uk
Date: Mon Dec 21 12:05:55 EET 2009
Hi,

On 19/12/09 20:41, Brent Chiodo wrote:
> Hi,
>
> I'm trying to make the background of a Desktop Widget semi-transparent 
> using the cairo graphics library. The widget is written in Python and 
> the only examples of this I've found are using C (I don't know much C 
> -- I wasn't even able to apply the examples to Python).
>

I haven't tried in Python, but there's this blog entry by Zaheer Abbas 
Merali which explains how to do it:
http://zaheer.merali.org/articles/2009/10/12/maemo-widget-experience-circular-clock-with-transparent-background/

Hope that helps,
marcoil

> This is the current code (that doesn't work -- the background appears 
> as a black box)
>
>
>    def __init__(self):
>       hildondesktop.HomePluginItem.__init__(self)
> ...
>       self..connect("screen-changed", self.screen_changed)
>       self.connect("expose-event", self.expose)
>
>
>    def expose(self, widget, event):
>       cr = self.window.cairo_create()
>       cr.set_source_rgba(1.0, 1.0, 1.0, 0.0) # Transparent
>
>       # Draw the background
>       cr.set_operator(cairo.OPERATOR_SOURCE)
>       cr.paint()
>
>       # draw rounded rect
>       width, height = self.allocation[2], self.allocation[3]
>
>       #/* a custom shape, that could be wrapped in a function */
>       x0 = 0   #/*< parameters like cairo_rectangle */
>       y0 = 0
>
>       radius = min(15, width/2, height/2)  #/*< and an approximate 
> curvature radius */
>
>       x1 = x0 + width
>       y1 = y0 + height
>
>       cr.move_to  (x0, y0 + radius)
>       cr.arc (x0 + radius, y0 + radius, radius, 3.14, 1.5 * 3.14)
>       cr.line_to (x1 - radius, y0)
>       cr.arc (x1 - radius, y0 + radius, radius, 1.5 * 3.14, 0.0)
>       cr.line_to (x1 , y1 - radius)
>       cr.arc (x1 - radius, y1 - radius, radius, 0.0, 0.5 * 3.14)
>       cr.line_to (x0 + radius, y1)
>       cr.arc (x0 + radius, y1 - radius, radius, 0.5 * 3.14, 3.14)
>
>       cr.close_path ()
>
>       bg_color=gtk.gdk.color_parse("#000000")
>
>       cr.set_source_rgba (bg_color.red / 65535.0, 
> bg_color.green/65535.0, bg_color.blue/65535.0, float(32767/65535.0))
>       cr.fill_preserve ()
>       # cr.set_source_rgba (0.5, 0.5, 1.0, 0.8)
>       # cr.stroke ()
>
>    def screen_changed(self, widget):
>       screen = self.get_screen()
>       colormap = screen.get_rgba_colormap()
>       self.set_colormap(colormap)
>
>       return False
>
>
> What I'm trying to do is have the widget appear semi-transparent with 
> rounded corners (like most widgets look) but the above code does not 
> work at all. I do not know much about cairo and got most of that code 
> from Khertan's HomeIP widget for Diablo/Chinook and only modified it a 
> little to work with Maemo 5.
>
> Any help would be greatly appreciated. I've been "banging my head 
> against a wall" for a while with this one.
>
> -- 
> Best Regards,
>
> Brent Chiodo
>
>
> _______________________________________________
> maemo-developers mailing list
> maemo-developers at maemo.org
> https://lists.maemo.org/mailman/listinfo/maemo-developers
>    

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maemo.org/pipermail/maemo-developers/attachments/20091221/6a47e78b/attachment.htm 
More information about the maemo-developers mailing list