maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Shaped widgets? (transparent background) (https://talk.maemo.org/showthread.php?t=16720)

SpiritGeek 2008-02-16 13:33

Shaped widgets? (transparent background)
 
I'm trying to create a button that is the shape of its image (instead of rectangular). From my research, it seems that the way to do this is with shape_combine_mask, which makes all the non-mask parts of the button transparent and thus displays in the desired shape.

However, I'm not getting anywhere in actually making it work. Here's my best guess (in Python):

Code:

PixBuf = gtk.gdk.pixbuf_new_from_file("image.png")
Image = gtk.image_new_from_pixbuf(PixBuf)
Button = gtk.Button()
Button.set_image(Image)
MapMask = PixBuf.render_pixmap_and_mask()
Mask = MapMask[1]
self.add(Button)
Button.shape_combine_mask(Mask, 0, 0)
Button.show()
self.show()

This gets me a button with the correct image, but no masking. Can anybody spot what I'm doing wrong?

Thanks!

pycage 2008-02-18 09:33

Re: Shaped widgets? (transparent background)
 
Make sure that the Pixbuf has an alpha channel. And you may have to give some alpha threshold value in render_pixmap_and_mask(threshold).
Pixbufs support 256 levels of transparency while a mask only knows two levels (transparent and opaque).
The threshold determines where the distinction is made between transparent and opaque. The default value that is used when not specifiying a threshold is 127, which means that only pixels below 50% transparency will be transparent in the mask.

icer 2008-02-19 12:32

Re: Shaped widgets? (transparent background)
 
Hi, did you get it working? Can you explain what you did to us?

How would I setup a frame background so that it is semitransparent? I'm thinking about desktop homeitem now. Hmm... I think I have to test what happens when I add labels on that frame box too. :confused:

SpiritGeek 2008-02-19 12:41

Re: Shaped widgets? (transparent background)
 
Quote:

Originally Posted by icer (Post 144588)
Hi, did you get it working? Can you explain what you did to us?

How would I setup a frame background so that it is semitransparent? I'm thinking about desktop homeitem now. Hmm... I think I have to test what happens when I add labels on that frame box too. :confused:

My hope was also for a desktop applet, but I still haven't been able to get anything working.

I'm wondering if perhaps it's a limitation of the platform. I notice that the weather applet seems to capture the surrounding background and draw it across itself, so perhaps that is a possible solution.

If anybody finds a working setup, we'd love to hear about it. :)

icer 2008-02-20 10:51

Re: Shaped widgets? (transparent background)
 
This is not directly related to this issue but I thought I post this here anyway.

So I was struggling to get an image to show up on the applet. I used the set_from_file method and pyphantom IDE(see maemo garage if you want to use it) for developing the home item. While developing in IDE I put the images on src or pixmaps directory. If I put the images on src then I just used the image name: image.set_from_file("lake.png"). However if I used the pixmaps folder I had to supply a relative path: image.set_from_file("../pixmaps/lake.png"). Then the images would show up on the IDE.

Now I ran a test on the IT and the images did not show up. I opened the python file with vi and setup an absolute path for the images. Now they show up on IT too. IIRC the images were installed on the /usr/share/pixmaps/applicationname folder.

Ok... back to topic. I read on Khertans blog that he is studying the transparency too.

I tried to fiddle with the transparency too. There are some examples for python applications about it. Though not directly for home applets. What I found out is that in one step I was setting a rgba colormap but that method did not work. Here are few lines as an example:
e = gtk.EventBox()
rgba = gtk.gdk.Screen().get_rgba_colormap()
e.set_colormap(rgba)

So the code breaks on that last line. and the error I get is:
TypeError: GtkWidget.set_colormap() argument 1 must be gtk.gdk.Colormap, not None

So maybe the colormap methods are not ready or that particular way of coding cannot be applied in this context. I don't know.

Khertan 2008-02-20 11:20

Re: Shaped widgets? (transparent background)
 
Quote:

rgba = gtk.gdk.Screen().get_rgba_colormap()
e.set_colormap(rgba)
Hum you are lucky ... gtk.gdk.Screen().get_rgba_colormap() give me a segfault !

SpiritGeek 2008-02-20 11:34

Re: Shaped widgets? (transparent background)
 
Quote:

Originally Posted by icer (Post 145177)
I tried to fiddle with the transparency too. There are some examples for python applications about it. Though not directly for home applets. What I found out is that in one step I was setting a rgba colormap but that method did not work. Here are few lines as an example:
e = gtk.EventBox()
rgba = gtk.gdk.Screen().get_rgba_colormap()
e.set_colormap(rgba)

So the code breaks on that last line. and the error I get is:
TypeError: GtkWidget.set_colormap() argument 1 must be gtk.gdk.Colormap, not None

So maybe the colormap methods are not ready or that particular way of coding cannot be applied in this context. I don't know.

From the documentation for gtk.gdk.Screen.get_rgba_colormap

Quote:

Returns : a gtk.gdk.Colormap for use with windows with an alpha channel or None if the capability is not available.
It's returning None, so it sounds like the IT doesn't support gtk.gdk.Screen alphas. But other desktop applets have transparent backgrounds (or are at least emulating them somehow), so we know it can be done. It's just a question of can it be done via the Python bindings?

icer 2008-02-20 13:46

Re: Shaped widgets? (transparent background)
 
Lol... :) ok. I tried it as you said:
...
rgba = gtk.gdk.Screen.get_rgba_colormap()

Now it says:
TypeError: descriptor 'get_rgba_colormap' of 'gtk.gdk.Screen' object needs an argument

But in the API it does not say anything about arguments. So doesn't look good really.

Khertan 2008-02-20 15:18

Re: Shaped widgets? (transparent background)
 
But if you find a solution spiritgeek i 'm interested, i m looking how to do transparent python applet since a few weeks now !

SpiritGeek 2008-02-20 22:01

Re: Shaped widgets? (transparent background)
 
It looks like the IT is handling the "home" portion of the desktop different than other areas of the desktop. The code for my task navigator panel sample produces a button with a transparent background without any problem. But the exact same code as a home applet (just changing hildondesktop.TaskNavigatorItem to hildondesktop.HomeItem) produces a button with a white background. I don't know why it's different, but we may have to figure that out to see if there's a way around it.

icer 2008-02-21 07:25

Re: Shaped widgets? (transparent background)
 
Hi,

I have not tested if I get anything appearing on the screen but at least python is happy with it:
Code:

import gtk
import hildondesktop
e = gtk.eventbox()
screen = e.get_screen()
rgba = gtk.gdk.Screen.get_rgba_colormap(screen)
e.set_colormap(rgba)

So this is just a small piece of incomplete code but it's accepted by the interpreter. I got some example code from somewhere, I really don't remember where I got it, anyway in it the screen line was commented out.
Also in pyphantom the code does not work but in the IT itself it's just fine.

wnd 2008-02-21 16:28

Re: Shaped widgets? (transparent background)
 
I know this thread started with code snippet in Python, but if you're interested, I wrote a simple example in C. It should show, among some other things, how to use real transparency in a Desktop plugin.

SpiritGeek 2008-02-22 01:29

Re: Shaped widgets? (transparent background)
 
Quote:

Originally Posted by wnd (Post 145792)
I know this thread started with code snippet in Python, but if you're interested, I wrote a simple example in C. It should show, among some other things, how to use real transparency in a Desktop plugin.

Thank you!

I'm not a C programmer, but I've tried to sift through your code and find the Python parallels. If I'm understanding it correctly, it looks like the initialization code (__init__ in Python) is example_init(). In there, the relevant bit for transparency seems to be:

Code:

rgba_colormap = gdk_screen_get_rgba_colormap(gdk_screen_get_default());
       
if (rgba_colormap != NULL) {
        gtk_widget_set_colormap(GTK_WIDGET(plugin), rgba_colormap);
}

Which I translate to Python as:

Code:

rgba_colormap = gtk.gdk.screen_get_default().get_rgba_colormap()

if rgba_colormap != None:
        self.set_colormap(rgba_colormap)

This seems to return the screen color map OK, but when it's applied to the plugin, the whole desktop crashes. So clearly I'm missing something.

Anyone have any thoughts?

icer 2008-02-26 07:42

Re: Shaped widgets? (transparent background)
 
Looks like I'm stuck with this. The code is kind of alright but does not work. I need a working example of a complete plugin...
I've been reading these:
GTK images: http://www.pygtk.org/pygtk2tutorial/sec-Images.html
window compositing: http://library.gnome.org/devel/pygtk...gdkwindow.html
Lucas's blog: http://blogs.gnome.org/lucasr/2007/0...ildon-desktop/

However Khertan used frame as the 'root' element or window and not gtk window as the base element and all these examples are based on using windows.

Only the gtk tutorials have complete code and don't have the hildon features obviously and I could not find complete program from Lucas either.:confused::(

Well the frame looks a bit dull but I can live with it.

Anybody know how to stack two images? You know for example I have one image as background and the other image is drawn over the background image.

icer 2008-02-28 15:15

Re: Shaped widgets? (transparent background)
 
This pygtk faq looks promising: http://faq.pygtk.org/index.py?req=sh...=faq08.018.htp
I did some tests and I think it sort of works. Ran a test on the IDE. I need to retest on the IT and also do other tests...

I made a composite image of 2 png's but the other image was much smaller than the other so it appeared in the top left corner in the composite image.

Anyway I'm not sure how much this concerns the original question about the background of the home plugin.

And here is an example how to setup a window background:http://faq.pygtk.org/index.py?req=sh...=faq08.013.htp
Except that so far we have used frames.

hns 2008-03-06 15:18

Re: Shaped widgets? (transparent background)
 
Quote:

Originally Posted by SpiritGeek (Post 143208)
However, I'm not getting anywhere in actually making it work. Here's my best guess (in Python):

Code:

PixBuf = gtk.gdk.pixbuf_new_from_file("image.png")
Image = gtk.image_new_from_pixbuf(PixBuf)
Button = gtk.Button()
Button.set_image(Image)
MapMask = PixBuf.render_pixmap_and_mask()
Mask = MapMask[1]
self.add(Button)
Button.shape_combine_mask(Mask, 0, 0)
Button.show()
self.show()

This gets me a button with the correct image, but no masking. Can anybody spot what I'm doing wrong?

I found that shape_combine_mask() works, but you have to apply it to the container, not the button (which I think is transparent by default).

So replacing Button.shape_combine_mask(Mask, 0, 0) with self.shape_combine_mask(Mask, 0, 0) should do the trick, I think.

hns 2008-03-06 15:40

Re: Shaped widgets? (transparent background)
 
1 Attachment(s)
Googling on this topic, I came across this example of semi-transparent windows with cairo, and to my utter amazement it works perfectly on OS2008!

So I went ahead and remodeled this clock widget to a desktop maemo plugin.

Here's how it looks (it's resizable too!)

Attachment 1060

And this is the code - following SpiritGeeks example I'll just paste it in :)

/usr/share/applications/hildon-home/pyclock.desktop:
Code:

[Desktop Entry]
Name=Python Cairo Clock
Type=python
X-Path=pyclock

/usr/lib/hildon-desktop/pyclock.py:
Code:

import sys
import gobject
import pango
import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk
import hildondesktop
import cairo
from datetime import datetime
import gobject
import math


# set this to False to disable display of seconds and update
# only once per minute (saves CPU cycles on the tablet)
enable_seconds = True

supports_alpha = False


class PyClock(hildondesktop.HomeItem):
        def __init__(self):
                hildondesktop.HomeItem.__init__(self)
                               
                self.set_resize_type(hildondesktop.HOME_ITEM_RESIZE_BOTH)
                self.set_size_request(140, 140)
                self.connect("expose-event", self.expose)
                self.connect("screen-changed", self.screen_changed)
                self.connect ("background", self.set_timer, False)
                self.connect ("foreground", self.set_timer, True)
                self.connect ("unrealize", self.unrealize)

                # set a timeout to update the clock, depending
                # on whether we are in the foreground or background
                self.timer = None
                self.set_timer(self, True)

                self.show_all()


        def expose(self, widget, event):

                global supports_alpha

                width, height = self.allocation[2], self.allocation[3]

                #Get a cairo context
                cr = widget.window.cairo_create()

                if supports_alpha == True:
                        cr.set_source_rgba(1.0, 1.0, 1.0, 0.0) # Transparent
                else:
                        cr.set_source_rgb(1.0, 1.0, 1.0) # Opaque white

                # Draw the background
                cr.set_operator(cairo.OPERATOR_SOURCE)
                cr.paint()

                #And draw everything we want
                #Some cos/sin magic is done, never mind
                if supports_alpha == True:
                        cr.set_source_rgba(1.0, 1.0, 1.0, 0.5)
                else:
                        cr.set_source_rgb(1.0, 1.0, 1.0)                       
                if width < height:
                        radius = float(width)/2 - 0.8
                else:
                        radius = float(height)/2 - 0.8
                       
                cr.arc(float(width)/2, float(height)/2, radius, 0, 2.0*3.14)
                cr.fill()
                cr.stroke()
       
                if supports_alpha == True:
                        cr.set_source_rgba(0.0, 0.0, 0.0, 1.0)
                else:
                        cr.set_source_rgb(0.0, 0.0, 0.0)

                cr.set_line_width(0.07 * radius)
                cr.move_to(-0.05 * radius + float(width/2 + radius), float(height)/2)
                cr.rel_line_to(-0.1 * radius, 0)
                cr.stroke()
                cr.move_to(float(width)/2, -0.05 * radius + float(height/2 + radius))
                cr.rel_line_to(0, -0.1 * radius)
                cr.stroke()
                cr.move_to(0.05 * radius + float(width/2 - radius), float(height)/2)
                cr.rel_line_to(0.1 * radius, 0)
                cr.stroke()
                cr.move_to(float(width)/2, 0.05 * radius + float(height/2 - radius))
                cr.rel_line_to(0, 0.1 * radius)
                cr.stroke()

                time = datetime.now()
                hour = time.hour
                minutes = time.minute
                seconds = time.second
                per_hour = (2 * 3.14) / 12
                dh = (hour * per_hour) + ((per_hour / 60) * minutes)
                dh += 2 * 3.14 / 4
                cr.set_line_width(0.07 * radius)
                cr.move_to(float(width)/2, float(height)/2)
                cr.rel_line_to(-0.6 * radius * math.cos(dh), -0.6 * radius * math.sin(dh))
                cr.stroke()
                per_minute = (2 * 3.14) / 60
                dm = minutes * per_minute
                dm += 2 * 3.14 / 4
                cr.set_line_width(0.05 * radius)
                cr.move_to(float(width)/2, float(height)/2)
                cr.rel_line_to(-0.9 * radius * math.cos(dm), -0.9 * radius * math.sin(dm))
                cr.stroke()
                # only draw seconds when in foreground
                if enable_seconds: # disable seconds, to much work for little tablets
                        per_second = (2 * 3.14) / 60
                        ds = seconds * per_second
                        ds += 2 * 3.14 / 4
                        cr.set_line_width(0.02 * radius)
                        cr.move_to(float(width)/2, float(height)/2)
                        cr.rel_line_to(-0.9 * radius * math.cos(ds), -0.9 * radius * math.sin(ds))
                        cr.stroke()
               
                cr.arc(float(width)/2, float(height)/2, 0.1 * radius, 0, 2.0*3.14)
                cr.fill()
                cr.stroke()
               
                #Once everything has been drawn, create our XShape mask
                #Our window content is contained inside the big circle,
                #so let's use that one as our mask
                """pm = gtk.gdk.Pixmap(None, width, height, 1)
                pmcr = pm.cairo_create()
                pmcr.arc(float(width)/2, float(height)/2, radius, 0, 2.0*3.14)
                pmcr.fill()
                pmcr.stroke()
                #Apply input mask
                self.input_shape_combine_mask(pm, 0, 0)"""
       
                return False


        def screen_changed(self, widget, old_screen=None):
                global supports_alpha
                # print "screen changed"
               
                # To check if the display supports alpha channels, get the colormap
                screen = self.get_screen()
                colormap = screen.get_rgba_colormap()
                if colormap == None:
                        # print 'Your screen does not support alpha channels!'
                        colormap = screen.get_rgb_colormap()
                        supports_alpha = False
                else:
                        # print 'Your screen supports alpha channels!'
                        supports_alpha = True
   
                # Now we have a colormap appropriate for the screen, use it
                self.set_colormap(colormap)
   
                return False
       
        def unrealize(self, widget, date=None):
                # cancel timeout
                if self.timer:
                        v = gobject.source_remove(self.timer)
                        print "canceled pyclock timeout:", v
                        self.timer = None


        def set_timer(self, widget, on):
                # when called first time from __init__ widget is None
                if self.timer != None:
                        # print "removing old timer"
                        gobject.source_remove(self.timer)
                if on:
                        # print "creating new timer"
                        delay = 1000 if enable_seconds else 60000
                        self.timer = gobject.timeout_add(delay, self.update)
                        # repaint immediately when coming to the foreground
                        self.update()


        def update(self):
                # print "updating pyclock"
                self.queue_draw()
                return True


def hd_plugin_get_objects():
        plugin = PyClock()
        return [plugin]

Remember that you need the hildondesktop python bindings installed which aren't in the main repositories yet. Happy hacking!

icer 2008-03-07 08:09

Re: Shaped widgets? (transparent background)
 
Thanks a lot. I tried it out and it works! Though only in device not on pyphantom ide. Well I guess it doesn't matter as long as it works on IT.

Now just need to learn that cairo mumbo jumbo.

I got a python program ready for integration with GUI and a complete GUI concept ready too but looks like it's not that trivial task after all.

Anyway thanks for the example! :)

hns 2008-03-07 09:55

Re: Shaped widgets? (transparent background)
 
Quote:

Originally Posted by icer (Post 151862)
Now just need to learn that cairo mumbo jumbo.

Well you can use Cairo just for transparency and widget shape, and than use ordinary GTK widgets on top of that. For example, here's a version of Khertan's HomeIP plugin that is rendered on a semitransparent black rectangle with rounded corners :)

Code:

import gtk
import hildondesktop
import cairo
import gobject
import pango
import commands
import traceback

supports_alpha = False

class IPPlugin(hildondesktop.HomeItem):
        def __init__(self):
                hildondesktop.HomeItem.__init__(self)
               
                self.label = gtk.Label()
                self.label.set_use_markup(True)
                self.label.modify_font(pango.FontDescription ("Sans 18"))
                self.label.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("#000"))
                self.label.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("#fff"))
                self.label.set_text(self.get_ip())
                self.label.set_padding(15, 10)
               
                self.connect("expose-event", self.expose)
                self.connect("screen-changed", self.screen_changed)
                self.add(self.label)
                self.show_all()
               
                # set a timeout to change the images
                self.timer = gobject.timeout_add(2000, self.update)
                print "foreground timer", self.timer
               
                self.connect ("background", self.background)
                self.connect ("foreground", self.foreground)



        def expose(self, widget, event):
                cr = self.window.cairo_create()
                if supports_alpha == True:
                        cr.set_source_rgba(1.0, 1.0, 1.0, 0.0) # Transparent
                else:
                        cr.set_source_rgb(1.0, 1.0, 1.0) # Opaque white

                # 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 ()

                cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
                cr.fill_preserve ()
                # cr.set_source_rgba (0.5, 0.5, 1.0, 0.8)
                # cr.stroke ()


        def screen_changed(self, widget, old_screen=None):
                global supports_alpha
   
                # To check if the display supports alpha channels, get the colormap
                screen = widget.get_screen()
                colormap = screen.get_rgba_colormap()
                if colormap == None:
                        print 'Your screen does not support alpha channels!'
                        colormap = screen.get_rgb_colormap()
                        supports_alpha = False
                else:
                        print 'Your screen supports alpha channels!'
                        supports_alpha = True
   
                # Now we have a colormap appropriate for the screen, use it
                widget.set_colormap(colormap)
   
                return False


        def background(self, widget, data=None):
                #Avoid refresh when applet is in background
                self.label.set_text("refreshing")
                if self.timer != None:
                        gobject.source_remove(self.timer)
                        self.timer = None

        def foreground(self, widget, data=None):
                self.label.set_text(self.get_ip())
                self.timer = gobject.timeout_add(2000, self.update)
                print "foreground timer", self.timer

        def unrealize(self, widget, date=None):
                # cancel timeout
                v = gobject.source_remove(self.timer)
                print "canceled homeip timeout:", v

        def update(self):
                self.label.set_text(self.get_ip())
               
        def get_ip(self):
                #CPU Speed
                text = "No IP"
                devs = commands.getoutput("/sbin/ifconfig wlan0")
                sdevs = devs.rsplit('\n')
                for stdev in sdevs:
                        #sdev = re.findall("(\S*)\s*", stdev)
                        if 'inet addr:' in stdev:
                                sdev = stdev.rsplit(':')
                                sdev2 = sdev[1].rsplit(' ')
                                text= "IP: " + sdev2[0]
                return text
                               
def hd_plugin_get_objects():
        plugin = IPPlugin()
        return [plugin]


Khertan 2008-03-07 10:32

Re: Shaped widgets? (transparent background)
 
Hey great !

Just a small thing ... my plugin is just an example, but if you use it ... sometimes label is too short for displaying the entire ip. (because it can be created with 'No IP') ... so just do a set_size_request(-1,-1) on it.

And THANKS ! I ll try this, maybe now will could have usefull and beautifful widget in python.

hns 2008-03-07 10:55

Re: Shaped widgets? (transparent background)
 
Well, thanks go right back to you! I connect to the tablet via SSH all the time, and even though it's always the same 2 or 3 addresses, this little widget has made my life so much easier. :)

icer 2008-03-07 11:55

Re: Shaped widgets? (transparent background)
 
Hey thanks for yet another great example! :) I too will use it for sure.

Can you explain a bit this code? Is it so that the method screen_changed is executed every time the window or widget is moved on the desktop? The background method is activated once the screen dims and the IT goes to power save mode? And the foreground is activated once the user does something which brings the IT out of the power save mode? Is that unrealize method used for cleanup when terminating the widget? update is used to periodically fetch the IP.

Ok... how would I execute my separate python program and wait for it to output a dictionary? It can take some time before there is any output. I use the dictionary for setting text to the labels on the desktop plugin.

Found out howto display an image, though I have tested this in desktop PC and don't have access to the IT atm... anyway here's the code:
Code:

        image = cairo.ImageSurface.create_from_png("image.png")
        cr.set_source_surface(image, 10, 10)
        cr.paint()

Really simple. Next need to try it on IT and move on trying out image compositing and coding the main program integration. :)

Almost forgot to mention that this piece of code is inserted in the expose event method.

... and compositing works so that I just draw the second image over first image. :) Dunno if that is really compositing but I get the desired effect done.

vash42 2008-03-17 17:28

Re: Shaped widgets? (transparent background)
 
Hi all,
your examples are amazing. I would want to create a application with a transparent background, but not a widget. I mean, not only on the desktop. Is it possible with python and cairo?
Thanks

icer 2008-03-17 19:55

Re: Shaped widgets? (transparent background)
 
Why not. Go see www.cairographics.org
I think they have examples there.

vash42 2008-03-19 08:21

Re: Shaped widgets? (transparent background)
 
Yes I tried cairo but it needs a compositing manager to manage the transparency, so impossible on maemo.

darethehair 2008-03-20 18:23

Re: Shaped widgets? (transparent background)
 
I hope it is not considered 'stealing' of this thread, but I have a question that is very close to the one that was posed here -- I simply want to create a button and supply my own image for it. Techniques in pygtk that I use on my desktop work just fine, but when I run them on my N800, the image is not shown (and no errors). I have tried various techniques with no luck. For example, this works on my desktop but not my N800:

path = os.path.dirname(os.path.abspath(sys.argv[0]))
image_file = os.path.join(path, "mephem_672x422.jpg")
image = gtk.Image()
image.set_from_file(image_file)
image.show

b=gtk.Button()
window.add(b)
b.connect("clicked",chart_object_info_basic,object ,local)
b.set_image(image)
#b.set_image(gtk.image_new_from_file(image_file))
b.set_size_request(672,422)
b.set_label('')
b.show()

One interesting thing I noticed is that the 'set_size_request' command is not needed on my desktop, but it is needed on the N800 -- otherwise an error occurs:

GtkWarning: gtk_widget_size_allocate(): attempt to allocate widget with width -8 and height 0

Thanks

EDIT: It looks like the "set_label('')" was messing me up, so I took it out! Strange that there are these subtle behavior differences between the desktop and N800...

devDiablo 2008-07-17 11:38

Re: Shaped widgets? (transparent background)
 
Hi, I'm starting working with N800 device.

I have read this post and I've a little trouble. I'm trying to make a desktop applet with transparent background, but it fails when I try to start it. I create a box (instead of the label in the IP example) and I fill it with gtk.Entry() and gtk.Button() objects. If I fill it with labels and no one of that objects it runs fine.

Can anyone help me?
Thanks!

konttori 2008-09-23 11:01

Re: Shaped widgets? (transparent background)
 
I was also looking into the exactly same thing of adding buttons to the view. With labels, it runs fine, but with buttons, it crashes the desktop.

Sergio 2008-10-15 17:13

Re: Shaped widgets? (transparent background)
 
The same problem for me, It only works well with labels: No buttons, no TreeView

What could be the problem?

darethehair 2008-10-20 20:46

Re: Shaped widgets? (transparent background)
 
Hi folks:

I am also trying to write a desktop Python applet, and the 'cairo' stuff works fine -- but I want to add some 'buttons' to the application. Can anyone give me some *simple* code to show a mixture of the two in the same applet? Or am I going to run into the same label/button problem as is being mentioned above?

It *appears* as though my request to add buttons is being totally ignored -- but I have noticed strange behavior (sometimes) when something is wong on an applet i.e. it almost looks like it uses a previously-worked 'cached' version instead of the current one in that case (????).

Sergio 2008-10-20 22:03

Re: Shaped widgets? (transparent background)
 
All the code samples that I've seen on the net are using only labels. If I just replace the label for a simple bottom it crash, and for now, nobody reply with any solution, so maybe it's a bug on Python libhildondesktop bindings and can't be done with current version.

darethehair 2008-10-21 16:34

Re: Shaped widgets? (transparent background)
 
Hmmm...unless I am mistaken, this current 'limitation' of the Python libhildondesktop bindings means that there is no way to 'influence' the behavior of a desktop applet once it is running i.e. no buttons, menus, etc. to change settings. True?

yerga 2008-10-21 18:27

Re: Shaped widgets? (transparent background)
 
Quote:

Originally Posted by Sergio (Post 235181)
All the code samples that I've seen on the net are using only labels. If I just replace the label for a simple bottom it crash, and for now, nobody reply with any solution, so maybe it's a bug on Python libhildondesktop bindings and can't be done with current version.

I had this experiment running some time ago: http://www.flickr.com/photos/yerga/2265719734/


It has a button but it hasn't transparency. I have the code in somewhere, I will have to search and test it. If it works I will come here with an example (on the weekend).

konttori 2008-10-22 17:31

Re: Shaped widgets? (transparent background)
 
It might either be bug in the python implementation or in the actual implementation. Can't tell. I can try to ask Rodrigo though.

darethehair 2008-10-24 14:53

Re: Shaped widgets? (transparent background)
 
While we are waiting (hopefully) for a solution to this matter, I was wondering if it was possible to have the transparent widgets respond to *other* events e.g. mouse clicks, mouse drags, focus, etc.?

I am a total newbie at this stuff, but I tried adding this code to my app and it was (apparently) ignored:

self.connect("button-press-event", self.toggle_text)
self.connect("drag-begin", self.toggle_text)
self.connect("enter-notify-event", self.toggle_text)
self.connect("focus", self.toggle_text)

def toggle_text(self, widget, event):

if text_flag == "on":
text_flag = "off"
else:
text_flag = "on"

self.update()

return

Even though I could click, drag, etc the widget, it didn't appear as though my 'text_toggle' module was being called. I also tried adding this:

self.add_events(gtk.gdk.KEY_PRESS_MASK |
gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.BUTTON_PRESS_MASK |
gtk.gdk.SCROLL_MASK)

...but that didn't work either. You folks are a lot smarter than me, so what am I doing wrong? Or is this another limitation?

Thanks

yerga 2008-10-25 13:46

Re: Shaped widgets? (transparent background)
 
I tested it and here are my conclusions:

* You can add buttons when you have a bored applet without transparency.
* Same problem of yours if the applet have transparency.

darethehair 2008-10-25 17:01

Re: Shaped widgets? (transparent background)
 
Yerga:

Using the examples of 'transparent shaped widgets' from this current thread, can you tell me (offhand) what minimal amount would need to be *removed* (i.e. 'transparency') in order for buttons to work? Also, can you post an example (code) of such a widget?

My own applet is stalled out -- waiting for some sort of resolution. Otherwise, I would need to release it without any 'controls' at all (buttons, menus, etc). :(

yerga 2008-10-27 18:29

Re: Shaped widgets? (transparent background)
 
Quote:

Originally Posted by darethehair (Post 236596)
Yerga:

Using the examples of 'transparent shaped widgets' from this current thread, can you tell me (offhand) what minimal amount would need to be *removed* (i.e. 'transparency') in order for buttons to work? Also, can you post an example (code) of such a widget?

My own applet is stalled out -- waiting for some sort of resolution. Otherwise, I would need to release it without any 'controls' at all (buttons, menus, etc). :(

The not-transparent applet was written from scratch, not from the transparent examples, is the following code, is a simple label+button gtk example:
Code:

import gtk
import hildondesktop

class IPPlugin(hildondesktop.HomeItem):
        def __init__(self):
                hildondesktop.HomeItem.__init__(self)

                self.vbox = gtk.VBox()
                self.vbox.set_size_request(200, 100)
                self.label = gtk.Label()
                self.label.set_text('A label')
                self.vbox.pack_start(self.label, 8)
               
                self.button = gtk.Button('Press')       
                self.button.connect("clicked", self.press_button)
                self.vbox.pack_start(self.button, True, True, 0)
               
                self.add(self.vbox)
                self.show_all()
               
        def press_button(self, widget):
                self.label.set_text('Button pressed!')   

def hd_plugin_get_objects():
        plugin = IPPlugin()
        return [plugin]

I think if you decide not use transparency to have buttons (or other widgets) is better write it with simple gtk without use the cairo examples.

Other thing you could look, is add an option in the Home menu to control the applet settings but I don't know how it works.

Khertan 2008-11-12 09:09

Re: Shaped widgets? (transparent background)
 
You can look in source code of the last HomeIP or other Home* applet i ve made. Source is available in extras repository.

If you need explain just ask, but it s in fact very easy to do.

darethehair 2008-11-12 13:35

Re: Shaped widgets? (transparent background)
 
Khertan:

I think I briefly looked at your apps in the past for the 'secret' -- but do your apps use 'shaped' widgets? I definitely want to integrate Cairo (drawing) into my own.


All times are GMT. The time now is 11:31.

vBulletin® Version 3.8.8