Getting the GtkGestureDrag from GtkScrolledWindow

I have a GtkLayout which is fairly large, so it’s put inside a GtkScrolledWindow. A nice feature would be that the user can grab the canvas and move the mouse around to scroll instead of using the scrollbars. After (unsuccesfully) messing with button-press-event, button-release-event and motion-notify-event, I found GtkGestureDrag. It’s as easy as setting the touch-only property to FALSE through the inspector. But I can’t find a way to get the GtkGestureDrag from the GtkScrolledWindow through my code. Is that even possibe?

No it’s not possible but you can just attach your own gesture to your layout and let people use that and it should work.

I tried that, but it doesn’t seem to work (if I done it correctly of course!). Here’s a simple working example:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import Gdk

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(500, 500)

        button = Gtk.Button(label="Just having something in the layout")

        layout = Gtk.Layout()
        layout.set_size(1200, 1200)
        # TODO: find out which events exactly are needed
        layout.set_events(Gdk.EventMask.ALL_EVENTS_MASK)
        layout.put(button, 400, 400)

        scrolledwindow = Gtk.ScrolledWindow()
        scrolledwindow.add(layout)

        self.add(scrolledwindow)

        # XXX: enable to add a GtkGestureDrag manually
        #self.add_gesturedrag(layout)

    def add_gesturedrag(self, widget):
        g = Gtk.GestureDrag.new(widget)
        g.set_touch_only(False)
        g.connect("drag-begin", self.on_drag_begin)
        g.connect("drag-update", self.on_drag_update)
        g.connect("drag-end", self.on_drag_end)

    def on_drag_begin(self, gesture_drag, start_x, start_y):
        print("drag begin")

    def on_drag_update(self, gesture_drag, offset_x, offset_y):
        print("drag update")

    def on_drag_end(self, gesture_drag, offset_x, offset_y):
        print("drag end")


win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Running this with the inspector, setting touch-only to FALSE in there is working. Then uncomment line 26 to add our own GtkGestureDrag and it looks like it isn’t added at all. Tried both by adding with the layout and scrolledwindow.

You need to keep a reference to the gesture you created, otherwise it will be garbage collected—in GTK3, widgets do not hold a reference to the gesture.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.