Friday, March 2, 2007

motion-event handling in gtk

I'm working on a Ruby/GTK app where I need to track the position of the cursor over a widget at all times. Clearly the 'motion-notify-event' was the ticket, but my event-handler wasn't being called after connecting the signal to my Gtk::EventBox. Turns out I had been adding the Gdk::Event::POINTER_MOTION_HINT_MASK to my widgets event mask. The signal-handler would be called only if I clicked and dragged on the event box. I messed around with that for a few hours with no luck, until I finally found this Python example that was adding both POINTER_MOTION_HINT_MASK and POINTER_MOTION_MASK to the EventBox's event mask.

The signal-handler was working wonderfully at that point, but I wasn't sure if I was getting the event from the MOTION_MASK or the MOTION_HINT_MASK. To test it I made the following signal-handler:

def handle_map_motion(w, e, handler_id)
puts "HINT!!!!" if e.hint?
puts "Moving over (#{e.x}, #{e.y})"
end

Here's the output:

HINT!!!!
Moving over (372.0, 277.0)
HINT!!!!
Moving over (386.0, 260.0)
HINT!!!!
Moving over (478.0, 410.0)
HINT!!!!

So in summary, when you want to detect mouse movement in GTK, make sure that both POINTER_MOTION_MASK and POINTER_MOTION_HINT_MASK are in your event mask, and you'll be good to go.

No comments: