GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Dec 20, 2014 2:25 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: long press support in gtk
PostPosted: Tue Jun 04, 2013 4:05 am 
Offline
Familiar Face

Joined: Thu Nov 01, 2012 5:48 am
Posts: 30
Hi ,

Could any one tell , how to implement long press in gtk+ applications .

Regards
hkishn


Top
 Profile  
 
 Post subject: Re: long press support in gtk
PostPosted: Sat Aug 10, 2013 10:21 pm 
Offline
Familiar Face

Joined: Tue Aug 06, 2013 1:09 pm
Posts: 6
I don't know what the context is here because you don't say, however you could set up button-press-event and button-release-event handlers on an arbitrary widgets with a timeout:

Code:
#!/usr/bin/python
# CC0/public domain, Andrew Chadwick, 2013
# http://creativecommons.org/publicdomain/zero/1.0/

import gi
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib

class LongPressLabel (Gtk.Label):

    def __init__(self, *args, **kwargs):
        Gtk.Label.__init__(self, *args, **kwargs)
        self.set_has_window(True)
        self._long_press_srcid = None
        self.connect("button-press-event", self._button_down)
        self.connect("button-release-event", self._button_up)
        self.add_events(  Gdk.EventMask.BUTTON_PRESS_MASK
                        | Gdk.EventMask.BUTTON_RELEASE_MASK )

    def _button_down(self, widget, event):
        if self._long_press_srcid:
            return
        srcid = GLib.timeout_add(1200, self._longpress_activated_cb)
        self._long_press_srcid = srcid

    def _button_up(self, widget, event):
        if self._long_press_srcid:
            GLib.source_remove(self._long_press_srcid)
            self._long_press_srcid = None

    def _longpress_activated_cb(self):
        self.set_text("Long-press received")

if __name__ == '__main__':
    import os, sys
    widget = LongPressLabel("Press and hold")
    window = Gtk.Window()
    window.add(widget)
    window.set_title(os.path.basename(sys.argv[0]))
    window.connect("destroy", lambda *a: Gtk.main_quit())
    window.set_size_request(250, 200)
    window.show_all()
    Gtk.main()


Top
 Profile  
 
 Post subject: Re: long press support in gtk
PostPosted: Thu Aug 15, 2013 2:35 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
The press and release above should do the trick. To be honest you can probably do it simpler by just checking that press and release were done on the same object (i.e. without the timeout). For some contexts (as mentioned above you really should explain what you are doing more fully) just a release event should do.
Are you trying to make your code relevant for touch screens?
You should take a look into ATK as it does a lot of the interfacing with accessible technology and would be more relevant to push such code there. You may find it's already set up to deal with what you want.


Top
 Profile  
 
 Post subject: Re: long press support in gtk
PostPosted: Wed Sep 18, 2013 9:46 am 
Offline
Familiar Face

Joined: Wed Jul 03, 2013 4:52 am
Posts: 9
Have a look at the source code for GtkScaleButton. It handles long presses.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group