View Single Post
Posts: 146 | Thanked: 15 times | Joined on Oct 2008
#16
Hi daperl,
I solved the problem. It was actually tricky needing three modifications, and I don't quite understand how this works. I post it here cause it could be useful to others.

Part of the solution was pointed out by this small drawing script:

http://www.pygtk.org/pygtk2tutorial/...ibblesimple.py

Here, it is used a few lines of a property called "HINT". In my scrolling function, it reads:

Code:
        if event.is_hint:
            xpos, ypos, state = \
                             event.window.get_pointer()
        else:
            xpos = event.x
            ypos = event.y
            state = event.state
        if state & gtk.gdk.BUTTON1_MASK:
             ...
So there is a 'is_hint' property that should be caught. This is the thing I don't understand. To link to this property, it was needed to create the event:

Code:
event_box.set_events(gtk.gdk.POINTER_MOTION_HINT_MASK)
And the third crucial modification was to unset the default positions if scrolling fails, with:

Code:
self.oldxpos = -1
self.oldypos = -1
So, the final complete code (of the 3 relevant parts) looks like this:

Code:
    def scrollimg(self, swi, event):
    "The method that actually scrolls"
        if event.is_hint:
            xpos, ypos, state = \
                      event.window.get_pointer()
        else:
            xpos = event.x
            ypos = event.y
            state = event.state
        if state & gtk.gdk.BUTTON1_MASK:
            swih = swi.props.hadjustment
            swiv = swi.props.vadjustment
            if xpos != self.oldxpos:
               aux = swih.value + self.oldxpos - xpos
               if swih.lower <= aux and aux <= \
                        swih.upper - swih.page_size:
                    swih.value = aux
            if ypos != self.oldypos:
               aux = swiv.value + self.oldypos - ypos
               if swiv.lower <= aux and aux <= \
                       swiv.upper - swiv.page_size:
                    swiv.value = aux
        else:
            self.oldxpos = -1
            self.oldypos = -1

    def pressimg(self, swi, event):
    "The method that sets the initial positions"
        self.oldxpos = event.x
        self.oldypos = event.y

def __init__(self):
          ...
        self.mpics_scroll = gtk.ScrolledWindow(None, None)
        event_box = gtk.EventBox()
        self.mpics = gtk.Image()
        event_box.add(self.mpics)
        event_box.set_above_child(True)
        self.mpics_scroll.add_with_viewport\
             (event_box)

        self.mpics_scroll.connect\
             ("button-press-event", self.pressimg)
        self.mpics_scroll.connect
             ("motion-notify-event", self.scrollimg)
        event_box.set_events\
             (gtk.gdk.POINTER_MOTION_HINT_MASK)

         ....
Hope this helps to others, and thanks for the help,
L.

Last edited by luis; 2011-02-24 at 03:01. Reason: Formatting
 

The Following User Says Thank You to luis For This Useful Post: