Distinguishing click from drag


#1

I have a TextButton subclass that I would like the user to be able to drag. Using the sample code from the DragAndDrop example, it’s easy to get the button to drag to the desktop, as I want it to, but I just realized that in overriding mouseDown(), I killed the button…it no longer responds to clicks, but instead assumes that every click is the beginning of a drag.

Is there a good way to distinguish a click on a component that is simply a click, and a click that is the beginning of a drag? I’m looking for something that might tell me that the button has been down for longer than x milliseconds, and that the mouse moved while it was down.


#2

MouseEvent::mouseWasClicked() ? You can’t do it in the mouseDown, though - it’d have to be done in the drag or mouse-up.


#3

[quote=“igor”]
Is there a good way to distinguish a click on a component that is simply a click, and a click that is the beginning of a drag? I’m looking for something that might tell me that the button has been down for longer than x milliseconds, and that the mouse moved while it was down.[/quote]

This is what I found in documentation, maybe it could help you in what you want to do…

int MouseEvent::getLengthOfMousePress ( ) const throw ()
[i]
Returns the time that the mouse button has been held down for.

If called from a mouseDrag or mouseUp callback, this will return the number of milliseconds since the corresponding mouseDown event occurred. If called in other contexts, e.g. a mouseMove, then the returned value may be 0 or an undefined value.
[/i]

Hope this helps a little.
Y.


#4

Can this be called in mouseDown()? Jules’ post suggested that MouseEvent calls couldn’t be done while in that function.

The Mac system call that does this is WaitMouseMoved(), which returns true if the mouse moves away from the initial location before the mouse button is released.


#5

I was just thinking that another solution would be a new routine that could be overridden, like a startMouseDrag(), during which I could handle things that only needed to be done once, as a drag was starting. That’s what I’m doing now in mouseDown().


#6

You can call it in mouseDown, but of course it’ll just return 0, because the mouse has just been pressed! Use it in mouseDrag to find out when a click becomes a drag.

And mouseWasMoved does a much better job than just using getLengthOfMousePress.


#7

mouseWasMoved? Do you mean the wasMovedSinceMouseDown member? That variable is always false in mouseDown(), and true in mouseDrag(), so it doesn’t help me initialize things at the start of a drag.

Perhaps some psudo-code will help illustrate what I’m trying to do:

void myButton::mouseDown(MouseEvent e){
    if (!e.mouseWasClicked()){    //start of a drag
       myComponent = new myComponent();
       myComponentDragger.startDraggingComponent(myComponent, 0);
       }
    else
      Button::mouseDown(e);   //a click on the button
}

If I could figure out from inside mouseDrag() when I’m at the start of a new drag, I could do the initialization there instead.


#8

I meant mouseWasClicked(), like I said in my first post.

How can you possibly expect to know in the mouseDown what the user is going to do?? They’ve not done anything at that point, and the MouseEvent class isn’t psychic!


#9

Ah, sorry to have been a thickie. I understand now. Thanks!


#10