![]() |
2008-11-13
, 13:15
|
|
Posts: 273 |
Thanked: 104 times |
Joined on Mar 2007
@ Manitoba, Canada
|
#42
|
![]() |
2008-11-22
, 08:21
|
Posts: 1,038 |
Thanked: 737 times |
Joined on Nov 2005
@ Helsinki
|
#43
|
![]() |
2010-01-29
, 19:56
|
|
Posts: 1,111 |
Thanked: 1,985 times |
Joined on Aug 2009
@ Åbo, Finland
|
#44
|
Hmm.. someone needs to make an example applet with cairo and mouse click handling so that we can start simulating button behavior by using pixmaps/vectors.
![]() |
2010-01-29
, 21:10
|
|
Posts: 1,111 |
Thanked: 1,985 times |
Joined on Aug 2009
@ Åbo, Finland
|
#45
|
# hildon home widget, png button example import hildondesktop import gtk import cairo imagepath = "/home/user/comic-widget/images/" class ExamplePlugin(hildondesktop.HomePluginItem): def __init__(self): hildondesktop.HomePluginItem.__init__(self) self.set_size_request(96, 97) # make the widget a nice size. # Interestingly, having the exactly correct amount of space, makes it not work properly, hence the 1 pixel extra. self.vbox = gtk.VBox() self.hbox = gtk.HBox() # Horisontally arranged box to put your buttons in self.hbox.set_size_request(96, 48) screen = self.get_screen() colormap = screen.get_rgba_colormap() self.set_colormap(colormap) self.set_app_paintable(True) self.label = gtk.Label() self.label.set_markup("<small>Buttons</small>") # The event boxes. self.e_text = gtk.EventBox() self.e_text.set_name('text') self.e_text.set_size_request(96, 48) self.e_next = gtk.EventBox() self.e_next.set_name('next') # The name is nice to have if you want to link it to a file name for example. self.e_next.set_size_request(48, 48) # I have 48x48px buttons :) self.e_prev = gtk.EventBox() self.e_prev.set_name('prev') self.e_prev.set_size_request(48, 48) # Add the label: self.e_text.add(self.label) # Preload som images an throw them in a dict. # I don't know if this actually helps make things snappy, but it was the simplest way I could think of. self.images = { 'next0':cairo.ImageSurface.create_from_png(imagepath + "next0-48x48.png"), 'next1':cairo.ImageSurface.create_from_png(imagepath + "next1-48x48.png"), 'prev0':cairo.ImageSurface.create_from_png(imagepath + "prev0-48x48.png"), 'prev1':cairo.ImageSurface.create_from_png(imagepath + "prev1-48x48.png") } #***************************************************************** # now here's the important part: make some events. # I have the same event for release and leave because my event handler checks if it was a real click or a bailout self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY) self.e_next.connect("button-press-event", self.button_press) self.e_next.connect("button-release-event", self.button_release) self.e_next.connect("leave-notify-event", self.button_release) self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY) self.e_prev.connect("button-press-event", self.button_press) self.e_prev.connect("button-release-event", self.button_release) self.e_prev.connect("leave-notify-event", self.button_release) # now let's put those eventboxes in the hbox self.vbox.pack_end(self.hbox,False,False,0) self.vbox.pack_end(self.e_text,False,False,0) self.hbox.pack_start(self.e_prev,False,False,0) # No padding for me, since I have a tight squeeze to get the buttons in. self.hbox.pack_end(self.e_next,False,False,0) self.hbox.show_all() #now add the stuff to the widget, and show it. (it's empty though) self.vbox.show_all() self.add(self.vbox) #now some functions. #expose function, runs automagically when the widget feels exposed. def do_expose_event(widget, event): cr = widget.window.cairo_create() # create a cairo thingamabob widget.draw(widget.e_prev,0) # call my show button function, first arg is where to put image, and second is which image. widget.draw(widget.e_next,0) # again for the other image # paint some background. cr.set_operator(cairo.OPERATOR_SOURCE) region = gtk.gdk.region_rectangle(event.area) # limit what to color cr.region(region) # bg_color=gtk.gdk.color_parse('#000000') cr.set_source_rgba (0.0, 0.0, 0.0, 0.5) # Black, with 50% opacity. Nice! cr.fill_preserve() # lets fill it with that. return False # Function that draws a button preloaded in the images dictionary. def draw(self, target, postfix): cr_e = target.window.cairo_create() cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # If you, like I do, have transparency in your buttons, cr_e.set_operator(cairo.OPERATOR_SOURCE) # you need to paint the background first. cr_e.paint() cr_e.set_operator(cairo.OPERATOR_OVER) # Not sure about this, but it works. # Now I have set it up so that the key in the dict to cairo imagesurface object is the name of the eventbox + a number. # 0 for idle, 1 for pressed.. cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0) # here's where the magic is done cr_e.paint() # now the event handlers # I don't want to do anything other than switch image on press down. def button_press(self, widget, event): # this is needed because the second click can trigger a _2BUTTON_PRESS # and the third a _3BUTTON_PRESS, along with the regular BUTTON_PRESS # and I only want to switch images once. if not event.type == gtk.gdk.BUTTON_PRESS: return False self.draw(widget, "1") #call the draw function, switch to image 1 which means pressed # then the button_release event. def button_release(self, widget, event): #check if it was released or moved out. if event.type == gtk.gdk.BUTTON_RELEASE: # here is where you actually do whatever the button is for. I'll print something. print "Success! The button was clicked!" else: # if you want to do something with a button click that was bailed from, that would be here. print "You almost clicked that button, but then you dragged away at the last minute!" # then call the draw function, regardless of the type of release. # I think is nice to do it after the action, so that you get a visual queue # when the thing that should be done is done. At least it's nice in my widget. self.draw(widget, "0") # register the plugin. hd_plugin_type = ExamplePlugin # This bit below is so that you can run the widget standalone. if __name__ == "__main__": import gobject gobject.type_register(hd_plugin_type) obj = gobject.new(hd_plugin_type, plugin_id="plugin_id") obj.show_all() gtk.main()
![]() |
2011-02-02
, 18:29
|
Posts: 87 |
Thanked: 46 times |
Joined on Nov 2010
@ lisbon, portugal
|
#46
|
![]() |
2011-02-02
, 20:49
|
|
Posts: 1,637 |
Thanked: 4,424 times |
Joined on Apr 2009
@ Germany
|
#47
|
The Following 2 Users Say Thank You to nicolai For This Useful Post: | ||
![]() |
2011-02-02
, 22:38
|
Posts: 87 |
Thanked: 46 times |
Joined on Nov 2010
@ lisbon, portugal
|
#48
|
![]() |
2011-02-03
, 19:40
|
Posts: 87 |
Thanked: 46 times |
Joined on Nov 2010
@ lisbon, portugal
|
#49
|
![]() |
2011-02-04
, 11:26
|
Posts: 87 |
Thanked: 46 times |
Joined on Nov 2010
@ lisbon, portugal
|
#50
|
Benoît HERVIER, Khertan
KhtEditor - Sdist_Maemo - Khweeteur - PyPackager - KhtSimpleText - KhtNotes - KhtBMA - Wleux - and more ...