How to use Button Motion event for dragging text

Hello,

I’m currently using the motion_notify event via a GDK_BUTTON_MOTION_MASK on a GtkScrolledWindow to detect when a user is pressing the left mouse button down while moving the cursor across a GtkDrawingArea. I connected the motion_notify event like so (sorry that it’s in Rust, I hope it’s understandable nonetheless):

if em.get_state().contains(ModifierType::BUTTON1_MASK) {
            let (x, y) = em.get_position();
            let (col, line) = self.da_px_to_cell(x, y);
            self.core.drag(self.view_id, line, col);
}
Inhibit(false)

This works fine normally,but at some point (I can’t reproduce what the trigger is) BUTTON1_MASK is always contained in the EventMotion, even if the user doesn’t hold down the left mouse button and simply moves their mouse (and as such text is selected even without holding down the mouse button :/). I’m a bit confused as to why that happens - maybe one of you has a clue? Should I use https://developer.gnome.org/gtk3/stable/GtkGestureDrag.html instead for this?

See https://gitlab.gnome.org/World/Tau/blob/master/src/editview/src/edit_view.rs#L929 for the code.

Gtk3, or already Gtk4? Exotic OS?

Are you absolutely sure your Hardware (Mouse) is fully OK? Some years ago I had issues with random double clicks – I only noticed them in my own Apps, the schematics editor written in Ruby-gtk, and some months later in my Nim editor. Was only my old mouse hardware.

But I have never seen what you are describing, such random behaviour is really ugly to debug. I can not imagine that it is due to the Rust GTK bindings…

For the gestures, ebassi was recently recommending using then, so we may try. I have still to learn how it works.

This is GTK3 on Alpine Linux, so I wouldn’t call this exotic.

Are you absolutely sure your Hardware (Mouse) is fully OK? Some years ago I had issues with random double clicks – I only noticed them in my own Apps, the schematics editor written in Ruby-gtk, and some months later in my Nim editor. Was only my old mouse hardware.

Hm, I’m reasonably sure my hardware is fine since whenever I trigger this bug in Tau it only happens in it, other applications aren’t affected by this.

Huh, I think I might have just found out the trigger while writing that response. It seems to only happen after I first dragged, after that it always drags even if I don’t hold down the left mouse button. Not quite sure why BUTTON1_MASK is contained in the EventMotion though or why GTK even sends me the motion_notify event then though, since I’ve only GDK_BUTTON_MOTION_MASK enabled and not GDK_POINTER_MOTION_MASK

Hm, seems like returning Inhibit(true) from connect_motion_notify_event (meaning GTK’s built-in handlers won’t be invoked) seems to fix it for me, but I’m not quite sure if this really is a proper solution to this.

Is that GDK_EVENT_PROPAGATE vs GDK_EVENT_STOP from

It’s the same as in https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-drag-motion :

Returns

TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.

Inhibit(true) maps to TRUE and Inhibit(false) to FALSE here

I can’t reproduce the problem at d6556429; is there a specific procedure to trigger it?

I do recommend using GtkGestureDrag.

Should be as easy to reproduce as:

  • Open Tau
  • Open a file (or type a bit)
  • drag some text
  • try to move the mouse cursor after aborting that drag, it keeps on dragging.

See https://dist.cogitri.dev/Peek%202019-08-11%2015-42.webm , I dragged at first but during scrolling I didn’t hold down the left mouse button but it dragged anyway.

I’ll try to switch to GtkGestures soon-ish, I guess, thanks for the suggestion :slight_smile:

Well, I’ve siwtched to GtkGestureDrag and that really was a breeze to switch and it works now, thank you very much for your help @StefanSalewski and @chrisaw , I really appreciate it! :slight_smile:

1 Like

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